LVM(逻辑卷管理)
1.首先就是必须准备几个空闲的分区。更改分区的系统ID。使用的是fdisk命令。下面是我分好的。怎么样分区可以查看之前的怎样分区和更改系统ID的方法点击打开链接
<span style="font-size:18px;"> Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 fd Linux raid autodetect
/dev/sdb2 2099200 4196351 1048576 fd Linux raid autodetect
/dev/sdb3 4196352 6293503 1048576 fd Linux raid autodetect
/dev/sdb4 6293504 16777215 5241856 5 Extended
/dev/sdb5 6295552 8392703 1048576 8e Linux LVM
/dev/sdb6 8394752 10491903 1048576 8e Linux LVM
/dev/sdb7 10493952 12591103 1048576 8e Linux LVM</span>
2.我是将/dev/sdb的5到7这个逻辑分区创建成Linux LVM的系统.下来就是创建PV了。顺便介绍以下与PV有关的命令。
pvcreate /dev/??? 创建PV
pvremove /dev/??? 移除PV
pvs 查看都有什么PV
pvdisplay 查看每个PV的详细信息
pvscan 扫描每个PV
pvmove 将某个PV上的信息移除.(在减小VG前的操作)
创建PV
[root@localhost ~]# pvcreate /dev/sdb{5..6}
Physical volume "/dev/sdb5" successfully created
Physical volume "/dev/sdb6" successfully created
查看一下PV的详细信息
[root@localhost ~]# pvdisplay
"/dev/sdb6" is a new physical volume of "1.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb6
VG Name
PV Size 1.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID cLQFAV-7i5L-ZANZ-Rp17-5yAu-5BVq-ma5SHZ
"/dev/sdb5" is a new physical volume of "1.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb5
VG Name
PV Size 1.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID mUDQqt-ZdFd-j3O4-at2z-l4ej-6His-c0H0eo
简单的查询PV
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb5 lvm2 --- 1.00g 1.00g
/dev/sdb6 lvm2 --- 1.00g 1.00g
扫描PV
[root@localhost ~]# pvscan
PV /dev/sdb6 lvm2 [1.00 GiB]
PV /dev/sdb5 lvm2 [1.00 GiB]
Total: 2 [2.00 GiB] / in use: 0 [0 ] / in no VG: 2 [2.00 GiB]
3.开始创建VG了。(同样介绍以下与VG相关的命令)
vgcreate <vgname> [-s 数字M] /dev/??? 创建VG(加 -s 指定PE大小。默认4M)
vgremove <vgname> 移除VG
vgextend <vgname> /dev/??? 扩展VG
vgreduce <vgname> /dev/??? 缩小VG
vgs 查看所有的vg
vgdisplay 查看vg的详细信息
创建VG
[root@localhost ~]# vgcreate myvg /dev/sdb{5..6}查看VG的详细信息。
Volume group "myvg" successfully created
[root@localhost ~]# vgdisplay myvg扩展VG
--- Volume group ---
VG Name myvg
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 1.99 GiB
PE Size 4.00 MiB
Total PE 510
Alloc PE / Size 0 / 0
Free PE / Size 510 / 1.99 GiB
VG UUID MNVQIj-GpzA-fxDq-zBeE-mO8j-EDDA-i20b3N
[root@localhost ~]# vgextend myvg /dev/sdb7缩小一个VG(从VG移除PV时一定要先将PV上的数据移到其他的PV上去,避免数据丢失)
Volume group "myvg" successfully extended
[root@localhost ~]# pvmove /dev/sdb7
No data to move for myvg
[root@localhost ~]# vgreduce myvg /dev/sdb7
Removed "/dev/sdb7" from volume group "myvg"
4.好了可以创建LV了(最终的逻辑卷)
lvcreate -n LV_name -L 卷大小 创建逻辑卷LV
lvremove /dev/vgname/lvname 移除LV
lvextend 扩大LV
lvreduce 缩小LV
lvs 查看LV都有哪些
lvdisplay 查看LV的详细信息
创建LV
[root@localhost ~]# lvcreate -n mylv -L 2000M myvg
Logical volume "mylv" created.
格式化LV,并挂在使用
[root@localhost ~]# mkfs.xfs /dev/myvg/mylv查看一下LV是否被挂载
meta-data=/dev/myvg/mylv isize=256 agcount=4, agsize=128000 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=512000, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
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@localhost ~]# mount /dev/myvg/mylv /mnt/lvm
[root@localhost ~]# df -Th查看LV的详细信息
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda3 xfs 18G 4.3G 14G 24% /
devtmpfs devtmpfs 1.4G 0 1.4G 0% /dev
tmpfs tmpfs 1.4G 144K 1.4G 1% /dev/shm
tmpfs tmpfs 1.4G 9.0M 1.4G 1% /run
tmpfs tmpfs 1.4G 0 1.4G 0% /sys/fs/cgroup
/dev/sda1 xfs 297M 199M 98M 68% /boot
/dev/md0 xfs 1020M 33M 988M 4% /mnt/md0
/dev/sr0 iso9660 4.1G 4.1G 0 100% /mnt/cdrom
tmpfs tmpfs 280M 20K 280M 1% /run/user/0
/dev/mapper/myvg-mylv xfs 2.0G 33M 2.0G 2% /mnt/lvm
[root@localhost ~]# lvdisplay /dev/myvg/mylv
--- Logical volume ---
LV Path /dev/myvg/mylv
LV Name mylv
VG Name myvg
LV UUID 6QgSCw-bDjJ-yaoJ-a7nO-IOGB-eTJe-oVObZb
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2016-10-28 17:08:20 +0800
LV Status available
# open 1
LV Size 1.95 GiB
Current LE 500
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
移除LV(移除前需要卸载)
[root@localhost ~]# lvremove /dev/myvg/mylv
Do you really want to remove active logical volume mylv? [y/n]: y
Logical volume "mylv" successfully removed
重点来啦(怎么实现LV的扩展。缩小。快照)
实现LV的扩展:
(前提:文件系统的边界不可以超越物理边界)
lv的扩展用lvextend ,文件系统的扩展用xfs_growfs(以CentOS7的默认文件系统xfs为例)
lvextend -L [+]# /PATH/VG/LV (物理边界)#代表大小
xfs_growfs /PATH/VG/LV (文件系统的边界)
下面演示一下处在挂载状态时的Lv扩展
1.先查看一下我的LVM有多大
[root@localhost lvm]# df -Th2.好了。添加PV,并加入VG中
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda3 xfs 18G 4.3G 14G 24% /
devtmpfs devtmpfs 1.4G 0 1.4G 0% /dev
tmpfs tmpfs 1.4G 144K 1.4G 1% /dev/shm
tmpfs tmpfs 1.4G 9.0M 1.4G 1% /run
tmpfs tmpfs 1.4G 0 1.4G 0% /sys/fs/cgroup
/dev/sda1 xfs 297M 199M 98M 68% /boot
/dev/md0 xfs 1020M 33M 988M 4% /mnt/md0
/dev/sr0 iso9660 4.1G 4.1G 0 100% /mnt/cdrom
tmpfs tmpfs 280M 20K 280M 1% /run/user/0
/dev/mapper/myvg-mylv xfs 2.0G 33M 2.0G 2% /mnt/lvm
[root@localhost lvm]# pvs3.将VG的添加至LV。使得LV的物理边界扩大
PV VG Fmt Attr PSize PFree
/dev/sdb5 myvg lvm2 a-- 1020.00m 0
/dev/sdb6 myvg lvm2 a-- 1020.00m 40.00m
/dev/sdb7 lvm2 --- 1.00g 1.00g
[root@localhost lvm]# vgextend myvg /dev/sdb7
Volume group "myvg" successfully extended
[root@localhost lvm]# lvextend -L +1G /dev/myvg/mylv4.扩展文件系统的边界。
Size of logical volume myvg/mylv changed from 1.95 GiB (500 extents) to 2.95 GiB (756 extents).
Logical volume mylv successfully resized.
[root@localhost lvm]# xfs_growfs /dev/myvg/mylv5.查看。看吧。文件系统在没卸载的情况下被扩展成了3G
meta-data=/dev/mapper/myvg-mylv isize=256 agcount=4, agsize=128000 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=512000, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 512000 to 774144
[root@localhost lvm]# df -Th6.(注意,这个扩展过程不威胁该文件系统的原有数据)
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda3 xfs 18G 4.3G 14G 24% /
devtmpfs devtmpfs 1.4G 0 1.4G 0% /dev
tmpfs tmpfs 1.4G 144K 1.4G 1% /dev/shm
tmpfs tmpfs 1.4G 9.0M 1.4G 1% /run
tmpfs tmpfs 1.4G 0 1.4G 0% /sys/fs/cgroup
/dev/sda1 xfs 297M 199M 98M 68% /boot
/dev/md0 xfs 1020M 33M 988M 4% /mnt/md0
/dev/sr0 iso9660 4.1G 4.1G 0 100% /mnt/cdrom
tmpfs tmpfs 280M 20K 280M 1% /run/user/0
/dev/mapper/myvg-mylv xfs 3.0G 33M 3.0G 2% /mnt/lvm
下面LVM的缩小
注意:1.不能在线缩小,缩小前需要卸载
2.确保缩小后的空间大小依然可以存放文件系统
3.在缩小前应该强行检查文件,以确保文件系统处于一致性。
但是很不幸CentOS的xfs 似乎不能被缩小,所以我又重新将/dev/myvg/mylv格式化为ext4来做这次演示
1.看看现在的大小
[root@localhost mnt]# df -Th2.先卸载,再强制检查文件
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda3 xfs 18G 4.3G 14G 24% /
devtmpfs devtmpfs 1.4G 0 1.4G 0% /dev
tmpfs tmpfs 1.4G 528K 1.4G 1% /dev/shm
tmpfs tmpfs 1.4G 9.0M 1.4G 1% /run
tmpfs tmpfs 1.4G 0 1.4G 0% /sys/fs/cgroup
/dev/sda1 xfs 297M 199M 98M 68% /boot
/dev/md0 xfs 1020M 33M 988M 4% /mnt/md0
/dev/sr0 iso9660 4.1G 4.1G 0 100% /mnt/cdrom
tmpfs tmpfs 280M 28K 280M 1% /run/user/0
/dev/mapper/myvg-mylv ext4 2.4G 6.0M 2.3G 1% /mnt/lvm
[root@localhost mnt]# umount /dev/myvg/mylv3.缩小文件系统
[root@localhost mnt]# e2fsck -f /dev/myvg/mylv
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/myvg/mylv: 11/163840 files (0.0% non-contiguous), 28212/655360 blocks
[root@localhost mnt]# resize2fs /dev/myvg/mylv 1G4.缩小物理边界
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/myvg/mylv to 262144 (4k) blocks.
The filesystem on /dev/myvg/mylv is now 262144 blocks long.
[root@localhost mnt]# lvreduce -L 1G /dev/myvg/mylv5.查看文件系统大小(真的缩小了)
WARNING: Reducing active logical volume to 1.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce mylv? [y/n]: y
Size of logical volume myvg/mylv changed from 2.50 GiB (640 extents) to 1.00 GiB (256 extents).
Logical volume mylv successfully resized.
[root@localhost mnt]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda3 xfs 18G 4.3G 14G 24% /
devtmpfs devtmpfs 1.4G 0 1.4G 0% /dev
tmpfs tmpfs 1.4G 528K 1.4G 1% /dev/shm
tmpfs tmpfs 1.4G 9.0M 1.4G 1% /run
tmpfs tmpfs 1.4G 0 1.4G 0% /sys/fs/cgroup
/dev/sda1 xfs 297M 199M 98M 68% /boot
/dev/md0 xfs 1020M 33M 988M 4% /mnt/md0
/dev/sr0 iso9660 4.1G 4.1G 0 100% /mnt/cdrom
tmpfs tmpfs 280M 28K 280M 1% /run/user/0
/dev/mapper/myvg-mylv ext4 944M 5.1M 874M 1% /mnt/lvm
LVM的快照卷
1.生命周期内为整个数据时长,数据增长量不能超出快照卷的大小
2.快照卷的创建需要创建成只读模式
3.快照卷和原卷在同一个卷组内
lvcreate
-s
-p r|w
给原文件加点东西再拍快照
[root@localhost lvm]# ls
issue login.defs lost+found
lvcreate -L # -n SLV_NAME -p r /PATH/VG/LV
[root@localhost mnt]# lvcreate -L 50M -n mylv_snap -s -p r /dev/myvg/mylv改变一下原磁盘内容
Rounding up size to full physical extent 52.00 MiB
Logical volume "mylv_snap" created.
[root@localhost lvm]# ls
issue login.defs lost+found
[root@localhost lvm]# rm issue -f
[root@localhost lvm]# echo "王八蛋" >> login.defs
再挂载起来。
[root@localhost mnt]# mkdir /mnt/snap进入挂载目录发现源文件都还在。而且login.defs这个文件还在
[root@localhost mnt]# mount /dev/myvg/mylv_snap !*
mount /dev/myvg/mylv_snap /mnt/snap
mount: /dev/mapper/myvg-mylv_snap is write-protected, mounting read-only
[root@localhost mnt]#
[root@localhost lvm]# cd /mnt/snap/
[root@localhost snap]# ls
issue login.defs lost+found
[root@localhost snap]# tail login.defs
UMASK 077
# This enables userdel to remove user groups if no members exist.
#
USERGROUPS_ENAB yes
# Use SHA512 to encrypt password.
ENCRYPT_METHOD MD5
MD5_CRYPT_ENAB yes
恢复快照卷的操作
1.进入快照目录。打包其中的内容
[root@localhost snap]# tar jcf /mnt/snap.tar.bz2 issue login.defs注意。快照是只读的。打包路径别放到快照这了。
[root@localhost snap]# cd ../
2.卸载快照卷。并删除
[root@localhost mnt]# umount /mnt/snap
[root@localhost mnt]# lvremove /dev/myvg/mylv-snap
Do you really want to remove active logical volume mylv-snap? [y/n]: y
Logical volume "mylv-snap" successfully removed
3.解压缩至原LV的目录(还原)
[root@localhost lvm]# ls
login.defs lost+found
[root@localhost lvm]# tar xf /mnt/snap.tar.bz2 -C .
[root@localhost lvm]# ls
issue login.defs lost+found
OK拉