Skip to content
Snippets Groups Projects
Commit 4fa5ce9e authored by Davide Cardillo's avatar Davide Cardillo
Browse files

[WIC][CLASS] Add EgehogSystemPartitionPlugin class

Add a new WIC's class used as plugin.
This class allows to export one or a set of folders from the rootfs and
place them into an external partition that inherits that class.

To specify the folders to export this variable have to be populated:

EXTERNAL_ROOTFS_FOLDERS

An example of export of folders /etc, /home and /var:

EXTERNAL_ROOTFS_FOLDERS = " \
    var/ \
    etc/ \
    home/ \
"

The partition is setting is:

part / --source edgehog-system-partition --ondisk mmcblk --fstype=ext4 --label datafs  --size 128M --align 4

Automatically, this partition will include the folders etc,var and home
in its root folder.
parent 2e686440
No related branches found
No related tags found
1 merge request!20[WIC][CLASS] Add EgehogSystemPartitionPlugin class
......@@ -9,7 +9,11 @@ WKS_FILE:rockchip = "sdimage-rauc-rk.wks.in"
IMAGE_FSTYPES += "tar.bz2 wic wic.bmap wic.gz wic.bz2 squashfs-lz4 ext4 "
WICVARS:append = " IMAGE_EDGEHOG_BOOT_FILES "
WICVARS:append = " \
IMAGE_EDGEHOG_BOOT_FILES \
EXTERNAL_ROOTFS_FOLDERS \
"
IMAGE_EDGEHOG_BOOT_FILES ?= " \
seco_boot.scr \
ramfs.img \
......
#
# SPDX-License-Identifier: GPL-2.0-only
#
# DESCRIPTION
#
#
# AUTHORS
# Davide Cardillo <davide.cardillo (at) seco.com>
#
import logging
import os
import shutil
import sys
from oe.path import copyhardlinktree
from pathlib import Path
from wic import WicError
from wic.pluginbase import SourcePlugin
from wic.misc import get_bitbake_var, exec_native_cmd
logger = logging.getLogger('wic')
class EgehogSystemPartitionPlugin(SourcePlugin):
"""
Populate partition content from a rootfs folders.
"""
name = 'edgehog-system-partition'
@staticmethod
def __validate_path(cmd, rootfs_dir, path):
if os.path.isabs(path):
logger.error("%s: Must be relative: %s" % (cmd, orig_path))
sys.exit(1)
# Disallow climbing outside of parent directory using '..',
# because doing so could be quite disastrous (we will delete the
# directory, or modify a directory outside OpenEmbedded).
full_path = os.path.realpath(os.path.join(rootfs_dir, path))
if not full_path.startswith(os.path.realpath(rootfs_dir)):
logger.error("%s: Must point inside the rootfs:" % (cmd, path))
sys.exit(1)
return full_path
@staticmethod
def __get_rootfs_dir(rootfs_dir):
if os.path.isdir(rootfs_dir):
return os.path.realpath(rootfs_dir)
image_rootfs_dir = get_bitbake_var("IMAGE_ROOTFS", rootfs_dir)
if not os.path.isdir(image_rootfs_dir):
raise WicError("No valid artifact IMAGE_ROOTFS from image "
"named %s has been found at %s, exiting." %
(rootfs_dir, image_rootfs_dir))
return os.path.realpath(image_rootfs_dir)
@classmethod
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)
# We need a new rootfs directory we can safely modify without
# interfering with other tasks. Copy to workdir.
new_rootfs = os.path.realpath(os.path.join(cr_workdir, "rootfs_external%d" % part.lineno))
if os.path.lexists(new_rootfs):
shutil.rmtree(os.path.join(new_rootfs))
os.mkdir(new_rootfs)
include_folder = (get_bitbake_var("EXTERNAL_ROOTFS_FOLDERS") or "").split()
for include in include_folder or []:
logger.info( "path to includer: %s" % include )
include_path = include
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))
full_dst_path = cls.__validate_path("external rootfs export", new_rootfs, include_path)
full_src_path = cls.__validate_path("external rootfs export", rootfs_dir, include_path)
if not os.path.lexists(full_src_path):
logger.info("source path does not exist: %s" % full_src_path)
continue
if os.path.lexists(full_dst_path):
logger.info("destination path already exists: %s" % full_dst_path)
continue
if os.path.isfile(full_src_path):
parent = os.path.dirname(os.path.realpath(full_src_path))
tar_cmd = "tar c --owner=root --group=root -f %s -C %s %s" % (
tar_file, parent, os.path.relpath(full_src_path, parent))
else:
tar_cmd = "tar cf %s -C %s ." % (tar_file, full_src_path)
logger.info("tar cmd: %s" % tar_cmd)
exec_native_cmd(tar_cmd, native_sysroot)
logger.info( "include tar file %s" % tar_file )
#create destination
Path(full_dst_path).mkdir(parents=True, exist_ok=True)
#extract destination
untar_cmd = "tar xf %s -C %s" % (tar_file, full_dst_path)
exec_native_cmd(untar_cmd, native_sysroot, None)
os.remove(tar_file)
part.prepare_rootfs(cr_workdir, oe_builddir,
new_rootfs, native_sysroot,
False)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment