LVM
Posted f-h-j-11-7
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LVM相关的知识,希望对你有一定的参考价值。
LVM详解
LVM,即逻辑管理器,它是Linux环境下对磁盘分区进行管理的一种机制;它可以将多个物理分区整合在一起,并且可以根据实际需要动态调整文件系统空间。另外,它还可以提供快照,快照在刚创建的时候,其内容是和原卷一模一样的,只有数据在原卷中发生变化的时候,或者快照自身数据改变时候,快照会将被改动数据的原数据保存起来。
在学习LVM之前,需要先知道几个概念,物理卷、卷组、逻辑卷、扩展逻辑卷。
1、物理卷(pv)
物理卷是组成LVM的最底层的元素,即Linux上的物理分区。
2、卷组(vg)
将各个独立的PV组合起来形成的一个存储空间就称为VG,VG的大小就是整个LVM的大小
3、逻辑卷(lv)
可以被用户格式化、挂载并提供数据存储的对象就是LV。
4、物理扩展块
PE相当于Linux分区中的block,它是LVM的最小存储单位,默认为4M。
一、LVM基本操作
创建PV –> 创建VG –> 创建LV –> 格式化 –> 挂载
扩展卷组的过程:
准备PV –> 完成扩展
缩减卷组的过程:
确定移除的PV –> 转移移除PV的数据到别的PV –> 从卷组中移除PV
扩展逻辑卷的过程:
确定扩展大小 –> 扩展物理边界 –> 扩展逻辑边界
缩减逻辑卷的过程:
确定缩减大小 –> 卸载逻辑卷 –> 缩减逻辑边界 –> 缩减物理边界
1.创建pv
首先得创建分区,将分区的类型改为8e,也可以添加硬盘。
[[email protected] ~]# fdisk /dev/sdh Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0x12aee9aa. Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): 1 First sector (2048-2097151, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): +500M Partition 1 of type Linux and of size 500 MiB is set Command (m for help): n Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): p Partition number (2-4, default 2): First sector (1026048-2097151, default 1026048): Using default value 1026048 Last sector, +sectors or +size{K,M,G} (1026048-2097151, default 2097151): +500M Partition 2 of type Linux and of size 500 MiB is set Command (m for help): t Partition number (1,2, default 2): 1 Hex code (type L to list all codes): 8e Changed type of partition ‘Linux‘ to ‘Linux LVM‘ Command (m for help): t Partition number (1,2, default 2): 2 Hex code (type L to list all codes): 8e Changed type of partition ‘Linux‘ to ‘Linux LVM‘ Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. pvcreate /dev/sdh1 /dev/sdh2 [root@localhost ~]# pvcreate /dev/sdh1 /dev/sdh2 Physical volume "/dev/sdh1" successfully created. Physical volume "/dev/sdh2" successfully created.
(1)pvs 简要查看PV信息
[[email protected] ~]# pvs /dev/sdh1 /dev/sdh2 PV VG Fmt Attr PSize PFree /dev/sdh1 lvm2 --- 500.00m 500.00m /dev/sdh2 lvm2 --- 500.00m 500.00m
(2)pvsdisplay 详细查看PV信息
[[email protected] ~]# pvdisplay /dev/sdh1 /dev/sdh2 "/dev/sdh2" is a new physical volume of "500.00 MiB" --- NEW Physical volume --- PV Name /dev/sdh2 VG Name PV Size 500.00 MiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID DR3W4b-mHaC-YdnN-uYm9-rIZa-8uE8-VyapNS
2.创建vg
vgcreate VG_NAME PV_NAME
-s PE:指定PE的大小
[[email protected] ~]# vgcreate -s 100M vgfhj /dev/sdh1 /dev/sdh2 Volume group "vgfhj" successfully created
(1)vgs:简要查看VG信息。
[[email protected] ~]# vgs VG #PV #LV #SN Attr VSize VFree centos 1 3 0 wz--n- 38.00g 4.00m vgfhj 2 0 0 wz--n- 800.00m 800.00m
(2)vgdisplay:详细查看VG信息。
[r[email protected] ~]# vgdisplay --- Volume group --- VG Name vgfhj 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 800.00 MiB PE Size 100.00 MiB Total PE 8 Alloc PE / Size 0 / 0 Free PE / Size 8 / 800.00 MiB VG UUID Z5HO4t-MKmX-yhQQ-NcoR-rkr4-HK6X-Xhtrd8
3.创建lv
使用的命令:
lvcreate -L SIZE -n LV_NAME VG_NAME
-L 指定逻辑卷的大小
-n 指定逻辑卷的名称
-l PE的个数
[[email protected] ~]# lvcreate -L 500M -n lvfhj vgfhj WARNING: ext4 signature detected on /dev/vgfhj/lvfhj at offset 1080. Wipe it? [y/n]: y Wiping ext4 signature on /dev/vgfhj/lvfhj. Logical volume "lvfhj" created.
(1)lvs: 简要查看LV信息
[[email protected] ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert app centos -wi-ao---- 20.00g root centos -wi-ao---- 10.00g swap centos -wi-ao---- 8.00g lvfhj vgfhj -wi-a----- 500.00m
(2)lvdisplay 逻辑卷的设备文件名称: 详细查看LV信息
[[email protected] ~]# lvdisplay --- Logical volume --- LV Path /dev/vgfhj/lvfhj LV Name lvfhj VG Name vgfhj LV UUID 4KNkDz-3CbQ-oAy4-ze5z-9eOK-3nwI-2Hp4pB LV Write Access read/write LV Creation host, time localhost.localdomain, 2018-08-11 17:18:00 +0800 LV Status available # open 0 LV Size 500.00 MiB Current LE 5 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:3
4.挂载lv
在挂载LV之前,我们要对LV进行格式化后再挂载,然后存放文件看是否正常。
[[email protected] ~]# mkfs -t ext4 /dev/vgfhj/lvfhj mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) Stride=0 blocks, Stripe width=0 blocks 128016 inodes, 512000 blocks 25600 blocks (5.00%) reserved for the super user First data block=1 Maximum filesystem blocks=34078720 63 block groups
5.扩展卷组
(1)准备好一个PV
(2)使用vgextend命令完成扩展,命令格式:vgextend VG_NAME /PATH/TO/PV
[[email protected] ~]# vgextend vgfhj /dev/sdi Volume group "vgfhj" successfully extended [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree centos 1 3 0 wz--n- 38.00g 4.00m vgfhj 3 1 0 wz--n- <1.76g <1.27g
6.扩展卷组
(1)确定要移除的PV(sdh1)
[[email protected] ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 centos lvm2 a-- 38.00g 4.00m /dev/sdh1 vgfhj lvm2 a-- 400.00m 0 /dev/sdh2 vgfhj lvm2 a-- 400.00m 300.00m /dev/sdi vgfhj lvm2 a-- 1000.00m 1000.00m
(2)将此PV上的数据转移至其他的PV
命令:pvmove PV_NAME
[[email protected] ~]# pvmove /dev/sdh1 /dev/sdh1: Moved: 0.00% /dev/sdh1: Moved: 100.00%
(3)从卷组中将此PV移除
命令:vgreduce /PATH/TO/PV
[[email protected] ~]# vgreduce vgfhj /dev/sdh1 Removed "/dev/sdh1" from volume group "vgfhj"
7.扩展逻辑卷
(1)先确定扩展的LV大小,并确保对应卷组有足够的空间
(2)扩展物理边界
使用lvextend命令:lvextend -L [+]SIZE /PATH/TO/LV
-L [+]SIZE: 指定扩展的容量;如果带+号,表示扩展的容量是在原来的基础上加上SIZE的容量;如果不带,表示扩展容量到SIZE,单位为"kKmMgGtT"
[[email protected] ~]# lvextend -L +500M /dev/vgfhj/lvfhj Size of logical volume vgfhj/lvfhj changed from 500.00 MiB (5 extents) to 1000.00 MiB (10 extents). Logical volume vgfhj/lvfhj successfully resized.
(3)扩展逻辑边界
使用resize2fs /PATH/TO/DEVICE
[[email protected] ~]# resize2fs /dev/vgfhj/lvfhj resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/vgfhj/lvfhj to 1024000 (1k) blocks. The filesystem on /dev/vgfhj/lvfhj is now 1024000 blocks long.
8.缩减逻辑卷
(1)确定缩减后的大小,至少确保缩减后能容纳原来的所有数据
(2)执行缩减:
A.因为缩减有风险,所以必须卸载并检测文件系统,使用e2fsck -f命令
[[email protected] ~]# umount /dev/vgfhj/lvfhj [root@localhost ~]# e2fsck -f /dev/vgfhj/lvfhj 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/vgfhj/lvfhj: 11/254000 files (0.0% non-contiguous), 42817/1024000 blocks
B.缩减逻辑边界,使用resize2fs命令
[[email protected] ~]# resize2fs /dev/vgfhj/lvfhj 910M resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/vgfhj/lvfhj to 931840 (1k) blocks. The filesystem on /dev/vgfhj/lvfhj is now 931840 blocks long.
C.缩减物理边界,使用lvreduce命令
lvreduce -L [-]SIZE /path/to/lv
[[email protected] ~]# lvreduce -L 910M /dev/vgfhj/lvfhj Rounding size to boundary between physical extents: 1000.00 MiB. New size (10 extents) matches existing size (10 extents).
9.移除逻辑卷
移除逻辑卷之前,确保已经没有被挂载。此时使用lvremove /path/to/lv命令即可
[[email protected] ~]# lvremove /dev/vgfhj/lvfhj Do you really want to remove active logical volume vgfhj/lvfhj? [y/n]: y Logical volume "lvfhj" successfully removed
10.移除卷组
使用vgremove VG_NAME
[[email protected] ~]# vgremove vgfhj Volume group "vgfhj" successfully removed
11.移除物理卷
使用pvremove PV_NAME命令
[[email protected] ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 centos lvm2 a-- 38.00g 4.00m /dev/sdh1 lvm2 --- 500.00m 500.00m /dev/sdh2 lvm2 --- 500.00m 500.00m /dev/sdi lvm2 --- 1.00g 1.00g [root@localhost ~]# pvremove /dev/sdh1 /dev/sdh2 /dev/sdi Labels on physical volume "/dev/sdh1" successfully wiped. Labels on physical volume "/dev/sdh2" successfully wiped. Labels on physical volume "/dev/sdi" successfully wiped.
以上是关于LVM的主要内容,如果未能解决你的问题,请参考以下文章