[PATCH] erofs-utils: add command line argument to override uid/gid

Hu Weiwen sehuww at mail.scut.edu.cn
Thu Apr 1 16:29:03 AEDT 2021


Also added '--all-root' option to set uid and gid to root conveniently.

This function can be useful if we want to pack some data owned by user with
large uid, but we want to use compact inode.

Signed-off-by: Hu Weiwen <sehuww at mail.scut.edu.cn>
---
 include/erofs/config.h |  2 ++
 lib/config.c           |  2 ++
 lib/inode.c            |  4 ++--
 mkfs/main.c            | 23 ++++++++++++++++++++++-
 4 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/include/erofs/config.h b/include/erofs/config.h
index 02ddf59..e6eaef6 100644
--- a/include/erofs/config.h
+++ b/include/erofs/config.h
@@ -54,6 +54,8 @@ struct erofs_configure {
 	/* < 0, xattr disabled and INT_MAX, always use inline xattrs */
 	int c_inline_xattr_tolerance;
 	u64 c_unix_timestamp;
+	u32 c_uid;
+	u32 c_gid;
 #ifdef WITH_ANDROID
 	char *mount_point;
 	char *target_out_path;
diff --git a/lib/config.c b/lib/config.c
index 3ecd481..b8df239 100644
--- a/lib/config.c
+++ b/lib/config.c
@@ -24,6 +24,8 @@ void erofs_init_configure(void)
 	cfg.c_force_inodeversion = 0;
 	cfg.c_inline_xattr_tolerance = 2;
 	cfg.c_unix_timestamp = -1;
+	cfg.c_uid = -1;
+	cfg.c_gid = -1;
 }
 
 void erofs_show_config(void)
diff --git a/lib/inode.c b/lib/inode.c
index 40189fe..d52facf 100644
--- a/lib/inode.c
+++ b/lib/inode.c
@@ -752,8 +752,8 @@ int erofs_fill_inode(struct erofs_inode *inode,
 	if (err)
 		return err;
 	inode->i_mode = st->st_mode;
-	inode->i_uid = st->st_uid;
-	inode->i_gid = st->st_gid;
+	inode->i_uid = cfg.c_uid == -1 ? st->st_uid : cfg.c_uid;
+	inode->i_gid = cfg.c_gid == -1 ? st->st_gid : cfg.c_gid;
 	inode->i_ctime = st->st_ctime;
 	inode->i_ctime_nsec = st->st_ctim.tv_nsec;
 
diff --git a/mkfs/main.c b/mkfs/main.c
index d9c4c7f..49b94b4 100644
--- a/mkfs/main.c
+++ b/mkfs/main.c
@@ -36,6 +36,7 @@ static struct option long_options[] = {
 #ifdef HAVE_LIBSELINUX
 	{"file-contexts", required_argument, NULL, 4},
 #endif
+	{"all-root", no_argument, NULL, 5},
 #ifdef WITH_ANDROID
 	{"mount-point", required_argument, NULL, 10},
 	{"product-out", required_argument, NULL, 11},
@@ -74,6 +75,9 @@ static void usage(void)
 #ifdef HAVE_LIBSELINUX
 	      " --file-contexts=X  specify a file contexts file to setup selinux labels\n"
 #endif
+	      " --all-root         make all files owned by root\n"
+	      " -u#                set all file uids to #\n"
+	      " -g#                set all file gids to #\n"
 	      " --help             display this help and exit\n"
 #ifdef WITH_ANDROID
 	      "\nwith following android-specific options:\n"
@@ -152,7 +156,7 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 	char *endptr;
 	int opt, i;
 
-	while((opt = getopt_long(argc, argv, "d:x:z:E:T:U:",
+	while ((opt = getopt_long(argc, argv, "d:x:z:E:T:U:u:g:",
 				 long_options, NULL)) != -1) {
 		switch (opt) {
 		case 'z':
@@ -203,6 +207,20 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 			}
 			cfg.c_timeinherit = TIMESTAMP_FIXED;
 			break;
+		case 'u':
+			cfg.c_uid = strtoul(optarg, &endptr, 0);
+			if (cfg.c_uid == -1 || *endptr != '\0') {
+				erofs_err("invalid uid %s", optarg);
+				return -EINVAL;
+			}
+			break;
+		case 'g':
+			cfg.c_gid = strtoul(optarg, &endptr, 0);
+			if (cfg.c_gid == -1 || *endptr != '\0') {
+				erofs_err("invalid gid %s", optarg);
+				return -EINVAL;
+			}
+			break;
 #ifdef HAVE_LIBUUID
 		case 'U':
 			if (uuid_parse(optarg, sbi.uuid)) {
@@ -233,6 +251,9 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 			if (opt && opt != -EBUSY)
 				return opt;
 			break;
+		case 5:
+			cfg.c_uid = cfg.c_gid = 0;
+			break;
 #ifdef WITH_ANDROID
 		case 10:
 			cfg.mount_point = optarg;
-- 
2.25.1



More information about the Linux-erofs mailing list