Skip to content

Instantly share code, notes, and snippets.

@crabdancing
Created April 2, 2021 01:35
Show Gist options
  • Save crabdancing/02d31b0c6311774887d4417a44d9a5a0 to your computer and use it in GitHub Desktop.
Save crabdancing/02d31b0c6311774887d4417a44d9a5a0 to your computer and use it in GitHub Desktop.
toolchain and comments for building a custom Ubuntu distro image
#!/bin/env bash
function init_rootfs() {
mkdir -p rootfs scratch image/live
sudo debootstrap focal rootfs/
}
# For how to initialize rootfs packages, see:
# https://bigdaddylinux.com/ubuntu-arch-style/
# I encountered this network-manager bug:
# https://bugs.launchpad.net/ubuntu/+source/network-manager/+bug/1638842
function fix_network_manager() {
rm rootfs/usr/lib/NetworkManager/conf.d/10-globally-managed-devices.conf
}
# First stage -- make squashfs
function make_squashfs() {
sudo mksquashfs rootfs/ image/live/filesystem.squashfs -noappend -comp xz -Xdict-size 8k -e boot
}
# Second stage -- copy boot files to their intended locations
function copy_boot_files() {
sudo rsync -v rootfs/boot/vmlinuz-* image/vmlinuz
sudo rsync -v rootfs/boot/initrd.img-* image/initrd
}
# Third stage -- make both GRUB & EFI boot images
function make_bootloader() {
sudo grub-mkstandalone --format=i386-pc --output=scratch/core.img \
--install-modules="linux normal iso9660 biosdisk memdisk search tar ls" \
--modules="linux normal iso9660 biosdisk search" \
--locales="" \
--fonts="" "boot/grub/grub.cfg=scratch/grub.cfg"
sudo cat /usr/lib/grub/i386-pc/cdboot.img scratch/core.img >scratch/bios.img
sudo grub-mkstandalone \
--format=x86_64-efi \
--output=scratch/bootx64.efi \
--locales="" \
--fonts="" "boot/grub/grub.cfg=scratch/grub.cfg"
(cd scratch && dd if=/dev/zero of=efiboot.img bs=1M count=10 && mkfs.vfat efiboot.img &&
mmd -i efiboot.img efi efi/boot && mcopy -i efiboot.img ./bootx64.efi ::efi/boot/)
}
# Fourth stage -- assemble ISO
function make_iso() {
sudo xorriso -as mkisofs -iso-level 3 -full-iso9660-filenames -volid "Zdani" \
-eltorito-boot boot/grub/bios.img -no-emul-boot -boot-load-size 4 -boot-info-table \
--eltorito-catalog boot/grub/boot.cat --grub2-boot-info --grub2-mbr \
/usr/lib/grub/i386-pc/boot_hybrid.img -eltorito-alt-boot -e EFI/efiboot.img \
-no-emul-boot -append_partition 2 0xef scratch/efiboot.img -output "Zdani.iso" \
-graft-points image /boot/grub/bios.img=scratch/bios.img /EFI/efiboot.img=scratch/efiboot.img
}
# Then there's nothing left to do but test
function qemu_test() {
qemu-system-x86_64 -cdrom Zdani.iso -m 2048 -machine accel=kvm
}
function clean_rootfs() {
echo cleaning...
for path in rootfs/{home/zdani,root}/{.bash_history,.local,.cache}; do
echo "Recursively delete: $path"
sudo rm -rf "$path"
done
for path in rootfs/var/log/alternatives.log rootfs/var/log/apt/*.log; do
echo "Zero over: $path"
echo -n | sudo tee "$path"
done
#for home in /home/
}
# Everything after this point is just convenience & UI
function build_all() {
clean_rootfs &&
make_squashfs &&
copy_boot_files &&
make_bootloader &&
make_iso
}
function build_boot() {
copy_boot_files &&
make_bootloader &&
make_iso
}
function arch_style_chroot() {
sudo arch-chroot rootfs
}
function usage_msg() {
echo "Look at my source code for usage."
}
if [ "$1" == "" ]; then
usage_msg
fi
while [ "$1" != "" ]; do
case $1 in
all)
build_all
;;
boot)
build_boot
;;
test)
qemu_test
;;
chroot)
arch_style_chroot
;;
*)
usage_msg
;;
esac
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment