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

 

邏輯卷的管理

概述:

物理分區:  用來製作物理卷
物理卷:    pv  用來組成物理卷組 ;包含整數個 pe(物理拓展)  
物理卷組 :  vg  用來製作邏輯卷
邏輯卷:    lvm 它的作用是實現硬盤的無限分區與擴容
1
2
3
4
1.邏輯卷的創建

(1).創建物理分區

##1.新建分區
[root@localhost ~]# fdisk /dev/vdb
1
2

##2.更改分區類型爲LVM
[root@localhost ~]# fdisk /dev/vdb
1
2

##同步分區表
[root@localhost ~]# partprobe
1
2
##3.格式化爲xfs類型
[root@localhost ~]# mkfs.xfs /dev/vdb1 
1
2


##4.建立載點
[root@localhost ~]# mkdir /weixindata
##5.掛載
[root@localhost ~]# mount /dev/vdb1 /weixindata/
[root@localhost ~]# df
1
2
3
4
5


##查看掛載設備的大小
[root@localhost ~]# df -H
1
2


監控:

##查看物理卷
[root@localhost ~]# pvs
##查看物理卷組
[root@localhost ~]# vgs
  No volume groups found
##查看邏輯卷
[root@localhost ~]# lvs
  No volume groups found
[root@localhost ~]# pvs;vgs;lvs
  No volume groups found
  No volume groups found
[root@localhost ~]# pvs;echo ++++++++++;vgs;echo ++++++++++;lvs;echo ++++++++++;df -h /weixindata
1
2
3
4
5
6
7
8
9
10
11
12


##監控
[root@localhost ~]# watch -n 1 'pvs;echo ++++++++++;vgs;echo ++++++++++;lvs;echo ++++++++++;df -h /weixindata'
1
2


##必須先卸載設備,才能創建物理卷
[root@localhost ~]# pvcreate /dev/vdb1
  Can't open /dev/vdb1 exclusively.  Mounted filesystem?
[root@localhost ~]# umount /dev/vdb1
1
2
3
4
(2).創建物理卷;將物理分區處理成物理卷

[root@localhost ~]# pvcreate /dev/vdb1
1

(3).創建物理卷組;將物理卷做成物理卷組

[root@l[root@localhost ~]# vgcreate weixingroup /dev/vdb1
1

(4).創建邏輯卷;將物理卷組(weixingroup) 劃分爲邏輯卷(weixindata)-L指定大小

[root@localhost ~]# lvcreate -L 60M -n weixindata weixingroup
1

(5).掛載邏輯卷設備

##1.格式化邏輯卷設備
[root@localhost ~]# mkfs.xfs /dev/weixingroup/weixindata 
##2.掛載邏輯卷設備
[root@localhost ~]# mount /dev/weixingroup/weixindata /weixindata/
1
2
3
4

@@發現顯示的掛載設備與邏輯卷名字有寫不同;但實際上它們都是dm-0的快捷方式,即表示的是同一個設備

[root@localhost ~]# ll /dev/mapper/weixingroup-weixindata
lrwxrwxrwx. 1 root root 7 Nov 10 11:25 /dev/mapper/weixingroup-weixindata -> ../dm-0
[root@localhost ~]# ll /dev/weixingroup/weixindata
lrwxrwxrwx. 1 root root 7 Nov 10 11:25 /dev/weixingroup/weixindata -> ../dm-0
1
2
3
4

2.邏輯卷的管理

(1).設備的擴容與縮容

監控:
[root@localhost ~]# watch -n 1 "pvs;echo ++++++++++;vgs;echo ++++++++++;lvs;echo ++++++++++;df -h /weixindata"
1
2

xfs類型:

xfs類型的設備只能擴容不能縮容!!!
1
1)VG容量夠時

直接擴大邏輯卷設備,再擴展系統文件即可
1
##1.擴大邏輯卷設備;-L指定大小,將邏輯卷擴大到80M
[root@localhost ~]# lvextend -L 80M /dev/weixingroup/weixindata 
1
2


##2.擴展文件系統
[root@localhost ~]# xfs_growfs /dev/weixingroup/weixindata 
1
2

由於VG容量只有96M,所以不能實現擴容到比96M還大

##擴大邏輯卷設備
[root@localhost ~]# lvextend -L 96M /dev/weixingroup/weixindata 
##擴展文件系統
[root@localhost ~]# xfs_growfs /dev/weixingroup/weixindata 
1
2
3
4


##將邏輯卷設備擴大到97M
[root@localhost ~]# lvextend -L 97M /dev/weixingroup/weixindata 
1
2


2)VG容量不夠時

首先需要先添加物理分區,再創建物理卷,然後將新創建物理卷加入到原有的物理卷組中,最後擴大邏輯卷和文件系統
1
##1.添加物理分區
由於我這裏一開始便添加了兩個物理分區/dev/vdb1和/dev/vdb2,故可以無需再添加設備
[root@localhost ~]# fdisk -l
1
2
3


##2.創建物理卷
[root@localhost ~]# pvcreate /dev/vdb2
1
2


##3.將剛創建的物理卷添加到已有的物理卷組wexingroup中
[root@localhost ~]# vgextend weixingroup /dev/vdb2
1
2


##4.擴大邏輯卷設備
[root@localhost ~]# lvextend -L 150M /dev/weixingroup/weixindata 
##5.擴展文件系統
[root@localhost ~]# xfs_growfs /dev/weixingroup/weixindata 
1
2
3
4

ext4類型:

ext4類型的設備不僅可以擴容還可以縮容!!!
1
1)設備的擴容

##1.卸載
[root@localhost ~]# umount /weixindata/
##2.格式化爲ext4類型
[root@localhost ~]# mkfs.ext4 /dev/weixingroup/weixindata 
1
2
3
4


##擴容時爲熱拉伸,即可以直接對掛載設備進行拉伸
[root@localhost ~]# mount /dev/weixingroup/weixindata /weixindata/
##4.擴大邏輯卷設備
[root@localhost ~]# lvextend -L 170M /dev/weixingroup/weixindata 
1
2
3
4


##5.擴展文件系統(注意:與xfs類型有所不同)
[root@localhost ~]# resize2fs /dev/weixingroup/weixindata
1
2

2)設備的縮容

必須先卸載設備才能縮容,並且必須先縮小文件系統(軟件)再縮小邏輯卷設備(硬件)!!!
1
##1.卸載
[root@localhost ~]# umount /weixindata/
##2.掃描文件系統
[root@localhost ~]# e2fsck -f /dev/weixingroup/weixindata 
1
2
3
4


##3.縮小文件系統
[root@localhost ~]# resize2fs /dev/weixingroup/weixindata 50M
##4.掛載
[root@localhost ~]# mount /dev/weixingroup/weixindata /weixindata/
1
2
3
4


##5.縮小邏輯卷設備
[root@localhost ~]# lvreduce -L 50M /dev/weixingroup/weixindata 
1
2

(2).物理卷的取出

此操作不區分文件系統類型,即不管是xfs類型還是ext4類型,均按照以下操作將物理卷取出
1
如果設備上有存儲數據,那麼要想取出物理卷,必須先將設備上的數據遷移到空閒的設備上!!!
1


##1.數據的遷移;將/dev/vdb1設備上的數據遷移到/dev/vdb2設備中(如果設備上沒有存儲數據,就不用執行此操作)
[root@localhost ~]# pvmove /dev/vdb1 /dev/vdb2
1
2


##2.取出物理卷組
[root@localhost ~]# vgreduce weixingroup /dev/vdb1
1
2


##3.取出物理卷
[root@localhost ~]# pvremove /dev/vdb1
1
2

(3).如何指pe的大小

所謂pe是指:
1
[root@localhost ~]# pvs
  PV         VG          Fmt  Attr PSize  PFree 
  /dev/vdb2  weixingroup lvm2 a--  96.00m 44.00m
[root@localhost ~]# vgs
  VG          #PV #LV #SN Attr   VSize  VFree 
  weixingroup   1   1   0 wz--n- 96.00m 44.00m
[root@localhost ~]# lvs
  LV         VG          Attr       LSize  Pool Origin Data%  Move Log Cpy%Sync Convert
  weixindata weixingroup -wi-ao---- 52.00m                                             
[root@localhost ~]# df
1
2
3
4
5
6
7
8
9
10


[root@localhost ~]# umount /weixindata/
[root@localhost ~]# lvremove /dev/weixingroup/weixindata 
[root@localhost ~]# vgremove weixingroup 
[root@localhost ~]# lvs
  No volume groups found
[root@localhost ~]# vgs
  No volume groups found
1
2
3
4
5
6
7

@1.默認創建的物理卷中一個pe的大小爲4MIB

[root@localhost ~]# pvs
  PV         VG   Fmt  Attr PSize   PFree  
  /dev/vdb2       lvm2 a--  100.00m 100.00m
[root@localhost ~]# vgcreate weixingroup /dev/vdb2
[root@localhost ~]# vgs
  VG          #PV #LV #SN Attr   VSize  VFree 
  weixingroup   1   0   0 wz--n- 96.00m 96.00m
[root@localhost ~]# vgdisplay 
1
2
3
4
5
6
7
8

@2.自行指定pe的大小

[root@localhost ~]# vgremove weixingroup 
[root@localhost ~]# vgs
  No volume groups found
[root@localhost ~]# vgcreate weixingroup /dev/vdb2 -s 16M
[root@localhost ~]# vgs
  VG          #PV #LV #SN Attr   VSize  VFree 
  weixingroup   1   0   0 wz--n- 96.00m 96.00m
[root@localhost ~]# vgdisplay 
1
2
3
4
5
6
7
8

3.邏輯卷(lvm)的快照的製作

製作快照的意義:
1
@1.沒有創建快照時,如果誤刪了文件,那麼文件將無法恢復!

[root@localhost ~]# df
1


[root@localhost ~]# cd /weixindata/
[root@localhost weixindata]# ls
lost+found
[root@localhost weixindata]# touch file{1..10}
[root@localhost weixindata]# ls
file1   file2  file4  file6  file8  lost+found
file10  file3  file5  file7  file9
[root@localhost weixindata]# rm -rf file*
[root@localhost weixindata]# ls
lost+found
1
2
3
4
5
6
7
8
9
10


##創建文件
[root@localhost weixindata]# touch file{1..10}
[root@localhost weixindata]# ls
file1   file2  file4  file6  file8  lost+found
file10  file3  file5  file7  file9
1
2
3
4
5


@2.創建了快照,如果誤刪文件,是可以恢復的

##製作快照;-L指定大小,-n指定快照名(名字任意),-s指定邏輯卷設備(照的對象,相當於母盤)
[root@localhost ~]# lvcreate -L 10M -n weixindata_backup -s /dev/weixingroup/weixindata
1
2


##掛載;直接掛載快照即可,因爲快照與對象是相同的設備
[root@localhost ~]# mount /dev/weixingroup/weixindata_backup /weixindata/
[root@localhost ~]# df
1
2
3


[root@localhost ~]# cd /weixindata/
[root@localhost weixindata]# ls
file1   file2  file4  file6  file8  lost+found
file10  file3  file5  file7  file9
1
2
3
4
模擬刪除文件:

[root@localhost weixindata]# rm -rf file*
[root@localhost weixindata]# ls
lost+found
1
2
3

解決方案:

##1.卸載
[root@localhost weixindata]# cd
[root@localhost ~]# umount /weixindata/
##2.刪除舊的快照
[root@localhost ~]# lvremove /dev/weixingroup/weixindata_backup 
1
2
3
4
5


##3.重新制作快照
[root@localhost ~]# lvcreate -L 10M -n weixindata_backup -s /dev/weixingroup/weixindata
1
2


##4.掛載
[root@localhost ~]# mount /dev/weixingroup/weixindata_backup /weixindata/
[root@localhost ~]# df
1
2
3


##發現此時文件被恢復
[root@localhost ~]# cd /weixindata/
[root@localhost weixindata]# ls
file1   file2  file4  file6  file8  lost+found
file10  file3  file5  file7  file9
1
2
3
4
5


注意:如果製作快照失敗,很可能是設備容量不夠,那麼需要先設備擴容,然後再製作快照
1
4.常見問題及解決方案

(1).當邏輯卷(lvm)正在使用時將設備刪了,vgs查看物理卷組時報錯,如何解決?

##查看物理卷
[root@localhost ~]# pvs
  PV         VG          Fmt  Attr PSize  PFree 
  /dev/vdb2  weixingroup lvm2 a--  96.00m 32.00m
##創建物理卷
[root@localhost ~]# pvcreate /dev/vdb1
  Physical volume "/dev/vdb1" successfully created
##查看物理卷
[root@localhost ~]# pvs
  PV         VG          Fmt  Attr PSize   PFree  
  /dev/vdb1              lvm2 a--  100.00m 100.00m
  /dev/vdb2  weixingroup lvm2 a--   96.00m  32.00m
1
2
3
4
5
6
7
8
9
10
11
12


##查看物理卷組
[root@localhost ~]# vgs
  VG          #PV #LV #SN Attr   VSize  VFree 
  weixingroup   1   2   1 wz--n- 96.00m 32.00m
##將物理卷加入到原有的wexingroup物理卷組中
[root@localhost ~]# vgextend weixingroup /dev/vdb1
  Volume group "weixingroup" successfully extended
##查看物理卷組
[root@localhost ~]# vgs
  VG          #PV #LV #SN Attr   VSize   VFree  
  weixingroup   2   2   1 wz--n- 192.00m 128.00m
##查看物理卷
[root@localhost ~]# 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

@1.模擬刪除系統正在使用的設備

[root@localhost ~]# fdisk /dev/vdb
1

[root@localhost ~]# partprobe 
[root@localhost ~]# vgs
1
2

@2.解決方案:

[root@localhost ~]# vgreduce --removemissing weixingroup 
1

測試:

[root@localhost ~]# vgs
1

2.若開機自啓文件內容編寫錯誤,系統重啓後,發現無法開啓。該如何解決呢?

@1.模擬實驗環境:

[root@localhost ~]# vim /etc/fstab
1


[root@localhost ~]# reboot
##顯示虛擬機;發現如下報錯
[kiosk@foundation34 ~]$ rht-vmctl view server
1
2
3

@2.解決方案

等待片刻會出現如下界面:
1


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


#2.編寫開機自啓文件,更正文件內容
1


#3.直接reboot重啓即可
1


此時發現系統恢復正常
1

5.如何徹底刪除物理分區?

##查看邏輯卷     
[root@localhost ~]# 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.刪除快照                       
[root@localhost ~]# lvremove /dev/weixingroup/weixindata_backup 
1
2
3
4
5
6
7


[root@localhost ~]# df
1


##2.卸載
[root@localhost ~]# umount /weixindata         
1
2
##3.刪除邏輯卷
[root@localhost ~]# lvremove /dev/weixingroup/weixindata 
##刪除邏輯卷的自動掛載;否則下次再開機時,系統起不來,因爲沒有了掛載設備
[root@localhost ~]# vim /etc/fstab 
############
刪除:/dev/weixingroup/weixindata     /weixindata     ext4   defaults   0  0

[root@localhost ~]# vgs
  VG          #PV #LV #SN Attr   VSize  VFree 
  weixingroup   1   0   0 wz--n- 96.00m 96.00m
1
2
3
4
5
6
7
8
9
10
##4.刪除物理卷組
[root@localhost ~]# vgremove weixingroup 
  Volume group "weixingroup" successfully removed
[root@localhost ~]# vgs
  No volume groups found
1
2
3
4
5


[root@localhost ~]# pvs
  PV         VG   Fmt  Attr PSize   PFree  
  /dev/vdb2       lvm2 a--  100.00m 100.00m
##5.刪除物理分區
[root@localhost ~]# pvremove /dev/vdb2
  Labels on physical volume "/dev/vdb2" successfully wiped
[root@localhost ~]# pvs
1
2
3
4
5
6
7


##6.刪除分區設備
[root@localhost ~]# fdisk /dev/vdb

https://blog.csdn.net/lilygg/article/details/84035315

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章