#!/bin/sh -e # update-kernel # # Kernel and firmware update script for Alpine installations set up # with setup-bootable # # Copyright (c) 2014 Timo Teräs # Copyright (c) 2014 Kaarle Ritvanen SCRIPT=update-kernel VIRTUAL=.tmp-$SCRIPT SUPERUSER= [ $(id -u) -eq 0 ] && SUPERUSER=Y BUILDDIR= FLAVOR= MNTDIR= PACKAGES= SIGNALS="HUP INT TERM" TMPDIR= features= error() { echo "$SCRIPT: $1" >&2 } usage() { [ "$2" ] && error "$2" local opts="[-F ]... [-p ]..." cat >&2 <] $SCRIPT -f $opts $SCRIPT -b $opts [] Options: -b|--build Install custom-built kernel -f|--flavor Install kernel of specified flavor -F|--feature Enable initfs feature -p|--package Additional module or firmware package -v|--verbose Verbose output EOF exit $1 } QUIET_OPT="--quiet" OPTS=$(getopt -l build-dir:,flavor:,feature:,help,package:,verbose -n $SCRIPT \ -o b:f:F:hp:v -- "$@") || usage 1 eval set -- "$OPTS" while :; do case "$1" in -b|--build-dir) BUILDDIR=$2 shift ;; -f|--flavor) FLAVOR=$2 shift ;; -F|--feature) features="$features $2" shift ;; -h|--help) echo "$SCRIPT @VERSION@" >&2 usage 0 ;; -p|--package) PACKAGES="$PACKAGES $2" shift ;; -v|--verbose) QUIET_OPT= ;; --) break ;; esac shift done DESTDIR=$2 [ "$BUILDDIR" -a "$FLAVOR" ] && \ usage 1 "Cannot specify both build directory and flavor" if [ -z "$DESTDIR" ]; then [ "$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 DESTDIR=$(dirname $(losetup $1 | cut -d " " -f 3)) MNTDIR=$(dirname "$DESTDIR") break done < /proc/mounts if [ -z "$MNTDIR" ]; then error "Module loopback device not mounted" exit 1 fi fi remount() { mount $1 -o remount "$MNTDIR" } ignore_sigs() { trap "" $SIGNALS } clean_up() { set +e ignore_sigs [ "$SUPERUSER" ] && apk del $QUIET_OPT $VIRTUAL rm -fr $TMPDIR } trap clean_up EXIT $SIGNALS if [ "$SUPERUSER" ]; then apk add $QUIET_OPT --update-cache -t $VIRTUAL mkinitfs squashfs-tools fi if [ -z "$features" ]; then . /etc/mkinitfs/mkinitfs.conf fi if [ -z "$FLAVOR" ]; then FLAVOR=$(uname -r | cut -d - -f 3-) [ "$FLAVOR" ] || FLAVOR=vanilla fi TMPDIR=$(mktemp -d /tmp/$SCRIPT.XXXXXX) ROOT=$TMPDIR/root BOOT=$ROOT/boot WRAPPER= _exec() { $WRAPPER "$@" } _apk() { local cmd=$1 shift local wrapper= if [ -z "$SUPERUSER" ]; then local opt= local fake_env=$TMPDIR/fake-env if [ -f $fake_env ]; then opt="-i $fake_env" WRAPPER="fakeroot $opt --" fi wrapper="fakeroot $opt -s $fake_env --" fi $wrapper apk $cmd $QUIET_OPT -p $ROOT --keys-dir /etc/apk/keys \ --repositories-file /etc/apk/repositories $* } extra_pkgs() { local res=$(_apk search -x $1) if [ "$res" ]; then echo $* fi } # set up the root and get the APKINDEX for search _apk add --initdb --update-cache if [ "$BUILDDIR" ]; then mkdir -p $BOOT make -C "$BUILDDIR" firmware_install install modules_install \ INSTALL_MOD_PATH=$ROOT INSTALL_PATH=$BOOT else if [ -z "$PACKAGES" ]; then PACKAGES="$(extra_pkgs "dahdi-linux-$FLAVOR" dahdi-linux) $(extra_pkgs "xtables-addons-$FLAVOR")" fi PACKAGES="$PACKAGES linux-$FLAVOR linux-firmware" fi _apk add --no-scripts alpine-base $PACKAGES KVER_FLAVOR= [ "$FLAVOR" = vanilla ] || KVER_FLAVOR=-$FLAVOR KVER=$(basename $(ls -d $ROOT/lib/modules/*"$KVER_FLAVOR")) depmod -b $ROOT "$KVER" STAGING=$TMPDIR/boot MODLOOP=$TMPDIR/modloop MODIMG=$FLAVOR.modloop.squashfs mkdir $MODLOOP $STAGING cp -a $ROOT/lib/modules $MODLOOP cp -a $ROOT/lib/firmware $MODLOOP/modules _exec mksquashfs $MODLOOP "$STAGING/$MODIMG" -comp xz _exec mkinitfs -q -b $ROOT -F "$features base squashfs" \ -o "$STAGING/$FLAVOR.gz" "$KVER" cp "$BOOT/vmlinuz$KVER_FLAVOR" "$STAGING/$FLAVOR" if [ "$MNTDIR" ]; then ignore_sigs umount /.modloop remount -w fi mv $STAGING/* "$DESTDIR" if [ "$MNTDIR" ]; then set +e sync remount -r mount -o loop "$DESTDIR/$MODIMG" /.modloop fi exit 0