I’m quite a heavy user of chroots, since I work with many different releases, and Debian, too. That usually means I have to clean them up at some point, too.
It happened at least twice now: I deleted an old chroot without unmounting all those bind mounts I have in them (/proc, /sys, /tmp, and most importantly, /home). Thus an rm -rf /chroots/edgy will remove my entire Home-partition, too! Fortunately I Control-C’ed early enough the second time to not cause any real damage except for killing my X socket in /tmp. Phew! But the first time (some months ago already) I lost quite some amount of data.
Thus, very high in my list of things I’d change to a default if I had a time machine to go back to the design of Unix:
alias rm='rm --one-file-system'
which now went into my standard ~/.bashrc.
#1 by John Gill on 2008/07/21 - 20:05
Zitieren
I would do:
alias rid=’rm –one-file-system’
instead and then get into the habit of using rid.
aliasing standard commands like rm to a less dangerous version of themselves seems a great idea until you go onto a system that doesn’t have the alias.
#2 by Rob J. Caskey on 2008/07/21 - 20:16
Zitieren
I have, in the past, rm -rf’d /target, although fortunately is a fresh install w/ debootstrap, but still, yeah, been there.
#3 by Michael MacDonald on 2008/07/21 - 21:38
Zitieren
It’s not a foolproof way to handle it, but you could write a wrapper script to manage your chroot work.
I can’t share my work scripts, but the logic goes something like this:
BIND_MOUNTS=/foo /bar /baz
for mount in $BIND_MOUNTS; do
done
sudo chroot $CHROOT
The trick is to create a rollback() function that un-does all the mounts, and then do something like this:
trap rollback INT
trap rollback TERM
trap rollback EXIT
That way, things get cleaned up on most exit conditions. You can make your script pretty fancy, to avoid multiple mounts and do distro-specific stuff. You can also use a ROLLBACK array, instead of hard-coding the cleanup process… etc. etc.
Might be too much work for a one-off, but if you spend a lot of time inside chroots, it makes life a lot easier.
#4 by foo on 2008/07/22 - 09:11
Zitieren
Use schroot instead of wasting time with scripting. schroot rocks.