为手机安装Linux

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

该章节将引导你为一些手机安装主线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

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