update-kernel.in 3.64 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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
error() {
    echo "$SCRIPT: $1" >&2
}

usage() {
    [ "$2" ] && error "$2"
    local opts="[-F <feature>]... [-p <package>]..."
    cat >&2 <<EOF

Syntax: $SCRIPT $opts [<dest_dir>]
        $SCRIPT -f <flavor> $opts <dest_dir>
        $SCRIPT -b <build_dir> $opts [<dest_dir>]

Options: -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

EOF
    exit $1
}

OPTS=$(getopt -l build-dir:,flavor:,feature:,help,package: -n $SCRIPT \
    -o b:f:F:hp: -- "$@") || usage 1
47 48 49
eval set -- "$OPTS"
while :; do
    case "$1" in
50 51 52
	-b|--build-dir)
	    BUILDDIR=$2
	    ;;
53 54 55 56 57 58
	-f|--flavor)
	    FLAVOR=$2
	    ;;
	-F|--feature)
	    features="$features $2"
	    ;;
59
	-h|--help)
60
	    echo "$SCRIPT @VERSION@" >&2
61 62
	    usage 0
	    ;;
63 64 65 66 67 68 69 70 71 72 73 74 75
	-p|--package)
	    PACKAGES="$PACKAGES $2"
	    ;;
	--)
	    break
	    ;;
    esac
    shift 2
done

DESTDIR=$2


76
[ "$BUILDDIR" -a "$FLAVOR" ] && \
77
    usage 1 "Cannot specify both build directory and flavor"
78

79
if [ -z "$DESTDIR" ]; then
80 81
    [ "$FLAVOR" ] && \
	usage 1 "Cannot specify flavor when updating the current kernel"
82

83 84 85 86
    while read MOUNT; do
	set -- $MOUNT
	[ $2 = /.modloop ] || continue
	DESTDIR=$(dirname $(losetup $1 | cut -d " " -f 3))
87
	MNTDIR=$(dirname "$DESTDIR")
88 89 90
	break
    done < /proc/mounts

91 92 93 94
    if [ -z "$MNTDIR" ]; then
	error "Module loopback device not mounted"
	exit 1
    fi
95 96 97
fi

remount() {
98
    mount $1 -o remount "$MNTDIR"
99 100 101 102 103 104 105 106 107 108 109
}


ignore_sigs() {
    trap "" $SIGNALS
}

clean_up() {
    set +e
    ignore_sigs

110
    apk del -q $VIRTUAL
111
    rm -fr $TMPDIR
112 113 114 115 116
}

trap clean_up EXIT $SIGNALS


117
apk add -qU -t $VIRTUAL mkinitfs squashfs-tools
118

119 120 121
if [ -z "$features" ]; then
    . /etc/mkinitfs/mkinitfs.conf
fi
122

123
if [ -z "$FLAVOR" ]; then
124 125
    FLAVOR=$(uname -r | cut -d - -f 3-)
    [ "$FLAVOR" ] || FLAVOR=vanilla
126 127
fi

128
TMPDIR=$(mktemp -d /tmp/$SCRIPT.XXXXXX)
129
ROOT=$TMPDIR/root
130
BOOT=$ROOT/boot
131

132
_apk() {
133 134
    local cmd=$1
    shift
135
    apk $cmd -p $ROOT --keys-dir /etc/apk/keys \
136 137 138
	--repositories-file /etc/apk/repositories $*
}

139 140 141 142 143 144 145 146 147 148 149
add_pkgs() {
    _apk add -q --no-scripts $*
}

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

150 151
add_pkgs -U --initdb alpine-base $PACKAGES
if [ "$BUILDDIR" ]; then
152
    mkdir -p $BOOT
153
    make -C "$BUILDDIR" firmware_install install modules_install \
154
	INSTALL_MOD_PATH=$ROOT INSTALL_PATH=$BOOT
155 156 157 158 159 160
else
    add_pkgs "linux-$FLAVOR" linux-firmware
    if [ -z "$PACKAGES" ]; then
	extra_pkgs "dahdi-linux-$FLAVOR" dahdi-linux
	extra_pkgs "xtables-addons-$FLAVOR"
    fi
161
fi
162

163

164 165
KVER_FLAVOR=
[ "$FLAVOR" = vanilla ] || KVER_FLAVOR=-$FLAVOR
166 167
KVER=$(basename $(ls -d $ROOT/lib/modules/*"$KVER_FLAVOR"))
depmod -b $ROOT "$KVER"
168 169 170 171 172 173


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

174 175 176 177
mkdir $MODLOOP $STAGING
cp -a $ROOT/lib/modules $MODLOOP
cp -a $ROOT/lib/firmware $MODLOOP/modules
mksquashfs $MODLOOP $STAGING/$MODIMG -comp xz
178

179
mkinitfs -q -b $ROOT -F "$features base squashfs" -o "$STAGING/$FLAVOR.gz" \
180
    "$KVER"
181
cp "$BOOT/vmlinuz$KVER_FLAVOR" "$STAGING/$FLAVOR"
182 183


184 185
if [ "$MNTDIR" ]; then
    ignore_sigs
186
    umount /.modloop
187 188
    remount -w
fi
189

190
mv $STAGING/* "$DESTDIR"
191

192 193 194 195
if [ "$MNTDIR" ]; then
    set +e
    sync
    remount -r
196
    mount -o loop "$DESTDIR/$MODIMG" /.modloop
197
fi
198 199

exit 0