LVM
一:建立PV
# pvcreate /dev/sdb{6,7,8,9}
# pvscan 檢視建立的PV
# pvdisplay 檢視詳細資訊
二:在pv基礎上建立VG
(1) 把/dev/sdb{6,7,8}建立一個VG,名為testvg
# vgcreate -s 16M testvg /dev/sdb{6,7,8}
# vgscan
(2) 檢視建立的vg卷
[root@centos ~]# vgdisplay
--- Volume group ---
VG Name testvg
System ID
Format lvm2
Metadata Areas 3
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 3
Act PV 3
VG Size 3.00 GiB
PE Size 16.00 MiB
Total PE 192
Alloc PE / Size 0 / 0
Free PE / Size 192 / 3.00 GiB
VG UUID 9KKogl-sH9a-SV64-RALb-LVx5-n9FO-VtFA1D
(3)把一個/sdb9增加到testvg卷
# vgextend testvg /dev/sdb9
(4)再次檢視vg卷,PE為256個
Metadata Areas 4
Metadata Sequence No 2
Cur PV 4
Act PV 4
VG Size 4.00 GiB
Total PE 256
Free PE / Size 256 / 4.00 GiB PE塊和容量都增加了
三:在VG的基礎上建立LV
(1)建立testlv,PE為256個。
# lvcreate -l 256 -n testlv testvg
(2)格式化和挂載
# mke2fs -t ext4 /dev/testvg/testlv
(3)建立挂載點,并挂載
# mkdir /mnt/lvm
# mount /dev/testvg/testlv /mnt/lvm
(4)檢視挂在後的lv
[root@centos ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/testvg-testlv
3.9G 8.0M 3.7G 1% /mnt/lvm
四:LV 擴容
(1)以sdb10 作為擴容盤,建立立一個PV。
# pvcreate /dev/sdb10
(2)檢視建立的pv
[root@centos ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 vg_centos lvm2 a-- 19.51g 0
/dev/sdb10 lvm2 --- 3.93g 3.93g
/dev/sdb6 testvg lvm2 a-- 1.00g 0
/dev/sdb7 testvg lvm2 a-- 1.00g 0
/dev/sdb8 testvg lvm2 a-- 1.00g 0
/dev/sdb9 testvg lvm2 a-- 1.00g 0
(2) 利用vgextend 把/sdb10 增加到testvg裡
# vgextend testvg /dev/sdb10
(3)再次檢視
[root@centos ~]# pvs
/dev/sdb10 testvg lvm2 a-- 3.92g 3.92g 已經添加到卷組testvg了
/dev/sdb9 testvg lvm2 a-- 1.00g 0
(4) 利用vgdisplay檢視增加的塊
# vgdisplay
Metadata Areas 5
Metadata Sequence No 4
Cur LV 1
Open LV 1
Cur PV 5
Act PV 5
VG Size 7.92 GiB
Total PE 507
Alloc PE / Size 256 / 4.41 GiB
Free PE / Size 251 / 3.92 GiB 增加的251個PE 塊
VG UUID d6YPIx-vfID-TL5H-Fmcy-nZVi-G7uq-LTVgQ0
(5)利用lvresize來增加PE塊到LV
# lvresize -l +251 /dev/testvg/testlv
Size of logical volume testvg/testlv changed from 4.00 GiB (256 extents) to 7.92 GiB (507 extents).
(6)利用 lvdisplay 檢視增加後的容量。
(7)用resize2fs 講檔案系統裡容量确實增加
# resize2fs /dev/testvg/testlv
(8)# df -h /mnt/lvm/ 增量成功。
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/testvg-testlv
7.7G 9.0M 7.3G 1% /mnt/lvm
五:縮小LV
(1)假如我們要把/dev/sdb6 抽離出來,先找出容量和PE塊數
[root@centos ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sdb6
PV Size 1.01 GiB / not usable 11.41 MiB
Allocatable yes (but full)
Total PE 64
Free PE 0
Allocated PE 64
PV UUID B3LTgT-zLCO-hseh-EdlT-jm3T-Z57d-WqesPv
(2)解除安裝lvm
[root@centos ~]# umount /mnt/lvm/
(3)強制檢查LV
[root@centos ~]# e2fsck -f /dev/testvg/testlv
e2fsck 1.41.12 (17-May-2010)
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: 11/524288 files (0.0% non-contiguous), 67983/2076672 blocks
[root@centos ~]# resize2fs /dev/testvg/testlv 6G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/testvg/testlv to 1572864 (4k) blocks.
The filesystem on /dev/testvg/testlv is now 1572864 blocks long.
(4)縮小分區到6G
[root@centos ~]# resize2fs /dev/testvg/testlv 6G 縮小到6G
這個最好總的PV減去/dev/sdb6後的大小,縮減太多容易浪費。比哪縮減到6900M,我這時縮減多了
(5)縮小LV邏輯邊界到6G
[root@centos ~]# lvreduce -L 6G /dev/testvg/testlv LV縮小到6G
WARNING: Reducing active logical volume to 6.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce testlv? [y/n]: y
Size of logical volume testvg/testlv changed from 7.92 GiB (507 extents) to 6.00 GiB (384 extents).
Logical volume testlv successfully resized
(6)再次挂載LV
[root@centos ~]# mount /dev/testvg/testlv /mnt/lvm
[root@centos ~]# df -h /mnt/lvm
5.8G 8.0M 5.5G 1% /mnt/lvm
(7)檢視PV上的空閑塊
[root@centos lvm]# pvdisplay
--- Physical volume ---
PV Name /dev/sdb10
PV Size 3.93 GiB / not usable 8.06 MiB
Allocatable yes
Total PE 251
Free PE 123 移除的空閑塊在/dev/sdb10上,這個自己注意觀察
Allocated PE 128
PV UUID GXWe7H-uNPt-mERp-gKk6-kz70-yUtj-XLXp0y
(7)把sdb6上占用塊移動到空閑塊/sdb10上
[root@centos lvm]# pvmove /dev/sdb6 /dev/sdb10
/dev/sdb6: Moved: 0.0%
/dev/sdb6: Moved: 73.4%
/dev/sdb6: Moved: 100.0%
(8)把/dev/sdb6從vg中移出去
[root@centos lvm]# vgreduce testvg /dev/sdb6 從testvg中移出去
Removed "/dev/sdb6" from volume group "testvg"
[root@centos lvm]# pvscan
PV /dev/sdb7 VG testvg lvm2 [1.00 GiB / 0 free]
PV /dev/sdb8 VG testvg lvm2 [1.00 GiB / 0 free]
PV /dev/sdb9 VG testvg lvm2 [1.00 GiB / 0 free]
PV /dev/sdb10 VG testvg lvm2 [3.92 GiB / 944.00 MiB free]
PV /dev/sda2 VG vg_centos lvm2 [19.51 GiB / 0 free]
PV /dev/sdb6 lvm2 [1.01 GiB]
(9)把/sdb6/從PV中移出
[root@centos lvm]# pvremove /dev/sdb6 從PV中移除去
Labels on physical volume "/dev/sdb6" successfully wiped
(10)在pv中已經沒有sdb6了
Total: 5 [26.43 GiB] / in use: 5 [26.43 GiB] / in no VG: 0 [0
已經沒有/dev/sdb6了
(11)pv裡剩下的塊在加回到LV裡。避免浪費。
[root@centos ~]# lvresize -l +59 /dev/testvg/testlv
Size of logical volume testvg/testlv changed from 6.00 GiB (384 extents) to 6.92 GiB (443 extents).
快照的建立:
1:檢視VG上已經沒有空閑PE塊了
2:把 /dev/sdb6添加到VG中
[root@centos ~]# pvcreate /dev/sdb6
[root@centos ~]# vgextend testvg /dev/sdb6
Volume group "testvg" successfully extended
3:檢視VG中空閑PE
[root@centos ~]# vgdisplay
Metadata Sequence No 20
Alloc PE / Size 443 / 6.92 GiB
Free PE / Size 64 / 1.00 GiB
3:建立快照區:
[root@centos ~]# lvcreate -l 64 -s -n testss /dev/testvg/testlv
Logical volume "testss" created
注解:-l :用來指定用多少PE塊
-s :用來說明使用快照功能
-n :用來指定快照區的名稱,名稱後接的LV完整路徑
4:檢視建立的快照區
[root@centos ~]# lvdisplay /dev/testvg/testss
--- Logical volume ---
LV Path /dev/testvg/testss
LV Name testss
VG Name testvg
LV UUID UI89Oh-fqtD-vNtD-LQ6v-ikGH-Vt6x-0BJK5g
LV Write Access read/write
LV Creation host, time centos, 2016-03-24 16:46:16 +0800
LV snapshot status active destination for testlv
LV Status available
# open 0
LV Size 6.92 GiB 被快照的LV磁盤容量
Current LE 443
COW-table size 1.00 GiB 快照區的大小
COW-table LE 64 快照區的PE數量
Allocated to snapshot 0.00%
Snapshot chunk size 4.00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2
5:建立挂載快照挂載點,并挂載
[root@centos lvm]# mkdir /mnt/snapshot
[root@centos lvm]# mount /dev/testvg/testss /mnt/snapshot/
6:檢視挂載同步
[root@centos lvm]# df -h
6.7G 45M 6.3G 1% /mnt/lvm
/dev/mapper/testvg-testss
6.7G 45M 6.3G 1% /mnt/snapshot
7:解除安裝挂載的快照
[root@centos lvm]# umount /mnt/snapshot/
8:測試,我添加和删除一些LVM中的檔案,再次挂載上快照,會發現不一樣了
6.7G 628M 5.8G 10% /mnt/lvm
9:我們把快照裡的檔案備份打包到/backups。
[root@centos ]# mkdir -p /backups/
[root@centos ]# cd /mnt/snapshot/
[root@centos snapshot]# tar -jcv -f /backups/lvm.tar.bz2 *
10:還原時解除安裝掉快照,并删除快照
[root@centos ~]# umount /mnt/snapshot/
[root@centos ~]# lvremove /dev/testvg/testss
Do you really want to remove active logical volume testss? [y/n]: y
Logical volume "testss" successfully removed
11:解除安裝LVM,并格式化挂載LVM。最後解壓資料
[root@centos ~]# mke2fs -t ext4 /dev/testvg/testlv
[root@centos backups]# cd /backups/
[root@centos backups]# tar -jxf lvm.tar.bz2 -C /mnt/lvm/
12:最後重新挂載LVM和快照,資料就會同步了。

快照損壞的再次同步LVM就行了
# umount /mnt/snapshot
# lvremove /dev/testvg/testss/
檢視VG中的空閑PE
# vgdisplay
# lvcreate -s -l 64 -n testss /dev/testvg/testlv
# mount /dev/testvg/testss /mnt/snapshot/
# ll /mnt/snapshot/