chroot: do better bookkeeping of mounted devices

This was brought up when a user tried to arch-chroot into the same
root twice from different terminals. The second chroot should fail, but
not tear down the tree that it doesn't own.
This commit is contained in:
Dave Reisner 2012-12-16 12:07:20 -05:00
parent 92fdaa397b
commit 517fcff074
2 changed files with 15 additions and 23 deletions

View File

@ -29,8 +29,6 @@ shift
trap '{ api_fs_umount "$chrootdir"; umount "$chrootdir/etc/resolv.conf"; } 2>/dev/null' EXIT
api_fs_mount "$chrootdir" || die "failed to setup API filesystems in chroot %s" "$chrootdir"
mount -B /etc/resolv.conf "$chrootdir/etc/resolv.conf"
track_mount /etc/resolv.conf "$chrootdir/etc/resolv.conf" --bind
SHELL=/bin/sh chroot "$chrootdir" "$@"
umount "$chrootdir/etc/resolv.conf"

34
common
View File

@ -11,30 +11,24 @@ in_array() {
done
}
track_mount() {
mount "$@" && CHROOT_ACTIVE_MOUNTS=("$2" "${CHROOT_ACTIVE_MOUNTS[@]}")
}
api_fs_mount() {
if ! mountpoint -q "$1"; then
mount -B "$1" "$1" && ROOT_IS_BIND=1
fi
mount -t proc proc "$1/proc" -o nosuid,noexec,nodev &&
mount -t sysfs sys "$1/sys" -o nosuid,noexec,nodev &&
mount -t devtmpfs udev "$1/dev" -o mode=0755,nosuid &&
mount -t devpts devpts "$1/dev/pts" -o mode=0620,gid=5,nosuid,noexec &&
mount -t tmpfs shm "$1/dev/shm" -o mode=1777,nosuid,nodev &&
mount -t tmpfs run "$1/run" -o nosuid,nodev,mode=0755 &&
mount -t tmpfs tmp "$1/tmp" -o mode=1777,strictatime,nodev,nosuid
CHROOT_ACTIVE_MOUNTS=()
{ mountpoint -q "$1" || track_mount "$1" "$1" --bind; } &&
track_mount proc "$1/proc" -t proc -o nosuid,noexec,nodev &&
track_mount sys "$1/sys" -t sysfs -o nosuid,noexec,nodev &&
track_mount udev "$1/dev" -t devtmpfs -o mode=0755,nosuid &&
track_mount devpts "$1/dev/pts" -t devpts -o mode=0620,gid=5,nosuid,noexec &&
track_mount shm "$1/dev/shm" -t tmpfs -o mode=1777,nosuid,nodev &&
track_mount run "$1/run" -t tmpfs -o nosuid,nodev,mode=0755 &&
track_mount tmp "$1/tmp" -t tmpfs -o mode=1777,strictatime,nodev,nosuid
}
api_fs_umount() {
umount \
"$1/tmp" \
"$1/run" \
"$1/dev/shm" \
"$1/dev/pts" \
"$1/dev" \
"$1/sys" \
"$1/proc"
(( ROOT_IS_BIND )) && umount "$1"
umount "${CHROOT_ACTIVE_MOUNTS[@]}"
}
valid_number_of_base() {