Windows OSs."""
from image_creator.os_type import OSBase, sysprep
-from image_creator.util import FatalError, check_guestfs_version
+from image_creator.util import FatalError, check_guestfs_version, get_command
import hivex
import tempfile
import os
+import time
+import random
+import string
+import subprocess
+
+kvm = get_command('kvm')
+
+BOOT_TIMEOUT = 300
class Windows(OSBase):
"""OS class for Windows"""
- @sysprep(enabled=False)
- def remove_user_accounts(self, print_header=True):
- """Remove all user accounts with id greater than 1000"""
- pass
-
+ def needed_sysprep_params(self):
+ """Returns a list of needed sysprep parameters. Each element in the
+ list is a SysprepParam object.
+ """
+
+ password = self.SysprepParam(
+ 'password', 'Image Administrator Password', 20, lambda x: True)
+
+ return [password]
+
+ @sysprep('Disabling IPv6 privacy extensions')
+ def disable_ipv6_privacy_extensions(self):
+ """Disable IPv6 privacy extensions"""
+
+ self._guest_exec('netsh interface ipv6 set global '
+ 'randomizeidentifiers=disabled store=persistent')
+
+ @sysprep('Executing sysprep on the image (may take more that 10 minutes)')
+ def microsoft_sysprep(self):
+ """Run the Microsoft System Preparation Tool on the Image. This will
+ remove system-specific data and will make the image ready to be
+ deployed. After this no other task may run.
+ """
+
+ self._guest_exec(r'C:\Windows\system32\sysprep\sysprep '
+ r'/quiet /generalize /oobe /shutdown')
+ self.syspreped = True
+
def do_sysprep(self):
"""Prepare system for image creation."""
if getattr(self, 'syspreped', False):
raise FatalError("Image is already syspreped!")
+ txt = "System preparation parameter: `%s' is needed but missing!"
+ for param in self.needed_sysprep_params():
+ if param[0] not in self.sysprep_params:
+ raise FatalError(txt % param[0])
+
self.mount(readonly=False)
try:
disabled_uac = self._update_uac_remote_setting(1)
+ token = self._enable_os_monitor()
finally:
self.umount()
self.out.success('done')
- self.out.output("Starting windows VM ...", False)
+ vm = None
+ monitor = None
try:
- pass
+ self.out.output("Starting windows VM ...", False)
+ monitorfd, monitor = tempfile.mkstemp()
+ os.close(monitorfd)
+ vm, display = self._create_vm(monitor)
+ self.out.success("started (console on vnc display: %d)." % display)
+
+ self.out.output("Waiting for OS to boot ...", False)
+ if not self._wait_on_file(monitor, token):
+ raise FatalError("Windows booting timed out.")
+ else:
+ self.out.success('done')
+
+ self.out.output("Disabling automatic logon ...", False)
+ self._disable_autologon()
+ self.out.success('done')
+
+ self.out.output('Preparing system from image creation:')
+
+ tasks = self.list_syspreps()
+ enabled = filter(lambda x: x.enabled, tasks)
+
+ size = len(enabled)
+
+ # Make sure the ms sysprep is the last task to run if it is enabled
+ enabled = filter(
+ lambda x: x.im_func.func_name != 'microsoft_sysprep', enabled)
+
+ ms_sysprep_enabled = False
+ if len(enabled) != size:
+ enabled.append(self.ms_sysprep)
+ ms_sysprep_enabled = True
+
+ cnt = 0
+ for task in enabled:
+ cnt += 1
+ self.out.output(('(%d/%d)' % (cnt, size)).ljust(7), False)
+ task()
+ setattr(task.im_func, 'executed', True)
+
+ self.out.output("Shutting down windows VM ...", False)
+ if not ms_sysprep_enabled:
+ self._shutdown()
+ self.out.success("done")
+
+ vm.wait()
finally:
+ if monitor is not None:
+ os.unlink(monitor)
+
+ if vm is not None:
+ self._destroy_vm(vm)
+
self.out.output("Relaunching helper VM (may take a while) ...",
False)
self.g.launch()
if disabled_uac:
self._update_uac_remote_setting(0)
- self.syspreped = True
+ def _create_vm(self, monitor):
+ """Create a VM with the image attached as the disk
+
+ monitor: a file to be used to monitor when the OS is up
+ """
+
+ def random_mac():
+ mac = [0x00, 0x16, 0x3e,
+ random.randint(0x00, 0x7f),
+ random.randint(0x00, 0xff),
+ random.randint(0x00, 0xff)]
+
+ return ':'.join(map(lambda x: "%02x" % x, mac))
+
+ # Use ganeti's VNC port range for a random vnc port
+ vnc_port = random.randint(11000, 14999)
+ display = vnc_port - 5900
+
+ vm = kvm('-smp', '1', '-m', '1024', '-drive',
+ 'file=%s,format=raw,cache=none,if=virtio' % self.image.device,
+ '-netdev', 'type=user,hostfwd=tcp::445-:445,id=netdev0',
+ '-device', 'virtio-net-pci,mac=%s,netdev=netdev0' %
+ random_mac(), '-vnc', ':%d' % display, '-serial',
+ 'file:%s' % monitor, _bg=True)
+
+ return vm, display
+
+ def _destroy_vm(self, vm):
+ """Destroy a VM previously created by _create_vm"""
+ if vm.process.alive:
+ vm.terminate()
+
+ def _shutdown(self):
+ """Shuts down the windows VM"""
+ self._guest_exec(r'shutdown /s /t 5')
+
+ def _wait_on_file(self, fname, msg):
+ """Wait until a message appears on a file"""
+
+ for i in range(BOOT_TIMEOUT):
+ time.sleep(1)
+ with open(fname) as f:
+ for line in f:
+ if line.startswith(msg):
+ return True
+ return False
+
+ def _disable_autologon(self):
+ """Disable automatic logon on the windows image"""
+
+ winlogon = \
+ r'"HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"'
+
+ self._guest_exec('REG DELETE %s /v DefaultUserName /f' % winlogon)
+ self._guest_exec('REG DELETE %s /v DefaultPassword /f' % winlogon)
+ self._guest_exec('REG DELETE %s /v AutoAdminLogon /f' % winlogon)
def _registry_file_path(self, regfile):
"""Retrieves the case sensitive path to a registry file"""
% (regfile, str(e)))
return path
+ def _enable_os_monitor(self):
+ """Add a script in the registry that will send a random string to the
+ first serial port when the windows image finishes booting.
+ """
+
+ token = "".join(random.choice(string.ascii_letters) for x in range(16))
+
+ path = self._registry_file_path('SOFTWARE')
+ softwarefd, software = tempfile.mkstemp()
+ try:
+ os.close(softwarefd)
+ self.g.download(path, software)
+
+ h = hivex.Hivex(software, write=True)
+
+ # Enable automatic logon.
+ # This is needed because we need to execute a script that we add in
+ # the RunOnce registry entry and those programs only get executed
+ # when a user logs on. There is a RunServicesOnce registry entry
+ # whose keys get executed in the background when the logon dialog
+ # box first appears, but they seem to only work with services and
+ # not arbitrary command line expressions :-(
+ #
+ # Instructions on how to turn on automatic logon in Windows can be
+ # found here: http://support.microsoft.com/kb/324737
+ #
+ # Warning: Registry change will not work if the “Logon Banner” is
+ # defined on the server either by a Group Policy object (GPO) or by
+ # a local policy.
+
+ winlogon = h.root()
+ for child in ('Microsoft', 'Windows NT', 'CurrentVersion',
+ 'Winlogon'):
+ winlogon = h.node_get_child(winlogon, child)
+
+ h.node_set_value(
+ winlogon,
+ {'key': 'DefaultUserName', 't': 1,
+ 'value': "Administrator".encode('utf-16le')})
+ h.node_set_value(
+ winlogon,
+ {'key': 'DefaultPassword', 't': 1,
+ 'value': self.sysprep_params['password'].encode('utf-16le')})
+ h.node_set_value(
+ winlogon,
+ {'key': 'AutoAdminLogon', 't': 1,
+ 'value': "1".encode('utf-16le')})
+
+ key = h.root()
+ for child in ('Microsoft', 'Windows', 'CurrentVersion'):
+ key = h.node_get_child(key, child)
+
+ runonce = h.node_get_child(key, "RunOnce")
+ if runonce is None:
+ runonce = h.node_add_child(key, "RunOnce")
+
+ value = (
+ r'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe '
+ r'-ExecutionPolicy RemoteSigned '
+ r'"&{$port=new-Object System.IO.Ports.SerialPort COM1,9600,'
+ r'None,8,one;$port.open();$port.WriteLine(\"' + token + r'\");'
+ r'$port.Close()}"').encode('utf-16le')
+
+ h.node_set_value(runonce,
+ {'key': "BootMonitor", 't': 1, 'value': value})
+
+ h.commit(None)
+
+ self.g.upload(software, path)
+ finally:
+ os.unlink(software)
+
+ return token
+
def _update_uac_remote_setting(self, value):
"""Updates the registry key value:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies
# Filter out the guest account
return filter(lambda x: x != "Guest", users)
+ def _guest_exec(self, command, fatal=True):
+ """Execute a command on a windows VM"""
+
+ user = "Administrator%" + self.sysprep_params['password']
+ addr = 'localhost'
+ runas = '--runas=%s' % user
+ winexe = subprocess.Popen(
+ ['winexe', '-U', user, "//%s" % addr, runas, command],
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+
+ stdout, stderr = winexe.communicate()
+ rc = winexe.poll()
+
+ if rc != 0 and fatal:
+ reason = stderr if len(stderr) else stdout
+ raise FatalError("Command: `%s' failed. Reason: %s" %
+ (command, reason))
+
+ return (stdout, stderr, rc)
+
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :