linux-erofs.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] erofs-utils: add command line argument to override uid/gid
@ 2021-04-01  5:29 Hu Weiwen
  2021-04-01  6:01 ` Gao Xiang
  0 siblings, 1 reply; 5+ messages in thread
From: Hu Weiwen @ 2021-04-01  5:29 UTC (permalink / raw)
  To: hsiangkao, linux-erofs

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@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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] erofs-utils: add command line argument to override uid/gid
  2021-04-01  5:29 [PATCH] erofs-utils: add command line argument to override uid/gid Hu Weiwen
@ 2021-04-01  6:01 ` Gao Xiang
  2021-04-01 11:36   ` [PATCH v2] erofs-utils: add cmd " Hu Weiwen
  0 siblings, 1 reply; 5+ messages in thread
From: Gao Xiang @ 2021-04-01  6:01 UTC (permalink / raw)
  To: Hu Weiwen; +Cc: linux-erofs

Hi Weiwen,

On Thu, Apr 01, 2021 at 01:29:03PM +0800, Hu Weiwen wrote:
> 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@mail.scut.edu.cn>

How about using long options for all options, e.g.
 --all-root,
 --force-uid=,
 --force-gid=, (seems squashfs uses such naming).

since I'd like to leave short options for more common usages.

And you might need to update manpage as well, so I can apply it
ASAP...

(Btw, even compact inodes have 16-bit ranges, we could introduce
 some mapping table to remap sparse uid/gid into a compact form,
 if that is what you want, you could help to implement it as well :) )

Thanks,
Gao Xiang


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2] erofs-utils: add cmd argument to override uid/gid
  2021-04-01  6:01 ` Gao Xiang
@ 2021-04-01 11:36   ` Hu Weiwen
  2021-04-01 12:01     ` Gao Xiang
  0 siblings, 1 reply; 5+ messages in thread
From: Hu Weiwen @ 2021-04-01 11:36 UTC (permalink / raw)
  To: hsiangkao, linux-erofs

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@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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] erofs-utils: add cmd argument to override uid/gid
  2021-04-01 11:36   ` [PATCH v2] erofs-utils: add cmd " Hu Weiwen
@ 2021-04-01 12:01     ` Gao Xiang
  2021-04-11 13:50       ` Li GuiFu via Linux-erofs
  0 siblings, 1 reply; 5+ messages in thread
From: Gao Xiang @ 2021-04-01 12:01 UTC (permalink / raw)
  To: Hu Weiwen; +Cc: linux-erofs

On Thu, Apr 01, 2021 at 07:36:10PM +0800, Hu Weiwen wrote:
> 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@mail.scut.edu.cn>

Yey! I've applied with the following modification,

diff --git a/include/erofs/config.h b/include/erofs/config.h
index e6eaef66b91c..15390f4ca9c8 100644
--- a/include/erofs/config.h
+++ b/include/erofs/config.h
@@ -54,8 +54,7 @@ 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;
+	u32 c_uid, c_gid;
 #ifdef WITH_ANDROID
 	char *mount_point;
 	char *target_out_path;
diff --git a/mkfs/main.c b/mkfs/main.c
index 72b7f17e1c66..d8823b539194 100644
--- a/mkfs/main.c
+++ b/mkfs/main.c
@@ -77,8 +77,8 @@ 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"
+	      " --force-uid=#      set all file uids to # (# = UID)\n"
+	      " --force-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

Otherwise looks good to me,
Reviewed-by: Gao Xiang <xiang@kernel.org>

Thanks,
Gao Xiang

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] erofs-utils: add cmd argument to override uid/gid
  2021-04-01 12:01     ` Gao Xiang
@ 2021-04-11 13:50       ` Li GuiFu via Linux-erofs
  0 siblings, 0 replies; 5+ messages in thread
From: Li GuiFu via Linux-erofs @ 2021-04-11 13:50 UTC (permalink / raw)
  To: Gao Xiang, Hu Weiwen; +Cc: linux-erofs



On 2021/4/1 20:01, Gao Xiang wrote:
> On Thu, Apr 01, 2021 at 07:36:10PM +0800, Hu Weiwen wrote:
>> 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@mail.scut.edu.cn>
> 
> Yey! I've applied with the following modification,
> 
> diff --git a/include/erofs/config.h b/include/erofs/config.h
> index e6eaef66b91c..15390f4ca9c8 100644
> --- a/include/erofs/config.h
> +++ b/include/erofs/config.h
> @@ -54,8 +54,7 @@ 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;
> +	u32 c_uid, c_gid;
>  #ifdef WITH_ANDROID
>  	char *mount_point;
>  	char *target_out_path;
> diff --git a/mkfs/main.c b/mkfs/main.c
> index 72b7f17e1c66..d8823b539194 100644
> --- a/mkfs/main.c
> +++ b/mkfs/main.c
> @@ -77,8 +77,8 @@ 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"
> +	      " --force-uid=#      set all file uids to # (# = UID)\n"
> +	      " --force-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
> 
> Otherwise looks good to me,
> Reviewed-by: Gao Xiang <xiang@kernel.org>
> 

It looks good
Reviewed-by: Li Guifu <bluce.lee@aliyun.com>

Thanks,

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-04-11 13:55 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-01  5:29 [PATCH] erofs-utils: add command line argument to override uid/gid Hu Weiwen
2021-04-01  6:01 ` Gao Xiang
2021-04-01 11:36   ` [PATCH v2] erofs-utils: add cmd " Hu Weiwen
2021-04-01 12:01     ` Gao Xiang
2021-04-11 13:50       ` Li GuiFu via Linux-erofs

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).