[wic][PATCH] partition.py: restore selinux label for fstab

Yi Zhao <[email protected]>
Newsgroups org.yoctoproject.lists.yocto-patches
Message-ID <[email protected]>
When SELinux is enabled, all files within a wic image retain their
SELinux labels except for /etc/fstab, which is replaced by a newly
generated one during the build, causing its extended attributes to be
lost.

Restore the SELinux context on the new fstab using debugfs ea_get and
ea_set commands, ensuring all files are correctly labeled at build time
and avoiding a full SELinux relabel on first boot.

Signed-off-by: Yi Zhao <[email protected]>
---
 src/wic/partition.py | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/src/wic/partition.py b/src/wic/partition.py
index 435d31d..506ef2b 100644
--- a/src/wic/partition.py
+++ b/src/wic/partition.py
@@ -362,10 +362,21 @@ class Partition():
 
         if self.updated_fstab_path and self.has_fstab and not self.no_fstab_update:
             debugfs_script_path = os.path.join(cr_workdir, "debugfs_script")
+            fstab_label_path = os.path.join(cr_workdir, "fstab.selinuxlabel")
+            with open(debugfs_script_path, "w") as f:
+                f.write("cd etc\n")
+                # Retrieve fstab selinux label
+                f.write("ea_get -f %s fstab security.selinux\n" % (fstab_label_path))
+            debugfs_cmd = "debugfs -w -f %s %s" % (debugfs_script_path, rootfs)
+            exec_native_cmd(debugfs_cmd, native_sysroot)
+
             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))
+                if os.path.isfile(fstab_label_path) and os.path.getsize(fstab_label_path) > 0:
+                    # Restore fstab selinux label
+                    f.write("ea_set -f %s fstab security.selinux\n" % (fstab_label_path))
             debugfs_cmd = "debugfs -w -f %s %s" % (debugfs_script_path, rootfs)
             exec_native_cmd(debugfs_cmd, native_sysroot)
 
-- 
2.43.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.