Linux系统的LVM磁盘可以很方便的实现在线扩容。下面是对LVM磁盘进行在线扩容的具体操作步骤的简单介绍:

物理服务器要进行磁盘扩容,需要首先往服务器的空闲槽位插上新的硬盘,然后配置相应的raid模式,启动服务器后,可以参照下面的步骤进行扩容。

如果是ESXi虚拟机需要扩容,则需要通过VCentervphere client给虚拟机添加适当大小的虚拟磁盘。如果是较新的linux内核版本,新增的磁盘通过fdisk -l命令可以直接识别到;如果是较老的内核版本,则需要重启系统才能识别到。

下面的过程,是在fdisk -l已经可以识别新增磁盘的情况下执行相应操作。

    首先执行df -h命令,确认需扩容的lvm分区的卷组和逻辑卷信息。如下例,拟对根分区进行扩容,根分区所在卷组是VolGroup00,对应逻辑卷是LogVol00

[root@idca-wx-web2 ~]# df -h
文件系统              容量  已用 可用 已用% 挂载点
/dev/mapper/VolGroup00-LogVol00
                       42G   13G   28G  31% /
/dev/sda1              99M   17M   78M  18% /boot
tmpfs                 7.9G     0  7.9G   0% /dev/shm

    然后通过确认新增磁盘的标识符,如下例,是/dev/sdc。一般新增磁盘是没有经过格式化的,不存在/dev/sdc1这样的物理分区。当然,如果在增加新磁盘前先用fdisk -l检查一下,增加磁盘后再做个对比,会比较可靠。另外要注意,在oracle数据库服务器上,有些磁盘是裸盘用作ASM数据盘了,也没有/dev/sdc1这样的物理分区。在进行格式化之前,一定要认真确认,避免误操作。

[root@idca-wx-web2 ~]# fdisk -l
 
Disk /dev/sda: 108 MB, 108134400 bytes
4 heads, 32 sectors/track, 1650 cylinders
Units = cylinders of 128 * 512 = 65536 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           2        1633      104448   83  Linux
 
Disk /dev/sdb: 57.9 GB, 57987432448 bytes
196 heads, 58 sectors/track, 9962 cylinders
Units = cylinders of 11368 * 512 = 5820416 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        9963    56627200   8e  Linux LVM
 
Disk /dev/sdc: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
Disk /dev/sdc doesn't contain a valid partition table

    确认/dev/sdc是新增加的用来对根分区进行扩容的磁盘后,建议首先对该磁盘进行格式化,然后在创建物理卷。直接创建物理卷也可以,但在以后使用过程中可能会出问题。

[root@idca-wx-web2 ~]# fdisk /dev/sdc
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.
 
 
The number of cylinders for this disk is set to 10443.
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 LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
 
Command (m for help): p           #注:首先查看磁盘分区信息
 
Disk /dev/sdc: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
   Device Boot      Start         End      Blocks   Id  System
 
Command (m for help): n           #注:创建新分区
Command action
   e   extended
   p   primary partition (1-4)
p                               #注:选择主分区模式
Partition number (1-4): 1            #注:选择分区号,根据需要确定,如果整块盘的空间都用于一个逻辑卷的扩容,那么就用分区号1即可
First cylinder (1-10443, default 1):         #注:初始柱面,直接回车即可
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-10443, default 10443):   #注:结束柱面,直接回车即可。如果要建多个分区,则需要根据分区容量输入相应数值
Using default value 10443
 
Command (m for help): p        #注:再次查看分区表,对结果进行确认,可以看到出现了/dev/sdc1的物理分区了。注意其Id值是83,表明是Linux物理磁盘
 
Disk /dev/sdc: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1       10443    83883366   83  Linux
 
Command (m for help): t         #注:修改分区Id
Selected partition 1            
Hex code (type L to list codes): 8e   #注:修改成LVM磁盘的Id,可以输入L查看可用Id列表
Changed system type of partition 1 to 8e (Linux LVM)
 
Command (m for help): p         #注:再次查看分区表,对结果进行确认
 
Disk /dev/sdc: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1       10443    83883366   8e  Linux LVM
 
Command (m for help): w        #注:没问题的话,把修改写入磁盘,到这步才真正修改分区表,如果之前有错,大可随时修改
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.

    格式化完成后,下面开始LVM相关操作。首先在刚才格式化的物理分区/dev/sdc1上创建物理卷,最好是在创建物理卷前后都执行一下pvs命令,方便确认创建物理卷前后的变化:

[root@idca-wx-web2 ~]# pvcreate /dev/sdc1
  dev_is_mpath: failed to get device for 8:33
  Writing physical volume data to disk "/dev/sdc1"
  Physical volume "/dev/sdc1" successfully created
[root@idca-wx-web2 ~]# pvs
  PV         VG         Fmt  Attr PSize  PFree
  /dev/sdb1  VolGroup00 lvm2 a--  54.00G     0
  /dev/sdc1             lvm2 a--  80.00G 80.00G

    把刚创建的物理卷加入已有卷组,扩展卷组容量。有多个卷组的话,要参考df -h命令结果确定具体卷组名称。扩展卷组前后执行vgs命令,比较卷组扩展前后变化:

[root@idca-wx-web2 ~]# vgs
  VG         #PV #LV #SN Attr   VSize  VFree
  VolGroup00   1   2   0 wz--n- 54.00G    0
[root@idca-wx-web2 ~]# vgextend VolGroup00 /dev/sdc1
  Volume group "VolGroup00" successfully extended
[root@idca-wx-web2 ~]# vgs
  VG         #PV #LV #SN Attr   VSize   VFree
  VolGroup00   2   2   0 wz--n- 134.00G 80.00G

    扩展卷组内的逻辑卷。卷组内有多个逻辑卷的话,要参考df -h命令结果确定具体逻辑卷名称。扩展逻辑卷前后执行lvs命令,比较逻辑卷扩展前后变化:

[root@idca-wx-web2 ~]# lvs
  LV       VG         Attr   LSize  Origin Snap%  Move Log Copy%  Convert
  LogVol00 VolGroup00 -wi-ao 50.00G
  LogVol01 VolGroup00 -wi-ao  4.00G
[root@idca-wx-web2 ~]# lvextend -l +100%FREE /dev/mapper/VolGroup00-LogVol00
  Extending logical volume LogVol00 to 130.00 GB
  Logical volume LogVol00 successfully resized
[root@idca-wx-web2 ~]# lvs
  LV       VG         Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  LogVol00 VolGroup00 -wi-ao 130.00G
  LogVol01 VolGroup00 -wi-ao   4.00G

    逻辑卷扩展后,逻辑卷的容量并不会发生变化,还需要执行resize2fs命令让文件系统的容量跟逻辑卷的容量一致。执行命令前后最好都执行一下df -h命令,比较文件系统容量变化:

[root@idca-wx-web2 ~]# resize2fs /dev/VolGroup00/LogVol00  #如果文件系统是xfs类型,请使用xfs_growfs命令
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/VolGroup00/LogVol00 is mounted on /; on-line resizing require                                  d
Performing an on-line resize of /dev/VolGroup00/LogVol00 to 34077696 (4k) blocks.
The filesystem on /dev/VolGroup00/LogVol00 is now 34077696 blocks long.
 
[root@idca-wx-web2 ~]# df -h
文件系统              容量  已用 可用 已用% 挂载点
/dev/mapper/VolGroup00-LogVol00
                      109G   13G   91G  12% /
/dev/sda1              99M   17M   78M  18% /boot
tmpfs                 7.9G     0  7.9G   0% /dev/shm