summaryrefslogtreecommitdiffstats
path: root/meta-netboot/recipes-core/initramfs-netboot/files/init.sh
blob: 4a4af318010f3291a1b66ed276f6b420d969f32c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/bin/sh

################################################################################
#
# Init script to boot over network through NBD
#
# Contact: Stéphane Desneux <stephane.desneux@iot.bzh>
#
################################################################################

# banner generator: echo "AGL - Netboot" | figlet -f slant -w 80 -c
cat <<'EOF' >&2
________________________________________________________________________________
         ___   ________                _   __     __  __                __
        /   | / ____/ /               / | / /__  / /_/ /_  ____  ____  / /_
       / /| |/ / __/ /      ______   /  |/ / _ \/ __/ __ \/ __ \/ __ \/ __/
      / ___ / /_/ / /___   /_____/  / /|  /  __/ /_/ /_/ / /_/ / /_/ / /_
     /_/  |_\____/_____/           /_/ |_/\___/\__/_.___/\____/\____/\__/
________________________________________________________________________________
EOF

# global variables

NBD_SERVER=
NBD_PORT=10809
NBD_DEV=/dev/nbd0
NBD_NAMEV3=
DEBUG=n

# -------------------------------------------

log_info() { echo "$0[$$]: $@" >&2; }
log_error() { echo "$0[$$]: ERROR $@" >&2; }

do_mount_fs() {
	log_info "mounting FS: $@"
	[[ -e /proc/filesystems ]] && { grep -q "$1" /proc/filesystems || { log_error "Unknown filesystem"; return 1; } }
	[[ -d "$2" ]] || mkdir -p "$2"
	[[ -e /proc/mounts ]] && { grep -q -e "^$1 $2 $1" /proc/mounts && { log_info "$2 ($1) already mounted"; return 0; } }
	mount -t "$1" "$1" "$2"
}

bail_out() {
	log_error "$@"
	check_debug "Reboot will occur after exiting this shell."
	log_info "Rebooting..."
	exec reboot -f
}

check_debug() {
	case $DEBUG in
		Y|y|yes|1|true)
			log_info "$@"
			/bin/sh -i
			;;
	esac
}

find_active_interface() {
	[[ ! -d /sys/class/net ]] && { log_error "find_active_interface: /sys/class/net doesn't exist"; return 2; }
	local iface
	for x in $(ls -d /sys/class/net/* 2>/dev/null); do
		iface=$(basename $x)
		# find interfaces with:
		# - type == 1 (ethernet)
		# - not wireless
		# - with state up

		[[ $(cat $x/type) != 1 ]] && continue
		[[ -d $x/wireless ]] && continue
		[[ $(cat $x/operstate) != "up" ]] && continue

		log_info "find_active_interface: first active interface is $iface"
		echo $iface
		return 0
	done

	log_error "Unable to find any active network interface."
	return 1
}

# -------------------------------------------

export PATH=/sbin:/usr/sbin:/bin:/usr/bin

log_info "starting initrd script"

do_mount_fs proc /proc
do_mount_fs sysfs /sys
do_mount_fs devtmpfs /dev
do_mount_fs devpts /dev/pts
do_mount_fs tmpfs /dev/shm
do_mount_fs tmpfs /tmp
do_mount_fs tmpfs /run

# parse kernel commandline to get NBD server
for x in $(cat /proc/cmdline); do
	case $x in
		nbd.server=*) NBD_SERVER=${x/*=/};;
		nbd.port=*) NBD_PORT=${x/*=/};;
		nbd.dev=*)  NBD_DEV=/dev/${x/*=/};;
		nbd.namev3=*) NBD_NAMEV3=${x/*=/};;
		nbd.debug=*) DEBUG=${x/*=/};;
	esac
done

check_debug "Debug point 1. Exit to continue initrd script (mount NBD device)."

log_info "NBD parameters: device $NBD_DEV, server $NBD_SERVER:$NBD_PORT"

# start nbd client
try=5
while :;do
	log_info "Starting NBD client"
	if [ -z "${NBD_NAMEV3}" ]; then
		nbd-client -persist $NBD_SERVER $NBD_PORT $NBD_DEV && { log_info "NBD client successfully started"; break; }
		log_info "NBD client failed"
	else
		nbd3-client $NBD_SERVER $NBD_DEV --name $NBD_NAMEV3 && { log_info "NBD3 client successfully started"; break; }
		log_info "NBDv3 client failed"
	fi
	[[ $try -gt 0 ]] && { log_info "Retrying ($try trie(s) left)..."; sleep 3; try=$(( try - 1 )); continue; }

	bail_out "Unable to mount NBD device $NBD_DEV using server $NBD_SERVER:$NBD_PORT"
done

# mount NBD device
mkdir -p /sysroot
mount $NBD_DEV -o noatime /sysroot || bail_out "Unable to mount root NBD device"

# move mounted devices to new root
cd /sysroot
for x in dev proc sys tmp run; do
	log_info "Moving /$x to new rootfs"
	mount -o move /$x $x
done

# switch to new rootfs
log_info "Switching to new rootfs"
mkdir -p boot/initramfs
pivot_root . boot/initramfs || bail_out "pivot_root failed."

# workaround for connman (avoid bringing down the network interface used for booting, disable DNS proxy)
if [[ -f /lib/systemd/system/connman.service ]]; then
	newopts="-r -n"
	iface=$(find_active_interface)
	# beside discovered iface, also ignore known renames
	[[ -n "$iface" ]] && newopts="$newopts -I ${iface},enp2s0,enp3s0,end0,enp0s2,enp0s1 "

	log_info "Adjusting Connman command line. Will be: 'connmand $newopts'"
	sed -i "s|connmand -n\$|connmand $newopts|g" /lib/systemd/system/connman.service
fi

# workaround for systemd-network-generator bringing the interface up/down/up
if [[ -f /usr/lib/systemd/system/systemd-network-generator.service ]] ; then
    mkdir /etc/systemd/system/systemd-network-generator.service.d/
    echo "[Unit]" > /etc/systemd/system/systemd-network-generator.service.d/10_disable_nbd.conf
    echo "ConditionKernelCommandLine=!nbd.server" >> /etc/systemd/system/systemd-network-generator.service.d/10_disable_nbd.conf
fi


# also use /proc/net/pnp to generate /etc/resolv.conf
rm -f /etc/resolv.conf
grep -v bootserver /proc/net/pnp | sed 's/^domain/search/g' >/etc/resolv.conf

# Do SELinux relabeling if required, to avoid a reboot that would complicate CI
if [ -f /.autorelabel ]; then
	# Nothing SELinux related works w/o the fs mounted
	do_mount_fs selinuxfs /sys/fs/selinux

	# Labeling requires the policy to be loaded
        log_info "Loading SELinux policy"
	/usr/sbin/load_policy

	/usr/bin/selinux-autorelabel.sh

	# Will get remounted by systemd startup, unmount to keep that behavior
	# more like the non-netboot case.
        umount /sys/fs/selinux
fi

# unmount tmp and run to let systemd remount them
log_info "Unmounting /tmp and /run"
umount /tmp
umount /run

# finally, run systemd
check_debug "Debug point 2. Exit to continue initrd script (run systemd)."

log_info "Exec'ing systemd"
# banner generator: echo "AGL Booting . . ." | figlet -f slant -w 80 -c
cat <<'EOF' >&2
________________________________________________________________________________
      ___   ________       ____              __  _
     /   | / ____/ /      / __ )____  ____  / /_(_)___  ____ _
    / /| |/ / __/ /      / __  / __ \/ __ \/ __/ / __ \/ __ `/
   / ___ / /_/ / /___   / /_/ / /_/ / /_/ / /_/ / / / / /_/ /   _     _     _
  /_/  |_\____/_____/  /_____/\____/\____/\__/_/_/ /_/\__, /   (_)   (_)   (_)
_____________________________________________________/____/_____________________
EOF

exec /lib/systemd/systemd </dev/console >/dev/console 2>&1
bail_out