LVM逻辑卷
1、 LVM逻辑卷机制
LVM逻辑卷(Logical volumes manager)是将多个分区或者物理硬盘从逻辑上作为一个整体,从这个整体上划分出不同的逻辑分区;逻辑分区可以根据需要进行扩大和缩小
整个创建逻辑分区过程依次创建pv、vg、lv;
pv为分区或者物理硬盘,又PE为基本单元组成;
vg 为一个或多个pv组成的整体
lv为从vg卷组中分割出分区,该分区可以格式化建立文件系统;
2、 LVM创建及管理
由于Linux支持LVM机制,那么就离不开LVM的创建和管理,LVM管理工具包括了pv、vg、lv、的扫描、创建、删除、扩展、显示、减小、以及PE大小的设置;
功能 PV VG LV
扫描 pvscan vgscan lvscan
创建 pvcreate vgcreate lvcreate
显示 pvdisplay vgdisplay lvdisplay
删除 pvremove vgremove lvremove
扩展 -- vgextend lvextend
减少 -- vgreduce lvreduce
常用语法:
pvcreate 物理卷或分区名
vgcreate –s pe大小 卷组名 物理卷
lvcreate -L lv大小 –n lv名 vg名
lvcreate -l pe个数 –n lv名 vg名
lvextend –L +大小 lv逻辑分区
lvreduce
resize2fs lv分区大小
注意事项:如果不是单个硬盘而是多个分区构建pv,一定要将分区化为8e格式;
3、 实例
现在有4个50G的硬盘,将sab、sdc、sdd作成一个vg,然后分三个40G的ext4分区,在创建一个250M的swap分区;
3.1、创建物理卷
[[email protected] ~]# pvcreate /dev/sdb /dev/sdc /dev/sdd
Physical volume "/dev/sdb" successfully created
Physical volume "/dev/sdc" successfully created
Physical volume "/dev/sdd" successfully created
[[email protected] ~]#
查看pv
[[email protected] ~]# pvdisplay
--- Physical volume ---
"/dev/sdb" is a new physical volume of "50.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb
VG Name
PV Size 50.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID t6XA0f-nAxl-0I8f-XGj0-eojd-PBr0-NnAUuU
"/dev/sdc" is a new physical volume of "50.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdc
VG Name
PV Size 50.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID AKREKZ-L5rt-UdZR-4iy6-Z2BH-vwI0-qEXqZa
"/dev/sdd" is a new physical volume of "50.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdd
VG Name
PV Size 50.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID vyHztS-vM5b-j40c-mHE9-9A3r-wU7b-tbggy3
3.2、创建卷组并查看
[[email protected] ~]# vgcreate woonvg /dev/sdb /dev/sdc /dev/sdd
Volume group "woonvg" successfully created
VG Name woonvg
System ID
Format lvm2
Metadata Areas 3
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 3
Act PV 3
VG Size 149.99 GiB
PE Size 4.00 MiB
Total PE 38397
Alloc PE / Size 0 / 0
Free PE / Size 38397 / 149.99 GiB
VG UUID tGmw2t-5iL0-FuEV-UmqP-RtsT-oOTc-O4aLNa;
默认PE大小4M
3.3、创建LV并查看绿分区
[[email protected] ~]# lvcreate -L 40G -n woon_lv1 woonvg
Logical volume "woon_lv1" created
[[email protected] ~]# lvcreate -l 10240 -n woon_lv2 woonvg
Logical volume "woon_lv2" created
[[email protected] ~]# lvcreate -l 10240 -n woon_lv3 woonvg
Logical volume "woon_lv3" created
[[email protected] ~]# lvdisplay
--- Logical volume ---
LV Path /dev/woonvg/woon_lv1
LV Name woon_lv1
VG Name woonvg
LV UUID zeOmDa-TAp0-oenx-YMi6-kYUa-IJpR-IWDzGD
LV Write Access read/write
LV Creation host, time woon, 2015-02-09 23:03:20 +0800
LV Status available
# open 0
LV Size 40.00 GiB
Current LE 10240
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2
--- Logical volume ---
LV Path /dev/woonvg/woon_lv2
LV Name woon_lv2
VG Name woonvg
LV UUID 10UXVD-2efM-GzuO-fQ2z-0guQ-1EW2-G3Hls7
LV Write Access read/write
LV Creation host, time woon, 2015-02-09 23:04:32 +0800
LV Status available
# open 0
LV Size 40.00 GiB
Current LE 10240
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:3
--- Logical volume ---
LV Path /dev/woonvg/woon_lv3
LV Name woon_lv3
VG Name woonvg
LV UUID DaHx1r-6hQe-w8kg-8PXR-zZeq-0lcP-uLOnPc
LV Write Access read/write
LV Creation host, time woon, 2015-02-09 23:04:40 +0800
LV Status available
# open 0
LV Size 40.00 GiB
Current LE 10240
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:4
3.4、格式化lv逻辑卷
[[email protected] ~]# mkfs.ext4 /dev/woonvg/woon_lv1
[[email protected] ~]# mkfs.ext4 /dev/woonvg/woon_lv2
[[email protected] ~]# mkfs.ext4 /dev/woonvg/woon_lv3
3.4.1、格式化后挂载到/opt/woon_lv1、woon_lv2、woon_lv3下
[[email protected] ~]# mount /dev/woonvg/woon_lv1 /opt/woon_lv1
[[email protected] ~]# mount /dev/woonvg/woon_lv2 /opt/woon_lv2
[[email protected] ~]# mount /dev/woonvg/woon_lv3 /opt/woon_lv3
3.4.2、 查看挂载情况
[[email protected] woon_lv1]# mount | tail -n 3
/dev/mapper/woonvg-woon_lv1 on /opt/woon_lv1 type ext4 (rw)
/dev/mapper/woonvg-woon_lv2 on /opt/woon_lv2 type ext4 (rw)
/dev/mapper/woonvg-woon_lv3 on /opt/woon_lv3 type ext4 (rw)
[[email protected] woon_lv1]#
3.4.3、分一个250M的swap分区并挂载
[[email protected] woon_lv1]# lvcreate -L 250M -n woon_swap woonvg
Rounding up size to full physical extent 252.00 MiB
Logical volume "woon_swap" created
此时发现明明分的是250M,但是执行命令后确实252M,经过排查是由于PE大小影响的,由于PE是PV的基本单位,导致250M不能精确划分;在目前的环境以下如果想要实现就必须删除所有的lv、vg,调整PE大小为1M即可;想要解决这个问题只需要在创建vg时使用以下命令:
vgcreate –s 1M woonvg /dev/sd[b-d]
4、调整分区大小
4.1、扩展分区
将woon_lv1扩展到50G
[[email protected] woon_lv1]# lvextend -L 50G /dev/woonvg/woon_lv1
Size of logical volume woonvg/woon_lv1 changed from 40.00 GiB (10240 extents) to 50.00 GiB (12800 extents).
Logical volume woon_lv1 successfully resized
此时分区扩展到50G了,但是内核并不是知道,所以要通知内核永达resize2fs指令
[[email protected] woon_lv1]# resize2fs /dev/woonvg/woon_lv1
[[email protected] woon_lv1]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_woon-lv_root
8.3G 4.0G 4.0G 51% /
tmpfs 504M 224K 503M 1% /dev/shm
/dev/sda1 477M 27M 426M 6% /boot
/dev/sdf1 100G 13G 88G 13% /opt/ntfs1
/dev/sde1 459G 70M 435G 1% /opt/gpt1
/dev/sr0 3.8G 3.8G 0 100% /media/CentOS_6.6_Final
/dev/mapper/woonvg-woon_lv1
50G 52M 47G 1% /opt/woon_lv1
/dev/mapper/woonvg-woon_lv2
40G 48M 38G 1% /opt/woon_lv2
/dev/mapper/woonvg-woon_lv3
40G 48M 38G 1% /opt/woon_lv3
4.2、减小一个分区
分区的减小是不能在线直接减小的,应该先卸载分区,然后减小文件系统,然后减小分区大小,最后重新挂载;如将woon_lv2减小到30G ,不损坏文件系统;
[[email protected] woon_lv1]# umount /dev/woonvg/woon_lv2
[[email protected] woon_lv1]# e2fsck -f /dev/woonvg/woon_lv2
[[email protected] woon_lv1]# resize2fs /dev/woonvg/woon_lv2 30G
[[email protected] woon_lv1]# lvreduce -L 30G /dev/woonvg/woon_lv2
WARNING: Reducing active logical volume to 30.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce woon_lv2? [y/n]: y
Size of logical volume woonvg/woon_lv2 changed from 40.00 GiB (10240 extents) to 30.00 GiB (7680 extents).
Logical volume woon_lv2 successfully resized
[[email protected] woon_lv1]# mount /dev/woonvg/woon_lv2 /opt/woon_lv2
[[email protected] woon_lv1]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_woon-lv_root
8.3G 4.0G 4.0G 51% /
tmpfs 504M 224K 503M 1% /dev/shm
/dev/sda1 477M 27M 426M 6% /boot
/dev/sdf1 100G 13G 88G 13% /opt/ntfs1
/dev/sde1 459G 70M 435G 1% /opt/gpt1
/dev/sr0 3.8G 3.8G 0 100% /media/CentOS_6.6_Final
/dev/mapper/woonvg-woon_lv1
50G 52M 47G 1% /opt/woon_lv1
/dev/mapper/woonvg-woon_lv3
40G 48M 38G 1% /opt/woon_lv3
/dev/mapper/woonvg-woon_lv2
30G 44M 28G 1% /opt/woon_lv2