-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitramfs.sh
243 lines (202 loc) · 6.6 KB
/
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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/bash
KERNEL_VERSION=6.6.22
BUSYBOX_VERSION=1.36.1
function getkernel() {
version="./utils/kernel/linux-${KERNEL_VERSION}/"
if [ ! -e $version ]; then
wget -P ./utils/kernel/ https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-${KERNEL_VERSION}.tar.xz
tar -xvf ./utils/kernel/linux-${KERNEL_VERSION}.tar.xz -C ./utils/kernel/
else
echo "linux source already downloaded"
fi
# or simply make -f
cd ./utils/kernel/linux-${KERNEL_VERSION}/ || exit
make defconfig
#make kvmconfig removed after Linux 5.10
make kvm_guest.config
echo "kvm_guest.config done"
make olddefconfig
echo "olddefconfig done"
make bzImage
echo "bzImage done"
make -j"$(nproc)"
echo "final kernel make done"
cd - || return
}
function bubo() {
#dirbusy="../../utils/busybox/busybox-1.36.1.tar.bz2"
dirbusy="busybox-${BUSYBOX_VERSION}.tar.bz2"
if [ ! -e ./utils/busybox/${dirbusy} ]; then
wget -P ./utils/busybox/ https://www.busybox.net/downloads/busybox-${BUSYBOX_VERSION}.tar.bz2
tar -xvjf ./utils/busybox/${dirbusy} -C ./utils/busybox/
else
printf "\n===== busybox already downloaded =====\n"
fi
cd "./utils/busybox/busybox-${BUSYBOX_VERSION}" || exit
#cd ./busybox-1.36.1/ || exit
make defconfig
sed -i 's/^.*CONFIG_STATIC[^_].*$/CONFIG_STATIC=y/g' ./.config
if command -v musl-gcc &>/dev/null; then
make -j"$(nproc)" CC="musl-gcc -static" busybox || return
#busybox || return
#CC="musl-gcc -static" busybox || return
else
make -j"$(nproc)" busybox || return
fi
make install
cd - || return
}
function setbridge() {
sudo brctl addbr br0
sudo brctl addif br0 eth0
sudo ip link set dev br0 up
sudo brctl show
}
function distro_artifact() { #generates the initramfs ramdisk
# CI todo: copy bzImage artifact from context and place it into current
cp ./utils/kernel/linux-${KERNEL_VERSION}/arch/x86/boot/bzImage ./artifacts/
mkdir -p ./ramdisk/{bin,dev,etc,lib,mnt/root,proc,root,sbin,sys,tmp,var}
#cp ./utils/busybox/busybox-1.36.1/busybox ./ramdisk/
#for binary in $(./ramdisk/busybox --list); do
# ln -s /bin/busybox ./ramdisk/bin/"$binary"
# ln -s /bin/busybox ./ramdisk/sbin/"$binary"
# ln -s /bin/busybox ./ramdisk/usr/bin/"$binary"
# ln -s /bin/busybox ./ramdisk/usr/sbin/"$binary"
#done
# the previous loop didn't do it with sbin and usr
#
#
# CI todo: get busybox build from context and place it on current
cp -a ./utils/busybox/busybox-1.36.1/_install/* ./ramdisk/
#fakeroot sh -c '
cat > ./ramdisk/init <<"EOF"
#!/bin/busybox sh
mount -t devtmpfs devtmpfs /dev
mount -t proc none /proc
mount -t sysfs none /sys
mount -t tmpfs tmpfs /tmp
ip link set lo # Bring up loopback interface
ip link set eth0 # Bring up Ethernet interface (replace eth0 with your network interface name)
udhcpc -i eth0
sysctl -w kernel.printk="2 4 1 7"
echo && echo
cat << 'asciiart'
.-"``"-.
/ _.-` (_) `-._
\ (_.----._) /
\ / \ / _ _
`\ \____/ /` | | | |
`-.____.-` ____ _ _| | ____| | _
/ \ / _ ) | | | |/ _ | || \
/ \ ( (/ /| |_| | ( ( | | |_) )
/_ | | _\ \____)\____|_|\_||_|____/
| | | deomorxsy/eulab-poc
|__|__| ----------------------------------------------
/_ | _\ Reboot (01.00.0, r3500-0f87d95)
----------------------------------------------
asciiart
printf "Boot took $(cut -d' ' -f1 /proc/uptime) seconds btw\n"
# get a shell
sh
EOF
# get a shell with sh before the EOF if needed.
# exit fakeroot context
#exit
#'
# append ASCII art
#cat >> ./ramdisk/init <<"EOF"
#printf "\nASCIIart didn't go well...
#Boot took $(cut -d' ' -f1 /proc/uptime) seconds btw\n
#"
## get a shell
#sh
#EOF
chmod +x ./ramdisk/init
cd ./ramdisk/ || return
#find . -print0 | busybox cpio --null --create --verbose --format=newc | gzip --best > ./initramfz.cpio.gz
find . -print0 | busybox cpio --null -ov --format=newc | gzip -9 > ../artifacts/initramfs.cpio.gz
cd - || return
# ci todo: release as artifact
}
function sparseFile() {
SPARSE="./utils/storage/eulab-hd"
dd if=/dev/zero of=$SPARSE bs=1M count=2048
mkfs.ext4 $SPARSE
}
function virtstoraged() {
QCOW_FILE="./utils/storage/eulab.qcow2"
if [ ! -e $QCOW_FILE ]; then
echo "Creating qcow2 image..."
qemu-img create -f qcow2 $QCOW_FILE 1G
guestmount -a $QCOW_FILE -i --ro /mnt
elif [ -e $QCOW_FILE ]; then
echo "Mounting qcow2 image into /mnt..."
guestmount -a $QCOW_FILE -i --ro /mnt
fi
}
function qemuit() {
# run vm
# initramfs-custom2.img created with arch-mkinitcpio.
qemu-system-x86_64 \
-kernel ./artifacts/bzImage \
-initrd ./artifacts/initramfs.cpio.gz \
-m 1024 \
-append 'console=ttyS0 root=/dev/sda earlyprintk net.ifnames=0' \
-nographic \
-no-reboot \
-drive file=./utils/storage/eulab--hd,format=raw \
-net user,host=10.0.2.10,hostfwd=tcp:127.0.0.1:10021-:22 \
-net nic,model=e1000
#-netdev user,id=mynet0,net=192.168.76.0/24,dhcpstart=192.168.76.9,restrict=yes \
#-object filter-dump,id=f1,netdev=mynet0,file=dump.dat \
#-nic user,ipv6=off,model=rtl8139,mac=10:10:10:10:10:11
#--enable-kvm \
#-action panic=-1
}
function vacuum() {
KERNEL_ART_PATH="./utils/kernel/lin*"
BUBO_ART_PATH="./utils/busybox/busy*"
SPARSE_ART_PATH="./utils/storage/kernel-hd"
# kernel
if [ -e "$KERNEL_ART_PATH" ] || [ -d "$KERNEL_ART_PATH" ]; then
echo "Kernel assets found. Cleaning now..."
rm -rf "./utils/kernel/lin*"
else
echo "No build assets found in the kernel directory."
fi
# busybox
if [ -e "$BUBO_ART_PATH" ] || [ -d "$BUBO_ART_PATH" ]; then
echo "Busybox assets found. Cleaning now..."
rm -rf "./utils/busybox/busy*"
else
echo "No build assets found in the busybox directory"
fi
# sparse file
if [ -e $SPARSE_ART_PATH ]; then
shred -z -n 3 $SPARSE_ART_PATH
rm $SPARSE_ART_PATH
else
echo "No sparse file asset found in the storage directory"
fi
# qcow2 disk image
QCOW_FILE="./utils/storage/eulab.qcow2"
if [ -e "$QCOW_FILE" ]; then
guestunmount /mnt
shred -z -n 3 $QCOW_FILE
rm $QCOW_FILE
fi
}
# path to utils passed by the Makefile
#bubo "$1"
#
#
# Check the argument passed from the command line
#if [ "$1" == "bubo" ]; then
# bubo
#elif [ "$1" == "function2" ]; then
# function2
#elif [ "$1" == "function3" ]; then
# function3
#else
# echo "Invalid function name. Please specify one of: function1, function2, function3"
#fi