Skip to content

Instantly share code, notes, and snippets.

@sulincix
Last active June 12, 2021 17:52
Show Gist options
  • Save sulincix/63b28ee674410d600d7c73a1f36c898a to your computer and use it in GitHub Desktop.
Save sulincix/63b28ee674410d600d7c73a1f36c898a to your computer and use it in GitHub Desktop.
Minimal linux live iso creator
#!/usr/bin/make -f
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option)
# any later version.
#
KERNEL_VERSION=5.11.6
BUSYBOX_VERSION=1.33.0
help:
: Usage: make [help/fetch/build/install/clean/reset/dist-clean]
: help: write this message
: fetch: download source archives
: build: build source
: install: install sources and create iso file
: clean: remove source files and installation
: reset: remove only installation
: dist-clean: remove all
all: fetch build
fetch: archives sources
# Fetch archice files
archives:
mkdir archives
wget -O archives/linux.tar.xz http://kernel.org/pub/linux/kernel/v5.x/linux-$(KERNEL_VERSION).tar.xz
wget -O archives/busybox.tar.bz2 http://busybox.net/downloads/busybox-$(BUSYBOX_VERSION).tar.bz2
# Extract archive files
sources:
mkdir sources
cd sources ; tar -xvf ../archives/linux.tar.xz
cd sources ; tar -xvf ../archives/busybox.tar.bz2
# Clean and dist-clean functions.
clean: reset
rm -rvf sources
reset:
rm -rfv isowork rootfs initramfs
make build_rootfs
dist-clean: clean
rm -rvf archives
# Build functions
build: build_rootfs build_busybox build_linux
build_initramfs:
mkdir -p initramfs/bin initramfs/lib initramfs/hooks
build_rootfs: build_isowork build_initramfs
mkdir -p rootfs/bin rootfs/lib rootfs/etc rootfs/dev rootfs/sys rootfs/proc rootfs/run rootfs/tmp
build_isowork:
mkdir -p isowork/boot/grub/
build_busybox:
make defconfig -C sources/busybox-$(BUSYBOX_VERSION)
sed -i "s|.*CONFIG_STATIC.*|CONFIG_STATIC=y|" sources/busybox-$(BUSYBOX_VERSION)/.config
make busybox install -C sources/busybox-$(BUSYBOX_VERSION)/ -j`nproc`
build_linux:
# Enable all modules and embed into vmlinuz
#make mrproper allyesconfig -C sources/linux-$(KERNEL_VERSION)/ -j`nproc`
# Use defconfig
make mrproper defconfig -C sources/linux-$(KERNEL_VERSION)/ -j`nproc`
# enable some modules
for config in CONFIG_OVERLAY_FS CONFIG_SQUASHFS CONFIG_HFSPLUS_FS ; do \
sed -i "s/# $$config is not set/$$config=y/g" sources/linux-$(KERNEL_VERSION)/.config ; \
done
make bzImage -C sources/linux-$(KERNEL_VERSION)/ -j`nproc`
make modules -C sources/linux-$(KERNEL_VERSION)/ -j`nproc`
# install_functions
install: install_busybox install_linux install_rootfs install_isowork
install_initramfs:
echo '#!/bin/busybox ash' > initramfs/init
echo '/bin/busybox --install -s /bin' >> initramfs/init
echo 'set -xe' >> initramfs/init
echo 'mkdir /dev /sys /proc /run /tmp || true' >> initramfs/init
echo 'mount -t devtmpfs none /dev' >> initramfs/init
echo 'mount -t proc none /proc' >> initramfs/init
echo 'mount -t sysfs none /sys' >> initramfs/init
echo 'for hook in $$(ls /hooks) ; do' >> initramfs/init
echo ' ash /hooks/$$hook || true' >> initramfs/init
echo 'done' >> initramfs/init
echo 'setsid cttyhack /bin/sh' >> initramfs/init
install_busybox:
install sources/busybox-$(BUSYBOX_VERSION)/busybox initramfs/bin/busybox
install sources/busybox-$(BUSYBOX_VERSION)/busybox rootfs/bin/busybox
rootfs/bin/busybox --list | grep -v busybox | while read applet ; do \
cp /bin/busybox rootfs/bin/$$applet ; \
done
MODDIR=lib/modules/$(KERNEL_VERSION)
WORKDIR=initramfs/
install_linux:
install sources/linux-$(KERNEL_VERSION)/arch/x86/boot/bzImage isowork/linux
make -C sources/linux-$(KERNEL_VERSION) modules_install INSTALL_MOD_PATH="$$(pwd)/rootfs" INSTALL_MOD_STRIP=1
mkdir -p $(WORKDIR)/$(MODDIR) || true
cp -prvf rootfs/$(MODDIR)/kernel/{crypto,fs,lib,block} $(WORKDIR)/$(MODDIR) || true
cp -prvf rootfs/$(MODDIR)/kernel/drivers/input/{keyboard,serio} $(WORKDIR)/$(MODDIR) || true
cp -prvf rootfs/$(MODDIR)/kernel/drivers/{ata,md,mmc,firewire} $(WORKDIR)/$(MODDIR) || true
cp -prvf rootfs/$(MODDIR)/kernel/drivers/{scsi,pcmcia,virtio,cdrom} $(WORKDIR)/$(MODDIR) || true
cp -prvf rootfs/$(MODDIR)/kernel/drivers/usb/ $(WORKDIR)/$(MODDIR) || true
cp -prvf rootfs/$(MODDIR)/kernel/drivers/acpi/ $(WORKDIR)/$(MODDIR) || true
# Some kernels not have this directories.
cp -prvf ${MODDIR}/kernel/drivers/{block,cdrom}/ $(WORKDIR)/$(MODDIR) || true
echo '#!/bin/busybox ash' > initramfs/hooks/00-modules
echo 'find /lib/modules/*/fs/ -type f | sed "s/.*\///g" | sed "s/^/modprobe /g" | sed "s/\.ko//g" | sed "s/$$/ &/g" > /fs' >> initramfs/hooks/00-modules
echo 'sh /fs &>/dev/null' >> initramfs/hooks/00-modules
echo 'find /lib/modules/*/crypto/ -type f | sed "s/.*\///g" | sed "s/^/modprobe /g" | sed "s/\.ko//g" | sed "s/$$/ &/g" > /crypto' >> initramfs/hooks/00-modules
echo 'sh /crypto &>/dev/null' >> initramfs/hooks/00-modules
echo 'cd /$(MODDIR)' >> initramfs/hooks/00-modules
echo 'find crypto lib block ata md firewire scsi \' >> initramfs/hooks/00-modules
echo 'message pcmcia virtio host storage \' >> initramfs/hooks/00-modules
echo '-type f 2> /dev/null | sed "s/.*\///g" | grep "\.ko" | sed "s/.ko.*/ &/g" | sed "s/^/modprobe /g"> /load_modules.sh' >> initramfs/hooks/00-modules
echo 'sh /load_modules.sh &>/dev/null' >> initramfs/hooks/00-modules
echo 'mdev -s &>/dev/null' >> initramfs/hooks/00-modules
install_rootfs: install_initramfs
# Live hook
echo '#!/bin/busybox ash' > initramfs/hooks/99-live
echo 'set -xe' >> initramfs/hooks/99-live
echo 'mkdir /rootfs /source /a /b /live-root' >> initramfs/hooks/99-live
echo 'while [ "$$root" == "" ] ; do' >> initramfs/hooks/99-live
echo ' for disk in $$(ls /sys/class/block/ | grep ".*[0-9]$$" | grep -v loop | grep -v ram | grep -v nbd | sed "s|^|/dev/|g") ; do' >> initramfs/hooks/99-live
echo ' mount -o ro $$disk /tmp || true' >> initramfs/hooks/99-live
echo ' [ -f /tmp/main.sfs ] && root=$$disk' >> initramfs/hooks/99-live
echo ' umount -lf /tmp || true' >> initramfs/hooks/99-live
echo ' sleep 0.3' >> initramfs/hooks/99-live
echo ' done' >> initramfs/hooks/99-live
echo 'done' >> initramfs/hooks/99-live
echo 'mount $$root /source' >> initramfs/hooks/99-live
echo 'mount /source/main.sfs /live-root' >> initramfs/hooks/99-live
echo 'mount -t overlay -o lowerdir=/live-root/,upperdir=/a/,workdir=/b overlay /rootfs' >> initramfs/hooks/99-live
echo 'for dir in dev sys proc tmp run ; do' >> initramfs/hooks/99-live
echo ' mount --bind $$dir /rootfs/$$dir' >> initramfs/hooks/99-live
echo 'done' >> initramfs/hooks/99-live
echo 'exec chroot /rootfs /bin/init' >> initramfs/hooks/99-live
# profile file
echo '#!/bin/ash' > rootfs/etc/profile
echo 'echo -e "\033[32;1mWelcome to minimal linux-$$(uname -r) live\033[0m"' >> rootfs/etc/profile
echo 'echo -e "Build source code: \033[31;1m/etc/build-live.mk\033[0m"' >> rootfs/etc/profile
install Makefile rootfs/etc/build-live.mk
install_isowork:
# fallback service manager
echo "#!/bin/ash" > rootfs/bin/init
echo "clear" >> rootfs/bin/init
echo "source /etc/profile" >> rootfs/bin/init
echo "setsid cttyhack /bin/sh" >> rootfs/bin/init
chmod 755 rootfs/bin/init
chmod 755 initramfs/hooks/*
chmod 755 initramfs/init
cd initramfs ; find . | cpio -R root:root -H newc -o > ../isowork/initramfs
echo 'menuentry minimal {' > isowork/boot/grub/grub.cfg
echo ' linux /linux quiet' >> isowork/boot/grub/grub.cfg
echo ' initrd /initramfs' >> isowork/boot/grub/grub.cfg
echo '}' >> isowork/boot/grub/grub.cfg
# create sfs file
mksquashfs rootfs isowork/main.sfs -comp gzip -wildcards
grub-mkrescue isowork -o minimal.iso
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment