This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
forked from mvo5/spike-tools
-
Notifications
You must be signed in to change notification settings - Fork 2
/
inject-kernel.sh
executable file
·129 lines (108 loc) · 2.9 KB
/
inject-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
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
#!/bin/bash
#
# Use this script to quickly inject a new kernel from a deb into the
# kernel snap.
#
# Example:
# $ ./inject-kernel -o kernel.snap ../kernel_214.snap linux-image.deb linux-modules.deb
source /usr/share/initramfs-tools/hook-functions
set -e
unsquash() {
echo "Unsquashing $kernel_snap..."
unsquashfs -d "$rootdir" "$kernel_snap"
rm "$rootdir"/vmlinuz*
rm "$rootdir"/initrd.img-*
}
extract() {
echo "Extracting initramfs..."
unmkinitramfs "$rootdir/initrd.img" "$fsdir"
}
extract_kernel() {
echo "Extracting kernel..."
mkdir "$tmpdir"/kernel
local dir="$PWD"
(cd "$tmpdir"/kernel
ar x "$dir/$kernel_deb"
tar xf data.tar.*
ar x "$dir/$modules_deb"
tar xf data.tar.*
)
kversion=$(basename "$tmpdir"/kernel/lib/modules/*)
echo "Kernel version is $kversion"
echo "Updating snap modules..."
rm -Rf "$rootdir"/lib/modules/[0-9]*
cp -rap "$tmpdir/kernel/lib/modules/$kversion" "$rootdir/lib/modules/"
}
add_kernel() {
cp "$tmpdir"/kernel/boot/vmlinuz-* "$rootdir/kernel.img"
ln -s kernel.img "$rootdir/vmlinuz"
ln -s kernel.img "$rootdir/vmlinuz-$kversion"
rm "$rootdir"/config-*
cp "$tmpdir/kernel/boot/config-$kversion" "$rootdir"/
}
add_modules() {
if [ ! -d "$tmpdir/fs/main/lib/modules/$kversion" ]; then
mv "$tmpdir"/fs/main/lib/modules/* "$tmpdir/fs/main/lib/modules/$kversion"
fi
for i in $(find "$tmpdir/fs/main/lib/modules/" -name "*.ko"); do
local m=$(echo $i | sed -e "s@$tmpdir/fs/main/lib/modules/[^/]*/@@")
if [ -f "$tmpdir/kernel/lib/modules/$kversion/$m" ]; then
cp "$tmpdir/kernel/lib/modules/$kversion/$m" "$i"
fi
done
depmod -b "$fsdir/main" "$kversion"
depmod -b "$rootdir" "$kversion"
}
repack() {
echo "Repacking initramfs..."
(cd "$fsdir/early"; find . | cpio -H newc -o) > "$rootdir/initrd.img"
(cd "$fsdir/main"; find . | cpio -H newc -o | gzip -c) >> "$rootdir/initrd.img"
ln -s initrd.img "$rootdir/initrd.img-$kversion"
}
resquash() {
if [ -z "$output" ]; then
num=1
while [ -f "$kernel_snap.$num" ]; do
num=$((num + 1))
done
output="$kernel_snap.$num"
fi
mksquashfs "$rootdir" "$output" -noappend -comp gzip -no-xattrs -no-fragments
echo "Created $output"
}
usage() {
echo "Usage: $0 [-o output] <kernel snap> <linux-image deb> <linux-modules deb>"
exit
}
while getopts "ho:" opt; do
case "${opt}" in
o)
output="$OPTARG"
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ $# -lt 3 ]; then
usage
fi
kernel_snap="$1"
kernel_deb="$2"
modules_deb="$3"
tmpdir=$(mktemp -d -t inject-XXXXXXXXXX)
rootdir="$tmpdir/root"
fsdir="$tmpdir/fs"
function finish {
echo "Cleaning up"
rm -Rf "$tmpdir"
}
trap finish EXIT
unsquash
extract
extract_kernel
add_kernel
add_modules
repack
resquash