在开发的过程中,我们经常在开发板中按照了很多程序,配置了复杂的环境,如果每个开发板子都配置一次,费时又费力,还容易出错。
本文介绍一种克隆开发板的文件系统的方法。
环境准备
在 PC 机中同步开发板的文件系统,请先将开发板和 PC 机置于同一局域网内。
由于一般文件系统比较大,可能5G以上,甚至更大,如果有条件使用网线直连的方式传输的数据更快。
我这里就是用网线直连,个人PC的网卡设置的192.168.158.13,
crabe@crabe-ThinkPad:~/project$ ifconfig enp0s31f6
enp0s31f6: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.158.13 netmask 255.255.255.0 broadcast 192.168.158.255
inet6 fe80::464:178e:c1b9:221b prefixlen 64 scopeid 0x20<link>
ether e8:80:88:84:60:7f txqueuelen 1000 (Ethernet)
RX packets 10926370 bytes 16428962988 (16.4 GB)
RX errors 0 dropped 3 overruns 0 frame 0
TX packets 1551006 bytes 128242179 (128.2 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 16 memory 0xb0180000-b01a0000
开发板一侧直接用命令ifconfig 设置ip为192.168.158.11,然后网线直连pc和开发版即可ping通。
ubuntu@localhost:~$ sudo ifconfig eth0 192.168.158.11
ubuntu@localhost:~$ ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 6e:80:37:d1:ef:14 txqueuelen 1000 (Ethernet)
RX packets 6 bytes 827 (827.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 109 bytes 17291 (17.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 78
ubuntu@localhost:~$ ping 192.168.158.13
PING 192.168.158.13 (192.168.158.13) 56(84) bytes of data.
64 bytes from 192.168.158.13: icmp_seq=1 ttl=64 time=0.866 ms
^C
--- 192.168.158.13 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.866/0.866/0.866/0.000 ms
文件同步
在PC端操作,创建文件夹
mkdir rootfs
然后执行文件同步命令,最好使用root用户,避免有些文件夹没有权限。
sudo rsync -avx root@192.168.158.11:/ ./rootfs
制作镜像
此时,rootfs 目录中保存的就是开发板文件系统中的所有文件,可以开始制作
dd if=/dev/zero of=ubuntu2204_rootfs.img bs=1M count=8000
将镜像文件格式化为ext4
mkfs.ext4 ubuntu2204_rootfs.img
创建一个文件夹,并将镜像文件挂载到这个文件夹上
mkdir temp
sudo mount ubuntu2204_rootfs.img ./temp
j将文件拷贝到挂载的文件夹内
sudo cp -rfp rootfs/* temp/
卸载镜像文件
sudo umount temp/
检查镜像文件完整性
crabe@crabe-ThinkPad:~/project/$ e2fsck -p -f ubuntu2204_rootfs.img
ubuntu2204_rootfs.img: 204027/512064 files (0.1% non-contiguous), 1857549/2048000 blocks
释放多余的空间
crabe@crabe-ThinkPad:~/project/$ resize2fs -M ubuntu2204_rootfs.img
resize2fs 1.45.5 (07-Jan-2020)
The filesystem is already 2048000 (4k) blocks long. Nothing to do!
结束
至此从一个开发板中克隆出来一个完整的文件系统完成。这个镜像文件就可以向其他开发中烧录或者做备份使用来。
需要注意的是最后一步释放了多余的空间,如果烧录到其他开发版中后,需要在开发板中做扩容。
执行下面命令就可以,完成扩容,再使用 df -lh 看一下,磁盘就是正常大小了。
sudo resize2fs /dev/mmcblk0p6