Linux给原有磁盘扩容(使用未分配空间)
Linux Expands Existing Disk Capacity (Using Unallocated Space)
————————————————————
1. 准备
需要 fdisk、partprobe、resize2fs 工具。
注意:示例只能给最后一个磁盘扩容。
系统分区大概像这样:
/dev/mmcblk0p1 /boot
/dev/mmcblk0p2 /system
只扩容 /dev/mmcblk0p2 这个系统分区。
2. 查看磁盘分区
观察到还有5-6G的未分配空间:
root@ls1043ardb:~# fdisk -l /dev/mmcblk0
Disk /dev/mmcblk0: 7.28 GiB, 7818182656 bytes, 15269888 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: 0xcce86c19
Device Boot Start End Sectors Size Id Type
/dev/mmcblk0p1 * 32768 373553 340786 166.4M 83 Linux
/dev/mmcblk0p2 376832 3123459 2746628 1.3G 83 Linux
root@ls1043ardb:~#
3. 开始扩容
3.1 编辑分区表
为了保证原来的数据还在,其他分区的磁盘block起点和终点需要一致。
root@ls1043ardb:~# fdisk /dev/mmcblk0
Welcome to fdisk (util-linux 2.37.4).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.
Command (m for help): p
Disk /dev/mmcblk0: 7.28 GiB, 7818182656 bytes, 15269888 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: 0xcce86c19
Device Boot Start End Sectors Size Id Type
/dev/mmcblk0p1 * 32768 373553 340786 166.4M 83 Linux
/dev/mmcblk0p2 376832 3123459 2746628 1.3G 83 Linux
Command (m for help): d
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): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-15269887, default 2048): 32768
Last sector, +/-sectors or +/-size{K,M,G,T,P} (32768-15269887, default 15269887): 373553
Created a new partition 1 of type 'Linux' and of size 166.4 MiB.
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): 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 (2048-15269887, default 2048): 376832
Last sector, +/-sectors or +/-size{K,M,G,T,P} (376832-15269887, default 15269887):
Created a new partition 2 of type 'Linux' and of size 7.1 GiB.
Partition #2 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): w
The partition table has been altered.
Syncing disks.
3.2 更新分区表
root@ls1043ardb:~# partprobe
root@ls1043ardb:~#
3.3 应用扩容
root@ls1043ardb:~# resize2fs /dev/mmcblk0p1
resize2fs 1.46.5 (30-Dec-2021)
The filesystem is already 170392 (1k) blocks long. Nothing to do!
root@ls1043ardb:~# resize2fs /dev/mmcblk0p2
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/mmcblk0p2 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mmcblk0p2 is now 1861632 (4k) blocks long.
3.4 检查扩容
root@ls1043ardb:~# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 6.9G 787M 5.8G 12% /
devtmpfs 908M 4.0K 908M 1% /dev
tmpfs 927M 168K 927M 1% /run
tmpfs 927M 176K 927M 1% /var/volatile
/dev/mmcblk0p1 156M 42M 103M 29% /run/media/boot-mmcblk0p1