一、LVM的基本概念
LVM(Logical Volume Manager,逻辑卷管理器)是Linux下对磁盘分区进行管理的一种机制。LVM是建立在磁盘分区与文件系统之上的一个逻辑层。其主要特点是可以动态的在不重新分区的情况下调整分区大小,比如当业务数据快要占满磁盘时,这时需要增加硬盘,LVM就可以将新增的硬盘空间直接扩展到原来的磁盘分区上。
LVM在centos 6以后升级为LVM2,但是查看其帮助文档还是通过“man lvm”来实现。
LVM除了在动态扩容方面有优势,还有快照功能,可以瞬间备份某一时刻的文件。
二、LVM中的术语
物理卷:(Physical Volume,PV),指硬盘分区或者从逻辑上看与硬盘分区类似的设备。
卷组:( Volume Group,VG),类似与非LVM系统上的物理硬盘,一个LVM卷组由一个或多个PV组成。
逻辑卷:(Logical Volume,LV),类似于非LVM系统上的硬盘分区,LV建立在VG上,可以在LV上面创建文件系统。
PE(Physical Extent,PE),PV中可以分配的最小存储单元。PE大小可以指定(默认为4M)
LE(Logical Extent,LE),LV中可以分配的最小单元。同一个卷组中LE与PE的大小是一样的,且一一对应。
下图清楚的说明了LVM各自组成部分之间的对应关系。
三、LVM创建与管理
首先,建议各位在使用时,现在测试机上面测试好再进行操作。
我这里新添加了一块硬盘为/dev/sdc,分为5个区,其中/dev/sdc4为扩展分区,此次将对/dev/sdc1、/dev/sdc2、/dev/sdc3、/dev/sdc5操作。
1、lvm管理命令中的查看命令
# 此处放在前面说是因为在创建之后需要进行查看验证,此处不做演示,后面实战中会演示其功能
a、pv(物理卷)查看工具
pvs [device]用于查看当前pv,可指定设备查看
pvdisplay [device]查看pv的详细信息,可指定设备查看
b、vg(卷组)查看工具
vgs [device]用于查看当前vg,可指定设备查看
vgdisplay [device]用于查看当前vg的详细信息,可指定设备查看
c、lv(逻辑卷)查看工具
lvs [device]用于查看当前lv,可指定设备查看
lvdisplay [device]用于查看当前lv的详细信息,可指定设备查看
2、创建与删除pv
创建PV
pvcreate /dev/DEVICE
# pvcreate /dev/sdc{1,2,3,5} Physical volume "/dev/sdc1" successfully created Physical volume "/dev/sdc2" successfully created Physical volume "/dev/sdc3" successfully created Physical volume "/dev/sdc5" successfully created # 查看当前系统上所有的pv # pvs PV VG Fmt Attr PSize PFree /dev/sdc1 lvm2 --- 2.00g 2.00g /dev/sdc2 lvm2 --- 3.00g 3.00g /dev/sdc3 lvm2 --- 4.00g 4.00g /dev/sdc5 lvm2 --- 6.00g 6.00g #查看每个pv的详细信息 # pvdisplay "/dev/sdc5" is a new physical volume of "6.00 GiB" --- NEW Physical volume --- PV Name /dev/sdc5 VG Name PV Size 6.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID o9mAZH-XW6M-B3qe-UUbG-62Xs-i10j-7kWJrs "/dev/sdc1" is a new physical volume of "2.00 GiB" --- NEW Physical volume --- PV Name /dev/sdc1 VG Name PV Size 2.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID f1S5ly-SjTw-SeJP-lRQ6-INWD-wSTP-VZbKgf "/dev/sdc3" is a new physical volume of "4.00 GiB" --- NEW Physical volume --- PV Name /dev/sdc3 VG Name PV Size 4.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID u0JyWo-DQ0j-YmVQ-BlQl-mADE-CUHO-Z8vHZW "/dev/sdc2" is a new physical volume of "3.00 GiB" --- NEW Physical volume --- PV Name /dev/sdc2 VG Name PV Size 3.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID e2bWFM-cSc8-3rf2-v0q2-xiBC-63fc-acbdsC
删除pv(注意:如果在使用的pv不能直接移除,需要使用pvmove命令将其数据移除完毕后在用pvremove移除)
pvremove /dev/DEVICE
# pvremove /dev/sdc1 Labels on physical volume "/dev/sdc1" successfully wiped # pvs PV VG Fmt Attr PSize PFree /dev/sdc2 lvm2 --- 3.00g 3.00g /dev/sdc3 lvm2 --- 4.00g 4.00g /dev/sdc5 lvm2 --- 6.00g 6.00g # pvremove /dev/sdc2 Labels on physical volume "/dev/sdc2" successfully wiped # pvs PV VG Fmt Attr PSize PFree /dev/sdc3 lvm2 --- 4.00g 4.00g /dev/sdc5 lvm2 --- 6.00g 6.00g
3、VG管理工具
创建VG:
vgcreate [-s #[kKmMgGtTpPeE]] VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]
-s:指定PE大小(默认为4M)
# vgcreate vg0 /dev/sdc{1,2,3,5} Volume group "vg0" successfully created #查看系统上所有的vg # vgs VG #PV #LV #SN Attr VSize VFree vg0 4 0 0 wz--n- 14.98g 14.98g #查看每个vg的详细信息 # vgdisplay --- Volume group --- VG Name vg0 System ID Format lvm2 Metadata Areas 4 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 4 Act PV 4 VG Size 14.98 GiB PE Size 4.00 MiB Total PE 3836 Alloc PE / Size 0 / 0 Free PE / Size 3836 / 14.98 GiB VG UUID p69GZ4-VGNi-IOaO-FGZp-csIy-713y-mwJyjg
扩展VG:
vgextend VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]
# pvs PV VG Fmt Attr PSize PFree /dev/sdc1 vg0 lvm2 a-- 2.00g 2.00g /dev/sdc2 vg0 lvm2 a-- 3.00g 3.00g /dev/sdc3 vg0 lvm2 a-- 4.00g 4.00g /dev/sdc5 vg0 lvm2 a-- 6.00g 6.00g # pvcreate /dev/sdc6 Physical volume "/dev/sdc6" successfully created # pvs PV VG Fmt Attr PSize PFree /dev/sdc1 vg0 lvm2 a-- 2.00g 2.00g /dev/sdc2 vg0 lvm2 a-- 3.00g 3.00g /dev/sdc3 vg0 lvm2 a-- 4.00g 4.00g /dev/sdc5 vg0 lvm2 a-- 6.00g 6.00g /dev/sdc6 vg0 lvm2 a-- 2.00g 2.00g # vgs VG #PV #LV #SN Attr VSize VFree vg0 4 0 0 wz--n- 14.98g 14.98g # vgextend vg0 /dev/sdc6 Volume group "vg0" successfully extended # vgs VG #PV #LV #SN Attr VSize VFree vg0 5 0 0 wz--n- 16.98g 16.98g
缩减VG:
# 注意在做缩减时,如果pv中有数据,必须先执行pvmove将数据移走后移除,此步骤不可少。此处为演示环境,所以没有数据。
vgreduce VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]
# vgs VG #PV #LV #SN Attr VSize VFree vg0 5 0 0 wz--n- 16.98g 16.98g # vgreduce vg0 /dev/sdc6 Removed "/dev/sdc6" from volume group "vg0" # vgs VG #PV #LV #SN Attr VSize VFree vg0 4 0 0 wz--n- 14.98g 14.98g
删除VG:
vgremove [VolumeGroupName...]
# vgs VG #PV #LV #SN Attr VSize VFree vg0 4 0 0 wz--n- 14.98g 14.98g # vgremove vg0 Volume group "vg0" successfully removed # vgs
4、LV管理工具
lv在缩减时必须注意每一个步骤不能出错,凡是有关数据的操作一定要100%确认后在执行。
创建lv:
# 注意:lv是基于vg的,而vg是基于pv的所以此处在创建lv时首先需要查看vg大小能不能容下你要创建的lv,不能则需要扩展。
lvcreate -L #[mMgGtT] -n NAME VolumeGroup
-L:指定lv大小
-n:指定lv名字
# vgs VG #PV #LV #SN Attr VSize VFree vg0 4 0 0 wz--n- 14.98g 14.98g # lvcreate -L 10G -n lv0 vg0 WARNING: xfs signature detected on /dev/vg0/lv0 at offset 0. Wipe it? [y/n]: y Wiping xfs signature on /dev/vg0/lv0. Logical volume "lv0" created. # lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv0 vg0 -wi-a----- 10.00g # lvdisplay --- Logical volume --- LV Path /dev/vg0/lv0 LV Name lv0 VG Name vg0 LV UUID FJ1TAj-oauO-oaol-k6yg-V5jE-BBF3-XsZeQs LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-03-17 21:08:26 +0800 LV Status available # open 0 LV Size 10.00 GiB Current LE 2560 Segments 3 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0
删除lv:
# 注意:移除lv时也要先将其中数据移除
lvremove /dev/VG_NAME/LV_NAME
# lvremove /dev/vg0/lv0 Do you really want to remove active logical volume lv0? [y/n]: y Logical volume "lv0" successfully removed # lvs
扩展lv:
# lv在扩展时可以动态扩展,所以不需要卸载文件系统
# 此处需要先建立文件系统并挂载
# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv0 vg0 -wi-a----- 10.00g # mkfs.ext4 /dev/vg0/lv0 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 655360 inodes, 2621440 blocks 131072 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2151677952 80 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, 1605632 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done # mount /dev/vg0/lv0 /backup/ # df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/sda2 xfs 40G 180M 40G 1% / devtmpfs devtmpfs 475M 0 475M 0% /dev tmpfs tmpfs 489M 0 489M 0% /dev/shm tmpfs tmpfs 489M 6.9M 483M 2% /run tmpfs tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda3 xfs 20G 2.6G 18G 13% /usr /dev/md127 ext4 9.8G 37M 9.2G 1% /raid /dev/sda1 xfs 497M 138M 359M 28% /boot tmpfs tmpfs 98M 0 98M 0% /run/user/0 /dev/mapper/vg0-lv0 ext4 9.8G 37M 9.2G 1% /backup
a、扩展lv
# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/sda2 xfs 40G 180M 40G 1% / devtmpfs devtmpfs 475M 0 475M 0% /dev tmpfs tmpfs 489M 0 489M 0% /dev/shm tmpfs tmpfs 489M 6.9M 483M 2% /run tmpfs tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda3 xfs 20G 2.6G 18G 13% /usr /dev/md127 ext4 9.8G 37M 9.2G 1% /raid /dev/sda1 xfs 497M 138M 359M 28% /boot tmpfs tmpfs 98M 0 98M 0% /run/user/0 /dev/mapper/vg0-lv0 ext4 9.8G 37M 9.2G 1% /backup # lvextend -L 11G /dev/vg0/lv0 # df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/sda2 xfs 40G 180M 40G 1% / devtmpfs devtmpfs 475M 0 475M 0% /dev tmpfs tmpfs 489M 0 489M 0% /dev/shm tmpfs tmpfs 489M 6.9M 483M 2% /run tmpfs tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda3 xfs 20G 2.6G 18G 13% /usr /dev/md127 ext4 9.8G 37M 9.2G 1% /raid /dev/sda1 xfs 497M 138M 359M 28% /boot tmpfs tmpfs 98M 0 98M 0% /run/user/0 /dev/mapper/vg0-lv0 ext4 9.8G 37M 9.2G 1% /backup
此时查看并未扩展成功,因为lvm逻辑上扩展了,需要扩展文件系统
b、扩展文件系统
# resize2fs /dev/vg0/lv0 resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/vg0/lv0 is mounted on /backup; on-line resizing required old_desc_blocks = 2, new_desc_blocks = 2 The filesystem on /dev/vg0/lv0 is now 2883584 blocks long. # df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/sda2 xfs 40G 180M 40G 1% / devtmpfs devtmpfs 475M 0 475M 0% /dev tmpfs tmpfs 489M 0 489M 0% /dev/shm tmpfs tmpfs 489M 6.9M 483M 2% /run tmpfs tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda3 xfs 20G 2.6G 18G 13% /usr /dev/md127 ext4 9.8G 37M 9.2G 1% /raid /dev/sda1 xfs 497M 138M 359M 28% /boot tmpfs tmpfs 98M 0 98M 0% /run/user/0 /dev/mapper/vg0-lv0 ext4 11G 41M 11G 1% /backup
再次查看,成功扩展到11G !
缩减lv:
# 缩减lv时必须卸载兵器需要先缩减文件系统,然后在缩减lvm,另外缩减时必须先对系统进行检查,查看是否能够缩减且缩减后的容量能容下所存在的数据。
a、卸载
查看挂载点文件及其内容
# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/sda2 xfs 40G 180M 40G 1% / devtmpfs devtmpfs 475M 0 475M 0% /dev tmpfs tmpfs 489M 0 489M 0% /dev/shm tmpfs tmpfs 489M 6.9M 483M 2% /run tmpfs tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda3 xfs 20G 2.6G 18G 13% /usr /dev/md127 ext4 9.8G 37M 9.2G 1% /raid /dev/sda1 xfs 497M 138M 359M 28% /boot tmpfs tmpfs 98M 0 98M 0% /run/user/0 /dev/mapper/vg0-lv0 ext4 11G 41M 11G 1% /backup # ls /backup/ issue lost+found # cat issue \S Kernel \r on an \m Mage Education Learning Services http://www.magedu.com # test lvreduce
卸载
# umount /backup/ # df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/sda2 xfs 40G 180M 40G 1% / devtmpfs devtmpfs 475M 0 475M 0% /dev tmpfs tmpfs 489M 0 489M 0% /dev/shm tmpfs tmpfs 489M 6.9M 483M 2% /run tmpfs tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda3 xfs 20G 2.6G 18G 13% /usr /dev/md127 ext4 9.8G 37M 9.2G 1% /raid /dev/sda1 xfs 497M 138M 359M 28% /boot tmpfs tmpfs 98M 0 98M 0% /run/user/0
b、检查设备
# e2fsck -f /dev/vg0/lv0 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/vg0/lv0: 12/720896 files (0.0% non-contiguous), 88277/2883584 blocks
c、缩减文件系统大小
# resize2fs /dev/vg0/lv0 5G resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/vg0/lv0 to 1310720 (4k) blocks. The filesystem on /dev/vg0/lv0 is now 1310720 blocks long.
d、缩减lvm大小
# lvreduce -L 5G /dev/vg0/lv0 WARNING: Reducing active logical volume to 5.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce lv0? [y/n]: y Size of logical volume vg0/lv0 changed from 11.00 GiB (2816 extents) to 5.00 GiB (1280 extents). Logical volume lv0 successfully resized.
e、挂载并查看数据
# mount /dev/vg0/lv0 /backup/ [root@localhost ~]# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/sda2 xfs 40G 180M 40G 1% / devtmpfs devtmpfs 475M 0 475M 0% /dev tmpfs tmpfs 489M 0 489M 0% /dev/shm tmpfs tmpfs 489M 6.9M 483M 2% /run tmpfs tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda3 xfs 20G 2.6G 18G 13% /usr /dev/md127 ext4 9.8G 37M 9.2G 1% /raid /dev/sda1 xfs 497M 138M 359M 28% /boot tmpfs tmpfs 98M 0 98M 0% /run/user/0 /dev/mapper/vg0-lv0 ext4 4.8G 33M 4.5G 1% /backup # ls /backup/ issue lost+found # cat /backup/issue \S Kernel \r on an \m Mage Education Learning Services http://www.magedu.com # test lvreduce
缩减完毕,但是还是在这提醒一下各位,缩减是非常危险的操作,一定要确保万无一失的情况下进行,数据是无价的!!!
快照(snapshot):
# 快照是lvm的一个特点,他可以快速的备份某一时刻的状态,并将其保存主要是用作在线备份
# 注意两点:
1)快照其实也是一个逻辑卷
2)快照只能对逻辑卷LVM进行备份,并且只能对同一个卷组的逻辑卷进行备
3)快照卷复制的只是一个metadata(元数据)
# 快照卷的核心技术
写时复制(copy-on-write,COW):因为快照卷复制的是元数据,所以指向的data block与源
数据的相同,当有写操作对原有数据进行操作时,此时不会先写到元数据中,会把要写的数据复制到快照卷中一份,然后再对源数据进行更改,这就是写时复制。
命令:
快照卷的大小可以根据公司的数据量所决定,比如两小时内复制完毕,取一下平常两小时所写的磁盘空间。
lvcreate -L #[mMgGtT] -p r -s -n snapshot_lv_name original_lv_name
-p r:指定快照卷为只读
-s:创建快照卷
-n:指定快照卷名字
# lvcreate -L 2G -p r -s -n snap_lv1 /dev/vg0/lv0 Logical volume "snap_lv1" created. # mount /dev/vg0/snap_lv1 /bak/ mount: /dev/mapper/vg0-snap_lv1 is write-protected, mounting read-only # df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/sda2 xfs 40G 180M 40G 1% / devtmpfs devtmpfs 475M 0 475M 0% /dev tmpfs tmpfs 489M 0 489M 0% /dev/shm tmpfs tmpfs 489M 6.9M 483M 2% /run tmpfs tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda3 xfs 20G 2.6G 18G 13% /usr /dev/md127 ext4 9.8G 37M 9.2G 1% /raid /dev/sda1 xfs 497M 138M 359M 28% /boot tmpfs tmpfs 98M 0 98M 0% /run/user/0 /dev/mapper/vg0-lv0 ext4 4.8G 33M 4.5G 1% /backup /dev/mapper/vg0-snap_lv1 ext4 4.8G 33M 4.5G 1% /bak # ls /backup/ issue lost+found # ls /bak issue lost+found # diff /backup/issue /bak/issue # echo "test snap">>/backup/issue # diff /backup/issue /bak/issue 8d7 < test snap
此处可以使用diff命令发现快照卷中的数据并未改变
好,对于lvm的管理就到这里了,最后在�嗦一遍,所有对数据的操作都必须确保无误,可以请同事帮忙来看一下,以免造成不可挽回的错误。