LVM管理

1 名词解释

磁盘或者分区可以创建成pv(物理卷),一个或多个pv可以组成vg(卷组),vg可以划分成lv(逻辑卷)提供使用
lv分为thick 和 thin类型,thick会立即从vg分配所有空间,thin类型的实际占用空间会根据数据量增加而增加

2 LVM资源管理

2.1 查看系统disk

root@vtela:~# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   20G  0 disk 
└─sda1   8:1    0   20G  0 part /
sdb      8:16   0    5G  0 disk 
sdc      8:32   0    3G  0 disk 
sr0     11:0    1 1024M  0 rom  

sdb和sdc可以用来做pv

2.2 PV

2.2.1 创建

创建pv

root@vtela:~# pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created.

查看pv(有两个命令可以查看,剩下的内容以简单的pvs,lvs等展示)

root@vtela:~# pvs
  PV         VG Fmt  Attr PSize PFree
  /dev/sdb      lvm2 ---  5.00g 5.00g
root@vtela:~# pvdisplay
  "/dev/sdb" is a new physical volume of "5.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb
  VG Name               
  PV Size               5.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               OZ7y5s-VlqF-dbxn-k0yp-R1Fm-BKNW-EmBM5R

2.2.2 删除

root@vtela:~# pvremove /dev/sdb
  Labels on physical volume "/dev/sdb" successfully wiped.
root@vtela:~# pvs
root@vtela:~# 

2.3 VG

2.3.1 创建

2.3.1.1 直接使用disk或part创建

不用提前创建pv,直接创建vg,vg0是名字,创建vg成功之后可以看到提示创建了pv和vg

root@vtela:~# pvs
root@vtela:~# 
root@vtela:~# vgcreate vg0 /dev/sdb
  Physical volume "/dev/sdb" successfully created.
  Volume group "vg0" successfully created
root@vtela:~# pvs
  PV         VG  Fmt  Attr PSize  PFree 
  /dev/sdb   vg0 lvm2 a--  <5.00g <5.00g
root@vtela:~# vgs
  VG  #PV #LV #SN Attr   VSize  VFree 
  vg0   1   0   0 wz--n- <5.00g <5.00g
2.3.1.2 使用pv创建
root@vtela:~# pvs
  PV         VG Fmt  Attr PSize PFree
  /dev/sdb      lvm2 ---  5.00g 5.00g
root@vtela:~# vgcreate vg0 /dev/sdb
  Volume group "vg0" successfully created
2.3.1.3 一次性使用多个设备创建
root@vtela:~# vgcreate vg1 /dev/sdb /dev/sdc
  Volume group "vg1" successfully created
root@vtela:~# vgs
  VG  #PV #LV #SN Attr   VSize VFree
  vg1   2   0   0 wz--n- 7.99g 7.99g
root@vtela:~# pvs
  PV         VG  Fmt  Attr PSize  PFree 
  /dev/sdb   vg1 lvm2 a--  <5.00g <5.00g
  /dev/sdc   vg1 lvm2 a--  <3.00g <3.00g

2.3.2 扩展

加入新的pv增大容量,也可直接使用未创建pv的设备来扩展

root@vtela:~# vgs
  VG  #PV #LV #SN Attr   VSize  VFree 
  vg0   1   0   0 wz--n- <5.00g <5.00g
root@vtela:~#  
root@vtela:~# vgextend vg0 /dev/sdc
  Physical volume "/dev/sdc" successfully created.
  Volume group "vg0" successfully extended
root@vtela:~# 
root@vtela:~# 
root@vtela:~# vgs
  VG  #PV #LV #SN Attr   VSize VFree
  vg0   2   0   0 wz--n- 7.99g 7.99g
root@vtela:~# 
root@vtela:~# pvs
  PV         VG  Fmt  Attr PSize  PFree 
  /dev/sdb   vg0 lvm2 a--  <5.00g <5.00g
  /dev/sdc   vg0 lvm2 a--  <3.00g <3.00g
root@vtela:~# 

2.3.3 缩小

即把某个pv从vg里面切割掉

root@vtela:~# vgs 
  VG  #PV #LV #SN Attr   VSize VFree
  vg0   2   0   0 wz--n- 7.99g 7.99g
root@vtela:~# 
root@vtela:~# vgreduce vg0 /dev/sdc
  Removed "/dev/sdc" from volume group "vg0"
root@vtela:~# 
root@vtela:~# vgs
  VG  #PV #LV #SN Attr   VSize  VFree 
  vg0   1   0   0 wz--n- <5.00g <5.00g
root@vtela:~# 
root@vtela:~# 

2.3.4 重命名

root@vtela:~# vgrename vg0 vg1
  Volume group "vg0" successfully renamed to "vg1"
root@vtela:~# vgs
  VG  #PV #LV #SN Attr   VSize  VFree 
  vg1   1   0   0 wz--n- <5.00g <5.00g

2.4 LV

2.4.1 Thick 类型

2.4.1.1 创建

不指定lv name,会分配名字

root@vtela:~# lvcreate -L 1g vg1
  Logical volume "lvol0" created.

指定lv name

root@vtela:~# lvcreate -L 1g -n lv1 vg1     
  Logical volume "lv1" created.
  
root@vtela:~# lvs
  LV    VG  Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv1   vg1 -wi-a----- 1.00g                                                    
  lvol0 vg1 -wi-a----- 1.00g  
2.4.1.2 resize

可以增大或减少容量,注意需要指定完整的lv路径,因为不同的vg可以创建名字相同的lv

root@vtela:~# lvresize -L +1g /dev/vg1/lv1
  Size of logical volume vg1/lv1 changed from 1.00 GiB (256 extents) to 2.00 GiB (512 extents).
  Logical volume vg1/lv1 successfully resized.
root@vtela:~# lvs
  LV    VG  Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv1   vg1 -wi-a-----  2.00g                                                    
  lv2   vg1 -wi-a----- 40.00m                                                    
  lv3   vg1 -wi-a----- 40.00m                                                    
  lvol0 vg1 -wi-a-----  1.00g                                                    
root@vtela:~# lvresize -L -1g /dev/vg1/lv1  
  WARNING: Reducing active logical volume to 1.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg1/lv1? [y/n]: y
  Size of logical volume vg1/lv1 changed from 2.00 GiB (512 extents) to 1.00 GiB (256 extents).
  Logical volume vg1/lv1 successfully resized.
root@vtela:~# lvs
  LV    VG  Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv1   vg1 -wi-a-----  1.00g                                                    
  lv2   vg1 -wi-a----- 40.00m                                                    
  lv3   vg1 -wi-a----- 40.00m                                                    
  lvol0 vg1 -wi-a-----  1.00g                                                    
root@vtela:~# 
2.4.1.3 删除
root@vtela:~# lvremove /dev/vg1/lv1
Do you really want to remove and DISCARD active logical volume vg1/lv1? [y/n]: y
  Logical volume "lv1" successfully removed

加上-f是强制删除

root@vtela:~# lvremove -f /dev/vg1/lv2
  Logical volume "lv2" successfully removed
2.4.1.4 重命名

lv4是新名字

root@vtela:~# lvrename vg1 lv3 lv4
  Renamed "lv3" to "lv4" in volume group "vg1"

2.4.2 Thin类型

创建thin lv需要先创建一个thin pool,这个thin pool也是LINSTOR lvmthin 类型的存储池的backend storage

2.4.2.1 创建thinpool

thinpool其实也是个特殊的lv,不能超过vg的容量

root@vtela:~# lvcreate -T -L 7g vg1/thinpool1  
  Using default stripesize 64.00 KiB.
  Thin pool volume with chunk size 64.00 KiB can address at most 15.81 TiB of data.
  Logical volume "thinpool1" created.
root@vtela:~# lvs
  LV        VG  Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  thinpool1 vg1 twi-a-tz-- 7.00g             0.00   10.74                           
root@vtela:~# vgs
  VG  #PV #LV #SN Attr   VSize VFree   
  vg1   2   1   0 wz--n- 7.99g 1000.00m
2.4.2.2 创建thin lv

thin lv可以超过thinpool的容量,后续可以通过扩展vg,thinpool来扩展thin lv真正能使用的空间

root@vtela:~# lvcreate -V 100G -n thinlv1 -T vg1/thinpool1
  Using default stripesize 64.00 KiB.
  WARNING: Sum of all thin volume sizes (100.00 GiB) exceeds the size of thin pool vg1/thinpool1 and the size of whole volume group (7.99 GiB).
  WARNING: You have not turned on protection against thin pools running out of space.
  WARNING: Set activation/thin_pool_autoextend_threshold below 100 to trigger automatic extension of thin pools before they get full.
  Logical volume "thinlv1" created.
root@vtela:~# lvs
  LV        VG  Attr       LSize   Pool      Origin Data%  Meta%  Move Log Cpy%Sync Convert
  thinlv1   vg1 Vwi-a-tz-- 100.00g thinpool1        0.00                                   
  thinpool1 vg1 twi-aotz--   7.00g                  0.00   10.79                           
root@vtela:~# 
2.4.2.3 resize thinpool

只能扩展不能减少

root@vtela:~# lvresize -L -1g /dev/vg1/thinpool1 
  Thin pool volumes vg1/thinpool1_tdata cannot be reduced in size yet.
root@vtela:~# lvresize -L +500m /dev/vg1/thinpool1    
  WARNING: Sum of all thin volume sizes (100.00 GiB) exceeds the size of thin pools and the size of whole volume group (7.99 GiB).
  WARNING: You have not turned on protection against thin pools running out of space.
  WARNING: Set activation/thin_pool_autoextend_threshold below 100 to trigger automatic extension of thin pools before they get full.
  Size of logical volume vg1/thinpool1_tdata changed from 7.00 GiB (1792 extents) to <7.49 GiB (1917 extents).
  Logical volume vg1/thinpool1_tdata successfully resized.
root@vtela:~# lvs
  LV        VG  Attr       LSize   Pool      Origin Data%  Meta%  Move Log Cpy%Sync Convert
  thinlv1   vg1 Vwi-a-tz-- 100.00g thinpool1        0.00                                   
  thinpool1 vg1 twi-aotz--  <7.49g                  0.00   10.79                
2.4.2.4 resize thinlv
root@vtela:~# lvresize -L +500m /dev/vg1/thinlv1   
  WARNING: Sum of all thin volume sizes (<100.49 GiB) exceeds the size of thin pool vg1/thinpool1 and the size of whole volume group (7.99 GiB).
  WARNING: You have not turned on protection against thin pools running out of space.
  WARNING: Set activation/thin_pool_autoextend_threshold below 100 to trigger automatic extension of thin pools before they get full.
  Size of logical volume vg1/thinlv1 changed from 100.00 GiB (25600 extents) to <100.49 GiB (25725 extents).
  Logical volume vg1/thinlv1 successfully resized.
root@vtela:~# lvs
  LV        VG  Attr       LSize    Pool      Origin Data%  Meta%  Move Log Cpy%Sync Convert
  thinlv1   vg1 Vwi-a-tz-- <100.49g thinpool1        0.00                                   
  thinpool1 vg1 twi-aotz--   <7.49g                  0.00   10.79                           
root@vtela:~# lvresize -L -500m /dev/vg1/thinlv1  
  WARNING: Reducing active logical volume to 100.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg1/thinlv1? [y/n]: y
  Size of logical volume vg1/thinlv1 changed from <100.49 GiB (25725 extents) to 100.00 GiB (25600 extents).
  Logical volume vg1/thinlv1 successfully resized.
root@vtela:~# lvs
  LV        VG  Attr       LSize   Pool      Origin Data%  Meta%  Move Log Cpy%Sync Convert
  thinlv1   vg1 Vwi-a-tz-- 100.00g thinpool1        0.00                                   
  thinpool1 vg1 twi-aotz--  <7.49g                  0.00   10.79                           
root@vtela:~# 
2.4.2.5 删除 & 重命名

同thick类型

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 Logical Volume Manager (LVM) 提供了一种灵活、高效的方式来管理存储资源,使得存储...
    yunbeeOS阅读 91评论 0 0
  • 一、设备文件 在 Linux 操作系统中,设备文件是一种特殊类型的文件。这些文件绝大多数位于 /dev 目录下,用...
    rollingstarky阅读 1,312评论 0 1
  • lvm相关概念lvm的重点在于可以弹性调节文件系统的容量。lvm的做法是将几个物理的分区或磁盘通过软件组合成一块看...
    ivan_cq阅读 961评论 0 0
  • 一、LVM的基本概念 对于生产环境下的服务器来说,如果存储数据的分区磁盘空间不够了怎么办? 只能换一个更大的磁盘。...
    jan29阅读 576评论 0 1
  • 一、分区 可使用fdisk、gdisk创建新分区,此处拿vdb做示范 进入交互界面后按m看到菜单,n:创建分区 p...
    yushui1995阅读 353评论 0 0