system

the Chamesle handbook, init, live ISO and install helpers
git clone https://github.com/tortr-rs/system
Log | Files | Refs | README | LICENSE

init (2138B)


      1 #!/bin/sh
      2 # the first process in the initramfs. mounts the medium or the root named on the command line and hands over.
      3 export PATH=/bin
      4 mount -t proc proc /proc
      5 mount -t sysfs sysfs /sys
      6 mount -t devtmpfs devtmpfs /dev
      7 
      8 while read -r ko; do insmod "$ko" 2>/dev/null; done < /modules
      9 
     10 read -r cmdline < /proc/cmdline
     11 for w in $cmdline; do
     12 	case $w in
     13 	root=UUID=*) uuid=${w#root=UUID=};;
     14 	root=ZFS=*) zfs=${w#root=ZFS=};;
     15 	root=*) dev=${w#root=};;
     16 	esac
     17 done
     18 
     19 fail() { echo "init: $*"; echo "init: dropping to a shell"; exec sh; }
     20 
     21 mkdir -p /mnt/root
     22 
     23 if [ -n "$zfs" ]; then
     24 	while read -r ko; do insmod "$ko" || fail "cannot load $ko"; done < /modules.zfs
     25 	n=0
     26 	until zpool import -N -f -d /dev "${zfs%%/*}" 2>/dev/null; do
     27 		n=$((n + 1))
     28 		[ $n -lt 15 ] || fail "cannot import the pool ${zfs%%/*}"
     29 		sleep 1
     30 	done
     31 	mount -o zfsutil -t zfs "$zfs" /mnt/root || fail "cannot mount $zfs"
     32 elif [ -n "$uuid$dev" ]; then
     33 	n=0
     34 	while [ $n -lt 15 ]; do
     35 		[ -n "$uuid" ] && dev=$(blkid -U "$uuid" 2>/dev/null)
     36 		[ -b "$dev" ] && break
     37 		sleep 1; n=$((n + 1))
     38 	done
     39 	[ -b "$dev" ] || fail "root device not found"
     40 	mount -o ro "$dev" /mnt/root || fail "cannot mount $dev"
     41 else
     42 	mkdir -p /mnt/medium /mnt/lower /mnt/rw
     43 	n=0
     44 	found=
     45 	while [ -z "$found" ] && [ $n -lt 20 ]; do
     46 		for d in /dev/sr* /dev/sd* /dev/nvme*n*p* /dev/vd* /dev/mmcblk*; do
     47 			[ -b "$d" ] || continue
     48 			mount -o ro "$d" /mnt/medium 2>/dev/null || continue
     49 			[ -f /mnt/medium/LiveOS/rootfs.sfs ] && { found=$d; break; }
     50 			umount /mnt/medium
     51 		done
     52 		[ -n "$found" ] || { sleep 1; n=$((n + 1)); }
     53 	done
     54 	[ -n "$found" ] || fail "no medium with LiveOS/rootfs.sfs"
     55 	mount -t squashfs -o loop,ro /mnt/medium/LiveOS/rootfs.sfs /mnt/lower || fail "cannot mount the squashfs"
     56 	mount -t tmpfs tmpfs /mnt/rw
     57 	mkdir -p /mnt/rw/up /mnt/rw/work
     58 	mount -t overlay overlay -o lowerdir=/mnt/lower,upperdir=/mnt/rw/up,workdir=/mnt/rw/work /mnt/root || fail "cannot mount the overlay"
     59 	mkdir -p /mnt/root/run/live/medium /mnt/root/run/live/rootfs
     60 	mount --move /mnt/medium /mnt/root/run/live/medium
     61 	mount --move /mnt/lower /mnt/root/run/live/rootfs
     62 fi
     63 
     64 exec switch_root /mnt/root /sbin/init