wic: Fix permissions
v5 cover wic: Fix permissions
Hi
Today wic behaves differently if we run it from bitbake of directly from the commandline.
When it is run from bitbake, the permissions/usersnames are handled by the pseudo database of the main image.
When it is run from the comandline , it is run outside the main image database.
This results on permissions/usernames not working ok on some usecases on both bitbake and wic.
This is an attempt to fix all the permission bugs that I am aware from wic. Using the following usecases
#exclude-path
part / --source rootfs --fstype=ext4 --exclude-path=home
#split_partition
part / --source rootfs --ondisk sda --fstype=ext4 --exclude-path=etc/
part /etc --source rootfs --rootfs-dir=tmp/work/qt5222-poky-linux/core-image-minimal/1.0-r0/rootfs/etc/ --fstype=ext4
#multi_partition
part / --source rootfs --ondisk sda --fstype=ext4
part /export --source rootfs --rootfs=core-image-minimal-mtdutils --fstype=ext4
With the current master:
-------------------------
#exclude-path
From Bitbake: OK
wic cmdline: FAIL
#split_partition
From Bitbake: OK
wic cmdline: FAIL, permissions invalid on both partitions
#multi_partition
From Bitbake: FAIL second partition
wic cmdline: OK
After: wic: Fix permissions when using exclude or include path
--------------------------------------------------------------------
#exclude-path
From Bitbake: OK
wic cmdline: OK
#split_partition
From Bitbake: OK
wic cmdline: FAIL, permissions invalid on second partition
#multi_partition
From Bitbake: FAIL second partition
wic cmdline: OK
After: wic: Fix multi images .wks with bitbake
----------------------------------------------
#exclude-path
From Bitbake: OK
wic cmdline: OK
#split_partition
From Bitbake: FAIL, permissions invalid on second partition wic cmdline: FAIL, permissions invalid on second partition
#multi_partition
From Bitbake: OK
wic cmdline: OK
After: wic: Add --change-directory argument
--------------------------------------------
we can have a .wks like:
part / --source rootfs --ondisk sda --fstype=ext4 --exclude-path=etc/ part /etc --source rootfs --fstype=ext4 --change-directory=/etc
Wich works fine from bitbake and from cmdline, and has the same functionality as split_partition.
Changelog v4->v5:
- Add unittesting
- wic: Avoid creating invalid pseudo directory
Ricardo Ribalda Delgado (6):
wic: Fix permissions when using exclude or include path
wic: Fix multi images .wks with bitbake
wic: Add --change-directory argument
wic: Continue if excluded_path does not exist
wic: Avoid creating invalid pseudo directory
oeqa: wic: Add tests for permissions and change-directory
meta/classes/image_types_wic.bbclass | 8 ++- meta/lib/oeqa/selftest/cases/wic.py | 91 ++++++++++++++++++++++++ scripts/lib/wic/help.py | 6 ++ scripts/lib/wic/ksparser.py | 1 + scripts/lib/wic/partition.py | 20 +++--- scripts/lib/wic/plugins/source/rootfs.py | 52 ++++++++++++-- 6 files changed, 162 insertions(+), 16 deletions(-)
Signature · 1 line
2.25.1
PATCH v5 1/6 wic: Fix permissions when using exclude or include path
When parameters include_path or exclude_path are passed to the rootfs plugin, it will copy the partition content into a folder and make all the modifications there.
This is done using copyhardlinktree(), which does not take into consideration the content of the pseudo folder, which contains the information about the right permissions and ownership of the folders.
This results in a rootfs owned by the user that is running the wic command (usually UID 1000), which makes some rootfs unbootable.
This bug can be easily triggerd with the following .wks
part / --source rootfs --fstype=ext4 --exclude-path=home
And this sequence:
$ wic create test-permissions -e core-image-minimal -o test/ $ sudo mount test/test-permissions-202004080823-sda.direct.p1 /mnt $ ls -la /mnt/etc/shadow
To fix this we copy the content of the pseudo folders to the new folder and modify the pseudo database using the "pseudo -B" command.
If the rootfs is not a rootfs generated by bitbake a warning is shown making the user aware that the permissions on the target might not match what he expects.
WARNING: /tmp/test/../pseudo folder does not exist. Usernames and permissions will be invalid
--- scripts/lib/wic/partition.py | 7 +++-- scripts/lib/wic/plugins/source/rootfs.py | 36 ++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 6 deletions(-)
scripts/lib/wic/partition.py +4 -3
@@ -190,7 +190,7 @@ class Partition(): (self.mountpoint, self.size, self.fixed_size)) def prepare_rootfs(self, cr_workdir, oe_builddir, rootfs_dir, - native_sysroot, real_rootfs = True): + native_sysroot, real_rootfs = True, pseudo_dir = None): """ Prepare content for a rootfs partition i.e. create a partition and fill it from a /rootfs dir.
@@ -198,8 +198,9 @@ class Partition(): Currently handles ext2/3/4, btrfs, vfat and squashfs. """ p_prefix = os.environ.get("PSEUDO_PREFIX", "%s/usr" % native_sysroot) - p_localstatedir = os.environ.get("PSEUDO_LOCALSTATEDIR", - "%s/../pseudo" % rootfs_dir) + if (pseudo_dir == None): + pseudo_dir = "%s/../pseudo" % rootfs_dir + p_localstatedir = os.environ.get("PSEUDO_LOCALSTATEDIR", pseudo_dir) p_passwd = os.environ.get("PSEUDO_PASSWD", rootfs_dir) p_nosymlinkexp = os.environ.get("PSEUDO_NOSYMLINKEXP", "1") pseudo = "export PSEUDO_PREFIX=%s;" % p_prefix
scripts/lib/wic/plugins/source/rootfs.py +33 -3
@@ -20,7 +20,7 @@ from oe.path import copyhardlinktree from wic import WicError from wic.pluginbase import SourcePlugin -from wic.misc import get_bitbake_var +from wic.misc import get_bitbake_var, exec_native_cmd logger = logging.getLogger('wic')
@@ -44,6 +44,15 @@ class RootfsPlugin(SourcePlugin): return os.path.realpath(image_rootfs_dir) + @staticmethod + def __get_pseudo(native_sysroot, rootfs, pseudo_dir): + pseudo = "export PSEUDO_PREFIX=%s/usr;" % native_sysroot + pseudo += "export PSEUDO_LOCALSTATEDIR=%s;" % pseudo_dir + pseudo += "export PSEUDO_PASSWD=%s;" % rootfs + pseudo += "export PSEUDO_NOSYMLINKEXP=1;" + pseudo += "%s " % get_bitbake_var("FAKEROOTCMD") + return pseudo + @classmethod def do_prepare_partition(cls, part, source_params, cr, cr_workdir, oe_builddir, bootimg_dir, kernel_dir,
@@ -68,8 +77,14 @@ class RootfsPlugin(SourcePlugin): "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: # We need a new rootfs directory we can delete files from. Copy to
@@ -78,9 +93,23 @@ class RootfsPlugin(SourcePlugin): if os.path.lexists(new_rootfs): shutil.rmtree(os.path.join(new_rootfs)) - copyhardlinktree(part.rootfs_dir, new_rootfs) + # Convert the pseudo directory to its new location + if (pseudo_dir): + new_pseudo = os.path.join(new_rootfs, "../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), + part.rootfs_dir, new_rootfs) + exec_native_cmd(pseudo_cmd, native_sysroot) + for path in part.include_path or []: copyhardlinktree(path, new_rootfs)
@@ -112,4 +141,5 @@ class RootfsPlugin(SourcePlugin): shutil.rmtree(full_path) part.prepare_rootfs(cr_workdir, oe_builddir, - new_rootfs or part.rootfs_dir, native_sysroot) + new_rootfs or part.rootfs_dir, native_sysroot, + pseudo_dir = new_pseudo or pseudo_dir)
Signature · 1 line
2.25.1
PATCH v5 2/6 wic: Fix multi images .wks with bitbake
In reply to Ricardo RibaldaIn order to support .wks files with multiple images inside bitbake we need to explicitly set the pseudo database in use.
Eg: If we try this .mks:
part / --source rootfs --ondisk sda --fstype=ext4
part /export --source rootfs --rootfs=core-image-minimal-mtdutils --fstype=ext4
The username for all the files under /export will be set to the runner of bitbake (usually UID 1000).
Before we run wic, we need to make sure that the pseudo database will be flushed, and contains all the data needed.
--- meta/classes/image_types_wic.bbclass | 8 ++++++-- scripts/lib/wic/partition.py | 9 +++------ 2 files changed, 9 insertions(+), 8 deletions(-)
meta/classes/image_types_wic.bbclass +6 -2
@@ -32,8 +32,7 @@ IMAGE_CMD_wic () { if [ -z "$wks" ]; then bbfatal "No kickstart files from WKS_FILES were found: ${WKS_FILES}. Please set WKS_FILE or WKS_FILES appropriately." fi - - BUILDDIR="${TOPDIR}" wic create "$wks" --vars "${STAGING_DIR}/${MACHINE}/imgdata/" -e "${IMAGE_BASENAME}" -o "$build_wic/" ${WIC_CREATE_EXTRA_ARGS} + BUILDDIR="${TOPDIR}" PSEUDO_UNLOAD=1 wic create "$wks" --vars "${STAGING_DIR}/${MACHINE}/imgdata/" -e "${IMAGE_BASENAME}" -o "$build_wic/" ${WIC_CREATE_EXTRA_ARGS} mv "$build_wic/$(basename "${wks%.wks}")"*.direct "$out${IMAGE_NAME_SUFFIX}.wic" } IMAGE_CMD_wic[vardepsexclude] = "WKS_FULL_PATH WKS_FILES TOPDIR"
@@ -86,6 +85,10 @@ python do_write_wks_template () { bb.utils.copyfile(wks_file, "%s/%s" % (depdir, basename + '-' + os.path.basename(wks_file))) } +do_flush_pseudodb() { + ${FAKEROOTENV} ${FAKEROOTCMD} -S +} + python () { if d.getVar('USING_WIC'): wks_file_u = d.getVar('WKS_FULL_PATH', False)
@@ -139,6 +142,7 @@ python do_rootfs_wicenv () { depdir = d.getVar('IMGDEPLOYDIR') bb.utils.copyfile(os.path.join(outdir, basename) + '.env', os.path.join(depdir, basename) + '.env') } +addtask do_flush_pseudodb after do_image before do_image_wic addtask do_rootfs_wicenv after do_image before do_image_wic do_rootfs_wicenv[vardeps] += "${WICVARS}" do_rootfs_wicenv[prefuncs] = 'set_image_size'
scripts/lib/wic/partition.py +3 -6
@@ -200,13 +200,10 @@ class Partition(): p_prefix = os.environ.get("PSEUDO_PREFIX", "%s/usr" % native_sysroot) if (pseudo_dir == None): pseudo_dir = "%s/../pseudo" % rootfs_dir - p_localstatedir = os.environ.get("PSEUDO_LOCALSTATEDIR", pseudo_dir) - p_passwd = os.environ.get("PSEUDO_PASSWD", rootfs_dir) - p_nosymlinkexp = os.environ.get("PSEUDO_NOSYMLINKEXP", "1") pseudo = "export PSEUDO_PREFIX=%s;" % p_prefix - pseudo += "export PSEUDO_LOCALSTATEDIR=%s;" % p_localstatedir - pseudo += "export PSEUDO_PASSWD=%s;" % p_passwd - pseudo += "export PSEUDO_NOSYMLINKEXP=%s;" % p_nosymlinkexp + pseudo += "export PSEUDO_LOCALSTATEDIR=%s;" % pseudo_dir + pseudo += "export PSEUDO_PASSWD=%s;" % rootfs_dir + pseudo += "export PSEUDO_NOSYMLINKEXP=1;" pseudo += "%s " % get_bitbake_var("FAKEROOTCMD") rootfs = "%s/rootfs_%s.%s.%s" % (cr_workdir, self.label,
Signature · 1 line
2.25.1
PATCH v5 3/6 wic: Add --change-directory argument
In reply to Ricardo RibaldaThis option allows to specify which part of a rootfs is going to be included, the same way the -C argument on tar.
Thanks to this option we can make sure the permissions and usernames on the target partition are respected, and also simplify the creation of splitted partitons, not neeting to invoke external vars or using .wks.in files. Eg:
part / --source rootfs --ondisk sda --fstype=ext4 --exclude-path=etc/ part /etc --source rootfs --fstype=ext4 --change-directory=/etc
--- scripts/lib/wic/help.py | 6 ++++++ scripts/lib/wic/ksparser.py | 1 + scripts/lib/wic/partition.py | 1 + scripts/lib/wic/plugins/source/rootfs.py | 17 ++++++++++++++--- 4 files changed, 22 insertions(+), 3 deletions(-)
scripts/lib/wic/help.py +6 -0
@@ -980,6 +980,12 @@ DESCRIPTION copies. This option only has an effect with the rootfs source plugin. + --change-directory: This option is specific to wic. It changes to the + given directory before copying the files. This + option is useful when we want to split a rootfs in + multiple partitions and we want to keep the right + permissions and usernames in all the partitions. + --extra-space: This option is specific to wic. It adds extra space after the space filled by the content of the partition. The final size can go
scripts/lib/wic/ksparser.py +1 -0
@@ -138,6 +138,7 @@ class KickStart(): part.add_argument('--align', type=int) part.add_argument('--exclude-path', nargs='+') part.add_argument('--include-path', nargs='+') + part.add_argument('--change-directory') part.add_argument("--extra-space", type=sizetype) part.add_argument('--fsoptions', dest='fsopts') part.add_argument('--fstype', default='vfat',
scripts/lib/wic/partition.py +1 -0
@@ -31,6 +31,7 @@ class Partition(): self.extra_space = args.extra_space self.exclude_path = args.exclude_path self.include_path = args.include_path + self.change_directory = args.change_directory self.fsopts = args.fsopts self.fstype = args.fstype self.label = args.label
scripts/lib/wic/plugins/source/rootfs.py +14 -3
@@ -86,14 +86,25 @@ class RootfsPlugin(SourcePlugin): new_rootfs = None new_pseudo = None # Handle excluded paths. - if part.exclude_path or part.include_path: + 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)) - copyhardlinktree(part.rootfs_dir, new_rootfs) + + if part.change_directory: + cd = part.change_directory + if cd[-1] == '/': + cd = cd[:-1] + if os.path.isabs(cd): + logger.error("Must be relative: --change-directory=%s" % cd) + sys.exit(1) + orig_dir = os.path.join(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):
@@ -107,7 +118,7 @@ class RootfsPlugin(SourcePlugin): pseudo_cmd = "%s -B -m %s -M %s" % (cls.__get_pseudo(native_sysroot, new_rootfs, new_pseudo), - part.rootfs_dir, new_rootfs) + orig_dir, new_rootfs) exec_native_cmd(pseudo_cmd, native_sysroot) for path in part.include_path or []:
Signature · 1 line
2.25.1
PATCH v5 4/6 wic: Continue if excluded_path does not exist
In reply to Ricardo RibaldaIf an excuded path does not exist, continue without an error. This allows to seamleasly reuse .wks among different projects.
Eg:
part / --source rootfs --fstype=ext4 --exclude-path=/opt/private_keys
Where /opt/private_keys in only populated by some of the image.bb files.
--- scripts/lib/wic/plugins/source/rootfs.py | 3 +++ 1 file changed, 3 insertions(+)
scripts/lib/wic/plugins/source/rootfs.py +3 -0
@@ -131,6 +131,9 @@ class RootfsPlugin(SourcePlugin): sys.exit(1) full_path = os.path.realpath(os.path.join(new_rootfs, path)) + + if not os.path.lexists(full_path): + continue # Disallow climbing outside of parent directory using '..', # because doing so could be quite disastrous (we will delete the
Signature · 1 line
2.25.1
PATCH v5 5/6 wic: Avoid creating invalid pseudo directory
In reply to Ricardo RibaldaIf the source of the rootfs is not a bitbake cooked image, or it is not pointing to the root of one, we call pseudo again, which will produce a new pseudo folder at rootfs/../pseudo
Eg:
part /etc --source rootfs --rootfs-dir=${IMAGE_ROOTFS}/home --fstype=ext4
--- scripts/lib/wic/partition.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
scripts/lib/wic/partition.py +8 -7
@@ -199,13 +199,14 @@ class Partition(): Currently handles ext2/3/4, btrfs, vfat and squashfs. """ p_prefix = os.environ.get("PSEUDO_PREFIX", "%s/usr" % native_sysroot) - if (pseudo_dir == None): - pseudo_dir = "%s/../pseudo" % rootfs_dir - pseudo = "export PSEUDO_PREFIX=%s;" % p_prefix - pseudo += "export PSEUDO_LOCALSTATEDIR=%s;" % pseudo_dir - pseudo += "export PSEUDO_PASSWD=%s;" % rootfs_dir - pseudo += "export PSEUDO_NOSYMLINKEXP=1;" - pseudo += "%s " % get_bitbake_var("FAKEROOTCMD") + if (pseudo_dir): + pseudo = "export PSEUDO_PREFIX=%s;" % p_prefix + pseudo += "export PSEUDO_LOCALSTATEDIR=%s;" % pseudo_dir + pseudo += "export PSEUDO_PASSWD=%s;" % rootfs_dir + pseudo += "export PSEUDO_NOSYMLINKEXP=1;" + pseudo += "%s " % get_bitbake_var("FAKEROOTCMD") + else: + pseudo = None rootfs = "%s/rootfs_%s.%s.%s" % (cr_workdir, self.label, self.lineno, self.fstype)
Signature · 1 line
2.25.1
PATCH v5 6/6 oeqa: wic: Add tests for permissions and change-directory
In reply to Ricardo RibaldaMake sure that the permissions and username are respected when using all the rootfs modifiers.
Add tests for checnage-directory command
--- meta/lib/oeqa/selftest/cases/wic.py | 91 +++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+)
meta/lib/oeqa/selftest/cases/wic.py +91 -0
@@ -62,6 +62,12 @@ def extract_files(debugfs_output): return [line.split('/')[5].strip() for line in \ debugfs_output.strip().split('/\n')] +def files_own_by_root(debugfs_output): + for line in debugfs_output.strip().split('/\n'): + if line.split('/')[3:5] != ['0', '0']: + print(debugfs_output) + return False + return True class WicTestCase(OESelftestTestCase): """Wic test class."""
@@ -84,6 +90,7 @@ class WicTestCase(OESelftestTestCase): self.skipTest('wic-tools cannot be built due its (intltool|gettext)-native dependency and NLS disable') bitbake('core-image-minimal') + bitbake('core-image-minimal-mtdutils') WicTestCase.image_is_ready = True rmtree(self.resultdir, ignore_errors=True)
@@ -506,6 +513,90 @@ part /part2 --source rootfs --ondisk mmcblk0 --fstype=ext4 --include-path %s""" % (wks_file, self.resultdir), ignore_status=True).status) os.remove(wks_file) + def test_permissions(self): + """Test permissions are respected""" + + oldpath = os.environ['PATH'] + os.environ['PATH'] = get_bb_var("PATH", "wic-tools") + + t_normal = """ +part / --source rootfs --fstype=ext4 +""" + t_exclude = """ +part / --source rootfs --fstype=ext4 --exclude-path=home +""" + t_multi = """ +part / --source rootfs --ondisk sda --fstype=ext4 +part /export --source rootfs --rootfs=core-image-minimal-mtdutils --fstype=ext4 +""" + t_change = """ +part / --source rootfs --ondisk sda --fstype=ext4 --exclude-path=etc/ +part /etc --source rootfs --fstype=ext4 --change-directory=etc +""" + tests = [t_normal, t_exclude, t_multi, t_change] + + try: + for test in tests: + include_path = os.path.join(self.resultdir, 'test-include') + os.makedirs(include_path) + wks_file = os.path.join(include_path, 'temp.wks') + with open(wks_file, 'w') as wks: + wks.write(test) + runCmd("wic create %s -e core-image-minimal -o %s" \ + % (wks_file, self.resultdir)) + + for part in glob(os.path.join(self.resultdir, 'temp-*.direct.p*')): + res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part)) + self.assertEqual(True, files_own_by_root(res.output)) + + rmtree(self.resultdir, ignore_errors=True) + + finally: + os.environ['PATH'] = oldpath + + def test_change_directory(self): + """Test --change-directory wks option.""" + + oldpath = os.environ['PATH'] + os.environ['PATH'] = get_bb_var("PATH", "wic-tools") + + try: + include_path = os.path.join(self.resultdir, 'test-include') + os.makedirs(include_path) + wks_file = os.path.join(include_path, 'temp.wks') + with open(wks_file, 'w') as wks: + rootfs_dir = get_bb_var('IMAGE_ROOTFS', 'core-image-minimal') + wks.write("part /etc --source rootfs --fstype=ext4 --change-directory=etc") + runCmd("wic create %s -e core-image-minimal -o %s" \ + % (wks_file, self.resultdir)) + + part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0] + + res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1)) + files = extract_files(res.output) + self.assertIn('shadow', files) + + finally: + os.environ['PATH'] = oldpath + + def test_change_directory_errors(self): + """Test --change-directory wks option error handling.""" + wks_file = 'temp.wks' + + # Absolute argument. + with open(wks_file, 'w') as wks: + wks.write("part / --source rootfs --fstype=ext4 --change-directory /usr") + self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \ + % (wks_file, self.resultdir), ignore_status=True).status) + os.remove(wks_file) + + # Argument pointing to parent directory. + with open(wks_file, 'w') as wks: + wks.write("part / --source rootfs --fstype=ext4 --change-directory ././..") + self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \ + % (wks_file, self.resultdir), ignore_status=True).status) + os.remove(wks_file) + class Wic2(WicTestCase): def test_bmap_short(self):
Signature · 1 line
2.25.1