-
Notifications
You must be signed in to change notification settings - Fork 7
/
custom-kernel-gen
executable file
·102 lines (83 loc) · 2.55 KB
/
custom-kernel-gen
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
#!/bin/bash
set -e
trap '
ret=$?;
set +e;
if [[ $ROOTFS ]]; then
rm -rf $ROOTFS
fi
if [[ $ret -ne 0 ]]; then
echo FAILED TO GENERATE CUSTOM KERNEL >&2
fi
exit $ret;
' EXIT
# clean up after ourselves no matter how we die.
trap 'exit 1;' SIGINT
usage() {
echo "
Usage: $(basename $0) [OPTIONS] [dir]
Pack a kernel to be installed in another board
OPTIONS
-x generate auto-extract script
-c [J|z|zstd] pass flag to tar while packing
ARGUMENTS
dir directory with the build. If not specified, CWD is used
" 1>&$1;
exit $(($1 - 1));
}
SCRIPT_DIR=$(dirname $(realpath ${BASH_SOURCE[0]}))
OPT_AUTOEXTRACT=0
OPT_COMPRESS="zstd"
EXT=""
while getopts "xc:h" o; do
case "${o}" in
x) OPT_AUTOEXTRACT=1; shift ;;
c) OPT_COMPRESS=${OPTARG}; shift 2 ;;
h) usage 1; shift ;;
\?) usage 2; shift ;;
esac
done
OPT_UNCOMPRESS=$OPT_COMPRESS
if [ $# -gt 0 ]; then
cd $1
fi
case "$OPT_COMPRESS" in
J) EXT="tar.xz"; OPT_COMPRESS=-J;;
z) EXT="tar.gz"; OPT_COMPRESS=-z;;
zstd) EXT="tar.zstd"; OPT_COMPRESS=--zstd;;
"") EXT=".tar";;
\?) usage 2;;
esac
KVER=$(make kernelrelease)
ROOTFS=$(mktemp -d --tmpdir custom-kernel.XXXXXXXX)
# workaround for make modules_install installing in /lib, which will be created
# as a dir if the directory didn't exit
mkdir -p $ROOTFS/usr/lib/modules
ln -s usr/lib $ROOTFS/lib
make modules_install INSTALL_MOD_PATH=$ROOTFS
# on target this will mean nothing
rm -f $ROOTFS/usr/lib/modules/$KVER/build || true
rm -f $ROOTFS/usr/lib/modules/$KVER/source || true
# copy the kernel image over to /usr/lib/custom-kernel - only one kernel will
# be kept there... user is supposed to call kernel-install on target (which
# copies this kernel to /boot) before installing another kernel
mkdir -p $ROOTFS/usr/lib/custom-kernel/
cp arch/x86/boot/bzImage $ROOTFS/usr/lib/custom-kernel/vmlinuz
cp System.map $ROOTFS/usr/lib/custom-kernel/
echo $KVER > $ROOTFS/usr/lib/custom-kernel/VERSION
# target doesn't need the workaround, so now remove it
rm $ROOTFS/lib
if [ $OPT_AUTOEXTRACT -eq 1 ]; then
s=${PWD}/linux-${KVER}${EXT}.sh
sed "s/^OPT_COMPRESS=.*/OPT_COMPRESS=$OPT_UNCOMPRESS/" $SCRIPT_DIR/custom-kernel-install > "$s"
echo -e "\nexit 0" >> "$s"
echo -e "\n__CUSTOM_KERNEL_BELLOW__" >> "$s"
( cd $ROOTFS && tar -c ${OPT_COMPRESS} * >> "$s" )
chmod +x "$s"
else
D=${PWD}
s=linux-${KVER}-config.xz
( cd $ROOTFS && tar -cf $D/linux-${KVER}${EXT} ${OPT_COMPRESS} * )
xz --stdout .config > "$s"
fi
echo "Done: $s"