def _prepare_kvm_export(self, image_path, download_dir): """ Prepare a KVM export (For OpenStack) Will also remove Euca-Specific files and add OS-Specific files """ (kernel_dir, ramdisk_dir, mount_point) = self._export_dirs(download_dir) #Labeling the image as 'root' allows for less reliance on UUID run_command(['e2label', image_path, 'root']) out, err = mount_image(image_path, mount_point) if err: raise Exception("Encountered errors mounting the image: %s" % err) xen_to_kvm_centos(mount_point) #Determine the latest (KVM) ramdisk to use latest_rmdisk, rmdisk_version = get_latest_ramdisk(mount_point) #Copy new kernel & ramdisk to the folder local_ramdisk_path = _copy_ramdisk(mount_point, rmdisk_version, ramdisk_dir) local_kernel_path = _copy_kernel(mount_point, rmdisk_version, kernel_dir) run_command(["umount", mount_point]) #Your new image is ready for upload to OpenStack return (image_path, local_kernel_path, local_ramdisk_path)
def mount_and_clean(image_path, mount_point, created_by=None, status_hook=None, method_hook=None, **kwargs): """ Clean the local image at <image_path> Mount it to <mount_point> """ #Prepare the paths if not os.path.exists(image_path): logger.error("Could not find local image!") raise Exception("Image file not found") if not os.path.exists(mount_point): os.makedirs(mount_point) #FSCK the image, FIRST! fsck_image(image_path) #Mount the directory #NOTE: the 'nbd_device' is not being properly passed through here. As a result, the FINAL umount does not use `qemu-nbd -d` result, nbd_device = mount_image(image_path, mount_point) if not result: raise Exception("Encountered errors mounting the image: %s" % image_path) if status_hook and hasattr(status_hook, 'on_update_status'): status_hook.on_update_status("mounted + cleaning image") try: #Required cleaning remove_user_data(mount_point, author=created_by) remove_atmo_data(mount_point) remove_vm_specific_data(mount_point) #Filesystem cleaning (From within the image) file_hook_cleaning(mount_point, **kwargs) #Driver specific cleaning if method_hook: method_hook(image_path, mount_point, **kwargs) #Required for atmosphere atmo_required_files(mount_point) #TODO: call `df -h <mount_point>` and record the `Use%` #TODO: IF `Use%` > 90%, set 'new_image=True' if status_hook and hasattr(status_hook, 'on_update_status'): status_hook.on_update_status("cleaning completed") except Exception as exc: if status_hook and hasattr(status_hook, 'on_update_status'): status_hook.on_update_status("cleaning failed - %s" % exc) finally: #Don't forget to unmount! run_command(['umount', '-lf', mount_point], check_return=True) if nbd_device: run_command(['qemu-nbd', '-d', nbd_device]) return True
def mount_and_clean(self, image_path, mount_point, *args, **kwargs): """ Clean the local image at <image_path> Mount it to <mount_point> """ #Prepare the paths if not os.path.exists(image_path): logger.error("Could not find local image!") raise Exception("Image file not found") if not os.path.exists(mount_point): os.makedirs(mount_point) #FSCK the image, FIRST! fsck_image(image_path) #Mount the directory #NOTE: the 'nbd_device' is not being properly passed through here. As a result, the FINAL umount does not use `qemu-nbd -d` result, nbd_device = mount_image(image_path, mount_point) if getattr(self, 'hook') and hasattr(self.hook, 'on_update_status'): self.hook.on_update_status("mount + cleaning image") if not result: raise Exception("Encountered errors mounting the image: %s" % image_path) try: #Required cleaning remove_user_data(mount_point, author=kwargs.get('created_by')) remove_atmo_data(mount_point) remove_vm_specific_data(mount_point) #Filesystem cleaning (From within the image) self.file_hook_cleaning(mount_point, **kwargs) #Driver specific cleaning self.clean_hook(image_path, mount_point, *args, **kwargs) #Required for atmosphere atmo_required_files(mount_point) #TODO: call `df -h <mount_point>` and record the `Use%` #TODO: IF `Use%` > 90%, set 'new_image=True' finally: #Don't forget to unmount! run_command(['umount', mount_point]) if nbd_device: run_command(['qemu-nbd', '-d', nbd_device]) if getattr(self, 'hook') and hasattr(self.hook, 'on_update_status'): self.hook.on_update_status("cleaning completed") #TODO: If `new_image=True` # replace disk image location with the 'new, larger disk' return
def convert(cls, image_path, upload_dir): (kernel_dir, ramdisk_dir, mount_point) = build_imaging_dirs(upload_dir, full_image=True) apply_label(image_path, label='root') # TODO: Is this necessary? try: out, err = mount_image(image_path, mount_point) if err: raise Exception("Encountered errors mounting image:%s" % err) #Our mount_point is in use, the image is mounted at this path mounted_path = mount_point distro = check_distro(mounted_path) #Hooks for debian/rhel specific cleaning commands if distro == 'ubuntu': cls.debian_mount(image_path, mount_point) elif distro == 'centos': cls.rhel_mount(image_path, mount_point) try: prepare_chroot_env(mounted_path) #Hooks for debian/rhel specific chroot commands if distro == 'ubuntu': cls.debian_chroot(image_path, mount_point) elif distro == 'centos': cls.rhel_chroot(image_path, mount_point) finally: remove_chroot_env(mounted_path) (kernel_path, ramdisk_path) = cls.get_kernel_ramdisk(mount_point, kernel_dir, ramdisk_dir) #Use the image, kernel, and ramdisk paths #to initialize any driver that implements 'upload_full_image' return (image_path, kernel_path, ramdisk_path) finally: run_command(["umount", mount_point])
def mount_and_clean(self, image_path, mount_point, *args, **kwargs): """ Clean the local image at <image_path> Mount it to <mount_point> """ #Prepare the paths if not os.path.exists(image_path): logger.error("Could not find local image!") raise Exception("Image file not found") if not os.path.exists(mount_point): os.makedirs(mount_point) #FSCK the image, FIRST! fsck_image(image_path) #Mount the directory result, nbd_device = mount_image(image_path, mount_point) if not result: raise Exception("Encountered errors mounting the image: %s" % image_path) try: #Required cleaning remove_user_data(mount_point) remove_atmo_data(mount_point) remove_vm_specific_data(mount_point) #Filesystem cleaning (From within the image) self.file_hook_cleaning(mount_point, **kwargs) #Driver specific cleaning self.clean_hook(image_path, mount_point, *args, **kwargs) #Required for atmosphere atmo_required_files(mount_point) finally: #Don't forget to unmount! run_command(['umount', mount_point]) if nbd_device: run_command(['qemu-nbd', '-d', nbd_device]) return