def prepare_rootfs_msdos(self, rootfs, oe_builddir, rootfs_dir, native_sysroot, pseudo): """ Prepare content for a msdos/vfat rootfs partition. """ du_cmd = "du -bks %s" % rootfs_dir out = exec_cmd(du_cmd) blocks = int(out.split()[0]) rootfs_size = self.get_rootfs_size(blocks) label_str = "-n boot" if self.label: label_str = "-n %s" % self.label size_str = "" if self.fstype == 'msdos': size_str = "-F 16" # FAT 16 dosfs_cmd = "mkdosfs %s -S 512 %s -C %s %d" % (label_str, size_str, rootfs, rootfs_size) exec_native_cmd(dosfs_cmd, native_sysroot) mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (rootfs, rootfs_dir) exec_native_cmd(mcopy_cmd, native_sysroot) chmod_cmd = "chmod 644 %s" % rootfs exec_cmd(chmod_cmd)
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.ks.bootloader.source disk_name = self.parts[0].disk if source_plugin: plugin = PluginMgr.get_plugins('source')[source_plugin] plugin.do_install_disk(self._image, disk_name, self, self.workdir, self.oe_builddir, self.bootimg_dir, self.kernel_dir, self.native_sysroot) full_path = self._image.path # Generate .bmap if self.bmap: logger.debug("Generating bmap file for %s", disk_name) exec_native_cmd("bmaptool create %s -o %s.bmap" % (full_path, full_path), self.native_sysroot) # Compress the image if self.compressor: logger.debug("Compressing disk %s with %s", disk_name, self.compressor) exec_cmd("%s %s" % (self.compressor, full_path))
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 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 logger.debug("Using custom configuration file %s " "for syslinux.cfg", bootloader.configfile) else: raise WicError("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 = get_bitbake_var("KERNEL_IMAGE") initrd = get_bitbake_var("INITRD_IMAGE") syslinux_conf += "KERNEL " + kernel + "\n" syslinux_conf += "APPEND label=boot root=%s initrd=%s %s\n" % \ (creator.rootdev, initrd, bootloader.append) logger.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 _build_initramfs_path(rootfs_dir, cr_workdir): """ Create path for initramfs image """ initrd = get_bitbake_var("INITRD_LIVE") or get_bitbake_var("INITRD") if not initrd: initrd_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") if not initrd_dir: raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting.") image_name = get_bitbake_var("IMAGE_BASENAME") if not image_name: raise WicError("Couldn't find IMAGE_BASENAME, exiting.") image_type = get_bitbake_var("INITRAMFS_FSTYPES") if not image_type: raise WicError("Couldn't find INITRAMFS_FSTYPES, exiting.") target_arch = get_bitbake_var("TRANSLATED_TARGET_ARCH") if not target_arch: raise WicError("Couldn't find TRANSLATED_TARGET_ARCH, exiting.") initrd = glob.glob('%s/%s*%s.%s' % (initrd_dir, image_name, target_arch, image_type))[0] if not os.path.exists(initrd): # Create initrd from rootfs directory initrd = "%s/initrd.cpio.gz" % cr_workdir initrd_dir = "%s/INITRD" % cr_workdir shutil.copytree("%s" % rootfs_dir, \ "%s" % initrd_dir, symlinks=True) if os.path.isfile("%s/init" % rootfs_dir): shutil.copy2("%s/init" % rootfs_dir, "%s/init" % initrd_dir) elif os.path.lexists("%s/init" % rootfs_dir): os.symlink(os.readlink("%s/init" % rootfs_dir), \ "%s/init" % initrd_dir) elif os.path.isfile("%s/sbin/init" % rootfs_dir): shutil.copy2("%s/sbin/init" % rootfs_dir, \ "%s" % initrd_dir) elif os.path.lexists("%s/sbin/init" % rootfs_dir): os.symlink(os.readlink("%s/sbin/init" % rootfs_dir), \ "%s/init" % initrd_dir) else: raise WicError("Couldn't find or build initrd, exiting.") exec_cmd("cd %s && find . | cpio -o -H newc -R +0:+0 >./initrd.cpio " \ % initrd_dir, as_shell=True) exec_cmd("gzip -f -9 -c %s/initrd.cpio > %s" \ % (cr_workdir, initrd), as_shell=True) shutil.rmtree(initrd_dir) return initrd
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 kernel_dir: kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") if not kernel_dir: raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") logger.debug('Kernel dir: %s', kernel_dir) if 'file' not in source_params: raise WicError("No file specified") src = os.path.join(kernel_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=int(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 = int(out.split()[0]) if filesize > part.size: part.size = filesize part.source_file = dst
def prepare_rootfs_ext(self, rootfs, oe_builddir, rootfs_dir, native_sysroot, pseudo): """ Prepare content for an ext2/3/4 rootfs partition. """ du_cmd = "du -ks %s" % rootfs_dir out = exec_cmd(du_cmd) actual_rootfs_size = int(out.split()[0]) rootfs_size = self.get_rootfs_size(actual_rootfs_size) with open(rootfs, 'w') as sparse: os.ftruncate(sparse.fileno(), rootfs_size * 1024) extra_imagecmd = "-i 8192" label_str = "" if self.label: label_str = "-L %s" % self.label mkfs_cmd = "mkfs.%s -F %s %s %s -d %s" % \ (self.fstype, extra_imagecmd, rootfs, label_str, rootfs_dir) exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo) mkfs_cmd = "fsck.%s -fy %s" % (self.fstype, rootfs) exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
def prepare_empty_partition_vfat(self, rootfs, oe_builddir, native_sysroot): """ Prepare an empty vfat partition. """ blocks = self.disk_size label_str = "-n boot" if self.label: label_str = "-n %s" % self.label dosfs_cmd = "mkdosfs %s -S 512 -C %s %d" % (label_str, rootfs, blocks) exec_native_cmd(dosfs_cmd, native_sysroot) chmod_cmd = "chmod 644 %s" % rootfs exec_cmd(chmod_cmd)
def prepare_empty_partition_squashfs(self, cr_workdir, oe_builddir, native_sysroot): """ Prepare an empty squashfs partition. """ logger.warning("Creating of an empty squashfs %s partition was attempted. " "Proceeding as requested.", self.mountpoint) path = "%s/fs_%s.%s" % (cr_workdir, self.label, self.fstype) if os.path.isfile(path): os.remove(path) # it is not possible to create a squashfs without source data, # thus prepare an empty temp dir that is used as source tmpdir = tempfile.mkdtemp() squashfs_cmd = "mksquashfs %s %s -noappend" % \ (tmpdir, path) exec_native_cmd(squashfs_cmd, native_sysroot) os.rmdir(tmpdir) # get the rootfs size in the right units for kickstart (kB) du_cmd = "du -Lbks %s" % path out = exec_cmd(du_cmd) fs_size = out.split()[0] self.size = int(fs_size)
def prepare_empty_partition_squashfs(self, cr_workdir, oe_builddir, native_sysroot): """ Prepare an empty squashfs partition. """ msger.warning("Creating of an empty squashfs %s partition was attempted. " \ "Proceeding as requested." % self.mountpoint) path = "%s/fs_%s.%s" % (cr_workdir, self.label, self.fstype) if os.path.isfile(path): os.remove(path) # it is not possible to create a squashfs without source data, # thus prepare an empty temp dir that is used as source tmpdir = tempfile.mkdtemp() squashfs_cmd = "mksquashfs %s %s -noappend" % \ (tmpdir, path) exec_native_cmd(squashfs_cmd, native_sysroot) os.rmdir(tmpdir) # get the rootfs size in the right units for kickstart (kB) du_cmd = "du -Lbks %s" % path out = exec_cmd(du_cmd) fs_size = out.split()[0] self.size = int(fs_size)
def do_prepare_partition(cls, part, source_params, image_creator, image_creator_workdir, oe_builddir, bootimg_dir, kernel_dir, krootfs_dir, native_sysroot): """ Creates partition out of rootfs directory Prepare content for a rootfs partition i.e. create a partition and fill it from a /rootfs dir. Install syslinux bootloader into root partition image file """ def is_exe(exepath): """Verify exepath is an executable file""" return os.path.isfile(exepath) and os.access(exepath, os.X_OK) # Make sure syslinux-nomtools is available in native sysroot or fail native_syslinux_nomtools = os.path.join(native_sysroot, "usr/bin/syslinux-nomtools") if not is_exe(native_syslinux_nomtools): msger.info("building syslinux-native...") exec_cmd("bitbake syslinux-native") if not is_exe(native_syslinux_nomtools): msger.error("Couldn't find syslinux-nomtools (%s), exiting\n" % native_syslinux_nomtools) if part.rootfs is None: if 'ROOTFS_DIR' not in krootfs_dir: msger.error("Couldn't find --rootfs-dir, exiting") rootfs_dir = krootfs_dir['ROOTFS_DIR'] else: if part.rootfs in krootfs_dir: rootfs_dir = krootfs_dir[part.rootfs] elif part.rootfs: rootfs_dir = part.rootfs else: msg = "Couldn't find --rootfs-dir=%s connection" msg += " or it is not a valid path, exiting" msger.error(msg % part.rootfs) real_rootfs_dir = cls._get_rootfs_dir(rootfs_dir) part.rootfs_dir = real_rootfs_dir part.prepare_rootfs(image_creator_workdir, oe_builddir, real_rootfs_dir, native_sysroot) # install syslinux into rootfs partition syslinux_cmd = "syslinux-nomtools -d /boot -i %s" % part.source_file exec_native_cmd(syslinux_cmd, native_sysroot)
def prepare_empty_partition_ext(self, rootfs, oe_builddir): """ Prepare an empty ext2/3/4 partition. """ size = self.disk_size with open(rootfs, 'w') as sparse: os.ftruncate(sparse.fileno(), size * 1024) extra_imagecmd = "-i 8192" label_str = "" if self.label: label_str = "-L %s" % self.label mkfs_cmd = "sudo mkfs.%s -F %s %s %s" % \ (self.fstype, extra_imagecmd, label_str, rootfs) exec_cmd(mkfs_cmd)
def prepare_rootfs_msdos(self, rootfs, oe_builddir, rootfs_dir): """ Prepare content for a msdos/vfat rootfs partition. """ du_cmd = "sudo du -bks %s" % rootfs_dir out = exec_cmd(du_cmd) blocks = int(out.split()[0]) rootfs_size = self.get_rootfs_size(blocks) label_str = "-n boot" if self.label: label_str = "-n %s" % self.label size_str = "" if self.fstype == 'msdos': size_str = "-F 16" # FAT 16 dosfs_cmd = "mkdosfs %s -S 512 %s -C %s %d" % (label_str, size_str, rootfs, rootfs_size) exec_cmd(dosfs_cmd) mcopy_cmd = "env MTOOLS_SKIP_CHECK=1 mcopy -i %s -s %s/* ::/" % \ (rootfs, rootfs_dir) exec_cmd(mcopy_cmd, True) chmod_cmd = "chmod 644 %s" % rootfs exec_cmd(chmod_cmd)
def prepare_rootfs(self, cr_workdir, oe_builddir, rootfs_dir, native_sysroot): """ Prepare content for a rootfs partition i.e. create a partition and fill it from a /rootfs dir. Currently handles ext2/3/4, btrfs and vfat. """ p_prefix = os.environ.get("PSEUDO_PREFIX", "%s/usr" % native_sysroot) p_localstatedir = os.environ.get("PSEUDO_LOCALSTATEDIR", "%s/../pseudo" % rootfs_dir) p_passwd = os.environ.get("PSEUDO_PASSWD", rootfs_dir) p_nosymlinkexp = os.environ.get("PSEUDO_NOSYMLINKEXP", "1") pseudo = "export PSEUDO_PREFIX=%s;" % p_prefix pseudo += "export PSEUDO_LOCALSTATEDIR=%s;" % p_localstatedir pseudo += "export PSEUDO_PASSWD=%s;" % p_passwd pseudo += "export PSEUDO_NOSYMLINKEXP=%s;" % p_nosymlinkexp pseudo += "%s " % get_bitbake_var("FAKEROOTCMD") rootfs = "%s/rootfs_%s.%s.%s" % (cr_workdir, self.label, self.lineno, self.fstype) if os.path.isfile(rootfs): os.remove(rootfs) if not self.fstype: raise WicError("File system for partition %s not specified in " "kickstart, use --fstype option" % self.mountpoint) # Get rootfs size from bitbake variable if it's not set in .ks file if not self.size: # 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') if rsize_bb: logger.warning( 'overhead-factor was specified, but size was not,' ' so bitbake variables will be used for the size.' ' In this case both IMAGE_OVERHEAD_FACTOR and ' '--overhead-factor will be applied') self.size = int(round(float(rsize_bb))) for prefix in ("ext", "btrfs", "vfat", "squashfs"): if self.fstype.startswith(prefix): method = getattr(self, "prepare_rootfs_" + prefix) method(rootfs, oe_builddir, rootfs_dir, native_sysroot, pseudo) self.source_file = rootfs # get the rootfs size in the right units for kickstart (kB) du_cmd = "du -Lbks %s" % rootfs out = exec_cmd(du_cmd) self.size = int(out.split()[0]) break
def do_configure_partition(cls, part, source_params, creator, cr_workdir, oe_builddir, bootimg_dir, kernel_dir): """ Called before do_prepare_partition(), creates loader-specific config """ isodir = "%s/ISO/" % cr_workdir if os.path.exists(cr_workdir): shutil.rmtree(cr_workdir) install_cmd = "install -d %s " % isodir exec_cmd(install_cmd) # Overwrite the name of the created image logger.debug(source_params) if 'image_name' in source_params and \ source_params['image_name'].strip(): creator.name = source_params['image_name'].strip() logger.debug("The name of the image is: %s", creator.name)
def prepare_rootfs(self, cr_workdir, oe_builddir, rootfs_dir, native_sysroot): """ Prepare content for a rootfs partition i.e. create a partition and fill it from a /rootfs dir. Currently handles ext2/3/4, btrfs and vfat. """ p_prefix = os.environ.get("PSEUDO_PREFIX", "%s/usr" % native_sysroot) p_localstatedir = os.environ.get("PSEUDO_LOCALSTATEDIR", "%s/../pseudo" % rootfs_dir) p_passwd = os.environ.get("PSEUDO_PASSWD", rootfs_dir) p_nosymlinkexp = os.environ.get("PSEUDO_NOSYMLINKEXP", "1") pseudo = "export PSEUDO_PREFIX=%s;" % p_prefix pseudo += "export PSEUDO_LOCALSTATEDIR=%s;" % p_localstatedir pseudo += "export PSEUDO_PASSWD=%s;" % p_passwd pseudo += "export PSEUDO_NOSYMLINKEXP=%s;" % p_nosymlinkexp pseudo += "%s " % get_bitbake_var("FAKEROOTCMD") rootfs = "%s/rootfs_%s.%s.%s" % (cr_workdir, self.label, self.lineno, self.fstype) if os.path.isfile(rootfs): os.remove(rootfs) if not self.fstype: raise WicError("File system for partition %s not specified in " "kickstart, use --fstype option" % self.mountpoint) # Get rootfs size from bitbake variable if it's not set in .ks file if not self.size: # 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') if rsize_bb: logger.warning('overhead-factor was specified, but size was not,' ' so bitbake variables will be used for the size.' ' In this case both IMAGE_OVERHEAD_FACTOR and ' '--overhead-factor will be applied') self.size = int(round(float(rsize_bb))) for prefix in ("ext", "btrfs", "vfat", "squashfs"): if self.fstype.startswith(prefix): method = getattr(self, "prepare_rootfs_" + prefix) method(rootfs, oe_builddir, rootfs_dir, native_sysroot, pseudo) self.source_file = rootfs # get the rootfs size in the right units for kickstart (kB) du_cmd = "du -Lbks %s" % rootfs out = exec_cmd(du_cmd) self.size = int(out.split()[0]) break
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 loader-specific config """ isodir = "%s/ISO/" % cr_workdir if os.path.exists(cr_workdir): shutil.rmtree(cr_workdir) install_cmd = "install -d %s " % isodir exec_cmd(install_cmd) # Overwrite the name of the created image logger.debug(source_params) if 'image_name' in source_params and \ source_params['image_name'].strip(): creator.name = source_params['image_name'].strip() logger.debug("The name of the image is: %s", creator.name)
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 loader-specific config """ hdddir = "%s/hdd/boot" % cr_workdir install_cmd = "install -d %s/EFI/BOOT" % hdddir exec_cmd(install_cmd) try: if source_params['loader'] == 'grub-efi': cls.do_configure_grubefi(creator, cr_workdir) elif source_params['loader'] == 'systemd-boot': cls.do_configure_systemdboot(hdddir, creator, cr_workdir, source_params) else: raise WicError("unrecognized bootimg-efi-isar loader: %s" % source_params['loader']) except KeyError: raise WicError("bootimg-efi-isar requires a loader, none specified")
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 loader-specific config """ hdddir = "%s/hdd/boot" % cr_workdir install_cmd = "install -d %s/EFI/BOOT" % hdddir exec_cmd(install_cmd) try: if source_params['loader'] == 'grub-efi': cls.do_configure_grubefi(creator, cr_workdir) elif source_params['loader'] == 'systemd-boot': cls.do_configure_systemdboot(hdddir, creator, cr_workdir, source_params) else: msger.error("unrecognized bootimg-efi loader: %s" % source_params['loader']) except KeyError: msger.error("bootimg-efi requires a loader, none specified")
def prepare_empty_partition_msdos(self, rootfs, oe_builddir): """ Prepare an empty vfat partition. """ blocks = self.disk_size label_str = "-n boot" if self.label: label_str = "-n %s" % self.label size_str = "" if self.fstype == 'msdos': size_str = "-F 16" # FAT 16 dosfs_cmd = "mkdosfs %s -S 512 %s -C %s %d" % (label_str, size_str, rootfs, blocks) exec_cmd(dosfs_cmd) chmod_cmd = "chmod 644 %s" % rootfs exec_cmd(chmod_cmd)
def prepare_rootfs_from_fs_image(self, cr_workdir, oe_builddir, rootfs_dir): """ Handle an already-created partition e.g. xxx.ext3 """ rootfs = oe_builddir du_cmd = "du -Lbks %s" % rootfs out = exec_cmd(du_cmd) rootfs_size = out.split()[0] self.size = int(rootfs_size) self.source_file = rootfs
def partitions(self): if self._partitions is None: self._partitions = OrderedDict() out = exec_cmd("%s -sm %s unit B print" % (self.parted, self.imagepath)) parttype = namedtuple("Part", "pnum start end size fstype") for line in out.splitlines()[2:]: pnum, start, end, size, fstype = line.split(':')[:5] partition = parttype(pnum, int(start[:-1]), int(end[:-1]), int(size[:-1]), fstype) self._partitions[pnum] = partition return self._partitions
def prepare_empty_partition_msdos(self, rootfs, oe_builddir, native_sysroot): """ Prepare an empty vfat partition. """ blocks = self.disk_size label_str = "-n boot" if self.label: label_str = "-n %s" % self.label size_str = "" if self.fstype == 'msdos': size_str = "-F 16" # FAT 16 dosfs_cmd = "mkdosfs %s -S 512 %s -C %s %d" % (label_str, size_str, rootfs, blocks) exec_native_cmd(dosfs_cmd, native_sysroot) chmod_cmd = "chmod 644 %s" % rootfs exec_cmd(chmod_cmd)
def prepare_rootfs_btrfs(self, rootfs, oe_builddir, rootfs_dir): """ Prepare content for a btrfs rootfs partition. Currently handles ext2/3/4 and btrfs. """ du_cmd = "sudo du -ks %s" % rootfs_dir out = exec_cmd(du_cmd) actual_rootfs_size = int(out.split()[0]) rootfs_size = self.get_rootfs_size(actual_rootfs_size) with open(rootfs, 'w') as sparse: os.ftruncate(sparse.fileno(), rootfs_size * 1024) label_str = "" if self.label: label_str = "-L %s" % self.label mkfs_cmd = "sudo mkfs.%s -b %d -r %s %s %s" % \ (self.fstype, rootfs_size * 1024, rootfs_dir, label_str, rootfs) exec_cmd(mkfs_cmd)
def prepare_rootfs_vfat(self, rootfs, oe_builddir, rootfs_dir, native_sysroot, pseudo): """ Prepare content for a vfat rootfs partition. """ du_cmd = "du -bks %s" % rootfs_dir out = exec_cmd(du_cmd) blocks = int(out.split()[0]) rootfs_size = self.get_rootfs_size(blocks) label_str = "-n boot" if self.label: label_str = "-n %s" % self.label dosfs_cmd = "mkdosfs %s -S 512 -C %s %d" % (label_str, rootfs, rootfs_size) exec_native_cmd(dosfs_cmd, native_sysroot) mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (rootfs, rootfs_dir) exec_native_cmd(mcopy_cmd, native_sysroot) chmod_cmd = "chmod 644 %s" % rootfs exec_cmd(chmod_cmd)
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 logger.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_cmd(cmd)
def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir, bootimg_dir, kernel_dir): """ 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. """ iso_img = "%s.p1" % disk.path full_path = creator._full_path(workdir, disk_name, "direct") full_path_iso = creator._full_path(workdir, disk_name, "iso") isohybrid_cmd = "isohybrid -u %s" % iso_img logger.debug("running command: %s", isohybrid_cmd) exec_cmd(isohybrid_cmd) # 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. logger.debug("Replaceing the image created by direct plugin\n") os.remove(disk.path) shutil.copy2(iso_img, full_path_iso) shutil.copy2(full_path_iso, full_path)
def prepare_rootfs_btrfs(self, rootfs, oe_builddir, rootfs_dir, native_sysroot, pseudo): """ Prepare content for a btrfs rootfs partition. Currently handles ext2/3/4 and btrfs. """ du_cmd = "du -ks %s" % rootfs_dir out = exec_cmd(du_cmd) actual_rootfs_size = int(out.split()[0]) rootfs_size = self.get_rootfs_size(actual_rootfs_size) with open(rootfs, 'w') as sparse: os.ftruncate(sparse.fileno(), rootfs_size * 1024) label_str = "" if self.label: label_str = "-L %s" % self.label mkfs_cmd = "mkfs.%s -b %d -r %s %s %s" % \ (self.fstype, rootfs_size * 1024, rootfs_dir, label_str, rootfs) exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
def prepare_rootfs_ext(self, rootfs, oe_builddir, rootfs_dir, native_sysroot, pseudo): """ Prepare content for an ext2/3/4 rootfs partition. """ du_cmd = "du -ks %s" % rootfs_dir out = exec_cmd(du_cmd) actual_rootfs_size = int(out.split()[0]) rootfs_size = self.get_rootfs_size(actual_rootfs_size) with open(rootfs, 'w') as sparse: os.ftruncate(sparse.fileno(), rootfs_size * 1024) extra_imagecmd = "-i 8192" label_str = "" if self.label: label_str = "-L %s" % self.label mkfs_cmd = "mkfs.%s -F %s %s %s -d %s" % \ (self.fstype, extra_imagecmd, rootfs, label_str, rootfs_dir) exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
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 = int(out.split()[0]) if filesize > part.size: part.size = filesize part.source_file = dst
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. """ syslinux_dir = cls._get_syslinux_dir(bootimg_dir) staging_kernel_dir = kernel_dir kernel = get_bitbake_var("KERNEL_IMAGE") initrd = get_bitbake_var("INITRD_IMAGE") hdddir = "%s/hdd/boot" % cr_workdir cmds = ("install -m 0644 %s/%s %s/%s" % (staging_kernel_dir, kernel, hdddir, kernel), "install -m 0644 %s/%s %s/%s" % (staging_kernel_dir, initrd, hdddir, initrd), "install -m 444 %s/modules/bios/ldlinux.c32 %s/ldlinux.c32" % (syslinux_dir, hdddir), "install -m 0644 %s/modules/bios/vesamenu.c32 %s/vesamenu.c32" % (syslinux_dir, hdddir), "install -m 444 %s/modules/bios/libcom32.c32 %s/libcom32.c32" % (syslinux_dir, hdddir), "install -m 444 %s/modules/bios/libutil.c32 %s/libutil.c32" % (syslinux_dir, hdddir)) for install_cmd in cmds: 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 logger.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_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(bootimg_size) part.source_file = bootimg
def do_prepare_partition(cls, part, source_params, cr, cr_workdir, oe_builddir, bootimg_dir, kernel_dir, krootfs_dir, native_sysroot): # Prologue from RootfsPlugin.do_prepare_partition, retrieves the # rootfs directory if part.rootfs_dir is None: if not 'ROOTFS_DIR' in krootfs_dir: raise WicError("Couldn't find --rootfs-dir, exiting") rootfs_dir = krootfs_dir['ROOTFS_DIR'] else: if part.rootfs_dir in krootfs_dir: rootfs_dir = krootfs_dir[part.rootfs_dir] elif part.rootfs_dir: rootfs_dir = part.rootfs_dir else: raise WicError("Couldn't find --rootfs-dir=%s connection or " "it is not a valid path, exiting" % part.rootfs_dir) if os.path.isdir(rootfs_dir): real_rootfs_dir = rootfs_dir else: image_rootfs_dir = get_bitbake_var("IMAGE_ROOTFS", rootfs_dir) if not os.path.isdir(image_rootfs_dir): raise WicError("No valid artifact IMAGE_ROOTFS from image " "named %s has been found at %s, exiting." % (rootfs_dir, image_rootfs_dir)) real_rootfs_dir = image_rootfs_dir u_boot_script = os.path.join(real_rootfs_dir, "etc/default/u-boot-script") if not os.path.exists(u_boot_script): raise WicError("u-boot-scripts package not installed") # Write new /etc/default/u-boot-script with open(u_boot_script, 'w') as cfg: cfg.write('# Generated by wic, rootfs-u-boot plugin\n') cfg.write('ROOT_PARTITION="%d"\n' % part.realnum) cfg.write('KERNEL_ARGS="root=%s %s"\n' % \ (cr.rootdev, cr.ks.bootloader.append or "")) no_initrd = source_params.get('no_initrd') or '' cfg.write('NO_INITRD="%s"\n' % no_initrd) overlays = source_params.get('overlays') or '' cfg.write('OVERLAYS="%s"\n' % overlays) script_prepend = source_params.get('script_prepend') or '' cfg.write('SCRIPT_PREPEND="%s"\n' % script_prepend) # Run update-u-boot-script in the target rootfs results = glob.glob(os.path.join("/usr/bin/qemu-*-static")) qemu_static = results[0] if len(results) > 0 else None if qemu_static: cp_cmd = "cp -L %s %s/usr/bin" % (qemu_static, real_rootfs_dir) exec_cmd(cp_cmd) update_cmd = "chroot %s sh -c update-u-boot-script" % real_rootfs_dir exec_cmd(update_cmd) if qemu_static: rm_cmd = "rm -f %s/usr/bin/%s" % (real_rootfs_dir, qemu_static) exec_cmd(rm_cmd) RootfsPlugin.do_prepare_partition(part, source_params, cr, cr_workdir, oe_builddir, bootimg_dir, kernel_dir, krootfs_dir, native_sysroot)
def do_prepare_partition(cls, part, source_params, creator, cr_workdir, oe_builddir, bootimg_dir, kernel_dir, rootfs_dir): """ 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: raise WicError("Couldn't find --rootfs-dir, exiting.") 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: raise WicError("Couldn't find --rootfs-dir=%s connection " "or it is not a valid path, exiting." % part.rootfs_dir) if not os.path.isdir(rootfs_dir): rootfs_dir = get_bitbake_var("IMAGE_ROOTFS") if not os.path.isdir(rootfs_dir): raise WicError("Couldn't find IMAGE_ROOTFS, exiting.") part.rootfs_dir = rootfs_dir # Prepare rootfs.img deploy_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") img_iso_dir = get_bitbake_var("ISODIR") 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) 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) # Support using a different initrd other than default if source_params.get('initrd'): initrd = source_params['initrd'] if not deploy_dir: raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") cp_cmd = "cp %s/%s %s" % (deploy_dir, initrd, cr_workdir) exec_cmd(cp_cmd) else: # Prepare initial ramdisk initrd = "%s/initrd" % deploy_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: raise WicError("Coludn't find target architecture") 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: raise WicError("grub-efi is incompatible with target %s" % target_arch) if not os.path.isfile("%s/EFI/BOOT/%s" \ % (bootimg_dir, grub_image)): grub_path = get_bitbake_var("STAGING_LIBDIR", "wic-tools") if not grub_path: raise WicError( "Couldn't find STAGING_LIBDIR, exiting.") grub_core = "%s/grub/%s" % (grub_path, grub_target) if not os.path.exists(grub_core): raise WicError("Please build grub-efi first") 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_cmd(grub_cmd) else: raise WicError("unrecognized bootimg-efi loader: %s" % source_params['loader']) except KeyError: raise WicError("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 logger.debug( "Added 100 extra blocks to %s to get to %d " "total blocks", part.mountpoint, blocks) # dosfs image for EFI boot bootimg = "%s/efi.img" % isodir dosfs_cmd = 'mkfs.vfat -n "EFIimg" -S 512 -C %s %d' \ % (bootimg, blocks) exec_cmd(dosfs_cmd) mmd_cmd = "mmd -i %s ::/EFI" % bootimg exec_cmd(mmd_cmd) mcopy_cmd = "env MTOOLS_SKIP_CHECK=1 " \ "mcopy -i %s -s %s/EFI/* ::/EFI/" \ % (bootimg, isodir) exec_cmd(mcopy_cmd, True) chmod_cmd = "chmod 644 %s" % bootimg exec_cmd(chmod_cmd) # Prepare files for legacy boot syslinux_dir = get_bitbake_var("STAGING_DATADIR", "wic-tools") if not syslinux_dir: raise WicError("Couldn't find STAGING_DATADIR, exiting.") 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 logger.debug("running command: %s", mkisofs_cmd) exec_cmd(mkisofs_cmd) 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 dir(self, pnum, path): return exec_cmd("{} -i {} ::{}".format(self.mdir, self._get_part_image(pnum), path))
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. """ bootimg_dir = cls._get_bootimg_dir(bootimg_dir, 'syslinux') staging_kernel_dir = kernel_dir hdddir = "%s/hdd/boot" % cr_workdir cmds = ("install -m 0644 %s/bzImage %s/vmlinuz" % (staging_kernel_dir, hdddir), "install -m 444 %s/syslinux/ldlinux.sys %s/ldlinux.sys" % (bootimg_dir, hdddir), "install -m 0644 %s/syslinux/vesamenu.c32 %s/vesamenu.c32" % (bootimg_dir, hdddir), "install -m 444 %s/syslinux/libcom32.c32 %s/libcom32.c32" % (bootimg_dir, hdddir), "install -m 444 %s/syslinux/libutil.c32 %s/libutil.c32" % (bootimg_dir, hdddir)) for install_cmd in cmds: 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 logger.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(bootimg_size) part.source_file = bootimg
def create(self): logger.debug("Creating sparse file %s", self.path) with open(self.path, 'w') as sparse: os.ftruncate(sparse.fileno(), self.min_size) logger.debug("Initializing partition table for %s", self.path) exec_cmd("parted -s %s mklabel %s" % (self.path, self.ptable_format)) logger.debug("Set disk identifier %x", self.identifier) with open(self.path, 'r+b') as img: img.seek(0x1B8) img.write(self.identifier.to_bytes(4, 'little')) logger.debug("Creating partitions") for part in self.partitions: if part.num == 0: continue if self.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(self.path, "extended", None, part.start - 1, self.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" if not part.system_id: part.system_id = '0x6' # 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 part.mountpoint == "/boot" and part.fstype in ["vfat", "msdos"] \ and part.size_sec % 2: logger.debug( "Subtracting one sector from '%s' partition to " "get even number of sectors for the partition", part.mountpoint) part.size_sec -= 1 self._create_partition(self.path, part.type, parted_fs_type, part.start, part.size_sec) if part.part_type: logger.debug("partition %d: set type UID to %s", part.num, part.part_type) exec_cmd("sgdisk --typecode=%d:%s %s" % \ (part.num, part.part_type, self.path)) if part.uuid and self.ptable_format == "gpt": logger.debug("partition %d: set UUID to %s", part.num, part.uuid) exec_cmd("sgdisk --partition-guid=%d:%s %s" % \ (part.num, part.uuid, self.path)) if part.label and self.ptable_format == "gpt": logger.debug("partition %d: set name to %s", part.num, part.label) exec_cmd("parted -s %s name %d %s" % \ (self.path, part.num, part.label)) if part.active: flag_name = "legacy_boot" if self.ptable_format == 'gpt' else "boot" logger.debug("Set '%s' flag for partition '%s' on disk '%s'", flag_name, part.num, self.path) exec_cmd("parted -s %s set %d %s on" % \ (self.path, part.num, flag_name)) if part.system_id: exec_cmd("sfdisk --part-type %s %s %s" % \ (self.path, part.num, part.system_id))
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 kernel_dir: kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") if not kernel_dir: raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") staging_kernel_dir = kernel_dir hdddir = "%s/hdd/boot" % cr_workdir try: if source_params['loader'] == 'grub-efi': shutil.copyfile("%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir, "%s/grub.cfg" % cr_workdir) shutil.copyfile( "%s/hdd/boot/EFI/BOOT/grub-mkimage.cfg" % cr_workdir, "%s/grub-mkimage.cfg" % cr_workdir) for mod in [ x for x in os.listdir(kernel_dir) if x.startswith("grub-efi-") ]: cp_cmd = "cp %s/%s %s/EFI/BOOT/%s" % (kernel_dir, mod, hdddir, mod[9:]) exec_cmd(cp_cmd, True) shutil.move("%s/grub.cfg" % cr_workdir, "%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir) distro_arch = get_bitbake_var("DISTRO_ARCH") if not distro_arch: raise WicError("Couldn't find target architecture") if distro_arch == "amd64": grub_target = 'x86_64-efi' grub_image = "bootx64.efi" elif distro_arch == "i386": grub_target = 'i386-efi' grub_image = "bootia32.efi" else: raise WicError("grub-efi is incompatible with target %s" % distro_arch) bootimg_dir = "%s/hdd/boot" % cr_workdir if not os.path.isfile("%s/EFI/BOOT/%s" \ % (bootimg_dir, grub_image)): # TODO: check that grub-mkimage is available grub_cmd = "grub-mkimage -p /EFI/BOOT " grub_cmd += "-c %s/grub-mkimage.cfg " % cr_workdir 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_cmd(grub_cmd) elif source_params['loader'] == 'systemd-boot': for mod in [ x for x in os.listdir(kernel_dir) if x.startswith("systemd-") ]: cp_cmd = "cp %s/%s %s/EFI/BOOT/%s" % (kernel_dir, mod, hdddir, mod[8:]) exec_cmd(cp_cmd, True) else: raise WicError("unrecognized bootimg-efi-isar loader: %s" % source_params['loader']) except KeyError: raise WicError( "bootimg-efi-isar requires a loader, none specified") startup = os.path.join(kernel_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 logger.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_cmd(dosfs_cmd) mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (bootimg, hdddir) exec_cmd(mcopy_cmd, True) 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 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: raise WicError("Couldn't find --rootfs-dir, exiting.") 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: raise WicError("Couldn't find --rootfs-dir=%s connection " "or it is not a valid path, exiting." % part.rootfs_dir) if not os.path.isdir(rootfs_dir): rootfs_dir = get_bitbake_var("IMAGE_ROOTFS") if not os.path.isdir(rootfs_dir): raise WicError("Couldn't find IMAGE_ROOTFS, exiting.") part.rootfs_dir = rootfs_dir # Prepare rootfs.img deploy_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") img_iso_dir = get_bitbake_var("ISODIR") 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) # Support using a different initrd other than default if source_params.get('initrd'): initrd = source_params['initrd'] if not deploy_dir: raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") cp_cmd = "cp %s/%s %s" % (deploy_dir, initrd, cr_workdir) exec_cmd(cp_cmd) else: # Prepare initial ramdisk initrd = "%s/initrd" % deploy_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: raise WicError("Coludn't find target architecture") 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: raise WicError("grub-efi is incompatible with target %s" % target_arch) if not os.path.isfile("%s/EFI/BOOT/%s" \ % (bootimg_dir, grub_image)): grub_path = get_bitbake_var("STAGING_LIBDIR", "wic-tools") if not grub_path: raise WicError("Couldn't find STAGING_LIBDIR, exiting.") grub_core = "%s/grub/%s" % (grub_path, grub_target) if not os.path.exists(grub_core): raise WicError("Please build grub-efi first") 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: raise WicError("unrecognized bootimg-efi loader: %s" % source_params['loader']) except KeyError: raise WicError("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 logger.debug("Added 100 extra blocks to %s to get to %d " "total blocks", part.mountpoint, blocks) # 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", "wic-tools") if not syslinux_dir: raise WicError("Couldn't find STAGING_DATADIR, exiting.") 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 logger.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: raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") cp_cmd = "cp %s/%s %s" % (bootimg_dir, initrd, hdddir) exec_cmd(cp_cmd, True) else: logger.debug("Ignoring missing initrd") logger.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 logger.debug("Using custom configuration file " "%s for systemd-boots's boot.conf", configfile) else: raise WicError("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 = "/vmlinuz" 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 logger.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, 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 kernel_dir: kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") if not kernel_dir: raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") logger.debug('Kernel dir: %s', bootimg_dir) boot_files = get_bitbake_var("IMAGE_BOOT_FILES") if not boot_files: raise WicError('No boot files defined, IMAGE_BOOT_FILES unset') logger.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]: raise WicError('Malformed boot file entry: %s' % src_entry) else: dst_entry = (src_entry, src_entry) logger.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(kernel_dir, src)) logger.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(kernel_dir, src), os.path.join(hdddir, dst))] for task in install_task: src_path, dst_path = task logger.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) logger.debug('Prepare boot partition using rootfs in %s', hdddir) part.prepare_rootfs(cr_workdir, oe_builddir, hdddir, native_sysroot)
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 kernel_dir: kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") if not kernel_dir: raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") staging_kernel_dir = kernel_dir hdddir = "%s/hdd/boot" % cr_workdir try: if source_params['loader'] == 'grub-efi': shutil.copyfile("%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir, "%s/grub.cfg" % cr_workdir) shutil.copyfile("%s/hdd/boot/EFI/BOOT/grub-mkimage.cfg" % cr_workdir, "%s/grub-mkimage.cfg" % cr_workdir) for mod in [x for x in os.listdir(kernel_dir) if x.startswith("grub-efi-")]: cp_cmd = "cp %s/%s %s/EFI/BOOT/%s" % (kernel_dir, mod, hdddir, mod[9:]) exec_cmd(cp_cmd, True) shutil.move("%s/grub.cfg" % cr_workdir, "%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir) distro_arch = get_bitbake_var("DISTRO_ARCH") if not distro_arch: raise WicError("Couldn't find target architecture") if distro_arch == "amd64": grub_target = 'x86_64-efi' grub_image = "bootx64.efi" grub_modules = "multiboot efi_uga iorw ata " elif distro_arch == "i386": grub_target = 'i386-efi' grub_image = "bootia32.efi" grub_modules = "multiboot efi_uga iorw ata " elif distro_arch == "arm64": grub_target = 'arm64-efi' grub_image = "bootaa64.efi" grub_modules = "" else: raise WicError("grub-efi is incompatible with target %s" % distro_arch) bootimg_dir = "%s/hdd/boot" % cr_workdir if not os.path.isfile("%s/EFI/BOOT/%s" \ % (bootimg_dir, grub_image)): # TODO: check that grub-mkimage is available grub_cmd = "grub-mkimage -p /EFI/BOOT " grub_cmd += "-c %s/grub-mkimage.cfg " % cr_workdir 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 " grub_cmd += "search efi_gop font gfxterm gfxmenu " grub_cmd += "terminal minicmd test 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 " + grub_modules exec_cmd(grub_cmd) elif source_params['loader'] == 'systemd-boot': for mod in [x for x in os.listdir(kernel_dir) if x.startswith("systemd-")]: cp_cmd = "cp %s/%s %s/EFI/BOOT/%s" % (kernel_dir, mod, hdddir, mod[8:]) exec_cmd(cp_cmd, True) else: raise WicError("unrecognized bootimg-efi-isar loader: %s" % source_params['loader']) except KeyError: raise WicError("bootimg-efi-isar requires a loader, none specified") startup = os.path.join(kernel_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 logger.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_cmd(dosfs_cmd) mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (bootimg, hdddir) exec_cmd(mcopy_cmd, True) 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 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", "wic-tools") if not bootimg_dir: raise WicError("Couldn't find STAGING_DATADIR, exiting") if not _has_syslinux(bootimg_dir): raise WicError("Please build syslinux first") # just so the result notes display it creator.bootimg_dir = bootimg_dir staging_kernel_dir = kernel_dir hdddir = "%s/hdd/boot" % cr_workdir cmds = ("install -m 0644 %s/bzImage %s/vmlinuz" % (staging_kernel_dir, hdddir), "install -m 444 %s/syslinux/ldlinux.sys %s/ldlinux.sys" % (bootimg_dir, hdddir), "install -m 0644 %s/syslinux/vesamenu.c32 %s/vesamenu.c32" % (bootimg_dir, hdddir), "install -m 444 %s/syslinux/libcom32.c32 %s/libcom32.c32" % (bootimg_dir, hdddir), "install -m 444 %s/syslinux/libutil.c32 %s/libutil.c32" % (bootimg_dir, hdddir)) for install_cmd in cmds: 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 logger.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(bootimg_size) part.source_file = bootimg
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 kernel_dir: kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") if not kernel_dir: raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") logger.debug('Kernel dir: %s', bootimg_dir) boot_files = get_bitbake_var("IMAGE_BOOT_FILES") if not boot_files: raise WicError('No boot files defined, IMAGE_BOOT_FILES unset') logger.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]: raise WicError('Malformed boot file entry: %s' % src_entry) else: dst_entry = (src_entry, src_entry) logger.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(kernel_dir, src)) logger.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(kernel_dir, src), os.path.join(hdddir, dst))] for task in install_task: src_path, dst_path = task logger.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) logger.debug('Prepare boot partition using rootfs in %s', hdddir) part.prepare_rootfs(cr_workdir, oe_builddir, hdddir, native_sysroot)
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("DEPLOY_DIR_IMAGE") if not bootimg_dir: msger.error("Couldn't find DEPLOY_DIR_IMAGE, exiting\n") # just so the result notes display it creator.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) for mod in [x for x in os.listdir(bootimg_dir) if x.startswith("grub-efi-")]: cp_cmd = "cp %s/%s %s/EFI/BOOT/%s" % (bootimg_dir, mod, hdddir, mod[9:]) 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'] == 'systemd-boot': for mod in [x for x in os.listdir(bootimg_dir) if x.startswith("systemd-")]: cp_cmd = "cp %s/%s %s/EFI/BOOT/%s" % (bootimg_dir, mod, hdddir, mod[8:]) 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