首页 > 代码库 > lVM逻辑卷管理相关总结

lVM逻辑卷管理相关总结

创建、管理LVM:

创建分区

Disk /dev/xvdc: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xd199e24a

    Device Boot      Start         End      Blocks   Id  System
/dev/xvdc1               1          65      522081   8e  Linux LVM #调整分区类型为 Linux LVM
/dev/xvdc2              66         104      313267+  8e  Linux LVM
/dev/xvdc3             105         181      618502+  8e  Linux LVM

创建PV:

pvcreate  /path/to/device 创建物理卷

[root@VM_168_102_centos ~]# pvcreate /dev/xvdc{1,2,3}
  Writing physical volume data to disk "/dev/xvdc1"
  Physical volume "/dev/xvdc1" successfully created
  Writing physical volume data to disk "/dev/xvdc2"
  Physical volume "/dev/xvdc2" successfully created
  Writing physical volume data to disk "/dev/xvdc3"
  Physical volume "/dev/xvdc3" successfully created

pvs [ /path/to/pv_device ] 查看现有物理卷

[root@VM_168_102_centos ~]# pvs
  PV         VG   Fmt  Attr PSize   PFree  
  /dev/xvdc1      lvm2 a--  509.84m 509.84m
  /dev/xvdc2      lvm2 a--  305.93m 305.93m
  /dev/xvdc3      lvm2 a--  604.01m 604.01m
[root@VM_168_102_centos ~]# pvs /dev/xvdc1
  PV         VG   Fmt  Attr PSize   PFree  
  /dev/xvdc1      lvm2 a--  509.84m 509.84m

pvdisplay [ /path/to/pv_device ] 显示物理卷属性

[root@VM_168_102_centos ~]# pvdisplay /dev/xvdc1
  "/dev/xvdc1" is a new physical volume of "509.84 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/xvdc1  #pv名字
  VG Name                           #所属VG组       
  PV Size               509.84 MiB  #PV大小
  Allocatable           NO
  PE Size               0 #pe大小,默认4   
  Total PE              0 #pe总计数量  
  Free PE               0 #空闲pe数量
  Allocated PE          0 #被分配的PE数量 
  PV UUID               sCID3E-GNNl-DoeW-BaLW-kuYj-fskV-RhLx8g

创建VG卷组:

vgcreate [-s SIZE] vg_name /path/to/pv_device

-s:指定PE大小,默认4MB

[root@VM_168_102_centos ~]# vgcreate -s 8 myvg /dev/xvdc{1,2}
  Volume group "myvg" successfully created

vgs [ /path/to/vg_device ] 查看现有卷组

[root@VM_168_102_centos ~]# vgs
  VG   #PV #LV #SN Attr   VSize   VFree  
  myvg   2   0   0 wz--n- 808.00m 808.00m

vgdisplay [ /path/to/vg_device ] 查看卷组属性

[root@VM_168_102_centos ~]# vgdisplay 
  --- Volume group ---
  VG Name               myvg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               808.00 MiB
  PE Size               8.00 MiB
  Total PE              101
  Alloc PE / Size       0 / 0   
  Free  PE / Size       101 / 808.00 MiB
  VG UUID               R35yt2-S4dp-M9oY-4O1d-1CqX-4CQd-t0TpB9

vgrename vg_name new_name 修改卷组名称

[root@VM_168_102_centos ~]# vgrename myvg testvg
  Volume group "myvg" successfully renamed to "testvg"
[root@VM_168_102_centos ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree  
  testvg   2   0   0 wz--n- 808.00m 808.00m

创建逻辑卷:

lvcreate –L size –n name  vg_name 创建逻辑卷

[root@VM_168_102_centos ~]# lvcreate -L 500M -n mylv /dev/myvg
  Rounding up size to full physical extent 504.00 MiB
  Logical volume "mylv" created

lvs [ /path/to/lv_device ] 查看现有逻辑卷

[root@VM_168_102_centos ~]# lvs
  LV   VG   Attr     LSize   Pool Origin Data%  Move Log Copy%  Convert
  mylv myvg -wi-a--- 504.00m

lvdisplay [ /path/to/lv_device ] 查看逻辑卷属性

/path/to/lv_device[root@VM_168_102_centos ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/myvg/mylv
  LV Name                mylv
  VG Name                myvg
  LV UUID                nnelF1-sSxC-0A1l-IzLf-YqwL-djsy-lMeiTH
  LV Write Access        read/write
  LV Creation host, time VM_168_102_centos, 2014-08-24 16:12:08 +0800
  LV Status              available
  # open                 0
  LV Size                504.00 MiB
  Current LE             63
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:7

讲mylv2逻辑据进行格式化挂载:

[root@VM_168_102_centos ~]# mke2fs -t ext4 /dev/myvg/mylv
mke2fs 1.41.12 (17-May-2010)
...省略
This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@VM_168_102_centos ~]# mount /dev/myvg/mylv /tmp/wanghan
[root@VM_168_102_centos ~]# mount 
/dev/xvda1 on / type ext3 (rw,noatime,acl,user_xattr)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,mode=0620,gid=5)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/mapper/myvg-mylv on /tmp/wanghan type ext4 (rw)

将myvg剩余的空间分配200M给mylv2:

[root@VM_168_102_centos ~]# lvcreate -L 200M -n mylv2 /dev/myvg
  Logical volume "mylv2" created
[root@VM_168_102_centos ~]# lvs
  LV    VG   Attr     LSize   Pool Origin Data%  Move Log Copy%  Convert
  mylv  myvg -wi-a--- 504.00m                                           
  mylv2 myvg -wi-a--- 200.00m                                           
[root@VM_168_102_centos ~]# vgs
  VG   #PV #LV #SN Attr   VSize   VFree  
  myvg   2   2   0 wz--n- 808.00m 104.00m

移除mylv2逻辑卷:

lvremove [ /path/to/lv_device ] 移除指定逻辑卷

[root@VM_168_102_centos ~]# lvremove /dev/myvg/mylv2
Do you really want to remove active logical volume mylv2? [y/n]: y
  Logical volume "mylv2" successfully removed
[root@VM_168_102_centos ~]# lvs
  LV   VG   Attr     LSize   Pool Origin Data%  Move Log Copy%  Convert
  mylv myvg -wi-a--- 504.00m

扩展卷组:

准备好要添加的物理卷

[root@VM_168_102_centos ~]# pvs
  PV         VG   Fmt  Attr PSize   PFree  
  /dev/xvdc1 myvg lvm2 a--  504.00m      0 
  /dev/xvdc2 myvg lvm2 a--  304.00m 304.00m
  /dev/xvdc3      lvm2 a--  604.01m 604.01m

向卷组中添加物理卷

vgextend vg_name /path/to/pv_device

[root@VM_168_102_centos ~]# vgextend /dev/myvg /dev/xvdc3
  Volume group "myvg" successfully extended
[root@VM_168_102_centos ~]# vgs
  VG   #PV #LV #SN Attr   VSize VFree  
  myvg   3   1   0 wz--n- 1.38g 904.00m
[root@VM_168_102_centos ~]# pvs
  PV         VG   Fmt  Attr PSize   PFree  
  /dev/xvdc1 myvg lvm2 a--  504.00m      0 
  /dev/xvdc2 myvg lvm2 a--  304.00m 304.00m
  /dev/xvdc3 myvg lvm2 a--  600.00m 600.00m

缩减卷组

1、要移除的物理卷的总空间大小,要小于当前VG可用空间的大小

2、将要移除的PV上的数据移动至其他pv

pvmove 可以将一个PV上的数据迁移到新的PV上

[root@VM_168_102_centos ~]# pvmove /dev/xvdc2
  No data to move for myvg

3、将要指定的PV从VG中移除

vgreduce 从卷组中删除物理卷

[root@VM_168_102_centos ~]# vgreduce /dev/myvg /dev/xvdc2
  Removed "/dev/xvdc2" from volume group "myvg"
[root@VM_168_102_centos ~]# pvs
  PV         VG   Fmt  Attr PSize   PFree  
  /dev/xvdc1 myvg lvm2 a--  504.00m      0 
  /dev/xvdc2      lvm2 a--  305.93m 305.93m
  /dev/xvdc3 myvg lvm2 a--  600.00m 600.00m
[root@VM_168_102_centos ~]# vgs
  VG   #PV #LV #SN Attr   VSize VFree  
  myvg   2   1   0 wz--n- 1.08g 600.00m

扩展逻辑卷:

1、确定要扩展的逻辑卷所属的卷组有足够的剩余空间

2、扩展物理边界(物理边界:我们对一个磁盘进行分区的分区边界)

lvextend 扩展逻辑卷空间

[root@VM_168_102_centos ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/xvda1            7.9G  2.2G  5.3G  30% /
/dev/mapper/myvg-mylv
                      489M   31M  433M   7% /tmp/wanghan
[root@VM_168_102_centos ~]# lvextend -L 700M /dev/myvg/mylv
  Rounding size to boundary between physical extents: 704.00 MiB
  Extending logical volume mylv to 704.00 MiB
  Logical volume mylv successfully resized
[root@VM_168_102_centos ~]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/xvda1            7.9G  2.2G  5.3G  30% /
/dev/mapper/myvg-mylv
                      489M   31M  433M   7% /tmp/wanghan

3、扩展逻辑边界(逻辑边界:我们格式化分区,建立文件系统后形成的分区边界)

resize2fs:文件系统大小调整工具

[root@VM_168_102_centos ~]# resize2fs /dev/myvg/mylv
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/myvg/mylv is mounted on /tmp/wanghan; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 3
Performing an on-line resize of /dev/myvg/mylv to 720896 (1k) blocks.
The filesystem on /dev/myvg/mylv is now 720896 blocks long.

[root@VM_168_102_centos ~]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/xvda1            7.9G  2.2G  5.3G  30% /
/dev/mapper/myvg-mylv
                      682M   31M  616M   5% /tmp/wanghan
[root@VM_168_102_centos ~]# lvs
  LV   VG   Attr     LSize   Pool Origin Data%  Move Log Copy%  Convert
  mylv myvg -wi-ao-- 704.00m

缩减逻辑卷(有风险,不推荐):

1、卸载卷,并执行强制检测

[root@VM_168_102_centos ~]# umount /dev/myvg/mylv
[root@VM_168_102_centos ~]# e2fsck -f /dev/myvg/mylv
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/myvg/mylv: 1307/180224 files (0.2% non-contiguous), 54323/720896 blocks

2、先缩减逻辑边界

[root@VM_168_102_centos ~]# resize2fs /dev/myvg/mylv 400M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/myvg/mylv to 409600 (1k) blocks.
The filesystem on /dev/myvg/mylv is now 409600 blocks long.

3、再缩减物理边界

[root@VM_168_102_centos ~]# lvreduce -L 400M /dev/myvg/mylv
  WARNING: Reducing active logical volume to 400.00 MiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce mylv? [y/n]: y
  Reducing logical volume mylv to 400.00 MiB
  Logical volume mylv successfully resized
[root@VM_168_102_centos ~]# lvs
  LV   VG   Attr     LSize   Pool Origin Data%  Move Log Copy%  Convert
  mylv myvg -wi-a--- 400.00m

快照卷:

1、生命周期为整个数据时长,在这段时长内,数据的增长量不能超出快照卷大小

2、快照卷应该是只读的

3、跟原卷在同一卷组内
[root@VM_168_102_centos ~]# lvcreate -s -L 50M -n snap-mylv -p r /dev/myvg/mylv
  Rounding up size to full physical extent 56.00 MiB
  Logical volume "snap-mylv" created
[root@VM_168_102_centos ~]# lvs
  LV        VG   Attr     LSize   Pool Origin Data%  Move Log Copy%  Convert
  mylv      myvg owi-a-s- 400.00m                                           
  snap-mylv myvg sri-a-s-  56.00m      mylv     0.00
 

lVM逻辑卷管理相关总结