1、什么是LVM
LVM(Logical Volume Manager)逻辑卷管理,是linux环境下将一种将一个或多个硬盘的分区在逻辑上集合来呈现给上层应用,对磁盘实现动态管理的机制。相对于普通的磁盘分区有很大的灵活性,使用LVM在一定程度上就可以解决普通磁盘分区带来的问题。
2、专业术语
●物理卷(PhysicalVolume)就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(可 以是RAID),是LVM的基本存储逻辑块
●卷组(VolumeGroup)是物理卷的集合
●LVM(LogicalVolumeManager)的逻辑卷类似于非LVM系统中的硬盘分区,在逻辑卷之上可以建立文件系统(比如/home或者/usr等)
●PE(PhysicalExtent)是每一个逻辑卷和逻辑卷的基本单元,具有唯一编号的PE是可以被LVM寻址 的最小单元。PE的大小是可配置的,默认为4MB
●LE(LogicalExtent)是逻辑卷的基本单位。在同一个卷组中,LE的大小和PE是相同的,并且一一对应
3、LVM工作原理图
原理:在Disk的基础上,创建PV,再在PV的基础上,创建VG,要注意:它们的基本单位是PE。在VG的基础上,创建LV,LV的基本单位是LE。PE和LE的单位相同。
4、简单实现
<1>创建文件系统
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): First sector (2048-419430399, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-419430399, default 419430399): +3G Partition 1 of type Linux and of size 3 GiB is set # Command (m for help): T Selected partition 1 Hex code (type L to list all codes): 8e # 调整分区类型为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. # 多创建几个分区,操作方法如上
<2>创建PV
[root@centos7~]#pvcreate /dev/sdc1 # 三个分区创建pv Physical volume "/dev/sdc1" successfully created [root@centos7~]#pvcreate /dev/sdc2 Physical volume "/dev/sdc2" successfully created [root@centos7~]#pvcreate /dev/sdc3 Physical volume "/dev/sdc3" successfully created
<3>创建VG
[root@centos7~]#vgcreate vg1 /dev/sdc{1,2,3} Volume group "vg1" successfully created
<4>创建LV
[root@centos7~]#lvcreate -n lv1 -L 6G /dev/vg1 Logical volume "lv1" created.
<5>为LV创建文件系统
[root@centos7~]#mke2fs -t ext4 /dev/vg1/lv1 mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 393216 inodes, 1572864 blocks 78643 blocks (5.00%) reserved for the super user .....
<6>挂载逻辑卷
# 创建挂载点 [root@centos7~]#mkdir /mnt/lv1 # 挂载lv1 [root@centos7~]#vim /etc/fstab ... UUID="82ed7610-1975-4433-a741-1db119e4bf0e" /mnt/lv1 ext4 default 0 0 ... # 挂载设备 [root@centos7~]#mount -a [root@centos7~]#
<7>测试逻辑卷大小
# 查看原始数据大小 [root@centos7/mnt/lv1]#df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 100G 6.0G 95G 6% / devtmpfs 474M 0 474M 0% /dev tmpfs 489M 84K 489M 1% /dev/shm tmpfs 489M 7.2M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sdg1 976M 2.6M 907M 1% /mnt/sdg1 /dev/mapper/centos-testdir 20G 33M 20G 1% /testdir /dev/sda1 197M 143M 55M 73% /boot tmpfs 98M 20K 98M 1% /run/user/42 tmpfs 98M 0 98M 0% /run/user/0 /dev/sde1 2.0G 55M 1.8G 3% /home /dev/mapper/vg1-lv1 5.8G 24M 5.5G 1% /mnt/lv1 # 原始大小 # 填充磁盘 [root@centos7/mnt/lv1]#dd if=/dev/zero of=/mnt/lv1/f1 bs=1M count=1024 1024+0 records in 1024+0 records out 1073741824 bytes (1.1 GB) copied, 2.58483 s, 415 MB/s # 再此查看磁盘空间 [root@centos7/mnt/lv1]#df -h ... /dev/mapper/vg1-lv1 5.8G 1.1G 4.5G 19% /mnt/lv1 # 已生效,只分配6g
<8>扩展LV,在线扩展(挂载即可扩展)
# 查看VG [root@centos7/mnt/lv1]#pvs PV VG Fmt Attr PSize PFree /dev/sda2 centos lvm2 a-- 122.00g 0 /dev/sdc1 vg1 lvm2 a-- 3.00g 3.00g /dev/sdc2 vg1 lvm2 a-- 5.00g 5.00g /dev/sdc3 vg1 lvm2 a-- 10.00g 4.00g # /dev/sdc一共18g,还有剩余,可扩展 # 扩展LV [root@centos7/mnt/lv1]#lvextend -L 12G /dev/vg1/lv1 # 扩展 Size of logical volume vg1/lv1 changed from 6.00 GiB (1536 extents) to 12.00 GiB (3072 extents). Logical volume lv1 successfully resized. # LV已同步,df -h查看未同步 [root@centos7/mnt/lv1]#lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert ... lv1 vg1 -wi-ao---- 12.00g [root@centos7/mnt/lv1]#df -h ... /dev/mapper/vg1-lv1 5.8G 1.1G 4.5G 19% /mnt/lv1 # 因为新加入的分区尚未创建文件系统,所以df -h查看同步失败,同步文件系统 [root@centos7/mnt/lv1]#resize2fs /dev/vg1/lv1 # resize2fs,ext文件系统专用命令 resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/vg1/lv1 is mounted on /mnt/lv1; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 2 The filesystem on /dev/vg1/lv1 is now 3145728 blocks long. # 再次查看 [root@centos7/mnt/lv1]#df -h Filesystem Size Used Avail Use% Mounted on ... /dev/mapper/vg1-lv1 12G 1.1G 11G 10% /mnt/lv1 # 同步完成
补充:扩展xfs的文件系统,使用xfs_growfs /mnt/lv1
当然,以上的步骤可以使用lvextend -r -l +100%FREE /dev/vg1/lv1实现:在全部扩展VG的基础上并且同步文件系统。
<9>扩展之后,查看PV,并且有需要更换/dev/sdc1硬盘来提高磁盘的性能或者增加磁盘容量
# 查看PV使用情况 [root@centos7/mnt/lv1]#pvdisplay --- Physical volume --- PV Name /dev/sdc1 VG Name vg1 PV Size 3.00 GiB / not usable 4.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 767 # 2G空间,占用一部分PV Free PE 254 Allocated PE 513 PV UUID vrAd2v-StgB-DmLw-y284-3sJy-dLQu-e4KQwc --- Physical volume --- PV Name /dev/sdc2 VG Name vg1 PV Size 5.00 GiB / not usable 4.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 1279 # PV并没有使用,需要转移sdc1的PE至此。sdc2(PV)>sdc1(PV) Free PE 1279 Allocated PE 0 PV UUID 255jQO-0mTo-eeQF-NUAc-tmAB-1rFF-8419at --- Physical volume --- PV Name /dev/sdc3 VG Name vg1 PV Size 10.00 GiB / not usable 4.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 2559 # 10G空间全部被分完 Free PE 0 Allocated PE 2559 PV UUID eBQJHR-wfJH-PDs5-4XGo-3Z8s-Vy2Y-UFzvs9 ... # 转移PV [root@centos7~]#pvmove /dev/sdc1 /dev/sdc1: Moved: 2.3% /dev/sdc1: Moved: 100.0% # 转移/dev/sdc1的PV # 再次查看PV [root@centos7~]#pvdisplay --- Physical volume --- PV Name /dev/sdc1 VG Name vg1 PV Size 3.00 GiB / not usable 4.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 767 # 已经转移,磁盘空间重置 Free PE 767 Allocated PE 0 PV UUID vrAd2v-StgB-DmLw-y284-3sJy-dLQu-e4KQwc --- Physical volume --- PV Name /dev/sdc2 VG Name vg1 PV Size 5.00 GiB / not usable 4.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 1279 # 已经将sdc1的PV转移至此 Free PE 766 Allocated PE 513 PV UUID 255jQO-0mTo-eeQF-NUAc-tmAB-1rFF-8419at --- Physical volume --- PV Name /dev/sdc3 VG Name vg1 PV Size 10.00 GiB / not usable 4.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 2559 Free PE 0 Allocated PE 2559 PV UUID eBQJHR-wfJH-PDs5-4XGo-3Z8s-Vy2Y-UFzvs9 # 将/dev/sdc1从vg1中移除 [root@centos7~]#vgreduce vg1 /dev/sdc1 Removed "/dev/sdc1" from volume group "vg1" ###### 已移除 [root@centos7~]#pvs PV VG Fmt Attr PSize PFree /dev/sda2 centos lvm2 a-- 122.00g 0 /dev/sdb2 lvm2 --- 6.00g 6.00g /dev/sdb3 lvm2 --- 3.00g 3.00g /dev/sdc1 lvm2 --- 3.00g 3.00g # /dev/sdc1已不再属于任何VG /dev/sdc2 vg1 lvm2 a-- 5.00g 2.99g /dev/sdc3 vg1 lvm2 a-- 10.00g 0 # 删除PV [root@centos7~]#pvremove /dev/sdc1 Labels on physical volume "/dev/sdc1" successfully wiped [root@centos7~]#pvs PV VG Fmt Attr PSize PFree /dev/sda2 centos lvm2 a-- 122.00g 0 /dev/sdb2 lvm2 --- 6.00g 6.00g /dev/sdb3 lvm2 --- 3.00g 3.00g /dev/sdc2 vg1 lvm2 a-- 5.00g 2.99g # /dev/sdc1PV删除成功 /dev/sdc3 vg1 lvm2 a-- 10.00g 0
<10>如果扩展LV的时候,发现VG空间不足,可继续扩展VG,步骤如下
# 添加一块新的磁盘或者分区,此处以分区为例,与<1>相同 # 再次创建PV [root@centos7~]#pvcreate /dev/sdc5 Physical volume "/dev/sdc5" successfully created [root@centos7~]#pvs PV VG Fmt Attr PSize PFree ... /dev/sdc1 vg1 lvm2 a-- 3.00g 1016.00m /dev/sdc2 vg1 lvm2 a-- 5.00g 5.00g /dev/sdc3 vg1 lvm2 a-- 10.00g 0 /dev/sdc5 lvm2 --- 1.00g 1.00g # 再次将其加入卷组vgextend vg1 /dev/sdc5 # 当然,如果VG空间足够,也就不需要这一步骤了
<11>如果有需要缩减逻辑卷的话,步骤如下(注:严格遵守以下五步骤)
# 首先建议:缩减之间尽量备份 # 卸载 [root@centos7~]#umount /dev/vg1/lv1 # 检查文件系统 [root@centos7~]#fsck -f /dev/vg1/lv1 fsck from util-linux 2.23.2 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/mapper/vg1-lv1: 12/786432 files (0.0% non-contiguous), 354533/3145728 blocks # 缩减文件系统 [root@centos7~]#resize2fs /dev/vg1/lv1 10G resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/vg1/lv1 to 2621440 (4k) blocks. The filesystem on /dev/vg1/lv1 is now 2621440 blocks long. # 缩减逻辑卷 [root@centos7~]#lvreduce -L 10G /dev/vg1/lv1 WARNING: Reducing active logical volume to 10.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce lv1? [y/n]: y Size of logical volume vg1/lv1 changed from 12.00 GiB (3072 extents) to 10.00 GiB (2560 extents). Logical volume lv1 successfully resized. # 挂载 [root@centos7~]#mount /dev/vg1/lv1 /mnt/lv1 完成
5、LVM快照
快照是对逻辑卷上的数据进行备份的空间,但是只备份修改的数据,最大空间与LVM相同。
<1>创建快照
[root@centos7~]#lvcreate -n lv1-snapshot -L 4G -p r -s /dev/vg1/lv1 # 先创建4G,默认只读 Logical volume "lv1-snapshot" created.
<2>建立目录
[root@centos7~]#mkdir /mnt/lv1-snapshot
<3>查看逻辑卷大小
[root@centos7/mnt/lv1-snapshot]#df Filesystem 1K-blocks Used Available Use% Mounted on ... /dev/mapper/vg1-lv1 10190100 1085468 8563964 12% /mnt/lv1 tmpfs 100136 0 100136 0% /run/user/0 /dev/mapper/vg1-lv1--snapshot 10190100 1085468 8563964 12% /mnt/lv1-snapshot # 和lv1大小相同 [root@centos7/mnt/lv1]#ls f1 lost+found [root@centos7/mnt/lv1-snapshot]#ls f1 lost+found # 虽然表面上和原始目录的文件大小一样,但实际并不占用空间
[root@centos7/mnt/lv1-snapshot]#lvdisplay ... --- Logical volume --- ... LV Size 10.00 GiB Current LE 2560 COW-table size 4.00 GiB COW-table LE 1024 Allocated to snapshot 0.00% # lvdisplay验证:快照空间并未使用 Snapshot chunk size 4.00 KiB Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:
<4>修改数据
[root@centos7/mnt/lv1]#dd if=/dev/zero of=f2 bs=1M count=500 # 添加数据 500+0 records in 500+0 records out 524288000 bytes (524 MB) copied, 2.12795 s, 246 MB/s
<5>再次查看逻辑卷
[root@centos7/mnt/lv1-snapshot]#lvdisplay ... --- Logical volume --- ... # open 1 LV Size 10.00 GiB Current LE 2560 COW-table size 4.00 GiB COW-table LE 1024 Allocated to snapshot 12.26% # 空间已经被占用,存储的是修改的数据 Snapshot chunk size 4.00 KiB Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:6 ... [root@centos7~]#df -h Filesystem Size Used Avail Use% Mounted on ... /dev/mapper/vg1-lv1 9.8G 1.6G 7.7G 17% /mnt/lv1 tmpfs 98M 0 98M 0% /run/user/0 /dev/mapper/vg1-lv1--snapshot 9.8G 1.1G 8.2G 12% /mnt/lv1-snapshot # 依然占用12% [root@centos7/mnt/lv1]#ls f1 f2 lost+found [root@centos7/mnt/lv1-snapshot]#ls f1 lost+found
6、删除操作
不论是LV,还是snapshot,创建之后的如果要删除,是不可以直接删除PV的,要在卸载的基础上删除。并且Centos7之后,可以直接删除VG,来很方便的删除LV和snapshot,如果系统不支持,可以先删除LV或者snapshot,再删除VG,PV
原创文章,作者:mfwing,如若转载,请注明出处:http://www.178linux.com/42772