X-Git-Url: https://code.grnet.gr/git/snf-image-creator/blobdiff_plain/c5effe03956a8200b923dfbc528abb80be45bdcb..9b49a63b45f9fb31e04f94e85eb4188a98f17ae1:/image_creator/os_type/windows.py diff --git a/image_creator/os_type/windows.py b/image_creator/os_type/windows.py index a2a41f2..a6816fe 100644 --- a/image_creator/os_type/windows.py +++ b/image_creator/os_type/windows.py @@ -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,21 +115,37 @@ class Windows(OSBase): 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) + self.product_name = self.image.g.inspect_get_product_name(self.root) self.syspreped = False @sysprep('Disabling IPv6 privacy extensions') @@ -195,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: @@ -250,7 +265,7 @@ class Windows(OSBase): querymax -= 100 if querymax < 0: - self.out.warn("Not enought available space to shrink the image!") + self.out.warn("Not enough available space to shrink the image!") return self.out.output("\tReclaiming %dMB ..." % querymax) @@ -265,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,7 +299,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: @@ -291,26 +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) + systemroot = self.image.g.inspect_get_windows_systemroot(self.root) try: pagefile = "%s/pagefile.sys" % systemroot - self.g.rm_rf(self.g.case_sensitive_path(pagefile)) + 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: - self.g.shutdown() - else: - self.g.kill_subprocess() - - self.out.success('done') + self.image.disable_guestfs() vm = None monitor = None @@ -381,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: @@ -426,10 +433,10 @@ 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) + path = self.image.g.case_sensitive_path(path) except RuntimeError as error: raise FatalError("Unable to retrieve registry file: %s. Reason: %s" % (regfile, str(error))) @@ -446,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) @@ -511,7 +518,7 @@ class Windows(OSBase): h.commit(None) - self.g.upload(software, path) + self.image.g.upload(software, path) finally: os.unlink(software) @@ -537,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) @@ -569,7 +576,7 @@ class Windows(OSBase): 'value': struct.pack("