aboutsummaryrefslogtreecommitdiff
path: root/init
diff options
context:
space:
mode:
Diffstat (limited to 'init')
-rwxr-xr-xinit115
1 files changed, 87 insertions, 28 deletions
diff --git a/init b/init
index bc1452c..f7bd6f5 100755
--- a/init
+++ b/init
@@ -40,7 +40,15 @@ mount -t proc -o nodev,noexec,nosuid proc /proc
info "init starting up..."
-mount -t devtmpfs -o noexec,nosuid,mode=0755 udev /dev
+mount -t devtmpfs -o nosuid,mode=0755 udev /dev
+
+# Prepare the /dev directory.
+#
+ln -s /proc/self/fd /dev/fd
+ln -s /proc/self/fd/0 /dev/stdin
+ln -s /proc/self/fd/1 /dev/stdout
+ln -s /proc/self/fd/2 /dev/stderr
+
mkdir -p /dev/pts
mount -t devpts -o noexec,nosuid,gid=5,mode=0620 devpts /dev/pts || true
@@ -70,13 +78,23 @@ udevadm trigger --type=subsystems --action=add
udevadm trigger --type=devices --action=add
udevadm settle || true
+# On 6-series kernels we seem to be executed a lot earlier (or a lot faster)
+# with many devices (Ethernet, USB storage) not being discovered yet (and
+# devices that require firmware generally taking a while). So let's wait a
+# bit for things to settle down.
+#
+for s in 5 4 3 2 1; do
+ info "waiting for devices ${s}s..."
+ sleep 1
+done
+
# Detect hardware sensors.
#
sensors-detect --auto
# Initialize KVM.
#
-#if ! (modprobe kvm_intel || modprobe kvm_amd); then
+#if ! (/sbin/modprobe kvm_intel || /sbin/modprobe kvm_amd); then
# error "no virtualization support available (is it disabled in BIOS?)"
#fi
@@ -86,6 +104,11 @@ sensors-detect --auto
# foo='foo fox'
# bar="bar 'box'"
#
+# Or (as rewritten by GRUB):
+#
+# 'foo=foo fox'
+# "bar=bar 'box'"
+#
# First we separete quoted variables and arguments with newlines (giving
# priority to assignments). Then we replace whitespaces with newline on
# lines that don't contain quotes. Finally, clean up by removing blank
@@ -102,11 +125,24 @@ readarray -t cmdline < <(cat /proc/cmdline | \
#
info "command line:"
for v in "${cmdline[@]}"; do
- var="$(sed -n -re 's/^buildos\.([^=]+)=.*$/\1/p' <<<"$v")" # Extract name.
+
+ # Rewrite "x=y" as x="y" (as well as the single-quote variant).
+ #
+ v1="$(sed -n -re "s/^\"([^= ]+)=(.*)\"\$/\1=\"\2\"/p" <<<"$v")"
+ if [ -n "$v1" ]; then
+ v="$v1"
+ else
+ v1="$(sed -n -re "s/^'([^= ]+)=(.*)'\$/\1='\2'/p" <<<"$v")"
+ if [ -n "$v1" ]; then
+ v="$v1"
+ fi
+ fi
+
+ var="$(sed -n -re 's/^buildos\.([^= ]+)=.*$/\1/p' <<<"$v")" # Extract name.
if [ -n "$var" ]; then
- val="$(sed -re 's/^[^=]+=(.*)$/\1/' <<<"$v")" # Extract value.
- val="$(sed -re "s/^('(.*)'|\"(.*)\")$/\2\3/" <<<"$val")" # Strip quoted.
+ val="$(sed -re 's/^[^= ]+=(.*)$/\1/' <<<"$v")" # Extract value.
+ val="$(sed -re "s/^('(.*)'|\"(.*)\")\$/\2\3/" <<<"$val")" # Strip quoted.
info " $var=$val"
# If the variable contains a dot, then it is a toolchain variable and we
@@ -269,6 +305,7 @@ iface br1 inet static
bridge_maxwait 0
bridge_fd 0
bridge_hw $lmac
+ #post-up ip link set br1 address $lmac
post-up ip link set br1 txqueuelen 4000
post-up iptables -t nat -A POSTROUTING -o br0 -j MASQUERADE
post-up iptables -A FORWARD -i br0 -o br1 -m state --state RELATED,ESTABLISHED -j ACCEPT
@@ -290,7 +327,8 @@ echo -n '' >$fstab
l=
state=
-machines=
+declare -A machines
+machines_mode=
while read l || [ -n "$l" ]; do
d="$(sed -re 's/.*NAME=\"([^\"]+)\".*/\1/' <<<"$l")"
t="$(sed -re 's/.*FSTYPE=\"([^\"]*)\".*/\1/' <<<"$l")"
@@ -349,42 +387,29 @@ while read l || [ -n "$l" ]; do
if [ "$l" = "machines" ]; then
# Single mount.
#
- if [ -n "$machines" ]; then
+ if [ "$machines_mode" = "multiple" ]; then
error "multiple disks labeled with buildos.machines/machines.*"
fi
m=/build/machines/default
- machines="single"
+ machines["$m"]="${machines["$m"]} $d"
+
+ machines_mode="single"
else
# Multiple mounts.
#
- if [ "$machines" = "single" ]; then
+ if [ "$machines_mode" = "single" ]; then
error "multiple disks labeled with buildos.machines/machines.*"
fi
n="$(sed -n -re 's/^machines\.([^ ]+)$/\1/p' <<<"$l")"
m="/build/machines/$n"
- machines="multiple"
- fi
+ machines["$m"]="${machines["$m"]} $d"
- info "mounting $d (buildos.$l) on $m"
-
- # Check it.
- #
- if ! btrfs check -p "$d"; then
- info "$d (buildos.$l) has errors; run btrfs check -p --repair $d"
- error
+ machines_mode="multiple"
fi
- o="defaults,noatime,nodiratime,user_subvol_rm_allowed"
- echo "$d $m btrfs $o 0 0" >>$fstab
-
- # Mount it and change the owner of the filesystem root.
- #
- mkdir -p "$m"
- mount -t btrfs -o "$o" "$d" "$m"
- chown build:build "$m"
-
+ info "will be mounting $d (buildos.$l) on $m"
continue
fi
done < <(lsblk --pairs --paths --output NAME,FSTYPE,LABEL)
@@ -400,13 +425,47 @@ if [ -z "$state" ]; then
error
fi
-if [ -z "$machines" ]; then
+if [ "${#machines[@]}" -eq 0 ]; then
info "no disks labaled with buildos.machines* among:"
lsblk --paths --output NAME,TYPE,FSTYPE,SIZE,LABEL,UUID
info "consider formatting and/or labelling a suitable disk"
error
fi
+for m in "${!machines[@]}"; do
+
+ ds="${machines["$m"]}" # Array-like list of devices.
+
+ info "mounting $ds on $m"
+
+ # Check the devices and collect them in options.
+ #
+ fd=
+ o=
+ for d in $ds; do
+ if ! btrfs check -p "$d"; then
+ info "$d has errors; run btrfs check -p --repair $d"
+ error
+ fi
+
+ if [ -z "$fd" ]; then
+ fd="$d"
+ o="device=$d"
+ else
+ o="$o,device=$d"
+ fi
+ done
+
+ o="$o,defaults,noatime,nodiratime,user_subvol_rm_allowed"
+ echo "$fd $m btrfs $o 0 0" >>$fstab
+
+ # Mount it and change the owner of the filesystem root.
+ #
+ mkdir -p "$m"
+ mount -t btrfs -o "$o" "$fd" "$m"
+ chown build:build "$m"
+done
+
# Create /build/tftp. We make it a size-limited tmpfs since potentially-
# compromized VMs will be able to upload to.
#