1. 程式人生 > >Linux中LVM邏輯卷管理

Linux中LVM邏輯卷管理

LVM(邏輯卷管理器)是一項非常普及的硬碟裝置資源管理技術。LVM可以允許使用者對硬碟資源進行動態調整。
邏輯卷管理器時Linux系統用於對硬碟分割槽進行管理的一種機制,理論性較強,其建立初衷是為了解決硬碟裝置在建立分割槽後不易修改分割槽大小的缺陷。儘管對傳統的硬碟分割槽進行強制擴容或縮容從理論上來講是可行的,但是卻可能造成資料的丟失。而LVM技術是在硬碟分割槽和檔案系統之間添加了一個邏輯層,它提供了一個抽象的卷組,可以把多塊硬碟進行卷組合並。這樣一來,使用者不必關心物理硬碟裝置的底層構架和佈局,就可以實現對硬碟分割槽的動態調整。LVM的技術構架如下圖:

在這裡插入圖片描述

為了幫助大家理解在這裡舉一個吃貨的例子。比如小明家裡想吃饅頭但是麵粉不夠了,於是媽媽從隔壁老王家、老李家、老張家分別皆來一些麵粉,準備蒸饅頭吃。首先需要把這些麵粉(物理卷[PV,Pyhsical Volume])揉成一個大面團(卷組[VG,Volume Group]),然後再把這個大面團分割成一個個小饅頭(邏輯卷[LV,Logical Volume]),而且每個小饅頭的中向必須是每勺麵粉(基本單元[PE,Physical Extent])的倍數。
物理卷處於LVM中的最底層,可以將其理解為物理硬碟,硬碟分割槽或者RAID磁碟陣列,這樣都可以。卷組建立再物理卷之上,一個卷組可以包含多個物理卷,而且在卷組建立之後也可以繼續向其中新增新的物理卷。邏輯卷用卷組中空閒的資源建立的,並且邏輯卷在建立後可以動態地擴充套件或縮小空間。這就是LVM的核心理念。

LVM的相關指令:

在這裡插入圖片描述

部署邏輯卷

[[email protected] ~]# fdisk /dev/vdb		建立分割槽2個並修改分割槽id
Command (m for help): n
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +200M
Command (m for help): t
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): wq			記得推出儲存
[
[email protected]
~]# partprobe 重新讀取硬碟資訊,同步分割槽 [[email protected] ~]# mkdir /mtf_lvm 新建一個用來掛載lvm的文目錄 [[email protected] ~]# pvcreate /dev/vdb1 /dev/vdb2 建立物理卷 [[email protected] ~]# vgcreate mtf_vg /dev/vdb1 /dev/vdb2 物理卷加入物理卷組 [[email protected] ~]# lvcreate -L 150M -n mtf_lv mtf_vg 建立邏輯卷大小為150M,劃分空間 [
[email protected]
~]# vgdisplay 顯示物理卷組 [[email protected] ~]# lvdisplay 顯示邏輯卷組 [[email protected] ~]# mkfs.xfs /dev/mtf_vg/mtf_lv 格式化 [[email protected] ~]# mount /dev/mtf_vg/mtf_lv /mtf_lvm/ 掛載 [[email protected] ~]# echo "/dev/mtf_vg/mtf_lv /mtf_lvm xfs defaults 0 0" >> /etc/fstab 開機自動掛載 [[email protected] ~]# mount -a 檢查有沒有錯誤 [[email protected] ~]# reboot 重啟驗證

在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述

擴容邏輯卷

[[email protected] ~]# umount /mtf_lvm/		解除安裝
[[email protected] ~]# lvextend -L 300M /dev/mtf_vg/mtf_lv 	將邏輯卷擴充套件為300M
[[email protected] ~]# mount /dev/mtf_vg/mtf_lv /mtf_lvm/	掛載,掛載後再重置硬碟容量,要不然會報錯,失敗
[[email protected] ~]# xfs_growfs /dev/mtf_vg/mtf_lv		重置硬碟容量
[[email protected] ~]# df -h		檢查

在這裡插入圖片描述

在這裡插入圖片描述

縮小邏輯卷

相較於擴容邏輯卷,在對邏輯捲進行縮容操作時,其丟失資料的風險更大。所以在生產環境中指令相應操作時,一定要提前備份好資料。另外Linux系統規定,在對LVM邏輯捲進行縮容操作之前,要先檢查檔案系統的完整性(當然這也是為了我們的資料安全)。在執行縮容操作前機的把檔案系統解除安裝掉。

剛才說過xfs不支援縮小,ext4支援縮小
[[email protected] ~]# umount /mtf_lvm/
[[email protected] ~]# e2fsck -f /dev/mtf_vg/mtf_lv
[[email protected] ~]# resize2fs /dev/mtf_vg/mtf_lv 50M
[[email protected] ~]# lvreduce -L 50M /dev/mtf_vg/mtf_lv
Do you really want to reduce mtf_lv? [y/n]: y
[[email protected] ~]# mount /dev/mtf_vg/mtf_lv /mtf_lvm/
[[email protected] ~]# df -h

在這裡插入圖片描述

在這裡插入圖片描述

邏輯卷快照

快照的容量必須等同於邏輯卷的容量(大於也行,小於的話恢復時候資料丟失)
快照卷僅一次有效,一旦執行還原操作後則會被立即自動刪除

[[email protected] ~]# mount /dev/mtf_vg/mtf_lv /mtf_lvm/
[[email protected] ~]# df -H
[[email protected] ~]# touch /mtf_lvm/ceshiwenjian
[[email protected] ~]# lvcreate -s -L 40M -n mtf_lvmbeitai /dev/mtf_vg/mtf_lv
[[email protected] ~]# lvdisplay
[[email protected] ~]# dd if=/dev/zero of=/mtf_lvm/files count=1 bs=30M
[[email protected] ~]# lvdisplay
[[email protected] ~]# umount /mtf_lvm/
[[email protected] ~]# lvconvert --merge /dev/mtf_vg/mtf_lvmbeitai
[[email protected] ~]# mount /dev/mtf_vg/mtf_lv /mtf_lvm/
[[email protected] ~]# ls /mtf_lvm/
命令的引數及為什麼要執行在圖片中詳細說明

在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述

刪除邏輯卷

當生產環境中想要重新部署LVM或者不再需要使用LVM時,則需要執行LVM的刪除操作。為此,需要提前備份好重要的資料資訊,然後依次刪除邏輯卷、卷組、物理卷裝置,這個順序不可顛倒

[[email protected] ~]# umount /mtf_lvm/
[[email protected] ~]# vim /etc/fstab 
[[email protected] ~]# lvremove /dev/mtf_vg/mtf_lv 
Do you really want to remove active logical volume mtf_lv? [y/n]: y
[[email protected] ~]# vgremove mtf_vg
[[email protected] ~]# pvremove /dev/vdb1 /dev/vdb2
[[email protected] ~]# partprobe
[[email protected] ~]# fdisk /dev/vdb		刪除分割槽或者修改分割槽id

在這裡插入圖片描述