一般情况下,我们都是选择使用fdisk工具来进行分区,但是目前在实际生产环境中使用的磁盘空间越来越大,呈TiB级别增长;
而常用的fdisk这个工具对分区是有大小限制的,它只能划分小于2T的磁盘,所以在划大于2T磁盘分区的时候fdisk就无法满足要求了;
这个时候有2个方法,其一是通过卷管理来实现,其二就是通过parted工具来实现对GPT磁盘进行分区操作;这里我们采用parted的方法来实现管理。
sdcard镜像是什么?其实就是img格式的文件,是ISO文件的超集。
查看sdcard镜像分区情况
ubuntu@ubuntu:~/kpartx$ parted core-image-minimal-imx8mm-20200227113353.rootfs.sdcard unit MiB print
Model: (file)
Disk /home/ubuntu/kpartx/core-image-minimal-imx8mm-20200227113353.rootfs.sdcard: 168MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 8.00MiB 72.0MiB 64.0MiB primary fat16 lba
2 72.0MiB 160MiB 88.0MiB primary ext4
truncate命令可以将一个文件缩小或者扩展到某个给定的大小
可以利用该命令和-s选项来特别指定文件的大小
ubuntu@ubuntu:~/kpartx$ truncate -s 7G core-image-minimal-imx8mm-20200227113353.rootfs.sdcard
查看sdcard镜像分区情况
ubuntu@ubuntu:~/kpartx$ parted core-image-minimal-imx8mm-20200227113353.rootfs.sdcard unit MiB print
Model: (file)
Disk /home/ubuntu/kpartx/core-image-minimal-imx8mm-20200227113353.rootfs.sdcard: 7168MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 8.00MiB 72.0MiB 64.0MiB primary fat16 lba
2 72.0MiB 160MiB 88.0MiB primary ext4
使用resizepart调整分区
ubuntu@ubuntu:~/kpartx$ parted core-image-minimal-imx8mm-20200227113353.rootfs.sdcard resizepart 2 5120MiB
查看sdcard镜像分区情况
ubuntu@ubuntu:~/kpartx$ parted core-image-minimal-imx8mm-20200227113353.rootfs.sdcard unit MiB print
Model: (file)
Disk /home/ubuntu/kpartx/core-image-minimal-imx8mm-20200227113353.rootfs.sdcard: 7168MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 8.00MiB 72.0MiB 64.0MiB primary fat16 lba
2 72.0MiB 5120MiB 5048MiB primary ext4
使用kpartx自动添加sdcard镜像到loop设备,并自动挂载
自动挂载不是每次都能成功,有时候还是要借助mount指令,后文有讲怎么挂载。
ubuntu@ubuntu:~/kpartx$ kpartx -av core-image-minimal-imx8mm-20200227113353.rootfs.sdcard
add map loop1p1 (252:2): 0 131072 linear 7:1 16384
add map loop1p2 (252:3): 0 10338305 linear 7:1 147456
需要先umount
ubuntu@ubuntu:~/kpartx$ umount /dev/mapper/loop1p*
用e2fsck来检查分区,并自动修复错误
ubuntu@ubuntu:~/kpartx$ e2fsck -f /dev/mapper/loop1p2
e2fsck 1.42.13 (17-May-2015)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mapper/loop1p2: 1824/22528 files (0.8% non-contiguous), 62806/90112 blocks
resize2fs命令是用来增大或者收缩未加载的“ext2/ext3/ext4”文件系统的大小
ubuntu@ubuntu:~/kpartx$ resize2fs /dev/mapper/loop1p2
resize2fs 1.42.13 (17-May-2015)
Resizing the filesystem on /dev/mapper/loop1p2 to 5169152 (1k) blocks.
The filesystem on /dev/mapper/loop1p2 is now 5169152 (1k) blocks long.
删除挂载的镜像
ubuntu@ubuntu:~/kpartx$ kpartx -d core-image-minimal-imx8mm-20200227113353.rootfs.sdcard
loop deleted : /dev/loop1
----------------------------------------------------------------------------------------------------------------------------------------
题外话,不重要的内容
losetup也可以用来处理lo设备,缺点是lo设备不能有分区
挂载sdcard到指定目录(例如/mnt/sd):
查看可用的loop设备
root@bert-u64:/opt/myd-sdk/rootfs# losetup -f
/dev/loop0
将loop0和sdcard镜像关联
root@bert-u64:/opt/myd-sdk/rootfs# losetup /dev/loop0 fsl-image-qt5-validation-imx-myd-imx8mm-20201023013524.rootfs.sdcard
挂载loop0到系统
root@bert-u64:/opt/myd-sdk/rootfs# kpartx -av /dev/loop0
add map loop0p1 (253:0): 0 131072 linear 7:0 16384
add map loop0p2 (253:1): 0 10338305 linear 7:0 147456
挂载
mount /dev/mapper/loop0p2 /mnt/sd
卸载
umount /mnt/sd
删除挂载的镜像
ubuntu@ubuntu:~/kpartx$ kpartx -d core-image-minimal-imx8mm-20200227113353.rootfs.sdcard
loop deleted : /dev/loop1