update-kernel.in 4.7 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 23 24 25 26
PACKAGES=
SIGNALS="HUP INT TERM"
TMPDIR=
features=

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

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

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

41 42
Options: -a|--arch <arch>        Install kernel for specified architecture
         -b|--build <build_dir>  Install custom-built kernel
43 44 45
         -f|--flavor <flavor>    Install kernel of specified flavor
         -F|--feature <feature>  Enable initfs feature
         -p|--package <package>  Additional module or firmware package
46
         -v|--verbose            Verbose output
47 48 49 50 51

EOF
    exit $1
}

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

DESTDIR=$2


95
[ "$BUILDDIR" -a "$FLAVOR" ] && \
96
    usage 1 "Cannot specify both build directory and flavor"
97

98
if [ -z "$DESTDIR" ]; then
99 100 101
    [ "$ARCH" ] && \
	usage 1 "Cannot specify architecture when updating the current kernel"

102 103
    [ "$FLAVOR" ] && \
	usage 1 "Cannot specify flavor when updating the current kernel"
104

105 106 107
    [ "$SUPERUSER" ] || \
	usage 1 "Specify destination directory or run as superuser"

108 109 110 111
    while read MOUNT; do
	set -- $MOUNT
	[ $2 = /.modloop ] || continue
	DESTDIR=$(dirname $(losetup $1 | cut -d " " -f 3))
112
	MNTDIR=$(dirname "$DESTDIR")
113 114 115
	break
    done < /proc/mounts

116 117 118 119
    if [ -z "$MNTDIR" ]; then
	error "Module loopback device not mounted"
	exit 1
    fi
120 121 122
fi

remount() {
123
    mount $1 -o remount "$MNTDIR"
124 125 126 127 128 129 130 131 132 133 134
}


ignore_sigs() {
    trap "" $SIGNALS
}

clean_up() {
    set +e
    ignore_sigs

135
    [ "$SUPERUSER" ] && apk del $QUIET_OPT $VIRTUAL
136
    rm -fr $TMPDIR
137 138 139 140 141
}

trap clean_up EXIT $SIGNALS


142
if [ "$SUPERUSER" ]; then
Natanael Copa's avatar
Natanael Copa committed
143
    apk add $QUIET_OPT --update-cache -t $VIRTUAL mkinitfs squashfs-tools
144
fi
145

146 147 148
if [ -z "$features" ]; then
    . /etc/mkinitfs/mkinitfs.conf
fi
149

150
if [ -z "$FLAVOR" ]; then
151 152
    FLAVOR=$(uname -r | cut -d - -f 3-)
    [ "$FLAVOR" ] || FLAVOR=vanilla
153 154
fi

155
TMPDIR=$(mktemp -d /tmp/$SCRIPT.XXXXXX)
156
ROOT=$TMPDIR/root
157
BOOT=$ROOT/boot
158
WRAPPER=
159 160

_exec() {
161
    $WRAPPER "$@"
162
}
163

164
_apk() {
165 166
    local cmd=$1
    shift
167 168 169 170 171 172 173 174 175 176 177 178

    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

179 180
    $wrapper apk $cmd $QUIET_OPT -p $ROOT ${ARCH:+--arch "$ARCH"} \
	--keys-dir /etc/apk/keys \
181 182 183
	--repositories-file /etc/apk/repositories $*
}

184 185 186
extra_pkgs() {
    local res=$(_apk search -x $1)
    if [ "$res" ]; then
187
	echo $*
188 189 190
    fi
}

191
# set up the root and get the APKINDEX for search
192
_apk add --initdb --update-cache
193

194
if [ "$BUILDDIR" ]; then
195
    mkdir -p $BOOT
196
    make -C "$BUILDDIR" firmware_install install modules_install \
197
	INSTALL_MOD_PATH=$ROOT INSTALL_PATH=$BOOT
198 199
else
    if [ -z "$PACKAGES" ]; then
200 201
	PACKAGES="$(extra_pkgs "dahdi-linux-$FLAVOR" dahdi-linux)
		$(extra_pkgs "xtables-addons-$FLAVOR")"
202
    fi
203
    PACKAGES="$PACKAGES linux-$FLAVOR linux-firmware"
204
fi
205
_apk add --no-scripts alpine-base $PACKAGES
206

207

208 209
KVER_FLAVOR=
[ "$FLAVOR" = vanilla ] || KVER_FLAVOR=-$FLAVOR
210 211
KVER=$(basename $(ls -d $ROOT/lib/modules/*"$KVER_FLAVOR"))
depmod -b $ROOT "$KVER"
212 213 214 215


STAGING=$TMPDIR/boot
MODLOOP=$TMPDIR/modloop
216
MODIMG=modloop-$FLAVOR
217

218 219 220
mkdir $MODLOOP $STAGING
cp -a $ROOT/lib/modules $MODLOOP
cp -a $ROOT/lib/firmware $MODLOOP/modules
221
_exec mksquashfs $MODLOOP "$STAGING/$MODIMG" -comp xz
222

223
_exec mkinitfs -q -b $ROOT -F "$features base squashfs" \
224
    -o "$STAGING/initramfs-$FLAVOR" "$KVER"
225 226 227 228

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


231 232
if [ "$MNTDIR" ]; then
    ignore_sigs
233
    umount /.modloop
234 235
    remount -w
fi
236

237
mv $STAGING/* "$DESTDIR"
238

239 240 241 242
if [ "$MNTDIR" ]; then
    set +e
    sync
    remount -r
243
    mount -o loop "$DESTDIR/$MODIMG" /.modloop
244
fi
245 246

exit 0