Turning an Old Android Phone into a Server (Redmi 4X [santoni])

hahnavi |
about image

Based on https://github.com/dreemurrs-embedded/arch-linux-santoni

Create img file

dd if=/dev/zero of=rootfs.img bs=1M count=2560

Format img file as ext4 filesystem

mkfs.ext4 rootfs.img

Mount img file

sudo mount rootfs.img /mnt/rootfs

Download Arch Linux ARM64 rootfs

wget http://os.archlinuxarm.org/os/ArchLinuxARM-aarch64-latest.tar.gz

Extract rootfs

sudo bsdtar -xpf ArchLinuxARM-aarch64-latest.tar.gz -C /mnt/rootfs

Install qemu-user-static

sudo pacman -S qemu-user-static

Setup DNS

sudo mv /mnt/rootfs/etc/resolv.conf /mnt/rootfs/etc/resolv.conf.ori
echo "nameserver 8.8.8.8" | sudo tee /mnt/rootfs/etc/resolv.conf

Copy qemu-user-static binary to the rootfs

sudo cp `which qemu-aarch64-static` /mnt/rootfs/sbin/

Mount dev, proc, and sys

sudo mount -o bind /dev /mnt/rootfs/dev
sudo mount -t proc proc /mnt/rootfs/proc
sudo mount -t sysfs sysfs /mnt/rootfs/sys
sudo mount -t devpts devpts /mnt/rootfs/dev/pts

Chroot to the rootfs

sudo chroot /mnt/rootfs /sbin/qemu-aarch64-static /bin/bash

Change pacman mirror to the nearest location

vi /etc/pacman.d/mirrorlist
...
# Server = http://mirror.archlinuxarm.org/$arch/$repo
...
...
Server = http://sg.mirror.archlinuxarm.org/$arch/$repo
...
pacman-key --init
pacman-key --populate archlinuxarm

Update pacman database and packages

pacman -Syu

Install base development tools and sudo

pacman -S sudo base-devel git

Running makepkg as root is not allowed as it can cause permanent, catastrophic damage to your system.

Add wheel group to sudoers

visudo
...
%wheel ALL=(ALL:ALL) NOPASSWD: ALL
...

Login as alarm user from root

su - alarm

Clone repo

git clone https://github.com/dreemurrs-embedded/arch-linux-santoni.git

Install firmware

cd arch-linux-santoni/Arch_PKGBUILDs/firmware-xiaomi-santoni
makepkg -si

Install firware loader

cd ../postmarketos-firmware-loader/
makepkg -si

Install USB Networking

cd ../postmarketos-usb-networking/
makepkg -si

Install WCNSS WLAN

cd ../postmarketos-wcnss-wlan/
makepkg -si

Install SSH key

mkdir ~/.ssh
chmod 700 ~/.ssh
echo "<public key>" | tee ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

Exit from alarm to root user

exit

Enable USB Networking service

systemctl enable usb_networking.service

Setup DHCPD edit /etc/dhcpcd.conf

interface rndis0
static ip_address=172.16.42.1/24
static routers=172.16.42.1
static domain_name_servers=8.8.8.8

Enable DHCPD service

systemctl enable dhcpcd.service

Enable WCNSS WLAN service

systemctl enable wcnss-wlan.service

Add root partition to /etc/fstab

echo "/dev/mmcblk0p49 / ext4 rw,relatime 0 1" | tee /etc/fstab

Clean pacman cache

pacman -Scc

Revert DNS to use the config from systemd-resolved

rm /etc/resolv.conf
mv /etc/resolv.conf.ori /etc/resolv.conf

Exit chroot

exit

Unmount

sudo umount /mnt/rootfs/dev/pts
sudo umount /mnt/rootfs/dev
sudo umount /mnt/rootfs/sys
sudo umount /mnt/rootfs/proc
sudo umount /mnt/rootfs

Flash img

fastboot flash userdata rootfs.img

…to be continued…