CentOS6.5虚拟机 磁盘扩容


  1. 关闭虚拟机
  2. 编辑虚拟机设置:增加硬盘的置备大小,或者添加新的硬盘
  3. 启动虚拟机,查看可用磁盘大小 :

# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root ext4 18G 1.8G 15G 11% /
tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm
/dev/sda1 ext4 485M 39M 421M 9% /boot
  此时为未扩容前:20G,目标扩容挂载在根目录的/dev/mapper/VolGroup-lv_root 逻辑卷。
  1. 查看分区信息:
# fdisk -l
Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 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: 0x000244d1

Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 2611 20458496 8e Linux LVM

Disk /dev/mapper/VolGroup-lv_root: 18.8 GB, 18798870528 bytes
255 heads, 63 sectors/track, 2285 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: 0x00000000

Disk /dev/mapper/VolGroup-lv_swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 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: 0x00000000
  磁盘/dev/sda已有两个分区sda1、sda2。
  1. 接下来要新建分区sda3:
# fdisk /dev/sda
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (2611-5221, default 2611): 回车默认
Using default value 2611
Last cylinder, +cylinders or +size{K,M,G} (2611-5221, default 5221): 回车默认
Using default value 5221
Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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)
Syncing disks.

  1. 尝试partprobe命令更新分区,若失败,则reboot。
  2. 创建pv(物理卷):

# pvcreate /dev/sda3
dev_is_mpath: failed to get device for 8:3
Physical volume "/dev/sda3" successfully created
  1. 查看vg(卷组),确认需要扩展的vg:
# vgdisplay
--- Volume group ---
VG Name VolGroup
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.51 GiB
PE Size 4.00 MiB
Total PE 4994
Alloc PE / Size 4994 / 19.51 GiB
Free PE / Size 0 / 0
VG UUID MT0rRr-UWcY-ZYQ5-oR0C-q96r-NcbG-ufKJnz
此时未分配的空间为0
  1. 扩展vg(卷组):
# vgextend VolGroup /dev/sda3
Volume group "VolGroup" successfully extended

再次查看vg:

# vgdisplay
--- Volume group ---
VG Name VolGroup
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 2
Act PV 2
VG Size 39.50 GiB
PE Size 4.00 MiB
Total PE 10112
Alloc PE / Size 4994 / 19.51 GiB
Free PE / Size 5118 / 19.99 GiB
VG UUID MT0rRr-UWcY-ZYQ5-oR0C-q96r-NcbG-ufKJnz
  1. 扩展lv(逻辑卷):
# lvextend /dev/mapper/VolGroup-lv_root -L +19.99G
Rounding size to boundary between physical extents: 19.99 GiB
Extending logical volume lv_root to 37.50 GiB
Logical volume lv_root successfully resized
  1. 格式化文件系统:
# resize2fs /dev/mapper/VolGroup-lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/VolGroup-lv_root is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 3
Performing an on-line resize of /dev/mapper/VolGroup-lv_root to 9830400 (4k) blocks.
The filesystem on /dev/mapper/VolGroup-lv_root is now 9830400 blocks long.

之前df -Th 查看/dev/mapper/VolGroup-lv_root的Type为ext4,所以使用resize2fs;如果文件类型是 xfs ,则使用如下命令格式化:

# xfs_growfs /dev/mapper/VolGroup-lv_root
  1. df -Th 确认扩容成功
# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root ext4 37G 1.8G 34G 6% /
tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm
/dev/sda1 ext4 485M 39M 421M 9% /boot

参考:

https://www.jianshu.com/p/94ab4588d77c">​https://www.jianshu.com/p/94ab4588d77c​