#!/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 EOF exit $1 } OPTS=$(getopt -l build-dir:,flavor:,feature:,help,package: -n $SCRIPT \ -o b:f:F:hp: -- "$@") || usage 1 eval set -- "$OPTS" while :; do case "$1" in -b|--build-dir) BUILDDIR=$2 ;; -f|--flavor) FLAVOR=$2 ;; -F|--feature) features="$features $2" ;; -h|--help) echo "$SCRIPT @VERSION@" >&2 usage 0 ;; -p|--package) PACKAGES="$PACKAGES $2" ;; --) break ;; esac shift 2 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 -q $VIRTUAL rm -fr $TMPDIR } trap clean_up EXIT $SIGNALS if [ "$SUPERUSER" ]; then apk add -qU -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=eval _exec() { $WRAPPER "$1" } _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 -p $ROOT --keys-dir /etc/apk/keys \ --repositories-file /etc/apk/repositories $* } add_pkgs() { _apk add -q --no-scripts $* } extra_pkgs() { local res=$(_apk search -x $1) if [ "$res" ]; then add_pkgs $* fi } add_pkgs -U --initdb alpine-base $PACKAGES if [ "$BUILDDIR" ]; then mkdir -p $BOOT make -C "$BUILDDIR" firmware_install install modules_install \ INSTALL_MOD_PATH=$ROOT INSTALL_PATH=$BOOT else add_pkgs "linux-$FLAVOR" linux-firmware if [ -z "$PACKAGES" ]; then extra_pkgs "dahdi-linux-$FLAVOR" dahdi-linux extra_pkgs "xtables-addons-$FLAVOR" fi fi 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