1、创建一台同版本的ubuntu23的虚拟机

2、下载

ubuntu-base-23.10-base-armhf.tar.gz

3、上传到虚拟机里,解压到rootfs文件夹下

tar -xf /opt/ubuntu-base-23.10-base-armhf.tar.gz -C /opt/rootfs

4、安装 qemu,对任何机器运行操作系统的全系统仿真。

qemu 是一个通用的、开源的机器仿真器和虚拟机,拷贝它是为了可以模拟 arm cpu 进行文件系统的配置。

sudo apt-get install qemu-user-static
sudo cp /usr/bin/qemu-arm-static rootfs/usr/bin/

5、复制虚拟机的dns文件到rootfs系统内

sudo cp /etc/resolv.conf rootfs/etc/resolv.conf

6、挂载ubuntu-base

编写挂载脚本

#!/bin/bash
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}
}
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 chmod +x mount.sh

运行脚本挂载根文件系统

bash mount.sh -m rootfs/

卸载则运行

bash mount.sh -u rootfs/

7、更新源然后装软件

apt update && apt install -y sudo language-pack-en-base ssh net-tools ethtool  ifupdown iputils-ping rsyslog htop  vim bash-completion systemd bash-completion wireless-tools usbutils lxqt build-essential qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools qt5* qtcreator network-manager

8、用户配置

设置密码、添加用户

passwd root
adduser xyy
usermod -G sudo  xyy

chmod u+s /bin/ping

设置主机名称和本机IP

echo "xyy" > /etc/hostname
echo "127.0.0.1 localhost" >> /etc/hosts
echo "127.0.0.1 xyy" >> /etc/hosts

9、创建开机自启动脚本
1.创建rc-local.service文件

sudo cp /lib/systemd/system/rc-local.service /etc/systemd/system

然后修改/etc/systemd/system/rc-local.service,在文件最下方添加如下两行:

[Install]   
WantedBy=multi-user.target   
Alias=rc-local.service

2.创建rc.local文件
创建/etc/rc.local,里边写自己想要运行的命令。例:

#!/bin/sh
 
 resize2fs /dev/mmcblk2p7
 
exit 0

给/etc/rc.local加上可执行权限

sudo chmod +x /etc/rc.local

9、打包镜像

先创建一个空镜像文件,大小为4096MB

dd if=/dev/zero of=ubuntu_rootfs.img bs=1M count=10240

将该文件格式化成ext4文件系统

mkfs.ext4 ubuntu_rootfs.img

将该镜像文件挂载到一个空的文件夹上,然后将ubuntu_rootfs的文件复制到该空文件夹中

mkdir ubuntu_base_rootfs
sudo mount ubuntu_rootfs.img ubuntu_base_rootfs
sudo cp -rfp rootfs/* ubuntu_base_rootfs/

复制完后用e2fsck修复及检测镜像文件系统,resize2fs 减小镜像文件的大小

sudo umount ubuntu_base_rootfs/
e2fsck -p -f ubuntu_rootfs.img
resize2fs -M ubuntu_rootfs.img
02-01 06:49