邏輯卷管理器(LVM)
普通磁盤分區管理在分區劃分成功後無法改變大小,後期出現分區空間不足時,擴展不方便。使用LVM管理可以很好解決這一問題。LVM可以跨分區,跨硬盤,跨文件系統。從邏輯上管理磁盤,在不停機的情況下調整各個分區大小。
具體實現方法
創建物理卷(PV)、創建卷組(VG)、創建邏輯卷(LV)、創建文件系統、掛載
創建物理卷(PV),可以是分區,也可以是一整個硬盤
[root@centos7 ~]#pvcreate /dev/sdb
Physical volume "/dev/sdb" successfully created.
[root@centos7 ~]#pvs
PV VG Fmt Attr PSize PFree
/dev/sdb lvm2 --- 20.00g 20.00g
[root@centos7 ~]#pvdisplay
"/dev/sdb" is a new physical volume of "20.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb
VG Name
PV Size 20.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID T2wECl-9Em4-gIeP-PjSY-m7mV-B12k-o1pknY
創建卷組(VG) 將多個物理卷組合成一個邏輯上的大硬盤,創建時可以指定PE(最小存儲空間,存儲小文件時PE值小更節約空間,大文件PE大些更合理。根據實際情況設定)的大小,默認爲4M。方法爲 -s PEsize
[root@centos7 ~]#vgcreate vg0 /dev/sdb /dev/sdc
Volume group "vg0" successfully created
[root@centos7 ~]#vgs
VG #PV #LV #SN Attr VSize VFree
vg0 2 0 0 wz--n- 39.99g 39.99g
[root@centos7 ~]#vgdisplay
--- Volume group ---
VG Name vg0
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 39.99 GiB
PE Size 4.00 MiB
Total PE 10238
Alloc PE / Size 0 / 0
Free PE / Size 10238 / 39.99 GiB
VG UUID yb06Vf-byOo-wgAm-4C1U-3y1J-Co0I-ev6zpC
[root@centos7 ~]#
創建邏輯卷(LV) 在VG上劃分多個LV,與硬盤分區功能相似
[root@centos7 ~]#lvcreate -n lv0 -L +10G /dev/vg0
WARNING: xfs signature detected on /dev/vg0/lv0 at offset 0. Wipe it? [y/n]: Y
Wiping xfs signature on /dev/vg0/lv0.
Logical volume "lv0" created.
[root@centos7 ~]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv0 vg0 -wi-a----- 10.00g
[root@centos7 ~]#lvdisplay
--- Logical volume ---
LV Path /dev/vg0/lv0
LV Name lv0
VG Name vg0
LV UUID FaZ8SU-47QZ-e1Qp-kMNq-p4gh-Uk4E-CKBLHB
LV Write Access read/write
LV Creation host, time centos7.localdomain, 2019-03-28 09:22:51 +0800
LV Status available
# open 0
LV Size 10.00 GiB
Current LE 2560
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
[root@centos7 ~]#
創建文件系統 LV創建後,和硬盤分區管理相似。創建文件系統並掛載到想要的目錄下
[root@centos7 ~]#mkfs.xfs /dev/vg0/lv0
meta-data=/dev/vg0/lv0 isize=512 agcount=4, agsize=655360 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
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
[root@centos7 ~]#mkdir /lvtest
[root@centos7 ~]#mount /dev/vg0/lv0 /lvtest
[root@centos7 ~]#df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 104806400 3775908 101030492 4% /
devtmpfs 915784 0 915784 0% /dev
tmpfs 931624 0 931624 0% /dev/shm
tmpfs 931624 10464 921160 2% /run
tmpfs 931624 0 931624 0% /sys/fs/cgroup
/dev/sda3 52403200 1081684 51321516 3% /data
/dev/sda1 1038336 178064 860272 18% /boot
tmpfs 186328 12 186316 1% /run/user/42
tmpfs 186328 0 186328 0% /run/user/0
/dev/mapper/vg0-lv0 10475520 32992 10442528 1% /lvtest
要長久保存掛載,要將掛載信息寫入/etc/fstab文件中
擴展邏輯卷
邏輯卷的擴展需要所在卷組有多餘空間的情況下進行擴展。如果沒有,先擴展卷組。
[root@centos7 ~]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv0 vg0 -wi-a----- 10.00g
[root@centos7 ~]#lvextend -r -L +5G /dev/vg0/lv0
Size of logical volume vg0/lv0 changed from 10.00 GiB (2560 extents) to 15.00 GiB (3840 extents).
Logical volume vg0/lv0 successfully resized.
[root@centos7 ~]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 15.00g
[root@centos7 ~]#
擴展卷組
[root@centos7 ~]#vgs
VG #PV #LV #SN Attr VSize VFree
vg0 2 0 0 wz--n- 39.99g 39.99g
[root@centos7 ~]#vgextend vg0 /dev/sdd
Volume group "vg0" successfully extended
[root@centos7 ~]#vgs
VG #PV #LV #SN Attr VSize VFree
vg0 3 1 0 wz--n- <59.99g <20.00g
[root@centos7 ~]#
縮減邏輯卷 只有ext文件系統的邏輯卷支持縮減,有數據丟失的風險
縮減前要取消掛載,再縮減文件系統,最後縮減邏輯卷
[root@centos7 ~]#lvs /dev/vg1/lv1
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg1 -wi-a----- 24.99g
[root@centos7 ~]#e2fsck -f /dev/vg1/lv1
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/vg1/lv1: 11/1638400 files (0.0% non-contiguous), 146893/6551552 blocks
[root@centos7 ~]#resize2fs /dev/vg1/lv1 10G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg1/lv1 to 2621440 (4k) blocks.
The filesystem on /dev/vg1/lv1 is now 2621440 blocks long.
[root@centos7 ~]#lvreduce -L 10G /dev/vg1/lv1
WARNING: Reducing active logical volume to 10.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg1/lv1? [y/n]: y
Size of logical volume vg1/lv1 changed from 24.99 GiB (6398 extents) to 10.00 GiB (2560 extents).
Logical volume vg1/lv1 successfully resized
[root@centos7 ~]#lvs /dev/vg1/lv1
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg1 -wi-a----- 10.00g
跨主機遷移卷組
源計算機上
1 在舊系統中,umount所有卷組上的邏輯卷
2 禁用卷組
vgchange –a n vg0
lvdisplay
3 導出卷組
vgexport vg0
pvscan
vgdisplay
拆下舊硬盤
在目標計算機上
4 在新系統中安裝舊硬盤,並導入卷組:vgimport vg0
5 vgchange –ay vg0
啓用
6 mount所有卷組上的邏輯卷
使用LVM快照
LV的快照要求與源LV處在同一個VG中,快照大小要小於等於源LV
創建快照 -L 指定大小 -s 指定爲快照 -n 指定名字 -p r 指定爲只讀 源lv
快照爲只讀時,則無法掛載
如創建是不爲只讀快照,掛載時掛載爲只讀(mount -o nouuid ro ),防止破壞快照
[root@centos7 ~]#lvcreate -L5G -s -n lv1_snapshot -p r /dev/vg1/lv1
Logical volume "lv1_snapshot" created.
[root@centos7 ~]#
恢復快照
恢復前取消LV源,快照的掛載
[root@centos7 ~]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 39.99g
lv1 vg1 owi-a-s--- 10.00g
lv1_snapshot vg1 sri-a-s--- 5.00g lv1 0.00
取消掛載 [root@centos7 ~]#umount /lvtest
恢復快照,快照恢復後,快照文件自動刪除
[root@centos7 ~]#lvconvert --merge /dev/vg1/lv1_snapshot
Merging of volume vg1/lv1_snapshot started.
vg1/lv1: Merged: 100.00%
[root@centos7 ~]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv0 vg0 -wi-a----- 39.99g
lv1 vg1 -wi-a----- 10.00g
重新掛載 [root@centos7 ~]#mount /dev/vg1/lv1 /lvtest/