X-Git-Url: https://code.grnet.gr/git/snf-image-creator/blobdiff_plain/c9d53ee235ad6d92a2407741121333fb81993256..5756c277d37278acc102b64d4493c040aabf38e1:/image_creator/image.py diff --git a/image_creator/image.py b/image_creator/image.py index ccc4f80..686fad6 100644 --- a/image_creator/image.py +++ b/image_creator/image.py @@ -33,7 +33,7 @@ # interpreted as representing official policies, either expressed # or implied, of GRNET S.A. -from image_creator.util import FatalError, check_guestfs_version +from image_creator.util import FatalError from image_creator.gpt import GPTPartitionTable from image_creator.os_type import os_cls @@ -45,17 +45,79 @@ from sendfile import sendfile class Image(object): """The instances of this class can create images out of block devices.""" - def __init__(self, device, output, meta={}): + def __init__(self, device, output, **kargs): """Create a new Image instance""" self.device = device self.out = output - self.meta = meta + + self.meta = kargs['meta'] if 'meta' in kargs else {} + self.sysprep_params = \ + kargs['sysprep_params'] if 'sysprep_params' in kargs else {} + self.progress_bar = None self.guestfs_device = None self.size = 0 self.g = guestfs.GuestFS() + self.guestfs_enabled = False + self.guestfs_version = self.g.version() + + def check_guestfs_version(self, major, minor, release): + """Checks if the version of the used libguestfs is smaller, equal or + greater than the one specified by the major, minor and release triplet + + Returns: + < 0 if the installed version is smaller than the specified one + = 0 if they are equal + > 0 if the installed one is greater than the specified one + """ + + for (a, b) in (self.guestfs_version['major'], major), \ + (self.guestfs_version['minor'], minor), \ + (self.guestfs_version['release'], release): + if a != b: + return a - b + + return 0 + + def enable(self): + """Enable a newly created Image instance""" + + self.enable_guestfs() + + self.out.output('Inspecting Operating System ...', False) + roots = self.g.inspect_os() + if len(roots) == 0: + raise FatalError("No operating system found") + if len(roots) > 1: + raise FatalError("Multiple operating systems found." + "We only support images with one OS.") + self.root = roots[0] + self.guestfs_device = self.g.part_to_dev(self.root) + self.size = self.g.blockdev_getsize64(self.guestfs_device) + self.meta['PARTITION_TABLE'] = \ + self.g.part_get_parttype(self.guestfs_device) + + self.ostype = self.g.inspect_get_type(self.root) + self.distro = self.g.inspect_get_distro(self.root) + self.out.success( + 'found a(n) %s system' % + self.ostype if self.distro == "unknown" else self.distro) + + def enable_guestfs(self): + """Enable the guestfs handler""" + + if self.guestfs_enabled: + self.out.warn("Guestfs is already enabled") + return + + # Before version 1.18.4 the behaviour of kill_subprocess was different + # and you need to reset the guestfs handler to relaunch a previously + # shut down qemu backend + if self.check_guestfs_version(1, 18, 4) < 0: + self.g = guestfs.GuestFS() + self.g.add_drive_opts(self.device, readonly=0, format="raw") # Before version 1.17.14 the recovery process, which is a fork of the @@ -63,7 +125,7 @@ class Image(object): # file descriptors. This can cause problems especially if the parent # process has opened pipes. Since the recovery process is an optional # feature of libguestfs, it's better to disable it. - if check_guestfs_version(self.g, 1, 17, 14) >= 0: + if self.check_guestfs_version(1, 17, 14) >= 0: self.out.output("Enabling recovery proc") self.g.set_recovery_proc(1) else: @@ -72,11 +134,6 @@ class Image(object): #self.g.set_trace(1) #self.g.set_verbose(1) - self.guestfs_enabled = False - - def enable(self): - """Enable a newly created Image instance""" - self.out.output('Launching helper VM (may take a while) ...', False) # self.progressbar = self.out.Progress(100, "Launching helper VM", # "percent") @@ -87,26 +144,30 @@ class Image(object): # self.g.delete_event_callback(eh) # self.progressbar.success('done') # self.progressbar = None + + if self.check_guestfs_version(1, 18, 4) < 0: + self.g.inspect_os() # some calls need this + self.out.success('done') - self.out.output('Inspecting Operating System ...', False) - roots = self.g.inspect_os() - if len(roots) == 0: - raise FatalError("No operating system found") - if len(roots) > 1: - raise FatalError("Multiple operating systems found." - "We only support images with one OS.") - self.root = roots[0] - self.guestfs_device = self.g.part_to_dev(self.root) - self.size = self.g.blockdev_getsize64(self.guestfs_device) - self.meta['PARTITION_TABLE'] = \ - self.g.part_get_parttype(self.guestfs_device) + def disable_guestfs(self): + """Disable the guestfs handler""" - self.ostype = self.g.inspect_get_type(self.root) - self.distro = self.g.inspect_get_distro(self.root) - self.out.success( - 'found a(n) %s system' % - self.ostype if self.distro == "unknown" else self.distro) + if not self.guestfs_enabled: + self.out.warn("Guestfs is already disabled") + return + + 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 self.check_guestfs_version(1, 19, 16) >= 0: + self.g.shutdown() + else: + self.g.kill_subprocess() + + self.guestfs_enabled = False + self.out.success('done') def _get_os(self): """Return an OS class instance for this image""" @@ -117,7 +178,7 @@ class Image(object): self.enable() cls = os_cls(self.distro, self.ostype) - self._os = cls(self) + self._os = cls(self, sysprep_params=self.sysprep_params) self._os.collect_metadata()