标签:lvm(逻辑卷管理)
一、简介
LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是基于内核的一种逻辑卷管理器,LVM适合于管理大存储设备,并允许用户动态调整文件系统大小。此外,LVM的快照功能可以帮助我们快速备份数据。LVM为我们提供了逻辑概念上的磁盘,使得文件系统不在关心底层物理磁盘的概念。并且它是Linux环境下对磁盘分区进行管理的一种机制,使用lvm用户可以在无需停机的情况下方便地调整各个分区大小。
看图识LVM
二、LVM基本概念
1、Physical Volume(PV),物理卷:创建分区时,标记分区类型为8e后,就可以将该分区做成物理卷供LVM使用了。可以用任意块设备做成物理卷,单个分区、整块硬盘、RAID设备都可以。
2、Volume Group(VG),卷组:顾名思义,就是将一个或多个PV组合为一个卷组,以PE为存储单位。一个VG至少包含一个PV,VG可以创建多个。
3、Physical Extent(PE),物理区域:PE是整个LVM中可分配的最小存储单元,大小在创建卷组时指定且确定后不能更改;同一卷组的PE大小需一致。新的PV加入卷组后,PE的大小会自动更改为原卷组PE的大小。
4、Logical Volume(LV),逻辑卷:建立在卷组的基础上,卷组中空闲的PE可分配给LV使用,创建LV后可通过增加或减少PE来动态扩展和缩小空间大小。LV可以直接创建文件系统并挂载使用。两种写入模式:线性模式(linear)、交错模式(triped)。
三、LVM常用管理工具
准备工作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | # fdisk /dev/sda #创建分区并调整分区格式(lvm格式为8e) 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 (1632-26108, default 1632): Using default value 1632 Last cylinder, +cylinders or +size{K,M,G} (1632-26108, default 26108): Using default value 26108 Command (m for help): n First cylinder (1632-26108, default 1632): Using default value 1632 Last cylinder, +cylinders or +size{K,M,G} (1632-26108, default 26108): +5G Command (m for help): n First cylinder (2286-26108, default 2286): Using default value 2286 Last cylinder, +cylinders or +size{K,M,G} (2286-26108, default 26108): +5G Command (m for help): n First cylinder (2940-26108, default 2940): Using default value 2940 Last cylinder, +cylinders or +size{K,M,G} (2940-26108, default 26108): +5G Command (m for help): n First cylinder (3594-26108, default 3594): +5G Value out of range. First cylinder (3594-26108, default 3594): Using default value 3594 Last cylinder, +cylinders or +size{K,M,G} (3594-26108, default 26108): +5G Command (m for help): t Partition number (1-8): 5 Hex code ( type L to list codes): 8e Changed system type of partition 5 to 8e (Linux LVM) Command (m for help): t Partition number (1-8): 6 Hex code ( type L to list codes): 8e Changed system type of partition 6 to 8e (Linux LVM) Command (m for help): t Partition number (1-8): 7 Hex code ( type L to list codes): 8e Changed system type of partition 7 to 8e (Linux LVM) Command (m for help): t Partition number (1-8): 8 Hex code ( type L to list codes): 8e Changed system type of partition 8 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: Device or resource busy. 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. # kpartx -af /dev/sda # partx -a /dev/sda (centos6.7需要这两步才能读取硬盘) # cat /proc/partitions (查看磁盘情况) major minor #blocks name 8 0 209715200 sda 8 1 819200 sda1 8 2 10240000 sda2 8 3 2048000 sda3 8 4 31 sda4 8 5 5246007 sda5 8 6 5253223 sda6 8 7 5253223 sda7 8 8 5253223 sda8 # fdisk -l Disk /dev/sda : 214.7 GB, 214748364800 bytes 255 heads, 63 sectors /track , 26108 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: 0x000711d9 Device Boot Start End Blocks Id System /dev/sda1 * 1 103 819200 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 103 1377 10240000 83 Linux /dev/sda3 1377 1632 2048000 82 Linux swap / Solaris /dev/sda4 1632 26108 196604286 5 Extended /dev/sda5 1632 2285 5246007 8e Linux LVM /dev/sda6 2286 2939 5253223+ 8e Linux LVM /dev/sda7 2940 3593 5253223+ 8e Linux LVM /dev/sda8 3594 4247 5253223+ 8e Linux LVM |
1、物理卷(PV)的常用管理命令
1 2 3 4 5 6 7 8 | pvcreate #创建一个物理卷 pvchange #修改物理卷的属性 pvresize #调整一个PV的大小 pvremove #删除一个PV pvdisplay #显示PV的属性信息 pvmove #移动PE到指定物理卷 pvscan #搜索所有磁盘上的物理卷 pvs #显示PV的简要信息 |
示例1:创建4个PV
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | # pvcreate /dev/sda{5..8} Physical volume "/dev/sda5" successfully created Physical volume "/dev/sda6" successfully created Physical volume "/dev/sda7" successfully created Physical volume "/dev/sda8" successfully created # pvs #查看磁盘PV情况 PV VG Fmt Attr PSize PFree /dev/sda5 lvm2 --- 5.00g 5.00g /dev/sda6 lvm2 --- 5.01g 5.01g /dev/sda7 lvm2 --- 5.01g 5.01g /dev/sda8 lvm2 --- 5.01g 5.01g # pvdisplay #查看PV详细信息 "/dev/sda5" is a new physical volume of "5.00 GiB" --- NEW Physical volume --- #新PV PV Name /dev/sda5 #物理卷名称 VG Name PV Size 5.00 GiB #物理卷大小 Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID F4waZ4-gQtM-lrZm-RN7O-1HFX-axqe-M5IdE0 "/dev/sda6" is a new physical volume of "5.01 GiB" --- NEW Physical volume --- PV Name /dev/sda6 VG Name PV Size 5.01 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID fykqC3-4PPg-lgfR-iWQO-WzPK-goRT-W4S7uy "/dev/sda7" is a new physical volume of "5.01 GiB" --- NEW Physical volume --- PV Name /dev/sda7 VG Name PV Size 5.01 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID xakBcc-U6lL-vLC7-WIc1-X2Td-tetj-GKBlOu "/dev/sda8" is a new physical volume of "5.01 GiB" --- NEW Physical volume --- PV Name /dev/sda8 VG Name PV Size 5.01 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID OOr5Dh-i7ZW-UjsC-rTxW-MrnP-USMI-XQcmOO |
示例2:删除PV
1 2 3 4 5 6 7 8 | # pvmove /dev/sda5 /dev/sda6 #注意:两个PV必须在同一卷组内 # pvremove /dev/sda5 #删除PV前要确保该PV没有在任何卷组中 Labels on physical volume "/dev/sda5" successfully wiped # pvs PV VG Fmt Attr PSize PFree /dev/sda6 lvm2 --- 5.01g 5.01g /dev/sda7 lvm2 --- 5.01g 5.01g /dev/sda8 lvm2 --- 5.01g 5.01g |
2、卷组(VG)的常用管理命令
1 2 3 4 5 6 7 8 9 10 11 12 | vgcreate #创建卷组 -l:设置可容纳的LV最大数,默认为255 -p:设置包含PV的最大数,默认为255 -s:设置PE大小,默认为4M vgextend #扩展VG大小 vgdisplay #显示VG属性信息 vgchange #修改VG属性信息 vgreduce #缩减VG大小 vgremove #删除VG vgscan #搜索所有磁盘上的卷组 vgs #显示VG简要信息 vgck #检查VG元数据 |
示例1:创建一个卷组,要求PE大小为8M,名称为bjwf,最大支持LV不能超过10个,包含的PV不能超过2个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | # vgcreate -s 8M -l 10 -p 2 bjwf /dev/sda{7,8} Volume group "bjwf" successfully created # vgdisplay bjwf #查看VG属性信息验证结果 --- Volume group --- VG Name bjwf #VG名称 System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 1 VG Access read /write VG Status resizable MAX LV 10 #可以看到,支持最大LV数为10个 Cur LV 0 Open LV 0 Max PV 2 #支持最大PV数为2个 Cur PV 2 Act PV 2 VG Size 10.02 GiB #卷组大小 PE Size 8.00 MiB #PE的大小 Total PE 1282 Alloc PE / Size 0 / 0 Free PE / Size 1282 / 10.02 GiB #剩余容量 VG UUID 7q5Lka-cnTN-qqlh-eIg1-Fpr2-2IPY-yrHA1S |
示例2:扩展bjwf的大小
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | # vgextend bjwf /dev/sda6 #这块由于此前定义了VG的最大PV数,所以无法扩展 No space for ‘/dev/sda6‘ - volume group ‘bjwf‘ holds max 2 physical volume(s). Unable to add physical volume ‘/dev/sda6‘ to volume group ‘bjwf‘ . # vgchange -p 3 bjwf #修改VG属性 Volume group "bjwf" successfully changed # vgextend bjwf /dev/sda6 #扩展VG Volume group "bjwf" successfully extended #显示成功 # vgdisplay bjwf #查看新扩展的VG --- Volume group --- VG Name bjwf System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 3 VG Access read /write VG Status resizable MAX LV 10 Cur LV 0 Open LV 0 Max PV 3 #最大PV数已经改为3个 Cur PV 3 Act PV 3 VG Size 15.02 GiB #VG大小也已经改变 PE Size 8.00 MiB Total PE 1923 Alloc PE / Size 0 / 0 Free PE / Size 1923 / 15.02 GiB VG UUID 7q5Lka-cnTN-qqlh-eIg1-Fpr2-2IPY-yrHA1S |
示例3:缩减bjwf大小,然后删除bjwf
1 2 3 4 5 6 7 8 | # vgreduce bjwf /dev/sda8 #注意:把PV移除卷组时要保证该PV数据已经迁移或未使用 Removed "/dev/sda8" from volume group "bjwf" # vgchange -an bjwf #标记bjwf为不可用状态 0 logical volume(s) in volume group "bjwf" now active # vgremove bjwf #不论VG包含多少PV都可以删除,前提是该VG上没有任何LV,如果有就需要先删除LV Volume group "bjwf" successfully removed # vgs #查看VG情况 |
3、逻辑卷(LV)的常用管理命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | lvcreate #创建LV -l:设置LV大小,以PE为单位 -L:设置LV大小,以字节为单位 -n:设置LV名称 -p:设置LV权限读写权限,默认为可读可写 -s:设置一个快照卷,对一个已存在的LV做快照 lvextend #扩展LV大小 lvreduce #缩减LV大小 lvchange #修改LV属性信息 -ay:标记LV为可用状态 -an:标记LV为不可用状态 lvremove #删除LV lvdisplay #显示LV属性信息 lvscan #搜索所有磁盘上的逻辑卷 lvs #显示LV简要信息 |
示例1:创建一个LV,要求大小为10G,名称为mylv,指定卷组为bjwf,并挂载到/data上
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | # lvcreate -L 10G -n mylv bjwf Logical volume "mylv" created. # lvdisplay #LV详细情况 --- Logical volume --- LV Path /dev/bjwf/mylv #逻辑卷mylv的陆佳 LV Name mylv #逻辑卷名称为mylv VG Name bjwf #基于bjwf卷组 LV UUID gvQMEB-1Zzo-IVth-KV7v-O6QH-8Nxw-IK1RIf LV Write Access read /write LV Creation host, time mail.bjwf.com, 2016-02-01 17:16:57 +0800 LV Status available # open 0 LV Size 10.00 GiB Current LE 2560 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 # mke2fs -t ext4 /dev/bjwf/mylv #格式化mylv # mkdir /data #创建目录 # mount /dev/bjwf/mylv /data #挂载到指定目录 # df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 9.5G 6.6G 2.5G 73% / tmpfs 3.9G 0 3.9G 0% /dev/shm /dev/sda1 772M 39M 693M 6% /boot /dev/mapper/bjwf-mylv 9.8G 23M 9.2G 1% /data #大小为10G的/data |
示例2:扩展mylv的大小,增加5G空间
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | # lvextend -L 15G /dev/bjwf/mylv #方法一,扩展物理边界增加到15G # lvextend -L +5G /dev/bjwf/mylv #方法二,扩展物理边界增加5G # df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 9.5G 6.6G 2.5G 73% / tmpfs 3.9G 0 3.9G 0% /dev/shm /dev/sda1 772M 39M 693M 6% /boot /dev/mapper/bjwf-mylv 9.8G 23M 9.2G 1% /data #仍然为10G,逻辑卷没有增加 # resize2fs /dev/bjwf/mylv #扩展逻辑边界。 注:只对ext文件系统使用。这里也可以指定扩展逻辑边界大小,默认物理边界大小。 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/bjwf/mylv is mounted on /data ; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of /dev/bjwf/mylv to 3932160 (4k) blocks. The filesystem on /dev/bjwf/mylv is now 3932160 blocks long. # lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv bjwf -wi-ao---- 15.00g # df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 9.5G 6.6G 2.5G 73% / tmpfs 3.9G 0 3.9G 0% /dev/shm /dev/sda1 772M 39M 693M 6% /boot /dev/mapper/bjwf-mylv 15G 25M 14G 1% /data #大小已经增加到15G |
示例三:缩减mylv的大小,减少9G空间(缩减的顺序与创建时的顺序相反,也就是先卸载文件系统,缩减逻辑卷,缩减物理卷。缩减很危险,不到最后一步千万不要缩减,缩减需要离线。)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | # umount /data/ #卸载,住:卸载前先将数据拿走,血的教训 # e2fsck -f /dev/bjwf/mylv #强制检查 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/bjwf/mylv : 12 /983040 files (0.0% non-contiguous), 100865 /3932160 blocks # resize2fs /dev/bjwf/mylv 7G #缩减逻辑边界至7G resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/bjwf/mylv to 1835008 (4k) blocks. The filesystem on /dev/bjwf/mylv is now 1835008 blocks long. # lvreduce -L 7G /dev/bjwf/mylv #缩减物理边界至7G WARNING: Reducing active logical volume to 7.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce mylv? [y /n ]: y #确认,后果自负 Size of logical volume bjwf /mylv changed from 15.00 GiB (3840 extents) to 7.00 GiB (1792 extents). Logical volume mylv successfully resized # mount /dev/bjwf/mylv /data/ #重新挂载 # df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 9.5G 6.6G 2.5G 73% / tmpfs 3.9G 0 3.9G 0% /dev/shm /dev/sda1 772M 39M 693M 6% /boot /dev/mapper/bjwf-mylv 6.8G 23M 6.4G 1% /data #已经缩减至7G |
示例四:对mylv创建一个快照卷,快照恢复后,卸载快照卷并删除
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # lvcreate -L 50M -n mylv_snap -p r -s /dev/bjwf/mylv #对mylv做快照,首先要确定逻辑卷里面暂用的空间大小小于50M,不然快照没意义 Rounding up size to full physical extent 52.00 MiB Logical volume "mylv_snap" created. # lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync C onvert mylv bjwf owi-a-s--- 10.00g mylv_snap bjwf sri-a-s--- 52.00m mylv 0.00 #快照卷 # mkdir /snap #创建挂载点 # mount /dev/bjwf//mylv_snap /snap/ #挂载快照至指定目录 mount : block device /dev/mapper/bjwf-mylv_snap is write-protected, mounting read -only # umount /snap/ #卸载,一定要确定数据是否移除 # lvremove /dev/bjwf/mylv_snap #删除快照卷 Do you really want to remove active logical volume mylv_snap? [y /n ]: y #确认 Logical volume "mylv_snap" successfully removed |
示例五:删除mylv
1 2 3 | # lvchange -an /dev/bjwf/mylv #标记mylv为不可用状态 # lvremove /dev/bjwf/mylv #删除LV Logical volume "mylv" successfully removed |
本文出自 “事在人为,知在天意” 博客,请务必保留此出处http://yangsj.blog.51cto.com/8702844/1794086
标签:lvm(逻辑卷管理)
原文地址:http://yangsj.blog.51cto.com/8702844/1794086