Porting Debian Linux to your cell phone part 2 of 3

If you are following along from the previous post: great! Good work. I’m glad that you are still with me! Today’s changes are actually all you need to do to have a dual phone, where you start Linux first, and then Linux turns on Android. With this setup, you actually have the ultimate rooted phone, because you can SSH into your own Linux, which is the boss of your Android ROM. Linux, being the head honcho, can do amazing things, such as change the size of your Android partitions! With this great power, however, comes great responsibility. So handle with care!

Now for today’s steps:

#1. Download:
Debian system (WARNING – this is almost 700 mb!)
[url]http://www.mediafire.com/download/88r6sr3u33bs532/20151106.tar.gz[/url]
Very tiny busybox (1.06 mb)
[url]http://www.mediafire.com/download/4uhu93prdtlqdmr/busybox[/url]

NOTE: These files will get you started. Later, you can build your own busybox and Linux root file system if you want, these worked for me, so I think it is best to start by using a known working source.

#2. Partition your sdcard:
I use a computer to do this, but there are many phone tools available also. You can later transfer all of this internally, and do this without an sdcard, but this is the best way to get started, as you learn, you can move it all to the phone.

My computer has an sdcard slot, you may use an adapter, or use your phone, it really doesn’t matter. The sizes here are a suggestion, but you will need 2 partitions on the card. I am using an 8 GB card as my example.

Partition Type Size Notes
1 fat32/vfat 2 GB (or more if your card is bigger)
2 ext4 6 GB (or the remainder)

Now, the first partition will be “seen” by Android. The second partition will not typically be seen by Android.

#3. Unzip the Debian system.
First, make a folder in your playgroup folder called sdcard. (~/playgroup/sdcard)
Right clicking on it will most likely work, depending on your distribution, but if you use the terminal, the command unzip should do it. When you unzip the file, it may error after completing the task, that is usually okay. It is a difference in zip endings for Linux/Windows. All the 97151 files should be there, you can right click on the folder to verify. It will be in a folder called 20151106, in that folder, you will see a bunch of folders, such as android, proc, lib, etc, and so on. Copy all of those files to your ~/playgroup/sdcard folder.

Now you have a basic Debian Linux system with an XFCE desktop and a user all ready to go. We will need to change a few things that are specific to your phone though to make this work right.

#4. Copy the original boot stuff to your sdcard folder.
In that ~/playgroup/sdcard folder, is a folder called android (~/playgroup/sdcard/android). This folder currently contains all of the ramdisk for the Cranium Kernel on an i927. You will go ahead and delete everything in the ~/playgroup/sdcard/android folder. It is useless to you.

When you broke down your boot.img file, you were given a new folder called “./boot.img-ramdisk/” this is the contents of your ramdisk for your kernel. Copy all of the files in the boot.img-ramdisk folder to the android folder. Make sure you copy, not move, you will need the other copy for modification shortly.

#5. Gather some information.
Install CM11 to your phone, if you have not already done so. Insert your sdcard. Turn on your phone. Once done, we need to get some information from it. Install a terminal app and give it root permission, or use adb shell from you computer, either way works. Now gather the following information:

[CODE]
$ su
# mount

…..info you need…..
[/CODE]

Write down, copy, or screenshot all of this information.

[CODE]
$ su
# ls /dev
…..info you need…..
# ls /dev/input
…..info you need…..
# ls /dev/graphic <—–(or graphics depending on your phone)
…..info you need…..
[/CODE]

Write down, copy, or screenshot all of this information. You may need some of it later.
WHY:
We will need the partition details and the frame buffer details.

Turn off your phone and remove the sdcard.

#6. Make some changes to init.stage2 file.
There should be a file called init.stage2 in the etc folder: ~/playgroup/sdcard/etc/init.stage2
Open that file with a text editor. Line 43 should say this:

[CODE]
export FRAMEBUFFER=/dev/graphics/fb1
[/CODE]

Change the last part “fb1” to be the earliest fb you wrote down from step 5: “# ls /dev/graphic”. So if you have an fb0, put that here. If it starts at fb1, put that here. save the file. You can close that now.
WHY:
Currently, it was set to the frame buffer I was using. We need the frame buffer (screen) for your phone.

#7. Edit your rc.local file.

Open the ~/playgroup/sdcard/etc/rc.local file.

NOTE: There is also an ~/playgroup/sdcard/etc/init.d/rc.local file, that is not the file you want.

It should say:

[CODE]
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will “exit 0” on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Start the ssh client, in the event you need it.
/etc/init.d/hostname.sh start
/etc/init.d/ssh start

# Clean up bad crash before starting x server.
# /sbin/busybox mkdir -p /tmp/.X11-unix/remove
# /sbin/busybox rmdir /tmp/.X11-unix/remove
# /sbin/busybox rmdir /tmp/.X11-unix/

# Start the x server, warning, if the touchscreen or keypad doesn’t work
# then you cannot escape without killing power!
/usr/bin/startx &
#export USER=root
#vncserver :5000

exit 0
[/CODE]

At this time, change it to say:

[CODE]
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will “exit 0” on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Start the ssh client, in the event you need it.
/etc/init.d/hostname.sh start
/etc/init.d/ssh start

# Clean up bad crash before starting x server.
# /sbin/busybox mkdir -p /tmp/.X11-unix/remove
# /sbin/busybox rmdir /tmp/.X11-unix/remove
# /sbin/busybox rmdir /tmp/.X11-unix/

# Start the x server, warning, if the touchscreen or keypad doesn’t work
# then you cannot escape without killing power!
#/usr/bin/startx &
#export USER=root
#vncserver :5000

exit 0
[/CODE]

Save the file, and you may now close that window.
WHY:
Currently, it was set to start the openssh server, then the x server. We want to only start the ssh server so we can get the proper information for the x server. We will change this file back later when we have the information.
#8. Put Debian Linux on the sdcard’s second partition:
Here is where things will differ for you and me. I don’t know where your card will mount, your system may auto-mount your sdcard when you put it in, but here is what you need to do:

-Mount the second partition to /mnt, or figure out where it is mounted.

[CODE]
$ cd ~/playgroup/sdcard/
$ sudo su
<enter your password>
# cp -Rav ./* /mnt
# sync
# exit
$ exit
[/CODE]
WHY:
If you drag and drop, some files will not copy right. We need all files to have the proper permissions and the proper file attributes.
Now you can un-mount the partitions and remove the sdcard from the computer/adapter.

#9. Prep your new boot image.
Okay, here is where rubber meets the road. Follow close, because this is really important.
The

[CODE]<—[/CODE]

are my comments, don’t type those (obviously).

[CODE]
$ cd ~/playgroup/bootimage/boot.img-ramdisk/ <—to get to the right place.
$ rm -rf * <—to empty out the ramdisk
$ mkdir data
$ mkdir dev
$ mkdir mnt
$ mkdir proc
$ mkdir sbin
$ mkdir sys
$ mkdir system
$ touch init
$ mkdir ./mnt/root
[/CODE]
WHY:
This makes the empty directory that you need. Most of this will be populated when the phone starts by busybox.

Remember that busybox file you downloaded? copy it into the sbin folder. This busybox will actually be the heart and soul of your computer/phone during startup. The Kernel will only interface with it at first.

Now open the init file with gedit or some other text editor, and fill it in with this:

[CODE]
#!/sbin/busybox sh

# initramfs pre-boot init script

# Mount the /proc and /sys filesystems
/sbin/busybox mount -t proc none /proc
/sbin/busybox mount -t sysfs none /sys
/sbin/busybox mount -t tmpfs none /dev

# System needs a few cycles here
/sbin/busybox sleep 1

# Populate /dev
/sbin/busybox mdev -s

# Mount the root filesystem, second partition on micro SDcard
/sbin/busybox mount -t ext4 -o noatime,nodiratime,errors=panic /dev/mmcblk1p2 /mnt/root

# Clean up
/sbin/busybox umount /proc
/sbin/busybox umount /sys
/sbin/busybox umount /dev

# Transfer root to SDcard
exec /sbin/busybox switch_root /mnt/root /etc/init
[/CODE]

Save the file and close it. Now, this only works if your step 5: “ls /dev” has /dev/mmcblk1p1 and /dev/mmcblk1p2, and no higher /dev/mmcblk1p* numbers. There should also be a bunch of /dev/mmcblk0p* numbers, that is okay, that is your phone’s internal storage. This should be correct, but if you do not have that in step 5, then let me know and we will look at your output from step 5.

Next, we will create the boot image. REMINDER: the mkbootimg line below was for the Samsung Galaxy Core 2 SM-G355HN! Put your boot parameters in that you got from the previous posts instructions on how to “break down” the boot image with unmkbootimg!

[CODE]
$ cd ~/playgroup/bootimage
$ rm initramfs.cpio.gz
$ cd ./boot.img-ramdisk/
$ sudo su
<enter your password>
# chmod a+x ./init
# chmod a+x ./sbin/busybox
# find . | cpio –quiet -H newc -o | gzip > ../initramfs.cpio.gz
# cd ..
# mkbootimg –kernel zImage –ramdisk initramfs.cpio.gz –base 0x0 –cmdline ‘console=ttyS1,115200n8 androidboot.selinux=permissive’ -o new_boot.img
[/CODE]

-With fastboot, heimdall, heimdall-frontend, or Odin, flash the new_boot.img to the boot partition of your phone. Make sure you already have your prepared sdcard in your phone. If it auto rebooted, proceed to step 10. If not, power off when done, then power on and go to the next step.

#10. Reboot your phone.
So, make sure your prepared sdcard is in the phone. Your phone probably rebooted when you flashed the new boot image. For the most part, if this worked, boot up will look normal. If it did not work. It should not have booted. CM11 will start and you will have no idea that anything is different. However, if it booted, there is a difference now, and there are a few easy ways to see it.

Install an ssh app on your phone from the play store or with adb. Remember in step 7, we left the openssh server running. This is your ticket “in” to the Linux that is actually in charge of your phone.

open-ssh is installed and running, so using the ssh app, set it for: trondroid@localhost
username is trondroid, password is trondroid

If successful, it should log into the command line of your Debian Linux powered phone! A couple things to remember:
root user has a password of root.
me has a password of me.
trondroid has a password of trondroid. trondroid also has sudo permission.

If you turn on the WiFi in Android, and your computer is on the same network, you can run this in your computer’s terminal to access the phone:
ssh trondroid@xxx.xxx.xxx.xxx (The IP address of your phone, get it from your settings menu.)

Debian actually is CM11’s boss. Debian can delete/move/modify anything in CM11’s world. BE CAREFUL! This is the ultimate root! You can even re-size CM11’s partitions from here. SO BE CAREFUL!

Apt-get works to download any Debian programs you want. We will hopefully get into the graphical stuff in the next few posts. Congratulations! You made it work. Now we just have to set up the buttons, keys, xorg.config, and sound for the Debian part, which we will do next. Let me know how it went, and if you have any trouble! Play around and have fun! Technically, you can stop here if you only want text/console based applications. If you want more, like graphics, you are now 2/3 of the way there! You have successfully put Debian Linux on your phone! Now we just need to finish setting up, so you can have full access and the Graphical User Interface!

Linux – keep it simple.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s