LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制,用户可以在无需停机的情况下可以方便地调整各个分区大小。
LVM名词:
物理卷(Physical Volume,PV):包含LVM信息的分区、磁盘
物理卷组(Volume Group,VG):多个物理卷组成,可以在VG上创建一个或多个LV
逻辑卷(Logical Volume,LV):创建在VG上的分区,可以在LV上创建文件系统
物理块(Physical Extent,PE):物理卷的基本单元,PV=PE*数量
创建LVM:
查看命令:
pvs|pvdisplay ##查看物理卷
vgs|vgdisplay ##查看物理卷组
lvs|lvdisplay ##查看逻辑卷
##测试前我们可以使用watch命令监控测试的变化
[root@server ~]# watch -n 1 'pvs;vgs;lvs;df -h /mnt'
##测试用的虚拟机准备了三块磁盘vdb、vdc、vdd,并且都创建了LVM分区
不会创建的可以参考https://blog.csdn.net/xin1889/article/details/80171516
[root@server ~]# parted
GNU Parted 3.1
Using /dev/vda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print list
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 10.7GB 10.7GB primary xfs boot
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 10.7GB 10.7GB primary lvm
Model: Virtio Block Device (virtblk)
Disk /dev/vdc: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 10.7GB 10.7GB primary lvm
Model: Virtio Block Device (virtblk)
Disk /dev/vdd: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 10.7GB 10.7GB primary lvm
创建LVM
创建命令:
pvcreate ##将物理分区制作成物理卷(pv)
vgcreate ##用制作好的物理卷制作一个物理卷组(vg)
lvcreate ##在物理卷组(vg)中创建lv设备,-L 指定大小,-n 指定设备名称
[root@server ~]# pvcreate /dev/vdb1 ##将分区/dev/vdb1创建成pv
Physical volume "/dev/vdb1" successfully created
[root@server ~]# vgcreate vg0 /dev/vdb1 / ##将pv制作成一个vg
Volume group "vg0" successfully created
[root@server ~]# lvcreate -L 8G -n lv0 vg0 ##在vg上创建一个lv
Logical volume "lv0" created
[root@server ~]# mkfs.xfs /dev/vg0/lv0 ##格式化
meta-data=/dev/vg0/lv0 isize=256 agcount=4, agsize=524288 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=2097152, 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@server ~]# mount /dev/vg0/lv0 /mnt/ ##挂载
####监控内容####
Every 1.0s: pvs;vgs;lvs;df -h /mnt Wed May 9 09:35:31 2018
PV VG Fmt Attr PSize PFree
/dev/vdb1 vg0 lvm2 a-- 10.00g 2.00g
VG #PV #LV #SN Attr VSize VFree
vg0 1 1 0 wz--n- 10.00g 2.00g
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 8.00g
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-lv0 8.0G 33M 8.0G 1% /mnt
扩充LVM
[root@server ~]# pvcreate /dev/vdc1 ##将/dev/vdc1制作成物理卷
Physical volume "/dev/vdd1" successfully created
[root@server ~]# vgextend vg0 /dev/vdc1 ##将/dev/vdc1添加至vg0
Volume group "vg0" successfully extended
[root@server ~]# lvextend -L 12G /dev/vg0/lv0 ##将lv0扩大至12G
Extending logical volume lv0 to 12.00 GiB
Logical volume lv0 successfully resized
[root@server ~]# xfs_growfs /dev/vg0/lv0 ##将文件系统扩大至lv0大小
meta-data=/dev/mapper/vg0-lv0 isize=256 agcount=4, agsize=524288 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=2097152, 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 2097152 to 3145728
####监控内容####
Every 1.0s: pvs;vgs;lvs;df -h /mnt Wed May 9 09:42:31 2018
PV VG Fmt Attr PSize PFree
/dev/vdb1 vg0 lvm2 a-- 10.00g 0
/dev/vdc1 vg0 lvm2 a-- 10.00g 7.99g
VG #PV #LV #SN Attr VSize VFree
vg0 2 1 0 wz--n- 19.99g 7.99g
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 12.00g
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-lv0 12G 33M 12G 1% /mnt
####xfs文件系统只能扩大,不能缩小,如果日后需要缩减,需要使用ext文件格式####
[root@server ~]# umount /dev/vg0/lv0 ##解除挂载
[root@server ~]# mkfs.ext4 /dev/vg0/lv0 ##格式化成ext4格式
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
786432 inodes, 3145728 blocks
157286 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2151677952
96 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208
[root@server ~]# mount /dev/vg0/lv0 /mnt/ ##重新挂载
[root@server ~]# lvextend -L 15G /dev/vg0/lv0 ##扩大lv0到15G大小
Extending logical volume lv0 to 15.00 GiB
Logical volume lv0 successfully resized
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
[root@server ~]# resize2fs /dev/vg0/lv0 ##扩展ext4文件系统到lv大小
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg0/lv0 is mounted on /mnt; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 2
The filesystem on /dev/vg0/lv0 is now 3932160 blocks long.
####监控内容####
Every 1.0s: pvs;vgs;lvs;df -h /mnt Wed May 9 09:53:31 2018
PV VG Fmt Attr PSize PFree
/dev/vdb1 vg0 lvm2 a-- 10.00g 0
/dev/vdc1 vg0 lvm2 a-- 10.00g 4.99g
VG #PV #LV #SN Attr VSize VFree
vg0 2 1 0 wz--n- 19.99g 4.99g
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 15.00g
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-lv0 15G 41M 14G 1% /mnt
LVM缩减
[root@server ~]# umount /dev/vg0/lv0 ##解除挂载
[root@server ~]# e2fsck -f /dev/vg0/lv0 ##扫描文件系统中已经存在的数据
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/vg0/lv0: 11/983040 files (0.0% non-contiguous), 104724/3932160 blocks
[root@server ~]# resize2fs /dev/vg0/lv0 8G ##缩减文件系统大小至8G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg0/lv0 to 2097152 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 2097152 blocks long.
[root@server ~]# mount /dev/vg0/lv0 /mnt/ ##重新挂载
[root@server ~]# lvreduce -L 8G /dev/vg0/lv0 ##缩减lv0大小至8G
WARNING: Reducing active logical volume to 8.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv0? [y/n]: y
Reducing logical volume lv0 to 8.00 GiB
Logical volume lv0 successfully resized
####监控内容####
Every 1.0s: pvs;vgs;lvs;df -h /mnt Wed May 9 10:11:15 2018
PV VG Fmt Attr PSize PFree
/dev/vdb1 vg0 lvm2 a-- 10.00g 2.00g
/dev/vdc1 vg0 lvm2 a-- 10.00g 10.00g
VG #PV #LV #SN Attr VSize VFree
vg0 2 1 0 wz--n- 19.99g 11.99g
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 8.00g
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-lv0 7.8G 37M 7.4G 1% /mnt
####如果我们需要移除/dev/vdb1,但是/dev/vdb1上面有数据,我们需要迁移数据至/dev/vdc1
[root@server ~]# pvmove /dev/vdb1 /dev/vdc1 ##迁移vdb1的数据至vdc1
/dev/vdb1: Moved: 0.4%
/dev/vdb1: Moved: 100.0%
[root@server ~]# vgreduce vg0 /dev/vdb1 ##从vg0中移除/dev/vdb1
Removed "/dev/vdb1" from volume group "vg0"
[root@server ~]# pvremove /dev/vdb1 ##从pv中移除/dev/vdb1
Labels on physical volume "/dev/vdb1" successfully wiped
####监控内容####
Every 1.0s: pvs;vgs;lvs;df -h /mnt Wed May 9 10:18:22 2018
PV VG Fmt Attr PSize PFree
/dev/vdc1 vg0 lvm2 a-- 10.00g 2.00g
VG #PV #LV #SN Attr VSize VFree
vg0 1 1 0 wz--n- 10.00g 2.00g
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 8.00g
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-lv0 7.8G 37M 7.4G 1% /mnt
LVM快照
###将vdb1重新添加至vg0###
[root@server ~]# pvcreate /dev/vdb1
Physical volume "/dev/vdb1" successfully created
[root@server ~]# vgextend vg0 /dev/vdb1
Volume group "vg0" successfully extended
[root@server ~]# touch /mnt/file{1..7} ##创建测试文件
[root@server ~]# ls /mnt
file1 file2 file3 file4 file5 file6 file7 lost+found
[root@server ~]# umount /dev/vg0/lv0 ##解除挂载
###创建快照###
[root@server ~]# lvcreate -L 2G -n lvbak -s /dev/vg0/lv0 ##创建一个2G大小的快照,名称lvbak
Logical volume "lvbak" created
[root@server ~]# mount /dev/vg0/lvbak /mnt ##挂载快照
[root@server ~]# ls /mnt ##查看测试文件
file1 file2 file3 file4 file5 file6 file7 lost+found
[root@server ~]# rm -rf /mnt/* ##删除测试文件
[root@server ~]# ls /mnt
[root@server ~]# umount /mnt ##解除挂载
[root@server ~]# lvremove /dev/vg0/lvbak ##删除快照
Do you really want to remove active logical volume lvbak? [y/n]: y
Logical volume "lvbak" successfully removed
[root@server ~]# lvcreate -L 2G -n lvbak -s /dev/vg0/lv0 ##重新创建快照
Logical volume "lvbak" created
[root@server ~]# mount /dev/vg0/lvbak /mnt ##挂载
[root@server ~]# ls /mnt ##查看
file1 file2 file3 file4 file5 file6 file7 lost+found
####监控内容####
Every 1.0s: pvs;vgs;lvs;df -h /mnt Wed May 9 10:35:16 2018
PV VG Fmt Attr PSize PFree
/dev/vdb1 vg0 lvm2 a-- 10.00g 8.00g
/dev/vdc1 vg0 lvm2 a-- 10.00g 2.00g
VG #PV #LV #SN Attr VSize VFree
vg0 2 2 1 wz--n- 19.99g 9.99g
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv0 vg0 owi-a-s--- 8.00g
lvbak vg0 swi-aos--- 2.00g lv0 0.00
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-lvbak 7.8G 37M 7.4G 1% /mnt
####创建快照设备的数据被更改,重新建立快照就可恢复数据####
磁盘配额
[root@server ~]# umount /dev/vg0/lvbak
[root@server ~]# mount -o usrquota,grpquota /dev/vg0/lvbak /mnt ##重新挂载激活配额功能参数
[root@server ~]# quotaon -ug /dev/vg0/lvba ##开启配额
[root@server ~]# edquota -u student ##编辑student用户的配额
isk quotas for user student (uid 1000):
Filesystem blocks soft hard inodes soft hard
/dev/mapper/vg0-lvbak 0 0 20M 0 0 0
[root@server ~]# su - student ##切换到student用户
Last login: Wed May 9 10:57:10 EDT 2018 on pts/0
[student@server ~]$ dd if=/dev/zero of=/mnt/studentfile bs=1M count=10 ##创建一个10M大小的测试文件
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.00391326 s, 2.7 GB/s
[student@server ~]$ ls -hl /mnt/studentfile ##查看文件
-rw-rw-r--. 1 student student 10M May 9 10:59 /mnt/studentfile
[student@server ~]$ dd if=/dev/zero of=/mnt/studentfile bs=1M count=30 ##创建一个30M大小的测试文件
dm-1: write failed, user block limit reached.
dd: error writing ‘/mnt/studentfile’: Disk quota exceeded
21+0 records in
20+0 records out
20971520 bytes (21 MB) copied, 0.029637 s, 708 MB/s
[student@server ~]$ ls -lh /mnt/studentfile
-rw-rw-r--. 1 student student 20M May 9 11:03 /mnt/studentfile ##配额20M,配额生效
###开机自动激活配额###
vim /etc/fstab
/dev/vg0/lvbak /mnt ext4 defaults,usrquota,grpquota 0 0
###关闭配额###
删除/etc/fstab中的配额参数
[root@server ~]# quotaoff -ugv /dev/vg0/lvbak ##关闭配额功能
/dev/mapper/vg0-lvbak [/mnt]: group quotas turned off
/dev/mapper/vg0-lvbak [/mnt]: user quotas turned off
删除LVM
[root@server ~]# umount /mnt ##解除挂载
[root@server ~]# lvremove /dev/vg0/lvbak ##删除lvbak
Do you really want to remove active logical volume lvbak? [y/n]: y
Logical volume "lvbak" successfully removed
[root@server ~]# lvremove /dev/vg0/lv0 ##删除lv0
Do you really want to remove active logical volume lv0? [y/n]: y
Logical volume "lv0" successfully removed
[root@server ~]# vgremove vg0 ##删除vg0
Volume group "vg0" successfully removed
[root@server ~]# pvremove /dev/vd{b,c}1 ##删除pv,/dev/vdb1和/dev/vdc1
Labels on physical volume "/dev/vdb1" successfully wiped
Labels on physical volume "/dev/vdc1" successfully wiped
###监控内容###
Every 1.0s: pvs;vgs;lvs;df -h /mnt Wed May 9 11:13:54 2018
No volume groups found
No volume groups found
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 10G 3.0G 7.1G 30% /
到此测试环境清理完毕,记得清理/etc/fstab中配置的自动挂载,否则开机时可能会出现错误。