LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。
物理卷(PV, Physical Volume)
物理卷就是指磁盘,磁盘分区或从逻辑上和磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有和LVM相关的管理参数。当前LVM允许你在每个物理卷上保存这个物理卷的0至2份元数据拷贝.默认为1,保存在设备的开始处.为2时,在设备结束处保存第二份备份.
卷组(VG, Volume Group)
LVM卷组类似于非LVM系统中的物理硬盘,其由物理卷组成。能在卷组上创建一个或多个“LVM分区”(逻辑卷),LVM卷组由一个或多个物理卷组成。
逻辑卷(LV, Logical Volume)
LVM的逻辑卷类似于非LVM系统中的硬盘分区,在逻辑卷之上能建立文件系统(比如/home或/usr等)。
物理拓展(PE, Physical Extend)
每个物理卷都被划分为数据块(称为物理扩展盘区),这些扩展盘区与卷组的逻辑盘区具有相同的大小。
[root@centos7 ~]$ pv # 使用tab按键获取pv相关命令
pvchange pvck pvcreate pvdisplay pvmove pvremove pvresize pvs pvscan
[root@centos7 ~]$ vg # 使用tabl按键获取vg相关的命令
vgcfgbackup vgck vgdisplay vgimport vgmknodes vgrename vgsplit
vgcfgrestore vgconvert vgexport vgimportclone vgreduce vgs
vgchange vgcreate vgextend vgmerge vgremove vgscan
[root@centos7 ~]$ lv # 使用tabl按键获取lv先关的命令
lvchange lvdisplay lvmconf lvmdump lvmsadc lvremove lvs
lvconvert lvextend lvmconfig lvmetad lvmsar lvrename lvscan
lvcreate lvm lvmdiskscan lvmpolld lvreduce lvresize
上面的命令比较多,简单介绍几个常用的命令。
1: 检查系统中是否安装了LVM管理工具
[root@lww ~]# rpm -qa |grep lvm
lvm2-2.02.177-4.el7.x86_64
llvm-private-5.0.0-3.el7.x86_64
lvm2-libs-2.02.177-4.el7.x86_64
2: 如果未安装,则使用yum 方式安装
yum install lvm*
3: 创建物理卷PV
[root@lww ~]# pvcreate /dev/sdl /dev/sdh
Physical volume "/dev/sdl" successfully created.
Physical volume "/dev/sdh" successfully created.
使用
pvs
或pvdisplay
查看pv
[root@lww ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdh lvm2 --- 1.00g 1.00g
/dev/sdl lvm2 --- 20.00g 20.00g
[root@lww ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sdh
VG Name sysvg
PV Size 1.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 256
Free PE 256
Allocated PE 256
PV UUID IxXWWa-2QED-5DWe-M4V7-grTp-XQRM-nggufL
4: 创建卷组vg
[root@lww ~]# vgcreate -s 16M vgqjc /dev/sdl
Volume group "vgqjc" successfully created
[root@lww ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vgqjc 1 0 0 wz--n- 19.98g 19.98g
[root@lww ~]#
5: 创建逻辑卷lv
[root@lww ~]# lvcreate -l 512 -n lvqjc vgqjc
WARNING: ext4 signature detected on /dev/vgqjc/lvqjc at offset 1080. Wipe it? [y/n]: y
Wiping ext4 signature on /dev/vgqjc/lvqjc.
Logical volume "lvqjc" created.
[root@lww ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lvqjc vgqjc -wi-a----- 8.00g
[root@lww ~]#
6: 格式化新建的LV
[root@lww ~]# mkfs.ext4 /dev/vgqjc/lvqjc
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
524288 inodes, 2097152 blocks
104857 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2147483648
64 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
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
7: 挂载使用
[root@lww ~]# mount /dev/vgqjc/lvqjc /mnt
[root@lww ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda5 20754432 4050424 16704008 20% /
devtmpfs 483088 0 483088 0% /dev
tmpfs 498976 0 498976 0% /dev/shm
tmpfs 498976 8836 490140 2% /run
tmpfs 498976 0 498976 0% /sys/fs/cgroup
/dev/sda2 10475520 116032 10359488 2% /app
/dev/sda1 201380 135436 65944 68% /boot
tmpfs 99796 4 99792 1% /run/user/42
tmpfs 99796 32 99764 1% /run/user/1000
/dev/sr0 9176232 9176232 0 100% /run/media/qijunchao/CentOS 7 x86_64
/dev/mapper/vgqjc-lvqjc 8125880 36852 7653216 1% /mnt
分为两种情况:
第一种情况,需要扩展的pe数量,vg可以提供
[root@lww ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lvqjc vgqjc -wi-ao---- 8.00g
lvtest vgtest -wi-ao---- 160.00m
[root@lww ~]# lvextend -L +100M /dev/vgtest/lvtest
Size of logical volume vgtest/lvtest changed from 160.00 MiB (40 extents) to 260.00 MiB (65 extents).
Logical volume vgtest/lvtest successfully resized.
[root@lww ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lvqjc vgqjc -wi-ao---- 8.00g
lvtest vgtest -wi-ao---- 260.00m
[root@lww ~]#
查看实际的磁盘容量,发现并没改变,需要对文件系统进行扩容
[root@lww ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda5 xfs 20G 3.9G 16G 20% /
devtmpfs devtmpfs 472M 0 472M 0% /dev
tmpfs tmpfs 488M 0 488M 0% /dev/shm
tmpfs tmpfs 488M 8.7M 479M 2% /run
tmpfs tmpfs 488M 0 488M 0% /sys/fs/cgroup
/dev/sda2 xfs 10G 114M 9.9G 2% /app
/dev/sda1 xfs 197M 133M 65M 68% /boot
tmpfs tmpfs 98M 4.0K 98M 1% /run/user/42
tmpfs tmpfs 98M 32K 98M 1% /run/user/1000
/dev/sr0 iso9660 8.8G 8.8G 0 100% /run/media/qijunchao/CentOS 7 x86_64
/dev/mapper/vgtest-lvtest ext4 151M 1.6M 139M 2% /mnt
[root@lww ~]# resize2fs /dev/vgtest/lvtest
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vgtest/lvtest is mounted on /mnt; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/vgtest/lvtest is now 266240 blocks long.
[root@lww ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda5 xfs 20G 3.9G 16G 20% /
devtmpfs devtmpfs 472M 0 472M 0% /dev
tmpfs tmpfs 488M 0 488M 0% /dev/shm
tmpfs tmpfs 488M 8.7M 479M 2% /run
tmpfs tmpfs 488M 0 488M 0% /sys/fs/cgroup
/dev/sda2 xfs 10G 114M 9.9G 2% /app
/dev/sda1 xfs 197M 133M 65M 68% /boot
tmpfs tmpfs 98M 4.0K 98M 1% /run/user/42
tmpfs tmpfs 98M 32K 98M 1% /run/user/1000
/dev/sr0 iso9660 8.8G 8.8G 0 100% /run/media/qijunchao/CentOS 7 x86_64
/dev/mapper/vgtest-lvtest ext4 248M 2.1M 231M 1% /mnt
[root@lww ~]#
resize2fs /dev/vgtest/lvtest #更新文件系统的大小,即激活
resize2fs -f /dev/vgtest/lvtest 500M #强制设置大小
dump2fs /dev/vgtest/lvtest #查看ext系列文件系统
第二种情况,vg空间不够,需先扩展vg,扩展vg就是往vg中加pv
[root@lww ~]# pvcreate /dev/sdg
Physical volume "/dev/sdg" successfully created.
[root@lww ~]# vgextend vgtest /dev/sdg
Volume group "vgtest" successfully extended
[root@lww ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vgqjc 1 1 0 wz--n- 19.98g 11.98g
vgtest 2 1 0 wz--n- 1.99g <1.74g
[root@lww ~]#
[root@lww ~]# lvextend -L 1G /dev/vgtest/lvtest
Size of logical volume vgtest/lvtest changed from 260.00 MiB (65 extents) to 1.00 GiB (256 extents).
Logical volume vgtest/lvtest successfully resized.
[root@lww ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lvqjc vgqjc -wi-ao---- 8.00g
lvtest vgtest -wi-ao---- 1.00g
1: umount 文件系统
2: 缩减文件系统
[root@lww ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lvqjc vgqjc -wi-a----- 8.00g
lvtest vgtest -wi-a----- 1.00g
[root@lww ~]# resize2fs /dev/vgqjc/lvqjc 5G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vgqjc/lvqjc to 1310720 (4k) blocks.
The filesystem on /dev/vgqjc/lvqjc is now 1310720 blocks long.
[root@lww ~]# lvreduce -L 5G /dev/vgqjc/lvqjc
WARNING: Reducing active logical volume to 5.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vgqjc/lvqjc? [y/n]: y
Size of logical volume vgqjc/lvqjc changed from 8.00 GiB (512 extents) to 5.00 GiB (320 extents).
Logical volume vgqjc/lvqjc successfully resized.
[root@lww ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lvqjc vgqjc -wi-a----- 5.00g
lvtest vgtest -wi-a----- 1.00g
[root@lww ~]# ls #检查文件是否丢失
3: 再次挂载
[root@lww ~]# mount /dev/vgqjc/lvqjc /mnt
[root@lww ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda5 xfs 20G 3.9G 16G 20% /
devtmpfs devtmpfs 472M 0 472M 0% /dev
tmpfs tmpfs 488M 0 488M 0% /dev/shm
tmpfs tmpfs 488M 8.7M 479M 2% /run
tmpfs tmpfs 488M 0 488M 0% /sys/fs/cgroup
/dev/sda2 xfs 10G 114M 9.9G 2% /app
/dev/sda1 xfs 197M 133M 65M 68% /boot
tmpfs tmpfs 98M 4.0K 98M 1% /run/user/42
tmpfs tmpfs 98M 32K 98M 1% /run/user/1000
/dev/sr0 iso9660 8.8G 8.8G 0 100% /run/media/qijunchao/CentOS 7 x86_64
/dev/mapper/vgqjc-lvqjc ext4 4.8G 32M 4.5G 1% /mnt
如果要彻底的来移除LVM的话,需要把创建的步骤反过来操作。
1:卸载 文件系统
2:删除lv
3:删除vg
4:删除pv
[root@lww ~]# lvremove /dev/mapper/vgqjc-lvqjc
Do you really want to remove active logical volume vgqjc/lvqjc? [y/n]: y
Logical volume "lvqjc" successfully removed
[root@lww ~]# vgremove /dev/mapper/vgqjc
Volume group "vgqjc" successfully removed
[root@lww ~]# pvremove /dev/sdl
Labels on physical volume "/dev/sdl" successfully wiped.
注:可以使用{}来指定多个磁盘或者分区,如下:
pvcreate /dev/sd{b1,b2,c1,c2,d1,d2,e1,e2} -y
vgcreate vg_test_01 /dev/sd{b1,c1,d1,e1}