aboutsummaryrefslogtreecommitdiffstats
path: root/initramfs-init.in
blob: 4e15b5a40f3f83bfadb7b28112b941f98fd9c288 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#!/bin/sh

# this is the init script version
VERSION=@VERSION@
SINGLEMODE=no
sysroot=/sysroot

/bin/busybox mkdir -p /usr/bin /usr/sbin /proc /sys /dev $sysroot \
	/media/cdrom /media/floppy /media/usb /tmp
/bin/busybox --install -s

# basic environment
export PATH=/usr/bin:/bin:/usr/sbin:/sbin

# needed devs
[ -c /dev/null ] || mknod -m 666 /dev/null c 1 3

# basic mounts
mount -t proc -o noexec,nosuid,nodev proc /proc
mount -t sysfs -o noexec,nosuid,nodev sysfs /sys

# some helpers
ebegin() {
	last_emsg="$*"
	[ -n "$KOPT_quiet" ] && return 0
	echo -n " * $last_emsg: "
}
eend() {
	local msg
	if [ "$1" = 0 ] || [ $# -lt 1 ] ; then
		[ -n "$KOPT_quiet" ] && return 0
		echo "ok."
	else
		shift
		if [ "$KOPT_quiet" = "yes" ]; then
			echo -n "$last_emsg "
		fi
		echo "failed. $*"
		echo "initramfs emergency recovery shell launched. Type 'exit' to continue boot"
		/bin/busybox sh
	fi
}

scan_drivers() {
	if [ "$KOPT_autodetect" != no ] ; then
		find /sys -name modalias | xargs sort -u | xargs modprobe -a 2> /dev/null
	fi
}

find_ovl() {
	local mnt="$1"
	local ovl
	local lines

	# look for apkovl's on mounted media
	ovl=$( ls -1 "$mnt"/*.apkovl.tar.gz* 2>/dev/null ) || return 1
	lines=$(echo "$ovl" | wc -l)

	if [ $lines -gt 1 ] ; then
		echo "ERROR: More than one apkovl file was found on $(basename $mnt). None will be read." >&2
		return 1
	fi
	echo "$ovl"
}

retry_mount() {
	# usb might need some time to settle so we retry a few times
	for i in $(seq 0 9); do
		mount $@ 2>/tmp/mount-error && return 0
		sleep 1
	done
	return 1
}

unpack_apkovl() {
	local ovl="$1"
	local dest="$2"
	local suffix=${ovl##*.}
	local i
	ovlfiles=/tmp/ovlfiles
	if [ "$suffix" = "gz" ]; then
		tar -C "$dest" -zxvf "$ovl" > $ovlfiles
		return $?
	fi

	# we need openssl. let apk handle deps
	apk add --quiet --initdb --repositories-file /tmp/repositories openssl\
		|| return 1

	if ! openssl list-cipher-commands | grep "^$suffix$" > /dev/null; then
		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 \
			-C "$dest" -zxv >$ovlfiles 2>/dev/null && return 0
		count=$(( $count + 1 ))
	done
	ovlfiles=
	return 1
}

# find mount dir for given device in an fstab
find_mnt() {
	local dev="$1"
	local fsfile="$2"
	awk "\$1 == \"$dev\" {print \$2}\"" "$fsfile" 2>/dev/null
}

# Wait for usb to settle
wait_usb() {
	if [ -n "$USB_DONE" ] || ! dmesg | grep '^usb-storage: waiting' >/dev/null; then
		return 0
	fi
	ebegin "Waiting for USB device to settle"
	while ! dmesg | grep 'usb-storage: device scan complete' >/dev/null; do
		sleep 1
	done
	USB_DONE=yes
	eend 0
}

#  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
}

# we have issues with some slow usb 1 hosts so we add 1 second delay
# with possibility to increase delay at boot prompt with usbdelay=<sec>
find_ovl_dev() {
	local n i
	# look for apkovl
	for n in $(seq 0 ${KOPT_usbdelay:-1}); do
		# wait for usb to settle if needed
		wait_usb
		for i in usb floppy cdrom; do
			mount -o ro /media/$i 2>/dev/null || continue
			ovl=$(find_ovl /media/$i)
			if [ -f "$ovl" ]; then
				ovl_unmount="$ovl_unmount /media/$i"
				return
			fi
			umount /media/$i 2>/dev/null
		done
		sleep 1
	done
}

setup_inittab_console(){
	while [ $# -gt 0 ]; do
		local tty=${1%,*}
		local speed=${1#*,}
		local line=
		local term=
		case "$tty" in
			ttyS*)
				[ "$speed" = "$1" ] && speed=9600
				term=vt100
				line=-L
				;;
			*)
				[ "$speed" = "$1" ] && speed=38400
				;;
		esac
		shift

		# do nothing if inittab already have the tty set up
		grep -q "^$tty:" $sysroot/etc/inittab && continue
		echo "# enable login on alternative console" >> $sysroot/etc/inittab
		echo "$tty::respawn:/sbin/getty $line $speed $tty $term" \
			>> $sysroot/etc/inittab
	done
}

start_raid() {
	local n= i=
	case "$KOPT_root" in
		/dev/md*) n=${KOPT_root#/dev/md} ;;
	esac
	case "$KOPT_autoraid" in
		[0-9]*) n="$n $(echo $KOPT_autoraid | tr ',' ' ')" ;;
	esac
	# if kernel can autostart the raid he will
	for i in $n; do
		mknod /dev/md$i b 9 $i
		raidautorun /dev/md$i
	done
	# kernel cannot autostart newer versions of mdadm metadata
	# so we also check if mdadm binary is there
	if [ -x /sbin/mdadm ]; then
		mdadm --assemble --scan
	fi
}

# start cryptsetup if exists
start_cryptsetup() {
	[ -x /sbin/cryptsetup ] || return
	modprobe dm-crypt
	if [ -n "$KOPT_cryptroot" ]; then
		modprobe dm-crypt
		for map in /etc/keymap/*; do if [ -f "$map" ]; then zcat "$map" | loadkmap && break; fi ; done
		cryptsetup luksOpen "$KOPT_cryptroot" "$KOPT_cryptdm"
	fi
}

# start lvm if exists
start_lvm() {
	[ -x /sbin/lvm ] || return
	modprobe dm-mod
	lvm vgscan --mknodes --ignorelockingfailure >/dev/null 2>&1 &&\
		lvm vgchange --ignorelockingfailure -a y >/dev/null 2>&1
}

# gotta start from somewhere :)
echo "Alpine Init $VERSION"

# read the kernel options. We use eval set so we can handle things like
# acpi_osi="!Windows 2006"
eval set -- `cat /proc/cmdline`

while [ $# -gt 0 ]; do
	case "$1" in
		s|single|1)
			SINGLEMODE=yes ;;
		console=*)
			CONSOLE="$CONSOLE ${1#console=}";;
		*=*)    eval "KOPT_${1%%=*}='${1#*=}'" ;;
		no*)    eval "KOPT_$(echo ${1#no} | sed 's: :_:g')=no" ;;
		*)      eval "KOPT_$(echo $1 | sed 's: :_:g')=yes" ;;
	esac
	shift
done

# 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

# start bootcharting if wanted
if [ "$KOPT_chart" = yes ]; then
	ebegin "Starting bootchart logging"
	/sbin/bootchartd start-initfs "$sysroot"
	eend 0
fi

# dma can be problematic
if [ "$KOPT_dma" = no ]; then
	modprobe libata dma=0
fi

ALPINE_DEV=${KOPT_alpine_dev%%:*}
ALPINE_DEV_FS=${KOPT_alpine_dev##*:}
if [ "$ALPINE_DEV_FS" = "$ALPINE_DEV" ]; then
	unset ALPINE_DEV_FS
fi
if [ -n "$KOPT_ovl_dev" ] ; then
	OVL_DEV=${KOPT_ovl_dev%%:*}
	OVL_DEV_FS=${KOPT_ovl_dev##*:}
	if [ "$OVL_DEV_FS" = "$OVL_DEV" ]; then
		unset OVL_DEV_FS
	fi
fi

case "$ALPINE_DEV" in
	UUID=*) ;;
	*) ALPINE_DEV=/dev/$ALPINE_DEV ;;
esac
	
# look for standard mountpoint locations
ALPINE_MNT=$(find_mnt $ALPINE_DEV /etc/fstab)
[ -z "$ALPINE_MNT" ] && ALPINE_MNT=/media/${ALPINE_DEV##*/}

# hide kernel messages
[ "$KOPT_quiet" = yes ] && dmesg -n 1

# setup /dev
ebegin "Starting mdev"
mount -t tmpfs -o exec,nosuid,mode=0755,size=1M mdev /dev
echo "/sbin/mdev" > /proc/sys/kernel/hotplug
mdev -s
RC=$?
[ -d /dev/pts ] || mkdir -m 755 /dev/pts
[ -c /dev/ptmx ] || mknod -m 666 /dev/ptmx c 5 2
# make sure /dev/null is setup correctly
[ -f /dev/null ] && rm -f /dev/null
[ -c /dev/null ] || mknod -m 666 /dev/null c 1 3
mount -t devpts -o gid=5,mode=0620,noexec,nosuid devpts /dev/pts
[ -d /dev/shm ] || mkdir /dev/shm
mount -t tmpfs -o nodev,nosuid,noexec shm /dev/shm
eend $RC

# load available drivers to get access to modloop media
ebegin "Loading boot drivers"

modprobe -a $(echo "$KOPT_modules" | tr ',' ' ' ) loop cramfs 2> /dev/null
if [ -f /etc/modules ] ; then
	sed 's/\#.*//g' < /etc/modules |
	while read module args; do
		modprobe -q $module $args
	done
fi
scan_drivers
scan_drivers
eend 0

ebegin "Setting up framebuffer console"
if [ -d /lib/modules/`uname -r`/kernel/drivers/gpu ]; then
	modprobe -q fbcon &>/dev/null
fi
eend 0

# 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
	# let usb settle in case we boot from usb disks
	[ -n "$KOPT_usbdelay" ] && sleep "$KOPT_usbdelay"
	wait_usb
	start_raid
	start_cryptsetup
	start_lvm
	ebegin "Mounting root"
	retry_mount -o ro $KOPT_root $sysroot 2>/dev/null
	eend $?
	cat /proc/mounts | while read DEV DIR TYPE OPTS ; do
		if [ "$DIR" != "/" -a "$DIR" != "$sysroot" -a -d "$DIR" ]; then
			mkdir -p $sysroot/$DIR
			mount -o move $DIR $sysroot/$DIR
		fi
	done
	sync
	exec /bin/busybox switch_root $sysroot $chart_init /sbin/init $KOPT_init_args
	echo "initramfs emergency recovery shell launched"
	exec /bin/busybox sh
fi

# we only want to wait for usb if really needed at this point
if [ -z "${ALPINE_DEV##*usb*}" ]; then
	wait_usb
fi

# incase we have alpine_dev on raid device...
start_raid
start_cryptsetup
start_lvm

# locate boot media and mount it
ebegin "Mounting boot media"
mkdir -p $ALPINE_MNT
if [ -n "$ALPINE_DEV_FS" ]; then
	mount_opts="-t $ALPINE_DEV_FS"
fi

retry_mount -o ro $mount_opts $ALPINE_DEV $ALPINE_MNT >/dev/null 2>&1
eend $?

# generate apk repositories file 
echo "$(find $ALPINE_MNT -name .boot_repository -type f -maxdepth 3 \
	| sed 's:/.boot_repository$::')" > /tmp/repositories

# early console?
if [ "$SINGLEMODE" = "yes" ]; then
	echo "Entering single mode. Type 'exit' to continue booting."
	sh
fi

# mount tmpfs sysroot
root_opts="-o mode=0755"
if [ -n "$KOPT_root_size" ]; then
	root_opts="$root_opts,size=$KOPT_root_size"
fi
mount -t tmpfs $root_opts tmpfs $sysroot

if [ -n "$OVL_DEV" ]; then
	mkdir -p /media/$OVL_DEV
	unset mount_opts
	if [ -n "$OVL_DEV_FS" ]; then
		mount_opts="-t $OVL_DEV_FS"
	fi
	
	retry_mount -o ro $mount_opts /dev/$OVL_DEV /media/$OVL_DEV \
		>/dev/null 2>&1
	ovl=$(find_ovl /media/$OVL_DEV)
else
	find_ovl_dev
fi
if ! [ -f "$ovl" ]; then
	ovl=$(find_ovl $ALPINE_MNT)
fi

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

# load apkovl or set up a minimal system
if [ -f "$ovl" ]; then
	ebegin "Loading user settings from $ovl"
	# create apk db and needed /dev/null and /tmp first
	apk add --root $sysroot --initdb --quiet

	unpack_apkovl "$ovl" $sysroot
	eend $? $errstr || ovlfiles=
	# hack, incase /root/.ssh was included in apkovl
	[ -d "$sysroot/root" ] && chmod 700 "$sysroot/root"
	umount $ovl_unmount 2>/dev/null &
	pkgs="$pkgs $(sed 's/\#.*//' $sysroot/etc/lbu/packages.list 2>/dev/null)"
	rm -f "$sysroot"/etc/lbu/packages.list
	pkgs="$pkgs $(cat $sysroot/var/lib/apk/world $sysroot/etc/apk/world 2>/dev/null)"
else
	# add some boot services by default
	rc_add devfs sysinit
	rc_add dmesg sysinit
	rc_add mdev sysinit

	rc_add hwclock boot
	rc_add modules boot
	rc_add sysctl boot
	rc_add hostname boot
	rc_add bootmisc boot
	rc_add syslog boot
	
	rc_add mount-ro shutdown
	rc_add killprocs shutdown
	rc_add savecache shutdown
fi

# let user override tmpfs size in fstab in apkovl
if [ -f $sysroot/etc/fstab ]; then
	mountopts=$(awk '$2 == "/" && $3 == "tmpfs" { print $4 }' $sysroot/etc/fstab)
	if [ -n "$mountopts" ]; then
		mount -o remount,$mountopts $sysroot
	fi
fi

# in case we upgrade we might need those:
rc_add hwdrivers sysinit
rc_add modloop sysinit


# hack so we get openrc
pkgs="$pkgs alpine-base"

# 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
new_mnt=$(find_mnt $ALPINE_DEV $sysroot/etc/fstab)
if [ -n "$new_mnt" ] && [ "$new_mnt" != "$ALPINE_MNT" ]; then
	mkdir -p $new_mnt
	mount -o move $ALPINE_MNT $new_mnt
	ALPINE_MNT="$new_mnt"
fi

# let user set ALPINE_MNT as readonly in fstab
if [ -f $sysroot/etc/fstab ]; then
	mountopts=$(awk "\$2 == \"$ALPINE_MNT\" { print \$4 }" \
		$sysroot/etc/fstab)
	if [ -n "$mountopts" ]; then
		mount -o remount,$mountopts $ALPINE_MNT
	fi
fi

# 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

# generate repo opts for apk
if [ ! -f $sysroot/etc/apk/repositories ]; then
	mv /tmp/repositories $sysroot/etc/apk/
else
	for i in $(cat /tmp/repositories); do
		repo_opt="$repo_opt --repository $i"
	done
fi

# install new root
ebegin "Installing packages to root filesystem"
# insert newline unless quiet to fix progress bar
[ -n "$KOPT_quiet" ] || echo ""

if [ "$KOPT_chart" = yes ]; then
	pkgs="$pkgs acct"
fi
apkflags="--initdb --quiet --progress --force --no-network"
if [ "$KOPT_keep_apk_new" != yes ]; then
	apkflags="$apkflags --clean-protected"
	[ -n "$ovlfiles" ] && apkflags="$apkflags --overlay-from-stdin"
fi

if [ -n "$ovlfiles" ]; then
	apk add --root $sysroot $repo_opt $apkflags $pkgs <$ovlfiles>/dev/null 
else
	apk add --root $sysroot $repo_opt $apkflags $pkgs >/dev/null
fi
eend $?

# fix inittab if alternative console
setup_inittab_console $CONSOLE

# copy alpine release info
if ! [ -f "$sysroot"/etc/alpine-release ]; then
	cp $ALPINE_MNT/.alpine-release $sysroot/
	ln -sf /.alpine-release $sysroot/etc/alpine-release
fi

# setup bootchart for switch_root
chart_init=""
if [ "$KOPT_chart" = yes ]; then
	/sbin/bootchartd stop-initfs "$sysroot"
	chart_init="/sbin/bootchartd start-rootfs"
fi

if [ ! -x $sysroot/sbin/init ]; then
	echo "/sbin/init not found in new root. Launching emergency recovery shell"
	echo "Type exit to continue boot."
	/bin/busybox sh
fi

# switch over to new root
cat /proc/mounts | while read DEV DIR TYPE OPTS ; do
	if [ "$DIR" != "/" -a "$DIR" != "$sysroot" -a -d "$DIR" ]; then
		mkdir -p $sysroot/$DIR
		mount -o move $DIR $sysroot/$DIR
	fi
done
sync

echo ""
exec /bin/busybox switch_root $sysroot $chart_init /sbin/init $KOPT_init_args

echo "initramfs emergency recovery shell launched"
exec /bin/busybox sh
reboot