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
/
Copy pathinject-initramfs.sh
executable file
·133 lines (113 loc) · 2.85 KB
/
inject-initramfs.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
129
130
131
132
#!/bin/bash
#
# Use this script to inject content from core-build/initramfs into an existing
# kernel snap.
#
# Example:
# $ ./inject-initramfs -o kernel.snap ../kernel_214.snap core-build/initramfs
source /usr/share/initramfs-tools/hook-functions
set -e
unsquash() {
echo "Unsquashing $kernel..."
unsquashfs -d "$rootdir" "$kernel"
}
extract() {
echo "Extracting initramfs..."
unmkinitramfs "$rootdir/initrd.img" "$fsdir"
}
inject() {
echo "Injecting $initramfs..."
cp -rap "$initramfs"/conf/* "$confdir"
cp -rap "$initramfs"/scripts/* "$scriptdir"
# rebuild script order cache
CONFDIR="$fsdir/main"
for b in $(cd "$scriptdir" && find . -mindepth 1 -type d); do
cache_run_scripts "$fsdir/main" "/scripts/${b#./}"
done
}
add_files() {
if [ "${#files[@]}" -gt 0 ]; then
for item in ${files[@]}; do
IFS=":" read -ra A <<< "$item"
destdir=${A[0]}
list=$(echo ${A[1]} | tr , '\n')
for f in $list; do
echo "Installing $f..."
mkdir -p "$fsdir/main/$destdir"
cp $f "$fsdir/main/$destdir"
done
done
fi
}
add_modules() {
kver=$(basename "$rootdir"/modules/*)
echo "Kernel version $kver"
if [ "${#modules[@]}" -gt 0 ]; then
for mod in ${modules[@]}; do
echo "Installing kernel module $mod..."
find "$rootdir/modules/$kver/" -name "$mod" -exec cp {} \
"$fsdir/main/lib/modules/$kver/kernel/drivers/" \;
done
depmod -b "$fsdir/main" "$kver"
fi
}
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"
}
resquash() {
if [ -z "$output" ]; then
num=1
while [ -f "$kernel.$num" ]; do
num=$((num + 1))
done
output="$kernel.$num"
fi
mksquashfs "$rootdir" "$output" -noappend -comp gzip -no-xattrs -no-fragments
echo "Created $output"
}
usage() {
echo "Usage: $0 [-o output] [-f destdir:srcfile] [-m module] <kernel snap> <initramfs>"
exit
}
while getopts "ho:f:m:" opt; do
case "${opt}" in
o)
output="$OPTARG"
;;
f)
files+=("$OPTARG")
;;
m)
modules+=("$OPTARG")
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ $# -lt 2 ]; then
usage
fi
kernel="$1"
initramfs="$2"
tmpdir=$(mktemp -d -t inject-XXXXXXXXXX)
rootdir="$tmpdir/root"
fsdir="$tmpdir/fs"
confdir="$fsdir"/main/conf/conf.d/
scriptdir="$fsdir"/main/scripts/
mkdir -p "$confdir" "$scriptdir"
function finish {
echo "Cleaning up"
rm -Rf "$tmpdir"
}
trap finish EXIT
unsquash
extract
inject
add_files
add_modules
repack
resquash