chroot abused as virtualization SW

As the title indicates, this article will show you an example usage of chroot, where we'll clone our currently running system into a disk-image (dd). Doing that, we can afterwards loop-mount it, chroot in it and let all the stuff run, that we don't want on our mainbox. On my box, for example, I've removed all the compilers and dev-libraries and I'm using this approach to keep it fast, lightweight and clean.

Why?

The intended purpose of virtualization on desktop systems for me is different than on the serverside. I'm using VM's on my desktop for several reasons. One of them is to simply have a clean mainsystem.
Now why do we use chroot instead of KVM, Virtualbox, VMware? I haven't done any benchmarks yet, but to my impression, the chroot thingy is much faster than KVM. The big advantage is also that we have a (almost) 0 second bootup time. Furthermore, the networking is much faster in comparison to KVM's internal networking, which I've used in the past. And last but not least, everything is already included on a basic-debian installation, so we don't need any further software installed.
Let's get started!

Initial Setup

Pretty cool, so there's no need to install anything. All the stuff that's needed (losetup, dd and chroot) comes shipped in a basic debian installation (packages: coreutils and mount). So you can start straightaway!

Create our project directory, feel free to change it to your needs.
[code lang="bash"]
mkdir \~/data/chroot-project
cd \~/data/chroot-project
[/code]

Create a diskimage using dd. You only have to run ONE of these commands! The first one creates a sparsefile, which is created very fast (<1sec), but you might see a worse performance afterwards. The second one will create a normal file, allocating the 4GB right at the beginning, which takes a while once, but in general should give you better performance afterwards. As always, you can change the 4000 to whatever you want, it's simply the size of your VM.
[code lang="bash"]
dd if=/dev/zero of=./001_debian.img bs=1M count=1 seek=4000
dd if=/dev/zero of=./001_debian.img bs=1M count=4000
[/code]

So now the choice of the Filesystem. I've used ext3 for my first setup, which was just working fine. BTRFS can be tried out safely. Since we're using chroot, we don't need any bootloader, so you don't need any patched grub2 or lilo etc. (not sure about the current development status to be honest..). As before, only run ONE of these commands.
[code lang="bash"]
mkfs.ext2 -v -F 001_debian.img
mkfs.ext3 -v -F 001_debian.img
mkfs.ext4 -v -F 001_debian.img
mkfs.btrfs 001_debian.img
[/code]

Loop-mount the filesystem to /media/target (create it if you didn't do so already).
[code lang="bash"]
sudo mount -o loop 001_debian.img /media/target
[/code]

Once again, you've got the choice, first command let's you re-use your currently running system with all the installed software. Please make sure that you add some more exclusions, so that you don't copy around the whole homedirectories, apt-cache etc. The second command will create you a fresh debian installation, with a bare minimum of installed packages.
[code lang="bash"]
sudo rsync -va --exclude=/proc --exclude=/sys --exclude=/media/target --exclude=/home/user/data/chroot-project / /media/target
sudo debootstrap squeeze /media/target
[/code]

And finally unmount it again.
[code lang="bash"]
umount /media/target
[/code]

Run it!

Now we're ready for a first run of the new image.

Loop-mount the filesystem to /media/target (which should be existing at this point)
[code lang="bash"]
sudo mount -o loop 001_debian.img /media/target
[/code]

Bind the special filesystems into our target.
[code lang="bash"]
mount -o bind /dev /media/target/dev
mount -o bind /proc /media/target/proc
mount -o bind /sys /media/target/sys
[/code]

Enter the shell of our VM.
[code lang="bash"]
chroot /media/target /bin/bash
[/code]

Now you're already in our 'VM', where you can run all your stuff. If you're finished, just exit and umount everything as listed below.
[code lang="bash"]
exit
umount /media/target/dev
umount /media/target/proc
umount /media/target/sys
umount /media/target
[/code]

Notes

  • It might be worth trying the whole thing with a BTRFS filesystem instead of EXT2/3/4, since there won't be any critical data in it and who know, maybe we can even get even higher speeds with it!
  • I'm currently looking for some benchmarks and will update this post as soon as I have some results. I'll do a simple comparison between KVM and chroot.

Edit (29.06.2010): There seems to be a new software which I've just found, that might supersede my chroot solution. Check it out here: LXC. I'll give it a shot and post about it in the next few days.

Thanks for reading! If you have questions, use the comments.

Cheers,
Raphi