为虚拟机进行LVM空间扩容(新手教程)

发布时间:2020-03-07编辑:脚本学堂
本文介绍下,为虚拟机环境中的LVM进行空间扩容的方法与步骤。有需要的朋友,参考下吧。

一台配置有CACTI的监控服务的虚拟机,空间快不够了,需要给这台虚拟机扩下容量了。

具体操作步骤如下:
 

复制代码 代码示例:
[root@localhost ~]# df
Filesystem           1K-blocks      Used Available Use% linuxjishu/9952.html target=_blank class=infotextkey>mounted on
/dev/mapper/VolGroup00-LogVol00
                       6983168   2253172   4369548  35% /
/dev/sda1               101086     12056     83811  13% /boot
tmpfs                   257668         0    257668   0% /dev/shm
[root@localhost ~]# fdisk -l
Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        1044     8281507+  8e  Linux LVM
Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdb doesn't contain a valid partition table

一,创建物理卷
 

复制代码 代码示例:
[root@localhost ~]# pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created
[root@localhost ~]# pvdisplay                     ##可以用“pvdisplay”命令查看物理卷情况
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               VolGroup00
  PV Size               7.90 GB / not usable 23.41 MB
  Allocatable           yes (but full)
  PE Size (KByte)       32768
  Total PE              252
  Free PE               0
  Allocated PE          252
  PV UUID               Up0jiK-5I0E-d5J5-wZSu-WIhX-aKRA-OR9Pjm
 
  "/dev/sdb" is a new physical volume of "1.00 GB"               ###新建的物理卷
  --- NEW Physical volume ---
  PV Name               /dev/sdb             
  VG Name             
  PV Size               1.00 GB
  Allocatable           NO
  PE Size (KByte)       0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               zkullQ-PgoI-fHH4-xL3K-VLGm-JgMr-T7R1oA
 

 
二,将新添的物理卷加入到已有的逻辑卷组中
 

复制代码 代码示例:
[root@localhost ~]# vgextend /dev/VolGroup00 /dev/sdb
  /dev/cdrom: open failed: Read-only file system
  /dev/cdrom: open failed: Read-only file system
  Attempt to close device '/dev/cdrom' which is not open.
  Volume group "VolGroup00" successfully extended
 
[root@localhost ~]# vgdisplay                ##查看卷组的空间
  --- Volume group ---
  VG Name               VolGroup00
  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               8.84 GB
  PE Size               32.00 MB
  Total PE              283
  Alloc PE / Size       252 / 7.88 GB                      ###原有的
  Free  PE / Size       31 / 992.00 MB                    ##新加的扩容进去的空间
  VG UUID               0q4Uy2-coR3-bLwU-izpJ-kLOC-PxO3-9Efm8H

三,查看下分区
 

复制代码 代码示例:
[root@localhost ~]# lvdisplay
  --- Logical volume ---
  LV Name                 /dev/VolGroup00/LogVol00
  VG Name                VolGroup00
  LV UUID                DDmERv-LIlu-cwXR-oM9t-33wj-l2ko-zw715W
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                6.88 GB
  Current LE             220
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
 
  --- Logical volume ---
  LV Name                 /dev/VolGroup00/LogVol01
  VG Name                VolGroup00
  LV UUID                e0uO3o-HHrl-LFD1-iy3j-GSQc-U5mM-Q0GU4F
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                1.00 GB
  Current LE             32
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

四,开始扩容的操作。
 

复制代码 代码示例:

[root@localhost ~]# lvextend -L +1G /dev/VolGroup00/LogVol00
  Extending logical volume LogVol00 to 7.88 GB
  Insufficient free space: 32 extents needed, but only 31 available
 
###这里显示失败,空间不够,我的命令是加一个G的空间,但是空间不足。

[root@localhost ~]# lvextend -L +900M /dev/VolGroup00/LogVol00
  Rounding up size to full physical extent 928.00 MB
  Extending logical volume LogVol00 to 7.78 GB
  Logical volume LogVol00 successfully resized
 

 
##成功了,我改为添加900M,其实在上面vgdisplay时,已可以看到添加了多少空间。
此时,再用lvdisplay命令看LV的空间的话,可以看到/dev/VolGroup00/LogVol00的空间已经增大 了。
 
检查LVM设备对应inode和block之间的关系:
 

复制代码 代码示例:
[root@localhost ~]# e2fsck -f /dev/VolGroup00/LogVol00
e2fsck 1.39 (29-May-2006)
/dev/VolGroup00/LogVol00 is mounted.
WARNING!!!  Running e2fsck on a mounted filesystem may cause
SEVERE filesystem damage.
Do you really want to continue (y/n)? yes
/dev/VolGroup00/LogVol00: recovering journal
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/VolGroup00/LogVol00: ***** FILE SYSTEM WAS MODIFIED *****
/dev/VolGroup00/LogVol00: ***** REBOOT LINUX *****
/dev/VolGroup00/LogVol00: 108951/2064384 files (0.3% non-contiguous), 627952/2048000 blocks

五,使增加的容量生效:
 

复制代码 代码示例:
[root@localhost ~]# resize2fs /dev/VolGroup00/LogVol00
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/VolGroup00/LogVol00 is mounted on /; on-line resizing required
Performing an on-line resize of /dev/VolGroup00/LogVol00 to 2048000 (4k) blocks.
The filesystem on /dev/VolGroup00/LogVol00 is now 2048000 blocks long.
 

六,用df查看下当前的空间情况:
 

复制代码 代码示例:
[root@localhost ~]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                       7933376   2253184   5270604  30% / #本来是35%     
/dev/sda1               101086     12056     83811  13% /boot
tmpfs                   257668         0    257668   0% /dev/shm

至此在虚拟机上,最简单的一个LVM扩容操作完毕,分享下,供大家参考。