The aim this time is to setup a base rootfs for Ubuntu/Debian distro which can be used for setting up a chroot jail or to create a docker container base image. This blog is based on a earlier blog which I have written but forgot to publish around June, 2012.

The below mentioned steps are validated on Fedora 25 Workstation installation.

For setting up a debian based rootfs we need a tool called debootstrap. It’s available as part of EPEL repository for Fedora and other Redhat based distros. Make sure to always install the latest version of debootstrap.

sudo dnf install debootstrap

To setup a base rootfs you need to decide on the version of debian distro (wheezy, jessie, buster), the target architecture (i386 or amd64) and what kind of variant you want to setup. Do read the man page of debootstrap to know about more advanced options.

For this post, we are looking to setup a debian wheezy based rootfs to be used as amd64 build environment.

sudo debootstrap --variant=buildd \
    --arch=amd64 \
    --components=main,contrib,non-free wheezy \
    wheezy-chroot \
    http://ftp.debian.org/debian/
# sudo debootstrap --variant=buildd \
#    --components=main,restricted,universe,multiverse \
#    --arch=amd64 precise \
#    precise-chroot \
#    http://archive.ubuntu.com/ubuntu

In the above command components argument is used to specify which components to enable the distri repository. For debian, there are three components - main, contrib and non-free. We have enabled all three for our base rootfs.

But if you notice, the second line is slightly different because this points to the ubuntu repository and creates a rootfs for precise release of ubuntu. The components of ubuntu repository are named differently than the components of debian repository.

Now if we want to customize our rootfs we can do so using the chroot command. For that we need to mount certain mount points and use the chroot command to enter and customize the rootfs.

export __CHROOT_DIR=${PWD}/wheezy-chroot
sudo mount --bind /dev ${__CHROOT_DIR}/dev
sudo mount --bind /dev/pts ${__CHROOT_DIR}/dev/pts
sudo mount --bind /sys ${__CHROOT_DIR}/sys
sudo mount --bind /proc ${__CHROOT_DIR}/proc
#sudo mount --bind /home ${__CHROOT_DIR}/home # optional
#sudo mount --bind /opt ${__CHROOT_DIR}/opt # optional
sudo cp /etc/resolv.conf ${__CHROOT_DIR}/etc/resolv.conf # required for resolving DNS inside the chroot

We are going to install sudo package inside the chroot.

sudo chroot ${__CHROOT_DIR}
export LC_ALL=C # this is to suppress the warning messages when we run apt-get install command
apt-get install sudo

Once the customization is done we need to properly unmount all the chroot mount points.

#sudo umount ${__CHROOT_DIR}/opt
#sudo umount ${__CHROOT_DIR}/home
sudo umount ${__CHROOT_DIR}/dev/pts
sudo umount ${__CHROOT_DIR}/dev
sudo umount ${__CHROOT_DIR}/sys
sudo umount ${__CHROOT_DIR}/proc
sudo rm ${__CHROOT_DIR}/etc/resolv.conf

In case you simply want to invoke a command inside the chroot skipping all the complex mount point setup you do so by running the command in the below mentioned format.

sudo chroot wheezy-chroot apt-get install sudo

As final step we can package the chroot as a tarball. It is recommended to clean the cache folder of the rootfs before creating the tarball.

sudo rm -rf ${__CHROOT_DIR}/var/cache/apt/*
sudo tar -Jcvf wheezy-chroot.tar.xz wheezy-chroot/

This will create a tarball named wheezy-chroot.tar.xz in the current directory. The newly created tarball can be used as base to create a new docker container or as a chroot jail.

References

  1. http://wiki.debian.org/Debootstrap
  2. https://wiki.ubuntu.com/DebootstrapChroot