一、简介
二、LVM原理图解
三、操作示例
四、快照
一、简介
LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制,它由Heinz Mauelshagen在Linux 2.4内核上实现。LVM是在磁盘分区和文件系统之间添加的一个逻辑层,来为文件系统屏蔽下层磁盘分区布局,提供一个抽象的存储卷,在存储卷上建立文件系统。当存储卷空间不足时,可以不用卸载存储卷而将其他硬盘分区加入其中,利用LVM可根据需求灵活管理磁盘空间。
二、LVM原理图解(图片点击后正常查看)
LVM的原理就是将分区或者磁盘转换为物理卷(Pysical Volume),然后将物理卷都加入一个卷组(Volume Group),我们只用通过相关的工具去管理这个抽象出来的卷组,将其划分为满足需求的逻辑卷(Logical Volume),就可灵活管理磁盘空间。
三、操作示例
相关命令:
创建PV:pvcreate /path/to/Device
创建VG:vgcreate VGNAME /path/to/Device
创建LV:lvcreate –L SIZE –n LVNAME VGNAME #-L指定大小,-n指定名称
扩展VG:vgextend VGNAME /path/to/Device
扩展LV物理大小:lvextend –L +SIZE /path/to/LVNAME
扩展LV逻辑容量:resize2fs /path/to/LVNAME
缩减LV物理大小:lvreduce -L –SIZE /path/to/LVNAME
转移PV数据:pvmove /path/to/Device
从VG中移除PV:vgreduce VGNAME /path/to/Device
1、了解文件系统状况
[root@TESTHOST ~]# fdisk –l #查看磁盘、分区信息 Disk /dev/sda: 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: 0x00018e93 Device Boot Start End Blocks Id System /dev/sda1 * 1 39 307200 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 39 692 5242880 83 Linux Partition 2 does not end on cylinder boundary. /dev/sda3 692 953 2097152 82 Linux swap / Solaris Partition 3 does not end on cylinder boundary. [root@TESTHOST ~]# mount #查看挂载情况 /dev/sda2 on / type ext4 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0") /dev/sda1 on /boot type ext4 (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
2、磁盘或分区的准备
[root@TESTHOST ~]# fdisk /dev/sda WARNING: DOS-compatible mode is deprecated. It‘s 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) e #这里创建了扩展分区,并新建两个文件系统,实操时没有限制,对象可以是任意磁盘任意分区的文件系统 Selected partition 4 First cylinder (953-2610, default 953): Using default value 953 Last cylinder, +cylinders or +size{K,M,G} (953-2610, default 2610): +8G Command (m for help): n First cylinder (953-1997, default 953): Using default value 953 Last cylinder, +cylinders or +size{K,M,G} (953-1997, default 1997): +2G Command (m for help): t #调整文件系统类型为8e Partition number (1-5): 5 Hex code (type L to list codes): 8e Changed system type of partition 5 to 8e (Linux LVM) Command (m for help): n First cylinder (1215-1997, default 1215): Using default value 1215 Last cylinder, +cylinders or +size{K,M,G} (1215-1997, default 1997): +2G Command (m for help): t #调整文件系统类型为8e Partition number (1-6): 6 Hex code (type L to list codes): 8e Changed system type of partition 6 to 8e (Linux LVM) Command (m for help): w #退出fdisk并保存 The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: 设备或资源忙. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks.
3、创建PV
[root@TESTHOST ~]# partx -a /dev/sda #使内核识别分区信息 BLKPG: Device or resource busy error adding partition 1 BLKPG: Device or resource busy error adding partition 2 BLKPG: Device or resource busy error adding partition 3 [root@TESTHOST ~]# cat /proc/partitions #查看是否已经识别 major minor #blocks name 8 0 20971520 sda 8 1 307200 sda1 8 2 5242880 sda2 8 3 2097152 sda3 8 4 31 sda4 8 5 2103167 sda5 8 6 2104483 sda6 [root@TESTHOST ~]# pvcreate /dev/sda5 #将分区创建为PV Physical volume "/dev/sda5" successfully created [root@TESTHOST ~]# pvcreate /dev/sda6 #将分区创建为PV Physical volume "/dev/sda6" successfully created [root@TESTHOST ~]# pvs #简单查看PV信息 PV VG Fmt Attr PSize PFree /dev/sda5 lvm2 a-- 2.01g 2.01g /dev/sda6 lvm2 a-- 2.01g 2.01g [root@TESTHOST ~]# pvdisplay #查看PV相关信息 "/dev/sda5" is a new physical volume of "2.01 GiB" --- NEW Physical volume --- PV Name /dev/sda5 VG Name PV Size 2.01 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID M49xnd-koFv-KYq6-gWz4-kwGE-vb0T-BjmIKn "/dev/sda6" is a new physical volume of "2.01 GiB" --- NEW Physical volume --- PV Name /dev/sda6 VG Name PV Size 2.01 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID Kx2vPd-jod7-t5WZ-mIMx-9GXe-37nY-IC8lrk
4、创建VG
[root@TESTHOST ~]# vgcreate vg1 /dev/sda{5,6} #创建名为vg1的卷组 Volume group "vg1" successfully created [root@TESTHOST ~]# vgs #简单查看vg信息 VG #PV #LV #SN Attr VSize VFree vg1 2 0 0 wz--n- 4.01g 4.01g [root@TESTHOST ~]# vgdisplay #查看vg相关信息 --- Volume group --- VG Name vg1 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 4.01 GiB PE Size 4.00 MiB Total PE 1026 Alloc PE / Size 0 / 0 Free PE / Size 1026 / 4.01 GiB VG UUID tVS1TX-7Csj-eZ6e-zOvT-QpZ1-RCjc-KZMfSY
5、在VG中创建LV
[root@TESTHOST ~]# lvcreate -L 1G -n lv1 vg1 #在vg1中创建名为lv1的逻辑卷 Logical volume "lv1" created [root@TESTHOST ~]# lvs #简单查看lv信息 LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert lv1 vg1 -wi-a----- 1.00g [root@TESTHOST ~]# lvdisplay #查看lv相关信息 --- Logical volume --- LV Path /dev/vg1/lv1 LV Name lv1 VG Name vg1 LV UUID GmctuV-gZFt-Eu5f-dRSc-CI3X-WVvB-702PgQ LV Write Access read/write LV Creation host, time TESTHOST, 2015-04-17 22:30:57 +0800 LV Status available # open 0 LV Size 1.00 GiB Current LE 256 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0
6、格式化后,挂载即可使用
[root@TESTHOST ~]# mke2fs -t ext4 /dev/vg1/lv1 #格式化 [root@TESTHOST ~]# mount /dev/vg1/lv1 /data1 #挂载 [root@TESTHOST ~]# mount #查看挂载信息 /dev/sda2 on / type ext4 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0") /dev/sda1 on /boot type ext4 (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) /dev/mapper/vg1-lv1 on /data1 type ext4 (rw) #挂载信息
7、当逻辑卷空间不够用时,先扩展卷组,再扩展逻辑卷
7.1、扩展卷组
[root@TESTHOST ~]# fdisk /dev/sda WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to switch off the mode (command ‘c‘) and change display units to sectors (command ‘u‘). Command (m for help): n #这里新增加一个文件系统 First cylinder (1477-1997, default 1477): Using default value 1477 Last cylinder, +cylinders or +size{K,M,G} (1477-1997, default 1997): +2G Command (m for help): t #同样调整文件系统类型为8e Partition number (1-7): 7 Hex code (type L to list codes): 8e Changed system type of partition 7 to 8e (Linux LVM) Command (m for help): w #保存分区信息并退出 The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: 设备或资源忙. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks. [root@TESTHOST ~]# partx -a /dev/sda #让内核识别分区表 BLKPG: Device or resource busy error adding partition 1 BLKPG: Device or resource busy error adding partition 2 BLKPG: Device or resource busy error adding partition 3 BLKPG: Device or resource busy error adding partition 4 BLKPG: Device or resource busy error adding partition 5 BLKPG: Device or resource busy error adding partition 6 [root@TESTHOST ~]# cat /proc/partitions #查看内核是否识别分区表 major minor #blocks name 8 0 20971520 sda 8 1 307200 sda1 8 2 5242880 sda2 8 3 2097152 sda3 8 4 31 sda4 8 5 2103167 sda5 8 6 2104483 sda6 8 7 2104483 sda7 #已识别 253 0 1048576 dm-0 253 1 1048576 dm-1 253 2 1048576 dm-2 253 3 1048576 dm-3 [root@TESTHOST ~]# vgextend vg1 /dev/sda7 #这里我直接扩展,但是没有先将分区创建为PV,不过程序自动帮我完成了这一过程,而且成功扩展了VG dev_is_mpath: failed to get device for 8:7 No physical volume label read from /dev/sda7 Physical volume /dev/sda7 not found Physical volume "/dev/sda7" successfully created [root@TESTHOST ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda5 vg1 lvm2 a-- 2.00g 4.00m /dev/sda6 vg1 lvm2 a-- 2.00g 4.00m /dev/sda7 vg1 lvm2 a-- 2.00g 2.00g #成功加入,扩展VG成功
7.2、扩展LV
需要先扩展其物理大小,然后扩展其逻辑容量
7.2.1、扩展LV物理大小
[root@TESTHOST ~]# lvextend -L +2G /dev/vg1/lv1 #扩展LV物理大小 Extending logical volume lv1 to 3.00 GiB Logical volume lv1 successfully resized [root@TESTHOST ~]# df –lh #查看磁盘使用情况 Filesystem Size Used Avail Use% Mounted on /dev/sda2 5.0G 1.2G 3.5G 26% / tmpfs 495M 0 495M 0% /dev/shm /dev/sda1 291M 32M 244M 12% /boot /dev/mapper/vg1-lv1 1008M 34M 924M 4% /data1 #此时可以看到lv1的大小还是1G
7.2.2、扩展LV逻辑容量
[root@TESTHOST ~]# resize2fs /dev/vg1/lv1 #扩展其逻辑容量 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/vg1/lv1 is mounted on /data1; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of /dev/vg1/lv1 to 786432 (4k) blocks. The filesystem on /dev/vg1/lv1 is now 786432 blocks long. [root@TESTHOST ~]# df -lh #查看磁盘使用情况 Filesystem Size Used Avail Use% Mounted on /dev/sda2 5.0G 1.2G 3.5G 26% / tmpfs 495M 0 495M 0% /dev/shm /dev/sda1 291M 32M 244M 12% /boot /dev/mapper/vg1-lv1 3.0G 34M 2.8G 2% /data1 #此时已经扩展成功
8、缩减LV
首先卸载LV并强行检测,然后
8.1、卸载LV并检测
[root@TESTHOST ~]# umount /dev/vg1/lv1 #卸载 [root@TESTHOST ~]# mount /dev/sda2 on / type ext4 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0") /dev/sda1 on /boot type ext4 (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) [root@TESTHOST ~]# e2fsck -f /dev/vg1/lv1 #强行检测 e2fsck 1.41.12 (17-May-2010) 第一步: 检查inode,块,和大小 第二步: 检查目录结构 第3步: 检查目录连接性 Pass 4: Checking reference counts 第5步: 检查簇概要信息 /dev/vg1/lv1: 11/196608 files (0.0% non-contiguous), 20924/786432 blocks
8.2、缩减逻辑容量
[root@TESTHOST ~]# resize2fs /dev/vg1/lv1 2G #缩减LV逻辑容量 resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/vg1/lv1 to 524288 (4k) blocks. The filesystem on /dev/vg1/lv1 is now 524288 blocks long. [root@TESTHOST ~]# lvs #查看LV信息,此时lv1大小仍是3GB LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert lv1 vg1 -wi-a----- 3.00g lv2 vg1 -wi-a----- 1.00g lv3 vg1 -wi-a----- 1.00g lv4 vg1 -wi-a----- 1.00g
8.3、缩减物理大小
[root@TESTHOST ~]# lvreduce -L -1G /dev/vg1/lv1 #缩减物理大小 WARNING: Reducing active logical volume to 2.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce lv1? [y/n]: y #提示是否缩小lv1,选择y Reducing logical volume lv1 to 2.00 GiB Logical volume lv1 successfully resized [root@TESTHOST ~]# lvs #此时已经看到lv1大小已经是2GB了 LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert lv1 vg1 -wi-a----- 2.00g lv2 vg1 -wi-a----- 1.00g lv3 vg1 -wi-a----- 1.00g lv4 vg1 -wi-a----- 1.00g
9、缩减VG
确定要移除的PV,将PV上的数据转移到其他PV(前提是移除PV后的VG容量足够存储原来的数据),从卷组中移除该PV
[root@TESTHOST ~]# vgs #移除PV前VG大小6GB VG #PV #LV #SN Attr VSize VFree vg1 3 1 0 wz--n- 6.01g 4.01g [root@TESTHOST ~]# pvmove /dev/sda7 #转移PV上的数据 /dev/sda7: Moved: 1.2% /dev/sda7: Moved: 85.5% /dev/sda7: Moved: 100.0% #进度提示 [root@TESTHOST ~]# vgreduce vg1 /dev/sda7 #从VG中移除PV Removed "/dev/sda7" from volume group "vg1" #移除成功 [root@TESTHOST ~]# vgs #VG容量已变为4GB VG #PV #LV #SN Attr VSize VFree vg1 2 1 0 wz--n- 4.01g 2.01g
四、快照
快照是LVM另一个重要功能,常用于备份,其工作原理是对LV创建快照后,LV上数据变动前,会在快照上先备份
命令:lvcreate –L SIZE –p r –n mylv-snap /dev/vg1/lv1
参数:-s 创建快照
-p 指定权限,后接r只读
-n 指定快照卷名称
示例1:创建快照
[root@TESTHOST date1]# cp -R /etc/init/ ./ #将文件夹拷贝到LV中 [root@TESTHOST date1]# ls ./init/ #查看文件夹明细 control-alt-delete.conf prefdm.conf rcS-emergency.conf start-ttys.conf init-system-dbus.conf quit-plymouth.conf rcS-sulogin.conf tty.conf kexec-disable.conf rc.conf serial.conf plymouth-shutdown.conf rcS.conf splash-manager.conf [root@TESTHOST date1]# lvcreate -L 50M -s -p r -n mylv-snap /dev/vg1/lv1 #对vg1创建快照卷,-L指定卷大小,-s指定创建类型为快照卷,-p指定卷读写权限,-n指定快照卷名称 Rounding up size to full physical extent 52.00 MiB Logical volume "mylv-snap" created #创建成功 [root@TESTHOST date1]# ls /dev/vg1 #创建后路径在/dev/vg1/下 lv1 mylv-snap [root@TESTHOST date1]# mount -o ro /dev/vg1/mylv-snap /mnt/ #将快照卷挂载到/mnt下 [root@TESTHOST date1]# ls /mnt/init/ #此时可以看到快照卷中内容同lv1中内容相同 control-alt-delete.conf prefdm.conf rcS-emergency.conf start-ttys.conf init-system-dbus.conf quit-plymouth.conf rcS-sulogin.conf tty.conf kexec-disable.conf rc.conf serial.conf plymouth-shutdown.conf rcS.conf splash-manager.conf
示例2:修改内容,下面操作说明,在我们更改LV中内容时,其快照卷在修改前备份了其上内容(修改前的状态)
[root@TESTHOST date1]# rm -rf ./init/ #删除lv1中init文件夹及其所有内容 [root@TESTHOST date1]# ls #查看,确实都删除了 lost+found [root@TESTHOST date1]# ll-a /mnt/init/ #查看快照卷,里面内容依然存在 总用量 64 drwxr-xr-x. 2 root root 4096 4月 18 00:57 . drwxr-xr-x. 4 root root 4096 4月 18 02:01 .. -rw-r--r--. 1 root root 412 4月 18 00:57 control-alt-delete.conf -rw-r--r--. 1 root root 130 4月 18 00:57 init-system-dbus.conf -rw-r--r--. 1 root root 463 4月 18 00:57 kexec-disable.conf -rw-r--r--. 1 root root 560 4月 18 00:57 plymouth-shutdown.conf -rw-r--r--. 1 root root 357 4月 18 00:57 prefdm.conf -rw-r--r--. 1 root root 505 4月 18 00:57 quit-plymouth.conf -rw-r--r--. 1 root root 417 4月 18 00:57 rc.conf -rw-r--r--. 1 root root 1046 4月 18 00:57 rcS.conf -rw-r--r--. 1 root root 430 4月 18 00:57 rcS-emergency.conf -rw-r--r--. 1 root root 725 4月 18 00:57 rcS-sulogin.conf -rw-r--r--. 1 root root 1302 4月 18 00:57 serial.conf -rw-r--r--. 1 root root 791 4月 18 00:57 splash-manager.conf -rw-r--r--. 1 root root 473 4月 18 00:57 start-ttys.conf -rw-r--r--. 1 root root 335 4月 18 00:57 tty.conf
示例3:备份和还原
[root@TESTHOST date1]# mkdir /tmp/lvbackup #创建一个文件夹放备份文件 [root@TESTHOST date1]# cd /mnt/ [root@TESTHOST mnt]# tar Jcf /tmp/lvbackup/backup-`date +%F-%H-%M-%S`.tar.xz ./init/ #备份快照 tar: 从成员名中删除开头的“/” [root@TESTHOST mnt]# ls /tmp/lvbackup/ #已备份 backup-2015-04-18-01-59-43.tar.xz [root@TESTHOST mnt]# tar xf /tmp/lvbackup/backup-2015-04-18-01-59-43.tar.xz -C /data1/ #恢复到指定目录 [root@TESTHOST mnt]# ls /data1/ init [root@TESTHOST mnt]# ll -a /date1/init/ #文件还原成功 总用量 64 drwxr-xr-x. 2 root root 4096 4月 18 00:57 . drwxr-xr-x. 4 root root 4096 4月 18 02:01 .. -rw-r--r--. 1 root root 412 4月 18 00:57 control-alt-delete.conf -rw-r--r--. 1 root root 130 4月 18 00:57 init-system-dbus.conf -rw-r--r--. 1 root root 463 4月 18 00:57 kexec-disable.conf -rw-r--r--. 1 root root 560 4月 18 00:57 plymouth-shutdown.conf -rw-r--r--. 1 root root 357 4月 18 00:57 prefdm.conf -rw-r--r--. 1 root root 505 4月 18 00:57 quit-plymouth.conf -rw-r--r--. 1 root root 417 4月 18 00:57 rc.conf -rw-r--r--. 1 root root 1046 4月 18 00:57 rcS.conf -rw-r--r--. 1 root root 430 4月 18 00:57 rcS-emergency.conf -rw-r--r--. 1 root root 725 4月 18 00:57 rcS-sulogin.conf -rw-r--r--. 1 root root 1302 4月 18 00:57 serial.conf -rw-r--r--. 1 root root 791 4月 18 00:57 splash-manager.conf -rw-r--r--. 1 root root 473 4月 18 00:57 start-ttys.conf -rw-r--r--. 1 root root 335 4月 18 00:57 tty.conf
本文出自 “Arvin Lau” 博客,请务必保留此出处http://64314491.blog.51cto.com/2784219/1633961
原文地址:http://64314491.blog.51cto.com/2784219/1633961