def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir, bootimg_dir, kernel_dir, native_sysroot): """ Called after all partitions have been prepared and assembled into a disk image. In this case, we install the MBR. """ mbrfile = "%s/syslinux/" % bootimg_dir if creator.ptable_format == 'msdos': mbrfile += "mbr.bin" elif creator.ptable_format == 'gpt': mbrfile += "gptmbr.bin" else: msger.error("Unsupported partition table: %s" % creator.ptable_format) if not os.path.exists(mbrfile): msger.error("Couldn't find %s. If using the -e option, do you " "have the right MACHINE set in local.conf? If not, " "is the bootimg_dir path correct?" % mbrfile) full_path = creator._full_path(workdir, disk_name, "direct") msger.debug("Installing MBR on disk %s as %s with size %s bytes" \ % (disk_name, full_path, disk['min_size'])) rcode = runner.show(['dd', 'if=%s' % mbrfile, 'of=%s' % full_path, 'conv=notrunc']) if rcode != 0: raise ImageError("Unable to set MBR to %s" % full_path)
def finalize(self): """ Finalize the disk image. For example, prepare the image to be bootable by e.g. creating and installing a bootloader configuration. """ source_plugin = self.get_default_source_plugin() if source_plugin: self._source_methods = pluginmgr.get_source_plugin_methods(source_plugin, disk_methods) for disk_name, disk in self.__image.disks.items(): self._source_methods["do_install_disk"](disk, disk_name, self, self.workdir, self.oe_builddir, self.bootimg_dir, self.kernel_dir, self.native_sysroot) # Compress the image if self.compressor: for disk_name, disk in self.__image.disks.items(): full_path = self._full_path(self.__imgdir, disk_name, "direct") msger.debug("Compressing disk %s with %s" % \ (disk_name, self.compressor)) exec_cmd("%s %s" % (self.compressor, full_path))
def do_configure_grubefi(cls, hdddir, cr, cr_workdir): """ Create loader-specific (grub-efi) config """ splash = os.path.join(cr_workdir, "/EFI/boot/splash.jpg") if os.path.exists(splash): splashline = "menu background splash.jpg" else: splashline = "" options = cr.ks.handler.bootloader.appendLine grubefi_conf = "" grubefi_conf += "serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1\n" grubefi_conf += "default=boot\n" timeout = kickstart.get_timeout(cr.ks) if not timeout: timeout = 0 grubefi_conf += "timeout=%s\n" % timeout grubefi_conf += "menuentry 'boot'{\n" kernel = "/bzImage" grubefi_conf += "linux %s root=%s rootwait %s\n" % (kernel, cr.rootdev, options) grubefi_conf += "}\n" msger.debug("Writing grubefi config %s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir) cfg = open("%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir, "w") cfg.write(grubefi_conf) cfg.close()
def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir, bootimg_dir, kernel_dir, native_sysroot): """ Called after all partitions have been prepared and assembled into a disk image. In this case, we insert/modify the MBR using isohybrid utility for booting via BIOS from disk storage devices. """ full_path = creator._full_path(workdir, disk_name, "direct") iso_img = "%s.p1" % full_path full_path_iso = creator._full_path(workdir, disk_name, "iso") isohybrid_cmd = "isohybrid -u %s" % iso_img msger.debug("running command: %s" % \ isohybrid_cmd) exec_native_cmd(isohybrid_cmd, native_sysroot) # Replace the image created by direct plugin with the one created by # mkisofs command. This is necessary because the iso image created by # mkisofs has a very specific MBR is system area of the ISO image, and # direct plugin adds and configures an another MBR. msger.debug("Replaceing the image created by direct plugin\n") os.remove(full_path) shutil.copy2(iso_img, full_path_iso) shutil.copy2(full_path_iso, full_path) # Remove temporary ISO file os.remove(iso_img)
def exec_native_cmd(cmd_and_args, native_sysroot, catch = 3): """ Execute native command, catching stderr, stdout Need to execute as_shell if the command uses wildcards Always need to execute native commands as_shell """ native_paths = \ "export PATH=%s/sbin:%s/usr/sbin:%s/usr/bin" % \ (native_sysroot, native_sysroot, native_sysroot) native_cmd_and_args = "%s;%s" % (native_paths, cmd_and_args) msger.debug("exec_native_cmd: %s" % cmd_and_args) args = cmd_and_args.split() msger.debug(args) rc, out = __exec_cmd(native_cmd_and_args, True, catch) if rc == 127: # shell command-not-found msger.error("A native program %s required to build the image " "was not found (see details above). Please make sure " "it's installed and try again." % args[0]) return (rc, out)
def do_prepare_partition(cls, part, source_params, cr, cr_workdir, oe_builddir, bootimg_dir, kernel_dir, rootfs_dir, native_sysroot): """ Called to do the actual content population for a partition i.e. it 'prepares' the partition to be incorporated into the image. """ if not bootimg_dir: bootimg_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") if not bootimg_dir: msger.error("Couldn't find DEPLOY_DIR_IMAGE, exiting\n") msger.debug('Bootimg dir: %s' % bootimg_dir) if 'file' not in source_params: msger.error("No file specified\n") return src = os.path.join(bootimg_dir, source_params['file']) dst = os.path.join(cr_workdir, "%s.%s" % (source_params['file'], part.lineno)) if 'skip' in source_params: sparse_copy(src, dst, skip=source_params['skip']) else: sparse_copy(src, dst) # get the size in the right units for kickstart (kB) du_cmd = "du -Lbks %s" % dst out = exec_cmd(du_cmd) filesize = out.split()[0] if int(filesize) > int(part.size): part.size = filesize part.source_file = dst
def do_install_disk(cls, disk, disk_name, image_creator, workdir, oe_builddir, bootimg_dir, kernel_dir, native_sysroot): """ Assemble partitions to disk image Called after all partitions have been prepared and assembled into a disk image. In this case, we install the MBR. """ mbrfile = os.path.join(native_sysroot, "usr/share/syslinux/") if image_creator.ptable_format == 'msdos': mbrfile += "mbr.bin" elif image_creator.ptable_format == 'gpt': mbrfile += "gptmbr.bin" else: msger.error("Unsupported partition table: %s" % \ image_creator.ptable_format) if not os.path.exists(mbrfile): msger.error("Couldn't find %s. Has syslinux-native been baked?" % mbrfile) full_path = disk['disk'].device msger.debug("Installing MBR on disk %s as %s with size %s bytes" \ % (disk_name, full_path, disk['min_size'])) ret_code = runner.show(['dd', 'if=%s' % mbrfile, 'of=%s' % full_path, 'conv=notrunc']) if ret_code != 0: raise ImageError("Unable to set MBR to %s" % full_path)
def do_configure_partition(cls, part, source_params, image_creator, image_creator_workdir, oe_builddir, bootimg_dir, kernel_dir, native_sysroot): """ Creates syslinux config in rootfs directory Called before do_prepare_partition() """ options = image_creator.ks.handler.bootloader.appendLine syslinux_conf = "" syslinux_conf += "PROMPT 0\n" timeout = kickstart.get_timeout(image_creator.ks) if not timeout: timeout = 0 syslinux_conf += "TIMEOUT " + str(timeout) + "\n" syslinux_conf += "ALLOWOPTIONS 1\n" # Derive SERIAL... line from from kernel boot parameters syslinux_conf += syslinux.serial_console_form_kargs(options) + "\n" syslinux_conf += "DEFAULT linux\n" syslinux_conf += "LABEL linux\n" syslinux_conf += " KERNEL /boot/bzImage\n" syslinux_conf += " APPEND label=boot root=%s %s\n" % \ (image_creator.rootdev, options) syslinux_cfg = os.path.join(image_creator.rootfs_dir['ROOTFS_DIR'], "boot", "syslinux.cfg") msger.debug("Writing syslinux config %s" % syslinux_cfg) with open(syslinux_cfg, "w") as cfg: cfg.write(syslinux_conf)
def do_configure_syslinux(cls, creator, cr_workdir): """ Create loader-specific (syslinux) config """ splash = os.path.join(cr_workdir, "/ISO/boot/splash.jpg") if os.path.exists(splash): splashline = "menu background splash.jpg" else: splashline = "" bootloader = creator.ks.bootloader syslinux_conf = "" syslinux_conf += "PROMPT 0\n" syslinux_conf += "TIMEOUT %s \n" % (bootloader.timeout or 10) syslinux_conf += "\n" syslinux_conf += "ALLOWOPTIONS 1\n" syslinux_conf += "SERIAL 0 115200\n" syslinux_conf += "\n" if splashline: syslinux_conf += "%s\n" % splashline syslinux_conf += "DEFAULT boot\n" syslinux_conf += "LABEL boot\n" kernel = "/bzImage" syslinux_conf += "KERNEL " + kernel + "\n" syslinux_conf += "APPEND initrd=/initrd LABEL=boot %s\n" \ % bootloader.append msger.debug("Writing syslinux config %s/ISO/isolinux/isolinux.cfg" \ % cr_workdir) with open("%s/ISO/isolinux/isolinux.cfg" % cr_workdir, "w") as cfg: cfg.write(syslinux_conf)
def do_install_disk(self, disk, disk_name, cr, workdir, oe_builddir, bootimg_dir, kernel_dir, native_sysroot): """ Called after all partitions have been prepared and assembled into a disk image. This provides a hook to allow finalization of a disk image e.g. to write an MBR to it. """ msger.debug("SourcePlugin: do_install_disk: disk: %s" % disk_name)
def assemble(self): """ Assemble partitions into disk image(s) """ for disk_name, disk in self.__image.disks.items(): full_path = self._full_path(self.__imgdir, disk_name, "direct") msger.debug("Assembling disk %s as %s with size %s bytes" % (disk_name, full_path, disk["min_size"])) self.__image.assemble(full_path)
def do_prepare_partition(self, part, source_params, cr, cr_workdir, oe_builddir, bootimg_dir, kernel_dir, rootfs_dir, native_sysroot): """ Called to do the actual content population for a partition i.e. it 'prepares' the partition to be incorporated into the image. """ msger.debug("SourcePlugin: do_prepare_partition: part: %s" % part)
def do_prepare_partition(self, part, source_params, cr, cr_workdir, oe_builddir, bootimg_dir, kernel_dir, rootfs_dir, native_sysroot): """ Called to do the actual content population for a partition i.e. it 'prepares' the partition to be incorporated into the image. In this case, does the following: - sets up a vfat partition - copies all files listed in IMAGE_BOOT_FILES variable """ hdddir = "%s/boot" % cr_workdir rm_cmd = "rm -rf %s" % cr_workdir exec_cmd(rm_cmd) install_cmd = "install -d %s" % hdddir exec_cmd(install_cmd) if not bootimg_dir: bootimg_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") if not bootimg_dir: msger.error("Couldn't find DEPLOY_DIR_IMAGE, exiting\n") msger.debug('Bootimg dir: %s' % bootimg_dir) boot_files = get_bitbake_var("IMAGE_BOOT_FILES") if not boot_files: msger.error('No boot files defined, IMAGE_BOOT_FILES unset') msger.debug('Boot files: %s' % boot_files) # list of tuples (src_name, dst_name) deploy_files = [] for src_entry in re.findall(r'[\w;\-\./]+', boot_files): if ';' in src_entry: dst_entry = tuple(src_entry.split(';')) if not dst_entry[0] or not dst_entry[1]: msger.error('Malformed boot file entry: %s' % (src_entry)) else: dst_entry = (src_entry, src_entry) msger.debug('Destination entry: %r' % (dst_entry,)) deploy_files.append(dst_entry) for deploy_entry in deploy_files: src, dst = deploy_entry src_path = os.path.join(bootimg_dir, src) dst_path = os.path.join(hdddir, dst) msger.debug('Install %s as %s' % (os.path.basename(src_path), dst_path)) install_cmd = "install -m 0644 -D %s %s" \ % (src_path, dst_path) exec_cmd(install_cmd) msger.debug('Prepare boot partition using rootfs in %s' % (hdddir)) part.prepare_rootfs(cr_workdir, oe_builddir, hdddir, native_sysroot)
def do_configure_partition(cls, part, source_params, creator, cr_workdir, oe_builddir, bootimg_dir, kernel_dir, native_sysroot): """ Called before do_prepare_partition(), creates syslinux config """ hdddir = "%s/hdd/boot" % cr_workdir rm_cmd = "rm -rf " + cr_workdir exec_cmd(rm_cmd) install_cmd = "install -d %s" % hdddir exec_cmd(install_cmd) bootloader = creator.ks.bootloader custom_cfg = None if bootloader.configfile: custom_cfg = get_custom_config(bootloader.configfile) if custom_cfg: # Use a custom configuration for grub syslinux_conf = custom_cfg msger.debug("Using custom configuration file " "%s for syslinux.cfg" % bootloader.configfile) else: msger.error("configfile is specified but failed to " "get it from %s." % bootloader.configfile) if not custom_cfg: # Create syslinux configuration using parameters from wks file splash = os.path.join(cr_workdir, "/hdd/boot/splash.jpg") if os.path.exists(splash): splashline = "menu background splash.jpg" else: splashline = "" syslinux_conf = "" syslinux_conf += "PROMPT 0\n" syslinux_conf += "TIMEOUT " + str(bootloader.timeout) + "\n" syslinux_conf += "\n" syslinux_conf += "ALLOWOPTIONS 1\n" syslinux_conf += "SERIAL 0 115200\n" syslinux_conf += "\n" if splashline: syslinux_conf += "%s\n" % splashline syslinux_conf += "DEFAULT boot\n" syslinux_conf += "LABEL boot\n" kernel = "/vmlinuz" syslinux_conf += "KERNEL " + kernel + "\n" syslinux_conf += "APPEND label=boot root=%s %s\n" % \ (creator.rootdev, bootloader.append) msger.debug("Writing syslinux config %s/hdd/boot/syslinux.cfg" \ % cr_workdir) cfg = open("%s/hdd/boot/syslinux.cfg" % cr_workdir, "w") cfg.write(syslinux_conf) cfg.close()
def _create(self): """ For 'wic', we already have our build artifacts - we just create filesystems from the artifacts directly and combine them into a partitioned image. """ parts = self._get_parts() self.__image = Image(self.native_sysroot) for p in parts: # as a convenience, set source to the boot partition source # instead of forcing it to be set via bootloader --source if not self.ks.handler.bootloader.source and p.mountpoint == "/boot": self.ks.handler.bootloader.source = p.source fstab_path = self._write_fstab(self.rootfs_dir.get("ROOTFS_DIR")) for p in parts: # need to create the filesystems in order to get their # sizes before we can add them and do the layout. # Image.create() actually calls __format_disks() to create # the disk images and carve out the partitions, then # self.assemble() calls Image.assemble() which calls # __write_partitition() for each partition to dd the fs # into the partitions. p.prepare(self, self.workdir, self.oe_builddir, self.rootfs_dir, self.bootimg_dir, self.kernel_dir, self.native_sysroot) self.__image.add_partition(int(p.size), p.disk, p.mountpoint, p.source_file, p.fstype, p.label, fsopts=p.fsopts, boot=p.active, align=p.align, no_table=p.no_table, part_type=p.part_type, uuid=p.uuid) if fstab_path: shutil.move(fstab_path + ".orig", fstab_path) self.__image.layout_partitions(self.ptable_format) self.__imgdir = self.workdir for disk_name, disk in self.__image.disks.items(): full_path = self._full_path(self.__imgdir, disk_name, "direct") msger.debug("Adding disk %s as %s with size %s bytes" \ % (disk_name, full_path, disk['min_size'])) disk_obj = fs_related.DiskImage(full_path, disk['min_size']) self.__disks[disk_name] = disk_obj self.__image.add_disk(disk_name, disk_obj) self.__image.create()
def _add_plugindir(self, path): path = os.path.abspath(os.path.expanduser(path)) if not os.path.isdir(path): msger.debug("Plugin dir is not a directory or does not exist: %s" % path) return if path not in self.plugin_dirs: self.plugin_dirs[path] = False
def do_configure_partition(self, part, source_params, cr, cr_workdir, oe_builddir, bootimg_dir, kernel_dir, native_sysroot): """ Called before do_prepare_partition(), typically used to create custom configuration files for a partition, for example syslinux or grub config files. """ msger.debug("SourcePlugin: do_configure_partition: part: %s" % part)
def assemble(self, image_file): msger.debug("Installing partitions") self.image_file = image_file for p in self.partitions: d = self.disks[p['disk_name']] self.__write_partition(p['num'], p['source_file'], p['start'], p['size'])
def do_configure_partition(self, part, source_params, cr, cr_workdir, oe_builddir, bootimg_dir, kernel_dir, native_sysroot): """ Called before do_prepare_partition(), creates syslinux config """ hdddir = "%s/hdd/boot" % cr_workdir rm_cmd = "rm -rf " + cr_workdir exec_cmd(rm_cmd) install_cmd = "install -d %s" % hdddir exec_cmd(install_cmd) splash = os.path.join(cr_workdir, "/hdd/boot/splash.jpg") if os.path.exists(splash): splashline = "menu background splash.jpg" else: splashline = "" (rootdev, root_part_uuid) = cr._get_boot_config() options = cr.ks.handler.bootloader.appendLine syslinux_conf = "" syslinux_conf += "PROMPT 0\n" timeout = kickstart.get_timeout(cr.ks) if not timeout: timeout = 0 syslinux_conf += "TIMEOUT " + str(timeout) + "\n" syslinux_conf += "\n" syslinux_conf += "ALLOWOPTIONS 1\n" syslinux_conf += "SERIAL 0 115200\n" syslinux_conf += "\n" if splashline: syslinux_conf += "%s\n" % splashline syslinux_conf += "DEFAULT boot\n" syslinux_conf += "LABEL boot\n" kernel = "/vmlinuz" syslinux_conf += "KERNEL " + kernel + "\n" if cr._ptable_format == 'msdos': rootstr = rootdev else: raise ImageError("Unsupported partition table format found") syslinux_conf += "APPEND label=boot root=%s %s\n" % (rootstr, options) msger.debug("Writing syslinux config %s/hdd/boot/syslinux.cfg" \ % cr_workdir) cfg = open("%s/hdd/boot/syslinux.cfg" % cr_workdir, "w") cfg.write(syslinux_conf) cfg.close()
def __create_partition(self, device, parttype, fstype, start, size): """ Create a partition on an image described by the 'device' object. """ # Start is included to the size so we need to substract one from the end. end = start + size - 1 msger.debug("Added '%s' partition, sectors %d-%d, size %d sectors" % (parttype, start, end, size)) cmd = "parted -s %s unit s mkpart %s" % (device, parttype) if fstype: cmd += " %s" % fstype cmd += " %d %d" % (start, end) return exec_native_cmd(cmd, self.native_sysroot)
def __create_partition(self, device, parttype, fstype, start, size): """ Create a partition on an image described by the 'device' object. """ # Start is included to the size so we need to substract one from the end. end = start + size - 1 msger.debug("Added '%s' partition, sectors %d-%d, size %d sectors" % (parttype, start, end, size)) args = ["-s", device, "unit", "s", "mkpart", parttype] if fstype: args.extend([fstype]) args.extend(["%d" % start, "%d" % end]) return self.__run_parted(args)
def __write_partition(self, num, source_file, start, size): """ Install source_file contents into a partition. """ if not source_file: # nothing to write return # Start is included in the size so need to substract one from the end. end = start + size - 1 msger.debug("Installed %s in partition %d, sectors %d-%d, size %d sectors" % (source_file, num, start, end, size)) dd_cmd = "dd if=%s of=%s bs=%d seek=%d count=%d conv=notrunc" % \ (source_file, self.image_file, self.sector_size, start, size) exec_cmd(dd_cmd)
def do_stage_partition(self, part, source_params, cr, cr_workdir, oe_builddir, bootimg_dir, kernel_dir, native_sysroot): """ Special content staging hook called before do_prepare_partition(), normally empty. Typically, a partition will just use the passed-in parame e.g straight bootimg_dir, etc, but in some cases, things need to be more tailored e.g. to use a deploy dir + /boot, etc. This hook allows those files to be staged in a customized fashion. Not that get_bitbake_var() allows you to acces non-standard variables that you might want to use for this. """ msger.debug("SourcePlugin: do_stage_partition: part: %s" % part)
def assemble(self, image_file): msger.debug("Installing partitions") for part in self.partitions: source = part['source_file'] if source: # install source_file contents into a partition sparse_copy(source, image_file, part['start'] * self.sector_size) msger.debug("Installed %s in partition %d, sectors %d-%d, " "size %d sectors" % \ (source, part['num'], part['start'], part['start'] + part['size'] - 1, part['size'])) os.rename(source, image_file + '.p%d' % part['num'])
def __run_parted(self, args): """ Run parted with arguments specified in the 'args' list. """ args.insert(0, self.parted) msger.debug(args) rc, out = runner.runtool(args, catch = 3) out = out.strip() if out: msger.debug('"parted" output: %s' % out) if rc != 0: # We don't throw exception when return code is not 0, because # parted always fails to reload part table with loop devices. This # prevents us from distinguishing real errors based on return # code. msger.error("WARNING: parted returned '%s' instead of 0 (use --debug for details)" % rc)
def do_configure_grubefi(cls, part, creator, cr_workdir): """ Create loader-specific (grub-efi) config """ configfile = creator.ks.bootloader.configfile if configfile: grubefi_conf = get_custom_config(configfile) if grubefi_conf: msger.debug("Using custom configuration file " "%s for grub.cfg" % configfile) else: msger.error("configfile is specified but failed to " "get it from %s." % configfile) else: splash = os.path.join(cr_workdir, "EFI/boot/splash.jpg") if os.path.exists(splash): splashline = "menu background splash.jpg" else: splashline = "" bootloader = creator.ks.bootloader grubefi_conf = "" grubefi_conf += "serial --unit=0 --speed=115200 --word=8 " grubefi_conf += "--parity=no --stop=1\n" grubefi_conf += "default=boot\n" grubefi_conf += "timeout=%s\n" % (bootloader.timeout or 10) grubefi_conf += "\n" grubefi_conf += "search --set=root --label %s " % part.label grubefi_conf += "\n" grubefi_conf += "menuentry 'boot'{\n" kernel = "/bzImage" grubefi_conf += "linux %s rootwait %s\n" \ % (kernel, bootloader.append) grubefi_conf += "initrd /initrd \n" grubefi_conf += "}\n" if splashline: grubefi_conf += "%s\n" % splashline msger.debug("Writing grubefi config %s/EFI/BOOT/grub.cfg" \ % cr_workdir) with open("%s/EFI/BOOT/grub.cfg" % cr_workdir, "w") as cfg: cfg.write(grubefi_conf)
def do_configure_gummiboot(self, hdddir, cr, cr_workdir): """ Create loader-specific (gummiboot) config """ install_cmd = "install -d %s/loader" % hdddir exec_cmd(install_cmd) install_cmd = "install -d %s/loader/entries" % hdddir exec_cmd(install_cmd) (rootdev, root_part_uuid) = cr._get_boot_config() options = cr.ks.handler.bootloader.appendLine timeout = kickstart.get_timeout(cr.ks) if not timeout: timeout = 0 loader_conf = "" loader_conf += "default boot\n" loader_conf += "timeout %d\n" % timeout msger.debug("Writing gummiboot config %s/hdd/boot/loader/loader.conf" \ % cr_workdir) cfg = open("%s/hdd/boot/loader/loader.conf" % cr_workdir, "w") cfg.write(loader_conf) cfg.close() kernel = "/bzImage" if cr._ptable_format == 'msdos': rootstr = rootdev else: raise ImageError("Unsupported partition table format found") boot_conf = "" boot_conf += "title boot\n" boot_conf += "linux %s\n" % kernel boot_conf += "options LABEL=Boot root=%s %s\n" \ % (rootstr, options) msger.debug("Writing gummiboot config %s/hdd/boot/loader/entries/boot.conf" \ % cr_workdir) cfg = open("%s/hdd/boot/loader/entries/boot.conf" % cr_workdir, "w") cfg.write(boot_conf) cfg.close()
def assemble(self, image_file): msger.debug("Installing partitions") for part in self.partitions: source = part['source_file'] if source: # install source_file contents into a partition cmd = "dd if=%s of=%s bs=%d seek=%d count=%d conv=notrunc" % \ (source, image_file, self.sector_size, part['start'], part['size']) exec_cmd(cmd) msger.debug("Installed %s in partition %d, sectors %d-%d, " "size %d sectors" % \ (source, part['num'], part['start'], part['start'] + part['size'] - 1, part['size'])) os.rename(source, image_file + '.p%d' % part['num'])
def __run_parted(self, args): """ Run parted with arguments specified in the 'args' list. """ args.insert(0, "parted") args = ' '.join(args) msger.debug(args) rc, out = exec_native_cmd(args, self.native_sysroot) if out: msger.debug('"parted" output: %s' % out) if rc != 0: # We don't throw exception when return code is not 0, because # parted always fails to reload part table with loop devices. This # prevents us from distinguishing real errors based on return # code. msger.error("WARNING: parted returned '%s' instead of 0 (use --debug for details)" % rc)
def assemble(self, image_file): msger.debug("Installing partitions") self.image_file = image_file for p in self.partitions: d = self.disks[p['disk_name']] if d['ptable_format'] == "msdos" and p['num'] == 5: # The last sector of the 3rd partition was reserved for the EBR # of the first _logical_ partition. This is why the extended # partition should start one sector before the first logical # partition. self.__write_partition(p['num'], p['source_file'], p['start'] - 1, d['offset'] - p['start']) self.__write_partition(p['num'], p['source_file'], p['start'], p['size'])
def __format_disks(self): self.layout_partitions() for dev in self.disks.keys(): d = self.disks[dev] msger.debug("Initializing partition table for %s" % \ (d['disk'].device)) self.__run_parted( ["-s", d['disk'].device, "mklabel", d['ptable_format']]) msger.debug("Creating partitions") for p in self.partitions: d = self.disks[p['disk_name']] if d['ptable_format'] == "msdos" and p['num'] == 5: # The last sector of the 3rd partition was reserved for the EBR # of the first _logical_ partition. This is why the extended # partition should start one sector before the first logical # partition. self.__create_partition(d['disk'].device, "extended", None, p['start'] - 1, d['offset'] - p['start']) if p['fstype'] == "swap": parted_fs_type = "linux-swap" elif p['fstype'] == "vfat": parted_fs_type = "fat32" elif p['fstype'] == "msdos": parted_fs_type = "fat16" else: # Type for ext2/ext3/ext4/btrfs parted_fs_type = "ext2" # Boot ROM of OMAP boards require vfat boot partition to have an # even number of sectors. if p['mountpoint'] == "/boot" and p['fstype'] in ["vfat", "msdos"] \ and p['size'] % 2: msger.debug("Substracting one sector from '%s' partition to " \ "get even number of sectors for the partition" % \ p['mountpoint']) p['size'] -= 1 self.__create_partition(d['disk'].device, p['type'], parted_fs_type, p['start'], p['size']) if p['boot']: flag_name = "boot" msger.debug("Set '%s' flag for partition '%s' on disk '%s'" % \ (flag_name, p['num'], d['disk'].device)) self.__run_parted([ "-s", d['disk'].device, "set", "%d" % p['num'], flag_name, "on" ]) # Parted defaults to enabling the lba flag for fat16 partitions, # which causes compatibility issues with some firmware (and really # isn't necessary). if parted_fs_type == "fat16": if d['ptable_format'] == 'msdos': msger.debug("Disable 'lba' flag for partition '%s' on disk '%s'" % \ (p['num'], d['disk'].device)) self.__run_parted([ "-s", d['disk'].device, "set", "%d" % p['num'], "lba", "off" ])
def __format_disks(self): self.layout_partitions() for dev in self.disks.keys(): d = self.disks[dev] msger.debug("Initializing partition table for %s" % \ (d['disk'].device)) self.__run_parted( ["-s", d['disk'].device, "mklabel", d['ptable_format']]) msger.debug("Creating partitions") for p in self.partitions: if p['num'] == 0: continue d = self.disks[p['disk_name']] if d['ptable_format'] == "msdos" and p['num'] == 5: # Create an extended partition (note: extended # partition is described in MBR and contains all # logical partitions). The logical partitions save a # sector for an EBR just before the start of a # partition. The extended partition must start one # sector before the start of the first logical # partition. This way the first EBR is inside of the # extended partition. Since the extended partitions # starts a sector before the first logical partition, # add a sector at the back, so that there is enough # room for all logical partitions. self.__create_partition(d['disk'].device, "extended", None, p['start'] - 1, d['offset'] - p['start'] + 1) if p['fstype'] == "swap": parted_fs_type = "linux-swap" elif p['fstype'] == "vfat": parted_fs_type = "fat32" elif p['fstype'] == "msdos": parted_fs_type = "fat16" else: # Type for ext2/ext3/ext4/btrfs parted_fs_type = "ext2" # Boot ROM of OMAP boards require vfat boot partition to have an # even number of sectors. if p['mountpoint'] == "/boot" and p['fstype'] in ["vfat", "msdos"] \ and p['size'] % 2: msger.debug("Substracting one sector from '%s' partition to " \ "get even number of sectors for the partition" % \ p['mountpoint']) p['size'] -= 1 self.__create_partition(d['disk'].device, p['type'], parted_fs_type, p['start'], p['size']) if p['boot']: flag_name = "boot" msger.debug("Set '%s' flag for partition '%s' on disk '%s'" % \ (flag_name, p['num'], d['disk'].device)) self.__run_parted([ "-s", d['disk'].device, "set", "%d" % p['num'], flag_name, "on" ]) # Parted defaults to enabling the lba flag for fat16 partitions, # which causes compatibility issues with some firmware (and really # isn't necessary). if parted_fs_type == "fat16": if d['ptable_format'] == 'msdos': msger.debug("Disable 'lba' flag for partition '%s' on disk '%s'" % \ (p['num'], d['disk'].device)) self.__run_parted([ "-s", d['disk'].device, "set", "%d" % p['num'], "lba", "off" ])
def do_prepare_partition(cls, part, source_params, cr, cr_workdir, oe_builddir, bootimg_dir, kernel_dir, rootfs_dir, native_sysroot): """ Called to do the actual content population for a partition i.e. it 'prepares' the partition to be incorporated into the image. In this case, does the following: - sets up a vfat partition - copies all files listed in IMAGE_BOOT_FILES variable """ hdddir = "%s/boot" % cr_workdir install_cmd = "install -d %s" % hdddir exec_cmd(install_cmd) if not bootimg_dir: bootimg_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") if not bootimg_dir: msger.error("Couldn't find DEPLOY_DIR_IMAGE, exiting\n") msger.debug('Bootimg dir: %s' % bootimg_dir) boot_files = get_bitbake_var("IMAGE_BOOT_FILES") if not boot_files: msger.error('No boot files defined, IMAGE_BOOT_FILES unset') msger.debug('Boot files: %s' % boot_files) # list of tuples (src_name, dst_name) deploy_files = [] for src_entry in re.findall(r'[\w;\-\./\*]+', boot_files): if ';' in src_entry: dst_entry = tuple(src_entry.split(';')) if not dst_entry[0] or not dst_entry[1]: msger.error('Malformed boot file entry: %s' % (src_entry)) else: dst_entry = (src_entry, src_entry) msger.debug('Destination entry: %r' % (dst_entry,)) deploy_files.append(dst_entry) for deploy_entry in deploy_files: src, dst = deploy_entry install_task = [] if '*' in src: # by default install files under their basename entry_name_fn = os.path.basename if dst != src: # unless a target name was given, then treat name # as a directory and append a basename entry_name_fn = lambda name: \ os.path.join(dst, os.path.basename(name)) srcs = glob(os.path.join(bootimg_dir, src)) msger.debug('Globbed sources: %s' % (', '.join(srcs))) for entry in srcs: entry_dst_name = entry_name_fn(entry) install_task.append((entry, os.path.join(hdddir, entry_dst_name))) else: install_task = [(os.path.join(bootimg_dir, src), os.path.join(hdddir, dst))] for task in install_task: src_path, dst_path = task msger.debug('Install %s as %s' % (os.path.basename(src_path), dst_path)) install_cmd = "install -m 0644 -D %s %s" \ % (src_path, dst_path) exec_cmd(install_cmd) msger.debug('Prepare boot partition using rootfs in %s' % (hdddir)) part.prepare_rootfs(cr_workdir, oe_builddir, hdddir, native_sysroot)
def assemble(self, image_file): msger.debug("Installing partitions") for p in self.partitions: self.__write_partition(p['num'], p['source_file'], p['start'], p['size'], image_file)
def do_prepare_partition(cls, part, source_params, creator, cr_workdir, oe_builddir, bootimg_dir, kernel_dir, rootfs_dir, native_sysroot): """ Called to do the actual content population for a partition i.e. it 'prepares' the partition to be incorporated into the image. In this case, prepare content for a bootable ISO image. """ isodir = "%s/ISO" % cr_workdir if part.rootfs_dir is None: if not 'ROOTFS_DIR' in rootfs_dir: msger.error("Couldn't find --rootfs-dir, exiting.\n") rootfs_dir = rootfs_dir['ROOTFS_DIR'] else: if part.rootfs_dir in rootfs_dir: rootfs_dir = rootfs_dir[part.rootfs_dir] elif part.rootfs_dir: rootfs_dir = part.rootfs_dir else: msg = "Couldn't find --rootfs-dir=%s connection " msg += "or it is not a valid path, exiting.\n" msger.error(msg % part.rootfs_dir) if not os.path.isdir(rootfs_dir): rootfs_dir = get_bitbake_var("IMAGE_ROOTFS") if not os.path.isdir(rootfs_dir): msger.error("Couldn't find IMAGE_ROOTFS, exiting.\n") part.rootfs_dir = rootfs_dir # Prepare rootfs.img hdd_dir = get_bitbake_var("HDDDIR") img_iso_dir = get_bitbake_var("ISODIR") rootfs_img = "%s/rootfs.img" % hdd_dir if not os.path.isfile(rootfs_img): rootfs_img = "%s/rootfs.img" % img_iso_dir if not os.path.isfile(rootfs_img): # check if rootfs.img is in deploydir deploy_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") image_name = get_bitbake_var("IMAGE_LINK_NAME") rootfs_img = "%s/%s.%s" \ % (deploy_dir, image_name, part.fstype) if not os.path.isfile(rootfs_img): # create image file with type specified by --fstype # which contains rootfs du_cmd = "du -bks %s" % rootfs_dir out = exec_cmd(du_cmd) part.size = int(out.split()[0]) part.extra_space = 0 part.overhead_factor = 1.2 part.prepare_rootfs(cr_workdir, oe_builddir, rootfs_dir, \ native_sysroot) rootfs_img = part.source_file install_cmd = "install -m 0644 %s %s/rootfs.img" \ % (rootfs_img, isodir) exec_cmd(install_cmd) # Remove the temporary file created by part.prepare_rootfs() if os.path.isfile(part.source_file): os.remove(part.source_file) # Prepare initial ramdisk initrd = "%s/initrd" % hdd_dir if not os.path.isfile(initrd): initrd = "%s/initrd" % img_iso_dir if not os.path.isfile(initrd): initrd = cls._build_initramfs_path(rootfs_dir, cr_workdir) install_cmd = "install -m 0644 %s %s/initrd" \ % (initrd, isodir) exec_cmd(install_cmd) # Remove the temporary file created by _build_initramfs_path function if os.path.isfile("%s/initrd.cpio.gz" % cr_workdir): os.remove("%s/initrd.cpio.gz" % cr_workdir) # Install bzImage install_cmd = "install -m 0644 %s/bzImage %s/bzImage" % \ (kernel_dir, isodir) exec_cmd(install_cmd) #Create bootloader for efi boot try: if source_params['loader'] == 'grub-efi': # Builds grub.cfg if ISODIR didn't exist or # didn't contains grub.cfg bootimg_dir = img_iso_dir if not os.path.exists("%s/EFI/BOOT" % bootimg_dir): bootimg_dir = "%s/bootimg" % cr_workdir if os.path.exists(bootimg_dir): shutil.rmtree(bootimg_dir) install_cmd = "install -d %s/EFI/BOOT" % bootimg_dir exec_cmd(install_cmd) if not os.path.isfile("%s/EFI/BOOT/boot.cfg" % bootimg_dir): cls.do_configure_grubefi(part, creator, bootimg_dir) # Builds bootx64.efi/bootia32.efi if ISODIR didn't exist or # didn't contains it target_arch = get_bitbake_var("TARGET_SYS") if not target_arch: msger.error("Coludn't find target architecture\n") if re.match("x86_64", target_arch): grub_target = 'x86_64-efi' grub_image = "bootx64.efi" elif re.match('i.86', target_arch): grub_target = 'i386-efi' grub_image = "bootia32.efi" else: msger.error("grub-efi is incompatible with target %s\n" \ % target_arch) if not os.path.isfile("%s/EFI/BOOT/%s" \ % (bootimg_dir, grub_image)): grub_path = get_bitbake_var("STAGING_LIBDIR") if not grub_path: msger.error("Couldn't find STAGING_LIBDIR, exiting.\n") grub_core = "%s/grub/%s" % (grub_path, grub_target) if not os.path.exists(grub_core): msger.info("Building grub-efi...\n") exec_cmd("bitbake grub-efi") if not os.path.exists(grub_core): msger.error("Please build grub-efi first\n") grub_cmd = "grub-mkimage -p '/EFI/BOOT' " grub_cmd += "-d %s " % grub_core grub_cmd += "-O %s -o %s/EFI/BOOT/%s " \ % (grub_target, bootimg_dir, grub_image) grub_cmd += "part_gpt part_msdos ntfs ntfscomp fat ext2 " grub_cmd += "normal chain boot configfile linux multiboot " grub_cmd += "search efi_gop efi_uga font gfxterm gfxmenu " grub_cmd += "terminal minicmd test iorw loadenv echo help " grub_cmd += "reboot serial terminfo iso9660 loopback tar " grub_cmd += "memdisk ls search_fs_uuid udf btrfs xfs lvm " grub_cmd += "reiserfs ata " exec_native_cmd(grub_cmd, native_sysroot) else: # TODO: insert gummiboot stuff msger.error("unrecognized bootimg-efi loader: %s" \ % source_params['loader']) except KeyError: msger.error("bootimg-efi requires a loader, none specified") if os.path.exists("%s/EFI/BOOT" % isodir): shutil.rmtree("%s/EFI/BOOT" % isodir) shutil.copytree(bootimg_dir+"/EFI/BOOT", isodir+"/EFI/BOOT") # If exists, remove cr_workdir/bootimg temporary folder if os.path.exists("%s/bootimg" % cr_workdir): shutil.rmtree("%s/bootimg" % cr_workdir) # Create efi.img that contains bootloader files for EFI booting # if ISODIR didn't exist or didn't contains it if os.path.isfile("%s/efi.img" % img_iso_dir): install_cmd = "install -m 0644 %s/efi.img %s/efi.img" % \ (img_iso_dir, isodir) exec_cmd(install_cmd) else: du_cmd = "du -bks %s/EFI" % isodir out = exec_cmd(du_cmd) blocks = int(out.split()[0]) # Add some extra space for file system overhead blocks += 100 msg = "Added 100 extra blocks to %s to get to %d total blocks" \ % (part.mountpoint, blocks) msger.debug(msg) # Ensure total sectors is an integral number of sectors per # track or mcopy will complain. Sectors are 512 bytes, and we # generate images with 32 sectors per track. This calculation is # done in blocks, thus the mod by 16 instead of 32. blocks += (16 - (blocks % 16)) # dosfs image for EFI boot bootimg = "%s/efi.img" % isodir dosfs_cmd = 'mkfs.vfat -n "EFIimg" -S 512 -C %s %d' \ % (bootimg, blocks) exec_native_cmd(dosfs_cmd, native_sysroot) mmd_cmd = "mmd -i %s ::/EFI" % bootimg exec_native_cmd(mmd_cmd, native_sysroot) mcopy_cmd = "mcopy -i %s -s %s/EFI/* ::/EFI/" \ % (bootimg, isodir) exec_native_cmd(mcopy_cmd, native_sysroot) chmod_cmd = "chmod 644 %s" % bootimg exec_cmd(chmod_cmd) # Prepare files for legacy boot syslinux_dir = get_bitbake_var("STAGING_DATADIR") if not syslinux_dir: msger.error("Couldn't find STAGING_DATADIR, exiting.\n") if os.path.exists("%s/isolinux" % isodir): shutil.rmtree("%s/isolinux" % isodir) install_cmd = "install -d %s/isolinux" % isodir exec_cmd(install_cmd) cls.do_configure_syslinux(creator, cr_workdir) install_cmd = "install -m 444 %s/syslinux/ldlinux.sys " % syslinux_dir install_cmd += "%s/isolinux/ldlinux.sys" % isodir exec_cmd(install_cmd) install_cmd = "install -m 444 %s/syslinux/isohdpfx.bin " % syslinux_dir install_cmd += "%s/isolinux/isohdpfx.bin" % isodir exec_cmd(install_cmd) install_cmd = "install -m 644 %s/syslinux/isolinux.bin " % syslinux_dir install_cmd += "%s/isolinux/isolinux.bin" % isodir exec_cmd(install_cmd) install_cmd = "install -m 644 %s/syslinux/ldlinux.c32 " % syslinux_dir install_cmd += "%s/isolinux/ldlinux.c32" % isodir exec_cmd(install_cmd) #create ISO image iso_img = "%s/tempiso_img.iso" % cr_workdir iso_bootimg = "isolinux/isolinux.bin" iso_bootcat = "isolinux/boot.cat" efi_img = "efi.img" mkisofs_cmd = "mkisofs -V %s " % part.label mkisofs_cmd += "-o %s -U " % iso_img mkisofs_cmd += "-J -joliet-long -r -iso-level 2 -b %s " % iso_bootimg mkisofs_cmd += "-c %s -no-emul-boot -boot-load-size 4 " % iso_bootcat mkisofs_cmd += "-boot-info-table -eltorito-alt-boot " mkisofs_cmd += "-eltorito-platform 0xEF -eltorito-boot %s " % efi_img mkisofs_cmd += "-no-emul-boot %s " % isodir msger.debug("running command: %s" % mkisofs_cmd) exec_native_cmd(mkisofs_cmd, native_sysroot) shutil.rmtree(isodir) du_cmd = "du -Lbks %s" % iso_img out = exec_cmd(du_cmd) isoimg_size = int(out.split()[0]) part.size = isoimg_size part.source_file = iso_img
def do_configure_systemdboot(cls, hdddir, creator, cr_workdir, source_params): """ Create loader-specific systemd-boot/gummiboot config """ install_cmd = "install -d %s/loader" % hdddir exec_cmd(install_cmd) install_cmd = "install -d %s/loader/entries" % hdddir exec_cmd(install_cmd) bootloader = creator.ks.bootloader loader_conf = "" loader_conf += "default boot\n" loader_conf += "timeout %d\n" % bootloader.timeout initrd = source_params.get('initrd') if initrd: # obviously we need to have a common common deploy var bootimg_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") if not bootimg_dir: msger.error("Couldn't find DEPLOY_DIR_IMAGE, exiting\n") cp_cmd = "cp %s/%s %s" % (bootimg_dir, initrd, hdddir) exec_cmd(cp_cmd, True) else: msger.debug("Ignoring missing initrd") msger.debug("Writing systemd-boot config %s/hdd/boot/loader/loader.conf" \ % cr_workdir) cfg = open("%s/hdd/boot/loader/loader.conf" % cr_workdir, "w") cfg.write(loader_conf) cfg.close() configfile = creator.ks.bootloader.configfile custom_cfg = None if configfile: custom_cfg = get_custom_config(configfile) if custom_cfg: # Use a custom configuration for systemd-boot boot_conf = custom_cfg msger.debug("Using custom configuration file " "%s for systemd-boots's boot.conf" % configfile) else: msger.error("configfile is specified but failed to " "get it from %s." % configfile) if not custom_cfg: # Create systemd-boot configuration using parameters from wks file kernel = "/bzImage" boot_conf = "" boot_conf += "title boot\n" boot_conf += "linux %s\n" % kernel boot_conf += "options LABEL=Boot root=%s %s\n" % \ (creator.rootdev, bootloader.append) if initrd: boot_conf += "initrd /%s\n" % initrd msger.debug("Writing systemd-boot config %s/hdd/boot/loader/entries/boot.conf" \ % cr_workdir) cfg = open("%s/hdd/boot/loader/entries/boot.conf" % cr_workdir, "w") cfg.write(boot_conf) cfg.close()
def do_prepare_partition(cls, part, source_params, creator, cr_workdir, oe_builddir, bootimg_dir, kernel_dir, rootfs_dir, native_sysroot): """ Called to do the actual content population for a partition i.e. it 'prepares' the partition to be incorporated into the image. In this case, prepare content for an EFI (grub) boot partition. """ if not bootimg_dir: bootimg_dir = get_bitbake_var("HDDDIR") if not bootimg_dir: msger.error("Couldn't find HDDDIR, exiting\n") # just so the result notes display it creator.set_bootimg_dir(bootimg_dir) staging_kernel_dir = kernel_dir hdddir = "%s/hdd/boot" % cr_workdir install_cmd = "install -m 0644 %s/bzImage %s/bzImage" % \ (staging_kernel_dir, hdddir) exec_cmd(install_cmd) try: if source_params['loader'] == 'grub-efi': shutil.copyfile("%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir, "%s/grub.cfg" % cr_workdir) cp_cmd = "cp %s/EFI/BOOT/* %s/EFI/BOOT" % (bootimg_dir, hdddir) exec_cmd(cp_cmd, True) shutil.move("%s/grub.cfg" % cr_workdir, "%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir) elif source_params['loader'] == 'gummiboot' \ or source_params['loader'] == 'systemd-boot': cp_cmd = "cp %s/EFI/BOOT/* %s/EFI/BOOT" % (bootimg_dir, hdddir) exec_cmd(cp_cmd, True) else: msger.error("unrecognized bootimg-efi loader: %s" % source_params['loader']) except KeyError: msger.error("bootimg-efi requires a loader, none specified") startup = os.path.join(bootimg_dir, "startup.nsh") if os.path.exists(startup): cp_cmd = "cp %s %s/" % (startup, hdddir) exec_cmd(cp_cmd, True) du_cmd = "du -bks %s" % hdddir out = exec_cmd(du_cmd) blocks = int(out.split()[0]) extra_blocks = part.get_extra_block_count(blocks) if extra_blocks < BOOTDD_EXTRA_SPACE: extra_blocks = BOOTDD_EXTRA_SPACE blocks += extra_blocks msger.debug("Added %d extra blocks to %s to get to %d total blocks" % \ (extra_blocks, part.mountpoint, blocks)) # dosfs image, created by mkdosfs bootimg = "%s/boot.img" % cr_workdir dosfs_cmd = "mkdosfs -n efi -C %s %d" % (bootimg, blocks) exec_native_cmd(dosfs_cmd, native_sysroot) mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (bootimg, hdddir) exec_native_cmd(mcopy_cmd, native_sysroot) chmod_cmd = "chmod 644 %s" % bootimg exec_cmd(chmod_cmd) du_cmd = "du -Lbks %s" % bootimg out = exec_cmd(du_cmd) bootimg_size = out.split()[0] part.size = int(bootimg_size) part.source_file = bootimg
def layout_partitions(self, ptable_format="msdos"): """ Layout the partitions, meaning calculate the position of every partition on the disk. The 'ptable_format' parameter defines the partition table format and may be "msdos". """ msger.debug("Assigning %s partitions to disks" % ptable_format) if ptable_format not in ('msdos'): raise ImageError("Unknown partition table format '%s', supported " \ "formats are: 'msdos'" % ptable_format) if self._partitions_layed_out: return self._partitions_layed_out = True # Go through partitions in the order they are added in .ks file for n in range(len(self.partitions)): p = self.partitions[n] if not self.disks.has_key(p['disk_name']): raise ImageError("No disk %s for partition %s" \ % (p['disk_name'], p['mountpoint'])) if p['part_type']: # The --part-type can also be implemented for MBR partitions, # in which case it would map to the 1-byte "partition type" # filed at offset 3 of the partition entry. raise ImageError("setting custom partition type is not " \ "implemented for msdos partitions") # Get the disk where the partition is located d = self.disks[p['disk_name']] d['numpart'] += 1 d['ptable_format'] = ptable_format if d['numpart'] == 1: if ptable_format == "msdos": overhead = MBR_OVERHEAD # Skip one sector required for the partitioning scheme overhead d['offset'] += overhead if p['align']: # If not first partition and we do have alignment set we need # to align the partition. # FIXME: This leaves a empty spaces to the disk. To fill the # gaps we could enlargea the previous partition? # Calc how much the alignment is off. align_sectors = d['offset'] % (p['align'] * 1024 / self.sector_size) if align_sectors: # If partition is not aligned as required, we need # to move forward to the next alignment point align_sectors = (p['align'] * 1024 / self.sector_size) - align_sectors msger.debug( "Realignment for %s%s with %s sectors, original" " offset %s, target alignment is %sK." % (p['disk_name'], d['numpart'], align_sectors, d['offset'], p['align'])) # increase the offset so we actually start the partition on right alignment d['offset'] += align_sectors p['start'] = d['offset'] d['offset'] += p['size'] p['type'] = 'primary' p['num'] = d['numpart'] if d['ptable_format'] == "msdos": if d['numpart'] > 2: # Every logical partition requires an additional sector for # the EBR, so steal the last sector from the end of each # partition starting from the 3rd one for the EBR. This # will make sure the logical partitions are aligned # correctly. p['size'] -= 1 if d['numpart'] > 3: p['type'] = 'logical' p['num'] = d['numpart'] + 1 d['partitions'].append(n) msger.debug("Assigned %s to %s%d, sectors range %d-%d size %d " "sectors (%d bytes)." \ % (p['mountpoint'], p['disk_name'], p['num'], p['start'], p['start'] + p['size'] - 1, p['size'], p['size'] * self.sector_size)) # Once all the partitions have been layed out, we can calculate the # minumim disk sizes. for disk_name, d in self.disks.items(): d['min_size'] = d['offset'] d['min_size'] *= self.sector_size
def __format_disks(self): self.layout_partitions() for dev in self.disks.keys(): disk = self.disks[dev] msger.debug("Initializing partition table for %s" % \ (disk['disk'].device)) exec_native_cmd("parted -s %s mklabel %s" % \ (disk['disk'].device, disk['ptable_format']), self.native_sysroot) msger.debug("Creating partitions") for part in self.partitions: if part['num'] == 0: continue disk = self.disks[part['disk_name']] if disk['ptable_format'] == "msdos" and part['num'] == 5: # Create an extended partition (note: extended # partition is described in MBR and contains all # logical partitions). The logical partitions save a # sector for an EBR just before the start of a # partition. The extended partition must start one # sector before the start of the first logical # partition. This way the first EBR is inside of the # extended partition. Since the extended partitions # starts a sector before the first logical partition, # add a sector at the back, so that there is enough # room for all logical partitions. self.__create_partition(disk['disk'].device, "extended", None, part['start'] - 1, disk['offset'] - part['start'] + 1) if part['fstype'] == "swap": parted_fs_type = "linux-swap" elif part['fstype'] == "vfat": parted_fs_type = "fat32" elif part['fstype'] == "msdos": parted_fs_type = "fat16" elif part['fstype'] == "ontrackdm6aux3": parted_fs_type = "ontrackdm6aux3" else: # Type for ext2/ext3/ext4/btrfs parted_fs_type = "ext2" # Boot ROM of OMAP boards require vfat boot partition to have an # even number of sectors. if part['mountpoint'] == "/boot" and part['fstype'] in ["vfat", "msdos"] \ and part['size'] % 2: msger.debug("Substracting one sector from '%s' partition to " \ "get even number of sectors for the partition" % \ part['mountpoint']) part['size'] -= 1 self.__create_partition(disk['disk'].device, part['type'], parted_fs_type, part['start'], part['size']) if part['part_type']: msger.debug("partition %d: set type UID to %s" % \ (part['num'], part['part_type'])) exec_native_cmd("sgdisk --typecode=%d:%s %s" % \ (part['num'], part['part_type'], disk['disk'].device), self.native_sysroot) if part['uuid']: msger.debug("partition %d: set UUID to %s" % \ (part['num'], part['uuid'])) exec_native_cmd("sgdisk --partition-guid=%d:%s %s" % \ (part['num'], part['uuid'], disk['disk'].device), self.native_sysroot) if part['boot']: flag_name = "legacy_boot" if disk[ 'ptable_format'] == 'gpt' else "boot" msger.debug("Set '%s' flag for partition '%s' on disk '%s'" % \ (flag_name, part['num'], disk['disk'].device)) exec_native_cmd("parted -s %s set %d %s on" % \ (disk['disk'].device, part['num'], flag_name), self.native_sysroot) # Parted defaults to enabling the lba flag for fat16 partitions, # which causes compatibility issues with some firmware (and really # isn't necessary). if parted_fs_type == "fat16": if disk['ptable_format'] == 'msdos': msger.debug("Disable 'lba' flag for partition '%s' on disk '%s'" % \ (part['num'], disk['disk'].device)) exec_native_cmd("parted -s %s set %d lba off" % \ (disk['disk'].device, part['num']), self.native_sysroot)
def do_prepare_partition(cls, part, source_params, cr, cr_workdir, oe_builddir, bootimg_dir, kernel_dir, rootfs_dir, native_sysroot): """ Called to do the actual content population for a partition i.e. it 'prepares' the partition to be incorporated into the image. In this case, prepare content for an EFI (grub) boot partition. """ if not bootimg_dir: bootimg_dir = get_bitbake_var("HDDDIR") if not bootimg_dir: msger.error("Couldn't find HDDDIR, exiting\n") # just so the result notes display it cr.set_bootimg_dir(bootimg_dir) staging_kernel_dir = kernel_dir hdddir = "%s/hdd/boot" % cr_workdir install_cmd = "install -m 0644 %s/bzImage %s/bzImage" % \ (staging_kernel_dir, hdddir) exec_cmd(install_cmd) try: if source_params['loader'] == 'grub-efi': shutil.copyfile("%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir, "%s/grub.cfg" % cr_workdir) cp_cmd = "cp %s/EFI/BOOT/* %s/EFI/BOOT" % (bootimg_dir, hdddir) exec_cmd(cp_cmd, True) shutil.move("%s/grub.cfg" % cr_workdir, "%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir) elif source_params['loader'] == 'gummiboot': cp_cmd = "cp %s/EFI/BOOT/* %s/EFI/BOOT" % (bootimg_dir, hdddir) exec_cmd(cp_cmd, True) else: msger.error("unrecognized bootimg-efi loader: %s" % source_params['loader']) except KeyError: msger.error("bootimg-efi requires a loader, none specified") du_cmd = "du -bks %s" % hdddir out = exec_cmd(du_cmd) blocks = int(out.split()[0]) extra_blocks = part.get_extra_block_count(blocks) if extra_blocks < BOOTDD_EXTRA_SPACE: extra_blocks = BOOTDD_EXTRA_SPACE blocks += extra_blocks msger.debug("Added %d extra blocks to %s to get to %d total blocks" % \ (extra_blocks, part.mountpoint, blocks)) # Ensure total sectors is an integral number of sectors per # track or mcopy will complain. Sectors are 512 bytes, and we # generate images with 32 sectors per track. This calculation is # done in blocks, thus the mod by 16 instead of 32. blocks += (16 - (blocks % 16)) # dosfs image, created by mkdosfs bootimg = "%s/boot.img" % cr_workdir dosfs_cmd = "mkdosfs -n efi -C %s %d" % (bootimg, blocks) exec_native_cmd(dosfs_cmd, native_sysroot) mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (bootimg, hdddir) exec_native_cmd(mcopy_cmd, native_sysroot) chmod_cmd = "chmod 644 %s" % bootimg exec_cmd(chmod_cmd) du_cmd = "du -Lbks %s" % bootimg out = exec_cmd(du_cmd) bootimg_size = out.split()[0] part.set_size(bootimg_size) part.set_source_file(bootimg)
def do_prepare_partition(cls, part, source_params, creator, cr_workdir, oe_builddir, bootimg_dir, kernel_dir, rootfs_dir, native_sysroot): """ Called to do the actual content population for a partition i.e. it 'prepares' the partition to be incorporated into the image. In this case, prepare content for legacy bios boot partition. """ def _has_syslinux(dirname): if dirname: syslinux = "%s/syslinux" % dirname if os.path.exists(syslinux): return True return False if not _has_syslinux(bootimg_dir): bootimg_dir = get_bitbake_var("STAGING_DATADIR") if not bootimg_dir: msger.error("Couldn't find STAGING_DATADIR, exiting\n") if not _has_syslinux(bootimg_dir): msger.error("Please build syslinux first\n") # just so the result notes display it creator.set_bootimg_dir(bootimg_dir) staging_kernel_dir = kernel_dir hdddir = "%s/hdd/boot" % cr_workdir install_cmd = "install -m 0644 %s/bzImage %s/vmlinuz" \ % (staging_kernel_dir, hdddir) exec_cmd(install_cmd) install_cmd = "install -m 444 %s/syslinux/ldlinux.sys %s/ldlinux.sys" \ % (bootimg_dir, hdddir) exec_cmd(install_cmd) du_cmd = "du -bks %s" % hdddir out = exec_cmd(du_cmd) blocks = int(out.split()[0]) extra_blocks = part.get_extra_block_count(blocks) if extra_blocks < BOOTDD_EXTRA_SPACE: extra_blocks = BOOTDD_EXTRA_SPACE blocks += extra_blocks msger.debug("Added %d extra blocks to %s to get to %d total blocks" % \ (extra_blocks, part.mountpoint, blocks)) # dosfs image, created by mkdosfs bootimg = "%s/boot.img" % cr_workdir dosfs_cmd = "mkdosfs -n boot -S 512 -C %s %d" % (bootimg, blocks) exec_native_cmd(dosfs_cmd, native_sysroot) mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (bootimg, hdddir) exec_native_cmd(mcopy_cmd, native_sysroot) syslinux_cmd = "syslinux %s" % bootimg exec_native_cmd(syslinux_cmd, native_sysroot) chmod_cmd = "chmod 644 %s" % bootimg exec_cmd(chmod_cmd) du_cmd = "du -Lbks %s" % bootimg out = exec_cmd(du_cmd) bootimg_size = out.split()[0] part.size = int(out.split()[0]) part.source_file = bootimg
def layout_partitions(self, ptable_format="msdos"): """ Layout the partitions, meaning calculate the position of every partition on the disk. The 'ptable_format' parameter defines the partition table format and may be "msdos". """ msger.debug("Assigning %s partitions to disks" % ptable_format) if self._partitions_layed_out: return self._partitions_layed_out = True # Go through partitions in the order they are added in .ks file for num in range(len(self.partitions)): part = self.partitions[num] if not self.disks.has_key(part['disk_name']): raise ImageError("No disk %s for partition %s" \ % (part['disk_name'], part['mountpoint'])) if ptable_format == 'msdos' and part['part_type']: # The --part-type can also be implemented for MBR partitions, # in which case it would map to the 1-byte "partition type" # filed at offset 3 of the partition entry. raise ImageError("setting custom partition type is not " \ "implemented for msdos partitions") # Get the disk where the partition is located disk = self.disks[part['disk_name']] disk['numpart'] += 1 if not part['no_table']: disk['realpart'] += 1 disk['ptable_format'] = ptable_format if disk['numpart'] == 1: if ptable_format == "msdos": overhead = MBR_OVERHEAD elif ptable_format == "gpt": overhead = GPT_OVERHEAD # Skip one sector required for the partitioning scheme overhead disk['offset'] += overhead if disk['realpart'] > 3: # Reserve a sector for EBR for every logical partition # before alignment is performed. if ptable_format == "msdos": disk['offset'] += 1 if part['align']: # If not first partition and we do have alignment set we need # to align the partition. # FIXME: This leaves a empty spaces to the disk. To fill the # gaps we could enlargea the previous partition? # Calc how much the alignment is off. align_sectors = disk['offset'] % (part['align'] * 1024 / self.sector_size) if align_sectors: # If partition is not aligned as required, we need # to move forward to the next alignment point align_sectors = (part['align'] * 1024 / self.sector_size) - align_sectors msger.debug( "Realignment for %s%s with %s sectors, original" " offset %s, target alignment is %sK." % (part['disk_name'], disk['numpart'], align_sectors, disk['offset'], part['align'])) # increase the offset so we actually start the partition on right alignment disk['offset'] += align_sectors part['start'] = disk['offset'] disk['offset'] += part['size'] part['type'] = 'primary' if not part['no_table']: part['num'] = disk['realpart'] else: part['num'] = 0 if disk['ptable_format'] == "msdos": if disk['realpart'] > 3: part['type'] = 'logical' part['num'] = disk['realpart'] + 1 disk['partitions'].append(num) msger.debug("Assigned %s to %s%d, sectors range %d-%d size %d " "sectors (%d bytes)." \ % (part['mountpoint'], part['disk_name'], part['num'], part['start'], part['start'] + part['size'] - 1, part['size'], part['size'] * self.sector_size)) # Once all the partitions have been layed out, we can calculate the # minumim disk sizes. for disk in self.disks.values(): disk['min_size'] = disk['offset'] if disk['ptable_format'] == "gpt": disk['min_size'] += GPT_OVERHEAD disk['min_size'] *= self.sector_size
def _create(self): """ For 'wic', we already have our build artifacts - we just create filesystems from the artifacts directly and combine them into a partitioned image. """ parts = self._get_parts() self.__image = Image(self.native_sysroot) disk_ids = {} for num, part in enumerate(parts, 1): # as a convenience, set source to the boot partition source # instead of forcing it to be set via bootloader --source if not self.ks.bootloader.source and part.mountpoint == "/boot": self.ks.bootloader.source = part.source # generate parition UUIDs if not part.uuid and part.use_uuid: if self.ptable_format == 'gpt': part.uuid = str(uuid.uuid4()) else: # msdos partition table if part.disk not in disk_ids: disk_ids[part.disk] = int.from_bytes( os.urandom(4), 'little') disk_id = disk_ids[part.disk] part.uuid = '%0x-%02d' % (disk_id, self.__get_part_num(num, parts)) fstab_path = self._write_fstab(self.rootfs_dir.get("ROOTFS_DIR")) shutil.rmtree(self.workdir) os.mkdir(self.workdir) for part in parts: # get rootfs size from bitbake variable if it's not set in .ks file if not part.size: # and if rootfs name is specified for the partition image_name = part.rootfs_dir if image_name: # Bitbake variable ROOTFS_SIZE is calculated in # Image._get_rootfs_size method from meta/lib/oe/image.py # using IMAGE_ROOTFS_SIZE, IMAGE_ROOTFS_ALIGNMENT, # IMAGE_OVERHEAD_FACTOR and IMAGE_ROOTFS_EXTRA_SPACE rsize_bb = get_bitbake_var('ROOTFS_SIZE', image_name) if rsize_bb: part.size = int(round(float(rsize_bb))) # need to create the filesystems in order to get their # sizes before we can add them and do the layout. # Image.create() actually calls __format_disks() to create # the disk images and carve out the partitions, then # self.assemble() calls Image.assemble() which calls # __write_partitition() for each partition to dd the fs # into the partitions. part.prepare(self, self.workdir, self.oe_builddir, self.rootfs_dir, self.bootimg_dir, self.kernel_dir, self.native_sysroot) self.__image.add_partition(part.disk_size, part.disk, part.mountpoint, part.source_file, part.fstype, part.label, fsopts=part.fsopts, boot=part.active, align=part.align, no_table=part.no_table, part_type=part.part_type, uuid=part.uuid, system_id=part.system_id) if fstab_path: shutil.move(fstab_path + ".orig", fstab_path) self.__image.layout_partitions(self.ptable_format) self.__imgdir = self.workdir for disk_name, disk in self.__image.disks.items(): full_path = self._full_path(self.__imgdir, disk_name, "direct") msger.debug("Adding disk %s as %s with size %s bytes" \ % (disk_name, full_path, disk['min_size'])) disk_obj = DiskImage(full_path, disk['min_size']) self.__disks[disk_name] = disk_obj self.__image.add_disk(disk_name, disk_obj, disk_ids.get(disk_name)) self.__image.create()
def _create(self): """ For 'wic', we already have our build artifacts - we just create filesystems from the artifacts directly and combine them into a partitioned image. """ parts = self._get_parts() self.__image = Image(self.native_sysroot) for p in parts: # as a convenience, set source to the boot partition source # instead of forcing it to be set via bootloader --source if not self.ks.handler.bootloader.source and p.mountpoint == "/boot": self.ks.handler.bootloader.source = p.source fstab_path = self._write_fstab(self.rootfs_dir.get("ROOTFS_DIR")) for p in parts: # get rootfs size from bitbake variable if it's not set in .ks file if not p.size: # and if rootfs name is specified for the partition image_name = p.get_rootfs() if image_name: # Bitbake variable ROOTFS_SIZE is calculated in # Image._get_rootfs_size method from meta/lib/oe/image.py # using IMAGE_ROOTFS_SIZE, IMAGE_ROOTFS_ALIGNMENT, # IMAGE_OVERHEAD_FACTOR and IMAGE_ROOTFS_EXTRA_SPACE rsize_bb = get_bitbake_var('ROOTFS_SIZE', image_name) if rsize_bb: # convert from Kb to Mb p.size = int(rsize_bb) / 1024 # need to create the filesystems in order to get their # sizes before we can add them and do the layout. # Image.create() actually calls __format_disks() to create # the disk images and carve out the partitions, then # self.assemble() calls Image.assemble() which calls # __write_partitition() for each partition to dd the fs # into the partitions. p.prepare(self, self.workdir, self.oe_builddir, self.rootfs_dir, self.bootimg_dir, self.kernel_dir, self.native_sysroot) self.__image.add_partition(int(p.size), p.disk, p.mountpoint, p.source_file, p.fstype, p.label, fsopts=p.fsopts, boot=p.active, align=p.align, no_table=p.no_table, part_type=p.part_type, uuid=p.uuid) if fstab_path: shutil.move(fstab_path + ".orig", fstab_path) self.__image.layout_partitions(self.ptable_format) self.__imgdir = self.workdir for disk_name, disk in self.__image.disks.items(): full_path = self._full_path(self.__imgdir, disk_name, "direct") msger.debug("Adding disk %s as %s with size %s bytes" \ % (disk_name, full_path, disk['min_size'])) disk_obj = fs_related.DiskImage(full_path, disk['min_size']) self.__disks[disk_name] = disk_obj self.__image.add_disk(disk_name, disk_obj) self.__image.create()
def do_configure_partition(cls, part, source_params, creator, cr_workdir, oe_builddir, bootimg_dir, kernel_dir, native_sysroot): """ Called before do_prepare_partition(), creates syslinux config """ hdddir = "%s/hdd/boot" % cr_workdir rm_cmd = "rm -rf " + cr_workdir exec_cmd(rm_cmd) install_cmd = "install -d %s" % hdddir exec_cmd(install_cmd) configfile = kickstart.get_bootloader_file(creator.ks) custom_cfg = None if configfile: custom_cfg = get_custom_config(configfile) if custom_cfg: # Use a custom configuration for grub syslinux_conf = custom_cfg msger.debug("Using custom configuration file " "%s for syslinux.cfg" % configfile) else: msger.error("configfile is specified but failed to " "get it from %s." % configfile) if not custom_cfg: # Create syslinux configuration using parameters from wks file splash = os.path.join(cr_workdir, "/hdd/boot/splash.jpg") if os.path.exists(splash): splashline = "menu background splash.jpg" else: splashline = "" options = creator.ks.handler.bootloader.appendLine syslinux_conf = "" syslinux_conf += "PROMPT 0\n" timeout = kickstart.get_timeout(creator.ks) if not timeout: timeout = 0 syslinux_conf += "TIMEOUT " + str(timeout) + "\n" syslinux_conf += "\n" syslinux_conf += "ALLOWOPTIONS 1\n" syslinux_conf += "SERIAL 0 115200\n" syslinux_conf += "\n" if splashline: syslinux_conf += "%s\n" % splashline syslinux_conf += "DEFAULT boot\n" syslinux_conf += "LABEL boot\n" kernel = "/vmlinuz" syslinux_conf += "KERNEL " + kernel + "\n" syslinux_conf += "APPEND label=boot root=%s %s\n" % \ (creator.rootdev, options) msger.debug("Writing syslinux config %s/hdd/boot/syslinux.cfg" \ % cr_workdir) cfg = open("%s/hdd/boot/syslinux.cfg" % cr_workdir, "w") cfg.write(syslinux_conf) cfg.close()