三、LVM的介绍及使用

LVM,Logical Volume Manager,即逻辑卷管理器。

最先由IBM开发,在AIX系统上实现,OS/2 操作系统与 HP-UX也支持这个功能。在1998年,Heinz Mauelshagen 根据在 HP-UX 上的逻辑卷管理器,写作出第一个 Linux 版本的逻辑卷管理器。基于Linux 2.6内核的可以全面支持硬盘、分区、RAID、LUN等。

 

应用场景

1、大量磁盘管理中,不能停机或者中断服务,LVM可以实现在线添加和置换

2、小系统应用时,LVM可以根据需要重新调整大小,而不必在安装的时候就预估出未来磁盘的使用量

3、使用逻辑卷快照完成备份,可以保证一致性

4、将多个物理卷或者整个磁盘,创建成单一逻辑卷,允许动态调整卷的大小

可以看出,LVM是创建在硬盘和分区之上的一个软件层,为磁盘置换、重分区、备份等管理实现了不间断性和易用性。

 

LVM的架构

Linux系统分区及LVM使用(二)_LVM

Linux系统分区及LVM使用(二)_LVM_02

以上2个图均来自互联网,从不同的角度描述了LVM的框架。

LVM就是一个建立在硬盘和分区之上一个逻辑层,它屏蔽了下层磁盘分区布局,提供一个抽象的盘卷,在其上建立文件系统,以提高磁盘分区管理的灵活性。

 

LVM的优点:

由以上应用场景、和架构图可知

  1. 使用卷组,使众多磁盘看起来像一个大磁盘一样供系统使用。
  2. 使用逻辑卷,可以创建跨越多个磁盘空间的分区,还可以动态调整逻辑卷大小。
  3. 可以在线对逻辑卷和卷组进行创建、删除、调整大小等操作。然后LVM上的文件系统也可以调整大小,有些文件系统还可以在线调整。
  4. 无需重新启动服务,就可以将服务中用到的逻辑卷在线、动态迁移至别的硬盘上。
  5. 允许创建快照,可以保存文件系统的备份,同时使服务的下线时间(downtime)降低到最小。

 

普通磁盘作为分区创建LV后,如果磁盘损坏,将导致数据丢失。

企业中使用LVM,应该在RAID(10或者5)之上创建LVM。

 

以下演示RAID10创建LV、扩展LV、缩减LV

(一)创建LV

1、使用上例中RAID10为基础,在其上重新分区,然后调整类型为8e

[root@localhost ~]# fdisk /dev/md3
 
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').
 
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1047552, default 257): 
Using default value 257
Last cylinder, +cylinders or +size{K,M,G} (257-1047552, default 1047552): 
Using default value 1047552
 
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.
Syncing disks.
[root@localhost ~]# fdisk -l /dev/md3
 
Disk /dev/md3: 4290 MB, 4290772992 bytes
2 heads, 4 sectors/track, 1047552 cylinders
Units = cylinders of 8 * 512 = 4096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 524288 bytes / 1048576 bytes
Disk identifier: 0x56fbb6c5
 
    Device Boot      Start         End      Blocks   Id  System
/dev/md3p1             257     1047552     4189184   8e  Linux LVM

2、创建PV

[root@localhost ~]# pvcreate /dev/md3p1
  Physical volume "/dev/md3p1" successfully created
[root@localhost ~]# pvs
  PV         VG   Fmt  Attr PSize  PFree
  /dev/md3p1      lvm2 a--   4.00g 4.00g

3、创建VG,使用-s选项调整PE大小为8M

[root@localhost ~]# vgcreate -s 8M vgr10 /dev/md3p1 
  Volume group "vgr10" successfully created
[root@localhost ~]# vgs
  VG    #PV #LV #SN Attr   VSize  VFree
  vgr10   1   0   0 wz--n-  3.99g 3.99g

4、创建2G的LV,指定名称为lvr10

[root@localhost ~]# lvcreate -L 2G -n lvr10 vgr10
  Logical volume "lvr10" created
[root@localhost ~]# lvs
  LV    VG    Attr       LSize  Pool Origin Data%  Move Log Cpy%Sync Convert                                            
  lvr10 vgr10 -wi-a-----  2.00g  

5、格式化lvr10,挂载它,然后测试大小,并测试

[root@localhost ~]# mke2fs -t ext4 /dev/vgr10/lvr10 
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=128 blocks, Stripe width=256 blocks
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912
 
Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
 
This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@localhost ~]# vim /etc/fstab
[root@localhost ~]# tail -1 /etc/fstab
/dev/vgr10/lvr10    /mnt/r10        ext4    defaults     0 0
[root@localhost ~]# mount -a
[root@localhost ~]# mount | grep "vgr10"
/dev/mapper/vgr10-lvr10 on /mnt/r10 type ext4 (rw)
[root@localhost ~]# df -h | grep "vgr10"
/dev/mapper/vgr10-lvr10  2.0G   67M  1.9G   4% /mnt/r10
 
[root@localhost ~]# cd /mnt/r10/
[root@localhost r10]# cat > lvtest.txt <<EOF
>this is a LV test file.
>EOF
[root@localhost r10]# cat /mnt/r10/lvtest.txt 
this is a LV test file.

(二)扩展LV

1、首先LV扩展到3G大小

[root@localhost ~]# lvextend -L +1G /dev/vgr10/lvr10 
  Extending logical volume lvr10 to 3.00 GiB
  Logical volume lvr10 successfully resized
[root@localhost ~]# lvs | grep "vgr10"
  lvr10 vgr10 -wi-ao----  3.00g   
[root@localhost ~]# df -h | grep "vgr10"
/dev/mapper/vgr10-lvr10  2.0G   68M  1.9G   4% /mnt/r10

2、在线扩展文件系统到3G,并验证文件

[root@localhost ~]# resize2fs /dev/vgr10/lvr10 
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vgr10/lvr10 is mounted on /mnt/r10; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vgr10/lvr10 to 786432 (4k) blocks.
The filesystem on /dev/vgr10/lvr10 is now 786432 blocks long.
[root@localhost ~]# df -h | grep "vgr10"
/dev/mapper/vgr10-lvr10  3.0G   68M  2.8G   3% /mnt/r10
[root@localhost ~]# cat /mnt/r10/lvtest.txt 
this is a LV test file.

(三)缩减LV

1、离线后,检查文件系统成功后,缩减文件系统到2G

[root@localhost ~]# e2fsck -f /dev/vgr10/lvr10
e2fsck 1.41.12 (17-May-2010)
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/vgr10/lvr10: 12/196608 files (0.0% non-contiguous), 29501/786432 blocks
[root@localhost ~]# resize2fs /dev/vgr10/lvr10 2G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vgr10/lvr10 to 524288 (4k) blocks.
The filesystem on /dev/vgr10/lvr10 is now 524288 blocks long.

2、缩减LV至2G

[root@localhost ~]# lvreduce -L 2G /dev/vgr10/lvr10 
  WARNING: Reducing active logical volume to 2.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lvr10? [y/n]: y
  Reducing logical volume lvr10 to 2.00 GiB
  Logical volume lvr10 successfully resized
[root@localhost ~]# lvs | grep "vgr10"
  lvr10 vgr10 -wi-a-----  2.00g      

3、挂载后,并验证数据

[root@localhost ~]# mount -a
[root@localhost ~]# df -h | grep "lvr10"
/dev/mapper/vgr10-lvr10  2.0G   68M  1.9G   4% /mnt/r10
[root@localhost ~]# cat /mnt/r10/lvtest.txt 
this is a LV test file.

四、LVM的快照

LVM快照是一个很有用的功能,这部分提出来在后续博客中介绍。

 

参考文献

1、Archlinux Wiki

https://wiki.archlinux.org/index.php/LVM_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)#.E5.BF.AB.E7.85.A7.E5.8A.9F.E8.83.BD

2、维基百科

http://en.wikipedia.org/wiki/Logical_Volume_Manager_(Linux)

3、Linux逻辑卷管理LVM功能详解及应用实例

https://community.emc.com/docs/DOC-17955

4、LVM Howto

http://www.tldp.org/HOWTO/LVM-HOWTO/index.html

5、逻辑卷管理,这是原帖
http://www.ibm.com/developerworks/cn/linux/l-lvm2/