Convert check_guestfs_version into an Image method
[snf-image-creator] / image_creator / os_type / windows.py
index 9b5d79f..4f427d6 100644 (file)
@@ -37,8 +37,7 @@
 Windows OSs."""
 
 from image_creator.os_type import OSBase, sysprep, add_sysprep_param
-from image_creator.util import FatalError, check_guestfs_version, \
-    get_kvm_binary
+from image_creator.util import FatalError, get_kvm_binary
 from image_creator.winexe import WinEXE, WinexeTimeout
 
 import hivex
@@ -116,15 +115,19 @@ class Windows(OSBase):
     def __init__(self, image, **kargs):
         super(Windows, self).__init__(image, **kargs)
 
-        # This commit was added in libguestfs 1.17.18 and is critical because
-        # Microsoft Sysprep removes this key:
+        # The commit with the following message was added in
+        # libguestfs 1.17.18:
         #
         # 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.
-        if check_guestfs_version(self.image.g, 1, 17, 18) < 0:
+        #
+        # 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:
             raise FatalError(
-                'For windows support libguestfs 1.17.17 or above is needed')
+                'For windows support libguestfs 1.17.18 or above is required')
 
         device = self.image.g.part_to_dev(self.root)
 
@@ -275,8 +278,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)
@@ -764,18 +771,21 @@ class _VM(object):
         # Use ganeti's VNC port range for a random vnc port
         self.display = random.randint(11000, 14999) - 5900
 
-        kvm = get_kvm_binary()
+        kvm, needed_args = get_kvm_binary()
 
         if kvm is None:
             FatalError("Can't find the kvm binary")
 
-        args = [
-            kvm, '-smp', '1', '-m', '1024', '-drive',
+        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)