-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstall_kernel.sh
executable file
·83 lines (66 loc) · 1.91 KB
/
install_kernel.sh
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
#!/bin/sh
# Copyright (C) 2023, John Clark <[email protected]>
set -e
main() {
local kerndeb="$(ls ../kernel/linux-image-*.deb | sort | tail -n1)"
if [ ! -e "$kerndeb" ]; then
echo 'kernel .deb file not found'
exit 1
fi
local imgfile="${1:-mmc_2g.img}"
if [ ! -e "$imgfile" ]; then
echo "error: \"$imgfile\" file not found"
exit 2
fi
kerndeb="$(realpath "$kerndeb")" # /foo/bar/kern.deb
local kerndir="$(dirname "$kerndeb")" # /foo/bar
local kernfile="$(basename "$kerndeb")" # kern.deb
local kernver="$(echo "$kernfile" | sed -rn 's/linux-image-(.*)_[[:digit:]].*/\1/p')"
print_hdr "mounting image $imgfile"
echo "image: $imgfile -> mount: $mountpt"
mkdir -p "$mountpt"
mount -vno loop,offset=16M "$imgfile" "$mountpt"
mount -vo bind "$kerndir" "$mountpt/mnt"
mount -vt proc '/proc' "$mountpt/proc"
mount -vt sysfs '/sys' "$mountpt/sys"
mount -vo bind '/dev' "$mountpt/dev"
mount -vo bind '/dev/pts' "$mountpt/dev/pts"
print_hdr "installing kernel $kernver"
chroot "$mountpt" "/usr/bin/dpkg" -i "/mnt/$kernfile"
}
print_hdr() {
local msg="$1"
echo "\n${h1}$msg...${rst}"
}
# ensure mount points get cleaned up
on_exit() {
local uml
for ep in 'dev/pts' 'dev' 'sys' 'proc' 'mnt' ''; do
mountpoint -q "$mountpt/$ep" && uml="$uml $mountpt/$ep"
done
if [ -n "$uml" ]; then
print_hdr "unmounting"
for ep in $uml; do
umount -v "$ep"
done
fi
rm -rf "$mountpt"
}
mountpt='rootfs'
trap on_exit EXIT INT QUIT ABRT TERM
rst='\033[m'
bld='\033[1m'
red='\033[31m'
grn='\033[32m'
yel='\033[33m'
blu='\033[34m'
mag='\033[35m'
cya='\033[36m'
h1="${blu}==>${rst} ${bld}"
if [ 0 -ne $(id -u) ]; then
echo 'this script must be run as root'
echo " run: ${bld}${grn}sudo sh $(basename $0)${rst}\n"
exit 9
fi
cd "$(dirname "$(realpath "$0")")"
main "$@"