在Linux上,如何不卸载分区,调整Ext4 root分区的大小

示例分区:当前的分区大小为7.8G。

# df -h .
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1      7.8G  642M  6.8G   9% /

但是,磁盘大小为20GB:

# fdisk -l
Disk /dev/xvda: 20 GiB, 21474836480 bytes, 41943040 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: 0xd7f2e0e8
Device     Boot Start      End  Sectors Size Id Type
/dev/xvda1 *     4096 16773119 16769024   8G 83 Linux

由于上面的分区是作为root分区挂载,因此不能卸载:

# umount /
umount: /: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))

当前分区的起始扇区是4096。要扩大分区大小。我们需要删除分区并重新创建一个从扇区4096开始的新分区。

我们只需使用fdisk命令。

首先让打印我们的当前分区表:

# fdisk /dev/xvda
Welcome to fdisk (util-linux 2.25.2).                                                                                                                                                           
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/xvda: 20 GiB, 21474836480 bytes, 41943040 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: 0xd7f2e0e8
Device     Boot Start      End  Sectors Size Id Type
/dev/xvda1 *     4096 16773119 16769024   8G 83 Linux

在fdisk的交互模式下删除分区:

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-41943039, default 2048): 4096
Last sector, +sectors or +size{K,M,G,T,P} (4096-41943039, default 41943039): 
Created a new partition 1 of type 'Linux' and of size 20 GiB.

使分区1可引导,再次打印新分区表:

Command (m for help): a
Selected partition 1
The bootable flag on partition 1 is enabled now.
Command (m for help): p
Disk /dev/xvda: 20 GiB, 21474836480 bytes, 41943040 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: 0xd7f2e0e8
Device     Boot Start      End  Sectors Size Id Type
/dev/xvda1 *     4096 41943039 41938944  20G 83 Linux

确认所有信息,然后编写新分区表:

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Device or resource busy
The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).

此时,需要重新启动系统,以便系统使用新的大小重新挂载root分区。

在下次重新启动时强制fsck,以确保在安装分区之前检查分区。
为此,只需在“/”分区的根目录中创建一个名为“forcefsck”的空文件:

# touch /forcefsck

重新启动系统。

检查分区大小:

df -h .
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1       20G  644M   19G   4% /

和最后执行fsck检查:

# tune2fs -l /dev/xvda1
日期:2020-07-07 20:56:32 来源:oir作者:oir