linux-PV,VG,LVm创建扩展缩减实现

lvm:logical volume Manager
版本号:lvm,lvm2
dm:device mapper
安全实现边界扩展
功用:将一个或多个底块设备(包括RAID)组织成一个 逻辑存储空间
其边界:可动态扩展或缩减:

由三个层次组成,分别是PV,VG,LV
逻辑卷工作机制:
PV刚被创建是没有PE的,只有PV加入vG以后才会确定PV上的PE有多大,每一个PV都会等分固定大小块的PE,由于这个快的大小是由VG决定的,所以,一个PV只有加入PV以后,PE才有固定大小,我们划分LV的时候,就是把Pe固定块大小组合起来形成一个存储空间,一旦PE被划分出来组织成LV,那他就叫做LE,所谓的逻辑盘,我们扩展逻辑盘的边界大小,就是通过PE来完成的。逻辑卷的大小是可以缩减的,通过LE的增加和缩减来完成,我们也可以称作PE。

lv,multipath

pe,le,
pysical extent
logical extent

管理lV
pv:pvcreate,pvdisplay,pvs,pvscan,pvremove,pvmove

vg:vgcreate,vgdisplay,vgs,vgscan.vgremove,vgextend,vgreduce

lv:lvcreate,lvdisplay,lvs,lvresize,lvextend,lvreduce,lvremove

示例:
创建一个4G VG:
/dev/sdb4 397 6527 49247257+ 5 Extended
/dev/sdb5 397 658 2104483+8e Linux LVM
/dev/sdb6 659 920 2104483+ 8e Linux LVM
[root@localhost ~]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
[root@localhost ~]#

[root@localhost ~]# pvcreate /dev/sdb[5,6]
Physical volume "/dev/sdb5" successfully created
Physical volume "/dev/sdb6" successfully created
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb5 lvm2 --- 2.01g 2.01g
/dev/sdb6 lvm2 --- 2.01g 2.01g
[root@localhost ~]# pvscan
PV /dev/sdb5 lvm2 [2.01 GiB]
PV /dev/sdb6 lvm2 [2.01 GiB]
Total: 2 [4.01 GiB] / in use: 0 [0 ] / in no VG: 2 [4.01 GiB]
[root@localhost ~]# pvdisplay (没有加入vg显示NEW Physical volume)
"/dev/sdb5" is a new physical volume of "2.01 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb5
VG Name
PV Size 2.01 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID u5YAZn-Rnuc-6lXW-TR2n-3Vzd-Aghq-7HDv73

"/dev/sdb6" is a new physical volume of "2.01 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb6
VG Name
PV Size 2.01 GiB
Allocatable NO
PE Size 0
Total PE 0(不属于vg是没有pe大小)
Free PE 0
Allocated PE 0
PV UUID 2pZ50q-moJt-qWYn-4n8t-qi21-7QRV-e

vg:
创建VG:新增一个VG,并指明其包含哪个或那些PV实现
# vgcreate [dev/sd]
-s 指明pe大小
-v 显示详细过程
-t 测试创建
创建VG:
[root@localhost ~]#vgcreate markvg /dev/sdb[5,6]
Volume group "markvg" successfully created
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
markvg 2 0 0 wz--n- 4.01g 4.01g
查看变化
[root@localhost ~]#pvdisplay /dev/sdb5
--- Physical volume ---
PV Name /dev/sdb5
VG Name markvg
PV Size 2.01 GiB / not usable 3.16 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 513
Free PE 513
Allocated PE 0
PV UUID u5YAZn-Rnuc-6lXW-TR2n-3Vzd-Aghq-7HDv73

[root@localhost ~]#
[root@localhost ~]#vgdisplay
--- Volume group ---
VG Name markvg
System ID
Format lvm2
Metadata Areas 2
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 2
Act PV 2
VG Size 4.01 GiB
PE Size 4.00 MiB(pe默认大小为4M,也可以指定pe大小)
Total PE 1026
Alloc PE / Size 0 / 0
Free PE / Size 1026 / 4.01 GiB
VG UUID tfyfuD-h9Rn-EHbH-wU2J-5YuH-ItiJ-pqFtfo

[root@localhost ~]#
注意:这里的PE大小将会在缩减和扩展时以PE的大小递增
创建16m的pe的vg
vgcreate -s 16m markvg /dev/sdb[5,6]

缩减vg
1,挪走vg中的pv实现
注意:计划移除的pv不能包含已经存储数据的pe,如果有需要先使用pvmove移动到同vg内其他pv上
pvmove /dev/sdv

删除pv pvremove /dev/sdb#
移除markvg
[root@localhost ~]#vgreduce markvg /dev/sdb5
Removed "/dev/sdb5" from volume group "markvg"
[root@localhost ~]#
[root@localhost ~]#vgdisplay
--- Volume group ---
VG Name markvg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size 2.00 GiB
PE Size 4.00 MiB
Total PE 513
Alloc PE / Size 0 / 0
Free PE / Size 513 / 2.00 GiB
VG UUID tfyfuD-h9Rn-EHbH-wU2J-5YuH-ItiJ-pqFtfo

[root@localhost ~]#
扩展vg:同vg中添加更多的pv实现
[root@localhost ~]#vgextend markvg /dev/sdb5
Volume group "markvg" successfully extended
[root@localhost ~]#vgdisplay
--- Volume group ---
VG Name markvg
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 4.01 GiB
PE Size 4.00 MiB
Total PE 1026
Alloc PE / Size 0 / 0
Free PE / Size 1026 / 4.01 GiB
VG UUID tfyfuD-h9Rn-EHbH-wU2J-5YuH-ItiJ-pqFtfo

[root@localhost ~]#
创建lv:logical volume
lvcreate:
lvcreate -l 多大[bskmgt] -n name vgname
注意:lv大小必须为pe的整数倍
[root@localhost ~]#lvcreate -L 2G -n name marklv markvg
Volume group "marklv" not found
[root@localhost ~]#
[root@localhost ~]#lvdisplay
--- Logical volume ---
LV Path /dev/markvg/marklv
LV Name marklv
VG Name markvg
LV UUID X2uIuJ-HU3J-rxp7-3Z0P-gnUq-1j8f-QP3w7k
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2015-04-07 07:05:23 -0700
LV Status available
# open 0
LV Size4.00 GiB
Current LE 1024
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
格式化,挂载使用
[root@localhost ~]# mke2fs -t ext4 /dev/markvg/marklv
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
262144 inodes, 1048576 blocks
52428 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1073741824
32 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

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 30 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
挂载至/tmp/mark.raid0.1
[root@localhost ~]# mount /dev/markvg/marklv /tmp/mark.raid0.1/
[root@localhost ~]# ls /tmp/mark.raid0.1/
lost+found
[root@localhost ~]# cp /etc/fstab /tmp/mark.raid0.1/
查看
[root@localhost ~]#df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 121418860 2533528 112710868 3% /
tmpfs 506144 72 506072 1% /dev/shm
/dev/sda1 289293 28484 245449 11% /boot
/dev/mapper/markvg-marklv
3997376 8192 3779472 1% /tmp/mark.raid0.1
[root@localhost ~]#

扩展lv:
存储设备有:物理边界
文件系统有:逻辑边界

扩展:先扩展物理边界,而后拉伸逻辑编辑
lvextend ,lvresize
resize2fs,

开始扩展:
在挂载时,对markvg-marklv做扩展,来验证。目前是3997376
如何扩展lv条件:

1,先确定目标大小:确保此lv此在的vg中有足够的空闲可用
2,扩展物理边界
lvextend -l +/# /dev/vgname/lvname
3,拉伸逻辑边界
resize2fs /dev/vgname/lvname
使用vgdisplay查看空间

设备路径:[root@localhost ~]# lvdisplay /dev/markvg/marklv

设备路径:
1,/dev/vgname/lvname
2,/dev/mapper/vgname-lvname
实际路径:/dev/dm-#
移除lv(操作不可逆,和移除分区是一样):
lvremove mylv路径
[root@localhost ~]# lvremove /dev/markvg/marklv
Do you really want to remove active logical volume marklv? [y/n]: y
Logical volume "marklv" successfully removed
[root@localhost ~]#

如何扩展lv:

1,先确定目标大小:确保此lv此在的vg中有足够的空闲可用
2,扩展物理边界
lvextend -l +/# /dev/vgname/lvname
3,拉伸逻辑边界
resize2fs /dev/vgname/lvname

示例:
vgdisplay myvg查看空间是否空闲
lvcreate -l +3g -n myvg 添加物理边界3g
mke2fs -t ext4 /dev/myvg/mylv
mount /dev/myvg/mylv

对mylv扩展
格式化:resize2fs /dev/myvg/mylv
扩展到5G:[root@localhost ~]# lvextend -L 5G /dev/myvg/mylv
查看信息:[root@localhost ~]#lvs
df -lh查看扩展后结果,而后拉伸
在线拉伸逻辑分区:resize2fs /dev/myvg/mylv
以上命令为在线扩展
在使用df -lh查看拉伸后结果

缩减lv;
注意:
1,缩减要离线;
2,先缩减系统大小,在缩减lv的大小‘
过程:
1,先确定缩减后的目标大小,要确保缩减后的大小能够容纳现在的所有数据
2,卸载逻辑卷,并强制做文件系统一致性检查
e2fsck -f /dev/myvg/mylv
3,开始缩减逻辑边界
resize2fs /dev/myvg/mylv #缩减大小
4,缩减物理边界
lvreduce -l [-] /dev/myvg/mylv

示例:

1,判断文件大小 du -sh 文件目标
2,umount 目标
3,e2fack -f /dev/myvg/mylv
4,resize2fs /dev/myvg/mylv 3g
5,lvreduce -l 3G /dev/myvg/mylv

0 分享

您可以选择一种方式赞助本站

支付宝扫码赞助

支付宝扫码赞助

日期: 2015-04-07分类: Sys Basics

标签: Fdisk

发表评论