Example #1
0
    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)

        extraopts = self.mkfs_extraopts or "-F -i 8192"

        label_str = ""
        if self.label:
            label_str = "-L %s" % self.label

        mkfs_cmd = "mkfs.%s %s %s %s -d %s" % \
            (self.fstype, extraopts, rootfs, label_str, rootfs_dir)
        exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)

        mkfs_cmd = "fsck.%s -pvfD %s" % (self.fstype, rootfs)
        exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
Example #2
0
    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

        extraopts = self.mkfs_extraopts or '-S 512'

        dosfs_cmd = "mkdosfs %s -i %s %s %s -C %s %d" % \
                    (label_str, self.fsuuid, extraopts, size_str, rootfs,
                     blocks)

        exec_native_cmd(dosfs_cmd, native_sysroot)

        chmod_cmd = "chmod 644 %s" % rootfs
        exec_cmd(chmod_cmd)
Example #3
0
    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)

        extraopts = self.mkfs_extraopts or "-F -i 8192"

        label_str = ""
        if self.label:
            label_str = "-L %s" % self.label

        mkfs_cmd = "mkfs.%s %s %s %s -d %s" % \
            (self.fstype, extraopts, rootfs, label_str, rootfs_dir)
        exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)

        mkfs_cmd = "fsck.%s -pvfD %s" % (self.fstype, rootfs)
        exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
Example #4
0
    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

        extraopts = self.mkfs_extraopts or '-S 512'

        dosfs_cmd = "mkdosfs %s %s %s -C %s %d" % \
                    (label_str, size_str, extraopts, 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)
Example #5
0
    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

        extraopts = self.mkfs_extraopts or '-S 512'

        dosfs_cmd = "mkdosfs %s -i %s %s %s -C %s %d" % \
                    (label_str, self.fsuuid, extraopts, size_str, rootfs,
                     blocks)

        exec_native_cmd(dosfs_cmd, native_sysroot)

        chmod_cmd = "chmod 644 %s" % rootfs
        exec_cmd(chmod_cmd)
Example #6
0
    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)
            python = os.path.join(self.native_sysroot, 'usr/bin/python3-native/python3')
            bmaptool = os.path.join(self.native_sysroot, 'usr/bin/bmaptool')
            exec_native_cmd("%s %s create %s -o %s.bmap" % \
                            (python, bmaptool, 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 loader-specific config
        """
        hdddir = "%s/hdd/%s.%s" % (cr_workdir, part.label, part.lineno)

        install_cmd = "install -d %s" % hdddir
        exec_cmd(install_cmd)

        bootloader = creator.ks.bootloader

        cmdline = "root=%s %s\n" % \
                   (creator.rootdev, bootloader.append)

        cwd = os.getcwd()
        os.chdir(hdddir)
        config_cmd = 'bg_setenv -f . -k "C:%s:bzImage" -a "%s" -r %s -w %s' % \
                      (part.label.upper(), \
                       cmdline.strip(), \
                       source_params.get("revision", 1), \
                       source_params.get("watchdog", 5))

        exec_native_cmd(config_cmd, native_sysroot)
        os.chdir(cwd)
Example #8
0
    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

        extraopts = self.mkfs_extraopts or '-S 512'

        dosfs_cmd = "mkdosfs %s %s %s -C %s %d" % \
                    (label_str, size_str, extraopts, 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)
Example #9
0
    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)
            python = os.path.join(self.native_sysroot,
                                  'usr/bin/python3-native/python3')
            bmaptool = os.path.join(self.native_sysroot, 'usr/bin/bmaptool')
            exec_native_cmd("%s %s create %s -o %s.bmap" % \
                            (python, bmaptool, 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_prepare_partition(cls, part, source_params, creator, cr_workdir,
                             oe_builddir, bootimg_dir, kernel_dir, rootfs_dir,
                             native_sysroot):
        """
        Called to do the actual content population for a partition i.e. it
        'prepares' the partition to be incorporated into the image.
        In this case, prepare content for an EFI (grub) boot partition.
        """
        if not bootimg_dir:
            bootimg_dir = get_bitbake_var("HDDDIR")
            if not bootimg_dir:
                msger.error("HDDDIR not set, exiting\n")

        staging_kernel_dir = kernel_dir

        hdddir = "%s/hdd/%s.%s" % (cr_workdir, part.label, part.lineno)

        install_cmd = "install -d %s/EFI/BOOT" % hdddir
        exec_cmd(install_cmd)

        cp_cmd = "cp %s/EFI/BOOT/* %s/EFI/BOOT" % (bootimg_dir, hdddir)
        exec_cmd(cp_cmd, True)

        # Calculate the number of extra blocks to be sure that the
        # resulting partition image is of the wanted size

        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
        efiimg = "%s/%s.%s.img" % (cr_workdir, part.label, part.lineno)

        dosfs_cmd = "mkdosfs -n %s -C %s %d" % (part.label.upper(), efiimg,
                                                blocks)
        exec_native_cmd(dosfs_cmd, native_sysroot)

        mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (efiimg, hdddir)
        exec_native_cmd(mcopy_cmd, native_sysroot)

        chmod_cmd = "chmod 644 %s" % efiimg
        exec_cmd(chmod_cmd)

        du_cmd = "du -Lbks %s" % efiimg
        out = exec_cmd(du_cmd)
        efiimg_size = out.split()[0]

        part.size = int(efiimg_size)
        part.source_file = efiimg
 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)
Example #12
0
 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)
Example #13
0
 def prepare_rootfs_erofs(self, rootfs, cr_workdir, oe_builddir, rootfs_dir,
                          native_sysroot, pseudo):
     """
     Prepare content for a erofs rootfs partition.
     """
     extraopts = self.mkfs_extraopts or ''
     erofs_cmd = "mkfs.erofs %s -U %s %s %s" % \
                    (extraopts, self.fsuuid, rootfs, rootfs_dir)
     exec_native_cmd(erofs_cmd, native_sysroot, pseudo=pseudo)
Example #14
0
 def prepare_rootfs_squashfs(self, rootfs, oe_builddir, rootfs_dir,
                             native_sysroot, pseudo):
     """
     Prepare content for a squashfs rootfs partition.
     """
     extraopts = self.mkfs_extraopts or '-noappend'
     squashfs_cmd = "mksquashfs %s %s %s" % \
                    (rootfs_dir, rootfs, extraopts)
     exec_native_cmd(squashfs_cmd, native_sysroot, pseudo=pseudo)
Example #15
0
 def prepare_rootfs_squashfs(self, rootfs, oe_builddir, rootfs_dir,
                             native_sysroot, pseudo):
     """
     Prepare content for a squashfs rootfs partition.
     """
     extraopts = self.mkfs_extraopts or '-noappend'
     squashfs_cmd = "mksquashfs %s %s %s" % \
                    (rootfs_dir, rootfs, extraopts)
     exec_native_cmd(squashfs_cmd, native_sysroot, pseudo=pseudo)
 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 set boot flag for GPT.
     """
     full_path = creator._full_path(workdir, disk_name, "direct")
     if creator.ptable_format == 'gpt':
         exec_native_cmd("parted -s %s set 1 boot on" % \
                         full_path, native_sysroot)
Example #17
0
    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)
Example #18
0
    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)

        label_str = ""
        if self.label:
            label_str = "-L %s" % self.label

        mkswap_cmd = "mkswap %s -U %s %s" % (label_str, self.fsuuid, path)
        exec_native_cmd(mkswap_cmd, native_sysroot)
Example #19
0
    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)
Example #21
0
    def check_for_Y2038_problem(self, rootfs, native_sysroot):
        """
        Check if the filesystem is affected by the Y2038 problem
        (Y2038 problem = 32 bit time_t overflow in January 2038)
        """
        def get_err_str(part):
            err = "The {} filesystem {} has no Y2038 support."
            if part.mountpoint:
                args = [part.fstype, "mounted at %s" % part.mountpoint]
            elif part.label:
                args = [part.fstype, "labeled '%s'" % part.label]
            elif part.part_name:
                args = [part.fstype, "in partition '%s'" % part.part_name]
            else:
                args = [part.fstype, "in partition %s" % part.num]
            return err.format(*args)

        # ext2 and ext3 are always affected by the Y2038 problem
        if self.fstype in ["ext2", "ext3"]:
            logger.warn(get_err_str(self))
            return

        ret, out = exec_native_cmd("dumpe2fs %s" % rootfs, native_sysroot)

        # if ext4 is affected by the Y2038 problem depends on the inode size
        for line in out.splitlines():
            if line.startswith("Inode size:"):
                size = int(line.split(":")[1].strip())
                if size < 256:
                    logger.warn("%s Inodes (of size %d) are too small." %
                                (get_err_str(self), size))
                break
Example #22
0
    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)

        extraopts = self.mkfs_extraopts or "-i 8192"

        label_str = ""
        if self.label:
            label_str = "-L %s" % self.label

        mkfs_cmd = "mkfs.%s -F %s %s %s" % \
            (self.fstype, extraopts, label_str, rootfs)
        exec_native_cmd(mkfs_cmd, native_sysroot)
Example #23
0
    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)

        extraopts = self.mkfs_extraopts or "-i 8192"

        label_str = ""
        if self.label:
            label_str = "-L %s" % self.label

        mkfs_cmd = "mkfs.%s -F %s %s %s" % \
            (self.fstype, extraopts, label_str, rootfs)
        exec_native_cmd(mkfs_cmd, native_sysroot)
Example #24
0
    def prepare_rootfs_msdos(self, rootfs, cr_workdir, 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 = ""

        extraopts = self.mkfs_extraopts or '-S 512'

        dosfs_cmd = "mkdosfs %s -i %s %s %s -C %s %d" % \
                    (label_str, self.fsuuid, size_str, extraopts, 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)

        if self.updated_fstab_path and self.has_fstab and not self.no_fstab_update:
            mcopy_cmd = "mcopy -i %s %s ::/etc/fstab" % (
                rootfs, self.updated_fstab_path)
            exec_native_cmd(mcopy_cmd, native_sysroot)

        chmod_cmd = "chmod 644 %s" % rootfs
        exec_cmd(chmod_cmd)
Example #25
0
    def prepare_rootfs_ext(self, rootfs, cr_workdir, 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)

        extraopts = self.mkfs_extraopts or "-F -i 8192"

        label_str = ""
        if self.label:
            label_str = "-L %s" % self.label

        mkfs_cmd = "mkfs.%s %s %s %s -U %s -d %s" % \
            (self.fstype, extraopts, rootfs, label_str, self.fsuuid, rootfs_dir)
        exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)

        if self.updated_fstab_path and self.has_fstab:
            debugfs_script_path = os.path.join(cr_workdir, "debugfs_script")
            with open(debugfs_script_path, "w") as f:
                f.write("cd etc\n")
                f.write("rm fstab\n")
                f.write("write %s fstab\n" % (self.updated_fstab_path))
            debugfs_cmd = "debugfs -w -f %s %s" % (debugfs_script_path, rootfs)
            exec_native_cmd(debugfs_cmd, native_sysroot)

        mkfs_cmd = "fsck.%s -pvfD %s" % (self.fstype, rootfs)
        exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
Example #26
0
    def prepare_rootfs_btrfs(self, rootfs, oe_builddir, rootfs_dir,
                             native_sysroot, pseudo):
        """
        Prepare content for a btrfs 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)

        label_str = ""
        if self.label:
            label_str = "-L %s" % self.label

        mkfs_cmd = "mkfs.%s -b %d -r %s %s %s -U %s %s" % \
            (self.fstype, rootfs_size * 1024, rootfs_dir, label_str,
             self.mkfs_extraopts, self.fsuuid, rootfs)
        exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
Example #27
0
    def prepare_rootfs_btrfs(self, rootfs, oe_builddir, rootfs_dir,
                             native_sysroot, pseudo):
        """
        Prepare content for a btrfs 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)

        label_str = ""
        if self.label:
            label_str = "-L %s" % self.label

        mkfs_cmd = "mkfs.%s -b %d -r %s %s %s -U %s %s" % \
            (self.fstype, rootfs_size * 1024, rootfs_dir, label_str,
             self.mkfs_extraopts, self.fsuuid, rootfs)
        exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
Example #28
0
    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)
Example #29
0
    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 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_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)
Example #32
0
    def do_prepare_partition(cls, part, source_params, creator, cr_workdir,
                             oe_builddir, bootimg_dir, kernel_dir,
                             rootfs_dir, native_sysroot):
        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")

        hdddir = "%s/hdd/trustme/" % cr_workdir

        workdir = get_bitbake_var("WORKDIR")
        if not workdir:
            raise WicError("Can not get WORKDIR directory - not in cooked mode?")

        deploy_dir_image = get_bitbake_var("DEPLOY_DIR_IMAGE")
        if not deploy_dir_image:
            raise WicError("Can not get DEPLOY_DIR_IMAGE directory - not in cooked mode?")


        topdir = get_bitbake_var("TOPDIR")
        if not topdir:
            raise WicError("Can not get TOPDIR directory - not in cooked mode?")

        tmpdir = TMPDIR = "%s/tmp_container" % topdir
        if not tmpdir:
            raise WicError("Can not get TMPDIR directory - not in cooked mode?")

        #machine = get_bitbake_var("MACHINE")
        #if not machine:
        #    raise WicError("Can not get MACHINE - not in cooked mode?")

        deploy_dir_image = "{0}/deploy/images/{1}".format(tmpdir, "qemux86-64")
        if not deploy_dir_image:
            raise WicError("Can not get DEPLOY_DIR_IMAGE directory - not in cooked mode?")

        D = get_bitbake_var("D")
        if not D:
            raise WicError("Can not get D directory - not in cooked mode?")

        trustme_hardware = get_bitbake_var("TRUSTME_HARDWARE")
        if not trustme_hardware:
            raise WicError("Can not get trustme_hardware directory - not in cooked mode?")

        src = "%s/../trustme/build/" % topdir
        config_creator_dir = "%s/config_creator" %src
        proto_file_dir = "%s/cml/daemon" % workdir
        provisioning_dir = "%s/device_provisioning" % src
        enrollment_dir = "%s/oss_enrollment" % provisioning_dir
        test_cert_dir = "%s/test_certificates" % topdir

        if not os.path.exists(test_cert_dir):
            raise WicError("Test PKI not generated at {0}\nIs trustx-cml-userdata built?".format(test_cert_dir))

        ## copying /lib/modules contents

        machine_translated = get_bitbake_var('MACHINE_ARCH')

        #underscores in MACHINE_ARCH are replaced by - in filenames
        machine_translated = machine_translated.replace("_","-")

        kernel_stagingdir = get_bitbake_var("STAGING_KERNEL_BUILDDIR")

        rootfs = get_bitbake_var("IMAGE_ROOTFS")

        versionfile = open(kernel_stagingdir + "/kernel-abiversion", "r")
        kernelversion = versionfile.read().rstrip()
        versionfile.close()
        
        modulesname = "{0}/modules-{1}.tgz".format(kernel_dir, machine_translated)
        modulesname = os.readlink(modulesname)

        install_cmd = "install -d {0}/tmp_modules".format(hdddir)
        exec_cmd(install_cmd)

        try:
            cp_cmd = "tar -xzf {0}/{1} --directory {2}/tmp_modules".format(kernel_dir, modulesname, hdddir)
            exec_cmd(cp_cmd, True)
        except KeyError:
            raise WicError("error while copying kernel modules")

        try:
            cp_cmd = "/sbin/depmod --basedir \"{1}/tmp_modules\" --config \"{0}/etc/depmod.d\" {2}".format(rootfs, hdddir, kernelversion)
            exec_cmd(cp_cmd, True)
        except KeyError:
            raise WicError("Failed to execute depmod on modules")

        try:
            cp_cmd = "mv {0}/tmp_modules/lib/modules {0}".format(hdddir)
            exec_cmd(cp_cmd, True)
        except KeyError:
            raise WicError("Failed to execute depmod on modules")

        try:
            cp_cmd = "rm -fr {0}/tmp_modules".format(hdddir)
            exec_cmd(cp_cmd, True)
        except KeyError:
            raise WicError("Failed to execute depmod on modules")
        
        ## coyping /data contents

        install_cmd = "install -d %s/userdata/cml/tokens" % hdddir 
        exec_cmd(install_cmd)

        install_cmd = "install -d %s/userdata/cml/containers_templates" % hdddir 
        exec_cmd(install_cmd)

        # copy device config
        cp_cmd = "cp {0}/trustx-configs/device.conf {1}/userdata/cml/".format(deploy_dir_image, hdddir)
        exec_cmd(cp_cmd)


        # copy container configs
        cp_cmd = "cp -ar {0}/trustx-configs/container/. {1}/userdata/cml/containers_templates/".format(deploy_dir_image, hdddir)
        exec_cmd(cp_cmd)


        cp_cmd = "cp {0}/ssig_rootca.cert {1}/userdata/cml/tokens/".format(test_cert_dir, hdddir)
        exec_cmd(cp_cmd)
        
        cp_cmd = "cp {0}/gen_rootca.cert {1}/userdata/cml/tokens/".format(test_cert_dir, hdddir)
        exec_cmd(cp_cmd)

        
        mkdir_cmd = "mkdir -p %s" % deploy_dir_image
        exec_cmd(mkdir_cmd)

        mkdir_cmd = "mkdir -p %s/userdata/cml/operatingsystems/" % hdddir
        exec_cmd(mkdir_cmd)

        mkdir_cmd = "mkdir -p %s/userdata/cml/containers/" % hdddir
        exec_cmd(mkdir_cmd)

        
        cp_cmd = "cp -ar {0}/trustx-guests/. {1}/userdata/cml/operatingsystems".format(deploy_dir_image, hdddir)
        exec_cmd(cp_cmd)

        du_cmd = "du --block-size=4096 -s %s" % hdddir
        out = exec_cmd(du_cmd)
        fs_4kblocks = int(out.split()[0])

        fs_4kblocks += 2**16

        logger.debug("out: %s, 1096 byte blocks: %d", out, fs_4kblocks)

        userdataimg = "%s/userdata.img" % cr_workdir

        mkfs_cmd = "dd if=/dev/zero of={0} bs={1} count={2}".format(userdataimg, "4096", fs_4kblocks)
        exec_cmd(mkfs_cmd, native_sysroot)

        #mkfs_cmd = "mkfs.btrfs --label data --byte-count {0} --rootdir {1} {2}".format(fs_bytes, hdddir, userdataimg)
        mkfs_cmd = "mkfs.ext4 -b 4096 -d {0} -L trustme {1} {2}".format(hdddir, userdataimg, fs_4kblocks)
        logger.debug("Executing: %s" % mkfs_cmd)
        exec_native_cmd(mkfs_cmd, native_sysroot)


        chmod_cmd = "chmod 644 %s" % userdataimg
        exec_cmd(chmod_cmd)

        du_cmd = "du -Lbks %s" % userdataimg
        out = exec_cmd(du_cmd)
        userdataimg_size = out.split()[0]

        part.size = int(userdataimg_size)
        part.source_file = userdataimg
Example #33
0
    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_name:
                logger.debug("partition %d: set name to %s",
                             part.num, part.part_name)
                exec_native_cmd("sgdisk --change-name=%d:%s %s" % \
                                         (part.num, part.part_name,
                                          self.path), self.native_sysroot)

            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 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
        deploy_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
        img_iso_dir = get_bitbake_var("ISODIR")

        # 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:
            target_dir = "%s/EFI/BOOT" % isodir
            if os.path.exists(target_dir):
                shutil.rmtree(target_dir)

            os.makedirs(target_dir)

            if source_params['loader'] == 'grub-efi':
                # 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_src_image = "grub-efi-bootx64.efi"
                    grub_dest_image = "bootx64.efi"
                elif re.match('i.86', target_arch):
                    grub_src_image = "grub-efi-bootia32.efi"
                    grub_dest_image = "bootia32.efi"
                else:
                    raise WicError("grub-efi is incompatible with target %s" %
                                   target_arch)

                grub_target = os.path.join(target_dir, grub_dest_image)
                if not os.path.isfile(grub_target):
                    grub_src = os.path.join(deploy_dir, grub_src_image)
                    if not os.path.exists(grub_src):
                        raise WicError("Grub loader %s is not found in %s. "
                                       "Please build grub-efi first" %
                                       (grub_src_image, deploy_dir))
                    shutil.copy(grub_src, grub_target)

                if not os.path.isfile(os.path.join(target_dir, "boot.cfg")):
                    cls.do_configure_grubefi(part, creator, target_dir)

            else:
                raise WicError("unrecognized bootimg-efi loader: %s" %
                               source_params['loader'])
        except KeyError:
            raise WicError("bootimg-efi requires a loader, none specified")

        # 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:
            # Default to 100 blocks of extra space for file system overhead
            esp_extra_blocks = int(source_params.get('esp_extra_blocks',
                                                     '100'))

            du_cmd = "du -bks %s/EFI" % isodir
            out = exec_cmd(du_cmd)
            blocks = int(out.split()[0])
            blocks += esp_extra_blocks
            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

            esp_label = source_params.get('esp_label', 'EFIimg')

            dosfs_cmd = 'mkfs.vfat -n \'%s\' -S 512 -C %s %d' \
                        % (esp_label, bootimg, blocks)
            exec_native_cmd(dosfs_cmd, native_sysroot)

            mmd_cmd = "mmd -i %s ::/EFI" % bootimg
            exec_native_cmd(mmd_cmd, native_sysroot)

            mcopy_cmd = "mcopy -i %s -s %s/EFI/* ::/EFI/" \
                        % (bootimg, isodir)
            exec_native_cmd(mcopy_cmd, native_sysroot)

            chmod_cmd = "chmod 644 %s" % bootimg
            exec_cmd(chmod_cmd)

        # Prepare files for legacy boot
        syslinux_dir = get_bitbake_var("STAGING_DATADIR")
        if not syslinux_dir:
            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
Example #35
0
    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

        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(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)
            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 loader: %s" %
                               source_params['loader'])
        except KeyError:
            raise WicError("bootimg-efi 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 -i %s -C %s %d" % \
                    (part.fsuuid, 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 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:
            target_dir = "%s/EFI/BOOT" % isodir
            if os.path.exists(target_dir):
                shutil.rmtree(target_dir)

            os.makedirs(target_dir)

            if source_params['loader'] == 'grub-efi':
                # 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_image = "grub-efi-bootx64.efi"
                elif re.match('i.86', target_arch):
                    grub_image = "grub-efi-bootia32.efi"
                else:
                    raise WicError("grub-efi is incompatible with target %s" %
                                   target_arch)

                grub_target = os.path.join(target_dir, grub_image)
                if not os.path.isfile(grub_target):
                    grub_src = os.path.join(deploy_dir, grub_image)
                    if not os.path.exists(grub_src):
                        raise WicError("Grub loader %s is not found in %s. "
                                       "Please build grub-efi first" % (grub_image, deploy_dir))
                    shutil.copy(grub_src, grub_target)

                if not os.path.isfile(os.path.join(target_dir, "boot.cfg")):
                    cls.do_configure_grubefi(part, creator, target_dir)

            else:
                raise WicError("unrecognized bootimg-efi loader: %s" %
                               source_params['loader'])
        except KeyError:
            raise WicError("bootimg-efi requires a loader, none specified")

        # 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")
        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
Example #37
0
    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/menu.c32 %s/menu.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))

        initrd = get_bitbake_var("INITRD")
        if initrd:
            cmds += (("install -m 0644 %s %s/initrd" % (initrd, 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%s.img" % (cr_workdir, part.lineno)

        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
Example #38
0
    def do_prepare_partition(cls, part, source_params, creator, cr_workdir,
                             oe_builddir, bootimg_dir, kernel_dir,
                             rootfs_dir, native_sysroot):
        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")

        hdddir = "%s/hdd/" % cr_workdir

        machine_translated = get_bitbake_var('MACHINE_ARCH')

        #underscores in MACHINE_ARCH are replaced by - in filenames
        machine_translated = machine_translated.replace("_","-")

        kernel_stagingdir = get_bitbake_var("STAGING_KERNEL_BUILDDIR")

        rootfs = get_bitbake_var("IMAGE_ROOTFS")

        versionfile = open(kernel_stagingdir + "/kernel-abiversion", "r")
        kernelversion = versionfile.read().rstrip()
        versionfile.close()
        
        modulesname = "{0}/modules-{1}.tgz".format(kernel_dir, machine_translated)
        modulesname = os.readlink(modulesname)

        try:
            cp_cmd = "tar -xzf {0}/{1} --directory {2}".format(kernel_dir, modulesname, hdddir)
            exec_cmd(cp_cmd, True)
        except KeyError:
            raise WicError("error while copying kernel modules")

        try:
            cp_cmd = "/sbin/depmod --basedir \"{1}\" --config \"{0}/etc/depmod.d\" {2}".format(rootfs, hdddir, kernelversion)
            exec_cmd(cp_cmd, True)
        except KeyError:
            raise WicError("Failed to execute depmod on modules")
        
        du_cmd = "du -B 1 -s %s" % hdddir
        out = exec_cmd(du_cmd)
        size_bytes = int(out.split()[0])

        size_bytes += 2**20

        logger.debug("out: %s, final size: %d", out, size_bytes)

        # create filesystem image 
        modulesimg = "%s/modules.img" % cr_workdir

        dosfs_cmd = "mksquashfs \"{0}/lib/modules/{1}\" {2} -b {3} -noI -noD -noF -noX -all-root  ".format(hdddir, kernelversion, modulesimg, "4096")
        logger.debug("Executing: %s" % dosfs_cmd)
        exec_native_cmd(dosfs_cmd, native_sysroot)

        chmod_cmd = "chmod 644 %s" % modulesimg
        exec_cmd(chmod_cmd)

        du_cmd = "du -Lbks %s" % modulesimg
        out = exec_cmd(du_cmd)
        modulesimg_size = out.split()[0]

        part.size = int(modulesimg_size)
        part.source_file = modulesimg
Example #39
0
    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)

        kernel_file = get_bitbake_var("KERNEL_FILE")
        rootfs_dir = rootfs_dir['ROOTFS_DIR']
        kernel = os.path.basename(
            os.path.realpath(os.path.join(rootfs_dir, kernel_file)))
        kernel_parts = kernel.split("-")
        kernel_suffix = "-".join(kernel_parts[1:])
        initrd = "initrd.img"
        config = "config"
        mapfile = "System.map"

        if kernel_suffix:
            initrd += "-%s" % kernel_suffix
            config += "-%s" % kernel_suffix
            mapfile += "-%s" % kernel_suffix

        hdddir = "%s/hdd/boot" % cr_workdir

        cmds = ("install -m 0644 %s/%s/%s %s/%s" %
                (rootfs_dir, "boot", kernel, hdddir, kernel),
                "install -m 0644 %s/%s/%s %s/%s" %
                (rootfs_dir, "boot", initrd, hdddir, initrd),
                "install -m 0644 %s/%s/%s %s/%s" %
                (rootfs_dir, "boot", config, hdddir, config),
                "install -m 0644 %s/%s/%s %s/%s" %
                (rootfs_dir, "boot", mapfile, hdddir, mapfile),
                "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 -i %s -S 512 -C %s %d" % \
                    (part.fsuuid, 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
Example #40
0
    def do_prepare_partition(cls, part, source_params, creator, cr_workdir,
                             oe_builddir, bootimg_dir, kernel_dir, rootfs_dir,
                             native_sysroot):
        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")

        hdddir = "%s/hdd/userdata/" % cr_workdir

        workdir = get_bitbake_var("WORKDIR")
        if not workdir:
            raise WicError(
                "Can not get WORKDIR directory - not in cooked mode?")

        deploy_dir_image = get_bitbake_var("DEPLOY_DIR_IMAGE")
        if not deploy_dir_image:
            raise WicError(
                "Can not get DEPLOY_DIR_IMAGE directory - not in cooked mode?")

        topdir = get_bitbake_var("TOPDIR")
        if not topdir:
            raise WicError(
                "Can not get TOPDIR directory - not in cooked mode?")

        tmpdir = TMPDIR = "%s/tmp_container" % topdir
        if not tmpdir:
            raise WicError(
                "Can not get TMPDIR directory - not in cooked mode?")

        #machine = get_bitbake_var("MACHINE")
        #if not machine:
        #    raise WicError("Can not get MACHINE - not in cooked mode?")

        deploy_dir_image = "{0}/deploy/images/{1}".format(tmpdir, "qemux86-64")
        if not deploy_dir_image:
            raise WicError(
                "Can not get DEPLOY_DIR_IMAGE directory - not in cooked mode?")

        D = get_bitbake_var("D")
        if not D:
            raise WicError("Can not get D directory - not in cooked mode?")

        trustme_hardware = get_bitbake_var("TRUSTME_HARDWARE")
        if not trustme_hardware:
            raise WicError(
                "Can not get trustme_hardware directory - not in cooked mode?")

        src = "%s/../trustme/build/" % topdir
        config_creator_dir = "%s/config_creator" % src
        proto_file_dir = "%s/cml/daemon" % workdir
        provisioning_dir = "%s/device_provisioning" % src
        enrollment_dir = "%s/oss_enrollment" % provisioning_dir
        test_cert_dir = "%s/test_certificates" % topdir

        if not os.path.exists(test_cert_dir):
            raise WicError(
                "Test PKI not generated at {0}\nIs trustx-cml-userdata built?".
                format(test_cert_dir))

        install_cmd = "install -d %s/cml/tokens" % hdddir
        exec_cmd(install_cmd)

        install_cmd = "install -d %s/cml/containers_templates" % hdddir
        exec_cmd(install_cmd)

        # copy device config
        cp_cmd = "cp {0}/trustx-configs/device.conf {1}/cml/".format(
            deploy_dir_image, hdddir)
        exec_cmd(cp_cmd)

        # copy container configs
        cp_cmd = "cp -ar {0}/trustx-configs/container/. {1}/cml/containers_templates/".format(
            deploy_dir_image, hdddir)
        exec_cmd(cp_cmd)

        cp_cmd = "cp {0}/ssig_rootca.cert {1}/cml/tokens/".format(
            test_cert_dir, hdddir)
        exec_cmd(cp_cmd)

        cp_cmd = "cp {0}/gen_rootca.cert {1}/cml/tokens/".format(
            test_cert_dir, hdddir)
        exec_cmd(cp_cmd)

        mkdir_cmd = "mkdir -p %s" % deploy_dir_image
        exec_cmd(mkdir_cmd)

        mkdir_cmd = "mkdir -p %s/cml/operatingsystems/" % hdddir
        exec_cmd(mkdir_cmd)

        mkdir_cmd = "mkdir -p %s/cml/containers/" % hdddir
        exec_cmd(mkdir_cmd)

        cp_cmd = "cp -ar {0}/trustx-guests/. {1}/cml/operatingsystems".format(
            deploy_dir_image, hdddir)
        exec_cmd(cp_cmd)

        du_cmd = "du --bytes -s %s" % hdddir
        out = exec_cmd(du_cmd)
        fs_bytes = int(out.split()[0])

        fs_bytes += 2**28

        logger.debug("out: %s, bytes final: %d", out, fs_bytes)

        userdataimg = "%s/userdata.img" % cr_workdir

        ddblocks = math.ceil(fs_bytes / 4096.0)

        mkfs_cmd = "dd if=/dev/zero of={0} bs={1} count={2}".format(
            userdataimg, "4096", ddblocks)
        exec_cmd(mkfs_cmd, native_sysroot)

        mkfs_cmd = "mkfs.btrfs --label data --byte-count {0} --rootdir {1} {2}".format(
            fs_bytes, hdddir, userdataimg)
        logger.debug("Executing: %s" % mkfs_cmd)
        exec_native_cmd(mkfs_cmd, native_sysroot)

        chmod_cmd = "chmod 644 %s" % userdataimg
        exec_cmd(chmod_cmd)

        du_cmd = "du -Lbks %s" % userdataimg
        out = exec_cmd(du_cmd)
        userdataimg_size = out.split()[0]

        part.size = int(userdataimg_size)
        part.source_file = userdataimg
Example #41
0
    def do_prepare_partition(cls, part, source_params, creator, cr_workdir,
                             oe_builddir, bootimg_dir, kernel_dir, rootfs_dir,
                             native_sysroot):

        hdddir = "%s/hdd/boot" % cr_workdir

        builddir = get_bitbake_var("RECIPE_SYSROOT_NATIVE")
        topdir = get_bitbake_var("TOPDIR")

        try:
            cp_cmd = "cp -L {0}/usr/bin/KeyTool.efi {1}/EFI/BOOT/BOOTX64.EFI".format(
                builddir, hdddir)
            exec_cmd(cp_cmd, True)
        except KeyError:
            raise WicError("error copying KeyTool.efi")

        try:
            cp_cmd = "cp  {0}/test_certificates/DB.esl {0}/test_certificates/KEK.esl {0}/test_certificates/PK.auth {1}/keys/".format(
                topdir, hdddir)
            exec_cmd(cp_cmd, True)
        except KeyError:
            raise WicError("error copying test keys")

        try:
            cp_cmd = "cp  {0}/test_certificates/DB.der {0}/test_certificates/KEK.der {0}/test_certificates/PK.der {1}/keys/".format(
                topdir, hdddir)
            exec_cmd(cp_cmd, True)
        except KeyError:
            raise WicError("error copying test certs")

        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_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.
        """
        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%s.img" % (cr_workdir, part.lineno)

        dosfs_cmd = "mkdosfs -n boot -i %s -S 512 -C %s %d" % \
                    (part.fsuuid, 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
Example #43
0
    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 == self.extendedpart:
                # 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 - 2, self.extended_size_sec)

            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_name:
                logger.debug("partition %d: set name to %s", part.num,
                             part.part_name)
                exec_native_cmd("sgdisk --change-name=%d:%s %s" % \
                                         (part.num, part.part_name,
                                          self.path), self.native_sysroot)

            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)
Example #44
0
    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

        kernel = get_bitbake_var("KERNEL_IMAGETYPE")
        if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1":
            if get_bitbake_var("INITRAMFS_IMAGE"):
                kernel = "%s-%s.bin" % \
                    (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME"))

        install_cmd = "install -m 0644 %s/%s %s/%s" % \
            (staging_kernel_dir, kernel, hdddir, kernel)

        install_cmd = isar_populate_boot_cmd(rootfs_dir['ROOTFS_DIR'], hdddir)
        exec_cmd(install_cmd)

        if get_bitbake_var("IMAGE_EFI_BOOT_FILES"):
            for src_path, dst_path in cls.install_task:
                install_cmd = "install -m 0644 -D %s %s" \
                              % (os.path.join(kernel_dir, src_path),
                                 os.path.join(hdddir, dst_path))
                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(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 += "-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 regexp " + grub_modules
                    exec_cmd(grub_cmd)
            elif source_params['loader'] == 'systemd-boot':
                kernel_dir = os.path.join(rootfs_dir['ROOTFS_DIR'],
                                          "usr/lib/systemd/boot/efi/")
                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

        label = part.label if part.label else "ESP"

        dosfs_cmd = "mkdosfs -n %s -i %s -C %s %d" % \
                    (label, part.fsuuid, 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
Example #45
0
    def do_prepare_partition(cls, part, source_params, cr, cr_workdir,
                             oe_builddir, bootimg_dir, kernel_dir, krootfs_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.
        """
        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)

        part.rootfs_dir = cls.__get_rootfs_dir(rootfs_dir)
        pseudo_dir = os.path.join(part.rootfs_dir, "../pseudo")
        if not os.path.lexists(pseudo_dir):
            logger.warn("%s folder does not exist. "
                        "Usernames and permissions will be invalid " %
                        pseudo_dir)
            pseudo_dir = None

        new_rootfs = None
        new_pseudo = None
        # Handle excluded paths.
        if part.exclude_path or part.include_path or part.change_directory:
            # We need a new rootfs directory we can delete files from. Copy to
            # workdir.
            new_rootfs = os.path.realpath(
                os.path.join(cr_workdir, "rootfs%d" % part.lineno))

            if os.path.lexists(new_rootfs):
                shutil.rmtree(os.path.join(new_rootfs))

            if part.change_directory:
                cd = part.change_directory
                if cd[-1] == '/':
                    cd = cd[:-1]
                orig_dir = cls.__validate_path("--change-directory",
                                               part.rootfs_dir, cd)
            else:
                orig_dir = part.rootfs_dir
            copyhardlinktree(orig_dir, new_rootfs)

            # Convert the pseudo directory to its new location
            if (pseudo_dir):
                new_pseudo = os.path.realpath(
                    os.path.join(cr_workdir, "pseudo%d" % part.lineno))
                if os.path.lexists(new_pseudo):
                    shutil.rmtree(new_pseudo)
                os.mkdir(new_pseudo)
                shutil.copy(os.path.join(pseudo_dir, "files.db"),
                            os.path.join(new_pseudo, "files.db"))

                pseudo_cmd = "%s -B -m %s -M %s" % (cls.__get_pseudo(
                    native_sysroot, new_rootfs,
                    new_pseudo), orig_dir, new_rootfs)
                exec_native_cmd(pseudo_cmd, native_sysroot)

            for in_path in part.include_path or []:
                #parse arguments
                include_path = in_path[0]
                if len(in_path) > 2:
                    logger.error(
                        "'Invalid number of arguments for include-path")
                    sys.exit(1)
                if len(in_path) == 2:
                    path = in_path[1]
                else:
                    path = None

                # Pack files to be included into a tar file.
                # We need to create a tar file, because that way we can keep the
                # permissions from the files even when they belong to different
                # pseudo enviroments.
                # If we simply copy files using copyhardlinktree/copytree... the
                # copied files will belong to the user running wic.
                tar_file = os.path.realpath(
                    os.path.join(cr_workdir,
                                 "include-path%d.tar" % part.lineno))
                if os.path.isfile(include_path):
                    parent = os.path.dirname(os.path.realpath(include_path))
                    tar_cmd = "tar c --owner=root --group=root -f %s -C %s %s" % (
                        tar_file, parent, os.path.relpath(
                            include_path, parent))
                    exec_native_cmd(tar_cmd, native_sysroot)
                else:
                    if include_path in krootfs_dir:
                        include_path = krootfs_dir[include_path]
                    include_path = cls.__get_rootfs_dir(include_path)
                    include_pseudo = os.path.join(include_path, "../pseudo")
                    if os.path.lexists(include_pseudo):
                        pseudo = cls.__get_pseudo(native_sysroot, include_path,
                                                  include_pseudo)
                        tar_cmd = "tar cf %s -C %s ." % (tar_file,
                                                         include_path)
                    else:
                        pseudo = None
                        tar_cmd = "tar c --owner=root --group=root -f %s -C %s ." % (
                            tar_file, include_path)
                    exec_native_cmd(tar_cmd, native_sysroot, pseudo)

                #create destination
                if path:
                    destination = cls.__validate_path("--include-path",
                                                      new_rootfs, path)
                    Path(destination).mkdir(parents=True, exist_ok=True)
                else:
                    destination = new_rootfs

                #extract destination
                untar_cmd = "tar xf %s -C %s" % (tar_file, destination)
                if new_pseudo:
                    pseudo = cls.__get_pseudo(native_sysroot, new_rootfs,
                                              new_pseudo)
                else:
                    pseudo = None
                exec_native_cmd(untar_cmd, native_sysroot, pseudo)
                os.remove(tar_file)

            for orig_path in part.exclude_path or []:
                path = orig_path

                full_path = cls.__validate_path("--exclude-path", new_rootfs,
                                                path)

                if not os.path.lexists(full_path):
                    continue

                if path.endswith(os.sep):
                    # Delete content only.
                    for entry in os.listdir(full_path):
                        full_entry = os.path.join(full_path, entry)
                        if os.path.isdir(
                                full_entry) and not os.path.islink(full_entry):
                            shutil.rmtree(full_entry)
                        else:
                            os.remove(full_entry)
                else:
                    # Delete whole directory.
                    shutil.rmtree(full_path)

        part.prepare_rootfs(cr_workdir,
                            oe_builddir,
                            new_rootfs or part.rootfs_dir,
                            native_sysroot,
                            pseudo_dir=new_pseudo or pseudo_dir)