On Thu, Jun 29, 2023 at 06:09:07PM +0200, Laszlo Ersek wrote:
On 6/29/23 14:50, Richard W.M. Jones wrote:
> This currently only works with numeric UID.GID. In theory in future
> we could look up IDs from the guest password file (eg. using Augeas)
> and do the right thing, but that's a bunch more work.
>
> For example:
>
> $ ./builder/virt-builder fedora-36 --chown 1.1:/var/tmp
> [ 1.0] Downloading:
http://builder.libguestfs.org/fedora-36.xz
> [ 1.5] Planning how to build this image
> [ 1.5] Uncompressing
> [ 3.4] Opening the new disk
> [ 7.3] Setting a random seed
> [ 7.3] Changing owner of /var/tmp to 1.1
> [ 7.3] Setting passwords
> virt-builder: Setting random password of root to x8fu6z7QNEdPeZHF
> [ 7.8] SELinux relabelling
> [ 12.0] Finishing off
> Output file: fedora-36.img
> Output size: 6.0G
> Output format: raw
> Total usable space: 6.0G
> Free space: 4.7G (79%)
>
> $ guestfish -a fedora-36.img -i ll /var
> total 8
> drwxr-xr-x. 18 root root 4096 May 12 2022 .
> dr-xr-xr-x. 18 root root 235 May 12 2022 ..
> drwxr-xr-x. 2 root root 6 Jan 20 2022 adm
> drwxr-xr-x. 9 root root 101 May 12 2022 cache
> drwxr-xr-x. 3 root root 18 May 12 2022 db
> drwxr-xr-x. 2 root root 6 Jan 20 2022 empty
> drwxr-xr-x. 2 root root 6 Jan 20 2022 ftp
> drwxr-xr-x. 2 root root 6 Jan 20 2022 games
> drwxr-xr-x. 3 root root 18 Apr 5 2022 kerberos
> drwxr-xr-x. 24 root root 4096 May 12 2022 lib
> drwxr-xr-x. 2 root root 6 Jan 20 2022 local
> lrwxrwxrwx. 1 root root 11 May 12 2022 lock -> ../run/lock
> drwxr-xr-x. 8 root root 105 May 12 2022 log
> lrwxrwxrwx. 1 root root 10 Jan 20 2022 mail -> spool/mail
> drwxr-xr-x. 2 root root 6 Jan 20 2022 nis
> drwxr-xr-x. 2 root root 6 Jan 20 2022 opt
> drwxr-xr-x. 2 root root 6 Jan 20 2022 preserve
> lrwxrwxrwx. 1 root root 6 May 12 2022 run -> ../run
> drwxr-xr-x. 5 root root 45 May 12 2022 spool
> drwxrwxrwt. 2 bin bin 6 May 12 2022 tmp
> drwxr-xr-x. 2 root root 6 Jan 20 2022 yp
>
> Fixes:
https://github.com/rwmjones/guestfs-tools/issues/12
> ---
> common | 2 +-
> customize/customize_run.ml | 11 +++++++++++
> 2 files changed, 12 insertions(+), 1 deletion(-)
>
> diff --git a/common b/common
> index 420892e660..bbb54714ce 160000
> --- a/common
> +++ b/common
> @@ -1 +1 @@
> -Subproject commit 420892e660726c7184c000b9b86b11f491a5a126
> +Subproject commit bbb54714ce24c76e5761d96a0227a753896dc4c4
> diff --git a/customize/customize_run.ml b/customize/customize_run.ml
> index f03774e003..409b46edc6 100644
> --- a/customize/customize_run.ml
> +++ b/customize/customize_run.ml
> @@ -153,6 +153,17 @@ let run (g : G.guestfs) root (ops : ops) =
> let mode = if String.is_prefix mode "0" then "0o" ^ mode
else mode in
> g#chmod (int_of_string mode) path
>
> + | `Chown (uid_gid, path) ->
> + let uid, gid = String.split "." uid_gid in
> + let uid, gid =
> + try int_of_string uid, int_of_string gid
> + with Failure _ ->
> + error (f_"--chown: could not parse numeric UID.GID from \
> + %s") uid_gid in
> +
> + message (f_"Changing owner of %s to %d.%d") path uid gid;
> + g#chown uid gid path
> +
> | `Command cmd ->
> message (f_"Running: %s") cmd;
> do_run ~display:cmd cmd
I've not looked at the context beyond this patch; the patch does look good.
Acked-by: Laszlo Ersek <lersek(a)redhat.com>
I think we could extend this in future to support fetching UIDs from
inside the guest, or to work recursively, but this is fine for the
most common case, setting files to root.root.
Thanks,
Rich.
--
Richard Jones, Virtualization Group, Red Hat
http://people.redhat.com/~rjones
Read my programming and virtualization blog:
http://rwmj.wordpress.com
libguestfs lets you edit virtual machines. Supports shell scripting,
bindings from many languages.
http://libguestfs.org