2 # Enter a chroot nicely. Stolen from KISS Linux, MIT license Dylan Araps 2020.
3 # Some slight modifications to make life easier.
6 printf '\033[32m->\033[m %s.\n' "$*"
15 log Unmounting /dev, /proc and /sys from chroot; {
16 umount "$1/sys/firmware/efi/efivars" 2>/dev/null ||:
22 log Cleaning leftover host files; {
23 rm -f "$1/root/.ash_history"
24 rm -f "$1/etc/resolv.conf"
29 # This is a pure shell mountpoint implementation. We're dealing
30 # with basic (and fixed/known) input so this doesn't need to
31 # handle more complex cases.
32 [ -e "$1" ] || return 1
33 [ -e /proc/mounts ] || return 1
35 while read -r _ target _; do
36 [ "$target" = "$1" ] && return 0
42 [ -z "$1" ] && die Need a path to the chroot
43 [ -d "$1" ] || die Given path does not exist
44 [ "$(id -u)" = 0 ] || die Script needs to be run as root
46 trap 'clean "$1"' EXIT INT
48 log Mounting /dev, /proc and /sys from host; {
49 mounted "$1/dev" || mount -o bind /dev "$1/dev" ||:
50 mounted "$1/proc" || mount -t proc proc "$1/proc" ||:
51 mounted "$1/sys" || mount -t sysfs sys "$1/sys" ||:
53 mounted "$1/sys/firmware/efi/efivars" ||
54 mount -t efivarfs efivarfs "$1/sys/firmware/efi/efivars" 2>/dev/null ||:
57 log Copying /etc/resolv.conf from host; {
58 cp -f /etc/resolv.conf "$1/etc" ||:
61 NEWSHELL=${2:-"/bin/sh"} \
63 log Entering chroot; {
64 chroot "$1" /usr/bin/env -i \
69 CFLAGS="${CFLAGS:--march=x86-64 -mtune=generic -pipe -Os}" \
70 CXXFLAGS="${CXXFLAGS:--march=x86-64 -mtune=generic -pipe -Os}" \
71 MAKEFLAGS="${MAKEFLAGS:--j$(nproc 2>/dev/null || echo 1)}" \