Install alpinelinux pkgs on mobile device (Part 1)

Howto install alpinelinux pkgs on mobile via adb (an overview).

Is it is possible to install linux on existing android devices without having to alter/change android installation on them ?

Yes, this also means one may be able to keep existing android setup and have regular updates happening to it.

Currently, process described and video below shows installing alpinelinux packages in cli mode for using only cli based applications. This article does not attempt to outline complete process, therefore using it to modify your device is strictly discouraged.

The process only refers to cli based pkgs using current stockrom kernel, and should not be referred to as standard or complete alpinelinux install.

Some usage

  1. For web-development of small to medium sized applications, this can serve as a potential environment.
  2. For serving static contents like images or as a CDN.
  3. As communication devices eg. running weechat on screen/tmux, sip-server, mqtt server, redis server, irc server.
  4. As Pastebin or web-notes server.
  • Caveat:
    So far I have only been able to successfully run applications as user root.
    Attempting to install X/Wayland or any graphical environment on the device, using process described could harm the device.
    Process does not insure a secure install, but is more a demonstration.

Requirement/Process

1. Root enabled adbd running on device.

Root enabled adbd running on current stockrom kernel in recovery mode. By root enabled adbd means, adb shell should get logged as root i.e uid=0.

adb shell

output from within android adb shell

id
  uid=0(root) gid=0(root) groups=0(root),0(root),1(bin),2(daemon),3(sys),4(adm),
  6(disk),10(wheel),11(floppy),20(dialout),26(tape),27(video)

There are few ways to get root enabled adbd,

  • install twrp/cwm recovery for the device, or extract adbd from it and re-build into current stockrom recovery.
  • compile it.
  • install engineering rom.

Devices newer than yr.2016+ may have extra protection layers, so be warned.

Alternatively, if the device offers fastboot boot <image file>, then using this method is preferred. As this allows to test run boot/recovery images without having to flash them.

2. Busybox (oh! my precious).

A statically compiled busybox for the device.

Many devices come with full busybox installed (i.e busybox --install -s) or just the file in /system/xbin/.

       BusyBox is a multi-call binary that combines many common Unix
       utilities into a single executable.

There are also tools similar to busybox, eg.toolbox, toybox suited for various needs.

3. Existing micro-sd card being used in android (vfat).
adb shell

output from within android adb shell

mount /dev/block/mmcblk1p1 /sdcard/
ls -lah /sdcard/
    total 410M   
    drwxrwxrwx   16 0        0           4.0K Jan  1  1970 .
    drwxr-xr-x   20 0        0              0 Jan  1 00:15 ..
    drwxrwxrwx    2 0        0           4.0K Sep  3  2022 .android_secure
    drwxrwxrwx    2 0        0           4.0K Jan  4  2011 Alarms
    drwxrwxrwx    3 0        0           4.0K Dec 15  2022 Android
    drwxrwxrwx    2 0        0           4.0K Jan  1  2011 DCIM
    drwxrwxrwx    2 0        0           4.0K Jan  4  2011 Download
    drwxrwxrwx    2 0        0           4.0K Sep  3  2022 LOST.DIR
    drwxrwxrwx    2 0        0           4.0K Jan  4  2011 Movies
    drwxrwxrwx    2 0        0           4.0K Jan  4  2011 Music
    drwxrwxrwx    2 0        0           4.0K Jan  4  2011 Notifications
    drwxrwxrwx    2 0        0           4.0K Jan  4  2011 Pictures
    drwxrwxrwx    2 0        0           4.0K Jan  4  2011 Podcasts
    drwxrwxrwx    2 0        0           4.0K Jan  4  2011 Ringtones
    drwxrwxrwx    3 0        0           4.0K Jan 19  2014 WhatsApp
    drwxrwxrwx    3 0        0           4.0K Nov 22 19:14 alpine      (1)
    drwxrwxrwx    2 0        0           4.0K Jan  1  2014 backup
    drwxrwxrwx    3 0        0           4.0K Nov 22 19:14 cache       (2)
4. Directory/files structures.
    (1)
    alpine
    |-- alpine-minirootfs-3.18.3-armv7.tar.gz
    |-- .alpine-release
    |-- apks   <- copied from alpine-standard-3.18.3-armv7.iso
    |   |-- armv7
    |   |   |-- alpine-base-3.18.3-r0.apk
    |   |   |-- alpine-baselayout-3.4.3-r1.apk
    |   |   |-- alpine-baselayout-data-3.4.3-r1.apk
    |   |   |-- alpine-conf-3.16.2-r0.apk
    |   |   |-- alpine-keys-2.4-r1.apk
    |   |   |-- alpine-release-3.18.3-r0.apk
    |   |   |-- APKINDEX.tar.gz
    |   |   |-- apk-tools-2.14.0-r2.apk
    |   |   |-- ...
    |   |   |-- yx-1.0.0-r1.apk
    |   |   `-- zlib-1.2.13-r1.apk
    |   `-- .boot_repository
    |-- cache
    |   `-- installed
    |-- etc
    |   |-- alias.txt
    |   `-- env.sysrecovery
    |-- localhost.apkovl.tar.gz    <- created on first_boot
    `-- sysroot.tgz -> alpine-minirootfs-3.18.3-armv7.tar.gz (copy of)

    (2)
    cache
    `-- bin
        |-- busybox
        `-- recovery.misc.sh
5. Shell scripts to bring them together.

Scripts below may not be usable or work on other devices. Please do not attempt if you are not sure what you are doing, rather consider this article more of learning than practical exercise.

Author of this article will not be responsible of any such attempt resulting in bricking or non-working of any device to any extent. Author is also not liable to any extent for loss of any data and/or damage of micro-sd card.

1. File: recovery.misc.sh. [4]

#!/system/bin/sh
# --------------------------------------------------------------------------
# --------------------------------------------------------------------------
# LENOVO-A1000-b (RECOVERY)
# --------------------------------------------------------------------------
# --------------------------------------------------------------------------
export media_usb=/media/usb
export BB=/cache/bin/busybox
export logfile=/tmp/recovery.misc.log
NOW=$(date +%Y%m%d-%H%M%S-%s) >/dev/null 2>&1

busybox mount -o remount,rw /
busybox umount /cache /system /data /data/alpine >/dev/null 2>&1
busybox mkdir -p /sdcard /media/usb /tmp /data/alpine
busybox mount -o bind /sdcard/cache /cache
busybox mount -o bind /sdcard/alpine /data/alpine

$BB mount -o bind /data/alpine /media/usb # better if done at start
echo 0 > /sys/class/backlight/sprd_backlight/brightness

$BB touch $logfile
echo $isRecovery1 >> /tmp/isRecovery.txt

# ---------------------------------------------------------------------
$BB mkdir -p /tmp
$BB echo "$NOW - ::: install.busybox.sh recovery ::: -- starting -- " >> $logfile

# to avoid calling stock/old busybox in $PATH
$BB cp -f $BB /sbin/ #kludges

cd /
if [ -h '/bin' ]; then
  $BB rm -f /bin               # remove /system/bin symlink
fi
$BB mkdir -p /bin/
$BB cp -f $BB /bin/
/bin/busybox mkdir -p /usr/bin /usr/sbin
/bin/busybox --install -s

export CHARSET=UTF-8
export LANG=en.utf8

export STDPATH=/usr/sbin:/usr/bin:/sbin:/bin
export LOCALPATH=/usr/local/sbin:/usr/local/bin
# export ANDROIDPATH=/vendor/bin:/system/sbin:/system/bin:/system/xbin
export PATH=$LOCALPATH:$STDPATH:$ANDROIDPATH

# ---------------------------------------------------------------------
if [ -h '/etc' ]; then
    $BB rm -f /etc             # remove /system/etc symlink
    $BB mkdir -p /etc/
fi
$BB cp ${media_usb}/etc/alias.txt /etc/

echo "$NOW - ::: end busybox install ::: -- - -- " >> $logfile
# ---------------------------------------------------------------------
# modloop mount code here ...
# ---------------------------------------------------------------------
  cd /
  $BB cp ${media_usb}/sysroot.tgz /
  $BB tar -zxf sysroot.tgz
  $BB sleep 3

  $BB cp /media/usb/.alpine-release ./
  $BB touch /var/cache/apk/installed
  $BB mkdir -p /run/lock
  $BB rm -f /sysroot.tgz
  $BB cp $BB /bin/   # v1.31  works
  $BB cp $BB /sbin/  # v1.31  works

  . $media_usb/etc/env.sysrecovery

# ---------------------------------------------------------------------
first_boot() {
    echo '::: first boot :::'
    . $media_usb/etc/env.sysrecovery
    echo /media/usb/apks > /etc/apk/repositories
    apk add alpine-base
    setup-hostname localhost
    setup-lbu -q
    cd /etc/apk; cat repositories > repositories.default; cat world > world.default
    lbu ci
    # setup-alpine # manually and carefully, if more setup needed
}

regular_boot() {
    . $media_usb/etc/env.sysrecovery
    echo '::: applying apkovl :::'
    lastconf=localhost.apkovl.tar.gz
    cp ${media_usb}/$lastconf /
    tar -zxf $lastconf; rm -f $lastconf
    # --------
    cat /etc/apk/repositories.default > /etc/apk/repositories
    cat /etc/apk/world.default > /etc/apk/world
    apk update; apk fix  # NEEDED, this installs lbu
    sleep 3
}

## !! important !! make sure to fix date if incorrect
lastconf=localhost.apkovl.tar.gz
if [ -f "${media_usb}/$lastconf" ]; then
    regular_boot # subsequent boot
else
    first_boot   # first setup boot
fi

echo '::: rc setup :::'
cd /; lbu st # make sure things ok
mkdir -p /run/openrc/; touch /run/openrc/softlevel
openrc # rc/openrc
echo '::: apk setup/fix :::'
apk update
apk fix # this unmounts /.modloop in alpinelinux v3.15+

# ---------------------------------------------------------------------

2. File: env.sysrecovery

export media_usb=/media/usb
export BB=/cache/bin/busybox

NOW=$($BB date +%Y%m%d-%H%M%S-%s)
export logfile=/tmp/recovery.misc.log

export CHARSET=UTF-8
export LANG=en.utf8

export STDPATH=/usr/sbin:/usr/bin:/sbin:/bin # 2a OK
export LOCALPATH=/usr/local/sbin:/usr/local/bin # 2b OK
export ANDROIDPATH=/vendor/bin:/system/sbin:/system/bin:/system/xbin # 2c OK
export PATH=$LOCALPATH:$STDPATH  # default
if [ x"$1" = 'x1' ]; then
    export PATH=$LOCALPATH:$STDPATH:$ANDROIDPATH
fi

Installing alpinelinux pkgs

Steps below assumes device is already booted in twrp/cwm like recovery environment.
There are host of forums and sites where one can get information on twrp or cwm,
eg. forum.xda-developers.com

Note of advice, consider this article more as fun on learning internals of mobile devices.

Step 1
  • Note: this is a ram based install and lbu ci would work and save localhost.apkovl.tar.gz for subsequent regular_boot.
adb shell mount -o remount,rw /
adb shell mkdir -p /sdcard
adb shell mount /dev/block/mmcblk1p1 /sdcard
adb shell sh /sdcard/cache/bin/recovery.misc.sh

Step 2
  • Logging into alpinelinux like environment via adb
adb shell

# from within android adb shell, get into alpine environment
. /media/usb/etc/env.sysrecovery
# . /etc/alias.txt # if you custom created one

Once pkgs is installed, the next step should be to setup ssh,
SSH can be installed and accessed via WIFI or using RNDIS(aka. usb tethering).

Installing either or both would be done on separate article/s.

Media/Attachment

Video 1 (download: Attach:Install-alpinelinux-via-adb.ogv)

I have been using Alpinelinux on mobile for over 2yrs now, without much problem(current success is 400days+ without reboot). The need for such setup rose mostly from necessity and incurring cost.

400days-and-running

Figure 1

My web-development setup:

web-development-setup-v1.0

Figure 2

It would be a while before I post detailed scripts/process for installing alpinelinux on a mobile device (there are few well going projects that already do them). There are some concerning areas like battery life, heating that needs to be addressed first.

Author

V.Krishn


Resources


Edit - History - Print - Recent Changes - Search
Page last modified on November 27, 2023, at 11:38 PM