Debian Chroot

From UbuntuPhone
Jump to navigation Jump to search

The following was written by Torsten Sachse and posted to the Ubuntu Phone email list on 16 Jun 2015 and reproduction was permitted under the terms of the GPL. There is also a updated version on Ask Ubuntu, see also this thread on Ask Ubuntu.

Debian Chroot

This is a simple guide describing how to set up a chroot on a Ubuntu touch device. It also explains how to set it up so that the chroot is located on the SD-card to save internal storage space and goes into some detail about why simply installing the chroot on an SD-card is not possible.

The guide is structured as follows:

  1. Comments on file systems and permissions
  2. Using phone only
  3. Using computer and phone (recommended)
  4. Unsing an image file (recommended)

1) Comments on file systems and permissions

Only some file system types support file permissions in a way the chroot needs them. An SD-card likely uses FAT32 which does not support any type of permissions and, hence, the chroot can't run on it. The permissions reported by commands like "ls -l" do not have any significance on such file systems. Some Android handsets for instance support EXT3 or ECT4 on their SD-cards making installing a chroot a lot easier.

2) Using phone only

(not recommended since it breaks over the air updates)

It is possible to install debootstrap on the phone by remounting the system as read-write and executing the following commands. At least it worked on my BQ on r22. Imagine you want to install your chroot into the directory "jessie" which should reside in the current working directory:

sudo mount / -o remount,rw
sudo apt-get update
sudo apt-get install debootstrap
sudo debootstrap --arch=armhf jessie ./jessie http://http.debian.net/debian

This only works on the internal storage without what's explained in section 4).

Or for stretch:

sudo mount / -o remount,rw
sudo apt-get update
sudo apt-get install debootstrap debian-archive-keyring
mkdir /home/phablet/debian
cd /home/phablet
sudo debootstrap --arch=armhf stretch ./debian https://mirrorservice.org/sites/ftp.debian.org/debian

3) Using computer and phone

The problem with this debootstrap-command failing your average computer is that your computer cannot execute binaries compiled for the ARM architechture. This is why you have to add the option --foreign to debootstrap which tells it to only download all packages and leave anything that needs executing of any of the installed binaries to another system. So do (sudo might not be needed):

sudo debootstrap --arch=armhf --foreign jessie ./jessie http://http.debian.net/debian

If you were to install an i386 architechture on a x86_64 system, you would not have to specify the --foreign flag as both should be able to run on an ordinary 64-bit processor although they're different architechrures.

Now best create a tar archive from this which you then copy to your phone. It does not matter whether the archive is put on the SD-card or the internal storage. The second command is needed to turn on ssh support, which is reset upon every reboot.

tar -cvzf jessie.tar.gz jessie
sudo service ssh start
scp jessie.tar.gz phablet@ubuntu-phablet:DIRECTORY_OF_YOUR_CHOICE

Now you only have to extract the archive wherever you want the new directory "jessie" to reside. Without what's expained in section 4), this will only work on your internal storage. Then, you only have to go into your chroot and finish the installation process. So do on the phone (the first command is needed since the terminal app is somehow restricted to execute binaries):

ssh localhost
tar -xvzf jessie.tar.gz
sudo chroot jessie /bin/bash
/debootstrap/debootstrap --second-stage
exit

Now, you should be able to chroot into your new system using the third of the above commands.

4) Using an image file

First, create an image file on the system on which you execute the first debootstrap command. To create an 1GB image, do

dd if=/dev/zero of=IMAGE.img bs=1G count=1

Note that the B in GB is left out. "if" stands for "input file", "of" for "output file" and "bs" for "block size". There are more efficient commands out there, but those only work on certain types of file systems, AFAIK. This one also works on an SD-card or a tempfs. Please note the use of /dev/zero and not /dev/random or /dev/urandom as that would take a lot longer and make it take a lot longer to transfer the image between devices.

Mount the image file to a loop-device and create a file system on it. Without a file system, you cannot yet mount the image to a directory, only to a block-device. So do

sudo losetup -f

and remember the output. Replace every "LOOP" in the following commands by the output of the above command. First, assign the image to a block device:

sudo losetup LOOP IMAGE.img

Next, create the file system on it:

sudo mkfs -t ext4 LOOP

Next, detach the image from the loop device:

sudo losetup -d LOOP

If you get command not found errors, just install the missing programmes. Now, create the directory and mount the image file to it:

mkdir jessie
sudo mount -o loop IMAGE.img $(pwd)/jessie

The command "pwd" will make sure you give mount an absolute path to your current working directory. Now, use the directory "jessie" with debootstrap as explained above (the command including the --foreign flag). After you're finished, umount the image via

sudo umount $(pwd)/jessie

and create an archive of the image file (will compress all the zeroes in the archive to almost nothing) and transfer that to your phone. Both SD-card and internal storage are fine. On your phone, create the directory "jessie" somewhere on the internal storage, mount the image to it and perform the second stage.

You should now have a nice chroot on your phone that does not waste internal storage space.