树莓派Qemu修改镜像

树莓派安装OpenMediaVault 5

IPV6可以用了,把吃灰已久的树莓派拿出来折腾一下。安装OpenMediaVault。
自己装了几编,把流程分享一下。另外为了方便备份这里直接修改镜像,备份tf卡太浪费空间,如果tf卡坏了,把修改后镜像再刷一遍就可以快速恢复。

为啥用Qemu?不用Vbox?因为不VBox不支持Arm

1. 准备软件

1.1 下载树莓派OS:

TUNA 镜像站(位于北京):https://mirrors.tuna.tsinghua.edu.cn/raspberry-pi-os-images/
SJTUG 镜像站(位于上海):https://mirrors.sjtug.sjtu.edu.cn/raspberry-pi-os-images/

1
wget https://mirrors.tuna.tsinghua.edu.cn/raspberry-pi-os-images/raspios_oldstable_lite_armhf/images/raspios_oldstable_lite_armhf-2021-12-02/2021-12-02-raspios-buster-armhf-lite.zip

1.2 下载树莓派Qemu内核

下载地址:https://github.com/dhruvvyas90/qemu-rpi-kernel

下载下面两个文件,跟2021-12-02-raspios-buster-armhf-lite.zip放到同一个文件夹,后面Qemu会用到;

内核: kernel-qemu-5.4.51-buster
dtb: versatile-pb-buster-5.4.51.dtb

注意如果版本不同,请下载不同文件,原文:

This repository contains three types of kernel images:

kernel-qemu-4..-buster are the most recent images, which are compatible with Raspbian Buster and Stretch. To use these images, you’ll need a compiled device tree file (.dtb) which is also contained in this repository. Use versatile-pb-buster.dtb for Buster, or use versatile-pb.dtb for Stretch. Unless you are positive you need a different kernel, the most recent of these images is probably what you want.

kernel-qemu-4..-stretch are images compatible with Raspbian Stretch and Jessie. To use these images, you’ll need the versatile-pb.dtb file which is also contained in this repository.

kernel-qemu-4.4.*-jessie are images compatible with Raspbian Jessie and Wheezy.

kernel-qemu-3.10.25-wheezy is the original image from xecdesign.com, which is compatible with Raspbian Wheezy only.

下载不了,我上传云盘
https://cloud.189.cn/t/fiYRRrIFB7Fb

2. 修改镜像

2.1 加载镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
$ echo "解压树莓派镜像"
$ unzip 2021-12-02-raspios-buster-armhf-lite.zip

$ echo "解压后删除"
$ rm -rf 2020-12-02-raspios-buster-armhf-lite.zip

$ echo "查看镜像分区结构"
$ sudo fdisk -l 2021-12-02-raspios-buster-armhf-lite.img
[sudo] password for wv:
Disk 2021-12-02-raspios-buster-armhf-lite.img: 2.8 GiB, 2961178624 bytes, 5783552 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x43f6ece2

Device Boot Start End Sectors Size Id Type
2021-12-02-raspios-buster-armhf-lite.img1 8192 532479 524288 256M c W95 FAT32 (LBA)
2021-12-02-raspios-buster-armhf-lite.img2 532480 5783551 5251072 2.5G 83 Linux

echo "上面显示树莓派镜像有两个分区:256M是boot分,2.5G的rootfs分区,注意532480这个参数"


echo "先扩充镜像大小: 增加1G,相当于磁盘扩容,后面再调整分区大小"
$ qemu-img resize 2020-12-02-raspios-buster-armhf-lite.img +4G

echo "加载第二个rootfs分区,注意532480是第二个分区开始的位置"
$ mkdir rootfs
$ sudo mount -v -o offset=$((532480 * 512)) -t ext4 2020-12-02-raspios-buster-armhf-lite.img ./rootfs

$ cat ./rootfs/etc/ld.so.preload
/usr/lib/arm-linux-gnueabihf/libarmmem-${PLATFORM}.so

echo "ld.so.preload中加了个#号注释,注意修改完镜像要原还"
$ sudo sed -i 's/^.*$/#\0/' ./rootfs/etc/ld.so.preload

echo "新版使用PARTUUID不用修改"
$ cat ./rootfs/etc/fstab
proc /proc proc defaults 0 0
PARTUUID=067e19d7-01 /boot vfat defaults 0 2
PARTUUID=067e19d7-02 / ext4 defaults,noatime 0 1

echo "卸载rootfs分区"
$ sudo umount ./rootfs

2.2 启动qemu虚拟机

启动虚拟机,用起来确实很慢,建议还是直接用TF卡在真上改好后再参考第7节备份TF卡比较省事。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ qemu-system-arm                       \
-M versatilepb \
-cpu arm1176 \
-m 256 \
-drive "file=2020-12-02-raspios-buster-armhf-lite.img,if=none,index=0,media=disk,format=raw,id=disk0" \
-device "virtio-blk-pci,drive=disk0,disable-modern=on,disable-legacy=off" \
-net "user,hostfwd=tcp::3022-:22" \
-dtb versatile-pb-buster-5.4.51.dtb \
-kernel kernel-qemu-5.4.51-buster \
-append 'root=/dev/vda2 panic=1' \
-no-reboot \
-net nic \
-nographic

echo "登录虚拟机pi:raspberry,进行下面操作, 输入ctrl + A 后按 X退出 QEMU"
pi@raspberrypi:~$: sudo service ssh start
pi@raspberrypi:~$: sudo update-rc.d ssh enable

2.3 分区扩容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
pi@raspberrypi:~$ sudo fdisk -l
Disk /dev/ram0: 4 MiB, 4194304 bytes, 8192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

...

Disk /dev/vda: 2.7 GiB, 2931818496 bytes, 5726208 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x067e19d7

Device Boot Start End Sectors Size Id Type
/dev/vda1 8192 532479 524288 256M c W95 FAT32 (LBA)
/dev/vda2 532480 3629055 3096576 1.5G 83 Linux


Disk /dev/mtdblock0: 64 MiB, 67108864 bytes, 131072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

pi@raspberrypi:~$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 1.5G 1.2G 179M 87% /
devtmpfs 122M 0 122M 0% /dev
tmpfs 122M 0 122M 0% /dev/shm
tmpfs 122M 3.4M 118M 3% /run
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 122M 0 122M 0% /sys/fs/cgroup
/dev/vda1 253M 54M 199M 22% /boot
tmpfs 25M 0 25M 0% /run/user/1000

echo "实际分区只使用1.5G"


pi@raspberrypi:~$ sudo fdisk /dev/vda

Welcome to fdisk (util-linux 2.33.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (2048-5726207, default 2048): 532480
Last sector, +/-sectors or +/-size{K,M,G,T,P} (532480-5726207, default 5726207):

Created a new partition 2 of type 'Linux' and of size 2.5 GiB.
Partition #2 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: n

Command (m for help): w

The partition table has been altered.
Syncing disks.

pi@raspberrypi:~$ sudo reboot

重启后,resize2fs后df -h检查是否扩充成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
pi@raspberrypi:~$ sudo resize2fs /dev/vda2
resize2fs 1.44.5 (15-Dec-2018)
Filesystem at /dev/vda2 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vda2 is now 649216 (4k) blocks long.

pi@raspberrypi:~$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 2.5G 1.2G 1.2G 51% /
devtmpfs 122M 0 122M 0% /dev
tmpfs 122M 0 122M 0% /dev/shm
tmpfs 122M 3.4M 118M 3% /run
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 122M 0 122M 0% /sys/fs/cgroup
/dev/vda1 253M 54M 199M 22% /boot
tmpfs 25M 0 25M 0% /run/user/1000

2.4 镜像开启SSH

1
2
pi@raspberrypi:~$: sudo service ssh start
pi@raspberrypi:~$: sudo update-rc.d ssh enable

2.5 替换国内源

参考:https://tvtv.fun/pc-to-nas/16th.html

    1. 编辑 sources.list
1
2
3
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo sh -c 'echo > /etc/apt/sources.list'
sudo nano /etc/apt/sources.list

替换如下内容:

1
2
3
# 编辑 `/etc/apt/sources.list` 文件,删除原文件所有内容,用以下内容取代:
deb [arch=armhf] http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main non-free contrib rpi
deb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main non-free contrib rpi
    1. 编辑 /etc/apt/sources.list.d/raspi.list 文件
1
2
3
sudo cp /etc/apt/sources.list.d/raspi.list /etc/apt/sources.list.d/raspi.list.bak
sudo sh -c 'echo > /etc/apt/sources.list.d/raspi.list'
sudo nano /etc/apt/sources.list.d/raspi.list

删除原文件所有内容,用以下内容取代:

1
deb http://mirrors.tuna.tsinghua.edu.cn/raspberrypi/ buster main
    1. 添加gpg
      不添加下面会报错,apt-get update时如果还报就加按下面添加
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
sudo gpg --keyserver keyserver.ubuntu.com --recv 648ACFD622F3D138
sudo gpg --export --armor 648ACFD622F3D138 | sudo apt-key add -

sudo gpg --keyserver keyserver.ubuntu.com --recv 112695A0E562B32A
sudo gpg --export --armor 112695A0E562B32A | sudo apt-key add -

sudo gpg --keyserver keyserver.ubuntu.com --recv 7EA0A9C3F273FCD8
sudo gpg --export --armor 7EA0A9C3F273FCD8 | sudo apt-key add -
sudo gpg --keyserver keyserver.ubuntu.com --recv 7E7A6C592EF35D13
sudo gpg --export --armor 7E7A6C592EF35D13 | sudo apt-key add -

sudo gpg --keyserver keyserver.ubuntu.com --recv C5E224500C1289C0
sudo gpg --export --armor C5E224500C1289C0 | sudo apt-key add -

sudo gpg --keyserver keyserver.ubuntu.com --recv 326A835E697B890A
sudo gpg --export --armor 326A835E697B890A | sudo apt-key add -

sudo gpg --keyserver keyserver.ubuntu.com --recv 24863F0C716B980B
sudo gpg --export --armor 24863F0C716B980B | sudo apt-key add -
    1. 更新
1
2
3
sudo apt-get update
sudo apt-get -y update --allow-releaseinfo-change
sudo apt-get upgrade -y

3. OpenMediaVault 5 安装

1
2
3
4
5
6
7
8
9
10
11
echo "开始安装OMV***************************"
echo "参考https://wiki.omv-extras.org/doku.php?id=installing_omv5_raspberry_pi"
sudo apt-get update
sudo apt-get upgrade -y
sudo rm -f /etc/systemd/network/99-default.link
# wget -O - https://github.com/OpenMediaVault-Plugin-Developers/installScript/raw/master/install | sudo bash
mkdir bin
cd bin
wget -c https://cdn.jsdelivr.net/gh/OpenMediaVault-Plugin-Developers/installScript@master/install -O installOmv.sh
chmod a+x installOmv.sh
sudo ./installOmv.sh -n

4. OpenMediaVault 配置Docker

清华源:
https://mirrors.tuna.tsinghua.edu.cn/OpenMediaVault/openmediavault-plugin-developers/pool/main/o/openmediavault-omvextrasorg/

1
2
3
wget https://mirrors.tuna.tsinghua.edu.cn/OpenMediaVault/openmediavault-plugin-developers/pool/main/o/openmediavault-omvextrasorg/openmediavault-omvextrasorg_5.6.5_all.deb

sudo dpkg -i openmediavault-omvextrasorg_5.6.5_all.deb

OMV5页面安装Docker和Portainer

5. 安装MariaDB

1
2
$ sudo apt-get install mariadb-server -y
$ sudo mysql -u root -p

6. 安装Gitea

参考:https://www.cnblogs.com/jianmo-o/p/14297821.html

6.1 创建数据库

1
2
$ sudo adduser --disabled-login --gecos 'Gitea' git
$ sudo mysql -u root -p

在mysql命令行:创建用户和数据库

1
2
3
4
5
6
MariaDB [mysql]> update user set password=password("xxxx"), host='%' where user='root';
MariaDB [mysql]> CREATE DATABASE gitea;
MariaDB [mysql]> GRANT ALL PRIVILEGES ON gitea.* TO 'gitea'@'localhost' IDENTIFIED BY 'xxxx';
MariaDB [mysql]> flush privileges;

MariaDB [mysql]> exit

6.2 安装Gitea

1
2
3
4
5
6
7
8
sudo su git
cd ~
mkdir gitea

cd gitea
wget https://dl.gitea.io/gitea/1.15.10/gitea-1.15.10-linux-arm-6
chmod +x gitea-1.15.10-linux-arm-6
sudo nano /etc/systemd/system/gitea.service

编辑以下内容到:/etc/systemd/system/gitea.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[Unit]
Description=Gitea (Git with a cup of tea)
After=syslog.target
After=network.target

[Service]
# Modify these two values ​​and uncomment them if you have
# repos with lots of files and get to HTTP error 500 because of that
###
# LimitMEMLOCK=infinity
# LimitNOFILE=65535
RestartSec=2s
Type=simple
User=git
Group=git
WorkingDirectory=/home/git/gitea
ExecStart=/home/git/gitea/gitea-1.15.10-linux-arm-6 web
Restart=always
Environment=USER=git
HOME=/home/git

[Install]
WantedBy=multi-user.target

注意:以下参数换成你自己的。
WorkingDirectory:
ExecStart:
HOME:

自启动相关命令:

1
2
3
4
5
sudo systemctl enable gitea.service
sudo systemctl start gitea.service
sudo systemctl restart gitea.service
sudo systemctl status gitea.service
sudo systemctl stop gitea.service

6.3 访问地址

第一访问会填写数据相关参数.

http://ip:3000

6.4 配置

禁用用户注册:gitea/custom/conf/app.ini

1
2
[service]
DISABLE_REGISTRATION = true

如果要域名访问,修改ROOT_URL,注意跟nginx配置要相同。

1
2
[server] 
ROOT_URL = http://your.domain/git/。

nginx配置

1
2
3
4
5
6
location /git/ { # Note: Trailing slash
proxy_pass http://localhost:3000/; # Note: Trailing slash
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

7. TF卡备份与恢复

重装了几次,每次都要apt-get upgrade太浪费时间,OMV也要重安装,于是想办法备份,操作如下:

Linux下操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
echo "备份TF卡,TF卡设备为/dev/disk2"
sudo dd if=/dev/disk2 of=raspi_omv5.img bs=2M count=64

echo "缩小镜像,删除空白空间,32G镜像文件会缩到5G大小,速度很快"
wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh
chmod +x pishrink.sh
sudo mv pishrink.sh /usr/local/bin
sudo pishrink.sh raspi_omv5.img
echo "也可以sudo pishrink.sh -s system.img new_system.img"

echo "恢复/dev/disk2"
sudo dd if=raspi_omv5.img of=/dev/sdc bs=2M count=64

echo "查看dd进度"
sudo killall -USR1 dd

8. 参考

QEMUlating a Rasbian: https://book-of-gehn.github.io/articles/2020/12/15/Qemulating-Rasbian-ARM.html