1
0
mirror of https://codeberg.org/kiss-community/kiss synced 2024-11-04 14:05:41 -07:00
kiss/contrib/kiss-chroot

89 lines
2.3 KiB
Plaintext
Raw Normal View History

2019-10-30 04:51:48 -06:00
#!/bin/sh -e
2020-05-01 00:07:20 -06:00
# Enter a kiss chroot
2019-10-30 04:51:48 -06:00
log() {
printf '\033[32m->\033[m %s.\n' "$*"
}
die() {
log "$*" >&2
exit 1
}
clean() {
log Unmounting host filesystems; {
umount "$1/sys/firmware/efi/efivars" 2>/dev/null ||:
umount "$1/tmp" ||:
umount "$1/run" ||:
umount "$1/dev/pts" ||:
umount "$1/dev/shm" ||:
2019-10-30 04:51:48 -06:00
umount "$1/dev" ||:
umount "$1/proc" ||:
umount "$1/sys" ||:
}
log Cleaning leftover host files; {
rm -f "$1/root/.ash_history"
2020-03-25 03:59:15 -06:00
rm -f "$1/etc/resolv.conf"
2019-10-30 04:51:48 -06:00
}
}
2020-05-15 00:58:16 -06:00
mounted() {
# This is a pure shell mountpoint implementation. We're dealing
# with basic (and fixed/known) input so this doesn't need to
# handle more complex cases.
[ -e "$1" ] || return 1
[ -e /proc/mounts ] || return 1
while read -r _ target _; do
[ "$target" = "$1" ] && return 0
done < /proc/mounts
printf 'mounting %s\n' "$1" >&2
2020-05-15 00:58:16 -06:00
return 1
}
set -- "${1%"${1##*[!/]}"}"
2020-04-18 03:27:38 -06:00
[ -z "$1" ] && die Need a path to the chroot
[ -d "$1" ] || die Given path does not exist
[ "$(id -u)" = 0 ] || die Script needs to be run as root
2019-10-30 04:51:48 -06:00
2020-04-18 03:27:38 -06:00
trap 'clean "$1"' EXIT INT
2019-10-30 04:51:48 -06:00
log Mounting host filesystems; {
mounted "$1/dev" || mount -o bind /dev "$1/dev" ||:
mounted "$1/proc" || mount -t proc proc "$1/proc" ||:
mounted "$1/sys" || mount -t sysfs sys "$1/sys" ||:
2019-10-30 04:51:48 -06:00
mounted "$1/dev/shm" ||
mount -t tmpfs shmfs "$1/dev/shm" ||:
mounted "$1/dev/pts" ||
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" ||:
2020-05-15 00:58:16 -06:00
mounted "$1/sys/firmware/efi/efivars" ||
mount -t efivarfs efivarfs "$1/sys/firmware/efi/efivars" 2>/dev/null ||:
2020-04-18 03:27:38 -06:00
}
2019-10-30 04:51:48 -06:00
2020-04-18 03:27:38 -06:00
log Copying /etc/resolv.conf from host; {
cp -f /etc/resolv.conf "$1/etc" ||:
2019-10-30 04:51:48 -06:00
}
2020-04-18 03:27:38 -06:00
log Entering chroot; {
chroot "$1" /usr/bin/env -i \
HOME=/root \
TERM="$TERM" \
SHELL=/bin/sh \
USER=root \
2020-06-13 12:14:58 -06:00
CFLAGS="${CFLAGS:--march=x86-64 -mtune=generic -pipe -Os}" \
CXXFLAGS="${CXXFLAGS:--march=x86-64 -mtune=generic -pipe -Os}" \
MAKEFLAGS="${MAKEFLAGS:--j$(nproc 2>/dev/null || echo 1)}" \
2020-04-18 03:27:38 -06:00
/bin/sh -l
}