update-kernel.in 3.63 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=

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
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"
	    ;;
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
59
60
61
	-h|--help)
	    usage 0
	    ;;
62
63
64
65
66
67
68
69
70
71
72
73
74
	-p|--package)
	    PACKAGES="$PACKAGES $2"
	    ;;
	--)
	    break
	    ;;
    esac
    shift 2
done

DESTDIR=$2


75
[ "$BUILDDIR" -a "$FLAVOR" ] && \
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
76
    usage 1 "Cannot specify both build directory and flavor"
77

78
if [ -z "$DESTDIR" ]; then
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
79
80
    [ "$FLAVOR" ] && \
	usage 1 "Cannot specify flavor when updating the current kernel"
81

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

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
90
91
92
93
    if [ -z "$MNTDIR" ]; then
	error "Module loopback device not mounted"
	exit 1
    fi
94
95
96
fi

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


ignore_sigs() {
    trap "" $SIGNALS
}

clean_up() {
    set +e
    ignore_sigs

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

trap clean_up EXIT $SIGNALS


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

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

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

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

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

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

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

149
150
151
152
153
154
155
156
157
158
159
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
160
fi
161

162

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


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

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

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


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

189
mv "$STAGING"/* "$DESTDIR"
190

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

exit 0