## 一、什么是LVM
LVM2(Logical Volume Manager, Version:2),邏輯卷管理器,是使用的是Linux系統內核中的dm(Device Mapper)模塊(能夠將一個或多個底層塊設備組織成一個邏輯設備的模塊)驅動的。
## 二、LVM的組成

1. PV(Physical Volume)
物理卷,我們通常理解的物理磁盤,這里的磁盤不會劃分最小單位的數據單元。
2. VG(Volume Group)
卷組,是若干個PV的集合,將若干個PV,整合在一起,提供一個整體的存儲空間。在這里可以設定VG的最小存儲單元,也就是PE(Physical Extent),是VG中的最小存儲單元。
3. LV(Logical Volume)
邏輯卷組,在VG之上被單獨劃分的獨立存儲區域,可以理解為單獨劃分的獨立分區。在這里可以設定LV的最小存儲單元,也就是LE(Logical Extent),是LV中的最小存儲單元。
## 三、LVM的特點
### 1. LVM的表現形式
/dev/dm-#就是在Linux系統中LVM文件的實際位置。
(1) /dev/mapper/VG_NAME-LV_NAME
例如:/dev/mapper/vol0-root
(2) /dev/VG_NAME/LV_NAME?????????? (注:這個是前一個的軟鏈接)
例如:/dev/vol0/root
### 2. LVM的特點
(1) 邊界動態擴展
(2) 邊界的擴展和收縮不影響其中存儲的數據
(3) 支持快照功能
## 四、管理LVM
### 1. 創建順序及注意事項
創建LVM文件系統需要遵循如下順序進行:PV –> VG –> LV
### 2. 各邏輯組成管理匯總
#### (1) PV管理
命令匯總:
命令 | 作用
—— | ——
pvchange | 修改物理卷屬性
pvck | 檢查物理卷
pvcreate | 創建物理卷
pvdisplay | 顯示物理卷
pvmove | 將物理卷上的現有數據(PE),移動到其他物理卷上
pvremove | 刪除物理卷
pvresize | 修改被LVM2使用的磁盤或分區的大小
pvs | 簡要顯示物理卷
pvscan | 掃描物理卷
**a. PV創建**
這里需要注意的是,創建LVM的PV,需要將分區格式調整為”8e Linux LVM”,并使用partx -a 讓內核識別新建分區,如果partx不好用,可以考慮使用partprobe,或者重啟系統。
創建命令:pvcreate DEVICE
示例:pvcreate /dev/sdb{5,6,7}
**b. PV移動**
pvmove /dev/sdb5
**c. PV移除**
pvremove /dev/sdb5
#### (2) VG管理
命令匯總:
命令 | 作用
—— | ——
vgdisplay | 顯示VG信息
vgs | 顯示VG簡要信息
vgcreate | 創建VG,可使用-s選項指定PE大小
vgrename | 重命名VG
vgremove | 刪除VG
vgextend | 擴展VG容量
vgscan | 掃描VG
vgreduce | 縮減VG容量
vgsplit | 切割VG
**a. VG創建**
一定要指定VG的名稱
創建命令:vgcreate VG_NAME PV_NAME -s PE_SIZE
示例:vgcreate vg0 /dev/sdb{5,6,7} -s 5M
**b. 縮減VG容量(注意要先做pvmove移動pv上的PE數據塊)**
vgreduce VG_NAME PV_NAME
示例:vgreduce vg0 /dev/sdb5
**c. 擴展VG容量**
vgextend VG_NAME PV_NAME
示例:vgextend vg0 /dev/sdb5
#### (3) LV管理
命令匯總:
命令 | 作用
—— | ——
lvdisplay | 顯示LV
lvs | 簡要顯示LV
lvcreate | 創建LV
lvextend | 擴展LV空間
lvreduce | 縮減LV空間,可使用-L設定LV大小,-n設定LV名稱,-l設定LV的LE數量
lvremove | 刪除LV
lvresize | 修改LV空間
lvscan | 掃描LV
lvconf | 設定LV的配置文件
**a. 創建LV**
創建命令:lvcreate { -L LV_SIZE | -l LV_Number } -n LV_NAME VG_NAME
示例:lvcreate -L 10G -n lv0 vg0,意思是在vg0卷組中,創建名為lv0,大小為10G的LV邏輯卷組。
> 注意:
在創建LV時,所指定的LV大小必須在VG中有足夠的空間才可以。
**b. 擴展LV**
lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME
**擴展LV時需要的步驟**
– 擴展物理邊界(擴展LV的大小)
lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME
– 擴展邏輯邊界(修改文件系統大?。?br />這里只限擴展ext系列文件系統,resize2fs /dev/VG_NAME/LV_NAME
其他文件系統使用其他命令。
> 注意:
其中,“+”有特殊意義:
帶“+”表示在現有基礎上增加多少;
不帶“+”表示將空間擴展至多大;
另外,
擴展時LV要寫全絕對路徑,不能只寫LV的名字。
**c. 縮減LV**
lvreduce -L [-]#[mMgGtT] /dev/VG_NAME/LV_NAME
**縮減LV時需要的步驟**
– 取消該LV的掛載
umount /dev/VG_NAME/LV_NAME
– 做文件系統強制性檢測和修復
e2fsck -f /dev/VG_NAME/LV_NAME
– 重新調整ext文件系統大小
resize2fs /dev/VG_NAME/LV_NAME #[mMgGtT]
– 縮減LV容量
lvreduce -L [-]#[mMgGtT] /dev/VG_NAME/LV_NAME
– 重新掛載文件系統
mount /dev/VG_NAME/LV_NAME /PATH/TO/MOUNT_POINT
> 注意:
其中,“-”有特殊意義:
帶“-”表示在現有基礎上縮減多少;
不帶“-”表示將空間縮減至多大;
另外,
縮減后的空間一定要能夠容納現有存儲的文件才可以,否則文件必然損壞。
示例:
“`
~]# umount /backup
~]# e2fsck -f /dev/mapper/vg0-lv0
~]# resize2fs /dev/mapper/vg0-lv0 5G
~]# lvreduce -L 5G /dev/mapper/vg0-lv0
~]# mount /dev/mapper/vg0-lv0 /backup
“`
**d. 刪除LV**
lvremove LV_NAME
**e. 快照卷**
lvcreate -L #[mMgGtT] -p r -s -n SNAPSHOT_LV_NAME Original_Name
其中
選項 | 含義
—— | ——
-L | 指明快照卷大小
-p | 指明快照卷權限,r為只讀,rw為讀寫
-s | 指明創建的是快照卷
-n | 指明快照卷名稱
示例:
“`
~]# lvcreate -s -L 2G -n lv0-snap -p r /dev/vg0/lv0
Using default stripesize 64.00 KiB.
Rounding up size to full physical extent 2.00 GiB
Logical volume “lv0-snap” created.
~]# mount /dev/vg0/lv0-snap /mnt
mount: /dev/mapper/vg0-lv0–snap is write-protected, mounting read-only
~]# tail /mnt/functions
“x$1” = xrestart -o \
“x$1” = xreload -o \
“x$1” = xtry-restart -o \
“x$1” = xforce-reload -o \
“x$1” = xcondrestart ] ; then
systemctl_redirect $0 $1
exit $?
fi
fi
~]# vim /backup/functions
在文件末尾添加如下一行:
test insert character.
~]# tail -2 /backup/functions
fi
test insert character.
~]# tail -2 /mnt/functions
fi
fi
~]# cp /etc/issue /backup/
~]# ls /backup/
functions? issue? lost+found
~]# ls /mnt/
functions? lost+found
“`
## 五、練習題
1. 創建一個至少由兩個PV組成,大小為20G,名為testvg的VG。要求:PE大小為16MB,而后在卷組中創建大小為5G的邏輯卷testlv。掛載至/users目錄。
“`
~]# pvcreate /dev/sdb12
Physical volume “/dev/sdb12” successfully created.
~]# pvcreate /dev/sdb13
Physical volume “/dev/sdb13” successfully created.
~]# pvdisplay /dev/sdb{12,13}
“/dev/sdb12” is a new physical volume of “10.00 GiB”
— NEW Physical volume —
PV Name?????????????? /dev/sdb12
VG Name
PV Size?????????????? 10.00 GiB
Allocatable?????????? NO
PE Size?????????????? 0
Total PE????????????? 0
Free PE?????????????? 0
Allocated PE????????? 0
PV UUID?????????????? 6bpfIP-9QEj-oNdQ-ih0e-VFbi-b3fw-YSBxgN
“/dev/sdb13” is a new physical volume of “10.00 GiB”
— NEW Physical volume —
PV Name?????????????? /dev/sdb13
VG Name
PV Size?????????????? 10.00 GiB
Allocatable?????????? NO
PE Size?????????????? 0
Total PE????????????? 0
Free PE?????????????? 0
Allocated PE????????? 0
PV UUID?????????????? NiEAMK-46oD-bdO7-lUEs-zQgM-jYau-kVlVcD
~]# vgcreate testvg -s 16M /dev/sdb{12,13}
Volume group “testvg” successfully created
~]# vgdisplay testvg
— Volume group —
VG Name?????????????? testvg
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.97 GiB
PE Size?????????????? 16.00 MiB
Total PE????????????? 1278
Alloc PE / Size?????? 0 / 0
Free? PE / Size?????? 1278 / 19.97 GiB
VG UUID?????????????? uBalL0-Zc2o-YOGd-MyTA-K2Aw-PDf3-ZR6YDk
~]# lvcreate -L 5G -n testlv testvg
Logical volume “testlv” created.
~]# lvdisplay
— Logical volume —
LV Path??????????????? /dev/testvg/testlv
LV Name??????????????? testlv
VG Name??????????????? testvg
LV UUID??????????????? mjmG6u-TRQB-1XDR-M06U-PMJ1-H0ik-r93Z3E
LV Write Access??????? read/write
LV Creation host, time sapbcs.mageedu.com, 2017-10-10 16:47:01 +0800
LV Status????????????? available
# open???????????????? 0
LV Size??????????????? 5.00 GiB
Current LE???????????? 320
Segments?????????????? 1
Allocation???????????? inherit
Read ahead sectors???? auto
– currently set to???? 8192
Block device?????????? 253:0
~]# mke2fs -t ext4 /dev/testvg/testlv
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
~]# mkdir /users
~]# mount /dev/testvg/testlv /users
“`
2. 新建用戶archlinux,要求其家目錄為/users/archlinux,而后切換至archlinux用戶,復制/etc/pam.d/內的所有文件至自己的家目錄。
“`
~]# useradd archlinux -d /users/archlinux
~]# su – archlinux
~]$ cp -r /etc/pam.d/* ./
~]$ ls
atd?????????????? fingerprint-auth-ac???? ksu?????????????? pluto???????? runuser-l????????? su????????????? vlock
chfn????????????? gdm-autologin?????????? liveinst????????? polkit-1????? setup????????????? sudo??????????? vmtoolsd
chsh????????????? gdm-fingerprint???????? login???????????? postlogin???? smartcard-auth???? sudo-i????????? xserver
config-util?????? gdm-launch-environment? other???????????? postlogin-ac? smartcard-auth-ac? su-l
crond???????????? gdm-password??????????? passwd??????????? ppp?????????? smtp?????????????? system-auth
cups????????????? gdm-pin???????????????? password-auth???? remote??????? smtp.postfix?????? system-auth-ac
fingerprint-auth? gdm-smartcard?????????? password-auth-ac? runuser?????? sshd?????????????? systemd-user
“`
3. 擴展testlv至7G,要求archlinux用戶的文件不能丟失
“`
~]# df -lh /users
Filesystem???????????????? Size? Used Avail Use% Mounted on
/dev/mapper/testvg-testlv? 4.8G?? 21M? 4.6G?? 1% /users
~]# lvs
LV???? VG???? Attr?????? LSize Pool Origin Data%? Meta%? Move Log Cpy%Sync Convert
testlv testvg -wi-ao—- 7.00g
~]# vgs
VG???? #PV #LV #SN Attr?? VSize? VFree
testvg?? 2?? 1?? 0 wz–n- 19.97g 12.97g
~]# lvextend -L 7G /dev/testvg/testlv
Size of logical volume testvg/testlv changed from 5.00 GiB (320 extents) to 7.00 GiB (448 extents).
Logical volume testvg/testlv successfully resized.
~]# df -lh /users
Filesystem???????????????? Size? Used Avail Use% Mounted on
/dev/mapper/testvg-testlv? 6.8G?? 23M? 6.4G?? 1% /users
~]# ls /users/archlinux/
atd?????????????? fingerprint-auth-ac???? ksu?????????????? pluto???????? runuser-l????????? su????????????? vlock
chfn????????????? gdm-autologin?????????? liveinst????????? polkit-1????? setup????????????? sudo??????????? vmtoolsd
chsh????????????? gdm-fingerprint???????? login???????????? postlogin???? smartcard-auth???? sudo-i????????? xserver
config-util?????? gdm-launch-environment? other???????????? postlogin-ac? smartcard-auth-ac? su-l
crond???????????? gdm-password??????????? passwd??????????? ppp?????????? smtp?????????????? system-auth
cups????????????? gdm-pin???????????????? password-auth???? remote??????? smtp.postfix?????? system-auth-ac
fingerprint-auth? gdm-smartcard?????????? password-auth-ac? runuser?????? sshd?????????????? systemd-user
“`
4. 縮減testlv至3G,要求archlinux用戶的文件不能丟失
“`
~]# umount /dev/testvg/testlv
~]# e2fsck -f /dev/testvg/testlv
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/testvg/testlv: 69/458752 files (0.0% non-contiguous), 67379/1835008 blocks
~]# resize2fs /dev/testvg/testlv 3G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/testvg/testlv to 786432 (4k) blocks.
The filesystem on /dev/testvg/testlv is now 786432 blocks long.
~]# lvreduce -L 3G /dev/testvg/testlv
WARNING: Reducing active logical volume to 3.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce testvg/testlv? [y/n]: y
Size of logical volume testvg/testlv changed from 7.00 GiB (448 extents) to 3.00 GiB (192 extents).
Logical volume testvg/testlv successfully resized.
~]# mount /dev/testvg/testlv /users
~]# ls /users/archlinux/
atd?????????????? fingerprint-auth-ac???? ksu?????????????? pluto???????? runuser-l????????? su????????????? vlock
chfn????????????? gdm-autologin?????????? liveinst????????? polkit-1????? setup????????????? sudo??????????? vmtoolsd
chsh????????????? gdm-fingerprint???????? login???????????? postlogin???? smartcard-auth???? sudo-i????????? xserver
config-util?????? gdm-launch-environment? other???????????? postlogin-ac? smartcard-auth-ac? su-l
crond???????????? gdm-password??????????? passwd??????????? ppp?????????? smtp?????????????? system-auth
cups????????????? gdm-pin???????????????? password-auth???? remote??????? smtp.postfix?????? system-auth-ac
fingerprint-auth? gdm-smartcard?????????? password-auth-ac? runuser?????? sshd?????????????? systemd-user
~]# df -lh /users
Filesystem???????????????? Size? Used Avail Use% Mounted on
/dev/mapper/testvg-testlv? 2.9G?? 16M? 2.7G?? 1% /users
“`
5. 對testlv創建快照并嘗試基于快照備份數據,驗證快照的功能
“`
~]# lvcreate -L 1G -p r -s -n testlv-snap /dev/testvg/testlv
Using default stripesize 64.00 KiB.
Logical volume “testlv-snap” created.
~]# mount /dev/testvg/testlv-snap /mnt
mount: /dev/mapper/testvg-testlv–snap is write-protected, mounting read-only
~]# ls /mnt
archlinux? lost+found
[root@sapbcs ~]# ls /mnt/archlinux/
atd?????????????? fingerprint-auth-ac???? ksu?????????????? pluto???????? runuser-l????????? su????????????? vlock
chfn????????????? gdm-autologin?????????? liveinst????????? polkit-1????? setup????????????? sudo??????????? vmtoolsd
chsh????????????? gdm-fingerprint???????? login???????????? postlogin???? smartcard-auth???? sudo-i????????? xserver
config-util?????? gdm-launch-environment? other???????????? postlogin-ac? smartcard-auth-ac? su-l
crond???????????? gdm-password??????????? passwd??????????? ppp?????????? smtp?????????????? system-auth
cups????????????? gdm-pin???????????????? password-auth???? remote??????? smtp.postfix?????? system-auth-ac
fingerprint-auth? gdm-smartcard?????????? password-auth-ac? runuser?????? sshd?????????????? systemd-user
“`
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/88076