update-kernel.in 5.53 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
143
    apk add $QUIET_OPT --update-cache -t $VIRTUAL mkinitfs squashfs-tools kmod
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 156
[ "$ARCH" ] || ARCH=$(apk --print-arch)

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

_exec() {
163
    $WRAPPER "$@"
164
}
165

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

    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

181
    $wrapper apk $cmd $QUIET_OPT -p $ROOT --arch "$ARCH" \
182
	--keys-dir /etc/apk/keys \
183 184 185
	--repositories-file /etc/apk/repositories $*
}

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

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

196
if [ "$BUILDDIR" ]; then
197 198 199 200 201 202
    local _install
    case "$ARCH" in
    arm*|aarch64*)	_install="zinstall dtbs_install" ;;
    *)			_install="install" ;;
    esac

203
    mkdir -p $BOOT
204 205 206 207
    make -C "$BUILDDIR" $_install firmware_install modules_install \
	INSTALL_MOD_PATH=$ROOT \
	INSTALL_PATH=$BOOT \
	INSTALL_DTBS_PATH='$ROOT/usr/lib/linux-$(KERNELRELEASE)' \
208 209
else
    if [ -z "$PACKAGES" ]; then
210 211
	PACKAGES="$(extra_pkgs "dahdi-linux-$FLAVOR" dahdi-linux)
		$(extra_pkgs "xtables-addons-$FLAVOR")"
212
    fi
213
    PACKAGES="$PACKAGES linux-$FLAVOR linux-firmware"
214
fi
215
_apk add --no-scripts alpine-base $PACKAGES
216

217

218 219
KVER_FLAVOR=
[ "$FLAVOR" = vanilla ] || KVER_FLAVOR=-$FLAVOR
220
KVER=$(basename $(ls -d $ROOT/lib/modules/*"$KVER_FLAVOR"))
221
DTBDIR=$ROOT/usr/lib/linux-$KVER
222
depmod -b $ROOT "$KVER"
223 224 225 226


STAGING=$TMPDIR/boot
MODLOOP=$TMPDIR/modloop
227
MODIMG=modloop-$FLAVOR
228
DTB_STAGING=$TMPDIR/dtbs
229

230
mkdir $DTB_STAGING $MODLOOP $STAGING
231
cp -a $ROOT/lib/modules $MODLOOP
232 233
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
234 235 236
	if [ -e "$ROOT/lib/firmware/$FW" ]; then
		install -pD $ROOT/lib/firmware/$FW $MODLOOP/modules/firmware/$FW
	fi
237
done
238
_exec mksquashfs $MODLOOP "$STAGING/$MODIMG" -comp xz
239

240
_exec mkinitfs -q -b $ROOT -F "$features base squashfs" \
241
    -o "$STAGING/initramfs-$FLAVOR" "$KVER"
242 243 244 245

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

247 248
if [ "$MNTDIR" ]; then
    ignore_sigs
249
    umount /.modloop
250 251
    remount -w
fi
252

253
mv $STAGING/* "$DESTDIR"
254

255 256 257 258 259 260 261 262 263 264 265 266
if [ -d "$DTBDIR" ]; then
    local _opwd=$PWD
    local _dtb
    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"
267 268
fi

269 270 271 272
if [ "$MNTDIR" ]; then
    set +e
    sync
    remount -r
273
    mount -o loop "$DESTDIR/$MODIMG" /.modloop
274
fi
275 276

exit 0