Repository navigation
Expand file tree
/
Copy pathextract.sh
More file actions
executable file
·46 lines (40 loc) · 2.12 KB
/
Copy pathextract.sh
File metadata and controls
executable file
·46 lines (40 loc) · 2.12 KB
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
#!/bin/sh
# Downloads kernel packages and lays the kernel out as /out/boot/vmlinuz and /out/lib/modules/<release>.
# Runs inside the build stage of the Dockerfile.
set -eu
mkdir -p /pkg /root.x /out/boot /out/lib/modules
for url in "$@"; do
file=/pkg/$(basename "$url")
echo ">> $url"
curl -fsSL --retry 5 --retry-delay 10 --retry-all-errors -o "$file" "$url"
case "$file" in
*.rpm) (cd /root.x && rpm2cpio "$file" | cpio -idm --quiet) ;;
*.deb) dpkg-deb -x "$file" /root.x ;;
*) echo "unsupported package: $file" >&2; exit 1 ;;
esac
done
# Packages keep modules either in /lib/modules or in /usr/lib/modules.
modules=$(find /root.x/lib/modules /root.x/usr/lib/modules -mindepth 1 -maxdepth 1 -type d 2>/dev/null | head -n 1)
[ -n "$modules" ] || { echo "no modules directory in the packages" >&2; exit 1; }
release=$(basename "$modules")
# The kernel image is /boot/vmlinuz-<release> (el7, Ubuntu) or /lib/modules/<release>/vmlinuz (el8 and newer).
vmlinuz=$(find /root.x/boot "$modules" -maxdepth 1 -name 'vmlinuz*' 2>/dev/null | head -n 1)
[ -n "$vmlinuz" ] || { echo "no kernel image in the packages" >&2; exit 1; }
cp "$vmlinuz" /out/boot/vmlinuz
# Hardware drivers are useless in a VM and make up most of the size. Paravirtualized ones are kept.
cd "$modules"
rm -f vmlinuz System.map config symvers.gz
rm -rf build source vdso
if [ -d kernel/drivers ]; then
find kernel/drivers -type f | grep -v -E '/(virtio|block/virtio|net/virtio|net/veth|net/dummy|net/bonding|net/vxlan|net/tun|net/macvlan|net/ipvlan|char/virtio|char/hw_random|scsi/virtio|block/loop|block/nbd|md)[^/]*(/|\.ko)' | xargs rm -f
fi
rm -rf kernel/sound kernel/drivers/gpu kernel/drivers/media
# Modules are stored uncompressed, so that any module loader can handle them.
find . -name '*.ko.xz' -exec xz -d {} +
find . -name '*.ko.zst' -exec zstd -d -q --rm {} +
find . -name '*.ko.gz' -exec gzip -d {} +
find . -type d -empty -delete
mv "$modules" /out/lib/modules/"$release"
depmod -b /out "$release"
echo "$release" > /out/boot/release
echo ">> kernel $release, $(find /out/lib/modules -name '*.ko' | wc -l) modules, $(du -sh /out | cut -f1)"