lvm實戰

先ping一下网关發表於2024-08-29

要求建立一個5G的邏輯卷

思路:
1. 物理的裝置
2. 將物理裝置做成物理卷
3. 建立卷組並將物理卷加入其中
4. 建立邏輯卷
5. 格式化邏輯卷
6. 掛載使用

1.準備好物理裝置,sdb、sdc硬碟

[root@uos01 ~]# lsblk /dev/sd[b,c]
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb    8:16   0  10G  0 disk 
sdc    8:32   0  10G  0 disk 

2.建立物理卷

[root@uos01 ~]# pvcreate /dev/sdb /dev/sdc
  Physical volume "/dev/sdb" successfully created.
  Physical volume "/dev/sdc" successfully created.

檢視物理卷

[root@uos01 ~]# pvs
  PV         VG  Fmt  Attr PSize   PFree 
  /dev/sda3  uos lvm2 a--  <23.50g     0 
  /dev/sdb       lvm2 ---   10.00g 10.00g
  /dev/sdc       lvm2 ---   10.00g 10.00g

檢視詳細資訊

[root@uos01 ~]# pvdisplay /dev/sdb
  "/dev/sdb" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb
  VG Name               
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               ujZ7N1-pZ2p-e6UK-k9vL-XspG-Gw0H-kA7Tmp
[root@uos01 ~]# pvdisplay /dev/sdc
  "/dev/sdc" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc
  VG Name               
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               RJridM-pc1m-OCG2-c5Vu-a2RH-NfdU-vQCI8k

3.建立卷組myvg01,加入物理卷PV

[root@uos01 ~]# vgcreate myvg01 /dev/sdb /dev/sdc
  Volume group "myvg01" successfully created

檢視卷組資訊

[root@uos01 ~]# vgs myvg01 
  VG     #PV #LV #SN Attr   VSize  VFree 
  myvg01   2   0   0 wz--n- 19.99g 19.99g
[root@uos01 ~]# vgdisplay myvg01 
  --- Volume group ---
  VG Name               myvg01
  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               19.99 GiB
  PE Size               4.00 MiB
  Total PE              5118
  Alloc PE / Size       0 / 0   
  Free  PE / Size       5118 / 19.99 GiB
  VG UUID               S4cFUe-cvEZ-RQZa-cSCS-PzA9-3ARB-gXZwoG
 

4.建立邏輯卷

引數解釋

-n:指定邏輯卷的名字
-L:指定邏輯卷的大小
-l:指定邏輯卷的大小
舉例:
-l 100            100個PE,每個PE大小預設4M,故邏輯卷大小為400M
-l 50%free        卷組剩餘空間的50%
-L 5G             建立大小為5GB的邏輯卷

建立邏輯卷mylv01

[root@uos01 ~]# lvcreate -n mylv01 -L 5G myvg01 
  Logical volume "mylv01" created.

檢視邏輯卷資訊

[root@uos01 ~]# lvs /dev/myvg01/mylv01 
  LV     VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv01 myvg01 -wi-a----- 5.00g    
[root@uos01 ~]# lvdisplay /dev/myvg01/mylv01 
  --- Logical volume ---
  LV Path                /dev/myvg01/mylv01
  LV Name                mylv01
  VG Name                myvg01
  LV UUID                8923dl-0uCR-Fox1-aJKY-c3IQ-d62K-EKjR7J
  LV Write Access        read/write
  LV Creation host, time uos01, 2024-08-28 16:09:14 +0800
  LV Status              available
  # open                 0
  LV Size                5.00 GiB
  Current LE             1280
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:3
   

可以在系統中的兩個地方,找到lv分割槽資訊

[root@uos01 ~]# ll /dev/mapper/myvg01-mylv01 
lrwxrwxrwx 1 root root 7  8月 28 16:09 /dev/mapper/myvg01-mylv01 -> ../dm-3
[root@uos01 ~]# ll /dev/myvg01/mylv01 
lrwxrwxrwx 1 root root 7  8月 28 16:09 /dev/myvg01/mylv01 -> ../dm-3

檢視卷組還剩餘多少空間

[root@uos01 ~]# vgs myvg01 
  VG     #PV #LV #SN Attr   VSize  VFree 
  myvg01   2   1   0 wz--n- 19.99g 14.99g

再建立一個200M的邏輯卷mylv02,每個LE預設4M,用-l 100 引數指定100個LE,因此是400M

[root@uos01 ~]# lvcreate -n mylv02 -l 100 myvg01
  Logical volume "mylv02" created.

檢視mylv02大小

[root@uos01 ~]# lvs /dev/myvg01/mylv02
  LV     VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv02 myvg01 -wi-a----- 400.00m     

檢視myvg01剩餘大小

[root@uos01 ~]# vgs myvg01 
  VG     #PV #LV #SN Attr   VSize  VFree 
  myvg01   2   2   0 wz--n- 19.99g 14.60g

建立卷組剩餘空間的50%,邏輯卷mylv03

[root@uos01 ~]# lvcreate -n mylv03 -l 50%free myvg01
  Logical volume "mylv03" created.

檢視mylv03

[root@uos01 ~]# lvs /dev/myvg01/mylv03
  LV     VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv03 myvg01 -wi-a----- 7.30g 

檢視此時vg剩餘

[root@uos01 ~]# vgs myvg01 
  VG     #PV #LV #SN Attr   VSize  VFree
  myvg01   2   3   0 wz--n- 19.99g 7.30g

5.格式化邏輯卷mylv01

[root@uos01 ~]# mkfs.xfs /dev/myvg01/mylv01
meta-data=/dev/myvg01/mylv01     isize=512    agcount=4, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=1310720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
Discarding blocks...Done.

6.掛載邏輯卷使用

[root@uos01 ~]# mkdir /funlyp-lv01
[root@uos01 ~]# mount /dev/myvg01/mylv01 /funlyp-lv01/
[root@uos01 ~]# df -Th | grep funlyp
/dev/mapper/myvg01-mylv01 xfs       5.0G   68M  5.0G    2% /funlyp-lv01
[root@uos01 ~]# 

寫入資料

[root@uos01 ~]# touch /funlyp-lv01/lvm實踐操作.log
[root@uos01 ~]# ls /funlyp-lv01/
lvm實踐操作.log


lvm動態擴容

任務:你現在覺得這個分割槽太小了,不夠用,如何擴容?

思路:
1.檢視該掛載點,對應的邏輯卷是哪一個/dev/mapper/vg01-lv01
2.檢視該邏輯卷所在的卷組,是否還有剩餘空間。
3.如果vg01空間不夠,就得先擴容卷組,再擴容邏輯卷
4.如果vg01空間足夠,直接擴容邏輯卷

直接邏輯卷擴容

1.檢視掛載點,屬於哪個邏輯卷

[root@uos01 ~]# df -Th | grep funlyp
/dev/mapper/myvg01-mylv01 xfs       5.0G   68M  5.0G    2% /funlyp-lv01

2.檢視邏輯卷大小

[root@uos01 ~]# lvs /dev/mapper/myvg01-mylv01
  LV     VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv01 myvg01 -wi-ao---- 5.00g   

3.檢視卷組空間

[root@uos01 ~]# vgs myvg01 
  VG     #PV #LV #SN Attr   VSize  VFree
  myvg01   2   3   0 wz--n- 19.99g 7.30g

4.卷組空間還剩7.3G,直接擴容邏輯卷

直接擴容加5G

[root@uos01 ~]# lvextend -L +5G /dev/myvg01/mylv01
  Size of logical volume myvg01/mylv01 changed from 5.00 GiB (1280 extents) to 10.00 GiB (2560 extents).
  Logical volume myvg01/mylv01 successfully resized.

檢視擴容結果

[root@uos01 ~]# df -Th | grep funlyp
/dev/mapper/myvg01-mylv01 xfs       5.0G   68M  5.0G    2% /funlyp-lv01

發現容量未更新
同步檔案系統,使其容量更新,只針對xfs

[root@uos01 ~]# xfs_growfs /dev/myvg01/mylv01
meta-data=/dev/mapper/myvg01-mylv01 isize=512    agcount=4, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=1310720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 1310720 to 2621440

再次檢視擴容結果

[root@uos01 ~]# df -Th | grep funlyp
/dev/mapper/myvg01-mylv01 xfs        10G  104M  9.9G    2% /funlyp-lv01

xfs檔案系統只支援增大分割槽空間的情況,不支援減小的情況(切記!!!!!)
硬要減小的話,只能在減小後將邏輯分割槽重新透過mkfs.xfs命令重新格式化才能掛載上,這樣的話這個邏輯分割槽上原來的資料就丟失了。
可以選擇用ext4檔案系統,支援擴縮容


還是覺得邏輯卷容量太小了,想要擴容到20G

先擴容卷組,再擴容邏輯卷

1.檢視邏輯卷大小和卷組空間

[root@uos01 ~]# lvs /dev/mapper/myvg01-mylv01
  LV     VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv01 myvg01 -wi-ao---- 10.00g   
[root@uos01 ~]# vgs myvg01 
  VG     #PV #LV #SN Attr   VSize  VFree
  myvg01   2   3   0 wz--n- 19.99g 2.30g


2.卷組空間已經不夠支援邏輯卷擴容到20G了,得先擴容卷組

新增新硬碟sde

[root@uos01 ~]# lsblk /dev/sde
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sde    8:64   0  10G  0 disk 

再把sde製作成物理卷pv

[root@uos01 ~]# pvcreate /dev/sde
WARNING: dos signature detected on /dev/sde at offset 510. Wipe it? [y/n]: y
  Wiping dos signature on /dev/sde.
  Physical volume "/dev/sde" successfully created.
[root@uos01 ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree 
  /dev/sda3  uos    lvm2 a--  <23.50g     0 
  /dev/sdb   myvg01 lvm2 a--  <10.00g     0 
  /dev/sdc   myvg01 lvm2 a--  <10.00g  2.30g
  /dev/sde          lvm2 ---   10.00g 10.00g

新增物理捲到卷組(卷組擴容)

[root@uos01 ~]# vgextend myvg01 /dev/sde
  Volume group "myvg01" successfully extended
[root@uos01 ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree  
  /dev/sda3  uos    lvm2 a--  <23.50g      0 
  /dev/sdb   myvg01 lvm2 a--  <10.00g      0 
  /dev/sdc   myvg01 lvm2 a--  <10.00g   2.30g
  /dev/sde   myvg01 lvm2 a--  <10.00g <10.00g
注意:
正常情況下,應該先將/dev/sde物理裝置建立為物理卷再加入到卷組中;如果直接加入卷組,系統會自動幫你將其做成物理卷。

檢視擴容後的卷組

[root@uos01 ~]# vgs myvg01 
  VG     #PV #LV #SN Attr   VSize   VFree  
  myvg01   3   3   0 wz--n- <29.99g <12.30g

3.擴容邏輯卷mylv01到20G

[root@uos01 ~]# lvextend -L +10G /dev/myvg01/mylv01
  Size of logical volume myvg01/mylv01 changed from 10.00 GiB (2560 extents) to 20.00 GiB (5120 extents).
  Logical volume myvg01/mylv01 successfully resized.

檢視擴容後的邏輯卷

[root@uos01 ~]# lvs /dev/myvg01/mylv01
  LV     VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv01 myvg01 -wi-ao---- 20.00g  

檢視掛載目錄的容量,發現還未更新

[root@uos01 ~]# df -h | grep funlyp
/dev/mapper/myvg01-mylv01   10G  104M  9.9G    2% /funlyp-lv01

手動更新

[root@uos01 ~]# xfs_growfs /dev/myvg01/mylv01
meta-data=/dev/mapper/myvg01-mylv01 isize=512    agcount=8, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=2621440, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 2621440 to 5242880

再次檢視已更新

[root@uos01 ~]# 
[root@uos01 ~]# df -h | grep funlyp
/dev/mapper/myvg01-mylv01   20G  176M   20G    1% /funlyp-lv01



刪除lvm

當你需要刪除邏輯卷,怎麼操作

思路:
需要提前把資料備份好,然後依次刪除
* 邏輯卷
* 卷組
* 物理卷

1.取消掛載

root@uos01 ~]# umount /funlyp-lv01 

2.刪除邏輯卷mylv01

[root@uos01 ~]# lvremove /dev/myvg01/mylv0
/dev/myvg01/mylv01  /dev/myvg01/mylv02  /dev/myvg01/mylv03  
[root@uos01 ~]# lvremove /dev/myvg01/mylv01
Do you really want to remove active logical volume myvg01/mylv01? [y/n]: y
  Logical volume "mylv01" successfully removed.
[root@uos01 ~]# 
[root@uos01 ~]# lvs /dev/myvg01
  LV     VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv02 myvg01 -wi-a----- 400.00m                                                    
  mylv03 myvg01 -wi-a-----   7.30g   

已成功刪除邏輯卷mylv01,此時檢視還有卷組myvg01上還存在兩個邏輯卷。

3.刪除卷組myvg01

直接刪除卷組,會提示先刪除卷組下的邏輯卷,然後再刪除卷組。

[root@uos01 ~]# vgremove myvg01 
Do you really want to remove volume group "myvg01" containing 2 logical volumes? [y/n]: y
Do you really want to remove active logical volume myvg01/mylv02? [y/n]: y
  Logical volume "mylv02" successfully removed.
Do you really want to remove active logical volume myvg01/mylv03? [y/n]: y
  Logical volume "mylv03" successfully removed.
  Volume group "myvg01" successfully removed
[root@uos01 ~]# 

4.刪除物理卷

檢視本機上的物理卷pv

[root@uos01 ~]# pvs
  PV         VG  Fmt  Attr PSize   PFree 
  /dev/sda3  uos lvm2 a--  <23.50g     0 
  /dev/sdb       lvm2 ---   10.00g 10.00g
  /dev/sdc       lvm2 ---   10.00g 10.00g
  /dev/sde       lvm2 ---   10.00g 10.00g

刪除物理卷/dev/sdb /dev/sdc /dev/sde

[root@uos01 ~]# pvremove /dev/sdb /dev/sdc /dev/sde
  Labels on physical volume "/dev/sdb" successfully wiped.
  Labels on physical volume "/dev/sdc" successfully wiped.
  Labels on physical volume "/dev/sde" successfully wiped.

檢視lvm相關資訊,是否還有殘留

[root@uos01 ~]# lvs
  LV   VG  Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root uos -wi-ao---- <17.50g                                                    
  swap uos -wi-ao----   4.00g                                                    
  var  uos -wi-ao----   2.00g                                                    
[root@uos01 ~]# 
[root@uos01 ~]# pvs
  PV         VG  Fmt  Attr PSize   PFree
  /dev/sda3  uos lvm2 a--  <23.50g    0 
[root@uos01 ~]# 
[root@uos01 ~]# vgs
  VG  #PV #LV #SN Attr   VSize   VFree
  uos   1   3   0 wz--n- <23.50g    0 
[root@uos01 ~]#