summaryrefslogtreecommitdiff
path: root/update-kernel.in
blob: 74dbe7515967766ca6058b9c128c8e297da4a1fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/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

BUILDDIR=
FLAVOR=
MNTDIR=
PACKAGES=
SIGNALS="HUP INT TERM"
TMPDIR=
features=

OPTS=$(getopt -l build-dir:,flavor:,feature:,package: -n $SCRIPT -o b:f:F:p: \
    -- "$@")
eval set -- "$OPTS"
while :; do
    case "$1" in
	-b|--build-dir)
	    BUILDDIR=$2
	    ;;
	-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
}

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

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

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

    [ "$MNTDIR" ] || error "Module loopback device not mounted"
fi

remount() {
    mount $1 -o remount "$MNTDIR"
}


ignore_sigs() {
    trap "" $SIGNALS
}

clean_up() {
    set +e
    ignore_sigs

    apk del -q $VIRTUAL
    rm -fr "$TMPDIR"
}

trap clean_up EXIT $SIGNALS


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

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

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

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

_apk() {
    local cmd=$1
    shift
    apk $cmd -p "$ROOT" --keys-dir /etc/apk/keys \
	--repositories-file /etc/apk/repositories $*
}

add_pkgs() {
    _apk add -q --no-scripts $*
}

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

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
fi


KVER_FLAVOR=
[ "$FLAVOR" = vanilla ] || KVER_FLAVOR=-$FLAVOR
KVER=$(basename $(ls -d "$ROOT"/lib/modules/*"$KVER_FLAVOR"))
depmod -b "$ROOT" "$KVER"


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

mkdir "$MODLOOP" "$STAGING"
cp -a "$ROOT/lib/modules" "$MODLOOP"
cp -a "$ROOT/lib/firmware" "$MODLOOP/modules"
mksquashfs "$MODLOOP" "$STAGING/$MODIMG" -comp xz

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


if [ "$MNTDIR" ]; then
    ignore_sigs
    remount -w
    rm -f "$DESTDIR/$MODIMG"
fi

mv "$STAGING"/* "$DESTDIR"

if [ "$MNTDIR" ]; then
    set +e
    sync
    remount -r
fi

exit 0