Skip to content

Instantly share code, notes, and snippets.

@SED4906
Created September 11, 2023 02:37
Show Gist options
  • Save SED4906/6acd1fdf399e17893a3b65d971485c09 to your computer and use it in GitHub Desktop.
Save SED4906/6acd1fdf399e17893a3b65d971485c09 to your computer and use it in GitHub Desktop.
A shell script to build a small ISO with just Linux and Busybox.
#!/bin/sh
set -ex
## Specific versions of packages
KERNEL_VERSION="6.1.52"
BUSYBOX_VERSION="1.36.1"
SYSLINUX_VERSION="6.03"
#LIMINE_VERSION="5.20230909.0"
## Download packages
if [ ! -d "packages" ]; then
mkdir packages
fi
wget -c -O packages/kernel-${KERNEL_VERSION}.tar.xz http://kernel.org/pub/linux/kernel/v$(echo $KERNEL_VERSION | cut -d. -f1).x/linux-${KERNEL_VERSION}.tar.xz
wget -c -O packages/busybox-${BUSYBOX_VERSION}.tar.bz2 http://busybox.net/downloads/busybox-${BUSYBOX_VERSION}.tar.bz2
wget -c -O packages/syslinux-${SYSLINUX_VERSION}.tar.xz http://kernel.org/pub/linux/utils/boot/syslinux/syslinux-${SYSLINUX_VERSION}.tar.xz
#wget -c -O packages/limine-${LIMINE_VERSION}.efi https://github.com/limine-bootloader/limine/raw/v${LIMINE_VERSION}-binary/BOOTX64.EFI
## Unpack sources
if [ ! -d "sources" ]; then
mkdir sources
fi
pushd sources
tar xvf ../packages/kernel-${KERNEL_VERSION}.tar.xz
tar xvf ../packages/busybox-${BUSYBOX_VERSION}.tar.bz2
tar xvf ../packages/syslinux-${SYSLINUX_VERSION}.tar.xz
popd
## Populate rootfs
if [ ! -d "rootfs" ]; then
mkdir rootfs
else
rm -rf rootfs/*
fi
pushd rootfs
mkdir boot dev etc proc sys usr
pushd usr
mkdir bin sbin
popd
ln -s usr/bin bin
ln -s usr/sbin sbin
popd
## Build Busybox
pushd sources/busybox-${BUSYBOX_VERSION}
make distclean defconfig
sed -i "s|.*CONFIG_STATIC.*|CONFIG_STATIC=y|" .config
sed -i "s|.*CONFIG_INSTALL_NO_USR.*|CONFIG_INSTALL_NO_USR=y|" .config
sed -i "s|.*CONFIG_LINUXRC.*|CONFIG_LINUXRC=n|" .config
echo "CONFIG_STATIC_LIBGCC=y" >> .config
make -j$(nproc) busybox
make install
popd
## Install Busybox
cp -P sources/busybox-${BUSYBOX_VERSION}/_install/bin/* rootfs/bin/
cp -P sources/busybox-${BUSYBOX_VERSION}/_install/sbin/* rootfs/sbin/
cp sources/busybox-${BUSYBOX_VERSION}/busybox rootfs/bin/
## Pack rootfs into isoimage
if [ ! -d "isoimage" ]; then
mkdir isoimage
else
rm -rf isoimage/*
fi
pushd rootfs
mkdir etc/init.d
cat << EOF > etc/init.d/rcS
#!/bin/sh
/bin/mount -t devtmpfs none /dev
/bin/mount -t proc proc /proc
/bin/mount -t sysfs none /sys
/bin/mount -o remount,rw /
/bin/mkdir -p /dev/pts
/bin/mkdir -p /dev/shm
/bin/mount -a
/bin/hostname -F /etc/hostname
EOF
chmod +x etc/init.d/rcS
cat << EOF > etc/fstab
LABEL=MINILIN /boot auto defaults 0 0
EOF
cat << EOF > etc/hostname
minilin
EOF
ln -s bin/busybox init
find . | cpio -R root:root -H newc -o | gzip > ../isoimage/rootfs.gz
popd
## Build Linux
pushd sources/linux-${KERNEL_VERSION}
make mrproper defconfig
make -j$(nproc) bzImage
popd
## Build ISO
cp sources/linux-${KERNEL_VERSION}/arch/x86/boot/bzImage isoimage/kernel.gz
cp sources/syslinux-${SYSLINUX_VERSION}/bios/core/isolinux.bin isoimage
cp sources/syslinux-${SYSLINUX_VERSION}/bios/com32/elflink/ldlinux/ldlinux.c32 isoimage
pushd isoimage
echo 'default kernel.gz initrd=rootfs.gz' > ./isolinux.cfg
xorriso \
-as mkisofs \
-V MINILIN \
-o ../minilin.iso \
-b isolinux.bin \
-c boot.cat \
-no-emul-boot \
-boot-load-size 4 \
-boot-info-table \
./
popd
set +ex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment