lvm
1.分区
给虚拟机加一块硬盘sdb, 分三个区分别为sdb1,sdb2,sdb3, id都为8e
如下所示:
2.创建物理卷
记得安装 lvm的包 才能使用pvcreate
[root@localhost ~]# pvcreate /dev/sdb{1,2,3}
Physical volume "/dev/sdb1" successfully created.
Physical volume "/dev/sdb2" successfully created.
Physical volume "/dev/sdb3" successfully created.
通过pvs或者pvdisplay查看物理卷信息
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 --- 200.00m 200.00m
/dev/sdb2 lvm2 --- 200.00m 200.00m
/dev/sdb3 lvm2 --- 200.00m 200.00m
3.创建卷组
[root@localhost ~]# vgcreate myvg /dev/sdb{1,2}
Volume group "myvg" successfully created
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg 2 0 0 wz–n- 392.00m 392.00m
4.创建逻辑卷
[root@localhost ~]# lvcreate -L 300M -n lv1 myvg
Logical volume "lv1" created.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 myvg -wi-a----- 300.00m
5.创建文件系统
[root@localhost ~]# mkfs.ext4 /dev/myvg/lv1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
6.挂载
[root@localhost ~]# mkdir /mnt/lv1
[root@localhost ~]# mount /dev/myvg/lv1 /mnt/lv1/
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 17.8G 0 part /
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 200M 0 part
│ └─myvg-lv1 253:0 0 300M 0 lvm /mnt/lv1
├─sdb2 8:18 0 200M 0 part
│ └─myvg-lv1 253:0 0 300M 0 lvm /mnt/lv1
└─sdb3 8:19 0 200M 0 part
sr0 11:0 1 4.2G 0 rom
7.扩大卷组
直接扩展
[root@localhost ~]# vgextend myvg /dev/sdb3
Volume group "myvg" successfully extended
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg 3 1 0 wz--n- 588.00m 288.00m
8.扩大逻辑卷
首先要确定扩展多大,并确定当前逻辑卷所在的卷组有足够的空闲空间可用,扩展时要先扩展物理边界,在扩张逻辑边界
不需要取消挂载
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 myvg -wi-ao---- 300.00m
[root@localhost ~]# lvextend -L 400M /dev/myvg/lv1
Size of logical volume myvg/lv1 changed from 300.00 MiB (75 extents) to 400.00 MiB (100 extents).
Logical volume myvg/lv1 successfully resized.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 myvg -wi-ao---- 400.00m
[root@localhost ~]# resize2fs /dev/myvg/lv1
9.缩减逻辑卷
确定要缩减为多大?至少能容纳原有的所有数据,逻辑卷缩减有风险,所以要卸载并强行检测文件系统
缩减逻辑卷空间的步骤如下:
1.先卸载逻辑卷/dev/myvg/lv1
2.然后通过e2fsck命令检测逻辑卷上空余的空间
3.使用resize2fs将文件系统减少到200M
4.再使用lvreduce命令将逻辑卷减少到200M
注意,文件系统大小和逻辑卷大小一定要保持一致才行,如果逻辑卷大于文件系统,由于部分区域未格式化成文件系统会造成空间的浪费,如果逻辑卷小于文件系统,那数据就出问题了
umount /mnt/lv1/
[root@localhost ~]# e2fsck -f /dev/myvg/lv1
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/myvg/lv1: 11/101200 files (0.0% non-contiguous), 23297/409600 blocks
[root@localhost ~]# resize2fs /dev/myvg/lv1 200M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/myvg/lv1 to 204800 (1k) blocks.
The filesystem on /dev/myvg/lv1 is now 204800 blocks long.
[root@localhost ~]# lvreduce -L 200M /dev/myvg/lv1
WARNING: Reducing active logical volume to 200.00 MiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce myvg/lv1? [y/n]: y
Size of logical volume myvg/lv1 changed from 400.00 MiB (100 extents) to 200.00 MiB (50 extents).
Logical volume myvg/lv1 successfully resized.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 myvg -wi-a----- 200.00m
最后重新挂载。。
10.缩减卷组
1、pvmove /dev/sdb3 #将/dev/sdb1上存储的数据移到其它物理卷中
2、vgreduce myvg /dev/sdb3 #将/dev/sdb3从myvg卷组中移除
3、pvremove /dev/sdb3 #将/dev/sdb3从物理卷上移除
[root@localhost ~]# pvmove /dev/sdb3
No data to move for myvg.
[root@localhost ~]# vgreduce myvg /dev/sdb3
Removed "/dev/sdb3" from volume group "myvg"
[root@localhost ~]# pvremove /dev/sdb3
Labels on physical volume "/dev/sdb3" successfully wiped.
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg 2 1 0 wz--n- 392.00m 192.00m