2b8bc63d0b7f094460f34247f9d7abb53c7c4559
[AGL/meta-agl.git] / meta-netboot / recipes-core / initramfs-netboot / files / init.sh
1 #!/bin/sh
2
3 ################################################################################
4 #
5 # Init script to boot over network through NBD
6 #
7 # Contact: Stéphane Desneux <stephane.desneux@iot.bzh>
8 #
9 ################################################################################
10
11 # banner generator: echo "AGL - Netboot" | figlet -f slant -w 80 -c
12 cat <<'EOF' >&2
13 ________________________________________________________________________________
14          ___   ________                _   __     __  __                __
15         /   | / ____/ /               / | / /__  / /_/ /_  ____  ____  / /_
16        / /| |/ / __/ /      ______   /  |/ / _ \/ __/ __ \/ __ \/ __ \/ __/
17       / ___ / /_/ / /___   /_____/  / /|  /  __/ /_/ /_/ / /_/ / /_/ / /_
18      /_/  |_\____/_____/           /_/ |_/\___/\__/_.___/\____/\____/\__/
19 ________________________________________________________________________________
20 EOF
21
22 # global variables
23
24 SMACK=n
25 NBD_SERVER=
26 NBD_PORT=10809
27 NBD_DEV=/dev/nbd0
28 NBD_NAMEV3=
29 DEBUG=n
30
31 # -------------------------------------------
32
33 log_info() { echo "$0[$$]: $@" >&2; }
34 log_error() { echo "$0[$$]: ERROR $@" >&2; }
35
36 do_mount_fs() {
37         log_info "mounting FS: $@"
38         [[ -e /proc/filesystems ]] && { grep -q "$1" /proc/filesystems || { log_error "Unknown filesystem"; return 1; } }
39         [[ -d "$2" ]] || mkdir -p "$2"
40         [[ -e /proc/mounts ]] && { grep -q -e "^$1 $2 $1" /proc/mounts && { log_info "$2 ($1) already mounted"; return 0; } }
41         mount -t "$1" "$1" "$2"
42 }
43
44 bail_out() {
45         log_error "$@"
46         check_debug "Reboot will occur after exiting this shell."
47         log_info "Rebooting..."
48         exec reboot -f
49 }
50
51 check_debug() {
52         case $DEBUG in
53                 Y|y|yes|1|true)
54                         log_info "$@"
55                         /bin/sh -i
56                         ;;
57         esac
58 }
59
60 find_active_interface() {
61         [[ ! -d /sys/class/net ]] && { log_error "find_active_interface: /sys/class/net doesn't exist"; return 2; }
62         local iface
63         for x in $(ls -d /sys/class/net/* 2>/dev/null); do
64                 iface=$(basename $x)
65                 # find interfaces with:
66                 # - type == 1 (ethernet)
67                 # - not wireless
68                 # - with state up
69
70                 [[ $(cat $x/type) != 1 ]] && continue
71                 [[ -d $x/wireless ]] && continue
72                 [[ $(cat $x/operstate) != "up" ]] && continue
73
74                 log_info "find_active_interface: first active interface is $iface"
75                 echo $iface
76                 return 0
77         done
78
79         log_error "Unable to find any active network interface."
80         return 1
81 }
82
83 # -------------------------------------------
84
85 export PATH=/sbin:/usr/sbin:/bin:/usr/bin
86
87 log_info "starting initrd script"
88
89 do_mount_fs proc /proc
90 do_mount_fs sysfs /sys
91 do_mount_fs devtmpfs /dev
92 do_mount_fs devpts /dev/pts
93 do_mount_fs tmpfs /dev/shm
94 do_mount_fs tmpfs /tmp
95 do_mount_fs tmpfs /run
96
97 # parse kernel commandline to get NBD server
98 for x in $(cat /proc/cmdline); do
99         case $x in
100                 nbd.server=*) NBD_SERVER=${x/*=/};;
101                 nbd.port=*) NBD_PORT=${x/*=/};;
102                 nbd.dev=*)  NBD_DEV=/dev/${x/*=/};;
103                 nbd.namev3=*) NBD_NAMEV3=${x/*=/};;
104                 nbd.debug=*) DEBUG=${x/*=/};;
105         esac
106 done
107
108 check_debug "Debug point 1. Exit to continue initrd script (mount NBD device)."
109
110 log_info "NBD parameters: device $NBD_DEV, server $NBD_SERVER:$NBD_PORT"
111
112 # check if smack is active (and if so, mount smackfs)
113 grep -q smackfs /proc/filesystems && {
114         SMACK=y
115
116         do_mount_fs smackfs /sys/fs/smackfs
117
118         # adjust current label and network label
119         echo System >/proc/self/attr/current
120         echo System >/sys/fs/smackfs/ambient
121 }
122
123 # start nbd client
124 try=5
125 while :;do
126         log_info "Starting NBD client"
127         if [ -z "${NBD_NAMEV3}" ]; then
128                 nbd-client -persist $NBD_SERVER $NBD_PORT $NBD_DEV && { log_info "NBD client successfully started"; break; }
129                 log_info "NBD client failed"
130         else
131                 nbd3-client $NBD_SERVER $NBD_DEV --name $NBD_NAMEV3 && { log_info "NBD3 client successfully started"; break; }
132                 log_info "NBDv3 client failed"
133         fi
134         [[ $try -gt 0 ]] && { log_info "Retrying ($try trie(s) left)..."; sleep 3; try=$(( try - 1 )); continue; }
135
136         bail_out "Unable to mount NBD device $NBD_DEV using server $NBD_SERVER:$NBD_PORT"
137 done
138
139 # mount NBD device
140 mkdir -p /sysroot
141 mount $NBD_DEV -o noatime /sysroot || bail_out "Unable to mount root NBD device"
142
143 # move mounted devices to new root
144 cd /sysroot
145 for x in dev proc sys tmp run; do
146         log_info "Moving /$x to new rootfs"
147         mount -o move /$x $x
148 done
149
150 # switch to new rootfs
151 log_info "Switching to new rootfs"
152 mkdir -p boot/initramfs
153 pivot_root . boot/initramfs || bail_out "pivot_root failed."
154
155 # workaround for connman (avoid bringing down the network interface used for booting, disable DNS proxy)
156 if [[ -f /lib/systemd/system/connman.service ]]; then
157         newopts="-r -n"
158         iface=$(find_active_interface)
159         [[ -n "$iface" ]] && newopts="$newopts -I $iface"
160
161         log_info "Adjusting Connman command line. Will be: 'connmand $newopts'"
162         sed -i "s|connmand -n\$|connmand $newopts|g" /lib/systemd/system/connman.service
163 fi
164
165 # also use /proc/net/pnp to generate /etc/resolv.conf
166 rm -f /etc/resolv.conf
167 grep -v bootserver /proc/net/pnp | sed 's/^domain/search/g' >/etc/resolv.conf
168 chsmack -A /etc/resolv.conf
169
170 # unmount tmp and run to let systemd remount them with correct smack labels (SPEC-2596)
171 log_info "Unmounting /tmp and /run"
172 umount /tmp
173 umount /run
174
175 # finally, run systemd
176 check_debug "Debug point 2. Exit to continue initrd script (run systemd)."
177
178 log_info "Exec'ing systemd"
179 # banner generator: echo "AGL Booting . . ." | figlet -f slant -w 80 -c
180 cat <<'EOF' >&2
181 ________________________________________________________________________________
182       ___   ________       ____              __  _
183      /   | / ____/ /      / __ )____  ____  / /_(_)___  ____ _
184     / /| |/ / __/ /      / __  / __ \/ __ \/ __/ / __ \/ __ `/
185    / ___ / /_/ / /___   / /_/ / /_/ / /_/ / /_/ / / / / /_/ /   _     _     _
186   /_/  |_\____/_____/  /_____/\____/\____/\__/_/_/ /_/\__, /   (_)   (_)   (_)
187 _____________________________________________________/____/_____________________
188 EOF
189
190 exec /lib/systemd/systemd </dev/console >/dev/console 2>&1
191 bail_out
192