Arch Linux运行树莓派系统(2022年)

需要安装QEMU及其相应的aarch64虚拟包,具体安装方法已经忘了,网上应该能搜得到(比如Arch wiki),本篇主要内容是树莓派系统镜像的提取

Arch wiki QEMU

参考国外的脚本(注意,稍微做了一丢丢修改,适应QEMU7.0的配置),把树莓派的btd和kernel提取出来,启动镜像

https://gist.githubusercontent.com/Ch0pin/191007e621e806a6283d5ec5416b8847/raw/1be4b0d62c526eff245734ed38b034fabc8448e2/rasp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
pwn@host$ mkdir aarch64_tests && cd aarch64_tests
pwn@host$ wget https://downloads.raspberrypi.org/raspios_arm64/images/raspios_arm64-2022-01-28/2022-01-28-raspios-bullseye-arm64.zip | busybox unzip -
pwn@host$ sudo mkdir /mnt/raspbian
pwn@host$ fdisk -l 2022-01-28-raspios-bullseye-arm64.img
# Check the 'Start' value of 2022-01-28-raspios-bullseye-arm64.img1 and multiply by 512 That will be your **N**
pwn@host$ sudo mount -v -o offset=N -t vfat 2022-01-28-raspios-bullseye-arm64.img /mnt/raspbian
pwn@host$ cp /mnt/raspbian/kernel8.img $(pwd)
pwn@host$ cp /mnt/raspbian/bcm2710-rpi-3-b-plus.dtb $(pwd)
pwn@host$ sudo umount /mnt/raspbian
# Ensure you have QEMU 7.0 installed at this point
pwn@host$ qemu-img resize 2022-01-28-raspios-bullseye-arm64.img 8G
pwn@host$ qemu-system-aarch64 -m 1024 -M raspi3b -kernel kernel8.img -dtb bcm2710-rpi-3-b-plus.dtb -sd 2022-01-28-raspios-bullseye-arm64.img -append "console=ttyAMA0 root=/dev/mmcblk0p2 rw rootwait rootfstype=ext4" -nographic -device usb-net,netdev=net0 -netdev user,id=net0,hostfwd=tcp::5555-:22
# At this point raspbian should boot on the terminal
raspberrypi login: pi
Password: raspberry
pi@raspberry:~$ sudo service ssh start
pi@raspberry:~$ sudo update-rc.d ssh enable
# At this point we should have been inside the QEMU RaspbianOS instance with ssh
pwn@host$ ssh pi@127.0.0.1 -p 5555
pi@raspberry:~$ sudo apt update && sudo apt install neovim nasm -y && bash -c "$(curl -fsSL http://gef.blah.cat/sh)"

由于没用libvirtd管理,所以只能通过脚本启动

1
2
//start.sh
qemu-system-aarch64 -m 1024 -M raspi3b -smp 4 -kernel kernel8.img -dtb bcm2710-rpi-3-b-plus.dtb -sd 2022-01-28-raspios-bullseye-arm64.img -append "console=ttyAMA0 root=/dev/mmcblk0p2 rw rootwait rootfstype=ext4" -nographic -device usb-net,netdev=net0 -netdev user,id=net0,hostfwd=tcp::5555-:22

换源

选了北外源

1
2
3
4
5
6
7
8
9
10
11
deb https://mirrors.bfsu.edu.cn/debian/ bullseye main contrib non-free
# deb-src https://mirrors.bfsu.edu.cn/debian/ bullseye main contrib non-free
deb https://mirrors.bfsu.edu.cn/debian/ bullseye-updates main contrib non-free
# deb-src https://mirrors.bfsu.edu.cn/debian/ bullseye-updates main contrib non-free
deb https://mirrors.bfsu.edu.cn/debian/ bullseye-backports main contrib non-free
# deb-src https://mirrors.bfsu.edu.cn/debian/ bullseye-backports main contrib non-free
deb https://mirrors.bfsu.edu.cn/debian-security bullseye-security main contrib non-free
# deb-src https://mirrors.bfsu.edu.cn/debian-security bullseye-security main contrib non-free

# 编辑 `/etc/apt/sources.list.d/raspi.list` 文件,删除原文件所有内容,用以下内容取代:
deb http://mirrors.bfsu.edu.cn/raspberrypi/ bullseye main

扩容+SSH配置

爬墙看下面参考资料

结语

感觉体验并不流畅,启动缓慢,一方面可能是qemu的网卡没配置好,网速时好时坏,另外有可能是用了dektop版本的问题,一堆桌面应用去抢1GB内存肯定不够用。平时的话qemu-user应该够我做Arm64实验了,用不上这个。

tips:后面换了lite版本,跑起来相对流畅,至少能顺利把实验做完了。

总之,不推荐使用这种方式开发树莓派

现在先写一下,没准以后能用得上

参考资料

Emulating Raspberry Pi with QEMU