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个
[root@centos ~]# vgdisplay
--- Volume group ---
VG Name testvg
System ID
Format lvm2
Metadata Areas 4
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 4
Act PV 4
VG Size 4.00 GiB
PE Size 16.00 MiB
Total PE 256
Alloc PE / Size 0 / 0
Free PE / Size 256 / 4.00 GiB PE块和容量都增加了
VG UUID 9KKogl-sH9a-SV64-RALb-LVx5-n9FO-VtFA1D
三:在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
PV VG Fmt Attr PSize PFree
/dev/sda2 vg_centos lvm2 a-- 19.51g 0
/dev/sdb10 testvg lvm2 a-- 3.92g 3.92g 已经添加到卷组testvg了
/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
(4) 利用vgdisplay查看增加的块
# vgdisplay
--- Volume group ---
VG Name testvg
System ID
Format lvm2
Metadata Areas 5
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 5
Act PV 5
VG Size 7.92 GiB
PE Size 16.00 MiB
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
VG Name testvg
PV Size 1.01 GiB / not usable 11.41 MiB
Allocatable yes (but full)
PE Size 16.00 MiB
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
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/testvg-testlv
5.8G 8.0M 5.5G 1% /mnt/lvm
(7)查看PV上的空闲块
[root@centos lvm]# pvdisplay
--- Physical volume ---
PV Name /dev/sdb10
VG Name testvg
PV Size 3.93 GiB / not usable 8.06 MiB
Allocatable yes
PE Size 16.00 MiB
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了
[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]
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).
Logical volume testlv successfully resized
快照的建立:
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
--- Volume group ---
VG Name testvg
System ID
Format lvm2
Metadata Areas 5
Metadata Sequence No 20
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 5
Act PV 5
VG Size 7.92 GiB
PE Size 16.00 MiB
Total PE 507
Alloc PE / Size 443 / 6.92 GiB
Free PE / Size 64 / 1.00 GiB
VG UUID 9KKogl-sH9a-SV64-RALb-LVx5-n9FO-VtFA1D
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
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/testvg-testlv
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中的文件,再次挂载上快照,会发现不一样了
[root@centos lvm]# mount /dev/testvg/testss /mnt/snapshot/
[root@centos lvm]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/testvg-testlv
6.7G 628M 5.8G 10% /mnt/lvm
/dev/mapper/testvg-testss
6.7G 45M 6.3G 1% /mnt/snapshot
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 ~]# umount /mnt/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/