[Android General] davidheis:
Hello friends! I'm a year long follower of the LinuxOnAndroid project. I have always wanted to install ubuntu on my sdcard instead of using image files. I waited a long time for an instruction to crop up somewhere on the internet about how to do this, and in the meantime, learnt a lot of things. Then I decided to experiment with the idea a few days ago and also managed to get satisfying results. There was a significant performance boost compared to using a linux image.

While writing this tutorial, I have assumed that you are already quite familiar with the LOA project and that you have already managed to run a linux distro on your android device.

Please note that while this tutorial aims to be applicable for all distros released under the LOA project, I have only run Ubuntu 12.04 small, Ubuntu 13.10 small and Debian small images. I have never had any experience with the other distros. But I'm pretty confident that the instructions can be applied to other distros without any problem. If you encounter any problem while applying the instructions for your device and distro, I'm willing to help you as much as I can.

Warning: I've never bricked my android devices while rooting, modding or hacking them and I hope you will be as lucky. But please remember that I will not be held responsible for any damage or losses suffered by you or your devices while following the instructions given in this tutorial. You will do so at your own risk.

Let's begin.

First of all, Things You'll Need:

Root access on your android device

Busybox

An sdcard of optimum storage capacity. You decide what's optimum for your case. I have a 16GB card with a 4GB partition for ubuntu small image.

Init.d script support for auto-mounting second sdcard partition on boot. You can skip this if you want to mount the second partition only when booting up linux.

Attached zip containing bootscript.sh and 03ubuntu files.

Step 1: Partitioning the sdcard

WARNING: This will erase all data on your sdcard!

Use MiniTool Partition Wizard for windows, gParted for linux or aParted for Android to create the two partitions on your sdcard. I reccomend that you do the partitioning on your PC. Note that you cannot partition your sdcard by connecting your device to your PC my USB. You need to use a card reader. If you have a USB modem with built in card reader, you can use that too.

The first partition will be used by your android device for mass storage. Make it FAT32 or exFAT or vFAT. Note that if you make the first partition exFAT, custom recoveries like CWM will not mount it. This can cause problems, for example, when you are trying to restore a nandroid backup from your sdcard.

As for the format for the second partition (let's call it the linux partition), it depends on what linux filesystems your device supports. You can't use (ex/v)FAT(32). You can only use linux filesystem for installing a linux OS.

If you are using aParted to partition the sdcard, not every format will work. My tablet supports mkfs.ext2 and make_ext4fs but

mke2fs -t ext3/4

doesn't work. So I used aParted to format the second partition as ext2 and ran

make_ext4fs /dev/block/mmcblk0p2

in terminal emulator. So my linux partition is ext4. You need to unmount your sdcard to use aParted.

NOTE: Before creating your second partition, make sure your device is able to mount the format that you want to use. Run the

cat /proc/filesystems
or
mount

command in terminal emulator to check for supported filesystems.

STEP 2: Mounting the sdcard partitions

WARNING: If you are using the Link2SD app or any other app2sd app, you might be prompted to create mount scripts for the second partition. DO NOT CREATE THE MOUNT SCRIPTS. Our linux partition is not meant for app2sd.

In this tutorial, we will be mounting the linux partition automatically on startup. For this, you'll need init.d script support on your device. You can use Uni-Init to check/enable init.d easily. If your device does not support init.d and you've failed to enabled it, you could go for more advanced methods like unpacking your boot image and editing init.rc to mount the linux partition. You could also try the Script Manager app. You can also choose to mount the linux partition only when you boot up linux. Let me know if you'd like instructions for this.

Assuming that you already have init.d support, let us proceed.

Download the attached 03ubuntu script and using a root explorer copy it to /system/etc/init.d/here. I use ES File Explorer. Set file permissions to rwx-rwx-rwx or 777(? I'm not very familiar with numerical permissions). rwx-r-xr-x might also work. You can also use terminal to copy, set file permissions, etc. It's up to you.

EDIT: WARNING! Depending on your device, your MicroSD card might me mmcblk1. You can check using the mount command or from the aParted app. It is mmcblk1 on one of my friends' phone. If this is the case you must edit the 03ubuntu script to replace mmcblk0p2 by mmcblk1p2

The 03ubuntu script mounts the linux partition as ext4 at /mnt/ubuntu. The mount options in the script are meant for the maximum performance but may reduce reliability. I haven't had any problems yet. If your linux partition is not ext4, edit the script accordingly. Some of the mount options used in the script are meant for ext4 filesystems only. Google "best/optimum ext(whatever your linux partition is) mount options" to learn more. My linux partition is mounted with noatime option but becomes relatime sometimes after shutting down linux. Somebody please tell me why this happens.

After placing the script in the init.d directory, reboot your phone.

After rebooting, open the /mnt folder. If you see a folder named ubuntu, congratulations, the init.d script worked. But is the linux partition mounted? You can check by opening the /mnt/ubuntu folder. If you see a lost+found or LOST.DIR folder, the partition is very likely to be mounted. Confirm it by running

mount

in the terminal. Look for the line

/dev/block/mmcblk0p2 /mnt/ubuntu ext4 rw,noatime,user_xattr,barrier=0,data=writeback 0 0

If you see that line, we're ready to move to the next step.

STEP 3: Copying the files from linux image to linux partition

Now, we install your linux distro on the sdcard. Place the image (a fresh one or the one that you've been using) in your sdcard and run the following commands. Note: You might need to make changes to the commands according to your image location and format.

su
mkdir /data/local/mnt
mknod /dev/block/loop255 b 7 255
losetup /dev/block/loop255 /mnt/sdcard/ubuntu.img
mount -t ext2 /dev/block/loop255 /data/local/mnt
cp -dpR /data/local/mnt/* /mnt/ubuntu
## wait till copy is complete ##
umount /data/local/mnt
exit

So that was it. You have your favorite linux distro installed on your sdcard now.

But before you go ahead and press that BOOT LINUX button, we still have some things left to do to make it possible to boot from sdcard.

STEP 4: Editing the bootscript.sh and init.sh files

Actually, I did the bootscript editing part for you. I made all (and only) the changes that are required to boot from the sdcard. The modified bootscript is based on the latest (v8) version. Things like sdcard and external sdcard mounts and binds are unchanged. I added comments in the script with my name to mark places where I made the changes. You can look through it if you like.

So now copy the modified bootscript.sh file inside /data/data/com.zpwebsites.linuxonandroid/files/here and set file permissions to rw-------.

After that, comes init.sh. Although the bootscript is common for all distros, it may not be the case for init.sh. You'll find it in /mnt/ubuntu/root/init.sh.

Copy it to your sdcard and keep a copy somewhere safe. Open the script in a text editor (I use Jota+ text editor) and find the following lines and remove them.

if [ $# -ne 0 ]; then
cfgfile=/root/cfg/$1.config
if [ -f $cfgfile ]; then
echo "Using config file $cfgfile"
else
echo "Config file not found, using defaults!($cfgfile)"
fi
fi

The original permissions for the init.sh file are rwx-rwx-rx

After that, place the modified init.sh file back inside /mnt/ubuntu/root/here and don't forget to set correct file permissions.

STEP 5: Final steps and booting linux

Everything is in place and now you can boot up your linux, or can you? Nope. The Complete Linux Installer app won't let you boot if it does not find an image file.

For this, create an empty file inside /mnt/ubuntu/here and name it ubuntu.img.

In the app, enter the location of linux image as /mnt/ubuntu/ubuntu.img and tap on the awesome boot button.

Voila! You've now successfully installed linux to your sdcard and managed to boot it up too! Well done, my son.

Aaaand...Just a reminder: Don't Update Script if you want to continue booting from thee linux partition.