为手机安装Linux

来自Uotan Wiki · 刷机百科
Snnbyyds讨论 | 贡献2024年2月15日 (四) 10:09的版本

该章节将引导你为一些手机安装主线Linux操作系统

注意:本教程中,操作环境为64位Ubuntu,目标机器为OnePlus6T,我们将尝试利用UEFI在OnePlus6T上启动Archlinux

  • 不同于Windows, Linux的安装过程可能具有一定的复杂性,步骤繁杂,方案多样,如果你使用不同的机型,请先仔细阅读本文和搜索资料;此外,主线Linux的开发(内核驱动移植,制作dts等)不在本文讨论范畴内。

1 编译内核

sdm845成品: [Releases · silime/kernel-builder (github.com)](https://github.com/silime/kernel-builder)

1.1 安装相关依赖

对于Ubuntu 22.04 LTS:

sudo apt install build-essential openssl pkg-config libssl-dev libncurses5-dev pkg-config minizip libelf-dev flex bison  libc6-dev libidn11-dev rsync bc liblz4-tool  
sudo apt install gcc-aarch64-linux-gnu dpkg-dev git

1.2 克隆内核源码

你可以在“-b”参数后选择其他分支

mkdir ~/workspace
cd ~/workspace
git clone --depth=1 https://gitlab.com/sdm845-mainline/linux.git -b sdm845/6.8-dev

可用分支可在gitlab网页查看

1.3 生成 .config

make ARCH=arm64 defconfig sdm845.config
# generate .config

1.4 进行编译

make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j$(nproc)

生成deb包供Debian系得GNU/Linux使用

make -j$(nproc) ARCH=arm64 KBUILD_DEBARCH=arm64 KDEB_CHANGELOG_DIST=mobile CROSS_COMPILE=aarch64-linux-gnu- deb-pkg
# This will generate several deb files in ../

2 制作rootfs

2.1 下载

对于Archlinux,从http://os.archlinuxarm.org/os/ArchLinuxARM-aarch64-latest.tar.gz下载

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

对于Ubuntu,从 [Index of /ubuntu-base](https://cdimage.ubuntu.com/ubuntu-base/)选择自己想要的版本

2.2 制作

2.2.1 进入chroot

由于rootfs为ARM64,所以安装qemu-user-static

sudo apt install qemu-user-static 

创建img并挂载

dd if=/dev/zero of=archlinux.img bs=1G count=6
mkfs.ext4 archlinux.img
mkdir arch
sudo mount archlinux.img arch

解压

cd arch
sudo tar -xpvf ../ArchLinuxARM-aarch64-latest.tar.gz

进入chroot环境

cd ..
sudo mount --bind /dev arch/dev
sudo mount -t devpts devpts arch/dev/pts -o gid=5,mode=620
sudo mount -t proc proc arch/proc
sudo mount -t sysfs sysfs arch/sys
sudo mount -t tmpfs tmpfs arch/run

sudo rm -rf arch/etc/resolv.conf && sudo cp /etc/resolv.conf arch/etc/

sudo chroot arch
2.2.2 初始化pacman keyring
pacman-key --init
pacman-key --populate archlinuxarm
2.2.3 换源 (可选)

详见 https://mirrors.tuna.tsinghua.edu.cn/help/archlinuxarm/

pacman -Syyu
# upgrade
2.2.4 卸载旧的内核和firmware
pacman -Q | grep -i linux
pacman -R linux-aarch64 linux-firmware linux-firmware-whence

exit
# exit chroot environment
2.2.5 安装新的内核
  • 确保已经退出`chroot`环境

然后:

cd ~/workspace/linux
sudo make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-  INSTALL_MOD_PATH=~/workspace/arch modules_install
sudo make ARCH=arm64 INSTALL_PATH=~/workspace/arch/boot install

如果是`debian`系也可以直接chroot环境中用dpkg安装deb:dpkg -i *.deb

2.2.6 安装新的firmware
  • 可以尝试从PostmarketOS[1]中提取

进入chroot环境

sudo chroot ~/workspace/arch

移动解压得到的文件

mv * /usr/lib/firmware/
ldconfig

修改/etc/mkinitcpio.conf

nano /etc/mkinitcpio.conf

找到

# BINARIES
# This setting includes any additional binaries a given user may
# wish into the CPIO image.  This is run last, so it may be used to
# override the actual binaries included by a given hook
# BINARIES are dependency parsed, so you may safely ignore libraries
BINARIES=()

在`BINARIES=()`填入`xxxx_zap.mbn/ipa_fws.mbn`的路径 这两个固件的来源一般是手机提取并放在特殊的位置加载(具体由`dts/dtsi`指定) 对于fajita,改为:

 BINARIES=(/usr/lib/firmware/qcom/sdm845/oneplus6/a630_zap.mbn /usr/lib/firmware/qcom/sdm845/oneplus6/ipa_fws.mbn)

生成initrd.img

ls /usr/lib/modules
# get kernel version
mkinitcpio --generate /boot/initrd.img-5.18.0-sdm845-00109-gf7ad1e5036bb-dirty --kernel 5.18.0-sdm845-00109-gf7ad1e5036bb-dirty
2.2.7 安装软件包和基本配置[2]
2.2.7.1 设置时区
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# Shanghai
2.2.7.2 设置locale

参考:https://wiki.archlinux.org/title/Locale

nano /etc/locale.gen
# uncomment en_US.UTF-8 UTF-8
# uncomment zh_CN.UTF-8 UTF-8

locale-gen
2.2.7.3 自定义主机名
# For example
echo 'OnePlus6T' > /etc/hostname
2.2.7.4 alarm(Archlinuxarm内置非root用户)配置

更改alarm密码,并添加到wheel用户组

passwd alarm
# Change its password. Default is "alarm"

usermod -aG wheel alarm

添加sudo,设置sudo权限

pacman -S sudo
EDITOR=nano visudo
找到下面这样的一行,把前面的注释符号 `#` 去掉
#%wheel ALL=(ALL:ALL) ALL
2.2.7.5 必要软件包
pacman -S base base-devel dhcpcd iwd
pacman -S grub efibootmgr

pacman -S networkmanager modemmanager
pacman -S bluez bluez-utils pulseaudio-bluetooth
pacman -S qrtr rmtfs
systemctl enable NetworkManager bluetooth qrtr-ns rmtfs 
systemctl enable ModemManager
pacman -S git wget
pacman -S ntfs-3g usbutils

再安装 pd-mapper 和 tqftpserv

  • 可以此处下载成品:https://github.com/silime/ArchLinux-Packages
  • 也可以在步骤2.2.7.6后直接用pacman安装!
  • 注:默认的PKGBUILD有问题,需要手动修复,在这里不详细描述,如果嫌麻烦,也可以直接编译安装

a. 直接编译安装(不推荐):

git clone https://github.com/andersson/pd-mapper.git
git clone https://github.com/andersson/tqftpserv.git
cd pd-mapper && make && make install && cd ..
cd tqftpserv && make && make install && cd ..

# clean
rm -rf tqftpserv pd-mapper

b. 使用pkgbuild

# Switch to alarm first
su alarm
cd ~
# Compile and Install pd-mapper
git clone https://aur.archlinux.org/pd-mapper-git.git ~/pd-mapper-git
cd ~/pd-mapper-git

nano PKGBUILD
# fix some errors
makepkg -si

#Compile and Install tqftpserv
git clone https://aur.archlinux.org/tqftpserv-git.git ~/tqftpserv-git
cd ~/tqftpserv-git

nano PKGBUILD
# fix some errors
makepkg -si

启用 pd-mapper 和 tqftpserv

sudo systemctl enable tqftpserv pd-mapper

回到root用户

exit
2.2.7.6 添加Renegade Project源[3](可选)

导入 GPG key:

pacman-key --recv-keys 5E29D8F17064598A7A2223E2C735F8DF5B624511
pacman-key --lsign-key 5E29D8F17064598A7A2223E2C735F8DF5B624511

在 /etc/pacman.conf 文件末尾添加以下内容:

[renegade-project]
Server = https://mirror.renegade-project.tech/arch/$arch

安装 renegade-project-keyring:

pacman -S renegade-project-keyring

刷新

pacman -Syy
2.2.7.7 安装用于切换slot的qbootctl(推荐)
  • 确保已经完成了步骤2.2.7.6
pacman -Sy qbootctl
2.2.7.8 安装更多package使一些硬件工作
2.2.7.9 修改Modemmanager
  • 重新编译ModemManager,开启`plugin_qcom_soc`,并安装
su alarm
mkdir ~/modemmanager
cd ~/modemmanager
nano PKGBUILD
10c10
< arch=(x86_64)
---
> arch=(x86_64 aarch64)
35d34
<     -D plugin_qcom_soc=disabled
  • 编译和安装
makepkg -rsi
# compile and install

cd ..
sudo rm -rf ~/modemmanager
exit
systemctl enable ModemManager
2.2.7.10 其他软件

中文字体

pacman -S noto-fonts-cjk
# Chinese fonts

yay

su alarm
cd ~
git clone https://aur.archlinux.org/yay-bin.git
cd yay-bin && makepkg -si
exit

桌面环境

  • 自选,建议gnome或phosh
    • 如`gnome`
pacman -S gnome
systemctl enable gdm

网络浏览器

# for example, chromium
pacman -S chromium
2.2.8 tfa98xx音频修复
echo 'snd-soc-tfa98xx' > /etc/modules-load.d/snd-soc-tfa98xx.conf
2.2.9 打包

先清理,在chroot环境中

pacman -Scc
# pacman

进alarm用户

su alarm # switch to alarm
# clean bash history for alarm
cat /dev/null > ~/.bash_history && history -c && exit

在root用户

# clean bash history for root
cat /dev/null > ~/.bash_history && history -c

退出chroot环境

exit

解除挂载

cd ~/workspace
sudo umount ~/workspace/arch/run
sudo umount ~/workspace/arch/sys
sudo umount ~/workspace/arch/proc
sudo umount ~/workspace/arch/dev/pts
sudo umount ~/workspace/arch/dev

用tar打包

cd ~/workspace/arch
sudo tar -czvf ~/workspace/arch.tar.gz *
# This will generate arch.tar.gz

3 安装到手机

3.1 刷入twrp和Magisk

  • 你也可以使用KernelSU或其它方式获取root权限

3.2 分区、格式化分区

连接手机、电脑,手机进twrp

将`parted`复制到手机储存

电脑输入:

adb shell

然后输入:

cp /sdcard/parted /sbin/ && chmod 755 /sbin/parted
umount /data && umount /sdcard
parted /dev/block/sda

输入`p`查看当前分区情况

p

对于一加6T,可这样分区:

  • 压缩分区17
resizepart 17
# input 61GB 
  • 创建新分区
mkpart esp fat32 61GB 62GB
mkpart arch ext4 62GB 125GB
set 18 esp on

随后退出parted

q

再次重启到twrp,格式化各分区

电脑输入:

adb shell

然后输入:

mkfs.fat -F32 -s1 /dev/block/by-name/esp
mke2fs -t ext4 /dev/block/by-name/arch
mke2fs -t ext4 /dev/block/by-name/userdata

3.3 获取UUID

重启到Android,在termux或adb shell输入

su
blkid /dev/block/by-name/arch

记下UUID值

3.4 准备grub配置文件

将boot.tar.gz中grub.cfg的uuid全部改为步骤3.3中得到的 替换所有这种字符

3.5 解压rootfs

将arch.tar.gz复制到手机内部存储

  • 建议使用termux操作
pkg update
pkg install tsu

提权

tsu
# root permission needed

挂载&解压

cd ~
mkdir arch
mount /dev/block/by-name/arch ~/arch

cd ~/arch
tar -xpvf /sdcard/arch.tar.gz
# extract

3.6 安装grub

tsu
cd ~
mkdir esp
mount /dev/block/by-name/esp esp
mkdir esp/EFI

将esp.zip中这两个文件夹复制到~/esp/EFI/

将boot.tar.gz内两个文件夹复制进~/arch/boot

  • 检查
# /dev/block/by-name/arch has been mounted!
cd ~/arch/boot && ls

3.7 获取、添加dtb

对于一加6T,还需要将这两个dtb复制到~/arch/boot

  • 注:对于一加6(T)不要使用内核编译生成的dtb