#######################
#########lvm###########
#######################
1.pv #物理卷
vg #物理卷组
pe #物理区域,是物理卷中最小的存储单元
le #逻辑区域,是逻辑卷中最小的存储单元
lvm #逻辑卷管理
#2.lvm的建立#
划分物理分区,并修改分区标签为lvm
*pvcreate /dev/vdb1 #创建物理卷
[root@foundation180 ~]# pvcreate /dev/vdb1
Physical volume "/dev/vdb1" successfully created
*vgcreate vg0 /dev/vdb1 #创建物理卷组
[root@foundation180 ~]# vgcreate vg0 /dev/vdb1
Volume group "vg0" successfully created
*lvcreate -l 100%FREE vg0 -n lv0 #将所有空闲vg分给lv
*lvcreate -L 100M -n lv0 vg0 #创建逻辑卷
[root@foundation180 ~]# lvcreate -L 100M -n lv0 vg0
Logical volume "lv0" created
*mkfs.xfs /dev/vg0/lv0 #格式化设备
[root@foundation180 ~]# mkfs.xfs /dev/vg0/lv0
meta-data=/dev/vg0/lv0 isize=256 agcount=4, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=25600, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
*mount /dev/vg0/lv0 /westos #挂载设备
[root@foundation180 ~]# mount /dev/vg0/lv0 /westos
[root@foundation180 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3123776 7350124 30% /
devtmpfs 481120 0 481120 0% /dev
tmpfs 496708 80 496628 1% /dev/shm
tmpfs 496708 13052 483656 3% /run
tmpfs 496708 0 496708 0% /sys/fs/cgroup
/dev/mapper/vg0-lv0 98988 5280 93708 6% /westos
#监控命令
watch -n 1 'pvs;echo ===;vgs;echo ===;lvs;echo ===;df -h /westos'
#3.lvm拉伸#
当vg中剩余容量充足时(可以直接进行扩展)
*lvextend -L 400M /dev/vg0
[root@foundation180 ~]# lvextend -L 400M /dev/vg0/lv0
Extending logical volume lv0 to 400.00 MiB
Logical volume lv0 successfully resized
*xfs_grofs /dev/vg0/lv0
[root@foundation180 ~]# xfs_growfs /dev/vg0/lv0
meta-data=/dev/mapper/vg0-lv0 isize=256 agcount=4, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=25600, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 25600 to 102400
当vg中的剩余容量不足时(要先扩展物理卷组,在扩展逻辑卷)
*新建分区
*pvcreate /dev/vdb2 #创建物理卷
*vgextend vg0 /dev/vdb2 #扩展物理卷组
*lvcreate -L 700M /dev/vg0/lv0 #扩展逻辑卷
*xfs_growfs /dev/vg0/lv0
在ext文件系统中:(ext文件系统可以缩减,xfs文件系统不能缩减)
*mkfs.ext4 /dev/vg0/lv0
[root@foundation180 ~]# mkfs.ext4 /dev/vg0/lv0
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
102400 inodes, 409600 blocks
20480 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=34078720
50 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
拉伸
*lvextend -L 400M /dev/vg0/lv0
*resizen2fs /dev/vg0/lv0
缩减
对设备
*umount /westos
*e2fsck -f /dev/vg0/lv0
*resize2fs /dev/vg0/lv0 200M
*mount /dev/vg0/lv0 /westos
*lvreduce -L 200M /dev/vg0/lv0
对卷组
*pvmove /dev/vdb1 /dev/vdb2 #当需要移除的分区上有数据时,需要将数据转移到其他分区
[root@foundation180 ~]# pvmove /dev/vdb1 /dev/vdb2
/dev/vdb1: Moved: 3.0%
/dev/vdb1: Moved: 100.0%
*vgreduce vg0 /dev/vdb1 #将/dev/vdb1卷从vg0卷组中移出
[root@foundation180 ~]# vgreduce vg0 /dev/vdb1
Removed "/dev/vdb1" from volume group "vg0"
*pvremove /dev/vdb1 #删除/dev/vdb1卷
[root@foundation180 ~]# pvremove /dev/vdb1
Labels on physical volume "/dev/vdb1" successfully wiped
不论是拉伸还是缩减lv,在操作后最好都进行格式化,以保证lv的正常使用。
##lvm快照##
当设备中有重要文件时,可以对设备进行快照。然后将快照挂载。若是删除了文件,只用重新再次快照即可。这样比较安全。类似于虚拟机快照。
[root@foundation180 vg0]# lvcreate -L 100M -n lv0backup -s /dev/vg0/lv0
Logical volume "lv0backup" created
在/mnt下建立文件作为参照
假装不小心删除了文件
卸载并移除快照
重新建立快照
挂在后在此查看/mnt文件重新出现
##lvm的删除##
*lvremove /dev/vg0/lv0
*vgremove vg0
*pvremove /dev/vdb2
*fdisk /dev/vdb
##lvm分区被误删##
出现如下错误
[root@foundation180 ~]# pvs
No device found for PV t3PP25-q94H-EdOz-rOQV-kQTH-avxr-H0Lkiu.
No device found for PV t3PP25-q94H-EdOz-rOQV-kQTH-avxr-H0Lkiu.
No device found for PV t3PP25-q94H-EdOz-rOQV-kQTH-avxr-H0Lkiu.
No device found for PV t3PP25-q94H-EdOz-rOQV-kQTH-avxr-H0Lkiu.
PV VG Fmt Attr PSize PFree
/dev/vdb1 vg0 lvm2 a-- 496.00m 396.00m
unknown device vg0 lvm2 a-m 496.00m 496.00m
[root@foundation180 ~]# vgs
No device found for PV t3PP25-q94H-EdOz-rOQV-kQTH-avxr-H0Lkiu.
No device found for PV t3PP25-q94H-EdOz-rOQV-kQTH-avxr-H0Lkiu.
VG #PV #LV #SN Attr VSize VFree
vg0 2 1 0 wz-pn- 992.00m 892.00m
解决方式
vgreduce vg0 --removemissing
再次查看pv、vg、lv恢复正常。
注意:删除lv时一定要按照从lv-vg-pv一步步取消,最后再删除分区,否则就会报错。