initramfs-init.in 22.3 KB
Newer Older
1
#!/bin/sh
Natanael Copa's avatar
Natanael Copa committed
2
3

# this is the init script version
4
VERSION=@VERSION@
Natanael Copa's avatar
Natanael Copa committed
5
SINGLEMODE=no
6
sysroot=/sysroot
7
splashfile=/.splash.ctrl
8
repofile=/tmp/repositories
Natanael Copa's avatar
Natanael Copa committed
9
10
11

# some helpers
ebegin() {
12
	last_emsg="$*"
13
	echo "$last_emsg..." > /dev/kmsg
Natanael Copa's avatar
Natanael Copa committed
14
	[ "$KOPT_quiet" = yes ] && return 0
15
	echo -n " * $last_emsg: "
Natanael Copa's avatar
Natanael Copa committed
16
17
18
19
}
eend() {
	local msg
	if [ "$1" = 0 ] || [ $# -lt 1 ] ; then
20
		echo "$last_emsg: ok." > /dev/kmsg
Natanael Copa's avatar
Natanael Copa committed
21
		[ "$KOPT_quiet" = yes ] && return 0
Natanael Copa's avatar
Natanael Copa committed
22
23
24
		echo "ok."
	else
		shift
25
		echo "$last_emsg: failed. $*" > /dev/kmsg
26
27
28
		if [ "$KOPT_quiet" = "yes" ]; then
			echo -n "$last_emsg "
		fi
Natanael Copa's avatar
Natanael Copa committed
29
30
31
32
33
34
35
36
37
38
39
		echo "failed. $*"
		echo "initramfs emergency recovery shell launched. Type 'exit' to continue boot"
		/bin/busybox sh
	fi
}

unpack_apkovl() {
	local ovl="$1"
	local dest="$2"
	local suffix=${ovl##*.}
	local i
40
	ovlfiles=/tmp/ovlfiles
Natanael Copa's avatar
Natanael Copa committed
41
	if [ "$suffix" = "gz" ]; then
42
		tar -C "$dest" -zxvf "$ovl" > $ovlfiles
Natanael Copa's avatar
Natanael Copa committed
43
44
45
		return $?
	fi

46
	# we need openssl. let apk handle deps
47
	apk add --quiet --initdb --repositories-file $repofile openssl || return 1
Natanael Copa's avatar
Natanael Copa committed
48

49
	if ! openssl list -1 -cipher-commands | grep "^$suffix$" > /dev/null; then
Natanael Copa's avatar
Natanael Copa committed
50
51
52
53
54
55
56
57
		errstr="Cipher $suffix is not supported"
		return 1
	fi
	local count=0
	# beep
	echo -e "\007"
	while [ $count -lt 3 ]; do
		openssl enc -d -$suffix -in "$ovl" | tar --numeric-owner \
58
			-C "$dest" -zxv >$ovlfiles 2>/dev/null && return 0
Natanael Copa's avatar
Natanael Copa committed
59
60
		count=$(( $count + 1 ))
	done
61
	ovlfiles=
Natanael Copa's avatar
Natanael Copa committed
62
63
64
	return 1
}

65
# find mount dir for given device in an fstab
66
# returns global MNTOPTS
67
find_mnt() {
Natanael Copa's avatar
Natanael Copa committed
68
69
	local search_dev="$1"
	local fstab="$2"
70
	case "$search_dev" in
Natanael Copa's avatar
Natanael Copa committed
71
	UUID*|LABEL*) search_dev=$(findfs "$search_dev");;
72
	esac
Natanael Copa's avatar
Natanael Copa committed
73
74
75
	MNTOPTS=
	[ -r "$fstab" ] || return 1
	local search_maj_min=$(stat -L -c '%t,%T' $search_dev)
76
	while read dev mnt fs MNTOPTS chk; do
Natanael Copa's avatar
Natanael Copa committed
77
78
79
80
81
82
83
84
85
86
		case "$dev" in
		UUID*|LABEL*) dev=$(findfs "$dev");;
		esac
		if [ -b "$dev" ]; then
			local maj_min=$(stat -L -c '%t,%T' $dev)
			if [ "$maj_min" = "$search_maj_min" ]; then
				echo "$mnt"
				return
			fi
		fi
87
88
	done < $fstab
	MNTOPTS=
89
90
}

91
92
93
94
95
96
#  add a boot service to $sysroot
rc_add() {
	mkdir -p $sysroot/etc/runlevels/$2
	ln -sf /etc/init.d/$1 $sysroot/etc/runlevels/$2/$1
}

97
98
99
100
101
102
# Recursively resolve tty aliases like console or tty0
list_console_devices() {
	if ! [ -e /sys/class/tty/$1/active ]; then
		echo $1
		return
	fi
103

104
105
106
107
	for dev in $(cat /sys/class/tty/$1/active); do
		list_console_devices $dev
	done
}
108

109
110
111
setup_inittab_console(){
	term=vt100
	# Inquire the kernel for list of console= devices
112
113
	consoles="$(for c in console $KOPT_consoles; do list_console_devices $c; done)"
	for tty in $consoles; do
114
		# do nothing if inittab already have the tty set up
115
116
117
		if ! grep -q "^$tty:" $sysroot/etc/inittab; then
			echo "# enable login on alternative console" \
				>> $sysroot/etc/inittab
118
119
			# Baudrate of 0 keeps settings from kernel
			echo "$tty::respawn:/sbin/getty -L 0 $tty $term" \
120
121
				>> $sysroot/etc/inittab
		fi
122
		if [ -e "$sysroot"/etc/securetty ] && ! grep -q -w "$tty" "$sysroot"/etc/securetty; then
123
124
			echo "$tty" >> "$sysroot"/etc/securetty
		fi
125
	done
126
127
}

128
# determine the default interface to use if ip=dhcp is set
129
# uses the first "eth" interface with operstate 'up'.
130
ip_choose_if() {
Ain's avatar
Ain committed
131
132
133
134
	if [ -n "$KOPT_BOOTIF" ]; then
		mac=$(printf "%s\n" "$KOPT_BOOTIF"|sed 's/^01-//;s/-/:/g')
		dev=$(grep -l $mac /sys/class/net/*/address|head -n 1)
		dev=${dev%/*}
135
		[ -n "$dev" ] && echo "${dev##*/}" && return
Ain's avatar
Ain committed
136
	fi
137
	for x in /sys/class/net/eth*; do
138
139
140
		if grep -iq up $x/operstate;then
			[ -e "$x" ] && echo ${x##*/} && return
		fi
141
	done
142
	[ -e "$x" ] && echo ${x##*/} && return
143
144
145
146
147
148
149
150
151
152
}

# if "ip=dhcp" is specified on the command line, we obtain an IP address
# using udhcpc. we do this now and not by enabling kernel-mode DHCP because
# kernel-model DHCP appears to require that network drivers be built into
# the kernel rather than as modules. At this point all applicable modules
# in the initrd should have been loaded.
#
# You need af_packet.ko available as well modules for your Ethernet card.
#
153
154
155
# See https://www.kernel.org/doc/Documentation/filesystems/nfs/nfsroot.txt
# for documentation on the format.
#
156
# Valid syntaxes:
157
158
#   ip=client-ip:server-ip:gw-ip:netmask:hostname:device:autoconf:
#     :dns0-ip:dns1-ip:ntp0-ip
159
#   ip=dhcp
160
#   "server-ip", "hostname" and "ntp0-ip" are not supported here.
Carlo Landmeter's avatar
Carlo Landmeter committed
161
162
# Default (when configure_ip is called without setting ip=):
#   ip=dhcp
163
164
#
configure_ip() {
Carlo Landmeter's avatar
Carlo Landmeter committed
165
	[ -n "$MAC_ADDRESS" ] && return
166

Carlo Landmeter's avatar
Carlo Landmeter committed
167
	local IFS=':'
168
	set -- ${KOPT_ip:-dhcp}
Carlo Landmeter's avatar
Carlo Landmeter committed
169
	unset IFS
170

171
172
173
174
175
	local client_ip="$1"
	local gw_ip="$3"
	local netmask="$4"
	local device="$6"
	local autoconf="$7"
176
177
	local dns1="$8"
	local dns2="$9"
178

179
	case "$client_ip" in
180
		off|none) return;;
181
182
		dhcp) autoconf="dhcp";;
	esac
Natanael Copa's avatar
Natanael Copa committed
183

184
	[ -n "$device" ] || device=$(ip_choose_if)
185

186
187
188
189
	if [ -z "$device" ]; then
		echo "ERROR: IP requested but no network device was found"
		return 1
	fi
190

Natanael Copa's avatar
Natanael Copa committed
191
	if [ "$autoconf" = "dhcp" ]; then
Carlo Landmeter's avatar
Carlo Landmeter committed
192
		# automatic configuration
193
194
		if [ ! -e /usr/share/udhcpc/default.script ]; then
			echo "ERROR: DHCP requested but not present in initrd"
195
			return 1
196
		fi
197
		ebegin "Obtaining IP via DHCP ($device)"
198
199
		ifconfig "$device" 0.0.0.0
		udhcpc -i "$device" -f -q
200
201
202
203
		eend $?
	else
		# manual configuration
		[ -n "$client_ip" -a -n "$netmask" ] || return
204
		ebegin "Setting IP ($device)"
205
206
207
		if ifconfig "$device" "$client_ip" netmask "$netmask"; then
			[ -z "$gw_ip" ] || ip route add 0.0.0.0/0 via "$gw_ip" dev "$device"
		fi
208
209
		eend $?
	fi
210
211
212
213
214
215

	# Never executes if variables are empty
	for i in $dns1 $dns2; do
		echo "nameserver $i" >> /etc/resolv.conf
	done

216
	MAC_ADDRESS=$(cat /sys/class/net/$device/address)
217
218
}

219
220
# relocate mountpoint according given fstab
relocate_mount() {
Natanael Copa's avatar
Natanael Copa committed
221
222
223
224
	local fstab="${1}"
	local dir=
	if ! [ -e $repofile ]; then
		return
225
	fi
Natanael Copa's avatar
Natanael Copa committed
226
	while read dir; do
227
228
229
230
		# skip http(s)/ftp repos for netboot
		if ! [ -d "$dir" ]; then
			continue
		fi
Natanael Copa's avatar
Natanael Copa committed
231
232
233
234
235
236
237
238
239
240
		local dev=$(df -P "$dir" | tail -1 | awk '{print $1}')
		local mnt=$(find_mnt $dev $fstab)
		if [ -n "$mnt" ]; then
			local oldmnt=$(awk -v d=$dev '$1==d {print $2}' /proc/mounts)
			if [ "$oldmnt" != "$mnt" ]; then
				mkdir -p "$mnt"
				mount -o move "$oldmnt" "$mnt"
			fi
		fi
	done < $repofile
241
242
}

243
244
# find the dirs under ALPINE_MNT that are boot repositories
find_boot_repositories() {
245
246
247
	if [ -n "$ALPINE_REPO" ]; then
		echo "$ALPINE_REPO"
	else
248
		find /media/* -name .boot_repository -type f -maxdepth 3 \
249
250
			| sed 's:/.boot_repository$::'
	fi
251
252
}

Carlo Landmeter's avatar
Carlo Landmeter committed
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
setup_nbd() {
	modprobe -q nbd max_part=8 || return 1
	local IFS=, n=0
	set -- $KOPT_nbd
	unset IFS
	for ops; do
		local server="${ops%:*}"
		local port="${ops#*:}"
		local device="/dev/nbd${n}"
		[ -b "$device" ] || continue
		nbd-client "$server" "$port" "$device" && n=$((n+1))
	done
	[ "$n" != 0 ] || return 1
}

268
269
270
271
272
273
274
275
rtc_exists() {
	local rtc=
	for rtc in /dev/rtc /dev/rtc[0-9]*; do
		[ -e "$rtc" ] && break
	done
	[ -e "$rtc" ]
}

276
277
278
279
280
281
282
283
284
285
# This is used to predict if network access will be necessary
is_url() {
	case "$1" in
	http://*|https://*|ftp://*)
		return 0;;
	*)
		return 1;;
	esac
}

286
287
288
# Do some tasks to make sure mounting the ZFS pool is A-OK
prepare_zfs_root() {
	local _root_vol=${KOPT_root#ZFS=}
Ben Hoskings's avatar
Ben Hoskings committed
289
	local _root_pool=${_root_vol%%/*}
290

291
292
	# Force import if this has been imported on a different system previously.
	# Import normally otherwise
293
	if [ "$KOPT_zfs_force" = 1 ]; then
294
295
296
		zpool import -N -d /dev -f $_root_pool
	else
		zpool import -N -d /dev $_root_pool
297
298
	fi

299

300
301
302
303
304
305
306
307
308
	# Ask for encryption password
	if [ $(zpool list -H -o feature@encryption $_root_pool) = "active" ]; then
		local _encryption_root=$(zfs get -H -o value encryptionroot $_root_vol)
		if [ "$_encryption_root" != "-" ]; then
			eval zfs load-key $_encryption_root
		fi
	fi
}

309
/bin/busybox mkdir -p /usr/bin /usr/sbin /proc /sys /dev $sysroot \
Natanael Copa's avatar
Natanael Copa committed
310
	/media/cdrom /media/usb /tmp /run/cryptsetup
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325

# Spread out busybox symlinks and make them available without full path
/bin/busybox --install -s
export PATH=/usr/bin:/bin:/usr/sbin:/sbin

# Make sure /dev/null is a device node. If /dev/null does not exist yet, the command
# mounting the devtmpfs will create it implicitly as an file with the "2>" redirection.
# The -c check is required to deal with initramfs with pre-seeded device nodes without
# error message.
[ -c /dev/null ] || mknod -m 666 /dev/null c 1 3

mount -t sysfs -o noexec,nosuid,nodev sysfs /sys
mount -t devtmpfs -o exec,nosuid,mode=0755,size=2M devtmpfs /dev 2>/dev/null \
	|| mount -t tmpfs -o exec,nosuid,mode=0755,size=2M tmpfs /dev

326
327
328
329
330
331
# Make sure /dev/kmsg is a device node. Writing to /dev/kmsg allows the use of the
# earlyprintk kernel option to monitor early init progress. As above, the -c check
# prevents an error if the device node has already been seeded.
[ -c /dev/kmsg ] || mknod -m 660 /dev/kmsg c 1 11

mount -t proc -o noexec,nosuid,nodev proc /proc
332
333
334
335
336
337
338
339
340
341
# pty device nodes (later system will need it)
[ -c /dev/ptmx ] || mknod -m 666 /dev/ptmx c 5 2
[ -d /dev/pts ] || mkdir -m 755 /dev/pts
mount -t devpts -o gid=5,mode=0620,noexec,nosuid devpts /dev/pts

# shared memory area (later system will need it)
[ -d /dev/shm ] || mkdir /dev/shm
mount -t tmpfs -o nodev,nosuid,noexec shm /dev/shm


342
343
344
# read the kernel options. we need surve things like:
#  acpi_osi="!Windows 2006" xen-pciback.hide=(01:00.0)
set -- $(cat /proc/cmdline)
345

346
myopts="alpine_dev autodetect autoraid chart cryptroot cryptdm cryptheader cryptoffset
347
	cryptdiscards cryptkey debug_init dma init init_args keep_apk_new modules ovl_dev
lemmarathon's avatar
lemmarathon committed
348
	pkgs quiet root_size root usbdelay ip alpine_repo apkovl alpine_start splash
349
350
	blacklist overlaytmpfs overlaytmpfsflags rootfstype rootflags nbd resume s390x_net
	dasd ssh_key BOOTIF zfcp"
351
352
353
354
355
356
357

for opt; do
	case "$opt" in
	s|single|1)
		SINGLEMODE=yes
		continue
		;;
358
359
360
	console=*)
		opt="${opt#*=}"
		KOPT_consoles="${opt%%,*} $KOPT_consoles"
361
		switch_root_opts="-c /dev/${opt%%,*}"
362
363
		continue
		;;
Natanael Copa's avatar
Natanael Copa committed
364
	esac
365
366
367

	for i in $myopts; do
		case "$opt" in
368
		$i=*)	eval "KOPT_${i}"='${opt#*=}';;
369
370
371
372
		$i)	eval "KOPT_${i}=yes";;
		no$i)	eval "KOPT_${i}=no";;
		esac
	done
Natanael Copa's avatar
Natanael Copa committed
373
374
done

375
echo "Alpine Init $VERSION" > /dev/kmsg
Natanael Copa's avatar
Natanael Copa committed
376
377
[ "$KOPT_quiet" = yes ] || echo "Alpine Init $VERSION"

378
379
380
# enable debugging if requested
[ -n "$KOPT_debug_init" ] && set -x

381
382
383
# set default values
: ${KOPT_init:=/sbin/init}

Natanael Copa's avatar
Natanael Copa committed
384
385
386
387
388
389
390
391
392
393
# pick first keymap if found
for map in /etc/keymap/*; do
	if [ -f "$map" ]; then
		ebegin "Setting keymap ${map##*/}"
		zcat "$map" | loadkmap
		eend
		break
	fi
done

Natanael Copa's avatar
Natanael Copa committed
394
# start bootcharting if wanted
395
if [ "$KOPT_chart" = yes ]; then
Natanael Copa's avatar
Natanael Copa committed
396
	ebegin "Starting bootchart logging"
397
	/sbin/bootchartd start-initfs "$sysroot"
Natanael Copa's avatar
Natanael Copa committed
398
399
400
	eend 0
fi

401
# The following values are supported:
Natanael Copa's avatar
Natanael Copa committed
402
#   alpine_repo=auto	 -- default, search for .boot_repository
403
404
#   alpine_repo=http://...   -- network repository
ALPINE_REPO=${KOPT_alpine_repo}
Natanael Copa's avatar
Natanael Copa committed
405
[ "$ALPINE_REPO" = "auto" ] && ALPINE_REPO=
406

Natanael Copa's avatar
Natanael Copa committed
407
# hide kernel messages
408
[ "$KOPT_quiet" = yes ] && dmesg -n 1
Natanael Copa's avatar
Natanael Copa committed
409

410
# optional blacklist
411
for i in ${KOPT_blacklist//,/ }; do
Natanael Copa's avatar
Natanael Copa committed
412
413
	echo "blacklist $i" >> /etc/modprobe.d/boot-opt-blacklist.conf
done
414

415
416
417
418
419
420
421
422
423
# determine if we are going to need networking
if [ -n "$KOPT_ip" ] || [ -n "$KOPT_nbd" ] || \
	is_url "$KOPT_apkovl" || is_url "$ALPINE_REPO"; then

	do_networking=true
else
	do_networking=false
fi

424
425
426
427
428
429
430
if [ -n "$KOPT_zfcp" ]; then
	modprobe zfcp
	for _zfcp in $(echo "$KOPT_zfcp" | tr ',' ' ' | tr [A-Z] [a-z]); do
		echo 1 > /sys/bus/ccw/devices/"${_zfcp%%:*}"/online
	done
fi

431
if [ -n "$KOPT_dasd" ]; then
432
433
434
	for mod in dasd_mod dasd_eckd_mod dasd_fba_mod; do
		modprobe $mod
	done
435
	for _dasd in $(echo "$KOPT_dasd" | tr ',' ' ' | tr [A-Z] [a-z]); do
436
437
438
439
		echo 1 > /sys/bus/ccw/devices/"${_dasd%%:*}"/online
	done
fi

440
if [ "${KOPT_s390x_net%%,*}" = "qeth_l2" ]; then
441
442
443
	for mod in qeth qeth_l2 qeth_l3; do
		modprobe $mod
	done
444
	_channel="$(echo ${KOPT_s390x_net#*,} | tr [A-Z] [a-z])"
445
446
447
448
449
	echo "$_channel" > /sys/bus/ccwgroup/drivers/qeth/group
	echo 1 > /sys/bus/ccwgroup/drivers/qeth/"${_channel%%,*}"/layer2
	echo 1 > /sys/bus/ccwgroup/drivers/qeth/"${_channel%%,*}"/online
fi

450
451
452
453
454
455
456
457
# make sure we load zfs module if root=ZFS=...
rootfstype=${KOPT_rootfstype}
if [ -z "$rootfstype" ]; then
	case "$KOPT_root" in
	ZFS=*) rootfstype=zfs ;;
	esac
fi

Natanael Copa's avatar
Natanael Copa committed
458
459
# load available drivers to get access to modloop media
ebegin "Loading boot drivers"
460

461
modprobe -a $(echo "$KOPT_modules $rootfstype" | tr ',' ' ' ) loop squashfs simpledrm 2> /dev/null
Natanael Copa's avatar
Natanael Copa committed
462
463
464
465
466
467
468
469
if [ -f /etc/modules ] ; then
	sed 's/\#.*//g' < /etc/modules |
	while read module args; do
		modprobe -q $module $args
	done
fi
eend 0

470
471
472
473
474
# workaround for vmware
if grep -q VMware /sys/devices/virtual/dmi/id/sys_vendor 2>/dev/null; then
	modprobe -a ata_piix mptspi sr-mod
fi

475
476
if [ -n "$KOPT_cryptroot" ]; then
	cryptopts="-c ${KOPT_cryptroot}"
Sören Tempel's avatar
Sören Tempel committed
477
478
479
	if [ "$KOPT_cryptdiscards" = "yes" ]; then
		cryptopts="$cryptopts -D"
	fi
480
481
482
	if [ -n "$KOPT_cryptdm" ]; then
		cryptopts="$cryptopts -m ${KOPT_cryptdm}"
	fi
483
484
485
486
487
488
	if [ -n "$KOPT_cryptheader" ]; then
		cryptopts="$cryptopts -H ${KOPT_cryptheader}"
	fi
	if [ -n "$KOPT_cryptoffset" ]; then
		cryptopts="$cryptopts -o ${KOPT_cryptoffset}"
	fi
lemmarathon's avatar
lemmarathon committed
489
490
491
492
493
	if [ "$KOPT_cryptkey" = "yes" ]; then
		cryptopts="$cryptopts -k /crypto_keyfile.bin"
	elif [ -n "$KOPT_cryptkey" ]; then
		cryptopts="$cryptopts -k ${KOPT_cryptkey}"
	fi
494
495
fi

Carlo Landmeter's avatar
Carlo Landmeter committed
496
if [ -n "$KOPT_nbd" ]; then
497
	# TODO: Might fail because nlplug-findfs hasn't plugged eth0 yet
Carlo Landmeter's avatar
Carlo Landmeter committed
498
499
500
501
	configure_ip
	setup_nbd || echo "Failed to setup nbd device."
fi

502
503
# zpool reports /dev/zfs missing if it can't read /etc/mtab
ln -s /proc/mounts /etc/mtab
Mark Riedesel's avatar
Mark Riedesel committed
504

Natanael Copa's avatar
Natanael Copa committed
505
506
507
508
509
510
# check if root=... was set
if [ -n "$KOPT_root" ]; then
	if [ "$SINGLEMODE" = "yes" ]; then
		echo "Entering single mode. Type 'exit' to continue booting."
		sh
	fi
511

Natanael Copa's avatar
Natanael Copa committed
512
	ebegin "Mounting root"
513
514
	nlplug-findfs $cryptopts -p /sbin/mdev ${KOPT_debug_init:+-d} \
		$KOPT_root
515

516
	if echo "$KOPT_modules $rootfstype" | grep -qw btrfs; then
517
518
		/sbin/btrfs device scan >/dev/null || \
			echo "Failed to scan devices for btrfs filesystem."
519
520
	fi

521
522
523
	if [ -n "$KOPT_resume" ]; then
		echo "Resume from disk"
		if [ -e /sys/power/resume ]; then
524
525
526
527
528
			case "$KOPT_resume" in
			UUID*|LABEL*) resume_dev=$(findfs "$KOPT_resume");;
			*) resume_dev="$KOPT_resume";;
			esac
			printf "%d:%d" $(stat -Lc "0x%t 0x%T" "$resume_dev") >/sys/power/resume
529
530
531
532
533
		else
			echo "resume: no hibernation support found"
		fi
	fi

534
	if [ "$KOPT_overlaytmpfs" = "yes" ]; then
535
		# Create mountpoints
536
537
		mkdir -p /media/root-ro /media/root-rw $sysroot/media/root-ro \
			$sysroot/media/root-rw
538
539
540
541
542
543
544
545
		# Mount read-only underlying rootfs
		rootflags="${KOPT_rootflags:+$KOPT_rootflags,}ro"
		mount ${KOPT_rootfstype:+-t $KOPT_rootfstype} -o $rootflags \
			$KOPT_root /media/root-ro
		# Mount writable overlay tmpfs
		overlaytmpfsflags="mode=0755,${KOPT_overlaytmpfsflags:+$KOPT_overlaytmpfsflags,}rw"
		mount -t tmpfs -o $overlaytmpfsflags root-tmpfs /media/root-rw
		# Create additional mountpoints and do the overlay mount
546
		mkdir -p /media/root-rw/work /media/root-rw/root
547
548
549
		mount -t overlay -o \
			lowerdir=/media/root-ro,upperdir=/media/root-rw/root,workdir=/media/root-rw/work \
			overlayfs $sysroot
550
	else
551
		if [ "$rootfstype" = "zfs" ]; then
552
553
			prepare_zfs_root
		fi
554
		mount ${rootfstype:+-t} ${rootfstype} \
555
			-o ${KOPT_rootflags:-ro} \
556
			${KOPT_root#ZFS=} $sysroot
557
558
	fi

Natanael Copa's avatar
Natanael Copa committed
559
560
	eend $?
	cat /proc/mounts | while read DEV DIR TYPE OPTS ; do
561
562
563
		if [ "$DIR" != "/" -a "$DIR" != "$sysroot" -a -d "$DIR" ]; then
			mkdir -p $sysroot/$DIR
			mount -o move $DIR $sysroot/$DIR
Natanael Copa's avatar
Natanael Copa committed
564
565
566
		fi
	done
	sync
567
	exec /bin/busybox switch_root $switch_root_opts $sysroot $chart_init "$KOPT_init" $KOPT_init_args
Natanael Copa's avatar
Natanael Copa committed
568
569
570
571
	echo "initramfs emergency recovery shell launched"
	exec /bin/busybox sh
fi

572
if $do_networking; then
573
574
575
576
577
	repoopts="-n"
else
	repoopts="-b $repofile"
fi

Natanael Copa's avatar
Natanael Copa committed
578
579
# locate boot media and mount it
ebegin "Mounting boot media"
580
nlplug-findfs $cryptopts -p /sbin/mdev ${KOPT_debug_init:+-d} \
581
	${KOPT_usbdelay:+-t $(( $KOPT_usbdelay * 1000 ))} \
582
	$repoopts -a /tmp/apkovls
Natanael Copa's avatar
Natanael Copa committed
583
584
eend $?

585
586
# Setup network interfaces
if $do_networking; then
Natanael Copa's avatar
Natanael Copa committed
587
	configure_ip
588
589
fi

Natanael Copa's avatar
Natanael Copa committed
590
591
592
593
594
595
# early console?
if [ "$SINGLEMODE" = "yes" ]; then
	echo "Entering single mode. Type 'exit' to continue booting."
	sh
fi

596
# mount tmpfs sysroot
597
rootflags="mode=0755"
598
if [ -n "$KOPT_root_size" ]; then
599
600
	echo "WARNING: the boot option root_size is deprecated. Use rootflags instead"
	rootflags="$rootflags,size=$KOPT_root_size"
601
fi
602
603
604
605
606
if [ -n "$KOPT_rootflags" ]; then
	rootflags="$rootflags,$KOPT_rootflags"
fi

mount -t tmpfs -o $rootflags tmpfs $sysroot
Natanael Copa's avatar
Natanael Copa committed
607

608
609
610
611
612
613
614
if [ -z "$KOPT_apkovl" ]; then
	# Not manually set, use the apkovl found by nlplug
	if [ -e /tmp/apkovls ]; then
		ovl=$(head -n 1 /tmp/apkovls)
	fi
elif is_url "$KOPT_apkovl"; then
	# Fetch apkovl via network
Ain's avatar
Ain committed
615
	MACHINE_UUID=$(cat /sys/class/dmi/id/product_uuid 2>/dev/null)
616
617
618
619
620
621
622
	url="${KOPT_apkovl/{MAC\}/$MAC_ADDRESS}"
	url="${url/{UUID\}/$MACHINE_UUID}"
	ovl=/tmp/${url##*/}
	wget -O "$ovl" "$url" || ovl=
else
	ovl="$KOPT_apkovl"
fi
Natanael Copa's avatar
Natanael Copa committed
623

624
625
626
627
628
# parse pkgs=pkg1,pkg2
if [ -n "$KOPT_pkgs" ]; then
	pkgs=$(echo "$KOPT_pkgs" | tr ',' ' ' )
fi

629
# load apkovl or set up a minimal system
Natanael Copa's avatar
Natanael Copa committed
630
631
if [ -f "$ovl" ]; then
	ebegin "Loading user settings from $ovl"
632
633
634
	# create apk db and needed /dev/null and /tmp first
	apk add --root $sysroot --initdb --quiet

635
	unpack_apkovl "$ovl" $sysroot
636
	eend $? $errstr || ovlfiles=
637
638
	# hack, incase /root/.ssh was included in apkovl
	[ -d "$sysroot/root" ] && chmod 700 "$sysroot/root"
639
	pkgs="$pkgs $(cat $sysroot/etc/apk/world 2>/dev/null)"
640
fi
641

642
if [ -f "$sysroot/etc/.default_boot_services" -o ! -f "$ovl" ]; then
643
	# add some boot services by default
Natanael Copa's avatar
Natanael Copa committed
644
	rc_add devfs sysinit
645
646
	rc_add dmesg sysinit
	rc_add mdev sysinit
647
648
	rc_add hwdrivers sysinit
	rc_add modloop sysinit
649
650
651
652
653
654

	rc_add modules boot
	rc_add sysctl boot
	rc_add hostname boot
	rc_add bootmisc boot
	rc_add syslog boot
Natanael Copa's avatar
Natanael Copa committed
655

656
657
658
	rc_add mount-ro shutdown
	rc_add killprocs shutdown
	rc_add savecache shutdown
659

660
661
	rc_add firstboot default

662
663
664
665
666
667
	# add openssh
	if [ -n "$KOPT_ssh_key" ]; then
		pkgs="$pkgs openssh"
		rc_add sshd default
	fi

668
	rm -f "$sysroot/etc/.default_boot_services"
Natanael Copa's avatar
Natanael Copa committed
669
670
fi

671
if [ "$KOPT_splash" != "no" ]; then
Timo Teräs's avatar
Timo Teräs committed
672
	echo "IMG_ALIGN=CM" > /tmp/fbsplash.cfg
673
674
	for fbdev in /dev/fb[0-9]; do
		[ -e "$fbdev" ] || break
675
		num="${fbdev#/dev/fb}"
676
		for img in /media/*/fbsplash$num.ppm; do
677
			[ -e "$img" ] || break
678
			config="${img%.*}.cfg"
679
680
681
682
683
			[ -e "$config" ] || config=/tmp/fbsplash.cfg
			fbsplash -s "$img" -d "$fbdev" -i "$config"
			break
		done
	done
684
	for fbsplash in /media/*/fbsplash.ppm; do
685
		[ -e "$fbsplash" ] && break
686
687
688
689
690
	done
fi

if [ -n "$fbsplash" ] && [ -e "$fbsplash" ]; then
	ebegin "Starting bootsplash"
691
	mkfifo $sysroot/$splashfile
692
	config="${fbsplash%.*}.cfg"
693
	[ -e "$config" ] || config=/tmp/fbsplash.cfg
Natanael Copa's avatar
Natanael Copa committed
694
	setsid fbsplash -T 16 -s "$fbsplash" -i $config -f $sysroot/$splashfile &
695
696
697
698
699
	eend 0
else
	KOPT_splash="no"
fi

700
if [ -f $sysroot/etc/fstab ]; then
701
	has_fstab=1
Natanael Copa's avatar
Natanael Copa committed
702
	fstab=$sysroot/etc/fstab
703
704

	# let user override tmpfs size in fstab in apkovl
705
706
707
708
	mountopts=$(awk '$2 == "/" && $3 == "tmpfs" { print $4 }' $sysroot/etc/fstab)
	if [ -n "$mountopts" ]; then
		mount -o remount,$mountopts $sysroot
	fi
709
710
711
	# move the ALPINE_MNT if ALPINE_DEV is specified in users fstab
	# this is so a generated /etc/apk/repositories will use correct
	# mount dir
Natanael Copa's avatar
Natanael Copa committed
712
713
714
	relocate_mount "$sysroot"/etc/fstab
elif [ -f /etc/fstab ]; then
	relocate_mount /etc/fstab
715
716
fi

Natanael Copa's avatar
Natanael Copa committed
717
# hack so we get openrc
718
pkgs="$pkgs alpine-base"
Natanael Copa's avatar
Natanael Copa committed
719

720
721
722
723
# copy keys so apk finds them. apk looks for stuff relative --root
mkdir -p $sysroot/etc/apk/keys/
cp -a /etc/apk/keys $sysroot/etc/apk

724
# generate apk repositories file. needs to be done after relocation
725
find_boot_repositories > $repofile
726

727
728
729
730
731
732
733
734
# silently fix apk arch in case the apkovl does not match
if [ -r "$sysroot"/etc/apk/arch ]; then
	apk_arch="$(apk --print-arch)"
	if [ -n "$apk_arch" ]; then
		echo "$apk_arch" > "$sysroot"/etc/apk/arch
	fi
fi

735
# generate repo opts for apk
736
for i in $(cat $repofile); do
737
738
	repo_opt="$repo_opt --repository $i"
done
739

Natanael Copa's avatar
Natanael Copa committed
740
741
# install new root
ebegin "Installing packages to root filesystem"
742

743
if [ "$KOPT_chart" = yes ]; then
Natanael Copa's avatar
Natanael Copa committed
744
745
	pkgs="$pkgs acct"
fi
746

747
# use swclock if no RTC is found
Tuan Hoang's avatar
Tuan Hoang committed
748
if rtc_exists || [ "$(uname -m)" = "s390x" ]; then
749
750
751
752
753
	rc_add hwclock boot
else
	rc_add swclock boot
fi

754
755
756
757
# enable support for modloop verification
if [ -f /var/cache/misc/*modloop*.SIGN.RSA.*.pub ]; then
	mkdir -p "$sysroot"/var/cache/misc
	cp /var/cache/misc/*modloop*.SIGN.RSA.*.pub "$sysroot"/var/cache/misc
758
	pkgs="$pkgs openssl"
759
760
fi

Henrik Riomar's avatar
Henrik Riomar committed
761
apkflags="--initramfs-diskless-boot --progress"
762
if [ -z "$MAC_ADDRESS" ]; then
763
764
	apkflags="$apkflags --no-network"
else
765
	apkflags="$apkflags --update-cache"
766
767
fi

Natanael Copa's avatar
Natanael Copa committed
768
if [ "$KOPT_quiet" = yes ]; then
769
770
771
	apkflags="$apkflags --quiet"
fi

772
if [ "$KOPT_keep_apk_new" != yes ]; then
773
774
775
	apkflags="$apkflags --clean-protected"
	[ -n "$ovlfiles" ] && apkflags="$apkflags --overlay-from-stdin"
fi
Timo Teräs's avatar
Timo Teräs committed
776
777
778
779
mkdir -p $sysroot/sys $sysroot/proc $sysroot/dev
mount -o bind /sys $sysroot/sys
mount -o bind /proc $sysroot/proc
mount -o bind /dev $sysroot/dev
780
if [ -n "$ovlfiles" ]; then
Natanael Copa's avatar
Natanael Copa committed
781
	apk add --root $sysroot $repo_opt $apkflags $pkgs <$ovlfiles
782
else
Natanael Copa's avatar
Natanael Copa committed
783
	apk add --root $sysroot $repo_opt $apkflags $pkgs
Natanael Copa's avatar
Natanael Copa committed
784
fi
Timo Teräs's avatar
Timo Teräs committed
785
umount $sysroot/sys $sysroot/proc $sysroot/dev
Natanael Copa's avatar
Natanael Copa committed
786
787
eend $?

788
789
790
791
792
# unmount ovl mount if needed
if [ -n "$ovl_unmount" ]; then
	umount $ovl_unmount 2>/dev/null
fi

793
# remount according default fstab from package
Natanael Copa's avatar
Natanael Copa committed
794
795
if [ -z "$has_fstab" ] && [ -f "$sysroot"/etc/fstab ]; then
	relocate_mount "$sysroot"/etc/fstab
796
797
fi

798
799
800
801
802
# generate repositories if none exists. this needs to be done after relocation
if ! [ -f "$sysroot"/etc/apk/repositories ]; then
	find_boot_repositories > "$sysroot"/etc/apk/repositories
fi

803
# respect mount options in fstab for ALPINE_MNT (e.g if user wants rw)
804
805
806
807
808
if [ -f "$sysroot"/etc/fstab ]; then
	opts=$(awk "\$2 == \"$ALPINE_MNT\" {print \$4}" $sysroot/etc/fstab)
	if [ -n "$opts" ]; then
		mount -o remount,$opts "$ALPINE_MNT"
	fi
809
810
fi

811
# fix inittab if alternative console
812
setup_inittab_console
813

Natanael Copa's avatar
Natanael Copa committed
814
# copy alpine release info
815
816
817
818
#if ! [ -f "$sysroot"/etc/alpine-release ] && [ -f $ALPINE_MNT/.alpine-release ]; then
#	cp $ALPINE_MNT/.alpine-release $sysroot/
#	ln -sf /.alpine-release $sysroot/etc/alpine-release
#fi
Natanael Copa's avatar
Natanael Copa committed
819

820
! [ -f "$sysroot"/etc/resolv.conf ] && [ -f /etc/resolv.conf ] && \
Natanael Copa's avatar
Natanael Copa committed
821
	cp /etc/resolv.conf "$sysroot"/etc
822

Natanael Copa's avatar
Natanael Copa committed
823
824
# setup bootchart for switch_root
chart_init=""
825
if [ "$KOPT_chart" = yes ]; then
826
	/sbin/bootchartd stop-initfs "$sysroot"
Natanael Copa's avatar
Natanael Copa committed
827
828
829
	chart_init="/sbin/bootchartd start-rootfs"
fi

830
if [ ! -x "${sysroot}${KOPT_init}" ]; then
831
	[ "$KOPT_splash" != "no" ] && echo exit > $sysroot/$splashfile
832
	echo "$KOPT_init not found in new root. Launching emergency recovery shell"
833
834
835
836
	echo "Type exit to continue boot."
	/bin/busybox sh
fi

Natanael Copa's avatar
Natanael Copa committed
837
838
# switch over to new root
cat /proc/mounts | while read DEV DIR TYPE OPTS ; do
839
840
841
	if [ "$DIR" != "/" -a "$DIR" != "$sysroot" -a -d "$DIR" ]; then
		mkdir -p $sysroot/$DIR
		mount -o move $DIR $sysroot/$DIR
Natanael Copa's avatar
Natanael Copa committed
842
843
844
845
	fi
done
sync

846
[ "$KOPT_splash" = "init" ] && echo exit > $sysroot/$splashfile
Natanael Copa's avatar
Natanael Copa committed
847
echo ""
848
exec /bin/busybox switch_root $switch_root_opts $sysroot $chart_init "$KOPT_init" $KOPT_init_args
Natanael Copa's avatar
Natanael Copa committed
849

850
[ "$KOPT_splash" != "no" ] && echo exit > $sysroot/$splashfile
Natanael Copa's avatar
Natanael Copa committed
851
852
853
echo "initramfs emergency recovery shell launched"
exec /bin/busybox sh
reboot