LVM的创建、扩容和缩减
文章目录
[隐藏]
- 一、创建LVM系统
- 1. 硬盘分区
- 2. 创建物理卷
- 3. 创建卷组
- 4. 创建逻辑卷
- 5. 挂载文件系统
- 二、扩展逻辑卷
- 三、缩减逻辑卷
- 四、从卷组移除物理卷
- 五、常用命令
一、创建LVM系统
1. 硬盘分区
首先我们需要为我们的虚拟机添加一个虚拟硬盘,过程略。
查看硬盘设备:
[[email protected] ~]# fdisk -l ... Disk /dev/sdc: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track, 1305 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xa2c6e480 Device Boot Start End Blocks Id System ...
如上/dev/sdc是我们新添加的硬盘设备。
我们需要在/dev/sdc基础上创建分区并修改分区系统类型为8e:
[[email protected] ~]# fdisk /dev/sdc WARNING: DOS-compatible mode is deprecated. It is strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). Command (m for help): n # 创建新分区 Command action e extended # 扩展分区 p primary partition (1-4) # 主分区 p Partition number (1-4): 1 # 分区号 First cylinder (1-1305, default 1): # 默认从第一个柱面开始划分 Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): +5G # 分区大小 Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (655-1305, default 655): Using default value 655 Last cylinder, +cylinders or +size{K,M,G} (655-1305, default 1305): +3G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 First cylinder (1048-1305, default 1048): Using default value 1048 Last cylinder, +cylinders or +size{K,M,G} (1048-1305, default 1305): Using default value 1305 Command (m for help): t # 修改分区系统类型 Partition number (1-4): 1 # 选择要修改的分区号 Hex code (type L to list codes): 8e # 修改为LVM分区类型,可使用'L'查看所有可使用类型 Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): t Partition number (1-4): 2 Hex code (type L to list codes): 8e Changed system type of partition 2 to 8e (Linux LVM) Command (m for help): t Partition number (1-4): 3 Hex code (type L to list codes): 8e Changed system type of partition 3 to 8e (Linux LVM) Command (m for help): p # 查看分区情况 Disk /dev/sdc: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track, 1305 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xa2c6e480 Device Boot Start End Blocks Id System /dev/sdc1 1 654 5253223+ 8e Linux LVM /dev/sdc2 655 1047 3156772+ 8e Linux LVM /dev/sdc3 1048 1305 2072385 8e Linux LVM Command (m for help): w # 保存分区表并退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [[email protected] ~]#
2. 创建物理卷
创建物理卷的命令为pvcreate,利用该命令将希望添加到卷组的所有分区或者硬盘创建为物理卷。
[[email protected] ~]# pvcreate /dev/sdc{1,2} # 将/dev/sdc1和2创建为物理卷 Physical volume "/dev/sdc1" successfully created Physical volume "/dev/sdc2" successfully created [[email protected] ~]# pvs # 查看物理卷信息 PV VG Fmt Attr PSize PFree /dev/sdc1 lvm2 ---- 5.01g 5.01g /dev/sdc2 lvm2 ---- 3.01g 3.01g [[email protected] ~]# pvdisplay # 查看物理卷详细信息 ... "/dev/sdc1" is a new physical volume of "5.01 GiB" --- NEW Physical volume --- PV Name /dev/sdc1 VG Name PV Size 5.01 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID jKHeSG-XYna-zFYi-9Kd9-bWRM-f1zY-Qs7FZZ "/dev/sdc2" is a new physical volume of "3.01 GiB" --- NEW Physical volume --- PV Name /dev/sdc2 VG Name PV Size 3.01 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID WcKTKi-av0e-so5s-QVXB-yf7G-Tynz-9TQDJb [[email protected] ~]#
3. 创建卷组
创建卷组的命令为vgcreate,将使用pvcreate建立的物理卷创建为一个完整的卷组。
[[email protected] ~]# vgcreate myvg /dev/sdc{1,2} # 创建卷组 Volume group "myvg" successfully created [[email protected] ~]# vgs # 查看卷组信息 VG #PV #LV #SN Attr VSize VFree myvg 2 0 0 wz--n- 8.02g 8.02g [[email protected] ~]# vgdisplay myvg # 查看卷组详细信息 --- Volume group --- VG Name myvg 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 8.02 GiB # 卷组的大小,由上面两个分区决定 PE Size 4.00 MiB # PE大小可以在vgcreate命令中使用'-s'指定,默认为4M Total PE 2052 Alloc PE / Size 0 / 0 Free PE / Size 2052 / 8.02 GiB VG UUID 4aIItM-FyPx-GJ47-1A70-b8W4-EwfH-beFZrZ [[email protected] ~]# pvdisplay # 再次查看物理卷,跟上一次查看有所区别 --- Physical volume --- PV Name /dev/sdc1 VG Name myvg PV Size 5.01 GiB / not usable 2.10 MiB Allocatable yes PE Size 4.00 MiB Total PE 1282 Free PE 1282 Allocated PE 0 PV UUID jKHeSG-XYna-zFYi-9Kd9-bWRM-f1zY-Qs7FZZ --- Physical volume --- PV Name /dev/sdc2 VG Name myvg PV Size 3.01 GiB / not usable 2.79 MiB Allocatable yes PE Size 4.00 MiB Total PE 770 Free PE 770 Allocated PE 0 PV UUID WcKTKi-av0e-so5s-QVXB-yf7G-Tynz-9TQDJb [[email protected] ~]#
4. 创建逻辑卷
创建逻辑卷的命令为lvcreate
[[email protected] ~]# lvcreate -L 6G -n mylv myvg Logical volume "mylv" created. [[email protected] ~]#
使用方法:
lvcreate -L #G -n LV_NAME VG_NAME
参数解释:
- -L: 指定逻辑卷大小,不可超过卷组大小
- -n: 指定逻辑卷名称
5. 挂载文件系统
至此我们的逻辑卷就已创建完成,接下来我们就需要格式化分区并挂载使用啦。
[[email protected] ~]# mkfs.ext4 /dev/myvg/mylv # 格式化分区 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 /dev/sdc3" successfully created [[email protected] ~]# vgextend myvg /dev/sdc3 # 添加物理卷到卷组 Volume group "myvg" successfully extended [[email protected] ~]# pvs # 查看物理卷信息 PV VG Fmt Attr PSize PFree /dev/sdb datavg lvm2 a--u 20.00g 1020.00m /dev/sdc1 myvg lvm2 a--u 5.01g 0 /dev/sdc2 myvg lvm2 a--u 3.01g 2.02g /dev/sdc3 myvg lvm2 a--u 1.97g 1.97g # /dev/sdc3已加入myvg卷组 [[email protected] ~]# vgs # 卷组信息 VG #PV #LV #SN Attr VSize VFree datavg 1 1 0 wz--n- 20.00g 1020.00m myvg 3 1 0 wz--n- 9.99g 3.99g # 卷组容量比之前扩大 [[email protected] ~]# lvextend -L 9G /dev/myvg/mylv # 扩展逻辑卷容量到9G Size of logical volume myvg/mylv changed from 6.00 GiB (1536 extents) to 9.00 GiB (2304 extents). Logical volume mylv successfully resized. [[email protected] ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert nsas01 datavg -wi-ao---- 19.00g mylv myvg -wi-a----- 9.00g # 逻辑卷容量成功扩展到9G [[email protected] ~]# df -h Filesystem Size Used Avail Use% Mounted on ... /dev/mapper/myvg-mylv 5.8G 12M 5.5G 1% /mnt # 但是分区空间还是初始大小 [[email protected] ~]# resize2fs -p /dev/myvg/mylv # 改变分区大小为逻辑卷大小 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/myvg/mylv is mounted on /mnt; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of /dev/myvg/mylv to 2359296 (4k) blocks. The filesystem on /dev/myvg/mylv is now 2359296 blocks long. [[email protected] ~]# df -h Filesystem Size Used Avail Use% Mounted on ... /dev/mapper/myvg-mylv 8.8G 14M 8.3G 1% /mnt # 分区大小成功扩展到9G [[email protected] ~]#
三、缩减逻辑卷
注意事项:
- 缩减前需要先卸载挂载;
- 要确保缩减后的空间依然能够存储原有的所有数据;
- 缩减之前需要先强行检查文件,确保文件系统处于一致性状态。
[[email protected] ~]# umount /dev/myvg/mylv # 卸载挂载分区 [[email protected] ~]# e2fsck -f /dev/myvg/mylv # 强行检查文件系统 e2fsck 1.41.12 (17-May-2010) 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/mylv: 11/589824 files (0.0% non-contiguous), 73247/2359296 blocks [[email protected] ~]# resize2fs /dev/myvg/mylv 8G # 改变分区大小 resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/myvg/mylv to 2097152 (4k) blocks. The filesystem on /dev/myvg/mylv is now 2097152 blocks long. [[email protected] ~]# lvreduce -L 8G /dev/myvg/mylv # 改变逻辑卷大小 WARNING: Reducing active logical volume to 8.00 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce myvg/mylv? [y/n]: y Size of logical volume myvg/mylv changed from 9.00 GiB (2304 extents) to 8.00 GiB (2048 extents). Logical volume mylv successfully resized. [[email protected] ~]# mount /dev/myvg/mylv /mnt/ [[email protected] ~]# df -h ... /dev/mapper/myvg-mylv 7.8G 14M 7.4G 1% /mnt # 分区大小成功缩减为8G [[email protected] ~]#
四、从卷组移除物理卷
[[email protected] ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert nsas01 datavg -wi-ao---- 19.00g mylv myvg -wi-ao---- 8.00g [[email protected] ~]# vgs VG #PV #LV #SN Attr VSize VFree datavg 1 1 0 wz--n- 20.00g 1020.00m myvg 3 1 0 wz--n- 9.99g 1.99g [[email protected] ~]# pvs PV VG Fmt Attr PSize PFree /dev/sdc1 myvg lvm2 a--u 5.01g 0 /dev/sdc2 myvg lvm2 a--u 3.01g 16.00m /dev/sdc3 myvg lvm2 a--u 1.97g 1.97g [[email protected] ~]#
我们的目前状态:
- 挂载分区空间为8G;
- 逻辑卷(LV)空间大小为8G;
- 卷组(VG)空间大小为9.99G,剩余1.99G;
- 物理卷/dev/sdc3空间未使用。
那么我们是否可以将/dev/sdc3从卷组中移除出来呢?
[[email protected] ~]# pvmove /dev/sdc3 # 移动/dev/sdc3中数据到其他物理卷 No data to move for myvg [[email protected] ~]# vgreduce myvg /dev/sdc3 # 从myvg卷组中移除/dev/sdc3物理卷 Removed "/dev/sdc3" from volume group "myvg" [[email protected] ~]# pvremove /dev/sdc3 # 移除物理卷/dev/sdc3 Labels on physical volume "/dev/sdc3" successfully wiped [[email protected] ~]# pvs # /dev/sdc3成功从卷组myvg中移除 PV VG Fmt Attr PSize PFree /dev/sdb datavg lvm2 a--u 20.00g 1020.00m /dev/sdc1 myvg lvm2 a--u 5.01g 0 /dev/sdc2 myvg lvm2 a--u 3.01g 16.00m [[email protected] ~]# vgs # 卷组空间大小缩减了/dev/sdc3空间大小 VG #PV #LV #SN Attr VSize VFree datavg 1 1 0 wz--n- 20.00g 1020.00m myvg 2 1 0 wz--n- 8.02g 16.00m [[email protected] ~]#
五、常用命令
- 物理卷管理(PV)
pvcreate、pvdisplay、pvmove、pvremove、pvs
- 卷组管理(VG)
vgcreate、vgdisplay、vgextend、vgreduce、vgremove、vgrename、vgs
- 逻辑卷管理(LV)
lvcreate、lvdisplay、lvextend、lvreduce、lvremove、lvrename、lvs
原文出处:51cto -> http://blog.51cto.com/10074802/2131204
本站所发布的一切资源仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如果侵犯你的利益,请发送邮箱到 [email protected],我们会很快的为您处理。