Linux板卡刷写img镜像后对分区进行扩容/调整分区大小
使用img镜像刷写至SD卡确实启动系统方便,但是刷写后的的根文件系统分区一般都很小。这时候就需要启动系统后对根文件系统分区进行扩容。
自制img镜像的方法可以参考:使用genimage工具自制img系统镜像
主要流程
进行根文件系统扩容的主要流程为:
- 确定根文件系统分区
- 删除旧分区
- 在原来的位置新建更大的分区
- 使用工具resize分区
具体步骤
确定根文件系统分区
-
使用
fdisk
工具查看根文件系统分区,如下所示,我这里为/dev/mmcblk0p2
,大小仅为900M,也就是我们要扩容的分区。root@ubuntu:~# fdisk -l Disk /dev/mmcblk0: 14.8 GiB, 15829303296 bytes, 30916608 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: dos Disk identifier: 0x9dc0e9eb Device Boot Start End Sectors Size Id Type /dev/mmcblk0p1 * 2048 67583 65536 32M c W95 FAT32 (LBA) /dev/mmcblk0p2 67584 1910783 1843200 900M 83 Linux
-
使用fdisk工具对分区所在设备进行分区修改,
root@ubuntu:~# fdisk /dev/mmcblk0 #指定p2分区所在设备 Welcome to fdisk (util-linux 2.33.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): p #打印下目前分区情况 Disk /dev/mmcblk0: 14.8 GiB, 15829303296 bytes, 30916608 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: dos Disk identifier: 0x9dc0e9eb Device Boot Start End Sectors Size Id Type /dev/mmcblk0p1 * 2048 67583 65536 32M c W95 FAT32 (LBA) /dev/mmcblk0p2 67584 1910783 1843200 900M 83 Linux
删除旧分区
Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 has been deleted.
在原来的位置新建更大的分区
- 从之前删除分区开始的地方新建一个更大的分区,一般默认即可。
Command (m for help): n #新建分区 Partition type p primary (1 primary, 0 extended, 3 free) e extended (container for logical partitions) Select (default p): p #选择为主分区 Partition number (2-4, default 2): #默认回车 First sector (67584-30916607, default 67584): #核对下是否为前面删除分区的开始位置,默认回车 Last sector, +/-sectors or +/-size{ K,M,G,T,P} (67584-30916607, default 30916607): #如果要占用剩下所有的sd卡的话,就默认回车,否则自行调整大小 Created a new partition 2 of type 'Linux' and of size 14.7 GiB. Partition #2 contains a ext4 signature. Do you want to remove the signature? [Y]es/[N]o: n #这里一定要选择n,否则就失败了
- 核对分区是否正确,大小从原来的900M变为了14.7G,也就是剩下的所有空间都给根文件系统了。
Command (m for help): p #打印查看分区 Disk /dev/mmcblk0: 14.8 GiB, 15829303296 bytes, 30916608 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: dos Disk identifier: 0x9dc0e9eb Device Boot Start End Sectors Size Id Type /dev/mmcblk0p1 * 2048 67583 65536 32M c W95 FAT32 (LBA) /dev/mmcblk0p2 67584 30916607 30849024 14.7G 83 Linux Command (m for help): w #保存退出 The partition table has been altered. Syncing disks.
使用resize2fs
对分区进行resize。
root@ubuntu:~# resize2fs /dev/mmcblk0p2 #/dev/mmcblk0p2为新的更大的分区
resize2fs 1.44.5 (15-Dec-2018)
Filesystem at /d[ 4987.424680] EXT4-fs (mmcblk0p2): resizing filesystem from 921600 to 15424512 blocks
ev/mmcblk0p2 is [ 4987.434179] EXT4-fs (mmcblk0p2): Converting file system to meta_bg
mounted on /; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 60
[ 4997.498500] EXT4-fs (mmcblk0p2): resized to 6748321 blocks
[ 5007.540934] EXT4-fs (mmcblk0p2): resized to 9596161 blocks
[ 5017.651059] EXT4-fs (mmcblk0p2): resized to 11783041 blocks
[ 5027.800246] EXT4-fs (mmcblk0p2): resized to 13578241 blocks
[ 5037.938371] EXT4-fs (mmcblk0p2): resized to 15193921 blocks
[ 5039.459514] EXT4-fs (mmcblk0p2): resized filesystem to 15424512
The filesystem on /dev/mmcblk0p2 is now 15424512 (1k) blocks long.