[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
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_?) 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@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
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
On Tue, Sep 02, 2014 at 06:15:11PM +0200, Pino Toscano wrote:
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
?
That works for me. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://people.redhat.com/~rjones/virt-df/
participants (2)
-
Pino Toscano -
Richard W.M. Jones