How to apply OTA update or UNROOT rooted nexus 5 without loosing user data.

These days I got notification from my phone (Nexus5 with android 5.0) that I have update to android 5.0.1.

The phone is rooted without modification of the boot partition. 

I tried to update, the phone rebooted and started the update process and after a few seconds it say ERROR, and stops.

It appears that the updater checks for modifications on  system partition and if some are found, the update is stopped.

Here is how I managed to update, without flashing all back to stock and suffering the pain of reinstalling all my apps, loosing user data (if not backed up), loosing app states and so on, basically a lot of time can be lost.

The below instructions will probably work on all nexus devices with slight modifications.

All the commands which I am going to present you are tested on linux, but they should work on windows and mac too, as long as you have downloaded android SDK.
 
I assume that your bootloader is unlocked. If not, you can not do anything without wiping out the phone.

How to check if bootloader is locked, two ways:

1. When you power on the phone you see the GOOGLE logo. If the bootloader is unlocked you will see unlocked padlock below the google logo.
2. Switch OFF the phone, then hold for 5-6 seconds Volume Down + Power.

This will start the bootloader for you (one lying down android with open stomach :))
Below the android you will see some info, about your phone, bootloader version etc.
On the last line it says: "LOCK STATE - unlocked"  if bootloader is unlocked


1. Download android sdk. http://developer.android.com/sdk/index.html#Other

If you do not intend to develop, you DO NOT need the "android studio", just download 'SDK tool only"

2. If undr Linus/Mac, extract the contents of the package

3. Run android-sdk-linux/tools/android. This will start GUI which will let you choose which components to install

4. Choose "Android Platform SDK tools" and hit install

5. If all went fine, you will have folder android-sdk-linux/platform-tools/ which contains the tools you need.

6. Switch On "usb debugging" on your device.

7. When you connect the phone, try this simple command:

sudo  android-sdk-linux/platform-tools/adb shell

The phone will probably pop up a dialog, asking for confirmation to allow the connection from the computer.
Hit YES and the command you executed will be drop you in a shell on the phone.
If this happens then all is good, you can continue

8. Download the stock images for your current version of android:
https://developers.google.com/android/nexus/images

9. Extract the file, in my case (hammerhead-lrx21o-factory-01315e08.tgz). You will get ZIP file called hammerhead-lrx21o/image-hammerhead-lrx21o.zip

10. Extract it too and you will get the image files

cache.img              
boot.img               
recovery.img           
userdata.img           
system.img             

11. If you want to root your phone after the update, download also:
Chanfire installable ZIP: As of time of this writing, version 2.40 (http://download.chainfire.eu/641/SuperSU/UPDATE-SuperSU-v2.40.zip)

You can see which is the latest version here: http://forum.xda-developers.com/apps/supersu

12. Upload the file to your device, you can do this with the following command:

android-sdk-linux/platform-tools/adb push  UPDATE-SuperSU-v2.40.zip /sdcard/Downloads

This will place the file in your Download folder on the phone.

13. Download TWRP rescue image:
http://techerrata.com/browse/twrp2/hammerhead

14. Be very careful from now on :)

15. Go in to fastboot mode by doing either power off the phone, then hit volumedown + power for a few seconds, or if the phone is on, with the following command:

sudo android-sdk-linux/platform-tools/adb reboot bootloader

This is supposed to send you in bootloader.

15. Flash stock recovery image: (you should be in the dir where the extracted images are )

sudo android-sdk-linux/platform-tools/fastboot flash boot boot.img 

reboot bootloader again
sudo android-sdk-linux/platform-tools/fastboot reboot-bootloader

16. Flash recovery image to stock

sudo android-sdk-linux/platform-tools/fastboot flash recovery recovery.img

reboot bootloader again
sudo android-sdk-linux/platform-tools/fastboot reboot-bootloader

17.  Flash the system img

sudo android-sdk-linux/platform-tools/fastboot flash system system.img





18. Reboot the device.
In the bootloader, navigate with (volume +/-) and when it shows "REBOOT", hit power to confirm, this will reboot the phone and boot android. And you now have unrooted device.

19. Apply the update, either go to settings-> about -> check for updates.
If nothing is shown there you can wait for some time and the notification will pop up again. (there is a faster method for update, but I intentionally do not show it here)

20. When the update is applied, if you want to root again the device, do the following:

sudo  android-sdk-linux/platform-tools/adb reboot bootloader

21. Flash the recovery image with the twrp image which you downloaded earlier:

sudo android-sdk-linux/platform-tools/fastboot flash recovery openrecovery-twrp-2.8.2.0-hammerhead.img

22. Reboot bootloader

sudo android-sdk-linux/platform-tools/fastboot reboot-bootloader

23. With volume +/- navigate to menu "Recovery Mode"
24. You will see a interface with buttons.
25. Go to Install
26. Navigate to your download folder and select the file you uploaded there earlier. In current case this is UPDATE-SuperSU-v2.40.zip
27. At the bottom "Swipe to Confirm Flash"
28. If all went fine, your device is rooted at this step
29. Reboot the phone and enjoy your root.

How to concatenate files containing columns of data in Linux

Ever wondered how to concatenate text files containing columns next to each other ?

One little bash trick which can help you do this the easy way:

Assume you have for example three files containing something like this:

file1.txt

a    b    c

a    b    c

a    b    c

a    b    c

 

file2.txt

1    2    3

1    2    3

1    2    3

1    2    3

file3.txt

m    n    p

m    n    p

m    n    p

m    n    p

What we want to achieve is the following result:

a    b    c    1    2    3    m    n    p

a    b    c    1    2    3    m    n    p

a    b    c    1    2    3    m    n    p

a    b    c    1    2    3    m    n    p

You have two options

Option 1: Try to do it in bash and die in pain. This is usually not funny.

Option 2: The easy way, use the command "paste":

[user@host ~]$ paste file1.txt file2.txt file3.txt
a    b    c     1    2    3     m    n    p

a    b    c     1    2    3     m    n    p

a    b    c     1    2    3     m    n    p

a    b    c     1    2    3     m    n    p

 

The command paste will concatenate the files for you

The program have some cool options, like you can set separator, or use the -s option which will cause the following behavior "paste one file at a time instead of in parallel"

Here is an example with the same files:


[user@host ~]$ paste -s file1.txt file2.txt file3.txt

a    b    c             a    b    c             a    b    c             a    b    c

1    2    3            1    2    3             1    2    3             1    2    3

m    n    p          m    n    p            m    n    p            m    n    p

Each row, becomes a new column.

Have fun :)