What is a rootfs

Rootfs is a tarball containing the root filesystem tree with all the required directories, sub-directories and files.  A typical rootfs contains sbin, bin, etc, lib, usr, dev, proc, sys, var, and tmp directories.

You may need to build your own rootfs if you don’t want all the fancy packages that come with default fedora or ubuntu container images.  You may need to create your own rootfs if you want a bare minimal embedded linux image to be booted in a specialized embedded hardware device.

Where can I download a rootfs tarball

Note: If you want a pre-built Linux minimal root filesystem tar ball. You can download it from the Alpine Linux Website as shown in the picture below.  You can choose to download a mini rootfs for whichever CPU architecture you want.

Alpine Linux Mini Rootfs Tar Ball - How to build a Linux rootfs or root file system tar ball

How to build a minimal rootfs from scratch ?

There are many tools available to build rootfs from scratch.  One such tool is Buildroot.  Buildroot is an SDK toolkit for creating rootfs.

To use Buildroot, go to https://buildroot.org and download the latest stable version of the buildroot tool kit.


$ curl https://buildroot.org/downloads/buildroot-2017.02.2.tar.gz

Next unzip and extract the contents of the tar.gz file.


$ tar xvf buildroot-2017.02.2.tar.gz

Just like how you’d run “make menuconfig” before compiling a linux kernel from scratch from its source code, run “make menuconfig” to configure your rootfs.


$ cd buildroot-2017.02.2
$ make menuconfig

It’ll open up a cursor window, as shown below.

1

You can traverse through this dialogue box to customize your rootfs contents.  If you plan to build a rootfs for creating a container image, and not a linux kernel image for booting an embedded device, then you can skip selecting ‘init system”, “Linux kernel” etc as part of your rootfs.

Note: When you select Linux Kernel Headers, make sure the Linux Kernel Header file version chosen in the menuconfig for the rootfs compilation is of a version less than or equal to Linux kernel version running in the host machine where you plan to run your container, otherwise you’ll get “kernel too old” error when you use the rootfs.

When you are done with configuration, save and exit.

Once you save and exit, a config file gets generated in your local directory.  Now all you have to do is to start building your rootfs using the ‘make’ command.

$ make

It will take sometime to build the rootfs from scratch.  After the build is completed, you can pickup your rootfs tar under ./output/images/ directory.

If you want to make changes to the content of your rootfs, then re-run “make menuconfig” and do ‘make clean” and then make.  It is safe to always perform a clean build every time you change the configuration of your rootfs.

Copy the rootfs.tar file generated to a temporary directory under /tmp.  Now you can access its contents and verify if it is good enough for your usecase.

$ mktemp –d
$ tar xvf ./output/images/rootfs.tar –C /tmp/tmp.IaDc7m7ymH
$ cd /tmp/tmp.IaDc7m7ymH
$ ls
bin  etc  lib64    media  opt   root  sbin  tmp  var
dev  lib  linuxrc  mnt    proc  run   sys   usr
$ pwd
/tmp/tmp.IaDc7m7ymH

In the test below, I did a chroot into rootfs and ran few commands to make sure it is sane.


$ sudo chroot . /bin/sh
/ # pwd
/
/ # ls
bin      lib      media    proc     sbin     usr
dev      lib64    mnt      root     sys      var
etc      linuxrc  opt      run      tmp
/ # which wget
/usr/bin/wget
/ # exit
$ pwd
/tmp/tmp.IaDc7m7ymH

Awesome! You have created your first rootfs. Now play with it.

I found this awesome youtube presentation by redbeard very useful.