kiss-chroot: improvements

- now displays exact commands which are executed on enter/leave.
- cleaned up code.
This commit is contained in:
Dylan Araps 2021-07-03 14:13:07 +00:00
parent b6c5a245bd
commit 15e7621d9f
No known key found for this signature in database
GPG Key ID: 13295DAC2CF13B5C

View File

@ -10,21 +10,25 @@ die() {
exit 1 exit 1
} }
run() {
printf '%s\n' "$*"
"$@" || return "${_ret:=0}"
}
clean() { clean() {
log Unmounting host filesystems; { log Unmounting host filesystems; {
umount "$1/sys/firmware/efi/efivars" 2>/dev/null ||: run umount "$1/dev/shm"
umount "$1/tmp" ||: run umount "$1/dev/pts"
umount "$1/run" ||: run umount "$1/dev"
umount "$1/dev/pts" ||: run umount "$1/proc"
umount "$1/dev/shm" ||: run umount "$1/run"
umount "$1/dev" ||: run umount "$1/sys/firmware/efi/efivars" 2>/dev/null
umount "$1/proc" ||: run umount "$1/sys"
umount "$1/sys" ||: run umount "$1/tmp"
} }
log Cleaning leftover host files; { log Cleaning leftover host files; {
rm -f "$1/root/.ash_history" run rm -f "$1/etc/resolv.conf"
rm -f "$1/etc/resolv.conf"
} }
} }
@ -39,44 +43,44 @@ mounted() {
[ "$target" = "$1" ] && return 0 [ "$target" = "$1" ] && return 0
done < /proc/mounts done < /proc/mounts
printf 'mounting %s\n' "$1" >&2
return 1 return 1
} }
mmount() {
dest=$1
shift
mounted "$dest" || run mount "$@" "$dest"
}
main() {
# Ensure input does not end in '/'.
set -- "${1%"${1##*[!/]}"}" set -- "${1%"${1##*[!/]}"}"
[ -z "$1" ] && die Need a path to the chroot
[ "$1" ] || die Need a path to the chroot
[ -d "$1" ] || die Given path does not exist [ -d "$1" ] || die Given path does not exist
[ "$(id -u)" = 0 ] || die Script needs to be run as root [ "$(id -u)" = 0 ] || die Script needs to be run as root
trap 'clean "$1"' EXIT INT trap 'clean "${1%"${1##*[!/]}"}"' EXIT INT
log Mounting host filesystems; { log Mounting host filesystems; {
mounted "$1/dev" || mount -o bind /dev "$1/dev" ||: mmount "$1/dev" -o bind /dev
mounted "$1/proc" || mount -t proc proc "$1/proc" ||: mmount "$1/dev/pts" -o bind /dev/pts
mounted "$1/sys" || mount -t sysfs sys "$1/sys" ||: mmount "$1/dev/shm" -t tmpfs shmfs
mmount "$1/proc" -t proc proc
mounted "$1/dev/shm" || mmount "$1/run" -t tmpfs tmpfs
mount -t tmpfs shmfs "$1/dev/shm" ||: mmount "$1/sys" -t sysfs sys
mmount "$1/sys/firmware/efi/efivars" -t efivarfs efivarfs 2>/dev/null
mounted "$1/dev/pts" || mmount "$1/tmp" -o mode=1777,nosuid,nodev -t tmpfs tmpfs
mount -o bind /dev/pts "$1/dev/pts" ||:
mounted "$1/tmp" ||
mount -o mode=1777,nosuid,nodev -t tmpfs tmpfs "$1/tmp" ||:
mounted "$1/run" ||
mount -t tmpfs tmpfs "$1/run" ||:
mounted "$1/sys/firmware/efi/efivars" ||
mount -t efivarfs efivarfs "$1/sys/firmware/efi/efivars" 2>/dev/null ||:
} }
log Copying /etc/resolv.conf from host; { log Copying /etc/resolv.conf from host; {
cp -f /etc/resolv.conf "$1/etc" ||: run cp -f /etc/resolv.conf "$1/etc"
} }
log Entering chroot; { log Entering chroot; {
chroot "$1" /usr/bin/env -i \ _ret=1
run chroot "$1" /usr/bin/env -i \
HOME=/root \ HOME=/root \
TERM="$TERM" \ TERM="$TERM" \
SHELL=/bin/sh \ SHELL=/bin/sh \
@ -86,3 +90,6 @@ log Entering chroot; {
MAKEFLAGS="${MAKEFLAGS:--j$(nproc 2>/dev/null || echo 1)}" \ MAKEFLAGS="${MAKEFLAGS:--j$(nproc 2>/dev/null || echo 1)}" \
/bin/sh -l /bin/sh -l
} }
}
main "$1"