aboutsummaryrefslogtreecommitdiffhomepage
path: root/content/2019-10-27-kernel-debugging-qemu/build_initrd.sh
blob: fd82990c3005cfdbfd0bd8af50783ca6612cb4d0 (plain) (blame)
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
#!/bin/bash

if test $(id -u) -ne 0; then
    SUDO=sudo
fi

set -e

BUSYBOX=busybox-1.33.1
INITRD=$PWD/initramfs.cpio.gz

## Build busybox

echo "[+] configure & build $BUSYBOX ..."
[[ ! -d $BUSYBOX ]] && {
    wget https://busybox.net/downloads/$BUSYBOX.tar.bz2
    bunzip2 $BUSYBOX.tar.bz2 && tar xf $BUSYBOX.tar
}

cd $BUSYBOX
make defconfig
sed -i 's/# CONFIG_STATIC .*/CONFIG_STATIC=y/' .config
make -j4 busybox
make install

## Create initrd

echo "[+] create initrd $INITRD ..."

cd _install

# 1. create initrd folder structure
mkdir -p bin sbin etc proc sys usr/bin usr/sbin dev

# 2. create init process
cat <<EOF > init
#!/bin/sh

mount -t proc none /proc
mount -t sysfs none /sys

exec setsid cttyhack sh
EOF
chmod +x init

# 3. create device nodes
$SUDO mknod dev/tty c 5 0
$SUDO mknod dev/tty0 c 4 0
$SUDO mknod dev/ttyS0 c 4 64

# 4. created compressed initrd
find . -print0                      \
    | cpio --null -ov --format=newc \
    | gzip -9 > $INITRD