-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkboot.sh
executable file
·165 lines (130 loc) · 4.5 KB
/
kboot.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/bash
# kboot | build the kernel for dev/hacking
# Written by ngn (https://ngn.tf) (2024)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
## import common scripts
if [ ! -d "common" ]; then
echo "You should run this script inside the repo's directory"
exit 1
fi
source common/log.sh
source common/util.sh
source common/depends.sh
## check QEMU/KVM
qemu="qemu-system-x86_64"
if ! command -v $qemu &> /dev/null; then
qemu="kvm"
fi
if ! command -v $qemu &> /dev/null; then
error "Failed to find x86_64 QEMU/KVM"
exit 1
fi
## check & load args
if [ -z "$1" ]; then
error "Please specify a kernel version, for example:"
print " $0 5.15.135"
exit 1
fi
if [ "${1}" != "staging" ]; then
load_version "${1}"
check_ret "Invalid version number"
fi
VERSION="${1}"
INITFS="${2}"
if [ -z "${2}" ]; then
INITFS="builds/initramfs.cpio.gz"
elif [ ! -z "${2}" ] && [ ! -f "${2}" ]; then
error "Failed access the initramfs, check the path you specified"
exit 1
fi
INITFS="$(realpath $INITFS)"
BZIMG="builds/${VERSION}/arch/x86_64/boot/bzImage"
if [ ! -f "$BZIMG" ]; then
error "Failed to find the bzImage for the specified version, have you built it yet?"
exit 1
fi
rm -rf builds/temp
rm -f builds/initramfs_temp.cpio.gz
## build the initfs
if [ ! -f "$INITFS" ]; then
old_pwd="${PWD}"
BUILD_DIR="builds/busybox"
rm -rf "$BUILD_DIR"
mkdir -p $BUILD_DIR
cd $BUILD_DIR
info "Downloading busybox archive"
download https://www.busybox.net/downloads/busybox-1.36.1.tar.bz2
info "Downloading busybox archive signature"
download https://www.busybox.net/downloads/busybox-1.36.1.tar.bz2.sig
gpg --receive-keys 47B70C55ACC9965B # https://busybox.net/~vda/vda_pubkey.gpg
gpg --verify "busybox-1.36.1.tar.bz2.sig" 2> /dev/null
check_ret "Signature verification failed, archive is corrupted or the upstream is hijacked"
success "Signature verification was successful"
info "Extracting busybox"
tar xf "busybox-1.36.1.tar.bz2"
check_ret "Failed to extract the busybox archive"
mv "busybox-1.36.1/"* .
rm -r "busybox-1.36.1"
rm "busybox-1.36.1.tar.bz2"
rm "busybox-1.36.1.tar.bz2.sig"
#cp "${old_pwd}/common/busybox.in" Config.in
info "Creating the default config"
make defconfig
check_ret "Failed to create the default config"
echo "CONFIG_STATIC=y" >> .config
info "Starting the build at $(date +%T)"
SECONDS=0
make -j$(nproc)
check_ret "Build failed"
passed=$SECONDS
success "Completed build at $(date +%T) ($($passed / 60)m and $($passed % 60)s)"
info "Creating the root filesystem for the initramfs"
mkdir -p temproot/proc
mkdir -p temproot/lib
mkdir -p temproot/dev
mkdir -p temproot/sys
mkdir -p temproot/bin
mkdir -p temproot/tmp
install -m755 "${old_pwd}/files/init.sh" temproot/init
cp busybox temproot/bin
pushd temproot/bin > /dev/null
for c in $(./busybox --list); do
ln -s /bin/busybox ./$c
done
popd > /dev/null
pushd temproot > /dev/null
info "Creating the initramfs with cpio and gzip"
find . | cpio -o --format=newc | gzip -9 > "${old_pwd}/builds/initramfs.cpio.gz"
check_ret "Failed to create the initramfs archive"
popd > /dev/null
success "Successfuly created the initramfs archive"
cd "${old_pwd}"
fi
mkdir -p builds/temp
pushd builds/temp > /dev/null
zcat "${INITFS}" | cpio -i
check_ret "Failed to extract the initramfs archive"
popd > /dev/null
pushd "builds/${VERSION}" > /dev/null
make INSTALL_MOD_PATH=../temp modules_install
check_ret "Failed to install kernel modules"
popd > /dev/null
pushd builds/temp > /dev/null
find . | cpio -o --format=newc | gzip -9 > "../initramfs_temp.cpio.gz"
check_ret "Failed to rebuild the initramfs archive"
popd > /dev/null
info "Booting the kernel with the initramfs using QEMU/KVM"
$qemu -kernel "${BZIMG}" -initrd builds/initramfs_temp.cpio.gz \
-nographic -append "console=ttyS0 nokaslr" -s
check_ret "Failed to boot using QEMU/KVM"
rm builds/initramfs_temp.cpio.gz
rm -r builds/temp