What is a chroot ?
Basically chroot is an almost complete Unix like operating system running inside another host operating system. But the differance is the chrooted system system runs with limited system resources on user specified root (/) directory and limited filesystem access.
A chroot system could be used for varity of purpouse like
- Testing an untrusted application.
- Isolating one/many application from the system.
- Running 32 bit applications on a 64 bit system.
- Building a development environment for other distributions like Arch Linux.
- Running some old obsolate application.
Interasting ? Lets build a 32 bit ubuntu chroot environment on a 64 bit system. This tutorial is same for any Debian based OS like Ubuntu, Linux Mint etc.
There are many ways to setup a chroot,
- Copying files from an existing system.
- Debootstrap on Debian based systems.
- Copying files from an live installation media.
Here we are going to get the system files from an 32 bit ubuntu (lubuntu more precisely) live CD, you can use your any existing live CD ISO files, this will save a lot of internet data.
1. Mount the Ubuntu live CD media
Create mountpoint directories
sudo mkdir /mnt/live_cd sudo mkdir /mnt/system_files
Mout the live CD ISO files
sudo mount lubuntu-14.04.1-desktop-i386.iso -t iso9660 /mnt/live_cd
Don’t forget to chenge the ISO file name according to yours.
Mount the squashfs filesystem containing the system files
sudo mount /mnt/live_cd/casper/filesystem.squashfs -t squashfs /mnt/system_files
2. Copy system files for the chroot environment
A chroot system’s files are usually stored under the /var/chroot folder, but you can store them anywhere you like. Create directories for the chroot system
sudo mkdir -p /var/chroot/lubuntu32
Copy files from the squashfs media
sudo cp -r /tmp/system_files/* /var/chroot/lubuntu32
It will take some time to copy the files and make sure that you have some free disk space to properly copy the files.
3. Install some basic 32 bit libraries on the host system
Basic 32 bit runtime libraries are required to execute the 32 bit binaries inside the chroot system, lets Install them.
sudo apt-get install libc6-i386 lib32z1 lib32stdc++6
4. Mount /dev , /sys , /proc , /var/run on the chroot
To avail system resources on the chroot system and for proper functionality, it’s important mount and bind the /dev , /dev/pts , /sys , /proc directories on the chroot system.
sudo mount --bind /proc/ /var/chroot/lubuntu32/proc sudo mount --bind /sys/ /var/chroot/lubuntu32/sys sudo mount --bind /dev/ /var/chroot/lubuntu32/dev sudo mount --bind /dev/pts/ /var/chroot/lubuntu32/dev/pts
5. Now test the chroot environment
Now test the new chroot system with the command bellow
sudo chroot /var/chroot/lubuntu32 /bin/bash
This command should drop you inside the chroot system with root privilage, and / as present directory.
Run this command to make sure that it’s a 32 bit system.
It should return i386, which denotes a 32 bit system. You may want to update the repository and install a ssh server for more functionality
sudo apt-get update # inside the chroot
sudo apt-get install openssh-server # inside the chroot
6. Fix failed to connect to socket com/ubuntu/upstart problem
As the chroot system not started through a standard init system. Thats why many services like dbus , ssh server will a return error message like this failed to connect to socket com/ubuntu/upstart. To fix this problem just run the command bellow INSIDE THE CHROOT SYSTEM. This problem is ubuntu specific.
dpkg-divert --local --rename --add /sbin/initctl # inside the chroot
ln -s /bin/true /sbin/initctl # inside the chroot
7. Fix dbus failed to start inside the chroot environment
You have to mount the /var/run directory on the chroot system’s /var/run directory. Run the first command in the host system and the second command inside the chroot.
sudo mount --rbind /var/run/dbus/ /var/chroot/lubuntu32/var/run/dbus/
service dbus start # inside the chroot
8. Strip down the system
You may want to remove unnecessary packages from the system, like ubiquity ubuntu installer, the kernel etc. etc.
apt-get purge ubiquity* # inside the chroot apt-get purge linux-image-generic # inside the chroot apt-gt autoremove --purge # inside the chroot
I hoe this simple tutorial will help you to build a chroot environment, If you have any further question, don’t hesitate to leave comments, we’ll be happy to assist you. Dont forget to share this tutorial with your friends.