最近想了解一下lvm(logcal volume manager)快照,通过lvm快照能对数据库或系统快速的备份,首先先了解一下lvm,再建一个测试环境。
工作原理:
LVM在每个物理卷头部都维护了一个metadata,每个metadata中都包含了整个VG(volume group:卷组)的信息,包括每个VG的布局配置,PV(physical volume:物理卷)的编号,LV(logical volume:逻辑卷)的编号,以及每个PE(physical extends:物理扩展单元)到LE(logical extends:物理扩展单元)的映射关系。同一个VG中的每个PV头部的信息都是相同的,这样有利于故障时进行数据恢复。
LVM对上层文件系统提供LV层,隐藏了操作细节。对文件系统而言,对LV的操作与原先对partition的操作没有差别。当对LV进行写入操作的时候,LVM定位相应的LE,通过PV头部的映射表将数据写入到相应的PE上。LVM实现的关LVM最大的特点就是可以对磁盘进行动态管理。因为逻辑卷的大小是可以动态调整的,而且不会丢失现有的数据。我们如果新增加了硬盘,其也不会改变现有上层的逻辑卷。键在于PE和LE之间建立映射关系,不同的映射规则决定了不同的LVM存储模型。LVM支持多个PV 的stripe和mirror。
LVM最大的特点就是可以对磁盘进行动态管理,因为逻辑卷的大小是可以动态调整的,而且不会丢失现有的数据,如果我们增加了硬盘也不会改变现有的上层逻辑卷。
下面说下LVM的优缺点
优点:
1. 文件系统可以跨多个磁盘,因此文件系统大小不会受物理磁盘的限制。
2. 可以在系统运行的状态下动态的扩展文件系统的大小。
3. 可以增加新的磁盘到LVM的存储池中。
4. 可以以镜像的方式冗余重要的数据到多个物理磁盘。
5. 可以方便的导出整个卷组到另外一台机器。
缺点:
1. 在从卷组中移除一个磁盘的时候必须使用reducevg命令(这个命令要求root权限,并且不允许在快照卷组中使用)。
2. 当卷组中的一个磁盘损坏时,整个卷组都会受到影响。
3. 因为加入了额外的操作,存贮性能受到影响。
下面建立一个lvm的环境:
linux版本为rhel6.3
[root@qht131 ~]# uname -a
Linux qht131 2.6.32-279.el6.x86_64 #1 SMP Wed Jun 13 18:24:36 EDT 2012 x86_64 x86_64 x86_64 GNU/Linux
[root@qht131 ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.3 (Santiago)
虚拟机增加了3个硬盘,每个1G大小,/dev/sda,/dev/sdb,/dev/sdc
[root@qht131 ~]# fdisk -l
Disk /dev/sda: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x00000000
Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x00000000
Disk /dev/sdc: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x00000000
Disk /dev/sdd: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x0007ff8c
Device Boot Start End Blocks Id System
/dev/sdd1 * 1 13 102400 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sdd2 13 77 512000 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sdd3 77 2611 20356096 83 Linux
1.通过分区fdisk工具将三个磁盘分区,格式为8e的linux LVM格式
[root@qht131 ~]# fdisk -l
Disk /dev/sda: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x7b78cf44
Device Boot Start End Blocks Id System
/dev/sda1 1 130 1044193+ 8e Linux LVM
Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0xb7130ee3
Device Boot Start End Blocks Id System
/dev/sdb1 1 130 1044193+ 8e Linux LVM
Disk /dev/sdc: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x3c1d9954
Device Boot Start End Blocks Id System
/dev/sdc1 1 130 1044193+ 8e Linux LVM
Disk /dev/sdd: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x0007ff8c
Device Boot Start End Blocks Id System
/dev/sdd1 * 1 13 102400 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sdd2 13 77 512000 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sdd3 77 2611 20356096 83 Linux
2.创建物理卷(PV)
将其中的两个区先创建物理卷
[root@qht131 ~]# pvcreate /dev/sda1
Writing physical volume data to disk "/dev/sda1"
Physical volume "/dev/sda1" successfully created
[root@qht131 ~]# pvcreate /dev/sdb1
Writing physical volume data to disk "/dev/sdb1"
Physical volume "/dev/sdb1" successfully created
3.创建卷组(VG)
[root@qht131 ~]# vgcreate vg_group /dev/sda1 /dev/sdb1
Volume group "vg_group" successfully created
现在已创建了一个卷组vg_group,可以通过vgdisplay检查一下卷组的状态:
[root@qht131 ~]# vgdisplay -v vg_group
Using volume group(s) on command line
Finding volume group "vg_group"
--- Volume group ---
VG Name vg_group
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 1.98 GiB
PE Size 4.00 MiB --PE物理块默认的是4M大小
Total PE 508
Alloc PE / Size 0 / 0
Free PE / Size 508 / 1.98 GiB
VG UUID CsDN4x-C64q-8VkY-9vBd-hxRp-36ne-KRCvKW
--- Physical volumes ---
PV Name /dev/sda1
PV UUID bpH2YU-g1DV-LeBj-dwoE-u6F7-syk3-RzxLzN
PV Status allocatable
Total PE / Free PE 254 / 254
PV Name /dev/sdb1
PV UUID Rk1Ci4-42Q9-PwMF-T19j-NlHC-Elkz-2wCaXX
PV Status allocatable
Total PE / Free PE 254 / 254
vgcreate命令第一个参数是指定该卷组的逻辑名:vg_group。后面参数是指定希望添加到该卷组的所有分区和磁盘。vgcreate 在创建卷组web_document以外,还设置使用大小为4MB的PE(默认为4MB),这表示卷组上创建的所有逻辑卷都以4MB为增量单位来进行扩充 或缩减。由于内核原因,PE大小决定了逻辑卷的最大大小,4MB的PE决定了单个逻辑卷最大容量为256GB,若希望使用大于256G的逻辑卷则创建卷组 时指定更大的PE。PE大小范围为8KB到512MB,并且必须总是2的倍数(使用-s指定,具体请参考manvgcreate)。(centos 6.2系统已发现没有这种限制)
4.激活卷组
为了立即使用卷组而不是重新启动系统,可以使用vgchange来激活卷组:
[root@qht131 ~]# vgchange -ay vg_group
0 logical volume(s) in volume group "vg_group" now active
顺便测试一下增加物理卷PV和删除物理卷,vgextend增加pv,vgreduce删除pv
[root@qht131 ~]# vgextend vg_group /dev/sdc1
No physical volume label read from /dev/sdc1
Writing physical volume data to disk "/dev/sdc1"
Physical volume "/dev/sdc1" successfully created
Volume group "vg_group" successfully extended
[root@qht131 ~]# vgdisplay -v vg_group
Using volume group(s) on command line
Finding volume group "vg_group"
--- Volume group ---
VG Name vg_group
System ID
Format lvm2
Metadata Areas 3
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 3
Act PV 3
VG Size 2.98 GiB --物理卷的数量和大小都发生了改变
PE Size 4.00 MiB
Total PE 762
Alloc PE / Size 0 / 0
Free PE / Size 762 / 2.98 GiB
VG UUID CsDN4x-C64q-8VkY-9vBd-hxRp-36ne-KRCvKW
--- Physical volumes ---
PV Name /dev/sda1
PV UUID bpH2YU-g1DV-LeBj-dwoE-u6F7-syk3-RzxLzN
PV Status allocatable
Total PE / Free PE 254 / 254
PV Name /dev/sdb1
PV UUID Rk1Ci4-42Q9-PwMF-T19j-NlHC-Elkz-2wCaXX
PV Status allocatable
Total PE / Free PE 254 / 254
PV Name /dev/sdc1
PV UUID sl0Qzh-ZLP3-5cCD-ZQ9t-9x3J-97c4-gxc7IR
PV Status allocatable
Total PE / Free PE 254 / 254
[root@qht131 ~]# vgreduce vg_group /dev/sdc1
Removed "/dev/sdc1" from volume group "vg_group"
[root@qht131 ~]# vgdisplay -v vg_group
Using volume group(s) on command line
Finding volume group "vg_group"
--- Volume group ---
VG Name vg_group
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 1.98 GiB
PE Size 4.00 MiB
Total PE 508
Alloc PE / Size 0 / 0
Free PE / Size 508 / 1.98 GiB
VG UUID CsDN4x-C64q-8VkY-9vBd-hxRp-36ne-KRCvKW
--- Physical volumes ---
PV Name /dev/sda1
PV UUID bpH2YU-g1DV-LeBj-dwoE-u6F7-syk3-RzxLzN
PV Status allocatable
Total PE / Free PE 254 / 254
PV Name /dev/sdb1
PV UUID Rk1Ci4-42Q9-PwMF-T19j-NlHC-Elkz-2wCaXX
PV Status allocatable
Total PE / Free PE 254 / 254
5.创建逻辑卷(LV)
首先检查vg一共有多少个物理块(PE),默认每个PE是4M大小
[root@qht131 ~]# vgdisplay vg_group | grep "Total PE"
Total PE 508
-L指定逻辑卷的大小,单位M(--size LogicalVolumeSize),-n指定逻辑卷的名称
这里建立了一个200M大小的LV
[root@qht131 ~]# lvcreate -L 200 -n data1 vg_group
Logical volume "data1" created
6.创建文件系统并挂载:
[root@qht131 ~]# mke2fs /dev/vg_group/data1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
51200 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
25 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@qht131 ~]# mkdir /data1
[root@qht131 ~]# mount /dev/vg_group/data1 /data1
[root@qht131 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sdd3 20G 5.9G 13G 33% /
tmpfs 499M 264K 499M 1% /dev/shm
/dev/sdd1 97M 28M 65M 31% /boot
/dev/mapper/vg_group-data1
194M 1.6M 183M 1% /data1
如果希望系统启动时自动加载文件系统,则还需要在/etc/fstab中添加内容。
7.扩展逻辑卷(LV),当前data1的逻辑卷只有200M,将其扩展到占用整个VG(2T)。
[root@qht131 ~]# lvextend -L 2G /dev/vg_group/data1
Extending logical volume data1 to 2.00 GiB
Insufficient free space: 462 extents needed, but only 458 available --扩展到2G时报错,vg管理需要使用几个块
[root@qht131 ~]# lvextend -L 1.98G /dev/vg_group/data1
Rounding size to boundary between physical extents: 1.98 GiB
Extending logical volume data1 to 1.98 GiB
Logical volume data1 successfully resized在
在lv扩容后,需要将相对应的文件系统也扩容,对于不同的文件系统使用相对应的扩容办法,下面使用resize2fs来扩容文件系统。
需要先将文件系统umount后再进行操作:
[root@qht131 ~]# umount /data1
[root@qht131 ~]# resize2fs /dev/vg_group/data1
resize2fs 1.41.12 (17-May-2010)
Please run 'e2fsck -f /dev/vg_group/data1' first.
[root@qht131 ~]# e2fsck -f /dev/vg_group/data1
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/vg_group/data1: 11/51200 files (0.0% non-contiguous), 8013/204800 blocks
[root@qht131 ~]# resize2fs /dev/vg_group/data1
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg_group/data1 to 2076672 (1k) blocks.
The filesystem on /dev/vg_group/data1 is now 2076672 blocks long.
[root@qht131 ~]# mount /dev/vg_group/data1 /data1
[root@qht131 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sdd3 20G 5.9G 13G 33% /
tmpfs 499M 264K 499M 1% /dev/shm
/dev/sdd1 97M 28M 65M 31% /boot
/dev/mapper/vg_group-data1
2.0G 3.0M 1.9G 1% /data1
逻辑卷/dev/vg_group_data1的容量已成功扩展到2G。
8.最后测试一下增加VG卷组,将将增加的容量增加到逻辑卷中:
[root@qht131 data1]# vgextend vg_group /dev/sdc1
Volume group "vg_group" successfully extended
[root@qht131 data1]# vgdisplay -v vg_group
Using volume group(s) on command line
Finding volume group "vg_group"
--- Volume group ---
VG Name vg_group
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 6
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 3
Act PV 3
VG Size 2.98 GiB
PE Size 4.00 MiB
Total PE 762
Alloc PE / Size 507 / 1.98 GiB
Free PE / Size 255 / 1020.00 MiB
VG UUID CsDN4x-C64q-8VkY-9vBd-hxRp-36ne-KRCvKW
--- Logical volume ---
LV Path /dev/vg_group/data1
LV Name data1
VG Name vg_group
LV UUID K4LO6e-v1CB-Dt0m-qwjb-GgfY-mI38-8ajE8b
LV Write Access read/write
LV Creation host, time qht131, 2018-04-16 22:47:19 +0800
LV Status available
# open 1
LV Size 1.98 GiB
Current LE 507
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
--- Physical volumes ---
PV Name /dev/sda1
PV UUID bpH2YU-g1DV-LeBj-dwoE-u6F7-syk3-RzxLzN
PV Status allocatable
Total PE / Free PE 254 / 0
PV Name /dev/sdb1
PV UUID Rk1Ci4-42Q9-PwMF-T19j-NlHC-Elkz-2wCaXX
PV Status allocatable
Total PE / Free PE 254 / 1
PV Name /dev/sdc1
PV UUID sl0Qzh-ZLP3-5cCD-ZQ9t-9x3J-97c4-gxc7IR
PV Status allocatable
Total PE / Free PE 254 / 254
[root@qht131 data1]# lvextend -L +1020M /dev/vg_group/data1
Extending logical volume data1 to 2.98 GiB
Logical volume data1 successfully resized
[root@qht131 /]# umount /data1
[root@qht131 /]# e2fsck -f /dev/vg_group/data1
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/vg_group/data1: 12/520192 files (0.0% non-contiguous), 68644/2076672 blocks
[root@qht131 /]# resize2fs /dev/vg_group/data1
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg_group/data1 to 3121152 (1k) blocks.
The filesystem on /dev/vg_group/data1 is now 3121152 blocks long.
[root@qht131 /]# mount /dev/vg_group/data1 /data1
[root@qht131 /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sdd3 20G 5.9G 13G 33% /
tmpfs 499M 264K 499M 1% /dev/shm
/dev/sdd1 97M 28M 65M 31% /boot
/dev/mapper/vg_group-data1
2.9G 3.2M 2.8G 1% /data1
新增加的物理卷已应用到逻辑卷并挂载。
参考: https://baike.baidu.com/item/LVM/6571177?fr=aladdin