commit 9887e9f

chld  ·  2025-12-20 17:20:19 +0000 UTC
parent 3f18bf8
util/ + setup-rootfs
2 files changed,  +81, -0
+1, -0
1@@ -7,3 +7,4 @@ wget \
2 	-O kiss.tar.xz
3 
4 tar -xf kiss.tar.xz -C /mnt
5+sh util/kiss-mount /mnt
+80, -0
 1@@ -0,0 +1,80 @@
 2+#!/bin/sh
 3+# Enter a kiss chroot
 4+
 5+log() {
 6+    printf '\033[32m->\033[m %s.\n' "$*"
 7+}
 8+
 9+die() {
10+    log "$*" >&2
11+    exit 1
12+}
13+
14+run() {
15+    printf '%s\n' "$*"
16+    "$@" || return "${_ret:=0}"
17+}
18+
19+clean() {
20+    log Unmounting host paths; {
21+        run umount "$1/dev/shm" 2>/dev/null
22+        run umount "$1/dev/pts"
23+        run umount "$1/dev"
24+        run umount "$1/proc"
25+        run umount "$1/run"
26+        run umount "$1/sys/firmware/efi/efivars" 2>/dev/null
27+        run umount "$1/sys"
28+        run umount "$1/tmp"
29+        run umount "$1/etc/resolv.conf"
30+    }
31+}
32+
33+mounted() {
34+    # This is a pure shell mountpoint implementation. We're dealing
35+    # with basic (and fixed/known) input so this doesn't need to
36+    # handle more complex cases.
37+    [ -e "$1" ]         || return 1
38+    [ -e /proc/mounts ] || return 1
39+
40+    while read -r _ target _; do
41+        [ "$target" = "$1" ] && return 0
42+    done < /proc/mounts
43+
44+    return 1
45+}
46+
47+mmount() {
48+    dest=$1
49+    shift
50+    mounted "$dest" || run mount "$@" "$dest"
51+}
52+
53+main() {
54+    # Ensure input does not end in '/'.
55+    set -- "${1%"${1##*[!/]}"}"
56+
57+    [ "$1" ]           || die Need a path to the chroot
58+    [ -d "$1" ]        || die Given path does not exist
59+    [ "$(id -u)" = 0 ] || die Script needs to be run as root
60+
61+    # Intended behaviour.
62+    # shellcheck disable=SC2064
63+    trap "clean ${1%"${1##*[!/]}"}" EXIT INT
64+
65+    log Mounting host paths; {
66+        mmount "$1/dev"     -o bind /dev
67+        mmount "$1/dev/pts" -o bind /dev/pts
68+        mmount "$1/dev/shm" -t tmpfs shmfs 2>/dev/null
69+        mmount "$1/proc"    -t proc  proc
70+        mmount "$1/run"     -t tmpfs tmpfs
71+        mmount "$1/sys"     -t sysfs sys
72+        mmount "$1/sys/firmware/efi/efivars" -t efivarfs efivarfs 2>/dev/null
73+        mmount "$1/tmp"     -o mode=1777,nosuid,nodev -t tmpfs tmpfs
74+
75+        touch  "$1/etc/resolv.conf"
76+        mmount "$1/etc/resolv.conf" -o bind /etc/resolv.conf
77+    }
78+
79+}
80+
81+main "$1"