3 ################################################################################
5 # Init script to boot over network through NBD
7 # Contact: Stéphane Desneux <stephane.desneux@iot.bzh>
9 ################################################################################
11 # banner generator: echo "AGL - Netboot" | figlet -f slant -w 80 -c
13 ________________________________________________________________________________
14 ___ ________ _ __ __ __ __
15 / | / ____/ / / | / /__ / /_/ /_ ____ ____ / /_
16 / /| |/ / __/ / ______ / |/ / _ \/ __/ __ \/ __ \/ __ \/ __/
17 / ___ / /_/ / /___ /_____/ / /| / __/ /_/ /_/ / /_/ / /_/ / /_
18 /_/ |_\____/_____/ /_/ |_/\___/\__/_.___/\____/\____/\__/
19 ________________________________________________________________________________
30 # -------------------------------------------
32 log_info() { echo "$0[$$]: $@" >&2; }
33 log_error() { echo "$0[$$]: ERROR $@" >&2; }
36 log_info "mounting FS: $@"
37 [[ -e /proc/filesystems ]] && { grep -q "$1" /proc/filesystems || { log_error "Unknown filesystem"; return 1; } }
38 [[ -d "$2" ]] || mkdir -p "$2"
39 [[ -e /proc/mounts ]] && { grep -q -e "^$1 $2 $1" /proc/mounts && { log_info "$2 ($1) already mounted"; return 0; } }
40 mount -t "$1" "$1" "$2"
45 check_debug "Reboot will occur after exiting this shell."
46 log_info "Rebooting..."
59 # -------------------------------------------
61 export PATH=/sbin:/usr/sbin:/bin:/usr/bin
63 log_info "starting initrd script"
65 do_mount_fs proc /proc
66 do_mount_fs sysfs /sys
67 do_mount_fs devtmpfs /dev
68 do_mount_fs devpts /dev/pts
69 do_mount_fs tmpfs /dev/shm
70 do_mount_fs tmpfs /tmp
71 do_mount_fs tmpfs /run
73 # parse kernel commandline to get NBD server
74 for x in $(cat /proc/cmdline); do
76 nbd.server=*) NBD_SERVER=${x/*=/};;
77 nbd.port=*) NBD_PORT=${x/*=/};;
78 nbd.dev=*) NBD_DEV=/dev/${x/*=/};;
79 nbd.debug=*) DEBUG=${x/*=/};;
83 check_debug "Debug point 1. Exit to continue initrd script (mount NBD device)."
85 log_info "NBD parameters: device $NBD_DEV, server $NBD_SERVER:$NBD_PORT"
87 # check if smack is active (and if so, mount smackfs)
88 grep -q smackfs /proc/filesystems && {
91 do_mount_fs smackfs /sys/fs/smackfs
93 # adjust current label and network label
94 echo System >/proc/self/attr/current
95 echo System >/sys/fs/smackfs/ambient
101 log_info "Starting NBD client"
102 nbd-client $NBD_SERVER $NBD_PORT $NBD_DEV && { log_info "NBD client successfully started"; break; }
103 log_info "NBD client failed"
104 [[ $try -gt 0 ]] && { log_info "Retrying ($try trie(s) left)..."; sleep 3; try=$(( try - 1 )); continue; }
106 bail_out "Unable to mount NBD device $NBD_DEV using server $NBD_SERVER:$NBD_PORT"
111 mount $NBD_DEV -o noatime /sysroot || bail_out "Unable to mount root NBD device"
113 # move mounted devices to new root
115 for x in dev proc sys tmp run; do
116 log_info "Moving /$x to new rootfs"
120 # switch to new rootfs
121 log_info "Switching to new rootfs"
122 mkdir -p run/initramfs
123 pivot_root . run/initramfs || bail_out "pivot_root failed."
125 # workaround for connman (avoid bringing down the network interface used for booting, disable DNS proxy)
126 if [[ -f /lib/systemd/system/connman.service ]]; then
127 log_info "Adjusting Connman configuration"
128 iface=$(ip -o link show up | tr ':' ' ' | awk '{print $2}' | grep -v -e "^lo$" | head -1)
129 sed -i "s|connmand -n\$|connmand -r -n -I $iface|g" /lib/systemd/system/connman.service
132 # also use /proc/net/pnp to generate /etc/resolv.conf
133 grep -v bootserver /proc/net/pnp | sed 's/^domain/search/g' >/etc/resolv.conf
135 # finally, run systemd
136 check_debug "Debug point 2. Exit to continue initrd script (run systemd)."
138 log_info "Exec'ing systemd"
139 # banner generator: echo "AGL Booting . . ." | figlet -f slant -w 80 -c
141 ________________________________________________________________________________
142 ___ ________ ____ __ _
143 / | / ____/ / / __ )____ ____ / /_(_)___ ____ _
144 / /| |/ / __/ / / __ / __ \/ __ \/ __/ / __ \/ __ `/
145 / ___ / /_/ / /___ / /_/ / /_/ / /_/ / /_/ / / / / /_/ / _ _ _
146 /_/ |_\____/_____/ /_____/\____/\____/\__/_/_/ /_/\__, / (_) (_) (_)
147 _____________________________________________________/____/_____________________
150 exec /lib/systemd/systemd </dev/console >/dev/console 2>&1