update-kernel.in 6.06 KB
Newer Older
1 2 3 4 5 6 7 8
#!/bin/sh -e

# update-kernel
#
# Kernel and firmware update script for Alpine installations set up
# with setup-bootable
#
# Copyright (c) 2014 Timo Teräs
9
# Copyright (c) 2014-2015 Kaarle Ritvanen
10 11 12 13 14


SCRIPT=update-kernel
VIRTUAL=.tmp-$SCRIPT

15 16
SUPERUSER=
[ $(id -u) -eq 0 ] && SUPERUSER=Y
17 18 19
if [ -z "$SUPERUSER" ] && [ -z "$FAKEROOTKEY" ]; then
	exec fakeroot "$0" "$@"
fi
20

21
ARCH=
22
BUILDDIR=
23
FLAVOR=
24
MEDIA=
25
MNTDIR=
26
PACKAGES=
27 28
MKINITFS_ARGS=
REPOSITORIES_FILE=/etc/apk/repositories
29 30 31 32
SIGNALS="HUP INT TERM"
TMPDIR=
features=

33
error() {
34
	echo "$SCRIPT: $1" >&2
35 36 37
}

usage() {
38 39 40
	[ "$2" ] && error "$2"
	local opts="[-F <feature>]... [-p <package>]..."
	local dest_args="[-a <arch>] <dest_dir>"
41
	cat >&2 <<-__EOF__
42

43 44 45
		Syntax: $SCRIPT $opts [$dest_args]
		        $SCRIPT -f <flavor> $opts $dest_args
		        $SCRIPT -b <build_dir> $opts [$dest_args]
46

47 48 49 50 51 52
		Options: -a|--arch <arch>        Install kernel for specified architecture
		         -b|--build <build_dir>  Install custom-built kernel
		         -f|--flavor <flavor>    Install kernel of specified flavor
		         -F|--feature <feature>  Enable initfs feature
		         -p|--package <package>  Additional module or firmware package
		         -v|--verbose            Verbose output
53
		         -k|--apk-pubkey <key>   Include given key in initramfs
54
		         -K|--hostkeys           Include host keys in initramfs
55
		         -C|--compression        Initramfs compression (see mkinitfs for options)
56
		         -M|--media              Boot media directory layout
57
		         --repositories-file <f> apk repositories file
58

59
	__EOF__
60
	exit $1
61 62
}

63
QUIET_OPT="--quiet"
64 65
OPTS=$(getopt -l arch:,build-dir:,flavor:,feature:,help,package:,verbose,apk-pubkey:,hostkeys,compression:,media,repositories-file: \
	-n $SCRIPT -o a:b:f:F:hp:vk:KC:M -- "$@") || usage 1
66

67 68
eval set -- "$OPTS"
while :; do
69
	case "$1" in
70
	-a|--arch)
71 72 73
		shift
		ARCH=$1
		;;
74
	-b|--build-dir)
75 76 77
		shift
		BUILDDIR=$1
		;;
78
	-f|--flavor)
79 80 81
		shift
		FLAVOR=$1
		;;
82
	-F|--feature)
83 84 85
		shift
		features="$features $1"
		;;
86
	-h|--help)
87 88 89
		echo "$SCRIPT @VERSION@" >&2
		usage 0
		;;
90
	-p|--package)
91 92 93
		shift
		PACKAGES="$PACKAGES $1"
		;;
94
	-v|--verbose)
95 96
		QUIET_OPT=
		;;
97 98 99 100
	-k|--apk-pubkey)
		shift
		APK_PUBKEY="$1"
		;;
101
	-K|--hostkeys)
102 103
		MKINITFS_ARGS="$MKINITFS_ARGS -K"
		;;
104 105 106 107
	-C|--compression)
		shift
		MKINITFS_ARGS="$MKINITFS_ARGS -C $1"
		;;
108 109 110
	-M|--media)
		MEDIA=yes
		;;
111 112 113 114
	--repositories-file)
		shift
		REPOSITORIES_FILE=$1
		;;
115
	--)
116 117 118 119
		break
		;;
	esac
	shift
120 121 122 123 124
done

DESTDIR=$2


125
[ "$BUILDDIR" -a "$FLAVOR" ] && \
126
	usage 1 "Cannot specify both build directory and flavor"
127

128
if [ -z "$DESTDIR" ]; then
129 130 131 132 133 134 135 136 137 138 139 140
	[ "$ARCH" ] && \
		usage 1 "Cannot specify architecture when updating the current kernel"

	[ "$FLAVOR" ] && \
		usage 1 "Cannot specify flavor when updating the current kernel"

	[ "$SUPERUSER" ] || \
		usage 1 "Specify destination directory or run as superuser"

	while read MOUNT; do
		set -- $MOUNT
		[ $2 = /.modloop ] || continue
141
		DESTDIR=$(dirname $(busybox losetup $1 | cut -d " " -f 3))
142 143 144 145 146 147 148 149
		MNTDIR=$(dirname "$DESTDIR")
		break
	done < /proc/mounts

	if [ -z "$MNTDIR" ]; then
		error "Module loopback device not mounted"
		exit 1
	fi
150 151 152
fi

remount() {
153
	mount $1 -o remount "$MNTDIR"
154 155 156 157
}


ignore_sigs() {
158
	trap "" $SIGNALS
159 160 161
}

clean_up() {
162 163
	set +e
	ignore_sigs
164

165 166 167
	if [ "$SUPERUSER" ] && [ -z "$FAKEROOTKEY" ]; then
		apk del $QUIET_OPT $VIRTUAL
	fi
168
	rm -fr $TMPDIR
169 170 171 172 173
}

trap clean_up EXIT $SIGNALS


174
if [ "$SUPERUSER" ] && [ -z "$FAKEROOTKEY" ]; then
175
	apk add $QUIET_OPT --update-cache -t $VIRTUAL mkinitfs squashfs-tools kmod
176
fi
177

178
if [ -z "$features" ]; then
179
	. /etc/mkinitfs/mkinitfs.conf
180
fi
181

182
if [ -z "$FLAVOR" ]; then
183 184
	FLAVOR=$(uname -r | cut -d - -f 3-)
	[ "$FLAVOR" ] || FLAVOR=vanilla
185 186
fi

187 188
[ "$ARCH" ] || ARCH=$(apk --print-arch)

189
TMPDIR=$(mktemp -d /tmp/$SCRIPT.XXXXXX)
190
ROOT=$TMPDIR/root
191
BOOT=$ROOT/boot
192

193
_apk() {
194 195 196
	local cmd=$1
	shift

197
	apk $cmd $QUIET_OPT -p $ROOT --arch "$ARCH" \
198 199
		--keys-dir /etc/apk/keys \
		--repositories-file "$REPOSITORIES_FILE" $*
200 201
}

202
extra_pkgs() {
203 204 205 206
	local res=$(_apk search -x $1)
	if [ "$res" ]; then
		echo $*
	fi
207 208
}

209
# set up the root and get the APKINDEX for search
210
_apk add --initdb --update-cache
211

212
if [ "$BUILDDIR" ]; then
213 214 215 216 217 218 219 220 221 222
	case "$ARCH" in
		arm*|aarch64*)	_install="zinstall dtbs_install" ;;
		*)		_install="install" ;;
	esac

	mkdir -p $BOOT
	make -C "$BUILDDIR" $_install firmware_install modules_install \
		INSTALL_MOD_PATH=$ROOT \
		INSTALL_PATH=$BOOT \
		INSTALL_DTBS_PATH='$ROOT/usr/lib/linux-$(KERNELRELEASE)'
223
else
224 225 226 227 228
	if [ -z "$PACKAGES" ]; then
		PACKAGES="$(extra_pkgs "dahdi-linux-$FLAVOR" dahdi-linux)
			$(extra_pkgs "xtables-addons-$FLAVOR")"
	fi
	PACKAGES="$PACKAGES linux-$FLAVOR linux-firmware"
229
fi
230
_apk add --no-scripts alpine-base $PACKAGES
231

232 233 234 235
if [ -n "$APK_PUBKEY" ]; then
	mkdir -p "$ROOT"/etc/apk/keys
	cp "$APK_PUBKEY" "$ROOT"/etc/apk/keys/
fi
236

237 238
KVER_FLAVOR=
[ "$FLAVOR" = vanilla ] || KVER_FLAVOR=-$FLAVOR
239
KVER=$(basename $(ls -d $ROOT/lib/modules/*"$KVER_FLAVOR"))
240
DTBDIR=$ROOT/usr/lib/linux-$KVER
241
depmod -b $ROOT "$KVER"
242 243 244 245


STAGING=$TMPDIR/boot
MODLOOP=$TMPDIR/modloop
246
MODIMG=modloop-$FLAVOR
247

248
mkdir $MODLOOP $STAGING
249
cp -a $ROOT/lib/modules $MODLOOP
250 251
mkdir -p $MODLOOP/modules/firmware
find $ROOT/lib/modules -type f -name "*.ko" | xargs modinfo -F firmware | sort -u | while read FW; do
252 253 254
	if [ -e "$ROOT/lib/firmware/$FW" ]; then
		install -pD $ROOT/lib/firmware/$FW $MODLOOP/modules/firmware/$FW
	fi
255
done
256
mksquashfs $MODLOOP "$STAGING/$MODIMG" -comp xz -exit-on-error
257

258
mkinitfs $MKINITFS_ARGS -q -b $ROOT -F "$features base squashfs" \
259
	-o "$STAGING/initramfs-$FLAVOR" "$KVER"
260 261

for file in System.map config vmlinuz; do
262
	cp "$BOOT/$file$KVER_FLAVOR" $STAGING
263
done
264

265
if [ "$MNTDIR" ]; then
266 267 268
	ignore_sigs
	umount /.modloop
	remount -w
269
fi
270

271 272
mkdir -p "$DESTDIR"/${MEDIA:+boot/}
mv $STAGING/* "$DESTDIR"/${MEDIA:+boot/}
273

274
if [ -d "$DTBDIR" ]; then
275
	_opwd=$PWD
276 277 278 279
	case "$MEDIA,$FLAVOR" in
	yes,rpi*) _dtb="$DESTDIR/" ;;
	yes,*)    _dtb="$DESTDIR/boot/dtbs" ;;
	*,*)      _dtb="$DESTDIR/dtbs" ;;
280 281 282 283 284 285
	esac
	mkdir -p "$_dtb"
	_dtb=$(realpath "$_dtb")
	cd "$DTBDIR"
	find -type f \( -name "*.dtb" -o -name "*.dtbo" \) | cpio -pudm "$_dtb" 2> /dev/null
	cd "$_opwd"
286 287
fi

288
if [ "$MNTDIR" ]; then
289 290 291 292
	set +e
	sync
	remount -r
	mount -o loop "$DESTDIR/$MODIMG" /.modloop
293
fi
294 295

exit 0