Create a debian chroot inside ubuntu

OK, quick way to create that which is stated in the title. Loosely based on this, with some simplifications. Extremely useful to build/test software with lots of possibly experimental or otherwise dangerous dependencies or features, without polluting the mainhost package management system. Can be a real life (and time) saver.

Here we’ll install a lenny (current Debian stable) chroot, modify the accordingly the commands below if you choose to use some other version.

The name of the chroot in our example will be mychroot

/var/chroot/ is the place where you want the chroot to be created (a folder named (in this case) mychroot will be automagically created inside /var/chroot/ )

1. Install debootstrap: $ aptitude install debootstrap;
(install the one that comes from the repositories, no need to download any deb’s manually)

2. Install dchroot (replace editor with vim or whatever suits you):

$ sudo apt-get install dchroot
$ sudo mkdir /var/chroot # Remember, we assume our chroot is here
$ sudo editor /etc/dchroot.conf

Append this:

mychroot /var/chroot

3. Build the chroot proper. Add -⁣-arch i386 to build a 32 bits chroot on top of an amd64 system. The example mirror is Portuguese; another can be choosen from here.

$ sudo debootstrap -⁣-variant=buildd lenny /var/chroot/

4. Run the following commands (*):

$ sudo cp /etc/resolv.conf /var/chroot/etc/resolv.conf
$ sudo chroot /var/chroot/
$ mount /proc
$ apt-get install aptitude # I prefer aptitude, so install it first
$ aptitude update
$ aptitude install wget debconf
$ aptitude install locales dialog
$ locale-gen en_GB.UTF-8  # this needs improving
$ exit

5. Configure dchroot (including users and passwords)

$ sudo cp /etc/passwd /var/chroot/etc/
$ sudo sed 's/\([^:]*\):[^:]*:/\1:*:/' /etc/shadow | sudo tee /var/chroot/etc/shadow
$ sudo cp /etc/group /var/chroot/etc/
$ sudo cp /etc/hosts /var/chroot/etc/ # avoid sudo warnings when resolving chroot's hostname

6. Enable sudo:

$ sudo cp /etc/sudoers /var/chroot/etc/
$ sudo chroot /var/chroot/
$ dpkg-reconfigure passwd
$ passwd <your first user in the admin group>
$ aptitude install sudo
$ pwconv # SEE BELOW
$ exit

At this point, an additional step was needed to make sudo work, viz.: $ pwconv This was totally empirical, and I have got no clue (as of yet) as to why it works… sorry.

7. Add lines to /etc/fstab (mainhost) that allow access to your home folder from inside the chroot (VERY useful). Note that these commands must be ran from the mainhost, not the chroot.

$ sudo editor /etc/fstab

[add the below lines ]

/home           /var/chroot/home        none    bind            0       0
 /tmp            /var/chroot/tmp         none    bind            0       0
 /media/cdrom    /var/chroot/media/cdrom none    bind            0       0
 /dev            /var/chroot/dev         none    bind            0       0
 proc-chroot     /var/chroot/proc        proc    defaults        0       0
 devpts-chroot   /var/chroot/dev/pts     devpts  defaults        0       0

8. And mount them:

$ sudo mount -a

9. Modify the prompt to tell you that you’re in a chrooted environment. VERY useful if you work with multiple shells (who doesn’t thes days?), both from the chroot and the mainhost. You can call it whatever you want.

$ sudo chroot /var/chroot/
$ echo mychroot > etc/debian_chroot
$ exit

10. Now from the mainhost, (no need to be root) do the command below and you’re in a working chroot environment:

$ dchroot -c mychroot -d

The end.
(*) The locale stuff needs some fine tunning, maybe in a future update.

One response to “Create a debian chroot inside ubuntu