Bootstrapping CentOS rootfs for use with chroots/containers

We will see how to generate a rootfs tarball for redhat based system. Since we are targetting redhat based system, I am going to use debian based system (Ubuntu 17.10 64-bit - latest at the time of writing this post) as my host. We will generate rootfs tarball for CentOS 7 64-bit.å

The below mentioned steps are validated on Ubuntu 17.10 desktop installation.

Lets get started

As usual the first step, install the required dependecy packages which in our case is yum.

Second, will prepare the required directory structure. In our example ~/work will be our working directory and we will install the CentOS 7 roofs in inside of work in a directory named centos7.

Now we need to generate the repo metadata file for CentOS 7 installation. Below gist does the same.

Once we have our directory structure and the repo file prepared as mentioned above, we can bootstrap the CentOS 7 root by executing the below mentioned command. This creates a base rootfs with networking utilities and sudo command.

After the yum installation is complete we need to prepare our rootfs for customization. We will start by mounting the required mount points to use chroot.

Now we can start using the rootfs using chroot command. We will do the following customizations

  1. Setup releasever and basearch yum variables. Unfortunately this is required because of the caveat mentioned in Appendix I.
  2. Remove unncessary files like yum cache, locale definitions, temporary files, etc.
  3. Generate proper yum.conf, locale definition, build timestamp file and machine-id file - these are pretty standard stuff required for storage optimization in case we decide to use this rootfs for docker images

After the rootfs configuration we need to umount all the mount points we mounted earlier. The unmount order is important here.

We do one last cleanup before we package the rootfs as a tarball.

And the final piece, run the below command to generate the CentOS 7 rootfs tarball.


  1. Customizing yum variables

Appendix I

While running yum command inside of the CentOS 7 chroot I ran into below error message. Strangely this happens on Ubuntu host but not on CentOS host. As a fix I have created yum variables releasever and basearch inside the CentOS rootfs. Please check Reference section for the manual describing how to customize yum variables.