5,606,722 Members 37,674 Now Online
XDA Developers Android and Mobile Development Forum

[HOWTO] Install LinuxOnAndroid distros on your sdcard's second partition.

Tip us?
 
davidheis
Old
(Last edited by davidheis; 6th January 2014 at 05:42 PM.)
#1  
davidheis's Avatar
Member - OP
Thanks Meter 9
Posts: 61
Join Date: Sep 2012
Location: Delhi
Default [HOWTO] Install LinuxOnAndroid distros on your sdcard's second partition.

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:
  1. Root access on your android device
  2. Busybox
  3. 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.
  4. 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.
  5. 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
Code:
mke2fs -t ext3/4
doesn't work. So I used aParted to format the second partition as ext2 and ran
Code:
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
Code:
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
Code:
mount
in the terminal. Look for the line
Code:
/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.

Code:
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.

Code:
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.
Attached Thumbnails
Click image for larger version

Name:	mount.png
Views:	645
Size:	76.6 KB
ID:	2496448   Click image for larger version

Name:	start.png
Views:	641
Size:	63.1 KB
ID:	2496477   Click image for larger version

Name:	shut.png
Views:	643
Size:	64.6 KB
ID:	2496478   Click image for larger version

Name:	desktop.png
Views:	643
Size:	45.9 KB
ID:	2496479   Click image for larger version

Name:	mnt.png
Views:	561
Size:	19.2 KB
ID:	2498471  

Click image for larger version

Name:	ubuntu.png
Views:	566
Size:	29.1 KB
ID:	2498472   Click image for larger version

Name:	location.png
Views:	563
Size:	42.8 KB
ID:	2498484  
Attached Files
File Type: zip Scripts.zip - [Click for QR Code] (2.8 KB, 52 views)
The Following User Says Thank You to davidheis For This Useful Post: [ Click to Expand ]
 
davidheis
Old
(Last edited by davidheis; 8th January 2014 at 06:49 PM.)
#2  
davidheis's Avatar
Member - OP
Thanks Meter 9
Posts: 61
Join Date: Sep 2012
Location: Delhi
If you have been successful in achieving the goal of this tut, please leave a feedback with your device name and any modifications that you had to make, if any, to the whole process. That will be of great help to others.


EDIT: RISK OF DOUBLE BOOTING!

In case of booting from linux image, when you (accidentally) tap on the boot widget or the boot button while linux is already running, the app would display a confirmation dialog to chroot into the mounted image. This may not happen when you are booting from the second sdcard partition.

To prevent errors when you accidentally tap on the boot widget/button, add the following code into the bootscript right after the error_exit() function at the beginning of the script.
Code:
echo "Boot up Linux? Make sure it is safe to boot before proceeding!"
read answer
if [ $answer != muchfunny ]; then
error_exit "Aborted"
fi
Replace "muchfunny" with y or yes or any word of your choice.

When you try to boot, you'll have to enter this word to proceed. If you type in the wrong word, it won't boot. This way, you will have a chance to prevent errors when you accidentally tap on the boot widget while linux is already running.

Any user of LOA, not just those who are booting from linux partition, can benefit from this. You could replace "muchfunny" with a secret code to prevent anyone from messing with your linux installation. So it's like implementing password protection.

I'm using this on my tablet.
The Following User Says Thank You to davidheis For This Useful Post: [ Click to Expand ]
 
joesnose
Old
#3  
Member
Thanks Meter 12
Posts: 95
Join Date: Dec 2009
hello david,

Looks nice and clear, haven't had the chance to try yet but will post my results when i do.

Just wanted to say big thanks for the write up.

Sent from my Nexus 7 using Tapatalk 2
 
davidheis
Old
#4  
davidheis's Avatar
Member - OP
Thanks Meter 9
Posts: 61
Join Date: Sep 2012
Location: Delhi
Quote:
Originally Posted by joesnose View Post
Looks nice and clear, haven't had the chance to try yet but will post my results when i do.
When you do a factory reset in CWM (probably also in other recoveries), the second sdcard partition is wiped.

So it's best to store the backups in the internal sd and remove the sdcard before performing a reset.

If you don't already know, and if you have not run "apt-get clean", you can back up any packages that you downloaded on ubuntu (if you're using it) by keeping a copy of the contents of the archives folder in /var/cache/apt/. In case your linux installation gets corrupted or the partition gets wiped, you can restore the backed-up packages later. So you won't have to download them again.
Tags
android, complete linux installer, linux, linux on sdcard partition
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes