调整(扩展)文件系统大小的可行性

EXT3/4 文件系统所在的底层设备(在我们的例子中是分区)必须首先调整大小,然后才能调整文件系统的大小。
因此,必须首先确定底层设备大小调整的可行性,以确定文件系统大小调整是否可行。

以下条件/场景可以调整(扩展)EXT3/4 文件系统的大小:

  • EXT3/4 文件系统驻留在设备/分区上,但并未完全占据分区内的整个空间,并且在文件系统末端和分区末端边界之间存在足够的磁盘空间。
  • EXT3/4 文件系统驻留在占据整个分区空间的设备/分区上,但是紧接在文件系统分区结束边界之后的磁盘/设备上存在足够的连续空闲块,或者
  • 下一个分区开始边界或者
  • 整个磁盘/设备的结尾)
更多: zhilu jiaocheng

备份要调整大小的文件系统上的所有数据

如果执行不正确,调整文件系统和底层设备的大小是危险的,并且可能具有破坏性。
重新分区设备同样具有破坏性,可能会导致数据完全丢失。
在继续之前,备份要调整大小的文件系统/设备的内容。

CentOS/RHEL:在非 LVM 设备(硬盘分区)上调整(扩展)非根 EXT3/4 文件系统的大小

在硬盘分区(非 LVM)上调整(扩展)非根文件系统的大小

  1. 示例 设置
    在此示例中,设备 /dev/sdb (20Gb) 包含一个 11Gb 的主分区 (/dev/sdb1) 和跨越整个分区的 EXT3/4 文件系统 (/data)。

分区末端和设备末端之间大约有 9Gb 的可用磁盘空间。

# df -k /data
Filesystem          1K-blocks      Used Available Use% Mounted on
/dev/sdb1            11353328  10668192    223764  98% /data
# cat /proc/partitions | grep sdb
   8    16   20971520 sdb
   8    17   12008556 sdb1
# fdisk -l /dev/sdb
Disk /dev/sdb: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 1495 12008556 83 Linux
  1. 卸载要调整大小的文件系统
    卸载要调整大小的文件系统,例如:
# umount /data
  1. 执行文件系统检查
    验证要调整大小的文件系统的文件系统类型。
# blkid /dev/sdb1
/dev/sdb1: LABEL="/data" UUID="1fc0bbcd-ba86-40b6-b562-5da90fb0d7af" TYPE="ext3"

对文件系统执行文件系统检查,确保对文件系统类型使用相应的文件系统检查实用程序(fsck.ext3. fsck.ext4),例如:

# fsck.ext3 -fy /dev/sdb1
e2fsck 1.39 (29-May-2006)
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
data: 21/1441792 files (4.8% non-contiguous), 2712300/2883584 blocks
  1. 调整(扩展)分区大小
    此步骤假定分区末尾和设备末尾之间存在可用磁盘空间。
    如果可用磁盘空间不可用,请让存储管理员在重新创建更大的分区之前增加/扩展底层设备。
    使用 fdisk 实用程序删除然后重新创建一个更大的分区,确保重新使用原始分区起始块,例如:
# fdisk /dev/sdb
The number of cylinders for this disk is set to 2610.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of jack)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Command (m for help): p
Disk /dev/sdb: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 1495 12008556 83 Linux
Command (m for help): d       ### this step deletes the partition
Selected partition 1
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1): [enter]
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-2610, default 2610): [enter]
Using default value 2610
Command (m for help): p
Disk /dev/sdb: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 2610 20964793+ 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
  1. 验证分区大小调整
    验证系统现在可以看到新创建的更大的分区,例如:
# cat /proc/partitions | grep sdb
   8    16   20971520 sdb
   8    17   20964793 sdb1

如果系统无法检测到新分区大小,请针对调整大小的设备运行 partprobe 实用程序,例如:

# partprobe /dev/sdb
  1. 调整(扩展)文件系统
    使用 resize2fs 实用程序扩展 EXT3/4 文件系统以利用分区中的另外空间,例如:
# resize2fs /dev/sdb1
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/sdb1 to 5241198 (4k) blocks.
The filesystem on /dev/sdb1 is now 5241198 blocks long.

注意:当运行 resize2fs 时,如果没有指定大小,文件系统将被扩展以利用分区中的所有可用/剩余空间。

  1. 执行文件系统检查
    对调整后的 EXT3/4 文件系统执行文件系统检查,确保对正在使用的文件系统类型使用相应的文件系统检查实用程序(fsck.ext3. fsck.ext4),例如:
# fsck.ext3 -fy /dev/sdb1
e2fsck 1.39 (29-May-2006)
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
data: 21/2621440 files (4.8% non-contiguous), 2750333/5241198 blocks
  1. 挂载调整后的文件系统
    挂载新调整大小的 EXT3/4 文件系统,例如:
# mount /data
  1. 验证文件系统调整大小
    查看 dmesg、消息日志等以验证文件系统大小调整成功,例如:
# df -h /data
Filesystem         Size Used Avail Use% Mounted on
/dev/sdb1           20G  11G  9.0G  54% /data
日期:2020-09-17 00:12:21 来源:oir作者:oir