Print a meaningful msg if windows shrinking fails
[snf-image-creator] / image_creator / os_type / windows.py
index 3592500..5d56a05 100644 (file)
@@ -116,16 +116,26 @@ 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:
+        #
+        # 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:
+            raise FatalError(
+                'For windows support libguestfs 1.17.18 or above is needed')
+
         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.image.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
@@ -265,8 +275,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,7 +294,7 @@ class Windows(OSBase):
         txt = "System preparation parameter: `%s' is needed but missing!"
         for name, param in self.needed_sysprep_params.items():
             if name not in self.sysprep_params:
-                raise FatalError(txt % param)
+                raise FatalError(txt % name)
 
         self.mount(readonly=False)
         try:
@@ -301,16 +315,7 @@ class Windows(OSBase):
         finally:
             self.umount()
 
-        self.out.output("Shutting down helper VM ...", False)
-        self.image.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:
-            self.image.g.shutdown()
-        else:
-            self.image.g.kill_subprocess()
-
-        self.out.success('done')
+        self.image.disable_guestfs()
 
         vm = None
         monitor = None
@@ -381,10 +386,7 @@ class Windows(OSBase):
                     vm.destroy()
                     self.out.success("done")
             finally:
-                self.out.output("Relaunching helper VM (may take a while) ...",
-                                False)
-                self.image.g.launch()
-                self.out.success('done')
+                self.image.enable_guestfs()
 
                 self.mount(readonly=False)
                 try: