以构建 Ubuntu16.04 为例,使用 qemu 构建文件系统.
1.下载 Ubuntu 官方提供的 base 文件系统
下载地址:http://cdimage.ubuntu.com/ubuntu-base/releases/20.04.3/release/ubuntu-base-20.04.3-base-arm64.tar.gz
wget http://cdimage.ubuntu.com/ubuntu-base/releases/20.04.3/release/ubuntu-base-20.04.3-base-arm64.tar.gz
mkdir ubuntu
sudo tar xpf ubuntu-base-16.04.6-base-armhf.tar.gz -C ubuntu/
2. 安装 qemu-user-static 搭建 arm 模拟环境
sudo apt-get install qemu-user-static
sudo cp /usr/bin/qemu-arm-static ubuntu/usr/bin
#sudo cp /usr/bin/qemu-aarch64-static ubuntu/usr/bin
sudo cp -b /etc/resolv.conf ubuntu/etc/
64位文件系统:copy 自 /usr/bin/qemu-aarch64-static
32位文件系统:需要复制/usr/bin/qemu-arm-static
3.挂载 proc,sys,dev 等文件系统,chroot 到模拟 arm 文件系统
在 PC 机 Ubuntu 下新建脚本文件 ch-mount.sh,文件内容如下:
#!/bin/bash
#
function mnt() {
echo "MOUNTING"
sudo mount -t proc /proc ${2}proc
sudo mount -t sysfs /sys ${2}sys
sudo mount -o bind /dev ${2}dev
sudo mount -o bind /dev/pts ${2}dev/pts
sudo chroot ${2}
}
function umnt() {
echo "UNMOUNTING"
sudo umount ${2}proc
sudo umount ${2}sys
sudo umount ${2}dev/pts
sudo umount ${2}dev
}
if [ "$1" == "-m" ] && [ -n "$2" ] ;
then
mnt $1 $2
elif [ "$1" == "-u" ] && [ -n "$2" ];
then
umnt $1 $2
else
echo ""
echo "Either 1'st, 2'nd or both parameters were missing"
echo ""
echo "1'st parameter can be one of these: -m(mount) OR -u(umount)"
echo "2'nd parameter is the full path of rootfs directory(with trailing
'/')"
echo ""
echo "For example: ch-mount -m /media/sdcard/"
echo ""
echo 1st parameter : ${1}
echo 2nd parameter : ${2}
fi
脚本文件新建好后就可以开始进入文件系统安装一些初始包了,键入以下指令进入模拟文件
系统:
sudo bash ch-mount.sh -m ubuntu/
或:
./ch-mount.sh -m ubuntu/
4.安装一些基础软件包
使用 ch-mount.sh 脚本进入模拟文件系统后就可以模拟 ARM 平台下的文件系统进行操作了。
执行以下安装必要的软件包,方便后续使用 U 盘或 TF 卡安装文件系统。
apt-get update
apt-get upgrade -y
apt-get install sudo dialog apt-utils iputils-ping wget -y
apt-get install net-tools ethtool wireless-tools network-manager resolvconf
-y
apt-get install rsyslog bash-completion htop lxtask vim -y
apt-get install synaptic xfce4-power-manager -y
5.制作文件系统镜像
mkdir tmp
dd if=/dev/zero of=rootfs-ubuntu.img bs=1M count=0 seek=4000
mkfs.ext4 rootfs-ubuntu.img
sudo mount rootfs-ubuntu16.img tmp/
sudo cp -rfp ubuntu/* tmp/
sudo umount tmp
e2fsck -p -f rootfs-ubuntu.img
resize2fs -M rootfs-ubuntu.img