Pino Toscano
2014-Sep-02 15:33 UTC
[Libguestfs] [PATCH] sysprep: user-account: remove the correct home
Query using augeas for the home directory of an user, instead of hardcoding /home/<username>. --- sysprep/sysprep_operation_user_account.ml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sysprep/sysprep_operation_user_account.ml b/sysprep/sysprep_operation_user_account.ml index 3d88ffc..dc194f4 100644 --- a/sysprep/sysprep_operation_user_account.ml +++ b/sysprep/sysprep_operation_user_account.ml @@ -84,6 +84,8 @@ let user_account_perform ~verbose ~quiet g root side_effects String.sub userpath (i+1) (String.length userpath -i-1) in if uid >= uid_min && uid <= uid_max && check_remove_user username then ( + (* Get the home before removing the passwd entry. *) + let home_dir = g#aug_get (userpath ^ "/home") in g#aug_rm userpath; (* XXX Augeas doesn't yet have a lens for /etc/shadow, so the * next line currently does nothing, but should start to @@ -91,7 +93,7 @@ let user_account_perform ~verbose ~quiet g root side_effects *) g#aug_rm (sprintf "/files/etc/shadow/%s" username); g#aug_rm (sprintf "/files/etc/group/%s" username); - g#rm_rf ("/home/" ^ username) + g#rm_rf home_dir ) ) users; g#aug_save (); -- 1.9.3
Richard W.M. Jones
2014-Sep-02 15:54 UTC
[Libguestfs] [PATCH] sysprep: user-account: remove the correct home
On Tue, Sep 02, 2014 at 05:33:26PM +0200, Pino Toscano wrote:> Query using augeas for the home directory of an user, instead of > hardcoding /home/<username>. > --- > sysprep/sysprep_operation_user_account.ml | 4 +++- > 1 file changed, 3 insertions(+), 1 deletion(-) > > diff --git a/sysprep/sysprep_operation_user_account.ml b/sysprep/sysprep_operation_user_account.ml > index 3d88ffc..dc194f4 100644 > --- a/sysprep/sysprep_operation_user_account.ml > +++ b/sysprep/sysprep_operation_user_account.ml > @@ -84,6 +84,8 @@ let user_account_perform ~verbose ~quiet g root side_effects > String.sub userpath (i+1) (String.length userpath -i-1) in > if uid >= uid_min && uid <= uid_max > && check_remove_user username then ( > + (* Get the home before removing the passwd entry. *) > + let home_dir = g#aug_get (userpath ^ "/home") inI suspect you'll want to catch an exception here (G.Error I _think_?) Not sure how to handle the exception -- perhaps by ignoring the user and continuing to the next one, and certainly by not deleting any directories ... ACK with some kind of exception handling. Rich.> g#aug_rm userpath; > (* XXX Augeas doesn't yet have a lens for /etc/shadow, so the > * next line currently does nothing, but should start to > @@ -91,7 +93,7 @@ let user_account_perform ~verbose ~quiet g root side_effects > *) > g#aug_rm (sprintf "/files/etc/shadow/%s" username); > g#aug_rm (sprintf "/files/etc/group/%s" username); > - g#rm_rf ("/home/" ^ username) > + g#rm_rf home_dir > ) > ) users; > g#aug_save (); > -- > 1.9.3 > > _______________________________________________ > Libguestfs mailing list > Libguestfs at redhat.com > https://www.redhat.com/mailman/listinfo/libguestfs-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-builder quickly builds VMs from scratch http://libguestfs.org/virt-builder.1.html
Pino Toscano
2014-Sep-02 16:15 UTC
Re: [Libguestfs] [PATCH] sysprep: user-account: remove the correct home
On Tuesday 02 September 2014 16:54:12 Richard W.M. Jones wrote:> On Tue, Sep 02, 2014 at 05:33:26PM +0200, Pino Toscano wrote: > > Query using augeas for the home directory of an user, instead of > > hardcoding /home/<username>. > > --- > > > > sysprep/sysprep_operation_user_account.ml | 4 +++- > > 1 file changed, 3 insertions(+), 1 deletion(-) > > > > diff --git a/sysprep/sysprep_operation_user_account.ml > > b/sysprep/sysprep_operation_user_account.ml index 3d88ffc..dc194f4 > > 100644 > > --- a/sysprep/sysprep_operation_user_account.ml > > +++ b/sysprep/sysprep_operation_user_account.ml > > @@ -84,6 +84,8 @@ let user_account_perform ~verbose ~quiet g root > > side_effects => > > String.sub userpath (i+1) (String.length userpath -i-1) > > in > > > > if uid >= uid_min && uid <= uid_max > > > > && check_remove_user username then ( > > > > + (* Get the home before removing the passwd entry. *) > > + let home_dir = g#aug_get (userpath ^ "/home") in > > I suspect you'll want to catch an exception here (G.Error I _think_?)Hm ok.> Not sure how to handle the exception -- perhaps by ignoring the user > and continuing to the next one, and certainly by not deleting any > directories ...So should the user still be removed? In such case, I guess something like (simplified): let home_dir try Some g#aug_get (userpath ^ "/home") with _ -> if verbose then warning "Cannot get the home directory for $user"; None in ... match home_dir with | None -> () | Some dir -> g#rm_rf dir ? -- Pino Toscano
Possibly Parallel Threads
- [PATCH] customize, sysprep: add a short SELinux note
- [PATCH 1/2] mllib: add and use last_part_of
- [PATCH 1/3] mllib: add an optional filter for rm_rf_only_files
- [PATCH] sysprep: remove /var/spool/mail/username
- [PATCH] sysprep: user-account: mark "changed file" if users were removed