resize2fs 功能说明:调整ext2/ext3/ext4文件系统大小
resize2fs命令用于扩容或收缩未挂载的ext2/ext3/ext4文件系统。
在Linux2.6或更高版本的内核中,该命令还支持在线扩容已经挂载的文件系统,该命令常用来针对LVM扩容后的分区使用。

参数选项:
-p 打印完成任务的进度条
-f 强制执行操作


范例:动态修改分区大小的例子
假设是要对/dev/sdb上的分区进行操作,将/mnt/data1扩容。

[root@cs6 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_cs6-lv_root
26G 834M 24G 4% /
tmpfs 931M 0 931M 0% /dev/shm
/dev/sda1 477M 28M 424M 7% /boot
/dev/sdb1 194M 1.8M 182M 1% /mnt/data1
/dev/sdb2 194M 1.8M 182M 1% /mnt/data2
[root@cs6 ~]# touch /mnt/data1/{1..5}.html
[root@cs6 ~]# touch /mnt/data2/{1..5}.html


提示:/dev/sdb是一块1GB的磁盘,现在分了两个主分区sdb1、sdb2,分别是200MB,剩余800MB未分区。现在需要把sdb2分区和sdb1分区合并,以实现对sdb1的扩容,
注意,此种情况不要在生产场景操作,仅作为演示用,生产场景一般是事先规划好不会出现扩容需求,非I/O密集应用可以采用LVM实现规范动态扩容。

以下是扩容实战步骤。

1)记录分区的柱面起始信息:

[root@cs6 ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x1bc7d413

Device Boot Start End Blocks Id System
/dev/sdb1 1 26 208813+ 83 Linux
/dev/sdb2 27 52 208845 83 Linux


2)卸载分区:

[root@cs6 ~]# umount /mnt/data1
[root@cs6 ~]# umount /mnt/data2

[root@cs6 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_cs6-lv_root
26G 834M 24G 4% /
tmpfs 931M 0 931M 0% /dev/shm
/dev/sda1 477M 28M 424M 7% /boot


3)重新分区:

[root@cs6 ~]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

Command (m for help): d
Partition number (1-4): 1

Command (m for help): d
Selected partition 2

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-130, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130): 52

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x1bc7d413

Device Boot Start End Blocks Id System
/dev/sdb1 1 52 417658+ 83 Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@cs6 ~]# partprobe /dev/sdb
[root@cs6 ~]# mount /dev/sdb1 /mnt/data1
[root@cs6 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_cs6-lv_root
26G 834M 24G 4% /
tmpfs 931M 0 931M 0% /dev/shm
/dev/sda1 477M 28M 424M 7% /boot
/dev/sdb1 194M 1.8M 182M 1% /mnt/data1


[root@cs6 ~]# ls /mnt/data1
1.html 2.html 3.html 4.html 5.html lost+found
[root@cs6 ~]# resize2fs /dev/sdb1 #<==在线调整磁盘大小,
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/sdb1 is mounted on /mnt/data1; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/sdb1 to 417656 (1k) blocks.
The filesystem on /dev/sdb1 is now 417656 blocks long.

[root@cs6 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_cs6-lv_root
26G 834M 24G 4% /
tmpfs 931M 0 931M 0% /dev/shm
/dev/sda1 477M 28M 424M 7% /boot
/dev/sdb1 392M 2.3M 369M 1% /mnt/data1
[root@cs6 ~]# ls /mnt/data1
1.html 2.html 3.html 4.html 5.html lost+found



但是数据还是只有sdb1里的了,sdb2的数据丢失了。
此种方法不适合生产场景扩容,比较规范方法是通过LVM逻辑卷管理进行扩容,扩容后也需要resize2fs进行最终实现扩容。