From Bochs to chroot

I used Bochs at 2004 for some projects of mine since that was the only way to emulate a complete PC back in the days. The performance was horrible, hardly usable, since all CPU & BIOS commands were emulated in software. Even on the best of hardware, performance was lacking. Fast forward a few years, and Qemu emerges. I was able to emulate most of my favorite OS’s in a snap, with reasonable performance, and once KVM and friends emerged, I was a happy camper. Lately I started working on a new ARM image for some project at work, and I had to build an emulated system. Naturally, I picked Qemu for the job, and used it as follows:

qemu-system-arm -append "root=/dev/mmcblk0p1 rw console=ttyAMA0" -M vexpress-a9 -kernel zImage -sd my.img -nographic

Performance was as expected, but I then remembered the binfmt_misc tricks and I thought to myself there must be nowdays a better way to emulate my image, and I was right. Today we can chroot an emulated image from any host and enjoy almost full capabilities of the host hardware. In the following example, I used an Amazon instance on the cloud with 32 CPUs as a host, and the emulated ARM image was happily utilizing all those 32 CPUs. Here is how:

sudo apt-get install qemu-user-static

This package has all the binaries needed and also registers the correct binfmt_misc enteries for you. We are now ready to build a new ARM system and chroot it:

dd if=/dev/zero of=my.img seek=20000000000 bs=1 count=1
mke2fs -F my.img
mkdir my.mnt
mount -o loop my.img my.mnt
qemu-debootstrap --no-check-gpg --arch=armhf sid my.mnt

mount -o bind /dev my.mnt/dev
mount -o bind /sys my.mnt/sys
mount -o bind /proc my.mnt/proc

chroot my.mnt
echo -e "deb sid main\ndeb-src sid main\n" > /etc/apt/sources.list
apt-get update

Once you exit your chroot (via CTRL-D or ‘exit’), don’t forget to unmount it:

umount my.mnt/{dev,sys,proc}
umount my.mnt

And thats it. Performance and usability is great, since we simply ‘login’ (via chroot) to our image. Enjoy.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s