update-kernel.in 5.84 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 17
SUPERUSER=
[ $(id -u) -eq 0 ] && SUPERUSER=Y

18
ARCH=
19
BUILDDIR=
20
FLAVOR=
21
MNTDIR=
22
PACKAGES=
23 24
MKINITFS_ARGS=
REPOSITORIES_FILE=/etc/apk/repositories
25 26 27 28
SIGNALS="HUP INT TERM"
TMPDIR=
features=

29 30 31 32 33 34 35
error() {
    echo "$SCRIPT: $1" >&2
}

usage() {
    [ "$2" ] && error "$2"
    local opts="[-F <feature>]... [-p <package>]..."
36
    local dest_args="[-a <arch>] <dest_dir>"
37 38
    cat >&2 <<EOF

39 40 41
Syntax: $SCRIPT $opts [$dest_args]
        $SCRIPT -f <flavor> $opts $dest_args
        $SCRIPT -b <build_dir> $opts [$dest_args]
42

43 44
Options: -a|--arch <arch>        Install kernel for specified architecture
         -b|--build <build_dir>  Install custom-built kernel
45 46 47
         -f|--flavor <flavor>    Install kernel of specified flavor
         -F|--feature <feature>  Enable initfs feature
         -p|--package <package>  Additional module or firmware package
48
         -v|--verbose            Verbose output
49 50
         -K|--hostkeys           Include host keys in initramfs
         --repositories-file <f> apk repositories file
51 52 53 54 55

EOF
    exit $1
}

56
QUIET_OPT="--quiet"
57 58
OPTS=$(getopt -l arch:,build-dir:,flavor:,feature:,help,package:,verbose,hostkeys,repositories-file: \
	      -n $SCRIPT -o a:b:f:F:hp:vK -- "$@") || usage 1
59 60 61
eval set -- "$OPTS"
while :; do
    case "$1" in
62 63 64 65
	-a|--arch)
	    shift
	    ARCH=$1
	    ;;
66
	-b|--build-dir)
67
	    shift
68
	    BUILDDIR=$1
69
	    ;;
70
	-f|--flavor)
71
	    shift
72
	    FLAVOR=$1
73 74
	    ;;
	-F|--feature)
75
	    shift
76
	    features="$features $1"
77
	    ;;
78
	-h|--help)
79
	    echo "$SCRIPT @VERSION@" >&2
80 81
	    usage 0
	    ;;
82
	-p|--package)
83
	    shift
84
	    PACKAGES="$PACKAGES $1"
85 86 87
	    ;;
	-v|--verbose)
	    QUIET_OPT=
88
	    ;;
89 90 91 92 93 94 95
	-K|--hostkeys)
	    MKINITFS_ARGS="$MKINITFS_ARGS -K"
	    ;;
        --repositories-file)
	    shift
	    REPOSITORIES_FILE=$1
	    ;;
96 97 98 99
	--)
	    break
	    ;;
    esac
100
    shift
101 102 103 104 105
done

DESTDIR=$2


106
[ "$BUILDDIR" -a "$FLAVOR" ] && \
107
    usage 1 "Cannot specify both build directory and flavor"
108

109
if [ -z "$DESTDIR" ]; then
110 111 112
    [ "$ARCH" ] && \
	usage 1 "Cannot specify architecture when updating the current kernel"

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

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

119 120 121 122
    while read MOUNT; do
	set -- $MOUNT
	[ $2 = /.modloop ] || continue
	DESTDIR=$(dirname $(losetup $1 | cut -d " " -f 3))
123
	MNTDIR=$(dirname "$DESTDIR")
124 125 126
	break
    done < /proc/mounts

127 128 129 130
    if [ -z "$MNTDIR" ]; then
	error "Module loopback device not mounted"
	exit 1
    fi
131 132 133
fi

remount() {
134
    mount $1 -o remount "$MNTDIR"
135 136 137 138 139 140 141 142 143 144 145
}


ignore_sigs() {
    trap "" $SIGNALS
}

clean_up() {
    set +e
    ignore_sigs

146
    [ "$SUPERUSER" ] && apk del $QUIET_OPT $VIRTUAL
147
    rm -fr $TMPDIR
148 149 150 151 152
}

trap clean_up EXIT $SIGNALS


153
if [ "$SUPERUSER" ]; then
154
    apk add $QUIET_OPT --update-cache -t $VIRTUAL mkinitfs squashfs-tools kmod
155
fi
156

157 158 159
if [ -z "$features" ]; then
    . /etc/mkinitfs/mkinitfs.conf
fi
160

161
if [ -z "$FLAVOR" ]; then
162 163
    FLAVOR=$(uname -r | cut -d - -f 3-)
    [ "$FLAVOR" ] || FLAVOR=vanilla
164 165
fi

166 167
[ "$ARCH" ] || ARCH=$(apk --print-arch)

168
TMPDIR=$(mktemp -d /tmp/$SCRIPT.XXXXXX)
169
ROOT=$TMPDIR/root
170
BOOT=$ROOT/boot
171
WRAPPER=
172 173

_exec() {
174
    $WRAPPER "$@"
175
}
176

177
_apk() {
178 179
    local cmd=$1
    shift
180 181 182 183 184 185 186 187 188 189 190 191

    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

192
    $wrapper apk $cmd $QUIET_OPT -p $ROOT --arch "$ARCH" \
193
	--keys-dir /etc/apk/keys \
194
	--repositories-file "$REPOSITORIES_FILE" $*
195 196
}

197 198 199
extra_pkgs() {
    local res=$(_apk search -x $1)
    if [ "$res" ]; then
200
	echo $*
201 202 203
    fi
}

204
# set up the root and get the APKINDEX for search
205
_apk add --initdb --update-cache
206

207
if [ "$BUILDDIR" ]; then
208 209 210 211 212
    case "$ARCH" in
    arm*|aarch64*)	_install="zinstall dtbs_install" ;;
    *)			_install="install" ;;
    esac

213
    mkdir -p $BOOT
214 215 216
    make -C "$BUILDDIR" $_install firmware_install modules_install \
	INSTALL_MOD_PATH=$ROOT \
	INSTALL_PATH=$BOOT \
217
	INSTALL_DTBS_PATH='$ROOT/usr/lib/linux-$(KERNELRELEASE)'
218 219
else
    if [ -z "$PACKAGES" ]; then
220 221
	PACKAGES="$(extra_pkgs "dahdi-linux-$FLAVOR" dahdi-linux)
		$(extra_pkgs "xtables-addons-$FLAVOR")"
222
    fi
223
    PACKAGES="$PACKAGES linux-$FLAVOR linux-firmware"
224
fi
225
_apk add --no-scripts alpine-base $PACKAGES
226

227

228 229
KVER_FLAVOR=
[ "$FLAVOR" = vanilla ] || KVER_FLAVOR=-$FLAVOR
230
KVER=$(basename $(ls -d $ROOT/lib/modules/*"$KVER_FLAVOR"))
231
DTBDIR=$ROOT/usr/lib/linux-$KVER
232
depmod -b $ROOT "$KVER"
233 234 235 236


STAGING=$TMPDIR/boot
MODLOOP=$TMPDIR/modloop
237
MODIMG=modloop-$FLAVOR
238
DTB_STAGING=$TMPDIR/dtbs
239

240
mkdir $DTB_STAGING $MODLOOP $STAGING
241
cp -a $ROOT/lib/modules $MODLOOP
242 243
mkdir -p $MODLOOP/modules/firmware
find $ROOT/lib/modules -type f -name "*.ko" | xargs modinfo -F firmware | sort -u | while read FW; do
Timo Teräs's avatar
Timo Teräs committed
244 245 246
	if [ -e "$ROOT/lib/firmware/$FW" ]; then
		install -pD $ROOT/lib/firmware/$FW $MODLOOP/modules/firmware/$FW
	fi
247
done
248
_exec mksquashfs $MODLOOP "$STAGING/$MODIMG" -comp xz
249

250
_exec mkinitfs $MKINITFS_ARGS -q -b $ROOT -F "$features base squashfs" \
251
    -o "$STAGING/initramfs-$FLAVOR" "$KVER"
252 253 254 255

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

257 258
if [ "$MNTDIR" ]; then
    ignore_sigs
259
    umount /.modloop
260 261
    remount -w
fi
262

263
mv $STAGING/* "$DESTDIR"
264

265
if [ -d "$DTBDIR" ]; then
266
    _opwd=$PWD
267 268 269 270 271 272 273 274 275
    case "$FLAVOR" in
    rpi*) _dtb="$DESTDIR" ;;
    *) _dtb="$DESTDIR/dtbs" ;;
    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"
276 277
fi

278 279 280 281
if [ "$MNTDIR" ]; then
    set +e
    sync
    remount -r
282
    mount -o loop "$DESTDIR/$MODIMG" /.modloop
283
fi
284 285

exit 0