remove mounts automatically

Also combined all these little mount and cleanup helpers into one
`cleanup` script with flags.
This commit is contained in:
Jarvis Carroll 2025-05-24 00:05:26 +10:00
parent 28de550295
commit 3898aa52be
6 changed files with 108 additions and 84 deletions

View File

@ -71,26 +71,18 @@ or
Destroy Environment Destroy Environment
------------------- -------------------
Because chroot environments require multiple mounted directories to work, you The `create_environment` and `enter_environment` scripts try to clean up the
can't simply `rm -r` a chroot environment you created, or the repository as a mounts that they create, and the mounts will all disappear on reboot, but just
whole, without unmounting the mount points first. If you have rebooted your in case they are still present, you can run `sudo ./cleanup` to delete the
machine since setting up the chroot environments, then you don't need to worry, `test_environment` safely. If you want to delete both environments and
part or all of the repository can be straight-forwardly deleted, but if you are `debootstrap` in one go, then `sudo ./cleanup everything` will safely unmount
working with the repository and want to delete something yourself, there are `test_environment` and then delete all three directories.
two helper scripts that can be used to clean up the mount points and chroot
environments properly.
First `sudo ./destroy_environment` will unmount and delete `test_environment`, `cleanup` has other options too. For example, if you want to chroot into the
allowing you to remove an old environment without immediately creating a new environment as root, you can manually add the mount points back using
one. Anything else in the repository can be straight-forwardly deleted with `sudo ./cleanup add_mounts`, and then chroot in yourself. There is also
`sudo rm -r`, so with this you can put the repository in whatever state you `sudo ./cleanup mounts` to remove the mounts manually without deleting
want it to be in. `test_environment`.
If you want to conveniently remove all debian/debootstrap tools added, then
`sudo ./clean_everything` will run `destroy_environment`, and then delete
`clean_environment` and `debootstrap` for you, as well as `debootstrap.tar.gz`
if that got left behind by accident. Think of this as the 'distclean', for one
specific distribution.
Reuse an Existing Environment Reuse an Existing Environment
----------------------------- -----------------------------

90
debian/cleanup vendored Executable file
View File

@ -0,0 +1,90 @@
#!/bin/sh
if test `id -u` -ne 0
then
echo "$0 must be run as root."
return
fi
FRESH=clean_environment
ROOT=test_environment
add_mount() {
if mountpoint "$ROOT$1" > /dev/null
then
echo "$ROOT$1 already mounted."
else
echo "Mounting $1 to $ROOT$1"
mkdir -p "$ROOT$1"
mount -o bind "$1" "$ROOT$1"
fi
}
remove_mount() {
if mountpoint "$ROOT$1" > /dev/null
then
echo "Unmounting $ROOT$1"
umount "$ROOT$1"
fi
}
add_mounts() {
add_mount /proc
add_mount /sys
add_mount /dev
add_mount /dev/pts
}
remove_mounts() {
remove_mount /dev/pts
remove_mount /dev
remove_mount /sys
remove_mount /proc
}
remove_environment() {
if test -d "$ROOT"
then
# Call this script that automatically unmounts the mount points too.
remove_mounts
echo "Removing $ROOT"
rm -r "$ROOT"
else
echo "No environment found at $ROOT. Doing nothing."
fi
}
remove_everything() {
if test -d "$ROOT"
then
# Call this script that automatically unmounts the mount points too.
remove_environment
fi
if test -d "$FRESH"
then
echo "Removing $FRESH"
rm -r "$FRESH"
fi
if test -d debootstrap
then
echo "Removing debootstrap"
rm -r debootstrap
fi
if test -f debootstrap.tar.gz
then
echo "Removing debootstrap.tar.gz"
rm debootstrap.tar.gz
fi
}
case "$1" in
everything) remove_everything ;;
mounts) remove_mounts ;;
add_mounts) add_mounts ;;
environment) remove_environment ;;
"") remove_environment ;;
esac

View File

@ -1,32 +0,0 @@
#!/bin/sh
if test `id -u` -ne 0
then
echo "$0 must be run as root."
return
fi
if test -d test_environment
then
# Call this script that automatically unmounts the mount points too.
./destroy_environment
fi
if test -d clean_environment
then
echo "Removing clean_environment"
rm -r clean_environment
fi
if test -d debootstrap
then
echo "Removing debootstrap"
rm -r debootstrap
fi
if test -f debootstrap.tar.gz
then
echo "Removing debootstrap.tar.gz"
rm debootstrap.tar.gz
fi

View File

@ -20,7 +20,7 @@ fi
if test -e "$ROOT" if test -e "$ROOT"
then then
echo "Existing installation found at $ROOT, removing." echo "Existing installation found at $ROOT, removing."
./destroy_environment ./cleanup
fi fi
echo "Copying $FRESH to $ROOT." echo "Copying $FRESH to $ROOT."
@ -28,7 +28,7 @@ cp -r "$FRESH" "$ROOT"
echo "Initializing $ROOT." echo "Initializing $ROOT."
./mountpoints ./cleanup add_mounts
# Don't bother creating a new tmpfs. We don't want to leak files in, and we # Don't bother creating a new tmpfs. We don't want to leak files in, and we
# don't want to waste more RAM on a second tmpfs. The whole thing is # don't want to waste more RAM on a second tmpfs. The whole thing is
@ -38,3 +38,5 @@ chmod 1777 "$ROOT/tmp"
cp -r install_scripts "$ROOT/root" cp -r install_scripts "$ROOT/root"
chroot "$ROOT" /root/install_scripts/user_setup "$@" chroot "$ROOT" /root/install_scripts/user_setup "$@"
./cleanup mounts

View File

@ -1,30 +0,0 @@
#!/bin/sh
if test `id -u` -ne 0
then
echo "$0 must be run as root."
return
fi
ROOT=test_environment
cleanup_mount() {
if mountpoint "$1" > /dev/null
then
echo "Unmounting $1"
umount "$1"
fi
}
if test -e "$ROOT"
then
cleanup_mount "$ROOT/dev/pts"
cleanup_mount "$ROOT/dev"
cleanup_mount "$ROOT/sys"
cleanup_mount "$ROOT/proc"
echo "Removing $ROOT"
rm -r "$ROOT"
else
echo "No environment found at $ROOT. Doing nothing."
fi

View File

@ -15,6 +15,8 @@ else
./create_environment ./create_environment
fi fi
./mountpoints ./cleanup add_mounts
chroot "$ROOT" sudo -iu user "$@" chroot "$ROOT" sudo -iu user "$@"
./cleanup mounts