ARM64的ubuntu-20.04镜像下载链接:https://old-releases.ubuntu.com/releases/20.04.4/ubuntu-20.04.4-live-server-arm64.iso
Linux内核源码下载链接: https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.4.52.tar.xz
vmware网络设置桥接模式,桥接模式可以分配网络,并且局域网可以ping通,也能ping通外网,nat模式待验证
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
# 在root权限下操作
apt install -y qemu-user-static
# 准备定制根文件系统
mkdir -p ~/temp; cd ~/temp
# 进入超级用户下
wget https://old-releases.ubuntu.com/releases/20.04.4/ubuntu-20.04.4-live-server-arm64.iso
mount ubuntu-20.04.4-live-server-arm64.iso /mnt
cp /mnt/casper/filesystem.squashfs ./
umount /mnt
unsquashfs filesystem.squashfs
echo 'nameserver 8.8.8.8' >> squashfs-root/etc/resolv.conf
echo 'nameserver 8.8.4.4' >> squashfs-root/etc/resolv.conf
# 如果提示失败:
# chroot: failed to run command ‘/bin/bash’: No such file or directory
# 拷贝 qemu-aarch64-static 到 rootfs
sudo cp /usr/bin/qemu-aarch64-static squashfs-root/usr/bin/
chroot squashfs-root
# 创建root密码(用于板卡登录根文件系统)
passwd root
apt-get update
apt-get install -y unzip vim git openssh-server python3-pip sysstat libnuma1 dmidecode rsync net-tools psmisc parted arping ntpdate iproute2 iputils-ping mawk cracklib-runtime ethtool ntp fdisk libssl-dev libpam-cracklib logrotate
# 配置ssh
<<'CONTENT'
Port 22
PermitRootLogin yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2
PasswordAuthentication yes
CONTENT
sed -i 's/^[[:space:]]*#\?Port.*/Port 22/' /etc/ssh/sshd_config
sed -i 's/^[[:space:]]*#\?PermitRootLogin.*/PermitRootLogin yes/' /etc/ssh/sshd_config
sed -i 's/^[[:space:]]*#\?PubkeyAuthentication.*/PubkeyAuthentication yes/' /etc/ssh/sshd_config
sed -i 's|^[[:space:]]*#\?AuthorizedKeysFile.*|AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2|' /etc/ssh/sshd_config
sed -i 's/^[[:space:]]*#\?PasswordAuthentication.*/PasswordAuthentication yes/' /etc/ssh/sshd_config
# 重启ssh服务
/etc/init.d/ssh restart
# 配置以太网共享,前提是需要通过网络连接电脑,然后电脑共享的有网络的网卡到以太网
cat > /etc/netplan/01-netcfg.yaml << EOF
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
addresses:
- 192.168.137.50/24
gateway4: 192.168.137.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
EOF
# # 应用网络配置,在板卡操作系统中操作(基本上用不上)
# netplan apply
# 板卡系统打印异常错误推荐根本解决方案(嵌入式必做)
# 在构建 rootfs 或 live-build 之前:
# 1)禁用 snap(嵌入式 Linux 基本上都禁用它)
apt purge -y snapd
rm -rf /snap /var/snap /var/lib/snapd
# 2)禁用 multipathd
apt purge -y multipath-tools
# 3)清理 systemd 单元防止镜像启动时报乱七八糟的错误
rm -f /etc/systemd/system/multi-user.target.wants/multipathd.service
rm -f /etc/systemd/system/*.wants/snap*.service
exit
cd squashfs-root
# find . | cpio -o -H newc | gzip > ../Sample-root-filesystem-soc_ubuntu-20.04.4-aarch64.img
find . | cpio -H newc -o | gzip > ../Sample-root-filesystem-soc_ubuntu-20.04.4-aarch64.cpio.gz
# find . | cpio -H newc -o | gzip > ../ai-root-filesystem-soc_ubuntu-20.04.4-aarch64.cpio.gz
# find . | cpio -H newc -o | gzip > ../ubuntu20.04.4_rootfs.cpio.gz
# # 制作 squashfs(用于 ISO)
# sudo mksquashfs rootfs filesystem.squashfs -comp xz -wildcards
# 交叉编译Linux内核源码
# 步骤 1:安装 ARM64 交叉编译工具链
sudo apt install -y gcc-aarch64-linux-gnu make git bc flex bison libssl-dev
# 步骤 2:下载 Linux Kernel 5.4.52 源码
wget https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.4.52.tar.xz
tar xf linux-5.4.52.tar.xz
cd linux-5.4.52
# 拷贝默认内核配置文件内核源码根目录下并重命名为.config,如果有其他来源的内核配置也是同样的方式拷贝到内核源码根目录下
make ARCH=arm64 defconfig
# cp arch/arm64/configs/defconfig ./.config
# # 根据现有的.config 文件更新内核配置
# make ARCH=arm64 oldconfig
# 你要确保你的内核支持:
<<'CONTENT'
CONFIG_SERIAL_AMBA_PL011=y
CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
CONTENT
# 查看你内核编译的 .config:
grep PL011 .config
# 如果没有这两个控制台驱动 → QEMU不会有任何输出,Linux 内核运行但你看不见!
sudo apt install -y libncurses5-dev libncursesw5-dev
# 你需要重新配置内核,把 PL011 打开:
# 添加PL011和virtio-blk驱动
<<'CONTENT'
Device Drivers --->
Character devices --->
Serial drivers --->
<*> ARM AMBA PL011 serial port
[*] Support for console on PL011 serial port
Block devices --->
<*> Virtio block driver
[*] Virtualization drivers ----
Virtio drivers
<*> Platform bus driver for memory mapped virtio devices
[*] Memory mapped virtio devices parameter parsing
CONTENT
# 打开内核配置
make ARCH=arm64 menuconfig
# 搜索并添加PL011,以及配置virtio-blk:
/PL011
# 开启:
<<'CONTENT'
CONFIG_SERIAL_AMBA_PL011=y
CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
CONFIG_VIRTIO=y
CONFIG_VIRTIO_BLK=y
CONFIG_VIRTIO_MMIO=y
CONTENT
# 如果想跳过 ncurses,也可以使用 非图形化配置:
sed -i 's/# CONFIG_SERIAL_AMBA_PL011 is not set/CONFIG_SERIAL_AMBA_PL011=y/' .config
sed -i 's/# CONFIG_SERIAL_AMBA_PL011_CONSOLE is not set/CONFIG_SERIAL_AMBA_PL011_CONSOLE=y/' .config
# 保证依赖配置也自动生效,否则可能缺少 Virtio 驱动依赖。
make ARCH=arm64 oldconfig
# # 然后重新编译内核:
# make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j$(nproc)
# 步骤 3:准备 kernel headers(用于编译外部模块)
# 必须准备 .config 和 modules 依赖,否则无法构建 modules。
# 如果你没有目标板的内核配置,建议使用通用 defconfig。
# 3.1 使用通用 ARM64 配置:
# make ARCH=arm64 defconfig
# make ARCH=arm64 oldconfig
# 3.2 编译内核(只需要生成 headers,不需要整机镜像)
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j$(nproc)
# 3.3 生成 modules(构建模块依赖)
# make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- modules -j$(nproc)
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- modules_prepare
# 3.4 安装 headers(关键步骤)
make ARCH=arm64 headers_install
# 如果需要从内核编译环境复制全套 modules 目录(最干净)
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- modules_install INSTALL_MOD_PATH=./modout
# 把这个目录复制进你的 rootfs
sudo cp -r modout/lib/modules/5.4.52 ../squashfs-root/lib/modules/
# 生成模块依赖,进入 rootfs chroot 或直接:
sudo depmod -a -b ../squashfs-root 5.4.52
# 如果是在板卡根文件系统可直接运行下面命令
depmod -a
# 步骤 4(可选,可略过):下载 udmabuf 源码
cd ../
git clone https://github.com/ikwzm/udmabuf.git
cd udmabuf
# 步骤 5:交叉编译 udmabuf(ARM64)
# 关键是指定:
# ARCH=arm64
# CROSS_COMPILE=aarch64-linux-gnu-
# KERNEL_SRC=你的内核路径
# 假设内核路径是:
# /home/you/linux-5.4.52
# 编译:
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- KERNEL_SRC=../linux-5.4.52
# 编译成功后会得到:
<<'CONTENT'
udmabuf.ko
CONTENT
cd -
# 之后,内核源码目录即可作为 KERNEL_SRC。
# 准备QEMU启动文件
sudo apt install -y qemu-system-arm
# 创建20GB的磁盘
dd if=/dev/zero of=rootfs.img bs=1M count=20480
mkfs.ext4 rootfs.img
mkdir mnt
sudo mount -o loop rootfs.img mnt
cd mnt
gunzip -c Sample-root-filesystem-soc_ubuntu-20.04.4-aarch64.cpio.gz | cpio -idm
# ARM 默认 ttyAMA0串口需要 ,Ubuntu 需要同时使能 ttyAMA0 and default tty1 , 在Ubuntu 根文件系统根目录中执行
sudo ln -s lib/systemd/system/getty@.service etc/systemd/system/getty.target.wants/getty@ttyAMA0.service
cd -
sudo umount mnt
# QEMU 启动命令
sudo qemu-system-aarch64 \
-machine virt \
-cpu cortex-a57 \
-m 4096 \
-nographic \
-kernel linux-5.4.52/arch/arm64/boot/Image \
-append "console=ttyAMA0,115200 root=/dev/vda rw rootwait systemd.mask=getty@ttyAMA0.service,getty@ttyPS0.service,rc-local.service" \
-drive file=rootfs.img,format=raw,if=none,id=hd0 \
-device virtio-blk-device,drive=hd0
# 导出内核配置文件(比如使用的是别人制作好的根文件系统,可以用此方式导出内核配置后替换内核源码根目录下的.config)
zcat /proc/config.gz > config-$(uname -r)
# zcat /proc/config.gz > config-5.4.52
# Ctrl+a,x,退出qemu,然后挂载根文件系统并拷贝出config-5.4.52
|