Fix version check introduced in aca3e6508c7ae3f5ee
[snf-image-creator] / image_creator / os_type / windows.py
index ad2d202..96efdab 100644 (file)
@@ -36,8 +36,8 @@
 """This module hosts OS-specific code common for the various Microsoft
 Windows OSs."""
 
-from image_creator.os_type import OSBase, sysprep
-from image_creator.util import FatalError, check_guestfs_version, get_command
+from image_creator.os_type import OSBase, sysprep, add_sysprep_param
+from image_creator.util import FatalError, get_kvm_binary
 from image_creator.winexe import WinEXE, WinexeTimeout
 
 import hivex
@@ -50,10 +50,6 @@ import string
 import subprocess
 import struct
 
-BOOT_TIMEOUT = 300
-SHUTDOWN_TIMEOUT = 120
-CONNECTION_RETRIES = 5
-
 # For more info see: http://technet.microsoft.com/en-us/library/jj612867.aspx
 KMS_CLIENT_SETUP_KEYS = {
     "Windows 8 Professional": "NG4HW-VH26C-733KW-K6F98-J8CK4",
@@ -104,36 +100,53 @@ KMS_CLIENT_SETUP_KEYS = {
     "Windows Server 2008 for Itanium-Based Systems":
     "4DWFP-JF3DJ-B7DTH-78FJB-PDRHK"}
 
+_POSINT = lambda x: type(x) == int and x >= 0
+
 
 class Windows(OSBase):
     """OS class for Windows"""
+    @add_sysprep_param(
+        'shutdown_timeout', int, 120, "Shutdown Timeout (seconds)", _POSINT)
+    @add_sysprep_param(
+        'boot_timeout', int, 300, "Boot Timeout (seconds)", _POSINT)
+    @add_sysprep_param(
+        'connection_retries', int, 5, "Connection Retries", _POSINT)
+    @add_sysprep_param('password', str, None, 'Image Administrator Password')
     def __init__(self, image, **kargs):
         super(Windows, self).__init__(image, **kargs)
 
-        device = self.g.part_to_dev(self.root)
-
-        self.last_part_num = self.g.part_list(device)[-1]['part_num']
+        # The commit with the following message was added in
+        # libguestfs 1.17.18 and was backported in version 1.16.11:
+        #
+        # When a Windows guest doesn't have a HKLM\SYSTEM\MountedDevices node,
+        # inspection fails.  However inspection should not completely fail just
+        # because we cannot get the drive letter mapping from a guest.
+        #
+        # Since Microsoft Sysprep removes the aforementioned key, image
+        # creation for windows can only be supported if the installed guestfs
+        # version is 1.17.18 or higher
+        if self.image.check_guestfs_version(1, 17, 18) < 0 and \
+                (self.image.check_guestfs_version(1, 17, 0) >= 0 or
+                 self.image.check_guestfs_version(1, 16, 11) < 0):
+            raise FatalError(
+                'For windows support libguestfs 1.16.11 or above is required')
+
+        device = self.image.g.part_to_dev(self.root)
+
+        self.last_part_num = self.image.g.part_list(device)[-1]['part_num']
         self.last_drive = None
         self.system_drive = None
 
-        for drive, partition in self.g.inspect_get_drive_mappings(self.root):
-            if partition == "%s%d" % (device, self.last_part_num):
+        for drive, part in self.image.g.inspect_get_drive_mappings(self.root):
+            if part == "%s%d" % (device, self.last_part_num):
                 self.last_drive = drive
-            if partition == self.root:
+            if part == self.root:
                 self.system_drive = drive
 
         assert self.system_drive
 
-        self.product_name = self.g.inspect_get_product_name(self.root)
-
-    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]
+        self.product_name = self.image.g.inspect_get_product_name(self.root)
+        self.syspreped = False
 
     @sysprep('Disabling IPv6 privacy extensions')
     def disable_ipv6_privacy_extensions(self):
@@ -197,7 +210,7 @@ class Windows(OSBase):
         """Install the appropriate KMS client setup key to the image to convert
         it to a KMS client. Computers that are running volume licensing
         editions of Windows 8, Windows Server 2012, Windows 7, Windows Server
-        2008 R2, Windows Vista, and Windows Server 2008 are, by default, KMS
+        2008 R2, Windows Vista, and Windows Server 2008 are by default KMS
         clients with no additional configuration needed.
         """
         try:
@@ -209,7 +222,7 @@ class Windows(OSBase):
             return
 
         self._guest_exec(
-            "cscript \Windows\system32\slmgr.vbs /ipk %s" % setup_key)
+            r"cscript \Windows\system32\slmgr.vbs /ipk %s" % setup_key)
 
     @sysprep('Shrinking the last filesystem')
     def shrink(self):
@@ -267,8 +280,12 @@ class Windows(OSBase):
             r'IF NOT !ERRORLEVEL! EQU 0 EXIT /B 1 & ' +
             r'DEL /Q %SCRIPT%"')
 
-        stdout, stderr, rc = self._guest_exec(cmd)
+        stdout, stderr, rc = self._guest_exec(cmd, False)
 
+        if rc != 0:
+            FatalError("Shrinking failed. Please make sure the media is "
+                       "defraged with a command like this: "
+                       "`Defrag.exe /U /X /W'")
         for line in stdout.splitlines():
             if line.find('shrunk') >= 0:
                 self.out.output(line)
@@ -280,9 +297,9 @@ class Windows(OSBase):
             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])
+        for name, param in self.needed_sysprep_params.items():
+            if name not in self.sysprep_params:
+                raise FatalError(txt % name)
 
         self.mount(readonly=False)
         try:
@@ -293,23 +310,17 @@ class Windows(OSBase):
             firewall_states = self._update_firewalls(0, 0, 0)
 
             # Delete the pagefile. It will be recreated when the system boots
-            systemroot = self.g.inspect_get_windows_systemroot(self.root)
-            pagefile = "%s/pagefile.sys" % systemroot
-            self.g.rm_rf(self.g.case_sensitive_path(pagefile))
+            systemroot = self.image.g.inspect_get_windows_systemroot(self.root)
+            try:
+                pagefile = "%s/pagefile.sys" % systemroot
+                self.image.g.rm_rf(self.image.g.case_sensitive_path(pagefile))
+            except RuntimeError:
+                pass
 
         finally:
             self.umount()
 
-        self.out.output("Shutting down helper VM ...", False)
-        self.g.sync()
-        # guestfs_shutdown which is the prefered way to shutdown the backend
-        # process was introduced in version 1.19.16
-        if check_guestfs_version(self.g, 1, 19, 16) >= 0:
-            ret = self.g.shutdown()
-        else:
-            ret = self.g.kill_subprocess()
-
-        self.out.success('done')
+        self.image.disable_guestfs()
 
         vm = None
         monitor = None
@@ -317,7 +328,7 @@ class Windows(OSBase):
             self.out.output("Starting windows VM ...", False)
             monitorfd, monitor = tempfile.mkstemp()
             os.close(monitorfd)
-            vm = _VM(self.image.device, monitor)
+            vm = _VM(self.image.device, monitor, self.sysprep_params)
             self.out.success("started (console on vnc display: %d)." %
                              vm.display)
 
@@ -336,22 +347,19 @@ class Windows(OSBase):
             self.out.output('Preparing system for image creation:')
 
             tasks = self.list_syspreps()
-            enabled = filter(lambda x: x.enabled, tasks)
+            enabled = [task for task in tasks if task.enabled]
             size = len(enabled)
 
             # Make sure shrink runs in the end, before ms sysprep
-            enabled = filter(lambda x: self.sysprep_info(x).name != 'shrink',
-                             enabled)
+            enabled = [task for task in enabled if
+                       self.sysprep_info(task).name != 'shrink']
 
-            shrink_enabled = False
             if len(enabled) != size:
                 enabled.append(self.shrink)
-                shrink_enabled = True
 
             # Make sure the ms sysprep is the last task to run if it is enabled
-            enabled = filter(
-                lambda x: self.sysprep_info(x).name != 'microsoft-sysprep',
-                enabled)
+            enabled = [task for task in enabled if
+                       self.sysprep_info(task).name != 'microsoft-sysprep']
 
             ms_sysprep_enabled = False
             if len(enabled) != size:
@@ -371,7 +379,7 @@ class Windows(OSBase):
             self.out.success("done")
 
             self.out.output("Waiting for windows to shut down ...", False)
-            vm.wait(SHUTDOWN_TIMEOUT)
+            vm.wait(self.sysprep_params['shutdown_timeout'])
             self.out.success("done")
         finally:
             if monitor is not None:
@@ -383,10 +391,7 @@ class Windows(OSBase):
                     vm.destroy()
                     self.out.success("done")
             finally:
-                self.out.output("Relaunching helper VM (may take a while) ...",
-                                False)
-                self.g.launch()
-                self.out.success('done')
+                self.image.enable_guestfs()
 
                 self.mount(readonly=False)
                 try:
@@ -404,7 +409,7 @@ class Windows(OSBase):
     def _wait_vm_boot(self, vm, fname, msg):
         """Wait until a message appears on a file or the vm process dies"""
 
-        for i in range(BOOT_TIMEOUT):
+        for _ in range(self.sysprep_params['boot_timeout']):
             time.sleep(1)
             with open(fname) as f:
                 for line in f:
@@ -428,13 +433,13 @@ class Windows(OSBase):
     def _registry_file_path(self, regfile):
         """Retrieves the case sensitive path to a registry file"""
 
-        systemroot = self.g.inspect_get_windows_systemroot(self.root)
+        systemroot = self.image.g.inspect_get_windows_systemroot(self.root)
         path = "%s/system32/config/%s" % (systemroot, regfile)
         try:
-            path = self.g.case_sensitive_path(path)
-        except RuntimeError as e:
+            path = self.image.g.case_sensitive_path(path)
+        except RuntimeError as error:
             raise FatalError("Unable to retrieve registry file: %s. Reason: %s"
-                             % (regfile, str(e)))
+                             % (regfile, str(error)))
         return path
 
     def _enable_os_monitor(self):
@@ -448,7 +453,7 @@ class Windows(OSBase):
         softwarefd, software = tempfile.mkstemp()
         try:
             os.close(softwarefd)
-            self.g.download(path, software)
+            self.image.g.download(path, software)
 
             h = hivex.Hivex(software, write=True)
 
@@ -513,7 +518,7 @@ class Windows(OSBase):
 
             h.commit(None)
 
-            self.g.upload(software, path)
+            self.image.g.upload(software, path)
         finally:
             os.unlink(software)
 
@@ -539,7 +544,7 @@ class Windows(OSBase):
         systemfd, system = tempfile.mkstemp()
         try:
             os.close(systemfd)
-            self.g.download(path, system)
+            self.image.g.download(path, system)
 
             h = hivex.Hivex(system, write=True)
 
@@ -571,7 +576,7 @@ class Windows(OSBase):
                            'value': struct.pack("<I", new_values.pop(0))})
 
             h.commit(None)
-            self.g.upload(system, path)
+            self.image.g.upload(system, path)
 
         finally:
             os.unlink(system)
@@ -600,7 +605,7 @@ class Windows(OSBase):
         softwarefd, software = tempfile.mkstemp()
         try:
             os.close(softwarefd)
-            self.g.download(path, software)
+            self.image.g.download(path, software)
 
             h = hivex.Hivex(software, write=True)
 
@@ -627,7 +632,7 @@ class Windows(OSBase):
             h.node_set_value(key, new_value)
             h.commit(None)
 
-            self.g.upload(software, path)
+            self.image.g.upload(software, path)
 
         finally:
             os.unlink(software)
@@ -641,11 +646,10 @@ class Windows(OSBase):
 
     def _get_users(self):
         """Returns a list of users found in the images"""
-        path = self._registry_file_path('SAM')
         samfd, sam = tempfile.mkstemp()
         try:
             os.close(samfd)
-            self.g.download(path, sam)
+            self.image.g.download(self._registry_file_path('SAM'), sam)
 
             h = hivex.Hivex(sam)
 
@@ -695,11 +699,17 @@ class Windows(OSBase):
     def _check_connectivity(self):
         """Check if winexe works on the Windows VM"""
 
+        retries = self.sysprep_params['connection_retries']
+        # If the connection_retries parameter is set to 0 disable the
+        # connectivity check
+        if retries == 0:
+            return True
+
         passwd = self.sysprep_params['password']
         winexe = WinEXE('Administrator', passwd, 'localhost')
         winexe.uninstall().debug(9)
 
-        for i in range(CONNECTION_RETRIES):
+        for i in range(retries):
             (stdout, stderr, rc) = winexe.run('cmd /C')
             if rc == 0:
                 return True
@@ -708,11 +718,12 @@ class Windows(OSBase):
                 log.file.write(stdout)
             finally:
                 log.close()
-            self.out.output("failed! See: `%' for the full output" % log.name)
-            if i < CONNECTION_RETRIES - 1:
-                self.out.output("Retrying ...", False)
-        raise FatalError("Connection to the VM failed after %d retries" %
-                         CONNECTION_RETRIES)
+            self.out.output("failed! See: `%s' for the full output" % log.name)
+            if i < retries - 1:
+                self.out.output("retrying ...", False)
+
+        raise FatalError("Connection to the Windows VM failed after %d retries"
+                         % retries)
 
     def _guest_exec(self, command, fatal=True):
         """Execute a command on a windows VM"""
@@ -739,7 +750,7 @@ class Windows(OSBase):
 
 class _VM(object):
     """Windows Virtual Machine"""
-    def __init__(self, disk, serial):
+    def __init__(self, disk, serial, params):
         """Create _VM instance
 
             disk: VM's hard disk
@@ -748,25 +759,35 @@ class _VM(object):
 
         self.disk = disk
         self.serial = serial
+        self.params = params
 
         def random_mac():
+            """creates a random mac address"""
             mac = [0x00, 0x16, 0x3e,
                    random.randint(0x00, 0x7f),
                    random.randint(0x00, 0xff),
                    random.randint(0x00, 0xff)]
 
-            return ':'.join(map(lambda x: "%02x" % x, mac))
+            return ':'.join(['%02x' % x for x in mac])
 
         # Use ganeti's VNC port range for a random vnc port
         self.display = random.randint(11000, 14999) - 5900
 
-        args = [
-            'kvm', '-smp', '1', '-m', '1024', '-drive',
+        kvm, needed_args = get_kvm_binary()
+
+        if kvm is None:
+            FatalError("Can't find the kvm binary")
+
+        args = [kvm]
+        args.extend(needed_args)
+
+        args.extend([
+            '-smp', '1', '-m', '1024', '-drive',
             'file=%s,format=raw,cache=unsafe,if=virtio' % self.disk,
             '-netdev', 'type=user,hostfwd=tcp::445-:445,id=netdev0',
             '-device', 'virtio-net-pci,mac=%s,netdev=netdev0' % random_mac(),
             '-vnc', ':%d' % self.display, '-serial', 'file:%s' % self.serial,
-            '-monitor', 'stdio']
+            '-monitor', 'stdio'])
 
         self.process = subprocess.Popen(args, stdin=subprocess.PIPE,
                                         stdout=subprocess.PIPE)
@@ -787,12 +808,11 @@ class _VM(object):
             if self.isalive():
                 self.process.kill()
             self.process.wait()
-            self.out.output("timed-out")
             raise FatalError("VM destroy timed-out")
 
         signal.signal(signal.SIGALRM, handler)
 
-        signal.alarm(SHUTDOWN_TIMEOUT)
+        signal.alarm(self.params['shutdown_timeout'])
         self.process.communicate(input="system_powerdown\n")
         signal.alarm(0)