本文基于Ubuntu 18.04系统,在U盘中制作ext4文件系统。在嵌入式调试中,可以从U盘中挂载文件系统。
# 1、查看U盘设备目录 root@ubuntu:/home/run/code# ls /dev/sd sda sda1 sdb sdb1 (U盘) # 2、卸载 root@ubuntu:/home/run/code# umount /dev/sdb1 # 3、格式化为ext4文件系统,并添加卷标‘rootfs’ root@ubuntu:/home/run/code# mkfs.ext4 -L rootfs /dev/sdb1 mke2fs 1.44.1 (24-Mar-2018) Creating filesystem with 7864059 4k blocks and 1966080 inodes Filesystem UUID: d20aee9e-3ada-4f3e-b806-9bbad7209362 Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done root@ubuntu:/home/run/code# # 4、重新拔插U盘 # 5、拷贝已有文件系统映像ubuntu_rootfs.img root@ubuntu:/home/run/code# file ubuntu_rootfs.img ubuntu_rootfs.img: Linux rev 1.0 ext4 filesystem data, UUID=faf10ee9-7e64-4826-98cc-239708736642, volume name "rootfs" (extents) (64bit) (large files) (huge files) root@ubuntu:/home/run/code# mkdir testimg root@ubuntu:/home/run/code# mount ubuntu_rootfs.img testimg/ root@ubuntu:/home/run/code# cd testimg/ root@ubuntu:/home/run/code/testimg# ls bin boot dev etc home lib lost+found media opt proc root run sbin srv sys system tmp usr var vendor root@ubuntu:/home/run/code/testimg# mount /dev/sdb1 /tmp/ root@ubuntu:/home/run/code/testimg# cp -rf * /tmp/ # 6、卸载 root@ubuntu:/home/run/code/testimg# umount /tmp
如果原有U盘有多个分区,可以使用fdisk命令删除或创建分区,流程如下:
root@ubuntu:/home/run/code# fdisk /dev/sdc Welcome to fdisk (util-linux 2.31.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): m ## 1、帮助 Help: Generic d delete a partition F list free unpartitioned space l list known partition types n add a new partition p print the partition table t change a partition type v verify the partition table i print information about a partition Misc m print this menu x extra functionality (experts only) Script I load disk layout from sfdisk script file O dump disk layout to sfdisk script file Save & Exit w write table to disk and exit q quit without saving changes Create a new label g create a new empty GPT partition table G create a new empty SGI (IRIX) partition table o create a new empty DOS partition table s create a new empty Sun partition table Command (m for help): p ## 2、显示分区 Disk /dev/sdc: 30 GiB, 32212254720 bytes, 62914560 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: gpt Disk identifier: 23000000-0000-4C4A-8000-699000005ABB Device Start End Sectors Size Type /dev/sdc1 16384 24575 8192 4M unknown /dev/sdc2 24576 32767 8192 4M unknown /dev/sdc3 32768 98303 65536 32M unknown /dev/sdc4 98304 163839 65536 32M unknown /dev/sdc5 163840 229375 65536 32M unknown /dev/sdc6 229376 4323327 4093952 2G unknown /dev/sdc7 4323328 62914495 58591168 28G unknown Command (m for help): d ## 3、删除分区7-1 Partition number (1-7, default 7): 7 Partition 7 has been deleted. Command (m for help): d Partition number (1-6, default 6): Partition 6 has been deleted. Command (m for help): d Partition number (1-5, default 5): Partition 5 has been deleted. Command (m for help): d Partition number (1-4, default 4): Partition 4 has been deleted. Command (m for help): d Partition number (1-3, default 3): Partition 3 has been deleted. Command (m for help): d Partition number (1,2, default 2): d Value out of range. Partition number (1,2, default 2): Partition 2 has been deleted. Command (m for help): d Selected partition 1 Partition 1 has been deleted. Command (m for help): p ## 4、显示分区 Disk /dev/sdc: 30 GiB, 32212254720 bytes, 62914560 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: gpt Disk identifier: 23000000-0000-4C4A-8000-699000005ABB Command (m for help): n ## 5、新建分区1 Partition number (1-128, default 1): First sector (34-62914526, default 2048): Last sector, +sectors or +size{K,M,G,T,P} (2048-62914526, default 62914526): Created a new partition 1 of type 'Linux filesystem' and of size 30 GiB. Partition #1 contains a ext4 signature. Do you want to remove the signature? [Y]es/[N]o: y The signature will be removed by a write command. Command (m for help): p Disk /dev/sdc: 30 GiB, 32212254720 bytes, 62914560 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: gpt Disk identifier: 23000000-0000-4C4A-8000-699000005ABB Device Start End Sectors Size Type /dev/sdc1 2048 62914526 62912479 30G Linux filesystem Filesystem/RAID signature on partition 1 will be wiped. Command (m for help): w ## 6、保存退出 The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks. root@ubuntu:/home/run/code#