如何在Linux系统上扩展LVM逻辑卷

2016-12-07 Linux

我们有一些客户的业务发展得非常迅速,经常他们会要求升级服务器存储空间。由于他们起初选择了单块硬盘并且在装系统的时候使用了LVM文件系统,那么存储扩容就需要增加硬盘并扩展LVM的逻辑容量了。

接下来我们就举个例子,假设当前的存储结构如下:

root ~:# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                       18G  4.0G   13G  24% /
/dev/sda1              99M   40M   55M  42% /boot
tmpfs                 502M     0  502M   0% /dev/shm
root ~:# 

现在,我们要使用 fdisk 来在新增加的磁盘上进行分区 (在这个例子中为 /dev/sdb)。

root ~:# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in the memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-261, default 1): 
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-261, default 261):
Using default value 261

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): w

The partition table has been altered!

Calling ioctl() to re-read partition table.
SCSI device sdb: 4194303 512-byte hdwr sectors (2147 MB)
sdb: Write Protect is off
SCSI device sdb: drive cache: write back
SCSI device sdb: 4194303 512-byte hdwr sectors (2147 MB)
sdb: Write Protect is off
SCSI device sdb: drive cache: write back
Syncing disks.
root ~:# 

接下来我们需要告诉系统分区表的变更。

root ~:# partprobe /dev/sdb

然后我们需要在新增加的硬盘上创建物理卷:

root ~:# pvcreate /dev/sdb1
   Physical volume "/dev/sdb1" successfully created

现在我们可以扩展物理卷组:

root ~:# vgentend VolGroup00 /dev/sdb1
   Volume group "VolGroup00" successfully extended

需要记住的是,在扩容的时候,往往需要留出一点空间给LVM的meta数据:

root ~:# lvextend -L +1.8G /dev/VolGroup00/LogVol00
   Rounding up size to full physical extend 1.81 GB
   Extending logical volume LogVol00 to 19.72 GB
   Logical volume LogVol00 successfully resized

最后,我们需要调整逻辑卷,进行附加磁盘空间的分配:

root ~:# 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 5169152 (4k) blocks.
The filesystem on /dev/VolGroup00/LogVol00 is now 5169152 blocks long.

最后检验一下,扩容是不是成功了:

root ~:# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                       20G  4.0G   15G  22% /
/dev/sda1              99M   40M   55M  42% /boot
tmpfs                 502M     0  502M   0% /dev/shm