def create_vfat_image(output_file, files_info=None, parameters=None, parameters_file='parameters.txt', fs_size_kib=100): """Creates the fat fs image on the desired file. This method copies the given files to a root directory (optional), writes the parameters specified to the parameters file within the root directory (optional), and then creates a vfat image of the root directory. :param output_file: The path to the file where the fat fs image needs to be created. :param files_info: A dict containing absolute path of file to be copied -> relative path within the vfat image. For example, { '/absolute/path/to/file' -> 'relative/path/within/root' ... } :param parameters: A dict containing key-value pairs of parameters. :param parameters_file: The filename for the parameters file. :param fs_size_kib: size of the vfat filesystem in KiB. :raises: ImageCreationFailed, if image creation failed while doing any of filesystem manipulation activities like creating dirs, mounting, creating filesystem, copying files, etc. """ try: utils.dd('/dev/zero', output_file, 'count=1', "bs=%dKiB" % fs_size_kib) except processutils.ProcessExecutionError as e: raise exception.ImageCreationFailed(image_type='vfat', error=e) with utils.tempdir() as tmpdir: try: # The label helps ramdisks to find the partition containing # the parameters (by using /dev/disk/by-label/ir-vfd-dev). # NOTE: FAT filesystem label can be up to 11 characters long. utils.mkfs('vfat', output_file, label="ir-vfd-dev") utils.mount(output_file, tmpdir, '-o', 'umask=0') except processutils.ProcessExecutionError as e: raise exception.ImageCreationFailed(image_type='vfat', error=e) try: if files_info: _create_root_fs(tmpdir, files_info) if parameters: parameters_file = os.path.join(tmpdir, parameters_file) params_list = ['%(key)s=%(val)s' % {'key': k, 'val': v} for k, v in parameters.items()] file_contents = '\n'.join(params_list) utils.write_to_file(parameters_file, file_contents) except Exception as e: LOG.exception(_LE("vfat image creation failed. Error: %s"), e) raise exception.ImageCreationFailed(image_type='vfat', error=e) finally: try: utils.umount(tmpdir) except processutils.ProcessExecutionError as e: raise exception.ImageCreationFailed(image_type='vfat', error=e)
def _collect_files(image_path): """Mount image and return a dictionary of paths found there. Mounts given image under a temporary directory, walk its contents and produce a dictionary of absolute->relative paths found on the image. :param image_path: ISO9660 or FAT-formatted image to mount. :raises: ImageCreationFailed, if image inspection failed. :returns: a dict mapping absolute paths to relative to the mount point. """ if not image_path: yield {} return with utils.tempdir() as mount_dir: try: utils.mount(image_path, mount_dir, '-o', 'loop') except processutils.ProcessExecutionError as e: LOG.exception("Mounting filesystem image %(image)s " "failed", {'image': image_path}) raise exception.ImageCreationFailed(image_type='iso', error=e) try: yield _read_dir(mount_dir) except EnvironmentError as e: LOG.exception("Examining image %(images)s failed: ", {'image': image_path}) _umount_without_raise(mount_dir) raise exception.ImageCreationFailed(image_type='iso', error=e) _umount_without_raise(mount_dir)
def _mount_deploy_iso(deploy_iso, mountdir): """This function opens up the deploy iso used for deploy. :param: deploy_iso: path to the deploy iso where its contents are fetched to. :raises: ImageCreationFailed if mount fails. :returns: a tuple consisting of - 1. a dictionary containing the values as required by create_isolinux_image, 2. efiboot.img relative path, and 3. grub.cfg relative path. """ e_img_rel_path = None e_img_path = None grub_rel_path = None grub_path = None try: utils.mount(deploy_iso, mountdir, '-o', 'loop') except processutils.ProcessExecutionError as e: LOG.exception(_LE("mounting the deploy iso failed.")) raise exception.ImageCreationFailed(image_type='iso', error=e) try: for (dir, subdir, files) in os.walk(mountdir): if 'efiboot.img' in files: e_img_path = os.path.join(dir, 'efiboot.img') e_img_rel_path = os.path.relpath(e_img_path, mountdir) if 'grub.cfg' in files: grub_path = os.path.join(dir, 'grub.cfg') grub_rel_path = os.path.relpath(grub_path, mountdir) except (OSError, IOError) as e: LOG.exception(_LE("examining the deploy iso failed.")) _umount_without_raise(mountdir) raise exception.ImageCreationFailed(image_type='iso', error=e) # check if the variables are assigned some values or not during # walk of the mountdir. if not (e_img_path and e_img_rel_path and grub_path and grub_rel_path): error = (_("Deploy iso didn't contain efiboot.img or grub.cfg")) _umount_without_raise(mountdir) raise exception.ImageCreationFailed(image_type='iso', error=error) uefi_path_info = {e_img_path: e_img_rel_path, grub_path: grub_rel_path} # Returning a tuple as it makes the code simpler and clean. # uefi_path_info: is needed by the caller for _create_root_fs to create # appropriate directory structures for uefi boot iso. # grub_rel_path: is needed to copy the new grub.cfg generated using # generate_cfg() to the same directory path structure where it was # present in deploy iso. This path varies for different OS vendors. # e_img_rel_path: is required by mkisofs to generate boot iso. return uefi_path_info, e_img_rel_path, grub_rel_path
def _mount_deploy_iso(deploy_iso, mountdir): """This function opens up the deploy iso used for deploy. :param: deploy_iso: path to the deploy iso where its contents are fetched to. :raises: ImageCreationFailed if mount fails. :returns: a tuple consisting of - 1. a dictionary containing the values as required by create_isolinux_image, 2. efiboot.img relative path, and 3. grub.cfg relative path. """ e_img_rel_path = None e_img_path = None grub_rel_path = None grub_path = None try: utils.mount(deploy_iso, mountdir, '-o', 'loop') except processutils.ProcessExecutionError as e: LOG.exception("mounting the deploy iso failed.") raise exception.ImageCreationFailed(image_type='iso', error=e) try: for (dir, subdir, files) in os.walk(mountdir): if 'efiboot.img' in files: e_img_path = os.path.join(dir, 'efiboot.img') e_img_rel_path = os.path.relpath(e_img_path, mountdir) if 'grub.cfg' in files: grub_path = os.path.join(dir, 'grub.cfg') grub_rel_path = os.path.relpath(grub_path, mountdir) except (OSError, IOError) as e: LOG.exception("examining the deploy iso failed.") _umount_without_raise(mountdir) raise exception.ImageCreationFailed(image_type='iso', error=e) # check if the variables are assigned some values or not during # walk of the mountdir. if not (e_img_path and e_img_rel_path and grub_path and grub_rel_path): error = (_("Deploy iso didn't contain efiboot.img or grub.cfg")) _umount_without_raise(mountdir) raise exception.ImageCreationFailed(image_type='iso', error=error) uefi_path_info = {e_img_path: e_img_rel_path, grub_path: grub_rel_path} # Returning a tuple as it makes the code simpler and clean. # uefi_path_info: is needed by the caller for _create_root_fs to create # appropriate directory structures for uefi boot iso. # grub_rel_path: is needed to copy the new grub.cfg generated using # generate_cfg() to the same directory path structure where it was # present in deploy iso. This path varies for different OS vendors. # e_img_rel_path: is required by mkisofs to generate boot iso. return uefi_path_info, e_img_rel_path, grub_rel_path
def create_vfat_image(output_file, files_info=None, parameters=None, parameters_file="parameters.txt", fs_size_kib=100): """Creates the fat fs image on the desired file. This method copies the given files to a root directory (optional), writes the parameters specified to the parameters file within the root directory (optional), and then creates a vfat image of the root directory. :param output_file: The path to the file where the fat fs image needs to be created. :param files_info: A dict containing absolute path of file to be copied -> relative path within the vfat image. For example, { '/absolute/path/to/file' -> 'relative/path/within/root' ... } :param parameters: A dict containing key-value pairs of parameters. :param parameters_file: The filename for the parameters file. :param fs_size_kib: size of the vfat filesystem in KiB. :raises: ImageCreationFailed, if image creation failed while doing any of filesystem manipulation activities like creating dirs, mounting, creating filesystem, copying files, etc. """ try: utils.dd("/dev/zero", output_file, "count=1", "bs=%dKiB" % fs_size_kib) except processutils.ProcessExecutionError as e: raise exception.ImageCreationFailed(image_type="vfat", error=e) with utils.tempdir() as tmpdir: try: utils.mkfs("vfat", output_file) utils.mount(output_file, tmpdir, "-o", "umask=0") except processutils.ProcessExecutionError as e: raise exception.ImageCreationFailed(image_type="vfat", error=e) try: if files_info: _create_root_fs(tmpdir, files_info) if parameters: parameters_file = os.path.join(tmpdir, parameters_file) params_list = ["%(key)s=%(val)s" % {"key": k, "val": v} for k, v in parameters.items()] file_contents = "\n".join(params_list) utils.write_to_file(parameters_file, file_contents) except Exception as e: LOG.exception(_LE("vfat image creation failed. Error: %s"), e) raise exception.ImageCreationFailed(image_type="vfat", error=e) finally: try: utils.umount(tmpdir) except processutils.ProcessExecutionError as e: raise exception.ImageCreationFailed(image_type="vfat", error=e)
def fetch(self): if self.is_fetched(): return res_dir, mount_dir = self.get_dir_names() local_path = os.path.join(res_dir, self.name) url_download(self._task.context, self._task.node, self.url, dest_path=local_path) mount_point = os.path.join(mount_dir, self.name) fileutils.ensure_tree(mount_point) utils.mount(local_path, mount_point, '-o', 'ro') pull_url = rsync.build_rsync_url_from_abs(mount_point, trailing_slash=True) self.local_path = local_path self.mount_point = mount_point self.pull_url = pull_url