Replacing Android with LineageOS

Fresh Breath of Life

Article from Issue 254/2022
Author(s):

Breathe new life into your old smartphone: The TWRP recovery image and the free Android offshoot LineageOS let you install the latest security updates every week.

Every mobile device needs its own Android build because of numerous drivers that are not available in the source code. The need to maintain every version of Android for every mobile device means that many manufacturers eventually stop supporting updates. Often, smartphones or tablets that still work perfectly can no longer be used without worry because the manufacturer has simply ceased to offer bug fixes and security updates.

The fact that many drivers for smartphone hardware are not available as source code makes it difficult to provide Android upgrades. The LineageOS project [1], the successor to the CyanogenMod project, which was discontinued in 2016, proves that it is not impossible to keep these devices up-to-date. Unpaid volunteers at LineageOS do the work that many manufacturers do not want to do: They combine current Android releases with the required device-specific drivers.

Dewy

The LineageOS project (Figure 1) provides Android systems with a fresh patch status every month for around 300 devices. The builds are released weekly, unless there is a problem during the build. The Devices page on the LineageOS Wiki [2] provides the details of whether a LineageOS build is available for your smartphone or tablet.

Figure 1: The LineageOS team continues work of the CyanogenMod project, which was discontinued in 2016. LineageOS provides over 300, often older, devices with fresh Android updates every month.

Practically all Android devices support a recovery mode that starts a small, specialized Android system to restore the main system in case of damage. Out of the box, recovery mode only installs the state intended by the manufacturer. Therefore, the first step towards installing an alternative OS such as LineageOS is to flash a manufacturer-independent recovery system. I prefer to use TWRP [3] as a recovery tool because, unlike the recovery system supplied by the LineageOS project, it creates backups of the existing system (Figure 2). TWRP (pronounced "twerp"), is a custom recovery tool for Android phones. TWRP replaces the recovery system provided by the hardare manufacturer, which then allows you to install an independent system such as LineageOS.

Figure 2: The TWRP recovery system offers a graphical interface that is friendly enough for non-professionals.

Unless you unlock the bootloader, you can't install another recovery system or a full system on the Android smartphone later (Figure 3). As of Android 6, you can enable the OEM unlock or Bootloader unlock item in the Android settings below Developer options | OEM unlock to unlock the bootloader.

Figure 3: The Android Fastboot bootloader reports "Device is UNLOCKED."

If you do not see the Developer options menu item under System | Advanced, tap the Build number displayed at the end of the heading seven times in the About the phone settings category. You also need to enable the USB debugging option for flashing via USB.

However, this procedure does not remove locks set up separately by the manufacturer to prevent the installation of other software and rooting of the smartphone. Some manufacturers require you to register the device to unlock it, and then – after telling you that the warranty is now void – they hand over a code. Others refuse to unlock the device altogether. However, it is probably not possible, in the EU at least, to make the hardware warranty dependent on you not installing your own software. The LineageOS Wiki page for your device [2] also describes how to unlock the bootloader.

Once you have freed the bootloader, it is time to download the necessary software: First, you need the TWRP recovery image [4] for your device. On your Linux computer, install the Android tools (the Ubuntu packages are android-tools-adb and android-tools-fastboot, otherwise it's just android-tools) containing the Fastboot program as a counterpart to the bootloader on the Android device. Working from the PC, the tools load the recovery image into the partition assigned by the manufacturer's recovery routine. Finally, you need an image of the actual LineageOS system, which the recovery routine finally installs on the system partition after a backup.

Right Choice

Android builds – the final LineageOS as well as TWRP – only work on the device they were built for using the Android open source project and numerous proprietary drivers. To make sure you're installing the right image, you need to know the manufacturer's code name for the device. This will help you identify the right images on the LineageOS and TWRP download pages. The app Device ID [5] gives you the name on a running system (Figure 4).

Figure 4: The Android Device ID app shows the code name of the device; you can then use this code name to find the right image to flash.

Variants of a basic device type can exist in some cases; they are stated in the Device name line in the About phone tab in Android settings. Normally a build should support all variants, but it doesn't hurt to check the LineageOS wiki page for the device matching the codename to see if your model shows up under Supported models.

Once all the software components are in place, boot the Android device to the unlocked bootloader. Most Android devices (with the exception of Samsung, see the box "Installing TWRP on Samsung Devices") rely on Android's own Fastboot bootloader. The LineageOS wiki explains how to access the bootloader for each device. Usually, you can reach the bootloader by turning off the device and then turning it back on while holding down the Volume Down key.

Installing TWRP on Samsung Devices

Smartphones and tablets by Samsung do not use the Fastboot bootloader, which is otherwise common for Android. Samsung has developed its own Windows tool named Odin for flashing firmware. Fortunately, there is a free Linux port named Heimdall [6]; the popular Linux distributions have Heimdall packages.

Volume Up+Home+Power boots a powered-down device to Download mode, which expects data for flashing via USB. If heimdall print-pit on the Linux console prints a series of messages and the device finally boots the normal system, then the communication via USB cable between PC and smartphone is working in Download mode.

After turning the device off again, you can switch back into Download mode using the key combination shown above. The command from Listing 1 installs the TWRP recovery image [4] for the model. Then immediately turn off the device by either removing the battery or pressing Volume Down+Power for eight to 10 seconds. Once the screen goes blank, release the keys immediately. On the powered-off device, press Home+Volume Up+Power to start recovery mode with TWRP. You will now be able to flash Lineage via the recovery option just as you would with other Android devices.

Listing 1

Flashing Samsung Devices

$ heimdall flash --RECOVERY twrp-version_number-Devicecode.img --no-reboot

Fastboot fields the data from the PC fastboot command-line program and writes it to partitions on the mobile device. The prerequisite is a USB connection between the PC and smartphone. After plugging in the USB cable, first run adb devices to test whether this connection with the bootloader launched as described and the console program works (Figure 5).

Figure 5: If the list returned by adb devices contains an entry, the USB connection between the mobile device bootloader and PC is working.

The command in Listing 2 installs the TWRP recovery system on the recovery partition. The percentage values displayed at the command line during the transfer often turn out to be not very meaningful. The main thing is that the operation completes without an error message.

Listing 2

Flashing the Image

$ fastboot flash recovery image_file

The factory-installed recovery system is now overwritten. The option to use it to return to the factory state of the smartphone is no longer available. Instead, you can now back up and restore the actual state of the system with TWRP.

Use the volume keys to select which system the currently active bootloader will start. The options START (normal system) and RECOVERY MODE are important. You can start the recovery that you need next right away or make sure the normal system is working to reassure yourself. In this case, turn off the device again and go back to the bootloader.

Data Manager

TWRP recovery comes with a clear graphical interface (Figure 2). First you need the Backup function to save the previous system, which has not yet been replaced by LineageOS. Later on, when you install the weekly LineageOS updates, it always makes sense to back up a version that you know will work up front. The backup only takes a few minutes on a newer device with a sufficiently fast SD card.

The backup screen opens, showing you the partitions System, Data (excl. storage), and Boot preselected for the backup. These settings are correct for a later restore of an entire system. In Select Storage, you now need to select the desired storage medium. The storage medium is usually not Internal Storage but an SD card.

Many users move the large backups to the PC immediately after they have been created. You will find the data on the storage medium below TWRP/BACKUPS/<DEVICE ID>/ in a directory with a name that starts with the date and time and contains a system name such as lineage. To restore a backup, restore this folder to the same location. The backups will then appear as a list in the TWRP Restore function. After selecting a backup, all you do to restore it is drag a slider to the right. If a file selector appears instead of the list of existing backups, use Select Storage to switch between Internal Storage and SD Card.

A file manager is available on the TWRP home screen after you select Advanced (Figure 6). You can access the home screen at any time via the button at the bottom of your screen. Using the file manager is not immediately intuitive: For example, it is important to know that the contents of the SD card can be found in /data/media/0/, including the aforementioned TWRP/ folder. To delete a backup subfolder, open it and tap Select Current Folder bottom right. The file manager then displays the Delete option (line top right in Figure 6).

Figure 6: TWRP integrates a file manager that you can use to delete older backups or other data as needed to free up space.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Open Hardware – eelo

    With the eelo project, Gaël Duval has big plans for a free-licensed phone, with an accompanying app store and online services.

  • Linux Android Backup

    Linux Android Backup offers an open source solution to backing up the most important content from your Android devices in just a few steps.

  • PostmarketOS

    The mobile Linux distribution PostmarketOS is a fork of Alpine Linux that supports around a dozen user interfaces.

  • Linux Apps on Android with UserLAnd

    UserLAnd lets you run Linux applications on your Android phone – all without replacing Android OS.

  • FOSSPicks

    This month Graham looks at Bespoke, Waydroid, OpenShot, pedalboard, Onivim 2, Mr. Rescue, and more!

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

Subscribe to our Linux newsletters

News