diff --git a/README.md b/README.md index eea29ef..8075354 100644 --- a/README.md +++ b/README.md @@ -71,26 +71,18 @@ or Destroy Environment ------------------- -Because chroot environments require multiple mounted directories to work, you -can't simply `rm -r` a chroot environment you created, or the repository as a -whole, without unmounting the mount points first. If you have rebooted your -machine since setting up the chroot environments, then you don't need to worry, -part or all of the repository can be straight-forwardly deleted, but if you are -working with the repository and want to delete something yourself, there are -two helper scripts that can be used to clean up the mount points and chroot -environments properly. +The `create_environment` and `enter_environment` scripts try to clean up the +mounts that they create, and the mounts will all disappear on reboot, but just +in case they are still present, you can run `sudo ./cleanup` to delete the +`test_environment` safely. If you want to delete both environments and +`debootstrap` in one go, then `sudo ./cleanup everything` will safely unmount +`test_environment` and then delete all three directories. -First `sudo ./destroy_environment` will unmount and delete `test_environment`, -allowing you to remove an old environment without immediately creating a new -one. Anything else in the repository can be straight-forwardly deleted with -`sudo rm -r`, so with this you can put the repository in whatever state you -want it to be in. - -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. +`cleanup` has other options too. For example, if you want to chroot into the +environment as root, you can manually add the mount points back using +`sudo ./cleanup add_mounts`, and then chroot in yourself. There is also +`sudo ./cleanup mounts` to remove the mounts manually without deleting +`test_environment`. Reuse an Existing Environment ----------------------------- diff --git a/debian/cleanup b/debian/cleanup new file mode 100755 index 0000000..7aad62e --- /dev/null +++ b/debian/cleanup @@ -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 + diff --git a/debian/cleanup_everything b/debian/cleanup_everything deleted file mode 100755 index 0cbb542..0000000 --- a/debian/cleanup_everything +++ /dev/null @@ -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 - diff --git a/debian/create_environment b/debian/create_environment index cef9233..6ef2cbe 100755 --- a/debian/create_environment +++ b/debian/create_environment @@ -20,7 +20,7 @@ fi if test -e "$ROOT" then echo "Existing installation found at $ROOT, removing." - ./destroy_environment + ./cleanup fi echo "Copying $FRESH to $ROOT." @@ -28,7 +28,7 @@ cp -r "$FRESH" "$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 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" chroot "$ROOT" /root/install_scripts/user_setup "$@" + +./cleanup mounts diff --git a/debian/destroy_environment b/debian/destroy_environment deleted file mode 100755 index 00eb008..0000000 --- a/debian/destroy_environment +++ /dev/null @@ -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 - diff --git a/debian/enter_environment b/debian/enter_environment index eeabf68..81bc884 100755 --- a/debian/enter_environment +++ b/debian/enter_environment @@ -15,6 +15,8 @@ else ./create_environment fi -./mountpoints +./cleanup add_mounts chroot "$ROOT" sudo -iu user "$@" + +./cleanup mounts