什么是LVM?
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@lxy ~]$ pv
pvchange pvck pvcreate pvdisplay pvmove pvremove pvresize pvs pvscan
[root@lxy ~]$ vg
vgcfgbackup vgck vgdisplay vgimport vgmknodes vgrename vgsplit
vgcfgrestore vgconvert vgexport vgimportclone vgreduce vgs
vgchange vgcreate vgextend vgmerge vgremove vgscan
[root@lxy ~]$ lv
lvchange lvdisplay lvmconf lvmdump lvmsadc lvremove lvs
lvconvert lvextend lvmconfig lvmetad lvmsar lvrename lvscan
lvcreate lvm lvmdiskscan lvmpolld lvreduce lvresize
上面的命令比较多,简单介绍几个常用的命令。
- pv打头的:代表pv相关的命令
- vg带头的:代表vg相关的命令
- lv带头的: 代表lv相关的命令
- create:创建相关
- remove:移除相关
- display:显示相关
- import:导入 相关
- export:导出相关
- rename:重命名
- vgchange:改变状态相关
- extend:扩展相关
- reduce:缩进相关
创建LVM具体操作
1: 检查系统中是否安装了LVM管理工具
[root@lxy ~]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@lxy ~]pvcreate /dev/lxy
Physical volume "/dev/lxy" successfully created.
使用pvs或pvdisplay查看pv
[root@lxy ~]pvs
PV VG Fmt Attr PSize PFree
/dev/lxy lvm2 --- 10.00g 10.00g
[root@lxy ~]pvdisplay
--- Physical volume ---
PV Name /dev/lxy
VG Name sysvg
PV Size 10.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@lxy ~]# vgcreate -s 16M lll /dev/lxy
Volume group "lxy" successfully created
[root@lxy ~]# vgs
VG #PV #LV #SN Attr VSize VFree
lxy 1 0 0 wz--n- 19.98g 19.98g
[root@lxy ~]#
5: 创建逻辑卷lv
[root@lxy ~]lvcreate -l 512 -n lll lxy
WARNING: ext4 signature detected on /dev/vgqjc/lvqjc at offset 1080. Wipe it? [y/n]: y
Logical volume "lll" created.
[root@lxy ~] lvs
6: 格式化新建的LV
[root@lxy ~]mkfs.ext4 /dev/lxy/lll
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@lxy ~]mount /dev/lxy/lll /data
[root@lxy ~]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/lxy-lll 8125880 36852 7653216 1% /data
扩展LVM具体操作
分为两种情况:
- 第一种情况,需要扩展的pe数量,vg可以提供
[root@lxy ~]lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lll vgqjc -wi-ao---- 8.00g [root@lxy ~]lvextend -L +100M /dev/lxy/lll Size of logical volume lxy/lll changed from 160.00 MiB (40 extents) to 260.00 MiB (65 extents). Logical volume lxy/lll successfully resized. [root@lxy ~]lvs
查看实际的磁盘容量,发现并没改变,需要对文件系统进行扩容
[root@lxy ~] 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/lxy-lll ext4 151M 1.6M 139M 2% /mnt
[root@lxy ~]resize2fs /dev/lxy/lll
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/lxy/lll is mounted on /mnt; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
[root@lxy ~] 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/lxy-lll ext4 248M 2.1M 231M 1% /mnt
[root@lxy ~]
resize2fs /dev/lxy/lll #更新文件系统的大小,即激活
resize2fs -f /dev/lxy/lll 500M #强制设置大小
dump2fs /dev/lxy/lll #查看ext系列文件系统
- 第二种情况,vg空间不够,需先扩展vg,扩展vg就是往vg中加pv
[root@lxy ~]pvcreate /dev/sdg
Physical volume "/dev/sdg" successfully created.
[root@lxy ~]vgextend lxy /dev/sdg
Volume group "lxy" successfully extended
[root@lxy ~]vgs
VG
vgqjc 1 1 0 wz--n- 19.98g 11.98g
lxy 2 1 0 wz--n- 1.99g <1.74g
[root@lxy ~]lvextend -L 1G /dev/lxy/lll
Size of logical volume lxy/lll changed from 260.00 MiB (65 extents) to 1.00 GiB (256 extents).
Logical volume lxy/lll successfully resized.
[root@lxy ~]lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lvqjc vgqjc -wi-ao---- 8.00g
lll lxy -wi-ao---- 1.00g
LVM缩减具体操作
1: umount 文件系统
2: 缩减文件系统
[root@lxy ~]lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lvqjc vgqjc -wi-a----- 8.00g lll lxy -wi-a----- 1.00g [root@lxy ~]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@lxy ~]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@lxy ~]lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lvqjc vgqjc -wi-a----- 5.00g lll lxy -wi-a----- 1.00g [root@lxy ~]# ls #检查文件是否丢失
3: 再次挂载
[root@lxy ~]mount /dev/vgqjc/lvqjc /mnt
[root@lxy ~]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
如果要彻底的来移除LVM的话,需要把创建的步骤反过来操作。
- 1:卸载 文件系统
- 2:删除lv
- 3:删除vg
- 4:删除pv
[root@lxy ~]lvremove /dev/mapper/lxy-lll
Do you really want to remove active logical volume lxy/lll? [y/n]: y
Logical volume "lll" successfully removed
[root@lxy ~]vgremove /dev/mapper/lxy
Volume group "lxy" successfully removed
[root@lxy ~]pvremove /dev/sd1
Labels on physical volume "/dev/sd1" successfully wiped.
注:可以使用{}来指定多个磁盘或者分区,如下:
pvcreate /dev/sd{b1,b2,c1,c2,d1,d2,e1,e2} -y
vgcreate vg_test_01 /dev/sd{b1,c1,d1,e1}
更多分享,关注微信公众号【安全info】!