Jun 27, 2011

Five steps to create Fedora chroot jail using yum

Pre-Notes:
- This will work only if your Linux Distro is based on RPM and YUM. I have tested it on Fedora but it may also work on RHEL and Centos... It will NOT work on Ubuntu, Debian, Suse...
- The operating system and its version will be the same inside and outside the chroot jail.
- On the example, the chroot will be placed at: /chroot/devel
- To install different version of Fedora or other RPM based distro, it will be necessary to manage yum package repositories outside the jail.

The five steps:
# 1 - Create the chroot directory
$ sudo mkdir -p /chroot/devel/var/lib/rpm

# 2 - Initiate rpm db on chroot
$ sudo rpm --root /chroot/devel --initdb

# 3 - download Fedora Release package.
# If you do not want Fedora, download the correct *-release package and use it
# on step 4. There are examples on the references.
$ yumdownloader --destdir=/tmp fedora-release

# 4 - Install downloaded Fedora release inside chroot
$ sudo rpm --root /chroot/devel -ivh /tmp/fedora-release*rpm

# If you want more than just bash, see 5a - OPTIONAL
# 5 - Install bash and its dependencies on the jail.
# For different versions of Fedora or to install other RPM based distros, it will be
# necessary to configure the correct yum repositories outside the jail, so yum
# can download the correct packages.
$ sudo yum --installroot=/chroot/devel install bash

#5a - OPTIONAL: Do a minimum system install
# If you want all packages from the minimum install option
# `yum grouplist` show all available groups
$ sudo yum --installroot=/chroot/devel groupinstall "minimal install"

Post-Notes:
- To enter the jail:
$ sudo chroot /chroot/devel
- If you want to install yum inside the jail:
$ sudo yum --installroot=/chroot/devel install yum

References:
Creating chroot environments with yum

Creating a FEDORA 10 CHROOT Environment in DEBIAN SID

9 comments:

Anonymous said...

this also works on debian wheezy. One just needs to download the fedora-release rpm manually as yumdownloader is not available. Other than that all steps apply.

Anonymous said...

Very useful. Thanks for sharing.

Art Win said...

as of 2013, to create chroot you only need yum and rpm. Distro does not matter. At least for oel6 rpm must be built with lua support

1) add needed repos to /etc/yum.repos.d (or alternative location)

2) init rpm db at desired chroot location
# rpm --root /chroot --initdb

3) install packages
# yum --installroot=/chroot yum bash fedora(centos,rhel,oraclelinux,whatever)-release another-useful-package-etc

that's it

Mathias Panzenböck (panzi) said...

Thanks for that description so far, but I cant start chrome in the chroot environment because it does nto find /proc/cpuinfo. There is nothing mounted in /proc and /etc/mtab is a link to /proc/mount, which does not exist. Do you know how I can solve this?

Peter Senna Tschudin said...

Check item 8 of:
http://blog.parahard.com/2013/03/creating-debian-chroot-inside-fedora.html

Tom Cipollone said...

Hi Peter and thank you for this blog and for sharing your insights.

I am a bit of a newbe at this so I have a question about using yumdownloader. I want to chroot fedora 9 (I am currently using fedora 20). Need I install the entire distro? I did try "yumdownloader --destdir=/tmp fedora 9" (and various other things) but I get "No match for argument" as a response. Can you give me advice? Thank You.

Tom Cipollone said...

Hi Peter and thank you for this blog and for sharing your insights.

I am a bit of a newbe at this so I have a question about using yumdownloader. I want to chroot fedora 9 (I am currently using fedora 20). Need I install the entire distro? I did try "yumdownloader --destdir=/tmp fedora 9" (and various other things) but I get "No match for argument" as a response. Can you give me advice? Thank You.

Peter Senna Tschudin said...

Tom why are you using fedora 9 instead of fedora-release?

Tom Cipollone said...

Thank you for replying. I not exactly sure what "fedora-release" would do. I imagine it would set up a directory structure (bind mounts?) using my current release of fedora, which is, fc20. My initial problem was that there is a large program (body of work, if you will) that was compiled under fedora 9. I want to make sure that I don't have problems with the compile under fedora 20. I realize now that I can not run a different kernel under a chroot but perhaps virtualization is not required. Again, thanks.