Skip to content

Getting Started with a Minimal Gentoo Linux LiveUSB

Thanks to the author of this script. Unknown author

To be updated later: 2024-09-02

This script assumes you're running it with sufficient privileges (e.g., as root) and that you've installed necessary tools like wget, 7z, and unsquashfs. Also, this script includes placeholders for manual steps and the final ISO creation, which you'll need to handle according to your specific needs

#!/bin/bash

# Define variables
liveUsbRoot="/mnt/livecd/gentoo"
squashfsRoot="/mnt/livecd/gentoo/squashfs-root"
isoName="install-amd64-minimal-20240121T170320Z.iso"
isoUrl="https://distfiles.gentoo.org/releases/amd64/autobuilds/current-install-amd64-minimal/$isoName"

# Create necessary directories
mkdir -p "${liveUsbRoot}"

# Download the Gentoo minimal install ISO
wget "${isoUrl}" -P "${liveUsbRoot}"

# Extract the ISO contents
7z x "${liveUsbRoot}/${isoName}" -o"${liveUsbRoot}"

# Unpack the SquashFS image
unsquashfs -f -d "${squashfsRoot}" "${liveUsbRoot}/image.squashfs"

# Mount necessary filesystems for chroot
mount --types proc /proc "${squashfsRoot}/proc"
mount --rbind /sys "${squashfsRoot}/sys"
mount --make-rslave "${squashfsRoot}/sys"
mount --rbind /dev "${squashfsRoot}/dev"
mount --make-rslave "${squashfsRoot}/dev"
mount --bind /run "${squashfsRoot}/run"
mount --make-slave "${squashfsRoot}/run"

# Placeholder for manual work inside chroot
# chroot "${squashfsRoot}"
# <... Do manual work here ...>
# exit

# Unmount filesystems
umount -l "${squashfsRoot}/proc"
umount -l "${squashfsRoot}/sys"
umount -l "${squashfsRoot}/dev"
umount -l "${squashfsRoot}/run"

# Recreate the SquashFS image
mksquashfs "${squashfsRoot}" "${liveUsbRoot}/new-image.squashfs" -comp xz

# Cleanup
rm -rf "${squashfsRoot}"
rm "${liveUsbRoot}/${isoName}"

# Placeholder for creating new ISO or copying to USB
# <... Create new ISO or prepare USB stick here ...>

# Example: Copying to USB (Replace /dev/sdX with your USB device)
# mkfs.ext4 /dev/sdX1
# mount /dev/sdX1 /mnt/usb
# cp -r "${liveUsbRoot}"/* /mnt/usb/
# grub-install --target=x86_64-efi --efi-directory=/mnt/usb/efi --boot-directory=/mnt/usb/boot /dev/sdX
# <... Configure grub.cfg ...>

# Unmount USB and cleanup
# umount /mnt/usb

Create ISO

Creating a new ISO file with your customized contents, including the new SquashFS image, involves using a tool like genisoimage (or mkisofs, which is functionally similar). This tool allows you to specify the contents of the ISO, including boot parameters and filesystem structure. Here's how you can create an ISO with your custom SquashFS file:

  1. Install Necessary Tools:
  2. Ensure you have genisoimage or mkisofs installed on your system.

  3. Prepare the ISO Contents:

  4. Make sure all the necessary files (like the new SquashFS image, kernel, initramfs, and EFI files) are in the correct locations within your working directory.

  5. Create the ISO:

  6. Use genisoimage to create the ISO. You'll need to specify options for bootability, especially if you're creating a UEFI-bootable ISO. The exact command will depend on your specific requirements, but here's a basic template:

    genisoimage -o custom-gentoo.iso \
                -b boot/isolinux/isolinux.bin \
                -c boot/isolinux/boot.cat \
                -no-emul-boot -boot-load-size 4 -boot-info-table \
                --efi-boot efi.img \
                -eltorito-alt-boot \
                -e efi.img \
                -no-emul-boot \
                -R -J -V "Custom Gentoo Live" \
                /path/to/iso/contents
    
  7. In this command: -

Replace /path/to/iso/contents with the path to the directory containing your ISO files (including the new SquashFS image). - The -b option specifies the boot image for BIOS systems, and -c specifies the boot catalog. - The --efi-boot and -e options are for UEFI bootability, pointing to your EFI image. - The -R, -J, and -V options are for Rock Ridge, Joliet, and Volume ID extensions, respectively.

  1. Testing:
  2. After creating the ISO, it's important to test it in a virtual machine or on a physical machine to ensure it boots correctly and all your customizations are present.

This command is a starting point and might need adjustments based on the specifics of your ISO structure and boot requirements. For example, if you're using GRUB as your bootloader for UEFI booting, the options will be different. Additionally, if your setup includes more complex boot configurations or specific file arrangements, you'll need to modify the command accordingly.

Creating a bootable ISO can be complex, and it often requires some trial and error to get all the options right, especially for boot compatibility across different systems.