[root@CentOS ~]# fdisk /dev/sdb
# 新建/dev/sdb上的四个分区:
Device Boot Start End Blocks Id System
/dev/sdb1 1 262 2104483+ 83 Linux
/dev/sdb2 263 524 2104515 83 Linux
/dev/sdb3 525 786 2104515 83 Linux
/dev/sdb4 787 2610 14651280 5 Extended
/dev/sdb5 787 1048 2104483+ 83 Linux
# 修改LVM格式:
Device Boot Start End Blocks Id System
/dev/sdb1 1 262 2104483+ 8e Linux LVM
/dev/sdb2 263 524 2104515 8e Linux LVM
/dev/sdb3 525 786 2104515 8e Linux LVM
/dev/sdb4 787 2610 14651280 5 Extended
/dev/sdb5 787 1048 2104483+ 8e Linux LVM
[root@CentOS ~]# partprobe /dev/sdbg
(2)PV阶段
与 PV 有关的指令:
pvcreate :将实体 partition 创建成为 PV ;
pvscan :搜寻目前系统里面任何具有 PV 的磁盘;
pvdisplay :显示出目前系统上面的 PV 状态;
pvremove :将 PV 属性移除,让该 partition 不具有 PV 属性。
```
# 检查是否存在PV
[root@CentOS ~]# pvscan
No matching physical volumes found
# 创建PV
[root@CentOS ~]# pvcreate /dev/sdb{1,2,3,5}
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdb2" successfully created
Physical volume "/dev/sdb3" successfully created
Physical volume "/dev/sdb5" successfully created
# 检查是否存在PV
[root@CentOS ~]# pvscan
PV /dev/sdb1 lvm2 [2.01 GiB]
PV /dev/sdb2 lvm2 [2.01 GiB]
PV /dev/sdb3 lvm2 [2.01 GiB]
PV /dev/sdb5 lvm2 [2.01 GiB]
Total: 4 [8.03 GiB] / in use: 0 [0 ] / in no VG: 4 [8.03 GiB]
# 详细列出每个PV的信息
[root@CentOS ~]# pvdisplay
"/dev/sdb1" is a new physical volume of "2.01 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 2.01 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID QTrS2e-1b6T-qjCV-3HDG-PcR0-dOeM-DV1V9W
```
(3)VG阶段
与VG有关的命令:
vgcreate :就是主要创建 VG 的指令啦!他的参数比较多,等一下介绍。
vgscan :搜寻系统上面是否有 VG 存在
vgdisplay :显示目前系统上面的 VG 状态;
vgextend :在 VG 内增加额外的 PV ;
vgreduce :在 VG 内移除 PV;
vgchange :设置 VG 是否启动 (active);
vgremove :删除一个 VG
与PV不同的是,VG的名字是自定义的,PV的分区是分区的设备文件名,VG就是组合的大磁盘,名称需要自定义。
```
# vgcreate -s 后面接PE的大小。单位可以使m,g,t(大小写均可)
# 将/dev/sdb1-3新建一个VG,且指定PE为16MB
[root@CentOS ~]# vgcreate -s 16M kevinvg /dev/sdb{1,2,3}
Volume group "kevinvg" successfully created
# 检查是否存在VG
[root@CentOS ~]# vgscan
Reading all physical volumes. This may take a while...
Found volume group "kevinvg" using metadata type lvm2
# 检查PV(用掉三个PV,剩余一个)
[root@CentOS ~]# vgscan
Reading all physical volumes. This may take a while...
Found volume group "kevinvg" using metadata type lvm2
[root@CentOS ~]# pvscan
PV /dev/sdb1 VG kevinvg lvm2 [2.00 GiB / 2.00 GiB free]
PV /dev/sdb2 VG kevinvg lvm2 [2.00 GiB / 2.00 GiB free]
PV /dev/sdb3 VG kevinvg lvm2 [2.00 GiB / 2.00 GiB free]
PV /dev/sdb5 lvm2 [2.01 GiB]
Total: 4 [8.01 GiB] / in use: 3 [6.00 GiB] / in no VG: 1 [2.01 GiB]
# 显示VG状态
[root@CentOS ~]# vgdisplay
--- Volume group ---
VG Name kevinvg
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 6.00 GiB
PE Size 16.00 MiB
Total PE 384
Alloc PE / Size 0 / 0
Free PE / Size 384 / 6.00 GiB
VG UUID lyvGeK-tLmy-OCx9-wnVN-EQis-TJSX-I9Ykut
# 假设要给kevinvg这个VG增加容量(使用LVM格式的/dev/sdb5):
[root@CentOS ~]# vgextend kevinvg /dev/sdb5
Volume group "kevinvg" successfully extended
# 显示VG状态
[root@CentOS ~]# vgdisplay
--- Volume group ---
VG Name kevinvg
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 8.00 GiB
PE Size 16.00 MiB
Total PE 512
Alloc PE / Size 0 / 0
Free PE / Size 512 / 8.00 GiB
VG UUID lyvGeK-tLmy-OCx9-wnVN-EQis-TJSX-I9Ykut
```
(4)LV阶段
创造出 VG 这个大磁盘之后,再来就是要创建分区区啦!这个分的区就是所谓的 LV 。假设我要将刚刚那个 kevinvg 磁盘,分区成为 kevinlv ,整个 VG 的容量都被分配到 vbirdlv 里面去。
与LV有关的命令
lvcreate :创建 LV 啦!
lvscan :查询系统上面的 LV ;
lvdisplay :显示系统上面的 LV 状态啊!
lvextend :在 LV 里面增加容量!
lvreduce :在 LV 里面减少容量;
lvremove :删除一个 LV !
lvresize :对 LV 进行容量大小的调整!
```
# 删除LV用法:lvremove /dev/kevinvg/kevinlv
# lvcreate -L:后面接容量(M,G,T),最小单位PE,必须是PE的倍数
# lvcreate -l:后面接PE的个数,需要自行计算PE数量
# lvcreate -n:后面接LV的名称
# 将整个kevinvg全部分给kevinlv,注意PE共有512个(vgdisplay查看)
[root@CentOS ~]# lvcreate -l 512 -n kevinlv kevinvg
Logical volume "kevinlv" created
# PE为16M,共512个,总大小8G,故也可以使用如下命令:
# lvcreate -L 8G -n kevinlv kevinvg
# 查看kevinlv设备文件(显示连接文件)
[root@CentOS ~]# ll /dev/kevinvg/kevinlv
lrwxrwxrwx. 1 root root 7 Mar 19 00:07 /dev/kevinvg/kevinlv -> ../dm-0
# 查看lv状态
[root@CentOS ~]# lvdisplay
--- Logical volume ---
LV Path /dev/kevinvg/kevinlv
LV Name kevinlv
VG Name kevinvg
LV UUID 1S91aJ-gdgu-lqKS-NtG8-sIhr-xTbn-O0kgJM
LV Write Access read/write
LV Creation host, time CentOS, 2017-03-19 00:07:30 -0400
LV Status available
# open 0
LV Size 8.00 GiB # LV容量
Current LE 512
Segments 4
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
```
要特别注意的是, VG 的名称为 vbirdvg , 但是 LV 的名称必须使用全名!亦即是 /dev/vbirdvg/vbirdlv 才对! 后续的处理都是这样的!这点初次接触 LVM 的朋友很容易搞错!
(5)格式化挂载文件系统阶段
```
# 格式化LV(需要全名)
[root@CentOS ~]# mkfs -t ext3 /dev/kevinvg/kevinlv
# 挂载LV(需要全名)
[root@CentOS ~]# mkdir /mnt/lvm
[root@CentOS ~]# mount /dev/kevinvg/kevinlv /mnt/lvm/
[root@CentOS ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.9G 7.0G 2.4G 75% /
tmpfs 504M 84K 504M 1% /dev/shm
/dev/sda1 194M 28M 157M 15% /boot
/dev/sda3 9.9G 1.5G 7.9G 16% /home
/dev/mapper/kevinvg-kevinlv 7.9G 147M 7.4G 2% /mnt/lvm
# 复制文件
[root@CentOS ~]# cp -a /etc /var/log/ /mnt/lvm
```
LV的名称构建成/dev/kevinvg/kevinlv是为了让用户能够直观地找到我们所需要的数据,实际上LVM使用的设备放置在/dev/mapper目录下。
放大LV容量
具体流程:
(1)用fdisk设置新的具有8e system ID的分区
(2)利用pvcreate创建PV
(3)利用vgentend将PV加入我们的kevinvg
(4)利用lvreszie将新加入的PV内的PE加入kevinlv
(5)通过resize2fs将文件系统的容量确实增加
文件系统格式化的时候新建的是多个block group,因此我们可以通过文件系统当中增加block group的方式来增减文件系统的量,而增减block group就是利用resize2fs。
重启后出现无法创建的情况,后来发现是自动开启RAID(/dev/md127)
# 使用fdisk创建一个新的大小为3G的LVM分区(/dev/sdb6):
[root@CentOS ~]# fdisk /dev/sdb
Device Boot Start End Blocks Id
/dev/sdb6 1049 1441 3156741 8e Linux LVM
[root@CentOS ~]# partprobe /dev/sdb
[root@CentOS ~]# fdisk -l
# 新建新的PV
[root@CentOS ~]# pvcreate /dev/sdb6
Physical volume "/dev/sdb6" successfully created
# 查看PV状态
[root@CentOS ~]# pvscan
PV /dev/sdb1 VG kevinvg lvm2 [2.00 GiB / 0 free]
PV /dev/sdb2 VG kevinvg lvm2 [2.00 GiB / 0 free]
PV /dev/sdb3 VG kevinvg lvm2 [2.00 GiB / 0 free]
PV /dev/sdb5 VG kevinvg lvm2 [2.00 GiB / 0 free]
PV /dev/sdb6 lvm2 [3.01 GiB]
Total: 5 [11.01 GiB] / in use: 4 [8.00 GiB] / in no VG: 1 [3.01 GiB]
# 放大VG
[root@CentOS ~]# vgextend kevinvg /dev/sdb6
Volume group "kevinvg" successfully extended
# 查看pv状态
[root@CentOS ~]# pvdisplay
# 查看VG状态
[root@CentOS ~]# vgdisplay
--- Volume group ---
VG Name kevinvg
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 0
Max PV 0
Cur PV 5
Act PV 5
VG Size 11.00 GiB
PE Size 16.00 MiB
Total PE 704
Alloc PE / Size 512 / 8.00 GiB
Free PE / Size 192 / 3.00 GiB
VG UUID lyvGeK-tLmy-OCx9-wnVN-EQis-TJSX-I9Ykut
# 放大LV(查看上面空闲192个PE)
[root@CentOS ~]# lvresize -l +192 /dev/kevinvg/kevinlv
Extending logical volume kevinlv to 11.00 GiB
Logical volume kevinlv successfully resized
# 查看lv状态
[root@CentOS ~]# lvdisplay
# 查看磁盘
[root@CentOS ~]# df /mnt/lvm/
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/kevinvg-kevinlv 8256952 200764 7636760 3% /mnt/lvm
# 注意:1K-blocks:8256952
# 查看文件系统
[root@CentOS ~]# dumpe2fs /dev/kevinvg/kevinlv
# resize2fs -f:强行进行resize操作
# 用法:resize2fs [-f] [device] [size](size必须带单位M,G)
# 完整地将整个LV人类扩充到整个文件系统
[root@CentOS ~]# resize2fs /dev/kevinvg/kevinlv
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/kevinvg/kevinlv is mounted on /mnt/lvm; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/kevinvg/kevinlv to 2883584 (4k) blocks.
The filesystem on /dev/kevinvg/kevinlv is now 2883584 blocks long.
[root@CentOS ~]# df /mnt/lvm/
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/kevinvg-kevinlv 11353328 202808 10573844 2% /mnt/lvm
# 注意:1K-blocks: 11353328
[root@CentOS ~]# ll /mnt/lvm/
total 32
drwxr-xr-x. 154 root root 12288 Mar 19 02:17 etc
drwxr-xr-x. 20 root root 4096 Mar 19 02:05 log
drwx------. 2 root root 16384 Mar 19 02:17 lost+found
# 查看文件系统
[root@CentOS ~]# dumpe2fs /dev/kevinvg/kevinlv
# 发现后续的blockgroup增加了
缩小LV容量
假设我们想将/dev/sdb6抽离出来
#先找出/dev/sdb6的容量大小,并尝试计算系统缩小到多少
# 查看PV详细信息
[root@CentOS ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sdb6
VG Name kevinvg
PV Size 3.01 GiB / not usable 10.75 MiB
Allocatable yes (but full)
PE Size 16.00 MiB
Total PE 192
Free PE 0
Allocated PE 192
PV UUID vMlc0C-KrSk-29ka-aeyw-nSVE-Q80o-LYKBte
# 内含192个PE,每个PE16M,计算得共3G大小
# 查看PV状态
[root@CentOS ~]# pvscan
PV /dev/sdb1 VG kevinvg lvm2 [2.00 GiB / 0 free]
PV /dev/sdb2 VG kevinvg lvm2 [2.00 GiB / 0 free]
PV /dev/sdb3 VG kevinvg lvm2 [2.00 GiB / 0 free]
PV /dev/sdb5 VG kevinvg lvm2 [2.00 GiB / 0 free]
PV /dev/sdb6 VG kevinvg lvm2 [3.00 GiB / 0 free]
Total: 5 [11.00 GiB] / in use: 5 [11.00 GiB] / in no VG: 0 [0 ]
# 计算得去掉/dev/sdb6后的总容量:2+2+2+2=8G
# 直接降低PV文件系统容量:
[root@CentOS ~]# resize2fs /dev/kevinvg/kevinlv 8G
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/kevinvg/kevinlv is mounted on /mnt/lvm; on-line resizing required
On-line shrinking from 2883584 to 2097152 not supported.
# 发现不可以在线修改缩小文件系统
# 卸载重新来做PV容量缩小
[root@CentOS ~]# umount /mnt/lvm/
[root@CentOS ~]# resize2fs /dev/kevinvg/kevinlv 8G
resize2fs 1.41.12 (17-May-2010)
Please run 'e2fsck -f /dev/kevinvg/kevinlv' first.
# 系统要求使用e2fsck -f 磁盘检查
[root@CentOS ~]# e2fsck -f /dev/kevinvg/kevinlv
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/kevinvg/kevinlv: 3616/720896 files (0.0% non-contiguous), 95954/2883584 blocks
# 继续缩小PV容量步骤
[root@CentOS ~]# resize2fs /dev/kevinvg/kevinlv 8G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/kevinvg/kevinlv to 2097152 (4k) blocks.
The filesystem on /dev/kevinvg/kevinlv is now 2097152 blocks long.
# 重新挂载磁盘
[root@CentOS ~]# mount /dev/kevinvg/kevinlv /mnt/lvm/
[root@CentOS ~]# df /mnt/lvm/
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/kevinvg-kevinlv 8256952 200764 7636788 3% /mnt/lvm
# 1K-blocks:8256952
# 查看/dev/sdb6这个PV上有多少PE
[root@CentOS ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sdb6
VG Name kevinvg
PV Size 3.01 GiB / not usable 10.75 MiB
Allocatable yes (but full)
PE Size 16.00 MiB
Total PE 192
Free PE 0
Allocated PE 192
PV UUID vMlc0C-KrSk-29ka-aeyw-nSVE-Q80o-LYKBte
# 降低LV容量
[root@CentOS ~]# lvresize -l 192 /dev/kevinvg/kevinlv
WARNING: Reducing active and open logical volume to 3.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce kevinlv? [y/n]: y
Reducing logical volume kevinlv to 3.00 GiB
Logical volume kevinlv successfully resized
# 查看lv详细信息
[root@CentOS ~]# lvdisplay
--- Logical volume ---
LV Path /dev/kevinvg/kevinlv
LV Name kevinlv
VG Name kevinvg
LV UUID iAQGBD-rciQ-2M93-VsFV-50yh-5LSB-pBqZXx
LV Write Access read/write
LV Creation host, time CentOS, 2017-03-19 02:14:41 -0400
LV Status available
# open 1
LV Size 3.00 GiB # LV大小
Current LE 192
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
# 将/dev/sdb6移出VG,需要先确定/defv/sdb6完全不被使用
[root@CentOS ~]# vgreduce kevinvg /dev/sdb6
Removed "/dev/sdb6" from volume group "kevinvg"
[root@CentOS ~]# pvremove /dev/sdb6
Labels on physical volume "/dev/sdb6" successfully wiped
LVM的系统快照
快照就是将当时的系统信息记录下来,就好像照相记录一般! 未来若有任何数据更动了,则原始数据会被搬移到快照区,没有被更动的区域则由快照区与文件系统共享。
LVM 的磁盘快照是非常棒的“备份工具”,因为他只有备份有被更动到的数据, 文件系统内没有被变更的数据依旧保持在原本的区块内,但是 LVM 快照功能会知道那些数据放置在哪里, 因此“快照”当时的文件系统就得以“备份”下来,且快照所占用的容量又非常小。
快照区与原本的 LV 共享很多 PE 区块,因此快照区与被快照的 LV 必须要在同一个 VG 上头。
下面我们针对传统 LV 磁盘进行快照的创建,大致流程为:
(1)预计被拿来备份的原始 LV 为 /dev/vbirdvg/vbirdlv
(2)使用传统方式快照创建,原始碟为 /dev/vbirdvg/vbirdlv,快照名称为 vbirdsnap1,容量为 vbirdvg 的所有剩余容量
(3)传统快照区的创建
# 创建快照区
# 先将de/sdb6加入到VG后,才能继续新建快照区
# 先查看VG还剩多少容量
[root@CentOS ~]# vgdisplay
Free PE / Size 0 / 0
# 若没有空余容量,则新增PV后加入VG
[root@CentOS ~]# pvcreate /dev/sdb10
Physical volume "/dev/sdb10" successfully created
[root@CentOS ~]# vgextend kevinvg /dev/sdb10
Volume group "kevinvg" successfully extended
[root@CentOS ~]# vgdisplay
Free PE / Size 128 / 2.00 GiB
# 空闲128个PE
# 创建快照区,取名kevinss,给60个PE
# lvcreate -s:快照(snapshot)
# lvcreate -n:后面接设备名
# lvcreate -l:后面接多少个PE
[root@CentOS ~]# lvcreate -l 60 -s -n kevinss /dev/kevinvg/kevinlv
Logical volume "kevinss" created
# 快照区的VG与原本的/dev/kevinvg/kevinlv容量相同
[root@CentOS ~]# lvdisplay
--- Logical volume ---
LV Path /dev/kevinvg/kevinss
LV Name kevinss
VG Name kevinvg
LV UUID ElEX59-bnfx-A8c1-9Vn5-9y27-AfRD-gmpbh5
LV Write Access read/write
LV Creation host, time CentOS, 2017-03-19 03:31:21 -0400
LV snapshot status active destination for kevinlv
LV Status available
# open 0
LV Size 9.00 GiB
Current LE 576
COW-table size 960.00 MiB
COW-table LE 60
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:1
# 如果将设备挂载时会发现两个设备数据完全相同
[root@CentOS ~]# mkdir /mnt/snapshot
[root@CentOS ~]#
[root@CentOS ~]# mount /dev/kevinvg/kevinss /mnt/snapshot/
[root@CentOS ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 10321208 8213352 1583568 84% /
tmpfs 515264 84 515180 1% /dev/shm
/dev/sda1 198337 28183 159914 15% /boot
/dev/sda3 10321208 1549940 8246980 16% /home
/dev/mapper/kevinvg-kevinlv 9289080 203060 8614164 3% /mnt/lvm
/dev/mapper/kevinvg-kevinss 9289080 203060 8614164 3% /mnt/snapshot
[root@CentOS ~]# umount /mnt/snapshot/
# 利用快照区复原系统
# 要注意的是,你要复原的数据量不能够高于快照区所能负载的实际容量。
# 将原本的/dev/kevinvh/kevinlv内容做更改,增减一些目录
[root@CentOS ~]# df /mnt/lvm/
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/kevinvg-kevinlv 9289080 203060 8614164 3% /mnt/lvm
[root@CentOS ~]# df /mnt/lvm/
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/kevinvg-kevinlv 9289080 203060 8614164 3% /mnt/lvm
[root@CentOS ~]# ll /mnt/lvm/
total 32
drwxr-xr-x. 154 root root 12288 Mar 19 03:25 etc
drwxr-xr-x. 20 root root 4096 Mar 19 02:05 log
drwx------. 2 root root 16384 Mar 19 03:25 lost+found
[root@CentOS ~]# rm -rf /mnt/lvm/log/
[root@CentOS ~]# cp -a /boot/ /lib/ /sbin/ /mnt/lvm/
[root@CentOS ~]# ll /mnt/lvm/
total 56
dr-xr-xr-x. 5 root root 4096 Mar 1 16:44 boot
drwxr-xr-x. 154 root root 12288 Mar 19 03:25 etc
dr-xr-xr-x. 20 root root 12288 Mar 1 20:23 lib
drwx------. 2 root root 16384 Mar 19 03:25 lost+found
dr-xr-xr-x. 2 root root 12288 Mar 1 20:24 sbin
[root@CentOS ~]# lvdisplay /dev/kevinvg/kevinss
Allocated to snapshot 19.46%
# 利用快照区原本的文件系统备份
[root@CentOS ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 10321208 8213320 1583600 84% /
tmpfs 515264 84 515180 1% /dev/shm
/dev/sda1 198337 28183 159914 15% /boot
/dev/sda3 10321208 1549940 8246980 16% /home
/dev/mapper/kevinvg-kevinlv 9289080 385800 8431424 5% /mnt/lvm
/dev/mapper/kevinvg-kevinss 9289080 203060 8614164 3% /mnt/snapshot
[root@CentOS ~]# mkdir -p /backups
[root@CentOS ~]# cd /mnt/snapshot/
[root@CentOS snapshot]# tar -jcv -f /backups/lvm.tar.bz2 *
# 进行还原
# 将kevinss卸除并删除
[root@CentOS ~]# umount /mnt/snapshot/
[root@CentOS ~]# lvremove /dev/kevinvg/kevinss
Do you really want to remove active logical volume kevinss? [y/n]: y
Logical volume "kevinss" successfully removed
[root@CentOS ~]# umount /mnt/lvm/
[root@CentOS ~]# mkfs -t ext4 /dev/kevinvg/kevinlv
[root@CentOS ~]# mount /dev/kevinvg/kevinlv /mnt/lvm/
[root@CentOS ~]# tar -jxv -f /backups/lvm.tar.bz2 -C /mnt/lvm/
[root@CentOS ~]# ll /mnt/lvm/
total 32
drwxr-xr-x. 154 root root 12288 Mar 19 03:25 etc
drwxr-xr-x. 20 root root 4096 Mar 19 02:05 log
drwx------. 2 root root 16384 Mar 19 03:25 lost+found
# 与最初的文件相比是相同的
利用快照区进行各项练习与测试的任务,再以原系统还原系统换个角度来想想,我们将原本的 vbirdlv 当作备份数据,然后将 vbirdsnap1 当作实际在运行中的数据, 任何测试的动作都在 vbirdsnap1 这个快照区当中测试,那么当测试完毕要将测试的数据删除时,只要将快照区删去即可! 而要复制一个 vbirdlv 的系统,再作另外一个快照区即可!这样是否非常方便啊? 这对于教学环境中每年都要帮学生制作一个练习环境主机的测试,非常有帮助
```
# 新建一个大一些的快照区,让我们将/dev/sdb10的PE全部给快照区
[root@CentOS ~]# pvscan
PV /dev/sdb6 VG kevinvg lvm2 [2.00 GiB / 0 free]
PV /dev/sdb7 VG kevinvg lvm2 [2.00 GiB / 0 free]
PV /dev/sdb8 VG kevinvg lvm2 [3.00 GiB / 0 free]
PV /dev/sdb9 VG kevinvg lvm2 [2.00 GiB / 0 free]
PV /dev/sdb10 VG kevinvg lvm2 [2.00 GiB / 2.00 GiB free]
Total: 5 [11.00 GiB] / in use: 5 [11.00 GiB] / in no VG: 0 [0 ]
[root@CentOS ~]# vgdisplay
Free PE / Size 128 / 2.00 GiB
[root@CentOS ~]# lvcreate -s -l 128 -n kevinss /dev/kevinvg/kevinlv
Logical volume "kevinss" created
[root@CentOS ~]# lvdisplay /dev/kevinvg/kevinss
Current LE 576
COW-table size 2.00 GiB
COW-table LE 128
# 隐藏kevinlv,挂载kevinss
[root@CentOS ~]# umount /mnt/lvm/
[root@CentOS ~]# mount /dev/kevinvg/kevinss /mnt/snapshot/
[root@CentOS ~]# df /mnt/snapshot/
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/kevinvg-kevinss 9289080 203188 8614036 3% /mnt/snapshot
# 开始恶搞
[root@CentOS ~]# rm -rf /mnt/snapshot/etc/ /mnt/snapshot/log/
[root@CentOS ~]# cp -a /boot/ /lib/ /sbin/ /mnt/snapshot/
[root@CentOS ~]# df /mnt/snapshot/
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/kevinvg-kevinss 9289080 342852 8474372 4% /mnt/snapshot
[root@CentOS ~]# ll /mnt/snapshot/
total 44
dr-xr-xr-x. 5 root root 4096 Mar 1 16:44 boot
dr-xr-xr-x. 20 root root 12288 Mar 1 20:23 lib
drwx------. 2 root root 16384 Mar 19 03:25 lost+found
dr-xr-xr-x. 2 root root 12288 Mar 1 20:24 sbin
[root@CentOS ~]# mount /dev/kevinvg/kevinlv /mnt/lvm/
[root@CentOS ~]# ll /mnt/lvm/
total 32
drwxr-xr-x. 154 root root 12288 Mar 19 03:25 etc
drwxr-xr-x. 20 root root 4096 Mar 19 02:05 log
drwx------. 2 root root 16384 Mar 19 03:25 lost+found
# 不论在快照区做什么,都不会影响原本的数据
# 假设快照区玩坏了,需要还原,
# 还原原本不能快照区的数据,回到与原文件系统相同的信息
[root@CentOS ~]# umount /mnt/snapshot/
[root@CentOS ~]# lvremove /dev/kevinvg/kevinss
Do you really want to remove active logical volume kevinss? [y/n]: y
Logical volume "kevinss" successfully removed
[root@CentOS ~]# lvcreate -s -l 128 -n kevinss /dev/kevinvg/kevinlv
Logical volume "kevinss" created
[root@CentOS ~]# mount /dev/kevinvg/kevinss /mnt/snapshot/
[root@CentOS ~]# ll /mnt/snapshot/
total 32
drwxr-xr-x. 154 root root 12288 Mar 19 03:25 etc
drwxr-xr-x. 20 root root 4096 Mar 19 02:05 log
drwx------. 2 root root 16384 Mar 19 03:25 lost+found
[root@CentOS ~]#
# 数据已恢复
```
LVM相关命令汇整与LVM的关闭
会玩 LVM 还不行!你必须要会移除系统内的 LVM 喔!因为你的实体 partition 已经被使用到LVM 去, 如果你还没有将 LVM 关闭就直接将那些 partition 删除或转为其他用途的话,系统是会发生很大的问题的! 所以啰,你必须要知道如何将 LVM 的设备关闭并移除才行!会不会很难呢?其实不会啦! 依据以下的流程来处理即可:
1 . 先卸载系统上面的 LVM 文件系统 (包括快照与所有 LV);
2 . 使用 lvremove 移除 LV ;
3 . 使用 vgchange -a n VGname 让 VGname 这个 VG 不具有 Active 的标志;
4 . 使用 vgremove 移除 VG:
5 . 使用 pvremove 移除 PV;
6 . 最后,使用 fdisk 修改 ID 回来。
[root@CentOS ~]# umount /mnt/lvm/
[root@CentOS ~]# umount /mnt/snapshot/
# 先移除快照
[root@CentOS ~]# lvremove /dev/kevinvg/kevinss
Do you really want to remove active logical volume kevinss? [y/n]: y
Logical volume "kevinss" successfully removed
# 再还原系统
[root@CentOS ~]# lvremove /dev/kevinvg/kevinlv
Do you really want to remove active logical volume kevinlv? [y/n]: y
Logical volume "kevinlv" successfully removed
[root@CentOS ~]# vgchange -a n kevinvg
0 logical volume(s) in volume group "kevinvg" now active
[root@CentOS ~]# vgremove kevinvg
Volume group "kevinvg" successfully removed
[root@CentOS ~]# pvremove /dev/sdb{6,7,8,9,10}
Labels on physical volume "/dev/sdb6" successfully wiped
Labels on physical volume "/dev/sdb7" successfully wiped
Labels on physical volume "/dev/sdb8" successfully wiped
Labels on physical volume "/dev/sdb9" successfully wiped
Labels on physical volume "/dev/sdb10" successfully wiped