update-kernel.in 3.01 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/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

15
BUILDDIR=
16
FLAVOR=
17
MNTDIR=
18 19 20 21 22
PACKAGES=
SIGNALS="HUP INT TERM"
TMPDIR=
features=

23 24
OPTS=$(getopt -l build-dir:,flavor:,feature:,package: -n $SCRIPT -o b:f:F:p: \
    -- "$@")
25 26 27
eval set -- "$OPTS"
while :; do
    case "$1" in
28 29 30
	-b|--build-dir)
	    BUILDDIR=$2
	    ;;
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
	-f|--flavor)
	    FLAVOR=$2
	    ;;
	-F|--feature)
	    features="$features $2"
	    ;;
	-p|--package)
	    PACKAGES="$PACKAGES $2"
	    ;;
	--)
	    break
	    ;;
    esac
    shift 2
done

DESTDIR=$2


error() {
    echo "$1" >&2
    exit 1
}
54

55 56 57
[ "$BUILDDIR" -a "$FLAVOR" ] && \
    error "Cannot specify both build directory and flavor"

58
if [ -z "$DESTDIR" ]; then
59 60
    [ "$FLAVOR" ] && error "Cannot specify flavor when updating the current kernel"

61 62 63 64
    while read MOUNT; do
	set -- $MOUNT
	[ $2 = /.modloop ] || continue
	DESTDIR=$(dirname $(losetup $1 | cut -d " " -f 3))
65
	MNTDIR=$(dirname "$DESTDIR")
66 67 68
	break
    done < /proc/mounts

69
    [ "$MNTDIR" ] || error "Module loopback device not mounted"
70 71 72
fi

remount() {
73
    mount $1 -o remount "$MNTDIR"
74 75 76 77 78 79 80 81 82 83 84
}


ignore_sigs() {
    trap "" $SIGNALS
}

clean_up() {
    set +e
    ignore_sigs

85
    apk del -q $VIRTUAL
86
    rm -fr "$TMPDIR"
87 88 89 90 91
}

trap clean_up EXIT $SIGNALS


92
apk add -qU -t $VIRTUAL mkinitfs squashfs-tools
93

94 95 96
if [ -z "$features" ]; then
    . /etc/mkinitfs/mkinitfs.conf
fi
97

98
if [ -z "$FLAVOR" ]; then
99 100
    FLAVOR=$(uname -r | cut -d - -f 3-)
    [ "$FLAVOR" ] || FLAVOR=vanilla
101 102
fi

103 104
TMPDIR=$(mktemp -dt $SCRIPT.XXXXXX)
ROOT=$TMPDIR/root
105
BOOT=$ROOT/boot
106

107
_apk() {
108 109 110
    local cmd=$1
    shift
    apk $cmd -p "$ROOT" --keys-dir /etc/apk/keys \
111 112 113
	--repositories-file /etc/apk/repositories $*
}

114 115 116 117 118 119 120 121 122 123 124
add_pkgs() {
    _apk add -q --no-scripts $*
}

extra_pkgs() {
    local res=$(_apk search -x $1)
    if [ "$res" ]; then
	add_pkgs $*
    fi
}

125 126 127 128 129 130 131 132 133 134 135
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
136
fi
137

138

139 140 141 142
KVER_FLAVOR=
[ "$FLAVOR" = vanilla ] || KVER_FLAVOR=-$FLAVOR
KVER=$(basename $(ls -d "$ROOT"/lib/modules/*"$KVER_FLAVOR"))
depmod -b "$ROOT" "$KVER"
143 144 145 146 147 148


STAGING=$TMPDIR/boot
MODLOOP=$TMPDIR/modloop
MODIMG=$FLAVOR.modloop.squashfs

149 150 151 152
mkdir "$MODLOOP" "$STAGING"
cp -a "$ROOT/lib/modules" "$MODLOOP"
cp -a "$ROOT/lib/firmware" "$MODLOOP/modules"
mksquashfs "$MODLOOP" "$STAGING/$MODIMG" -comp xz
153

154 155
mkinitfs -q -b "$ROOT" -F "$features base squashfs" -o "$STAGING/$FLAVOR.gz" \
    "$KVER"
156
cp "$BOOT/vmlinuz$KVER_FLAVOR" "$STAGING/$FLAVOR"
157 158


159 160 161 162 163
if [ "$MNTDIR" ]; then
    ignore_sigs
    remount -w
    rm -f "$DESTDIR/$MODIMG"
fi
164

165
mv "$STAGING"/* "$DESTDIR"
166

167 168 169 170 171
if [ "$MNTDIR" ]; then
    set +e
    sync
    remount -r
fi
172 173

exit 0