[PATCH v2] erofs-utils: add cmd argument to override uid/gid
Hu Weiwen
sehuww at mail.scut.edu.cn
Thu Apr 1 22:36:10 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.
This interface mimics that of 'mksquashfs'.
Signed-off-by: Hu Weiwen <sehuww at mail.scut.edu.cn>
---
include/erofs/config.h | 2 ++
lib/config.c | 2 ++
lib/inode.c | 4 ++--
man/mkfs.erofs.1 | 9 +++++++++
mkfs/main.c | 23 +++++++++++++++++++++++
5 files changed, 38 insertions(+), 2 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/man/mkfs.erofs.1 b/man/mkfs.erofs.1
index dcaf9d7..254c3ec 100644
--- a/man/mkfs.erofs.1
+++ b/man/mkfs.erofs.1
@@ -69,6 +69,15 @@ You may give multiple `--exclude-regex` options.
.BI "\-\-file-contexts=" file
Specify a \fIfile_contexts\fR file to setup / override selinux labels.
.TP
+.BI "\-\-force-uid=" UID
+Set all file uids to \fIUID\fR.
+.TP
+.BI "\-\-force-gid=" GID
+Set all file gids to \fIGID\fR.
+.TP
+.B \-\-all-root
+Make all files owned by root.
+.TP
.B \-\-help
Display this help and exit.
.SH AUTHOR
diff --git a/mkfs/main.c b/mkfs/main.c
index d9c4c7f..72b7f17 100644
--- a/mkfs/main.c
+++ b/mkfs/main.c
@@ -36,6 +36,9 @@ static struct option long_options[] = {
#ifdef HAVE_LIBSELINUX
{"file-contexts", required_argument, NULL, 4},
#endif
+ {"force-uid", required_argument, NULL, 5},
+ {"force-gid", required_argument, NULL, 6},
+ {"all-root", no_argument, NULL, 7},
#ifdef WITH_ANDROID
{"mount-point", required_argument, NULL, 10},
{"product-out", required_argument, NULL, 11},
@@ -74,6 +77,9 @@ static void usage(void)
#ifdef HAVE_LIBSELINUX
" --file-contexts=X specify a file contexts file to setup selinux labels\n"
#endif
+ " --force-uid=UID set all file uids to UID\n"
+ " --force-gid=GID set all file gids to GID\n"
+ " --all-root make all files owned by root\n"
" --help display this help and exit\n"
#ifdef WITH_ANDROID
"\nwith following android-specific options:\n"
@@ -233,6 +239,23 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
if (opt && opt != -EBUSY)
return opt;
break;
+ case 5:
+ cfg.c_uid = strtoul(optarg, &endptr, 0);
+ if (cfg.c_uid == -1 || *endptr != '\0') {
+ erofs_err("invalid uid %s", optarg);
+ return -EINVAL;
+ }
+ break;
+ case 6:
+ cfg.c_gid = strtoul(optarg, &endptr, 0);
+ if (cfg.c_gid == -1 || *endptr != '\0') {
+ erofs_err("invalid gid %s", optarg);
+ return -EINVAL;
+ }
+ break;
+ case 7:
+ 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