扩容虚拟机硬盘
前提条件
通过fdisk -l
能看到 /dev/mapper/XXXX-lv_swap
和 /dev/mapper/XXXX-lv_root
才可以使用该方法,其中的XXXX
每台设备可能不一样;如下:
[root@localhost ~]# 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: 0x00024cd6
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 5222 41430016 8e Linux LVM
Disk /dev/mapper/VolGroup-lv_root: 38.2 GB, 38193332224 bytes
255 heads, 63 sectors/track, 4643 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: 4227 MB, 4227858432 bytes
255 heads, 63 sectors/track, 514 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
关闭linux操作系统
shutdown -h now
添加虚拟机磁盘
查看新添加的硬盘对应的linux系统的文件
使用fdisk -l
查看系统的磁盘情况,每一个Disk
表示一块磁盘,由于我们添加的是硬盘,所以是sd
开头的,而且这块是新的硬盘所以没有分区信息; 从上图看就是sdb
了;
这样我们知道了新添加的硬盘对应的 linux系统中的哪个文件了;
扩容磁盘空间
现在开始扩容linux系统的磁盘空间了,此处使用LVM
的知识,有兴趣的可以自行去了解下;
-
新建物理卷,将新添加的硬盘创建为LVM格式的物理卷,
/dev/sdb
磁盘文件;[root@localhost ~]# pvcreate /dev/sdb Physical volume "/dev/sdb" successfully created
-
查看逻辑卷组的名称,也即是
VG Name
;bash [root@localhost ~]# 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 39.51 GiB PE Size 4.00 MiB Total PE 10114 Alloc PE / Size 10114 / 39.51 GiB Free PE / Size 0 / 0 VG UUID cKtBdq-4n3A-LuLe-QJnQ-j5Be-uOM7-bvVFoM
-
将新的物理卷添加到已经存在的逻辑卷组中;
VolGroup
是逻辑卷组的名称,dev/sdb
是linux系统中的磁盘文件(新添加的硬盘文件)[root@localhost ~]# vgextend VolGroup /dev/sdb Volume group "VolGroup" successfully extended
-
查看逻辑卷的名称,每个逻辑卷可以看成是在逻辑卷组上一个分区;要先给逻辑卷扩容,首先要将磁盘划分成物理卷,然后添加到逻辑卷组中才行;
[root@localhost ~]# lvdisplay --- Logical volume --- LV Path /dev/VolGroup/lv_root LV Name lv_root VG Name VolGroup LV UUID fqtkt5-jl0k-aBQt-c0o9-lpQN-w5y3-XGLXhO LV Write Access read/write LV Creation host, time localhost.localdomain, 2018-11-07 17:10:34 +0800 LV Status available # open 1 LV Size 35.57 GiB Current LE 9106 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 --- Logical volume --- LV Path /dev/VolGroup/lv_swap LV Name lv_swap VG Name VolGroup LV UUID 0IWtgU-JShd-3uua-Vz1d-Cllg-CjFR-lRk4B2 LV Write Access read/write LV Creation host, time localhost.localdomain, 2018-11-07 17:10:41 +0800 LV Status available # open 1 LV Size 3.94 GiB Current LE 1008 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:1
-
给逻辑卷扩容,28G是根据新增硬盘的大小设定的;
/dev/VolGroup/lv_root
是逻辑卷的名称;[root@localhost ~]# lvextend -L +28G /dev/VolGroup/lv_root Size of logical volume VolGroup/lv_root changed from 35.57 GiB (9106 extents) to 63.57 GiB (16274 extents). Logical volume lv_root successfully resized
-
重新加载磁盘信息,
resize2fs
的参数表示逻辑卷的名称,[root@localhost ~]# resize2fs /dev/VolGroup/lv_root resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/VolGroup/lv_root is mounted on /; on-line resizing required old desc_blocks = 3, new_desc_blocks = 4 Performing an on-line resize of /dev/VolGroup/lv_root to 16664576 (4k) blocks. The filesystem on /dev/VolGroup/lv_root is now 16664576 blocks long. [root@localhost ~]#
使用
df -h
命令查看下,大功告成