标签:
1:目的
LVM是逻辑盘卷管理(Logical Volume Manager)的简称,他是磁盘管理的另一种工具,就目前基本上所有操作系统均支持,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。通过LVM系统管理员可以轻松管理磁盘分区;
2:试用范围
本文档适用的环境为linux系统;
3:术语
(1)PV(physical volume):物理卷在逻辑卷管理系统最底层,可为整个物理硬盘或实际物理硬盘上的分区;
(2)VG(volume group):卷组建立在物理卷上,一卷组中至少要包括一物理卷,卷组建立后可动态的添加卷到卷组中,一个逻辑卷管理系统工程中可有多个卷组;
(3)LV(logical volume ):逻辑卷建立在卷组的基础上卷组中未分配空间可用于建立新的逻辑卷,逻辑卷建立后可以动态扩展和缩小空间;
(4)PE(physical extent):物理区域是物理卷中可用于分配的最小存储单元,物理区域大小在建立卷组时指定,一旦确定不能更改,同一卷组所有物理卷的物理区域大小需一致,新的PV加入到VG后,PE的大小自动更改为VG中定义的PE大小;
(5)LE(logical extent):逻辑区域是逻辑卷中可用于分配的最小单元,逻辑区域的大小取决于逻辑卷所在卷组中的物理区域的大小;
(6)卷组描述区域:卷组描述区域存在于每个物理卷中,用于描述物理卷本身,物理卷所属卷组,卷组中逻辑卷,逻辑卷中物理区域的分配信息,它是在使用PVcreate建立物理卷时建立的;
4:实例
将若干个磁盘分区连接为一个整块的卷组(volume group),形成一个存储池。管理员可以在卷组上随意创建逻辑卷组(logical volumes),并进一步在逻辑卷组上创建文件系统。管理员通过LVM可以方便的调整存储卷组的大小,并且可以对磁盘存储按照组的方式进行命名、管理和分配,例如按照使用用途进行定义:“DBdata”和“DBSoft”,而不是使用物理磁盘名“sda”和“sdb”或”hda”和”hdb”。而且当系统添加了新的磁盘,通过LVM管理员就不必将磁盘的文件移动到新的磁盘上以充分利用新的存储空间,而是直接扩展文件系统跨越磁盘即可;
5:创建LVM准备工作
6:具体操作
(1)LVM分区
#fdisk /dev/xvdb #p(显示分区情况)——>#n(新建分区)——>#e(创建扩展分区)——>#l(创建逻辑分区) #此处为实验环境方便后面的增减操作,所以我们将/dev/xvdb5全部划分为一个分区 #t(设置磁盘Hex code)——>#8e(LinuxLVM)——>#w(保存操作) #p Disk /dev xvdb: 1073 MB, 1073741824 bytes 16 heads, 63 sectors/track, 2080 cylinders Units = cylinders of 1008 * 512 = 516096 bytes Device Boot Start End Blocks Id System /dev/xvdb1 1 2080 1048288+ 5 Extended /dev/xvdb 5 1 2080 1048257 8e Linux LVM
(2)创建LVM
创建PV盘 [root@localhost ~]# pvcreate /dev/xvdb5 Physical volume "/dev/xvdb5" successfully created 查看PV盘 [root@localhost ~]# pvdisplay –v /dev/xvdb5" is a new physical volume of "1019.69 MiB" --- NEW Physical volume --- PV Name /dev/xvdb5 VG Name PV Size 1019.69 MiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID qXLoqy-tmiJ-yfpH-avuS-WEdG-7S19-9XVC25 [root@localhost ~]# pvscan PV /dev/xvda2 VG VolGroup lvm2 [29.51 GiB / 0 free] PV /dev/xvdb5 lvm2 [1019.69 MiB] Total: 2 [30.50 GiB] / in use: 1 [29.51 GiB] / in no VG: 1 [1019.69 MiB 删除PV盘 [root@localhost ~]# pvremove /dev/xvdb5 Labels on physical volume "/dev/xvdb5" successfully wiped 创建VG存储池 [root@localhost ~]# vgcreate vg1 /dev/xvdb5 (表示:存储池name) Volume group "vg1" successfully created 查看VG存储池 [root@localhost ~]# vgdisplay --- Volume group --- VG Name vg1 System ID Format lvm2 Metadata Areas 1 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 1 Act PV 1 VG Size 1016.00 MiB PE Size 4.00 MiB Total PE 254 Alloc PE / Size 0 / 0 Free PE / Size 254 / 1016.00 MiB VG UUID up2ZaM-Iww4-wjJj-Ayjt-d8nX-dKXO-BuDxwH --- Volume group --- VG Name VolGroup System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size 29.51 GiB PE Size 4.00 MiB Total PE 7554 Alloc PE / Size 7554 / 29.51 GiB Free PE / Size 0 / 0 VG UUID KnuCdp-opQn-Q7JV-dmtQ-Wrds-Wtb2-cal1t9 注:
当多个物理卷组合成一个卷组后时,LVM会在所有的物理卷上做类似格式化的工作,将每个物理卷切成一块一块的空间,这一块一块空间就称为PE(Physical Extent),
默认大小是4MB;由于受内核的限制的原因,一个逻辑卷最多只能包含65536个容量为256GB,若希望使用大于256G的逻辑卷,则创建卷组时需要指定更大的PE。 分配LV逻辑卷 [root@localhost ~]# lvcreate -L 500M -n work vg1 Logical volume "work" created 注: lvcreate –L +某个逻辑卷大小 lvcreate –l +所加的某个逻辑卷大小是通过指定PE数来设定逻辑分区大小; 查看分配LV逻辑卷 [root@localhost ~]# lvdisplay --- Logical volume --- LV Path /dev/vg1/work LV Name work VG Name vg1 LV UUID 2VhGVe-Rmlz-NEtp-o5Ef-WddG-ze2D-TPm910 LV Write Access read/write LV Creation host, time localhost.localdomain, 2014-07-25 14:53:05 +0800 LV Status available # open 0 LV Size 500.00 MiB Current LE 125 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:2 --- Logical volume --- LV Path /dev/VolGroup/lv_root LV Name lv_root VG Name VolGroup LV UUID VXUZw5-FRHl-GH52-DFNi-2NSn-Ftcp-x7FJVc LV Write Access read/write LV Creation host, time localhost.localdomain, 2014-04-17 15:56:30 +0800 LV Status available # open 1 LV Size 25.59 GiB Current LE 6550 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 --- Logical volume --- LV Path /dev/VolGroup/lv_swap LV Name lv_swap VG Name VolGroup LV UUID m5YTtn-znsJ-9whZ-WaF9-hNih-YFeI-EAgkRi LV Write Access read/write LV Creation host, time localhost.localdomain, 2014-04-17 15:56:35 +0800 LV Status available # open 1 LV Size 3.92 GiB Current LE 1004 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:1
(3)创建文件系统
在逻辑卷上创建ext4的文件系统 mkfs -t ext4 /dev/vg1/work 与LV Path所对应;
(4)挂载逻辑卷
建立挂载点 mkdir /opt/oracle 进行挂载 Mount /dev/vg1/work /opt/oracle 为了在系统启动时自动加载文件系统,在/etc/fstab中添加内容: [root@localhost ~]# vi /etc/fstab /*添加下面的命令*/ /dev/vg1/work /opt/oracle ext4 defaults noauto 0 0
(5)增加linux Lv 空间大小
目前linux默认安装都采用了LVM逻辑卷管理,此技术用于方便管理硬盘、分区,及灵活调整分区大小等; 系统默认安装分区显示如下: [root@localhost ~]# df -l Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/VolGroup-lv_root 26407188 3039120 22026628 13% / tmpfs 1987360 0 1987360 0% /dev/shm /dev/xvda1 495844 32400 437844 7% /boot /dev/mapper/vg1-work 495844 10510 459734 3% /mnt 假如分区空间要增加,可以增加新硬盘来加入VG内使其增加; 环境:目前oracle系统空间不够,需增加其/u01/oracle/10g目录大小,增加一硬盘来加大VG及根目录空间 按没使用LVM之前的做法是将oracle目录单独重新挂着在新硬盘分区上,这样调整非常麻烦;而且要停止oracle,数据拷贝等 方便操作: 将新硬盘增加到VG来加大根目录空间 给新加的硬盘分区: fdisk /dev/sdc (分成一个分区,格式为linux LVM.) 下面开始把分区加到LVM内去: [root@localhost ~]# pvcreate /dev/xvdc1 Physical volume "/dev/xvdc1" successfully created 把新物理卷加入到卷组中去 [root@localhost ~]# vgextend vg1 /dev/xvdc1 Volume group "vg1" successfully extended 把新的空间加到逻辑卷中去 root@localhost ~]# lvextend -L +10G vg1 /dev/xvdc1 Extending logical volume work to 10.49 GiB Insufficient free space: 2560 extents needed, but only 383 available 加上去之后,目前用df -h还看不到新的空间,需要激活 [root@localhost ~]# resize2fs -p vg1 /dev/xvdc1 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/vg000/lvtest is mounted on /test; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of /dev/vg000/lvtest to 405504 (4k) blocks. The filesystem on /dev/vg000/lvtest is now 405504 blocks long. 接下来就可以用df –l看见新的空间 root@localhost ~]# pvdisplay --- Physical volume --- PV Name /dev/xvdb5 VG Name vg1 (原来的磁盘) PV Size 1019.69 MiB / not usable 3.69 MiB Allocatable yes PE Size 4.00 MiB Total PE 254 Free PE 129 Allocated PE 125 PV UUID A4wHrI-j3KY-i9yn-Yc97-pJzk-DeUt-SmcUrX --- Physical volume --- PV Name /dev/xvdc1 VG Name vg1 (新加的磁盘) PV Size 1019.72 MiB / not usable 3.72 MiB Allocatable yes PE Size 4.00 MiB Total PE 254 Free PE 254 Allocated PE 0 PV UUID 0iDtgU-y7Rv-AX8x-P2fC-R3Vp-Fbqp-Dx1pDL --- Physical volume --- PV Name /dev/xvda2 VG Name VolGroup PV Size 29.51 GiB / not usable 3.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 7554 Free PE 0 Allocated PE 7554 PV UUID XpTZJc-tGo6-843p-fMgz-ov63-EQl0-6VICRN
(6)缩小LV容量(本操作在生产环境中危险系数极高)
第一步:先找出需要消减分区的容量大小,并计算出文件系统需缩小多少; [root@localhost ~]# pvdisplay --- Physical volume --- PV Name /dev/xvdb5 VG Name vg000 PV Size 1019.69 MiB / not usable 11.69 MiB Allocatable yes (but full) PE Size 16.00 MiB Total PE 63 Free PE 0 Allocated PE 63 PV UUID rZcwxR-DhcY-ebIP-Fult-py0B-ADUS-IlrSvX 第二步:降低文件系统的容量; 取消挂载 [root@localhost ~]# umount /test 先进性磁盘检测 [root@localhost ~]# e2fsck -f /dev/vg000/lvtest 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/vg000/lvtest: 66/95888 files (0.0% non-contiguous), 10714/405504 blocks 降低文件系统的容量 [root@localhost ~]# resize2fs /dev/vg000/lvtest 421M resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/vg000/lvtest to 107776 (4k) blocks. The filesystem on /dev/vg000/lvtest is now 107776 blocks long. 重新挂载磁盘 [root@localhost ~]# mount /dev/vg000/lvtest /test 查看文件系统大小变化: 1)没有减少文件系统的大小: /dev/mapper/vg000-lvtest 1597892 18732 1501352 2% /test 2)减少以后文件系统的大小 /dev/mapper/vg000-lvtes 423672 18312 384680 5% /test 将LV容量降低(知道/dev/xvdb5有多少个PE) [root@localhost ~]# lvresize -l -63 /dev/vg000/lvtest WARNING: Reducing active and open logical volume to 576.00 MiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce lvtest? [y/n]: y Reducing logical volume lvtest to 576.00 MiB Logical volume lvtest successfully resized 将/dev/xvdb5移出vg000这个组外; 1)如果/dev/xvdb5没有free PE,将/dev/xvdb5的PE数全部转移在/dev/xvda5,
具体操如下:Pvremove /dev/xvdb5 /dev/xvda5 2)将/dev/xvdb5移出vg000这个组外 [root@localhost ~]# vgreduce vg000 /dev/xvdb5 Removed "/dev/xvdb5" from volume group "vg000
标签:
原文地址:http://www.cnblogs.com/qlwang/p/4276466.html