Monday, December 8, 2008

Using 32-bit applications in 64-bit Debian with chroot

Some time ago I switched to 64-bit Debian on my desktop. I used to be scared off by the fact that certain, important applications were not yet available in 32 bit, such as Adobe's flash player. With some workarounds some of these programs are workable in 64 bit, for example nspluginwrapper uses the 32 bit version of flash and forces it to play nice with your 64 bit browser, with moderate to good success. (Adobe also just released their alpha version of 64 bit flash for Linux, but it kept causing my browser to crash.) For other programs, though, there is no way to run them effectively (or at all) in 64 bit. Until recently there were very few programs I wanted in this category (really, only TweetDeck, a twitter application on the Adobe AIR interface), but suddenly there have been a few more programs that I wanted/needed that are only available in 32 bit versions. The solution? Run them in 32 bit using a chroot! A chroot is sort of like a disk within a disk. It is often used for testing so that developers can test a program without it destroying the filesystem on their machine. Here, we are using it to install a 32 bit Debian inside a 64 bit Debian. This process is also possible to do on Ubuntu, with slightly modified commands. *Note, most of this comes from this page, but I have cleaned it up, fixed some typos and, in my opinion, made it easier to understand.

Step 1: Set up the base file system
First you need to set up the base file system. This is going to install a very minimal version of 32-bit Debian Sid inside your 64-bit Debian install. So first, make a directory for this second install and then use debootstrap to install (as root or with sudo):
mkdir /var/sid-386-chroot
debootstrap --arch i386 sid /var/sid-386-chroot http://ftp.debian.org/debian/

This will download a number of packages for the chroot and install them. Depending on your internet connection this may take a little while. When it is all done you should see that you have a whole file system inside /var/sid-386-chroot, similar to your main file system.

I am not entirely sure what the next steps do, but the howto I followed states that you have to add the library path of your chroot to your ld.so.conf, and you need a link to your 32 bit linker in the /lib path. In any case, what you need to do is add the following lines to /etc/ld.so.conf:
# chroot i386 system libs
/var/sid-386-chroot/lib
/var/sid-386-chroot/usr/lib
/var/sid-386-chroot/usr/X11R6/lib
/var/sid-386-chroot/usr/local/lib

Then you need to run these commands in the terminal:
cd /lib
ln -s /var/sid-386-chroot/lib/ld-linux.so.2 ld-linux.so.2
ldconfig


Step 2: Set up the chroot to run alongside your 64 bit install
For your 32-bit applications to work just like they were on your 64 bit install, certain parts of your 64 bit install have to be accessible to your chroot (normally, a chroot can't access anything outside of the chroot. It's called a chroot jail and it is why developers use chroots to test programs.) For this, we use what is called a bind mount, which is used to mount a directory, such as /home, to the home folder inside the chroot. There are a few folders which should be mounted in the chroot, and this can be done by modifying the /etc/fstab file and adding the following lines at the bottom:
/home /var/sid-386-chroot/home none bind 0 0
/tmp /var/sid-386-chroot/tmp none bind 0 0
/proc /var/sid-386-chroot/proc proc defaults 0 0
/dev /var/sid-386-chroot/dev none bind 0 0
/usr/share/fonts /var-sid-386-chroot/usr/share/fonts none bind 0 0

It is VERY IMPORTANT to note that now that these folders are bound to their locations on your main file system, if you ever decide to delete your chroot, make sure that you unmount these bind mounts or else you can end up erasing these folders on your main file system. Also make sure that there are directories for each of these mounts. The only one you should end up having to make is /media/cdrom0. Then mount them:
sudo mkdir /var/sid-386-chroot/media/cdrom0
sudo mount -a

If all went well you should get no error messages and these locations will be bound to their counterparts in the chroot.

Lastly, you need to make sure that all of the users on your machine also exist in the chroot. This can be done by copying certain files to the chroot:
# sudo cp /etc/passwd /var/sid-386-chroot/etc/
sudo cp /etc/shadow /var/sid-386-chroot/etc/
sudo cp /etc/group /var/sid-386-chroot/etc/
sudo cp /etc/sudoers /var/sid-386-chroot/etc/
sudo cp /etc/hosts /var/sid-386-chroot/etc/


Now you should be able to enter the chroot and run programs from within. To get into the chroot (which you must be root to do) you can type:
sudo chroot /var/sid-386-chroot

Now you can run commands. Try out a command or two. Keep in mind that the chroot is a brand new Debian install, so most of the programs you use will not be installed yet. For example, if you try running gedit, it won't run because gedit is not yet installed in the chroot. You can install it and other programs with apt-get or aptitude just like in your 64 bit install. Should you ever want to exit the chroot, simply type "exit"

Step 3: Installing your 32-bit applications
Anything in the Debian repositories is as easy as entering the chroot and installing it via the apt-get or aptitude command line. Keep in mind that since it is a different install you will have to add contrib and non-free to your sources.list file if you want to use those repositories. Downloaded .deb files are a bit tougher. Download them to your home directory and install them with dpkg -i (from within the chroot), for example, if you wanted to install skype (a 32-bit only app), download it, navigate to the directory where you downloaded it in the terminal, and type:
dpkg -i skype-debian_2.0.0.72-1_i386.deb
. This method is a bit more tedious because, since the chroot is a brand-new, minimal install, there are most likely a host of dependencies that are not installed yet. This means that it will come up with all sorts of errors about dependencies. For each dependency that it is lacking, use apt-get or aptitude to install them.

You may also get a lot of errors about locale settings. To fix this, I had to install locales and reconfigure it:
sudo apt-get install locales
sudo dpkg-reconfigure locales


You should now be able to run your 32 bit application from within the chroot. (Note: TweetDeck did not function properly until after the next step, but other applications ran okay.) In this example, while in the chroot simply type "skype" into the terminal and skype will launch.

Step 4: Make it work seamlessly from your 64-bit install.
So you got your application up and running, but as you are probably already thinking, entering the chroot and running the application is rather tedious. So the next step is to work up a script that will load it in one fell swoop. First, make sure you exit the chroot and install dchroot, which will allow you to run programs installed in the chroot from your 64-bit Debian.
sudo apt-get install dchroot

Now you need to edit (or create) /etc/dchroot.conf and add:
# sid386 chroot
sid386 /var/sid-386-chroot

This essentially names the chroot and defines where it is located, so that dchroot knows where to look when it is run. Now you should be able to launch skype using dchroot without having to enter the chroot itself by running the command:
dchroot -c sid386 -d skype

To make life even easier, we can write a simple script to call that command whenever we want to launch Skype. While you could certainly write a script to specifically launch Skype, it would mean that you would have to make a new script for every 32-bit application you install. Instead, we are going to create one universal wrapper script. Make the following script called /usr/local/bin/do_chroot:
#!/bin/sh
ARGS=""
for i in "$@" ; do
ARGS="$ARGS '$i'"
done

exec dchroot -c sid386 -d -q "`basename $0`" "$ARGS"

I am no scripting expert, but basically what this script does is it uses the name of the script to determine what program to run from the chroot, and will also take any arguments given and append them to the end of the dchroot command. Make sure it is executable ("sudo chmod 755 /usr/local/bin/do_chroot"). Now, make a link to the script called "skype" in /usr/bin:
sudo ln -s /usr/local/bin/do_chroot /usr/bin/skype

This script is slick in the fact that it will use the name of the link in the script to run the whatever program the link is named after. This means that when you make your link, make sure it is typed exactly as it needs to be to launch it in the chroot (So, in this example, make sure it is "skype" and not "Skype" or "launch_skype" or anything else. Just "skype".) If all has worked correctly, all you have to do now is type "skype" into the terminal to launch the 32-bit only Skype!

Conclusion
It is a little bit of effort to set up, but after the chroot is set up, new 32-bit applications can be installed by entering the chroot, installing the application, exiting the chroot, and linking to the do_chroot script. Using this method, I have installed and use the following 32-bit only applications:
  • Skype
  • TweetDeck (which uses the Adobe AIR environment, currently only for 32-bit)
  • SwiftFox (The 64-bit Flash Player alpha kept crashing IceWeasel so I switched to SwiftFox and a 32-bit Flash Player)
  • Amazon.com MP3 downloader