dogeystamp home about projects gitea

making custom arch linux live ISOs without arch linux

2023-06-07

If you’ve ever installed any Linux distro, you probably had to do it using a live install environment. Once you flashed the USB drive, you could boot off it and you’d be greeted with a Linux system.

I’ve always found it fascinating that you could pack the entire OS onto a stick and bring it around with you. However, something even more enticing is being able to customize that system.

This is where archiso comes in handy: it’s a tool that can be used to generate ISOs with Arch Linux on them. You can flash these ISOs to USB drives to make portable Arch Linux systems. archiso is also incredibly flexible, and you can customize it very well. In fact, it’s the tool that Arch’s maintainers use to generate the official live installation images.

The one issue I have, though is that you need to be running Arch Linux to run archiso:

“Currently creating the images is only supported on Arch Linux but may work on other operating systems as well.”

As I run another distro on my laptop, I could not use archiso. So, in the last few days, I set out to make archiso run on my system. The final results of this endeavour can be found on GitHub.

pacstrap

One of the essential dependencies of archiso is pacstrap. Essentially, what pacstrap does is that it creates a small Arch Linux system in a folder. This is the bootstrapped root filesystem (root FS). Later, archiso takes that folder, compresses it, and puts it in the final ISO file.

The problem is that pacstrap needs your host system to be Arch Linux so that it can bootstrap a new Arch system. In fact, if you look at the source code, you’ll find that the host system uses its own pacman to install everything to the bootstrapped system:

pacman -r "$newroot" "${pacman_args[@]}"

pacstrap only has to create a few directories, but the rest is done by installing the base metapackage through pacman. base includes both filesystem and pacman. As far as I understand, this means that all the important files in an Arch Linux system come from installing the filesystem package. Once the host pacman installs base in the bootstrapped system, we have a full Arch Linux root FS, including its own pacman.

However, since I do not have pacman to bootstrap this way, I needed another way to obtain an Arch root FS. I came across archstrap, which does exactly that. archstrap downloads a pre-built Arch Linux root FS as a tarball, then installs packages to it just like regular pacstrap. Cleverly, this script removes the need for arch-chroot on the host system: it runs the arch-chroot inside the downloaded Arch filesystem.

Somewhat annoyingly though, archstrap does not operate exactly the same way pacstrap does: I had to patch it to get it working with archiso. Also, I patched archiso itself to remove some flags archstrap doesn’t parse.

other changes

The other main dependency missing in archiso is pacman, Arch’s package manager. Since we aren’t running Arch, we of course do not have it on our host system. However, the bootstrapped Arch root FS we downloaded earlier does have pacman inside of it. Therefore, I replaced all invocations of pacman inside archiso with invocations of the bootstrapped pacman.

archiso also includes a small script to test your generated ISOs in a QEMU virtual machine. I added a check to it that switches some hard-coded paths. In Arch, the path is /usr/share/edk2-ovmf/x64, but on my system it was at /usr/share/edk2-ovmf.

conclusion

It turns out that modifying open source software isn’t that difficult. Given that archiso’s maintainers wrote very structured and organized code, it’s surprisingly easy to navigate around the script to patch things.

Of course, I definitely ruined the quality of archiso by doing this: there’s missing features and everything is untested and unlinted. Then again, this isn’t going to be production-grade software; I just wanted to make a custom portable Arch USB while using Gentoo on my PC.

Anyways, if you want to make your own custom Arch USBs but don’t have Arch, check out archiso-portable on GitHub.

As a bonus, here’s a screenshot of the Arch Linux live environment I made earlier on a Gentoo system:

preview