A delightful question!
Setting up a root file system (rootfs) for a base Ubuntu 24.04 image involves creating a minimal directory hierarchy with the essential files and directories required for the system to boot and run. I'll guide you through the process. We'll use debootstrap (a Debian tool, which works perfectly for Ubuntu too) to create the base system. Buckle up!
Prerequisites:
- Host system: You'll need a Linux system (Ubuntu or Debian, preferably) with internet access. I'll assume you're running Ubuntu 20.04 or later (64-bit).
- Required tools:
debootstrap, qemu-user-static (for arm64 or other non-native architectures), and basic utilities like mount, mkdir, cp, etc. Most of these are already installed on your host system.
- Target architecture: Decide on the target architecture for your rootfs:
amd64 (x86-64, default if you're unsure)
arm64 (AArch64, e.g., for Raspberry Pi 4 or other ARM boards)
armhf (ARMv7 hard-float, e.g., for older Raspberry Pi models)
Step 1: Install debootstrap (if not already installed)
sudo apt update
sudo apt install debootstrap
Step 2: Create a directory for your rootfs
Choose a location and create a directory for your rootfs. This will be the parent directory for your new Ubuntu 24.04 system.
mkdir -p ~/ubuntu-24.04-rootfs
Step 3: Run debootstrap
Now, the magic happens! Run debootstrap with the following options:
--arch: specify the target architecture (e.g., amd64, arm64, armhf)
--variant: minbase for a minimal system (no desktop, just CLI); use buildd if you need build tools like gcc
noble is the codename for Ubuntu 24.04 (check Ubuntu Releases for future versions)
~/ubuntu-24.04-rootfs: the target directory created in Step 2
http://archive.ubuntu.com/ubuntu/: the Ubuntu package repository (you can use a mirror closer to you)
For amd64 (x86-64):
sudo debootstrap --arch=amd64 --variant=minbase noble ~/ubuntu-24.04-rootfs http://archive.ubuntu.com/ubuntu/
For arm64 (AArch64):
You'll also need qemu-user-static for cross-architecture installation:
sudo apt install qemu-user-static
sudo debootstrap --arch=arm64 --foreign --variant=minbase noble ~/ubuntu-24.04-rootfs http://archive.ubuntu.com/ubuntu/
sudo cp /usr/bin/qemu-aarch64-static ~/ubuntu-24.04-rootfs/usr/bin/
sudo chroot ~/ubuntu-24.04-rootfs /debootstrap/debootstrap --second-stage
For armhf (ARMv7 hard-float):
Similar to arm64, but with qemu-arm-static:
sudo apt install qemu-user-static
sudo debootstrap --arch=armhf --foreign --variant=minbase noble ~/ubuntu-24.04-rootfs http://archive.ubuntu.com/ubuntu/
sudo cp /usr/bin/qemu-arm-static ~/ubuntu-24.04-rootfs/usr/bin/
sudo chroot ~/ubuntu-24.04-rootfs /debootstrap/debootstrap --second-stage
This process downloads and unpacks the base system into ~/ubuntu-24.04-rootfs. It may take a few minutes, depending on your internet connection.
Step 4: Configure the rootfs
Now that debootstrap has created the base system, let's configure it:
- Mount necessary filesystems (proc, sysfs, dev):
sudo mount -t proc /proc ~/ubuntu-24.04-rootfs/proc
sudo mount -t sysfs /sys ~/ubuntu-24.04-rootfs/sys
sudo mount -o bind /dev ~/ubuntu-24.04-rootfs/dev
sudo mount -o bind /dev/pts ~/ubuntu-24.04-rootfs/dev/pts
- Chroot into the new system:
sudo chroot ~/ubuntu-24.04-rootfs /bin/bash
You're now inside your new Ubuntu 24.04 system!
- Set locale, hostname, and timezone:
locale-gen en_US.UTF-8
echo "ubuntu" > /etc/hostname
ln -sf /usr/share/zoneinfo/UTC /etc/localtime
Replace en_US.UTF-8 with your desired locale, ubuntu with your preferred hostname, and UTC with your timezone.
- Set up the
root password:
passwd root
Enter a password for the root user when prompted.
- Optional: Create a new user (recommended):
useradd -m -s /bin/bash yourusername
passwd yourusername
usermod -aG sudo yourusername
Replace yourusername with... well, your desired username.
- Update and clean up:
apt update
apt full-upgrade -y
apt autoremove -y
apt clean
- Exit the chroot:
exit
Step 5: Unmount filesystems
Go back to your host system and unmount the filesystems:
sudo umount ~/ubuntu-24.04-rootfs/dev/pts
sudo umount ~/ubuntu-24.04-rootfs/dev
sudo umount ~/ubuntu-24.04-rootfs/sys
sudo umount ~/ubuntu-24.04-rootfs/proc
Congratulations! Your Ubuntu 24.04 rootfs is now ready in ~/ubuntu-24.04-rootfs.
What's next?
- Pack it up: You can tarball the rootfs for distribution or storage:
sudo tar -czpf ubuntu-24.04-rootfs.tar.gz -C ~/ubuntu-24.04-rootfs .
- Use it with QEMU: Boot your new rootfs with QEMU for testing (e.g.,
qemu-system-x86_64 -m 2048 -vnc :0 -device virtio-blk-device,drive=rootfs -drive file=ubuntu-24.04-rootfs.ext4,if=none,id=rootfs,format=raw)
- Write it to a disk:
dd the rootfs to an SD card or a virtual disk image for use on real hardware or a VM.
Tips:
- For a smaller rootfs, consider
--variant=minbase and removing unnecessary packages later with apt.
- To add packages after
debootstrap, chroot into the system and use apt install.
- If you're cross-compiling (e.g.,
arm64 on amd64), ensure you have the correct qemu-*-static binary copied into the target rootfs.
Happy hacking, and enjoy your fresh Ubuntu 24.04 rootfs!