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 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: name = "do_install_disk" methods = pluginmgr.get_source_plugin_methods( source_plugin, {name: None}) methods["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: msger.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: msger.debug("Compressing disk %s with %s" % (disk_name, self.compressor)) exec_cmd("%s %s" % (self.compressor, 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.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 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 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_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_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_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 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 prepare_rootfs_squashfs(self, rootfs, oe_builddir, rootfs_dir, native_sysroot, pseudo): """ Prepare content for a squashfs rootfs partition. """ squashfs_cmd = "mksquashfs %s %s -noappend" % \ (rootfs_dir, rootfs) exec_native_cmd(squashfs_cmd, native_sysroot, pseudo=pseudo)
def prepare_rootfs_squashfs(self, rootfs, oe_builddir, rootfs_dir, native_sysroot, pseudo): """ Prepare content for a squashfs rootfs partition. """ squashfs_cmd = "mksquashfs %s %s -noappend" % \ (rootfs_dir, rootfs) exec_native_cmd(squashfs_cmd, native_sysroot, pseudo=pseudo)
def prepare_swap_partition(self, cr_workdir, oe_builddir, native_sysroot): """ Prepare a swap partition. """ path = "%s/fs.%s" % (cr_workdir, self.fstype) with open(path, 'w') as sparse: os.ftruncate(sparse.fileno(), self.size * 1024) import uuid label_str = "" if self.label: label_str = "-L %s" % self.label mkswap_cmd = "mkswap %s -U %s %s" % (label_str, str(uuid.uuid1()), path) exec_native_cmd(mkswap_cmd, native_sysroot)
def prepare_swap_partition(self, cr_workdir, oe_builddir, native_sysroot): """ Prepare a swap partition. """ path = "%s/fs.%s" % (cr_workdir, self.fstype) with open(path, 'w') as sparse: os.ftruncate(sparse.fileno(), self.size * 1024) import uuid label_str = "" if self.label: label_str = "-L %s" % self.label mkswap_cmd = "mkswap %s -U %s %s" % (label_str, str(uuid.uuid1()), path) exec_native_cmd(mkswap_cmd, native_sysroot)
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_btrfs(self, rootfs, oe_builddir, native_sysroot): """ Prepare an empty btrfs partition. """ size = self.disk_size with open(rootfs, 'w') as sparse: os.ftruncate(sparse.fileno(), size * 1024) label_str = "" if self.label: label_str = "-L %s" % self.label mkfs_cmd = "mkfs.%s -b %d %s %s" % \ (self.fstype, self.size * 1024, label_str, rootfs) exec_native_cmd(mkfs_cmd, native_sysroot)
def prepare_empty_partition_btrfs(self, rootfs, oe_builddir, native_sysroot): """ Prepare an empty btrfs partition. """ size = self.disk_size with open(rootfs, 'w') as sparse: os.ftruncate(sparse.fileno(), size * 1024) label_str = "" if self.label: label_str = "-L %s" % self.label mkfs_cmd = "mkfs.%s -b %d %s %s" % \ (self.fstype, self.size * 1024, label_str, rootfs) exec_native_cmd(mkfs_cmd, native_sysroot)
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 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, native_sysroot): """ 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 = "mkfs.%s -F %s %s %s" % \ (self.fstype, extra_imagecmd, label_str, rootfs) exec_native_cmd(mkfs_cmd, native_sysroot)
def prepare_empty_partition_ext(self, rootfs, oe_builddir, native_sysroot): """ 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 = "mkfs.%s -F %s %s %s" % \ (self.fstype, extra_imagecmd, label_str, rootfs) exec_native_cmd(mkfs_cmd, native_sysroot)
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 _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_native_cmd(cmd, self.native_sysroot)
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_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 _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 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. """ 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_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. 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 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, 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, locate the temporary root partition and hash it. """ # We rely on the --label parameter and the naming convention # in partition.py prepare_rootfs() here to find the already # prepared rootfs partition image. pattern = '%s/rootfs_%s.*' % (cr_workdir, part.label) rootfs = glob.glob(pattern) if len(rootfs) != 1: raise WicError( "%s shell pattern does not match exactly one rootfs image (missing --label parameter?): %s" % (pattern, rootfs)) else: rootfs = rootfs[0] logger.debug("Calculating dm-verity hash for rootfs %s (native %s)." % (rootfs, native_sysroot)) hashimg = '%s/dm-verity_%s.img' % (cr_workdir, part.label) # Reserve some fixed amount of space at the start of the hash image # for our own data (in particular, the signed root hash). # The content of that part is: # roothash=<....> # <potentially some more assignments in the future> # signature=<single line of base64 encoded OpenSSL sha256 digest> header_size = 4096 ret, out = exec_native_cmd( "veritysetup format '%s' '%s' --hash-offset=%d" % (rootfs, hashimg, header_size), native_sysroot) m = re.search(r'^Root hash:\s*(\S+)$', out, re.MULTILINE) if ret or not m: raise WicError('veritysetup failed: %s' % out) else: root_hash = m.group(1) privkey = get_bitbake_var('REFKIT_DMVERITY_PRIVATE_KEY') password = get_bitbake_var('REFKIT_DMVERITY_PASSWORD') tmp = tempfile.mkdtemp(prefix='dm-verity-') try: data_filename = os.path.join(tmp, 'data') header = ('roothash=%s\nheadersize=%d\n' % (root_hash, header_size)).encode('ascii') with open(data_filename, 'wb') as data: data.write(header) # Must use a temporary file, exec_native_cmd() only supports UTF-8 output. signature = os.path.join(tmp, 'sig') ret, out = exec_native_cmd( "openssl dgst -sha256 -passin '%s' -sign '%s' -out '%s' '%s'" % (password, privkey, signature, data_filename), native_sysroot) if ret: raise WicError('openssl signing failed') with open(signature, 'rb') as f: header += b'signature=' + base64.standard_b64encode( f.read()) + b'\n' if len(header) + 1 >= header_size: raise WicError( 'reserved space for dm-verity header too small') with open(hashimg, 'rb+') as hash: hash.write(header) finally: shutil.rmtree(tmp) data_bytes = os.stat(rootfs).st_size hash_bytes = os.stat(hashimg).st_size logger.debug( "dm-verity data partition %d bytes, hash partition %d bytes, ratio %f." % (data_bytes, hash_bytes, data_bytes / hash_bytes)) part.size = data_bytes // 1024 part.source_file = hashimg
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
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 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_native_cmd("parted -s %s mklabel %s" % (self.path, self.ptable_format), self.native_sysroot) 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_native_cmd("sgdisk --typecode=%d:%s %s" % \ (part.num, part.part_type, self.path), self.native_sysroot) if part.uuid and self.ptable_format == "gpt": logger.debug("partition %d: set UUID to %s", part.num, part.uuid) exec_native_cmd("sgdisk --partition-guid=%d:%s %s" % \ (part.num, part.uuid, self.path), self.native_sysroot) if part.label and self.ptable_format == "gpt": logger.debug("partition %d: set name to %s", part.num, part.label) exec_native_cmd("parted -s %s name %d %s" % \ (self.path, part.num, part.label), self.native_sysroot) 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_native_cmd("parted -s %s set %d %s on" % \ (self.path, part.num, flag_name), self.native_sysroot) if part.system_id: exec_native_cmd("sfdisk --part-type %s %s %s" % \ (self.path, part.num, part.system_id), self.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 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 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 __format_disks(self): self.layout_partitions() for dev in self.disks: 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) if disk['identifier']: msger.debug("Set disk identifier %x" % disk['identifier']) with open(disk['disk'].device, 'r+b') as img: img.seek(0x1B8) img.write(disk['identifier'].to_bytes(4, 'little')) 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("Subtracting 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'] and disk['ptable_format'] == "gpt": 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['label'] and disk['ptable_format'] == "gpt": msger.debug("partition %d: set name to %s" % \ (part['num'], part['label'])) exec_native_cmd("parted -s %s name %d %s" % \ (disk['disk'].device, part['num'], part['label']), 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) if part['system_id']: exec_native_cmd("sfdisk --part-type %s %s %s" % \ (disk['disk'].device, part['num'], part['system_id']), 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 create(self): msger.debug("Creating sparse file %s" % self.path) with open(self.path, 'w') as sparse: os.ftruncate(sparse.fileno(), self.min_size) msger.debug("Initializing partition table for %s" % self.path) exec_native_cmd( "parted -s %s mklabel %s" % (self.path, self.ptable_format), self.native_sysroot) msger.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')) msger.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" 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_sec % 2: msger.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: 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, self.path), self.native_sysroot) if part.uuid and self.ptable_format == "gpt": 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, self.path), self.native_sysroot) if part.label and self.ptable_format == "gpt": msger.debug("partition %d: set name to %s" % \ (part.num, part.label)) exec_native_cmd("parted -s %s name %d %s" % \ (self.path, part.num, part.label), self.native_sysroot) if part.active: flag_name = "legacy_boot" if self.ptable_format == 'gpt' else "boot" msger.debug("Set '%s' flag for partition '%s' on disk '%s'" % \ (flag_name, part.num, self.path)) exec_native_cmd("parted -s %s set %d %s on" % \ (self.path, part.num, flag_name), self.native_sysroot) if part.system_id: exec_native_cmd("sfdisk --part-type %s %s %s" % \ (self.path, part.num, part.system_id), 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 self.ptable_format == 'msdos': msger.debug("Disable 'lba' flag for partition '%s' on disk '%s'" % \ (part.num, self.path)) exec_native_cmd("parted -s %s set %d lba off" % \ (self.path, part.num), self.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 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