Debian has a lovely command for quickly creating new Debian installs, debootstrap. In a single command line you can create an entire new chrooted Debian install.
Sadly Fedora lacks the simplicity of debootstrap, but there are relatively easy ways to get some of the functionality of debootstrap, which I will document here.
Firstly the suggested method is to use mock, as described here. Mock is a bit heavyweight and rather too interactive.
You can use yum directly:
# mkdir /tmp/root # cp /etc/yum.repos.d/fedora.repo /tmp/test.repo edit /tmp/test.repo if you want to change distro, arch, etc, and change the name of the repo in this file to "test" # yum -c /tmp/test.repo \ --disablerepo=* --enablerepo=test \ --disableplugin=* \ --installroot=/tmp/root \ install list of packages
That will create a basic chroot environment in
/tmp/root. Even a simple root filesystem with a few packages can be quite large — 200 MB is not uncommon.
There’s a little bit of post configuration still needed. You’ll find that the chroot
/dev is empty. I investigated this and found that mock, etc actually create the basic dev nodes manually, which is painful but not too difficult.
Now debootstrap has a nice
foreign feature, which allows you to make a chroot for an entirely different architecture, eg. a ppc chroot from an i386 host. Yum won’t let you do this, or more precisely, rpm will try to run the %pre and %post scripts and probably fail.
I suspect it is possible to modify yum so that it passes
--noscripts to rpm, and saves up the scripts for you to run after the guest has booted (as does debootstrap).
You have to run the yum command as root, and unlike debootstrap it is not possible to use fakeroot, or at least I couldn’t make it work. I think it fails at the chroot step, which fakeroot cannot really fake.
Making an initramfs (initrd.img)
It’s easy to create an initramfs image, however first you need to create a startup script for the kernel to run when it boots. Just create a shell script (or even a program, but a shell script is most common) called
/init. By which I mean, it must be at the top of the root, so
/tmp/root/init. The script or program must be executable (
To create the final initramfs:
cd /tmp/root find -print0 | cpio -o0c | gzip > /tmp/initrd.img
You can test boot the image using qemu:
qemu -kernel /some/kernel -initrd /tmp/initrd.img -m 1024 -hda /dev/zero
(Enough memory is required in the guest to extract the root filesystem, plus more to run programs).
There is certainly room for a modified mock or a Fedora-specific debootstrap. Having to create device nodes by hand is particularly unsatisfying — I’m not sure why these aren’t created by some base RPM.