aboutsummaryrefslogblamecommitdiffhomepage
path: root/content/2019-10-27-kernel-debugging-qemu/build_initrd.sh
blob: f5bece403f8d8c0409a90972848d8a160252892a (plain) (tree)
1
2
3
4
5
6
7
8
9

           



                            

      
                      












                                                       
                                  
















                                                   
                           




                       
                              


                                     
#!/bin/bash

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

set -e

BUSYBOX=busybox-1.36.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 -j$(nproc --ignore=2) 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
mount -t devtmpfs none /dev

exec setsid cttyhack sh
EOF
chmod +x init

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