Linux逻辑卷管理(LVM)
测试环境:RHEL5.3;硬盘8G,1000M一个分区分成6个
一、 LVMH逻辑卷模型图:

创建逻辑卷的步骤:
1)通过pvcreate命令将linux分区处理成物理卷(PV);
2)通过vgcreate命令将创建好的物理卷处理成卷组(Vg);
3)通过lvcreate命令将卷组分成若干个逻辑卷(Lv);
之后我们可以对逻辑卷进行格式化,挂载,删除等操作,我们可以动态的调整逻辑卷的大小,并且该操作不会影响我们在逻辑卷(Lv)上的数据。
二、物理卷创建管理操作:
 1)pvcreate创建物理卷:( 把普通Linux分区变成物理卷)
    格式:pvcreate /dev/sdb{1,2,3...} 
———————————————————————————————
[root@station1 ~]# [root@station1 ~]# pvcreate /dev/sdb{1,2,3,5,6,7}
  Physical volume "/dev/sdb3" successfully created
  Physical volume "/dev/sdb5" successfully created
  Physical volume "/dev/sdb6" successfully created
  Physical volume "/dev/sdb7" successfully created
————————————————————————————————
 2)pvscan查看物理卷信息:会显示所有物理卷的情况。
    格式:pvscan
————————————————————————————————
[root@station1 ~]# pvscan
  PV /dev/sdb1                      lvm2 [964.81 MB]
  PV /dev/sdb2                      lvm2 [964.84 MB]
  PV /dev/sdb3                      lvm2 [964.84 MB]
  PV /dev/sdb5                      lvm2 [964.81 MB]
  PV /dev/sdb6                      lvm2 [964.81 MB]
  PV /dev/sdb7                      lvm2 [964.81 MB]
  Total: 6 [5.65 GB] / in use: 0 [0   ] / in no VG: 6 [5.65 GB]
————————————————————————————————
 3)pvdisplay查看各个物理卷的详细参数:
     格式:pvdisplay  [/dev/sdb{1,2,3...}]  不跟参数则显示所有的物理卷信息
————————————————————————————————
[root@station1 ~]# pvdisplay /dev/sdb1
  "/dev/sdb1" is a new physical volume of "964.81 MB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name
  PV Size               964.81 MB
  Allocatable           NO
  PE Size (KByte)       0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               FKaYKk-d46E-KHCQ-WRnn-jUUE-Cd1d-mM8mPN
————————————————————————————————————
4)pvremove删除物理卷:当物理卷里的扩展快没有被使用时可以删除。
 格式:pvremove  /dev/sdb5
——————————————————————————————
[root@station1 ~]# pvscan
  PV /dev/sdb7   VG vg1             lvm2 [960.00 MB / 960.00 MB free]
  PV /dev/sdb1   VG vg0             lvm2 [964.00 MB / 964.00 MB free]
  PV /dev/sdb2   VG vg0             lvm2 [964.00 MB / 964.00 MB free]
  PV /dev/sdb3   VG vg0             lvm2 [964.00 MB / 964.00 MB free]
  PV /dev/sdb5                      lvm2 [964.81 MB]
  PV /dev/sdb6                      lvm2 [964.81 MB]
  Total: 6 [5.65 GB] / in use: 4 [3.76 GB] / in no VG: 2 [1.88 GB]
[root@station1 ~]# pvremove /dev/sdb5
  Labels on physical volume "/dev/sdb5" successfully wiped
[root@station1 ~]# pvscan
  PV /dev/sdb7   VG vg1             lvm2 [960.00 MB / 960.00 MB free]
  PV /dev/sdb1   VG vg0             lvm2 [964.00 MB / 964.00 MB free]
  PV /dev/sdb2   VG vg0             lvm2 [964.00 MB / 964.00 MB free]
  PV /dev/sdb3   VG vg0             lvm2 [964.00 MB / 964.00 MB free]
  PV /dev/sdb6                      lvm2 [964.81 MB]
  Total: 5 [4.70 GB] / in use: 4 [3.76 GB] / in no VG: 1 [964.81 MB]
————————————————————————————————
三、卷组的创建管理:
 1)vgcreate创建卷组:
    格式:vgcreate [-s  <8M|16M|...>] vg0 /dev/sdb{1,2,3...}
  -s:指定扩展块(PE)的大小,默认为4MB;(相当与磁盘上的簇,扩展逻辑卷的基本单位。 后面的值可以是8M 16M 32M 64M .....最多65532个扩展块。
  vg0:新创建的卷组的名字。
  /dev/sdb{1,2,3...}:需要的物理卷可以写多个;
————————————————————————————————
[root@station1 ~]# vgcreate vg0 /dev/sdb{1,2,3}
  Volume group "vg0" successfully created
————————————————————————————————
 2)vgdisplay查看各个卷组的详细参数:
    格式:vgdisplay  /dev/vg0
————————————————————————————————
[root@station1 ~]# vgdisplay /dev/vg0
  --- Volume group ---
  VG Name               vg0
  System ID
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  Cur PV                3
  Act PV                3
  VG Size               2.82 GB
  PE Size               4.00 MB   //扩展快大小 默认4MB
  Total PE              723
  Alloc PE / Size       0 / 0
  Free  PE / Size       723 / 2.82 GB
  VG UUID               w4q8ET-UBLY-rHwg-2ZVv-NOw4-dd1e-1ABr2f
[root@station1 ~]# vgcreate -s 16M vg1 /dev/sdb{5,6,7}
  Volume group "vg1" successfully created
[root@station1 ~]# vgdisplay /dev/vg1
  --- Volume group ---
  VG Name               vg1
  System ID
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  Cur PV                3
  Act PV                3
  VG Size               2.81 GB
  PE Size               16.00 MB     //指定扩展块大小为16MB
  Total PE              180
  Alloc PE / Size       0 / 0
  Free  PE / Size       180 / 2.81 GB
  VG UUID               x97eh2-JYxW-4Ygh-5axx-TquS-0vxR-YdFyJ1
———————————————————————————————————
3)vgscan查看卷组的信息:
    格式:vgsacn
————————————————————————————————
[root@station1 ~]# vgscan
  Found volume group "vg1" using metadata type lvm2
  Found volume group "vg0" using metadata type lvm2
—————————————————————————————————
 4)vgreduce缩小卷组:把物理卷重卷组中删除。
  格式:vgreduce vg0 /dev/sdb3
——————————————————————————————————
[root@station1 ~]# vgreduce vg0 /dev/sdb3
Removed "/dev/sdb3" from volume group "vg0"
——————————————————————————————————
 4)vgextend扩展卷组:把某个物理卷添加到卷组中。
     格式:vgextend vg0 /dev/sdb2   将物理卷/dev/sdb2添加到vg0卷组 
——————————————————————————————————
[root@station1 ~]# vgextend vg0 /dev/sdb3
  Volume group "vg0" successfully extended
——————————————————————————————————
 5)vgremove删除卷组:
  格式:vgremove /dev/vg1
——————————————————————————————————
[root@station1 ~]# pvscan
  PV /dev/sdb7   VG vg1   lvm2 [960.00 MB / 960.00 MB free]
  PV /dev/sdb5   VG vg1   lvm2 [960.00 MB / 960.00 MB free]
  PV /dev/sdb6   VG vg1   lvm2 [960.00 MB / 960.00 MB free]
  PV /dev/sdb1   VG vg0   lvm2 [964.00 MB / 964.00 MB free]
  PV /dev/sdb2   VG vg0   lvm2 [964.00 MB / 964.00 MB free]
  PV /dev/sdb3   VG vg0   lvm2 [964.00 MB / 964.00 MB free]
  Total: 6 [5.64 GB] / in use: 6 [5.64 GB] / in no VG: 0 [0   ]
[root@station1 ~]# vgremove /dev/vg1
  Volume group "vg1" successfully removed
[root@station1 ~]# pvscan
  PV /dev/sdb1   VG vg0             lvm2 [964.00 MB / 964.00 MB free]
  PV /dev/sdb2   VG vg0             lvm2 [964.00 MB / 964.00 MB free]
  PV /dev/sdb3   VG vg0             lvm2 [964.00 MB / 964.00 MB free]
  PV /dev/sdb5                      lvm2 [964.81 MB]
  PV /dev/sdb6                      lvm2 [964.81 MB]
  PV /dev/sdb7                      lvm2 [964.81 MB]
  Total: 6 [5.65 GB] / in use: 3 [2.82 GB] / in no VG: 3 [2.83 GB]
——————————————————————————————————
四、逻辑卷的创建及管理:
 1)lvcreate创建逻辑卷命令:
  格式1:lvcreate -L 512M -n data vg0
  -L 512M:设置逻辑卷的大小为512M。
  -n data:设置逻辑卷的名字为data ;表示法:/dev/卷组名/data
  vg0 :设置是有那个卷组生成的逻辑卷。
——————————————————————————————————
[root@station1 ~]# lvcreate -L 1000M -n data vg0
  Logical volume "data" created
//创建一个容量为1000MB的逻辑卷data
——————————————————————————————————
  格式2:lvcreate -l 32 -n data vg0
  -l 32:生成的逻辑卷使用32个扩展块,逻辑卷大小=32*PE(默认4M)则为128M
  -n data:逻辑卷的名字;
   vg0 :设置是有那个卷组生成的逻辑卷。
——————————————————————————————————
[root@station1 ~]# lvcreate -l 32 -n data128 vg0
  Logical volume "data128" created
 //创建一个32个扩展块,容量为32*4=128MB的逻辑卷data128
——————————————————————————————————
 2)格式化逻辑卷:
mkfs.ext3 /dev/vg0/data     mke2fs -j /dev/vg0/data
  3)lvsacn查看逻辑卷的信息:
¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬——————————————————————————————————
[root@station1 ~]# lvscan
  ACTIVE            '/dev/vg0/data' [1000.00 MB] inherit
——————————————————————————————————
 4)lvdisplay查看逻辑卷的具体参数:
——————————————————————————————————
[root@station1 ~]# lvdisplay /dev/vg0/data
  --- Logical volume ---
  LV Name                /dev/vg0/data
  VG Name                vg0
  LV UUID                9Od1nq-cHmY-MlrL-vgXW-7Kam-pAfU-Qc8mal
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                1000.00 MB
  Current LE             250
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
——————————————————————————————————
 3)增大逻辑卷大小:
  lvextend命令:增大逻辑卷大小
     格式:lvextend -L +100M /dev/vg0/data
——————————————————————————————————
[root@station1 ~]# lvextend -L +1000M /dev/vg0/data
  Extending logical volume data to 1.95 GB
  Logical volume data successfully resized
——————————————————————————————————
resize2fs /dev/vg0/data  使上面增加的立即生效。
——————————————————————————————————
[root@station1 ~]# resize2fs /dev/vg0/data
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/vg0/data to 512000 (4k) blocks.
The filesystem on /dev/vg0/data is now 512000 blocks long.
[root@station1 ~]# lvscan
  ACTIVE            '/dev/vg0/data' [1.95 GB] inherit
——————————————————————————————————
 4)减少逻辑卷大小:
  lvreduce命令:减少逻辑卷大小
 格式:Lvreduce -L -100M /dev/vg0/data
——————————————————————————————————
[root@station1 ~]# lvreduce -L -1000M /dev/vg0/data
  WARNING: Reducing active logical volume to 1000.00 MB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce data? [y/n]: y
  Reducing logical volume data to 1000.00 MB
  Logical volume data successfully resized
——————————————————————————————————
  resize2fs /dev/vg0/data  使上面增加的立即生效。
——————————————————————————————————
[root@station1 ~]# resize2fs /dev/vg0/data
resize2fs 1.39 (29-May-2006)
resize2fs: Bad magic number in super-block while trying to open /dev/vg0/data
Couldn't find valid filesystem superblock.
[root@station1 ~]# lvscan
  ACTIVE            '/dev/vg0/data' [1000.00 MB] inherit
  ACTIVE            '/dev/vg0/data128' [128.00 MB] inherit
——————————————————————————————————
 5)lvremove删除逻辑卷:
  格式:lvremove /dev/卷组名/逻辑卷名
——————————————————————————————————
[root@station1 ~]# lvremove /dev/vg0/data128
Do you really want to remove active logical volume "data128"? [y/n]: y
  Logical volume "data128" successfully removed
[root@station1 ~]# lvscan
  ACTIVE            '/dev/vg0/data' [1000.00 MB] inherit
——————————————————————————————————
五、LVM的快照:
 1)快照的创建:
 Lvcreate -l 64 -s -n databackup /dev/vg0/data
 -l 64:之前我们已经说过了这里是 64个扩展块,总容量=64*单扩展块大小
 -n databackup:生成的快照的名字。
  /dev/vg0/data:源逻辑卷名字。
————————————————————————————————————
[root@station1 ~]# lvscan
  ACTIVE            '/dev/vg0/data' [1000.00 MB] inherit
[root@station1 ~]# lvcreate -L 1000M -s -n databackup /dev/vg0/data
  Logical volume "databackup" created
[root@station1 ~]# lvscan
  ACTIVE   Original '/dev/vg0/data' [1000.00 MB] inherit
  ACTIVE   Snapshot '/dev/vg0/databackup' [1000.00 MB] inherit
————————————————————————————————————
 2)快照的删除:
  lvremove /dev/vg0/databackup
————————————————————————————————————
[root@station1 ~]# lvremove /dev/vg0/databackup
Do you really want to remove active logical volume "databackup"? [y/n]: y
  Logical volume "databackup" successfully removed
————————————————————————————————————
六、总结:
 创建逻辑卷的顺序:Linux分区---物理卷---卷组---逻辑卷
 删除逻辑卷的顺序:逻辑卷---卷组----物理卷---Linux分区