在终端下调整ext4文件系统的大小

在带有GUI的Linux系统中,我们可以通过GParted方便地管理磁盘分区,但是在很多时候我们没有条件使用GUI工具,所以本文简单介绍一下如何在命令行界面调整ext4分区的大小。

系统环境

  • 待分配分区:/dev/sdb1
  • 分区工具:fdisk
  • 文件系统检查工具:e2fsck
  • 文件系统调整工具:resize2fs

扩大文件系统

首先需要明确,决定文件系统上限的是其所在的分区大小,因此,如果待调整的文件系统已经占满了整个分区,那么在调整文件系统之前,需要先调整分区大小,本文假设待调整分区的后面仍然有可用的空间

重建分区

原始状态下,分区dev/sdb1512M,我们要把它调整为768M

1
2
3
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 1G 0 disk
└─sdb1 8:17 0 512M 0 part /mnt
  1. 解挂分区

    1
    sudo umount /mnt
  2. 删除旧分区

    1
    sudo fdisk /dev/sdb
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    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): p
    Disk /dev/sdb: 1 GiB, 1073741824 bytes, 2097152 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: 0x485cc8a2

    Device Boot Start End Sectors Size Id Type
    /dev/sdb1 2048 1050623 1048576 512M 83 Linux

    Command (m for help): d
    Selected partition 1
    Partition 1 has been deleted.

    Command (m for help): w
    The partition table has been altered.
    Calling ioctl() to re-read partition table.
    Syncing disks.
  3. 创建新分区

    1
    sudo fdisk /dev/sdb
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    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): 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-2097151, default 2048):
    Last sector, +sectors or +size{K,M,G,T,P} (2048-2097151, default 2097151): +768M

    Created a new partition 1 of type 'Linux' and of size 768 MiB.
    Partition #1 contains a ext4 signature.

    Do you want to remove the signature? [Y]es/[N]o: n

    Command (m for help): w

    The partition table has been altered.
    Calling ioctl() to re-read partition table.
    Syncing disks.

    注意:新分区的起始扇区必须跟老分区的起始扇区一致。

检查文件系统

1
sudo e2fsck -f /dev/sdb1
1
2
3
4
5
6
7
e2fsck 1.44.1 (24-Mar-2018)
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/sdb1: 12/32768 files (0.0% non-contiguous), 6354/131072 blocks

调整文件系统

1
sudo resize2fs /dev/sdb1
1
2
3
resize2fs 1.44.1 (24-Mar-2018)
Resizing the filesystem on /dev/sdb1 to 196608 (4k) blocks.
The filesystem on /dev/sdb1 is now 196608 (4k) blocks long.

缩小文件系统

缩小文件系统理论上不需要调整分区,但是这样会造成空间的浪费,其操作顺序与扩大文件系统相反。我们现在把刚才的768M的文件系统再调整为512M

检查文件系统

1
sudo e2fsck -f /dev/sdb1
1
2
3
4
5
6
7
e2fsck 1.44.1 (24-Mar-2018)
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/sdb1: 12/49152 files (0.0% non-contiguous), 7447/196608 blocks

调整文件系统

1
sudo resize2fs /dev/sdb1 512M
1
2
3
resize2fs 1.44.1 (24-Mar-2018)
Resizing the filesystem on /dev/sdb1 to 131072 (4k) blocks.
The filesystem on /dev/sdb1 is now 131072 (4k) blocks long.

重建分区

  1. 删除旧分区

    1
    sudo fdisk /dev/sdb
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    Command (m for help): p
    Disk /dev/sdb: 1 GiB, 1073741824 bytes, 2097152 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: 0x485cc8a2

    Device Boot Start End Sectors Size Id Type
    /dev/sdb1 2048 1574911 1572864 768M 83 Linux

    Command (m for help): d
    Selected partition 1
    Partition 1 has been deleted.

    Command (m for help): w
    The partition table has been altered.
    Calling ioctl() to re-read partition table.
    Syncing disks.
  2. 创建新分区

    1
    sudo fdisk /dev/sdb
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    Command (m for help): p
    Disk /dev/sdb: 1 GiB, 1073741824 bytes, 2097152 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: 0x485cc8a2

    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-2097151, default 2048):
    Last sector, +sectors or +size{K,M,G,T,P} (2048-2097151, default 2097151): +512M

    Created a new partition 1 of type 'Linux' and of size 512 MiB.
    Partition #1 contains a ext4 signature.

    Do you want to remove the signature? [Y]es/[N]o: n

    Command (m for help): w

    The partition table has been altered.
    Calling ioctl() to re-read partition table.
    Syncing disks.

总结

本文所描述的场景仅仅适用于分区后面仍然有可分配的空间,如果后面紧跟着其它分区或者该分区是最后一个分区,会涉及到分区的移动,以后有机会会讲到。

参考资料

Resize Partition and Filesystem with fdisk & resize2fs