3.4 創建物理卷的范例:
直接在硬盤設備上創建物理卷(PV):
pvcreate /dev/sdb;
pvcreate /dev/sdc;
pvcreate /dev/sdd;
pvcreate /dev/sde;
在磁盤分區(類型8e)上創建物理卷(PV):
pvcreate /dev/hda1;
pvcreate /dev/hdb1;
pvcreate /dev/hdc1;
pvscan; #掃描看看剛創建的PV;
- - - - - - - - - - - - - - - - - - - - - - - - - - -
PV /dev/hda1 lvm2 [80.00 GB]
PV /dev/hdc1 lvm2 [80.00 GB]
PV /dev/hdb1 lvm2 [80.00 GB]
Total: 3 [240.00 GB] / in use: 0 [0 ] / in no VG: 3 [240.00 GB]
- - - - - - - - - - - - - - - - - - - - - - - - - - -
pvdisplay /dev/hda1; #顯示物理卷信息;
- - - - - - - - - - - - - - - - - - - - - - - - - - -
--- Physical volume ---
PV Name /dev/hda1
VG Name vg1
PV Size 80.00 GB / not usable 0
Allocatable yes
PE Size (KByte) 4096
Total PE 20479
Free PE 20479
Allocated PE 0
PV UUID sfEXEg-xYSz-NE3C-Timx-s0og-tyAh-Rgb3s6
- - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3.5 創建卷組的范例:
vgcreate vg1 /dev/hda1; #創建包含一個物理卷的卷組;
vgcreate vg2 /dev/hdb1 /dev/hdc1; #一個卷組可以包含多個物理卷;
vgscan; #掃描看看剛創建的卷組;
- - - - - - - - - - - - - - - - - - - - - - - - - - -
Reading all physical volumes. This may take a while...
Found volume group "vg2" using metadata type lvm2
Found volume group "vg1" using metadata type lvm2
- - - - - - - - - - - - - - - - - - - - - - - - - - -
vgdisplay -v vg2; #顯示卷組vg2的信息;
- - - - - - - - - - - - - - - - - - - - - - - - - - -
Using volume group(s) on command line
Finding volume group "vg2"
--- Volume group ---
VG Name vg2
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 159.99 GB
PE Size 4.00 MB
Total PE 40958
Alloc PE / Size 0 / 0
Free PE / Size 40958 / 159.99 GB
VG UUID m2BEoc-liXS-h3dn-L4tH-ORF7-bG5p-Yx052h
--- Physical volumes ---
PV Name /dev/hdb1
PV UUID 6rdZod-Cprh-a9tS-zCK2-5DVx-hkFC-7ndR04
PV Status allocatable
Total PE / Free PE 20479 / 20479
PV Name /dev/hdc1
PV UUID 1coCsZ-BFCW-kYmJ-uCfG-jtol-WqjC-auA4zX
PV Status allocatable
Total PE / Free PE 20479 / 20479
- - - - - - - - - - - - - - - - - - - - - - - - - - -
vgextend vg1 /dev/hdd1; #將物理卷/dev/hdd添加到卷組vg1中;
pvmove /dev/hdd1; #準備從卷組vg1中移除物理卷/dve/hdd,先騰空該物理卷的數據;
vgreduce vg1 /dev/hdd1; #將物理卷/dev/hdd移出卷組vg1;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3.6 創建邏輯卷的范例:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
lvcreate -L 100M -n lv1 vg1;
lvcreate -L 500M -n lv2 vg1;
lvcreate -L 100G -n lv1 vg2;
lvcreate -L 500M -n lv2 vg2;
ls -l /dev/vg*; #創建邏輯卷(LV)後,會自動產生設備節點;
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
/dev/vg1:
total 0
lrwxrwxrwx 1 root root 19 Aug 8 01:08 lv1 -> /dev/mapper/vg1-lv1
lrwxrwxrwx 1 root root 19 Aug 8 01:09 lv2 -> /dev/mapper/vg1-lv2
/dev/vg2:
total 0
lrwxrwxrwx 1 root root 19 Aug 8 01:08 lv1 -> /dev/mapper/vg2-lv1
lrwxrwxrwx 1 root root 19 Aug 8 01:10 lv2 -> /dev/mapper/vg2-lv2
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
lvscan; #掃描看看剛創建的邏輯卷;
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
ACTIVE '/dev/vg2/lv1' [100.00 GB] inherit
ACTIVE '/dev/vg2/lv2' [500.00 MB] inherit
ACTIVE '/dev/vg1/lv1' [100.00 MB] inherit
ACTIVE '/dev/vg1/lv2' [500.00 MB] inherit
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
lvdisplay /dev/vg2/lv1; #顯示邏輯卷信息;
- - - - - - - - - - - - - - - - - - - - - - - - - - -
--- Logical volume ---
LV Name /dev/vg2/lv1
VG Name vg2
LV UUID 0Y3o6N-pVlw-gFd9-8zSH-nS0D-27eH-2u4GU4
LV Write Access read/write
LV Status available
# open 0
LV Size 100.00 GB
Current LE 25600
Segments 2
Allocation inherit
Read ahead sectors 0
Block device 253:1
- - - - - - - - - - - - - - - - - - - - - - - - - - -
lvdisplay vg2; #顯示卷組vg2中的所有邏輯卷信息;
- - - - - - - - - - - - - - - - - - - - - - - - - - -
--- Logical volume ---
LV Name /dev/vg2/lv1
VG Name vg2
LV UUID 0Y3o6N-pVlw-gFd9-8zSH-nS0D-27eH-2u4GU4
LV Write Access read/write
LV Status available
# open 0
LV Size 100.00 GB
Current LE 25600
Segments 2
Allocation inherit
Read ahead sectors 0
Block device 253:0
--- Logical volume ---
LV Name /dev/vg2/lv2
VG Name vg2
LV UUID UkUiTC-BO5l-3om3-VY1N-6pG2-AWpe-6zKV8X
LV Write Access read/write
LV Status available
# open 0
LV Size 500.00 MB
Current LE 125
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 253:1
- - - - - - - - - - - - - - - - - - - - - - - - - - -
擴大空間:
lvextend -L +500M vg1/lv1; #添加500M空間到邏輯卷vg1/lv1;
e2fsck -f /dev/vg1/lv1; #先掃描一下掃描文件系統;
resize2fs /dev/vg1/lv1; #擴大文件系統;
縮小空間:
e2fsck -f /dev/vg1/lv1; #先掃描一下掃描文件系統;
resize2fs /dev/vg1/lv1 700M; #縮小空間,新空間等于700M;
lvreduce -L -200M vg1/lv1; #邏輯卷空間減少200M(因為原先的總空間為900M);
lvreduce -L 700M vg1/lv1; #也可以直接將邏輯卷空間變更為700M;
resize2fs /dev/vg1/lv1; #重新調整文件系統的空間;
注意:
可以不創建文件系統而直接使用邏輯卷LV,直接進行數據讀寫操作以提高性能. 也就是說可以
將邏輯卷當作裸设备(raw device)來使用,例如有些數據庫系統就是利用這種方式來儲存資料.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3.7 其他LVM的操作范例:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
如欲保存數據,需先騰空相關物理卷的數據,例如:
pvmove /dev/hda1; #騰空物理卷/dev/hda數據;
先刪除卷組中的所有邏輯卷:
lvremove vg1/lv1; #刪除邏輯卷vg1/lv1;
lvremove /dev/vg1/lv2; #刪除邏輯卷vg1/lv2;
lvremove vg2/lv1; #刪除邏輯卷vg2/lv1;
將物理卷移出卷組:
vgreduce vg2 /dev/hdb1; #從卷組中移出物理卷;
將物理卷加入卷組:
vgextend vg2 /dev/hdb1; #將物理卷添加到卷組;
如果要刪除卷組,請按如下步驟:
vgchange -a n vg1; #刪除卷組前需要先停止該卷組的活動狀態;
vgremove vg1; #然后再刪除卷組;
注意: 如要刪除整個卷組,需先刪除該卷組的所有邏輯卷;
最后刪除物理卷:
pvremove /dev/hda1; #此命令只在LVM2版本中才有;
維護操作步驟:
vgscan; #掃描卷組的物理卷和邏輯卷狀態信息;
vgchange -ay; #啟動LVM;
vgchange -an; #停止LVM
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3.8 LVM快照
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
快照(snapshot)是LVM所提供的一個非常有用的特性. 利用快照您可以凍結一個正在使用中的
邏輯卷,然后制作一份凍結時刻的備份,這個備份是具有一致性的. 快照是特殊類型的邏輯卷,
它含有創建時刻所指定的原始邏輯卷的完整數據,因此您可以操作快照而無需擔心數據的變更
令備份失效.
lsmod | grep dm; #使用snapshot需要內核模塊dm-snapshot;
modprobe dm-snapshot; #如有需要,必須先加載此模塊;
建立邏輯卷vg1/lv1的快照:
lvcreate -L500M -s -n sh1 vg1/lv1;
lvscan; #看看剛創建的快照;
- - - - - - - - - - - - - - - - - - - - - - - - - - -
ACTIVE Original '/dev/vg1/lv1' [1.00 GB] inherit
ACTIVE '/dev/vg1/lv2' [2.00 GB] inherit
ACTIVE '/dev/vg1/lv3' [3.00 GB] inherit
ACTIVE Snapshot '/dev/vg1/sh1' [500.00 MB] inherit
- - - - - - - - - - - - - - - - - - - - - - - - - - -
快照卷無需做格式化步驟,可以直接將快照卷掛載,卸載等操作,而且操作完成後,可以立即刪除
快照,因為快照不會自動更新,長久保留是沒有意義的:
lvremove /dev/vg1/sh1; #刪除快照;
注意: 創建快照卷的容量必須大于原始卷的實際數據總量(通常是1.1倍);
通常創建快照卷的主要目的是做備份用途, 可以寫一個簡單的備份腳本來自動創建快照, 然后
掛載進行備份, 備份完成後又自動卸載和刪除. 因為快照的創建速度非常快, 所以使用快照來
操作備份作業不僅可以解決一致性問題, 而且可以減少備份時間. 請參考如下代碼:
vi snapshot_backup.sh; #建立備份腳本;
- - - - - - - - - - - - - - - - - - - - - - - - - - -
#!/bin/bash
today=`date "+%Y%m%d"`;
lvcreate -L1G -s -n sh1 /dev/vg1/lv1;
mount /dev/vg1/sh1 /disk/vg1/sh1;
tar -zcvf /tmp/snapshot_backup_$today.tar.gz /disk/vg1/sh1/*;
umount /dev/vg1/sh1;
lvremove -f /dev/vg1/sh1;
- - - - - - - - - - - - - - - - - - - - - - - - - - -
chmod 755 snapshot_backup.sh; #設置運行權限;
可以將此備份腳本設置成每天定時運行,就可當成是一個完整備份的方案.