agl-selinux: pull in meta-clang
[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 NBD_SERVER=
25 NBD_PORT=10809
26 NBD_DEV=/dev/nbd0
27 NBD_NAMEV3=
28 DEBUG=n
29
30 # -------------------------------------------
31
32 log_info() { echo "$0[$$]: $@" >&2; }
33 log_error() { echo "$0[$$]: ERROR $@" >&2; }
34
35 do_mount_fs() {
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"
41 }
42
43 bail_out() {
44         log_error "$@"
45         check_debug "Reboot will occur after exiting this shell."
46         log_info "Rebooting..."
47         exec reboot -f
48 }
49
50 check_debug() {
51         case $DEBUG in
52                 Y|y|yes|1|true)
53                         log_info "$@"
54                         /bin/sh -i
55                         ;;
56         esac
57 }
58
59 find_active_interface() {
60         [[ ! -d /sys/class/net ]] && { log_error "find_active_interface: /sys/class/net doesn't exist"; return 2; }
61         local iface
62         for x in $(ls -d /sys/class/net/* 2>/dev/null); do
63                 iface=$(basename $x)
64                 # find interfaces with:
65                 # - type == 1 (ethernet)
66                 # - not wireless
67                 # - with state up
68
69                 [[ $(cat $x/type) != 1 ]] && continue
70                 [[ -d $x/wireless ]] && continue
71                 [[ $(cat $x/operstate) != "up" ]] && continue
72
73                 log_info "find_active_interface: first active interface is $iface"
74                 echo $iface
75                 return 0
76         done
77
78         log_error "Unable to find any active network interface."
79         return 1
80 }
81
82 # -------------------------------------------
83
84 export PATH=/sbin:/usr/sbin:/bin:/usr/bin
85
86 log_info "starting initrd script"
87
88 do_mount_fs proc /proc
89 do_mount_fs sysfs /sys
90 do_mount_fs devtmpfs /dev
91 do_mount_fs devpts /dev/pts
92 do_mount_fs tmpfs /dev/shm
93 do_mount_fs tmpfs /tmp
94 do_mount_fs tmpfs /run
95
96 # parse kernel commandline to get NBD server
97 for x in $(cat /proc/cmdline); do
98         case $x in
99                 nbd.server=*) NBD_SERVER=${x/*=/};;
100                 nbd.port=*) NBD_PORT=${x/*=/};;
101                 nbd.dev=*)  NBD_DEV=/dev/${x/*=/};;
102                 nbd.namev3=*) NBD_NAMEV3=${x/*=/};;
103                 nbd.debug=*) DEBUG=${x/*=/};;
104         esac
105 done
106
107 check_debug "Debug point 1. Exit to continue initrd script (mount NBD device)."
108
109 log_info "NBD parameters: device $NBD_DEV, server $NBD_SERVER:$NBD_PORT"
110
111 # start nbd client
112 try=5
113 while :;do
114         log_info "Starting NBD client"
115         if [ -z "${NBD_NAMEV3}" ]; then
116                 nbd-client -persist $NBD_SERVER $NBD_PORT $NBD_DEV && { log_info "NBD client successfully started"; break; }
117                 log_info "NBD client failed"
118         else
119                 nbd3-client $NBD_SERVER $NBD_DEV --name $NBD_NAMEV3 && { log_info "NBD3 client successfully started"; break; }
120                 log_info "NBDv3 client failed"
121         fi
122         [[ $try -gt 0 ]] && { log_info "Retrying ($try trie(s) left)..."; sleep 3; try=$(( try - 1 )); continue; }
123
124         bail_out "Unable to mount NBD device $NBD_DEV using server $NBD_SERVER:$NBD_PORT"
125 done
126
127 # mount NBD device
128 mkdir -p /sysroot
129 mount $NBD_DEV -o noatime /sysroot || bail_out "Unable to mount root NBD device"
130
131 # move mounted devices to new root
132 cd /sysroot
133 for x in dev proc sys tmp run; do
134         log_info "Moving /$x to new rootfs"
135         mount -o move /$x $x
136 done
137
138 # switch to new rootfs
139 log_info "Switching to new rootfs"
140 mkdir -p boot/initramfs
141 pivot_root . boot/initramfs || bail_out "pivot_root failed."
142
143 # workaround for connman (avoid bringing down the network interface used for booting, disable DNS proxy)
144 if [[ -f /lib/systemd/system/connman.service ]]; then
145         newopts="-r -n"
146         iface=$(find_active_interface)
147         [[ -n "$iface" ]] && newopts="$newopts -I $iface"
148
149         log_info "Adjusting Connman command line. Will be: 'connmand $newopts'"
150         sed -i "s|connmand -n\$|connmand $newopts|g" /lib/systemd/system/connman.service
151 fi
152
153 # also use /proc/net/pnp to generate /etc/resolv.conf
154 rm -f /etc/resolv.conf
155 grep -v bootserver /proc/net/pnp | sed 's/^domain/search/g' >/etc/resolv.conf
156
157 # Do SELinux relabeling if required, to avoid a reboot that would complicate CI
158 if [ -f /.autorelabel ]; then
159         # Nothing SELinux related works w/o the fs mounted
160         do_mount_fs selinuxfs /sys/fs/selinux
161
162         # Labeling requires the policy to be loaded
163         log_info "Loading SELinux policy"
164         /usr/sbin/load_policy
165
166         /usr/bin/selinux-autorelabel.sh
167
168         # Will get remounted by systemd startup, unmount to keep that behavior
169         # more like the non-netboot case.
170         umount /sys/fs/selinux
171 fi
172
173 # unmount tmp and run to let systemd remount them
174 log_info "Unmounting /tmp and /run"
175 umount /tmp
176 umount /run
177
178 # finally, run systemd
179 check_debug "Debug point 2. Exit to continue initrd script (run systemd)."
180
181 log_info "Exec'ing systemd"
182 # banner generator: echo "AGL Booting . . ." | figlet -f slant -w 80 -c
183 cat <<'EOF' >&2
184 ________________________________________________________________________________
185       ___   ________       ____              __  _
186      /   | / ____/ /      / __ )____  ____  / /_(_)___  ____ _
187     / /| |/ / __/ /      / __  / __ \/ __ \/ __/ / __ \/ __ `/
188    / ___ / /_/ / /___   / /_/ / /_/ / /_/ / /_/ / / / / /_/ /   _     _     _
189   /_/  |_\____/_____/  /_____/\____/\____/\__/_/_/ /_/\__, /   (_)   (_)   (_)
190 _____________________________________________________/____/_____________________
191 EOF
192
193 exec /lib/systemd/systemd </dev/console >/dev/console 2>&1
194 bail_out
195