Skip to content

Instantly share code, notes, and snippets.

@lighta
Created May 31, 2019 04:00
Show Gist options
  • Save lighta/de2805682252e471ba64c41030b73cc6 to your computer and use it in GitHub Desktop.
Save lighta/de2805682252e471ba64c41030b73cc6 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This script is to build a busybox system
# Requirement
# fedora : curl, ncurses-devel, qemu-system-$BUSYBOX_ARCH
# author : MMoreau
# uncomment next lines for debugging (logging)
#logfile=/tmp/build_busybox.log
#exec 1> >(tee $logfile) 2>&1
#configuration that could be changed
BUSYBOX_VERSION=1.26.2
BUSYBOX_ARCH=x86
LINUX_KERNEL_BRANCH=4
LINUX_KERNEL_SUB_VERSION=10.6
STAGE=$HOME/tl
#end editable config
#preapare working env
LINUX_KERNEL_FULL_VERSION=$LINUX_KERNEL_BRANCH.$LINUX_KERNEL_SUB_VERSION
TOP=$STAGE/teeny-linux
mkdir -p $STAGE
#Download required pkg
cd $STAGE
if [ ! -d busybox-$BUSYBOX_VERSION ]; then
echo "Downloading busybox"
curl https://busybox.net/downloads/busybox-$BUSYBOX_VERSION.tar.bz2 | tar xjf -
fi
if [ ! -d linux-$LINUX_KERNEL_FULL_VERSION ]; then
echo "Downloading linux kernel"
curl https://cdn.kernel.org/pub/linux/kernel/v$LINUX_KERNEL_BRANCH.x/linux-$LINUX_KERNEL_FULL_VERSION.tar.xz | tar xJf -
fi
#Create minimal userland
echo "Creating minimal userland"
cd $STAGE/busybox-$BUSYBOX_VERSION
mkdir -pv $TOP/obj/busybox-$BUSYBOX_ARCH
make O=$TOP/obj/busybox-$BUSYBOX_ARCH defconfig
#Enable static linking
echo "Enabling static linking"
make O=$TOP/obj/busybox-$BUSYBOX_ARCH menuconfig
#Press enter on Busybox Settings --->
#Press the down arrow until you hit Build BusyBox as a static binary (no shared libs).
#Press Y
#Select Exit twice and hit Enter to Save (cursor will be on Yes).
#build busybox
echo "Compiling busybox"
cd $TOP/obj/busybox-$BUSYBOX_ARCH
make -j4
make install
#build initramfs structure
echo "Setup initramfs"
mkdir -pv $TOP/initramfs/$BUSYBOX_ARCH-busybox
cd $TOP/initramfs/$BUSYBOX_ARCH-busybox
mkdir -pv {bin,dev,sbin,etc,proc,sys/kernel/debug,usr/{bin,sbin},lib,lib64,mnt/root,root}
cp -av $TOP/obj/busybox-$BUSYBOX_ARCH/_install/* $TOP/initramfs/$BUSYBOX_ARCH-busybox
sudo cp -av /dev/{null,console,tty,sda1} $TOP/initramfs/$BUSYBOX_ARCH-busybox/dev/
#build init file
echo "build init file"
cat > $TOP/initramfs/$BUSYBOX_ARCH-busybox/init <<\EOL
#!/bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
mount -t debugfs none /sys/kernel/debug
echo -e "\nBoot took $(cut -d' ' -f1 /proc/uptime) seconds\n"
exec /bin/sh
...
EOL
chmod +x $TOP/initramfs/$BUSYBOX_ARCH-busybox/init
#create initramfs image
cd $TOP/initramfs/$BUSYBOX_ARCH-busybox
find . | cpio -H newc -o > ../initramfs.cpio
cd ..
cat initramfs.cpio | gzip > $TOP/obj/initramfs.igz
#check content
mkdir $STAGE/checkarchive
cd $STAGE/checkarchive
zcat $TOP/obj/initramfs.igz | cpio -idmv --no-absolute-filenames
#recreate archive with checked
cd $STAGE/checkarchive
find . | cpio -H newc -o > ../checked-initramfs.cpio
cd ..
cat checked-initramfs.cpio | gzip > $TOP/obj/checked-initramfs.igz
#kernel
#Config the kernel with the minimal config
cd $STAGE/linux-$LINUX_KERNEL_FULL_VERSION
make O=$TOP/obj/linux-$BUSYBOX_ARCH-allnoconfig allnoconfig
#Re-enable config options for QEMU
cd $STAGE/linux-$LINUX_KERNEL_FULL_VERSION
make O=$TOP/obj/linux-$BUSYBOX_ARCH-allnoconfig nconfig
#[*] 64-bit kernel
#-> General setup
# -> Configure standard kernel features (expert user)
#[*] Enable support for printk
#-> General setup
#[*] Initial RAM filesystem and RAM disk (initramfs/initrd) support
#-> Executable file formats / Emulations
#[*] Kernel support for ELF binaries
#[*] Kernel support for scripts starting with #!
#-> Device Drivers
# -> Character devices
#[*] Enable TTY
#-> Device Drivers
# -> Character devices
# -> Serial drivers
#[*] 8250/16550 and compatible serial support
#[*] Console on 8250/16550 and compatible serial port
#-> File systems
# -> Pseudo filesystems
#[*] /proc file system support
#[*] sysfs file system support
#-> Kernel hacking
# -> Compile-time checks and compiler options
#[*] Debug filesystem
#-> Kernel hacking
#[*] Early printk
#compile kernel
cd $STAGE/linux-$LINUX_KERNEL_FULL_VERSION
make O=$TOP/obj/linux-$BUSYBOX_ARCH-allnoconfig -j4
#run QEMu
#qemu-system-x86_64 \
# -kernel $TOP/obj/linux-x86-allnoconfig/arch/x86/boot/bzImage \
# -initrd $TOP/obj/initramfs.igz \
# -nographic -append "earlyprintk=serial,ttyS0 console=ttyS0"
cd $STAGE/linux-$LINUX_KERNEL_FULL_VERSION
make O=$TOP/obj/linux-x86-allnoconfig nconfig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment