[Sprint Samsung Galaxy Note 3] Homefix:
Build your own Ubuntu Filesystem and Run (chroot) with Android

NOTE: This is an ongoing project for me so I am not done with this post,

Everything works but obviously room for Improvement. Starting with the Mounts in script, not sure proper mounts i should use.

e.g.

mount -t devpts devpts $mnt/dev/pts

mount -t proc proc $mnt/proc

mount -t sysfs sysfs $mnt/sys

or:

mount -t devpts none $mnt/dev/pts

mount -t proc proc $mnt/proc

mount -t sys $mnt/sys

or:

mount -t dev $mnt/dev/pts

mount -t proc $mnt/proc

mount -t sysfs $mnt/sys

???

Lets get on with it.

I was having trouble with corrupted image file or ubuntu.img that i made so i tried a different method.

I kept getting input/output errors.

Using a partition on my external sdcard and Mounting it back to the external card,

to hold the file system, instead of an image file, Mounted in a Loop device.

I dont have the time to to get into detail of the steps involved, I gotta get back to work.

If you want to do this bad enough,im sure you will figure it out.

My Tools:

Ubuntu 13.10 setup on VirtualBox in Windows8 Envirement

Sdcard is class 10 64G, however i have tried class 4 16G and 16G class 10 and it worked too.

SdcardFormatter: https://www.sdcard.org/downloads/formatter_4/

Galaxy Note3 SM-N900P

First

any sdcard will do.

Now im not going to get into how to figure out the "start sector" (mine is 2048 for the particular card im using) all i can say is,

if its a "stock card", and you haven't partitioned it before, you should be ok. If your not sure use SdcardFormatter to get card back to stock.

In ubuntu find your card:

open Gparted and top right corner select: yor sdcard

Mine is sdb..

(Make sure all partitions are unmounted)

Click on first partition then select: information...It will tell you the start sector, make note.

select: last partition,

Do same for last step: if applicable for end sector.

Make sure all partitions are Unmounted

MAKE SURE WHEN YOU DO THE NEXT STEP, YOUR NOT DELETING YOUR SYSTEM

delete all partitions,

select: unnallocated space,

select: the unnallocated space,

select: new,

select: space before 0, space after, (leave space for whatever your system or image size is mine was 3.94G) so i left 4G.

select: fat32 for file system

select: ok

select: next unallocated space,

select: space before is 0 space after is 0

select: file system ext4

select: ok

MAKE SURE WHEN YOU DO THE NEXT STEP YOUR NOT DELETING YOUR SYSTEM

DONT FORMAT ANYTHING

select: apply

Check information again, check for start and end sector to see if they match the origanal settings you took note of at the begining.

If they are different, use SD Formater ^LINK^ to set sdcart back to stock setting.

Try again after you do some homework on Partitioning and formating sdcards.

Make your Image:

Install Qemu:

sudo apt-get install qemu-user-static qemu-system

sudo apt-get install debootstrap

Make dirctory:

mkdir ~/arm

Or whatever

Then make another directory:

mkdir ~/arm/ubuntu

Or whatever

Go to that directory:

cd ~/arm

Then:

Make an image...........................Pick one or make your own size:

dd if=/dev/zero of=ubuntu.img bs=8k count=524000 >...3.99

dd if=/dev/zero of=ubuntu.img bs=8k count=350000 >...2.9G

dd if=/dev/zero of=ubuntu.img bs=8k count=510000 >...3.6G...........etc.

Example: for a 4G do:

dd if=/dev/zero of=ubuntu.img bs=8k count=524000

Make Filesystem:

sudo mkfs.ext3 -F ubuntu.img

YES THATS EXT3:

Set the Filesystem checking interval:

tune2fs -c 0 ubuntu.img

Mount Filesystem:

sudo mount -o loop ubuntu.img ubuntu

Bring in Base System:

sudo debootstrap --arch armhf --foreign raring ubuntu

You can change raring for other distros, such as stable, or hoary. look in /usr/share/debootstrap/scripts

Make folders for sdcard links:

sudo mkdir ubuntu/media && sudo mkdir ubuntu/media/sdcard && sudo mkdir ubuntu/media/extSdCard

Or you can use your own sdcard settings

Copy Qemu file over to Ubuntu folder:

sudo cp /usr/bin/qemu-arm-static ubuntu/usr/bin

Run first Chroot:

LANG=C sudo chroot ubuntu /usr/bin/qemu-arm-static -cpu cortex-a9 /bin/bash

Finish Installation:

/debootstrap/debootstrap --second-stage

Exit Chroot

exit

Mount some stuff

sudo mount -t proc none ubuntu/proc/ && sudo mount -t devpts devpts ubuntu/dev/pts

Start second Croot:

sudo chroot ubuntu /usr/bin/qemu-arm-static -cpu cortex-a9 /bin/bash

Set some Variables:

export HOME=/root && export USER=root && export LC_ALL=C

Create User:

adduser [PICK A USER NAME]

Set Root Password:

passwd

Add some Groups for permissions:

groupadd -g 3001 aid_net_bt_admin && usermod -G aid_net_bt_admin -a dad && groupadd -g 3002 aid_net_bt && usermod -G aid_net_bt -a dad && groupadd -g 3003 aid_inet && usermod -G aid_inet -a dad && groupadd -g 3004 aid_net_raw && usermod -G aid_net_raw -a dad && groupadd -g 3005 aid_net_admin && usermod -G aid_net_admin -a dad && groupadd -g 3006 aid_net_bw_stats && usermod -G aid_net_bw_stats -a dad && groupadd -g 3007 aid_net_bw_acct && usermod -G aid_net_bw_acct -a dad && groupadd -g 3008 aid_net_bt_stack && usermod -G aid_net_bt_stack -a dad

For Raring...........................

echo "deb http://ports.ubuntu.com saucy main restricted universe multiverse" >> /etc/apt/sources.list

apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 428D7C01

For Quantal.........................

echo "deb http://ports.ubuntu.com quantal main restricted universe multiverse" >> /etc/apt/sources.list

For Wheezy.........................

echo "deb http://ftp.us.debian.org/debian stable main contrib non-free" >> /etc/apt/sources.list

Dont hold me to the last one you may want to double check.

Update Package Manager

apt-get update

Now you can add packages, these are some of the programs i use:

(i dont like to add all packages at once, i break them up because somtimes i notice dpkg error's when i try all at once).

apt-get install git gnupg flex bison gperf build-essential zip curl

apt-get install xfce4 xfce4-goodies

apt-get --reinstall install xfonts-base

apt-get install samba samba-common system-config-samba

Debian: apt-get install openoffice.org

Ubuntu: sudo apt-get install libreoffice

apt-get install tightvncserver streamtuner2

apt-get install xchat-gnome xchat

MAKE SURE YOU INSTALL TIGHTVNCSERVER..................Thats how you will connect to Ubuntu.........,if you want GUI.

Add permissions for sudo:

visudo

Example root ALL=(ALL:ALL) ALL

dad ALL=(ALL:ALL) ALL

(My username is dad)

Dont know if you need this or not i do it:

chmod a+rw /dev/null

chmod 1777 /tmp

To Fix SDCARD

groupadd -g 1015 sdcard-rw && usermod -a -G sdcard-rw dad && groupadd -g 1016 extSdCard-rw && usermod -a -G extSdCard-rw dad

Exit Chroot

exit

Unmount some stuff

sudo umount ubuntu/proc/ && sudo umount ubuntu/dev/pts && sudo umount ubuntu

Transfer your Image to Sdcard

Now Dump the image to Sdcard:

MAKE SURE THE EXT4 PARTITION ON SDCARD IS UNMOUNTED

My sdcard was sdb, so sdb1 would be partition 1 sdb2 would be partition 2

sudo dd if=ubuntu.img of=/dev/sdb2

...........If you notice below, it took 2.8 hrs to dump the image.

dad@dad-VirtualBox:/media/dad/Development/arm$ sudo dd if=ubuntu.img of=/dev/sdb2

[sudo] password for dad:

8160000+0 records in

8160000+0 records out

4177920000 bytes (4.2 GB) copied, 10371.3 s, 403 kB/s

[UPDATE]

So far I have not found any problems using the copy command to transfer filesystem over to sdcard.

example:

sudo cp -rp ubuntu/* /media/dad/4920cacb-49aa-4366-8fb7-016b8e502406 (only takes 15-20min)

(Make sure your ubuntu.img is mounted in the ubuntu folder) using:

sudo mount -o loop ubuntu.img ubuntu

To Chroot it.............

With sdcard in Phone,

Connect to windows usb:

IN WINDOWS:

Open File Explorer:

Create directory "ubuntu" on root of sdcard

Copy ext4ubuntu.sh to root of sdcard (not in the ubuntu folder)

ON PHONE:

Open Terminal:

su

cd /storage/extSdCard

chmod 777 ext4ubuntu.sh

sh ./ext4ubuntu.sh

Ready to go.

Connect to Ubuntu:

Get bVNC Free from Market

In Terminal type:

login dad

vncserver :0 -geometry 960x540

Open VNC Viewer and enter:

localhost:0 for ip address

and password if you want.

Connect

Notes:

I was able to sudo cp -rp ubuntu/* /media/dad/912c45f4-b0bd-4e14-a55d-d8d9bcd4bd6a (which is the path to my sdcard), worked the second time i tried it.

( I mounted the ubuntu.img then copied all files to sdcard)

The first time i tried it i got dpkg errors.

That took only 20min to copy vrs. almost 3 hrs using dd if=

Here are some after thought I made note of, I havent tried them yet but u can, just let us know your experiances with them.

Some things you can put in a script to run after ubuntu started.

df doesn't work, there is no mtab file containing the currently mounted filesystems:

grep -Ev "rootfs|tmpfs|cgroup|mmcblk|usbfs|asec|storage" /proc/mounts | sort -r | uniq > /etc/mtab

if you can only run vncserver/Xvnc as root Try

chmod 01777 /tmp/.X11-unix

or

Fix Upstart

dpkg-divert --local --rename --add /sbin/initctl > /dev/null 2>&1

ln -s /bin/true /sbin/initctl > /dev/null 2>&1

Sources:

http://the.taoofmac.com/space/blog/2013/04/28/2330

http://whiteboard.ping.se/Android/Debian

http://linux-expert.net/?Trucs_et_astuces___Android___Tutoriel_:_Chroot_Debian

http://xdaforums.com/showthread.php?t=2598481

http://gautampk.tumblr.com/post/23032189096/ubuntuonandroid-linuxshell#

BELOW IS:.....ext4ubuntu.sh

Just copy and paste into txt editor and name it ext4ubuntu.sh

Dont make too much fun of it. Im still learnen.

###################################################################Cut

echo "This will Install Ununtu ...................."

sleep .8

c=5 # seconds to wait

REWRITE="\e[25D\e[1A\e[K"

echo "Starting..."

while [ $c -gt 0 ]; do

c=$((c-1))

sleep 1

echo -e "${REWRITE}$c"

done

echo -e "${REWRITE}Done."

mount -o remount,rw /dev/block/mmcblk0p23 /system

export bin=/system/bin

export mnt=/storage/extSdCard/ubuntu

export TMPDIR=/tmp

export PATH=$bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:$PATH

export HOME=/root

export PWD=/root

export SHELL=/bin/bash

export TERM=linux

export EDITOR=/usr/bin/nano

export VISUAL=/usr/bin/vi

echo "Mounting File System..........................."

sleep .5

mount -t ext4 -o rw,noatime /dev/block/mmcblk1p2 $mnt

sleep .5

echo "Mounting and Binding Neccesary Devices..............."

sleep .5

echo ".................................DONE!.................."

sleep .5

mount -o bind /dev $mnt/dev

echo "dev"

sleep .5

mount -t devpts devpts $mnt/dev/pts

echo "devpts......................................................"

sleep .5

mount -o bind /proc $mnt/proc

echo "proc..................................................."

sleep .5

mount -o bind /sys $mnt/sys

echo "sys..............................................."

sleep .5

sysctl -w net.ipv4.ip_forward=1

echo "inet................................."

sleep .5

echo "MOUNTING SD CARDS................"

sleep .5

mount -o bind /data/media/0 $mnt/media/sdcard

echo "Card: sdcard............"

sleep .5

mount -o bind /storage/extSdCard $mnt/media/extSdCard

echo "Card: extSdCard....."

sleep .5

echo "Setting /etc/resolv.conf to Google Open DNS 8.8.8.8 and 8.8.4.4"

echo "nameserver 8.8.8.8" > $mnt/etc/resolv.conf

echo "nameserver 8.8.4.4" >> $mnt/etc/resolv.conf

echo "Setting localhost on /etc/hosts "

echo "127.0.0.1 localhost" > $mnt/etc/hosts

echo "Ubuntu Has Started "

echo "Enjoy "

echo "..."

chroot $mnt /bin/bash

echo " "

echo "Shutting down Ubuntu"

busybox umount $mnt/dev/pts

busybox umount $mnt/proc

busybox umount $mnt/sys

busybox umount $mnt

echo "...It might have Worked... "

#####################################################################END