1. 程式人生 > >邏輯卷lvm的管理(邏輯卷的建立 、掛載 、擴容與縮容、指定pe的大小 、邏輯卷快照的製作 、物理分割槽的徹底刪除)

邏輯卷lvm的管理(邏輯卷的建立 、掛載 、擴容與縮容、指定pe的大小 、邏輯卷快照的製作 、物理分割槽的徹底刪除)

邏輯卷的管理

概述:

物理分割槽:  用來製作物理卷
物理卷:    pv  用來組成物理卷組 ;包含整數個 pe(物理拓展)  
物理卷組 :  vg  用來製作邏輯卷
邏輯卷:    lvm 它的作用是實現硬碟的無限分割槽與擴容

1.邏輯卷的建立

(1).建立物理分割槽

##1.新建分割槽
[[email protected] ~]# fdisk /dev/vdb

在這裡插入圖片描述
在這裡插入圖片描述

##2.更改分割槽型別為LVM
[[email protected] ~]# fdisk /dev/vdb

在這裡插入圖片描述
在這裡插入圖片描述

##同步分割槽表
[[email protected]
~]# partprobe
##3.格式化為xfs型別
[[email protected] ~]# mkfs.xfs /dev/vdb1 

在這裡插入圖片描述

##4.建立載點
[[email protected] ~]# mkdir /weixindata
##5.掛載
[[email protected] ~]# mount /dev/vdb1 /weixindata/
[[email protected] ~]# df

在這裡插入圖片描述

##檢視掛載裝置的大小
[[email protected] ~]# df -H

在這裡插入圖片描述

監控:

##檢視物理卷
[[email protected]
~]# pvs ##檢視物理卷組 [[email protected] ~]# vgs No volume groups found ##檢視邏輯卷 [[email protected] ~]# lvs No volume groups found [[email protected] ~]# pvs;vgs;lvs No volume groups found No volume groups found [[email protected] ~]# pvs;echo ++++++++++;vgs;echo ++++++++++;lvs;echo ++++++++++;df -h /weixindata

在這裡插入圖片描述

##監控
[[email protected] ~]# watch -n 1 'pvs;echo ++++++++++;vgs;echo ++++++++++;lvs;echo ++++++++++;df -h /weixindata'

在這裡插入圖片描述

##必須先解除安裝裝置,才能建立物理卷
[[email protected] ~]# pvcreate /dev/vdb1
  Can't open /dev/vdb1 exclusively.  Mounted filesystem?
[[email protected] ~]# umount /dev/vdb1

(2).建立物理卷;將物理分割槽處理成物理卷

[[email protected] ~]# pvcreate /dev/vdb1

在這裡插入圖片描述
(3).建立物理卷組;將物理卷做成物理卷組

[[email protected][[email protected] ~]# vgcreate weixingroup /dev/vdb1

在這裡插入圖片描述
(4).建立邏輯卷;將物理卷組(weixingroup) 劃分為邏輯卷(weixindata)-L指定大小

[[email protected] ~]# lvcreate -L 60M -n weixindata weixingroup

在這裡插入圖片描述
(5).掛載邏輯卷裝置

##1.格式化邏輯卷裝置
[[email protected] ~]# mkfs.xfs /dev/weixingroup/weixindata 
##2.掛載邏輯卷裝置
[[email protected] ~]# mount /dev/weixingroup/weixindata /weixindata/

在這裡插入圖片描述
@@發現顯示的掛載裝置與邏輯卷名字有寫不同;但實際上它們都是dm-0的快捷方式,即表示的是同一個裝置

[[email protected] ~]# ll /dev/mapper/weixingroup-weixindata
lrwxrwxrwx. 1 root root 7 Nov 10 11:25 /dev/mapper/weixingroup-weixindata -> ../dm-0
[[email protected] ~]# ll /dev/weixingroup/weixindata
lrwxrwxrwx. 1 root root 7 Nov 10 11:25 /dev/weixingroup/weixindata -> ../dm-0

在這裡插入圖片描述
2.邏輯卷的管理

(1).裝置的擴容與縮容

監控:
[[email protected] ~]# watch -n 1 "pvs;echo ++++++++++;vgs;echo ++++++++++;lvs;echo ++++++++++;df -h /weixindata"

在這裡插入圖片描述
xfs型別:

xfs型別的裝置只能擴容不能縮容!!!

1)VG容量夠時

直接擴大邏輯卷裝置,再擴充套件系統檔案即可
##1.擴大邏輯卷裝置;-L指定大小,將邏輯卷擴大到80M
[[email protected] ~]# lvextend -L 80M /dev/weixingroup/weixindata 

在這裡插入圖片描述

##2.擴充套件檔案系統
[[email protected] ~]# xfs_growfs /dev/weixingroup/weixindata 

在這裡插入圖片描述
由於VG容量只有96M,所以不能實現擴容到比96M還大

##擴大邏輯卷裝置
[[email protected] ~]# lvextend -L 96M /dev/weixingroup/weixindata 
##擴充套件檔案系統
[[email protected] ~]# xfs_growfs /dev/weixingroup/weixindata 

在這裡插入圖片描述

##將邏輯卷裝置擴大到97M
[[email protected] ~]# lvextend -L 97M /dev/weixingroup/weixindata 

在這裡插入圖片描述

2)VG容量不夠時

首先需要先新增物理分割槽,再建立物理卷,然後將新建立物理卷加入到原有的物理卷組中,最後擴大邏輯卷和檔案系統
##1.新增物理分割槽
由於我這裡一開始便添加了兩個物理分割槽/dev/vdb1和/dev/vdb2,故可以無需再新增裝置
[[email protected] ~]# fdisk -l

在這裡插入圖片描述

##2.建立物理卷
[[email protected] ~]# pvcreate /dev/vdb2

在這裡插入圖片描述

##3.將剛建立的物理卷新增到已有的物理卷組wexingroup中
[[email protected] ~]# vgextend weixingroup /dev/vdb2

在這裡插入圖片描述

##4.擴大邏輯卷裝置
[[email protected] ~]# lvextend -L 150M /dev/weixingroup/weixindata 
##5.擴充套件檔案系統
[[email protected] ~]# xfs_growfs /dev/weixingroup/weixindata 

在這裡插入圖片描述
ext4型別:

ext4型別的裝置不僅可以擴容還可以縮容!!!

1)裝置的擴容

##1.解除安裝
[[email protected] ~]# umount /weixindata/
##2.格式化為ext4型別
[[email protected] ~]# mkfs.ext4 /dev/weixingroup/weixindata 

在這裡插入圖片描述

##擴容時為熱拉伸,即可以直接對掛載裝置進行拉伸
[[email protected] ~]# mount /dev/weixingroup/weixindata /weixindata/
##4.擴大邏輯卷裝置
[[email protected] ~]# lvextend -L 170M /dev/weixingroup/weixindata 

在這裡插入圖片描述

##5.擴充套件檔案系統(注意:與xfs型別有所不同)
[[email protected] ~]# resize2fs /dev/weixingroup/weixindata

在這裡插入圖片描述
2)裝置的縮容

必須先解除安裝裝置才能縮容,並且必須先縮小檔案系統(軟體)再縮小邏輯卷裝置(硬體)!!!
##1.解除安裝
[[email protected] ~]# umount /weixindata/
##2.掃描檔案系統
[[email protected] ~]# e2fsck -f /dev/weixingroup/weixindata 

在這裡插入圖片描述

##3.縮小檔案系統
[[email protected] ~]# resize2fs /dev/weixingroup/weixindata 50M
##4.掛載
[[email protected] ~]# mount /dev/weixingroup/weixindata /weixindata/

在這裡插入圖片描述

##5.縮小邏輯卷裝置
[[email protected] ~]# lvreduce -L 50M /dev/weixingroup/weixindata 

在這裡插入圖片描述
(2).物理卷的取出

此操作不區分檔案系統型別,即不管是xfs型別還是ext4型別,均按照以下操作將物理卷取出
如果裝置上有儲存資料,那麼要想取出物理卷,必須先將裝置上的資料遷移到空閒的裝置上!!!

在這裡插入圖片描述

##1.資料的遷移;將/dev/vdb1裝置上的資料遷移到/dev/vdb2裝置中(如果裝置上沒有儲存資料,就不用執行此操作)
[[email protected] ~]# pvmove /dev/vdb1 /dev/vdb2

在這裡插入圖片描述

##2.取出物理卷組
[[email protected] ~]# vgreduce weixingroup /dev/vdb1

在這裡插入圖片描述

##3.取出物理卷
[[email protected] ~]# pvremove /dev/vdb1

在這裡插入圖片描述
(3).如何指pe的大小

所謂pe是指:
[[email protected] ~]# pvs
  PV         VG          Fmt  Attr PSize  PFree 
  /dev/vdb2  weixingroup lvm2 a--  96.00m 44.00m
[[email protected] ~]# vgs
  VG          #PV #LV #SN Attr   VSize  VFree 
  weixingroup   1   1   0 wz--n- 96.00m 44.00m
[[email protected] ~]# lvs
  LV         VG          Attr       LSize  Pool Origin Data%  Move Log Cpy%Sync Convert
  weixindata weixingroup -wi-ao---- 52.00m                                             
[[email protected] ~]# df

在這裡插入圖片描述

[[email protected] ~]# umount /weixindata/
[[email protected] ~]# lvremove /dev/weixingroup/weixindata 
[[email protected] ~]# vgremove weixingroup 
[[email protected] ~]# lvs
  No volume groups found
[[email protected] ~]# vgs
  No volume groups found

在這裡插入圖片描述
@1.預設建立的物理卷中一個pe的大小為4MIB

[[email protected] ~]# pvs
  PV         VG   Fmt  Attr PSize   PFree  
  /dev/vdb2       lvm2 a--  100.00m 100.00m
[[email protected] ~]# vgcreate weixingroup /dev/vdb2
[[email protected] ~]# vgs
  VG          #PV #LV #SN Attr   VSize  VFree 
  weixingroup   1   0   0 wz--n- 96.00m 96.00m
[[email protected] ~]# vgdisplay 

在這裡插入圖片描述
@2.自行指定pe的大小

[[email protected] ~]# vgremove weixingroup 
[[email protected] ~]# vgs
  No volume groups found
[[email protected] ~]# vgcreate weixingroup /dev/vdb2 -s 16M
[[email protected] ~]# vgs
  VG          #PV #LV #SN Attr   VSize  VFree 
  weixingroup   1   0   0 wz--n- 96.00m 96.00m
[[email protected] ~]# vgdisplay 

在這裡插入圖片描述
3.邏輯卷(lvm)的快照的製作

製作快照的意義:

@1.沒有建立快照時,如果誤刪了檔案,那麼檔案將無法恢復!

[[email protected] ~]# df

在這裡插入圖片描述

[[email protected] ~]# cd /weixindata/
[[email protected] weixindata]# ls
lost+found
[[email protected] weixindata]# touch file{1..10}
[[email protected] weixindata]# ls
file1   file2  file4  file6  file8  lost+found
file10  file3  file5  file7  file9
[[email protected] weixindata]# rm -rf file*
[[email protected] weixindata]# ls
lost+found

在這裡插入圖片描述

##建立檔案
[[email protected] weixindata]# touch file{1..10}
[[email protected] weixindata]# ls
file1   file2  file4  file6  file8  lost+found
file10  file3  file5  file7  file9

在這裡插入圖片描述

@2.建立了快照,如果誤刪檔案,是可以恢復的

##製作快照;-L指定大小,-n指定快照名(名字任意),-s指定邏輯卷裝置(照的物件,相當於母盤)
[[email protected] ~]# lvcreate -L 10M -n weixindata_backup -s /dev/weixingroup/weixindata

在這裡插入圖片描述

##掛載;直接掛載快照即可,因為快照與物件是相同的裝置
[[email protected] ~]# mount /dev/weixingroup/weixindata_backup /weixindata/
[[email protected] ~]# df

在這裡插入圖片描述

[[email protected] ~]# cd /weixindata/
[[email protected] weixindata]# ls
file1   file2  file4  file6  file8  lost+found
file10  file3  file5  file7  file9

模擬刪除檔案:

[[email protected] weixindata]# rm -rf file*
[[email protected] weixindata]# ls
lost+found

在這裡插入圖片描述
解決方案:

##1.解除安裝
[[email protected] weixindata]# cd
[[email protected] ~]# umount /weixindata/
##2.刪除舊的快照
[[email protected] ~]# lvremove /dev/weixingroup/weixindata_backup 

在這裡插入圖片描述

##3.重新制作快照
[[email protected] ~]# lvcreate -L 10M -n weixindata_backup -s /dev/weixingroup/weixindata

在這裡插入圖片描述

##4.掛載
[[email protected] ~]# mount /dev/weixingroup/weixindata_backup /weixindata/
[[email protected] ~]# df

在這裡插入圖片描述

##發現此時檔案被恢復
[[email protected] ~]# cd /weixindata/
[[email protected] weixindata]# ls
file1   file2  file4  file6  file8  lost+found
file10  file3  file5  file7  file9

在這裡插入圖片描述

注意:如果製作快照失敗,很可能是裝置容量不夠,那麼需要先裝置擴容,然後再製作快照

4.常見問題及解決方案

(1).當邏輯卷(lvm)正在使用時將裝置刪了,vgs檢視物理卷組時報錯,如何解決?

##檢視物理卷
[[email protected] ~]# pvs
  PV         VG          Fmt  Attr PSize  PFree 
  /dev/vdb2  weixingroup lvm2 a--  96.00m 32.00m
##建立物理卷
[[email protected] ~]# pvcreate /dev/vdb1
  Physical volume "/dev/vdb1" successfully created
##檢視物理卷
[[email protected] ~]# pvs
  PV         VG          Fmt  Attr PSize   PFree  
  /dev/vdb1              lvm2 a--  100.00m 100.00m
  /dev/vdb2  weixingroup lvm2 a--   96.00m  32.00m

在這裡插入圖片描述

##檢視物理卷組
[[email protected] ~]# vgs
  VG          #PV #LV #SN Attr   VSize  VFree 
  weixingroup   1   2   1 wz--n- 96.00m 32.00m
##將物理卷加入到原有的wexingroup物理卷組中
[[email protected] ~]# vgextend weixingroup /dev/vdb1
  Volume group "weixingroup" successfully extended
##檢視物理卷組
[[email protected] ~]# vgs
  VG          #PV #LV #SN Attr   VSize   VFree  
  weixingroup   2   2   1 wz--n- 192.00m 128.00m
##檢視物理卷
[[email protected] ~]# pvs
  PV         VG          Fmt  Attr PSize  PFree 
  /dev/vdb1  weixingroup lvm2 a--  96.00m 96.00m
  /dev/vdb2  weixingroup lvm2 a--  96.00m 32.00m

在這裡插入圖片描述
@1.模擬刪除系統正在使用的裝置

[[email protected] ~]# fdisk /dev/vdb

在這裡插入圖片描述
在這裡插入圖片描述

[[email protected] ~]# partprobe 
[[email protected] ~]# vgs

在這裡插入圖片描述
@2.解決方案:

[[email protected] ~]# vgreduce --removemissing weixingroup 

在這裡插入圖片描述
測試:

[[email protected] ~]# vgs

在這裡插入圖片描述
2.若開機自啟檔案內容編寫錯誤,系統重啟後,發現無法開啟。該如何解決呢?

@1.模擬實驗環境:

[[email protected] ~]# vim /etc/fstab

在這裡插入圖片描述

[[email protected] ~]# reboot
##顯示虛擬機器;發現如下報錯
[[email protected] ~]$ rht-vmctl view server

在這裡插入圖片描述
@2.解決方案

等待片刻會出現如下介面:

在這裡插入圖片描述

#1.輸入root使用者密碼後,得到一個shell環境:

在這裡插入圖片描述

#2.編寫開機自啟檔案,更正檔案內容

在這裡插入圖片描述

#3.直接reboot重啟即可

在這裡插入圖片描述

此時發現系統恢復正常

在這裡插入圖片描述
5.如何徹底刪除物理分割槽?

##檢視邏輯卷     
[[email protected] ~]# lvs
  LV                VG          Attr       LSize  Pool Origin     Data%  Move Log Cpy%Sync Convert
  weixindata        weixingroup owi-aos--- 52.00m                                                 
  weixindata_backup weixingroup swi-a-s--- 12.00m      weixindata   0.10  
##1.刪除快照                       
[[email protected] ~]# lvremove /dev/weixingroup/weixindata_backup 

在這裡插入圖片描述

[[email protected] ~]# df

在這裡插入圖片描述

##2.解除安裝
[[email protected] ~]# umount /weixindata         
##3.刪除邏輯卷
[[email protected] ~]# lvremove /dev/weixingroup/weixindata 
##刪除邏輯卷的自動掛載;否則下次再開機時,系統起不來,因為沒有了掛載裝置
[[email protected] ~]# vim /etc/fstab 
############
刪除:/dev/weixingroup/weixindata     /weixindata     ext4   defaults   0  0

[[email protected] ~]# vgs
  VG          #PV #LV #SN Attr   VSize  VFree 
  weixingroup   1   0   0 wz--n- 96.00m 96.00m
##4.刪除物理卷組
[[email protected] ~]# vgremove weixingroup 
  Volume group "weixingroup" successfully removed
[[email protected] ~]# vgs
  No volume groups found

在這裡插入圖片描述

[[email protected] ~]# pvs
  PV         VG   Fmt  Attr PSize   PFree  
  /dev/vdb2       lvm2 a--  100.00m 100.00m
##5.刪除物理分割槽
[[email protected] ~]# pvremove /dev/vdb2
  Labels on physical volume "/dev/vdb2" successfully wiped
[[email protected] ~]# pvs

在這裡插入圖片描述

##6.刪除分割槽裝置
[[email protected] ~]# fdisk /dev/vdb

在這裡插入圖片描述