parted创建LVM
把一块1T硬盘全部设为LVM
#parted /dev/sdb
>mklabel gpt
由于MBR分区表只支持2T硬盘,所以如果大于2T必须用GPT分区表
>mkpart primary xfs 0G 500G
xfs就是ext4文件系统
>mkpart primary xfs 500G -0G
>set 1 lvm on 设定编号1为LVM分区,on开启 off是关闭
>set 2 lvm on
>quit
#partprobe 立即生效
退出后进行建立pv,vg,lv等
创建LVM
#yum install lvm2*
#pvcreate /dev/sdc1 创建pv
#pvcreate /dev/sdc2
#pvdisplay 显示PV分区情况
#vgcreate vg0 /dev/sdc1 /dev/sdc2 把分区加入到vg0组里
#vgdisplay
#lvcreate -L 100G -n lv0 vg0 创建lv大小100G名称lv0
#mkfs.ext4 /dev/vg0/lv0 格式化lv0
#mount /dev/vg0/lv0 /mnt 挂载
开机自动启动就写入到fstab文件中
管理LVM
1扩展logical volume容量
#lvextend -L +20G /dev/vg0/lv0 需要卸载下来分区
#lvdisplay 显示为120G容量,可是挂载后发现还是100G
#resize2fs /dev/vg0/lv0 不需要卸载分区
2扩展volume group容量
在添加一块硬盘创建好PV后
#vgextend vg0 /dev/sdd1 添加sdd1分区加入到vg0组
#vgdisplay
3删除
#lvremove /dev/vg0/lv0
注意:在整个过程中对磁盘进行调整后,遇到重启不能进入系统,可通过以下方法解决
1检查/etc/fstab文件
2用光盘引导进入修复模式,重新安装grub
#parted /dev/sdc
按两下TAB能显示出所有能使用的命令
>print 显示分区列表
>mkpart 创建分区 (会立即生效)
>会提示分区类型 p 主分区 e扩展分区 l逻辑分区
>start 100G 从上个分区的末尾100G开始分区
>end 300G 到300G结束,总共磁盘空间是200G
>resize 1 重设编号1的大小空间
>start 100G
>end 200G
>rm 1 删除编号1分区
>quit 退出
#partprobe
#mkfs.ext4 /dev/sdc2
一、LVM介绍
LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制。LVM,逻辑卷管理器 (Logical Volume Manager),顾名思义,它就是用于管理逻辑卷的,LVM 在 Linux 内核得到支持。在传统的存储模型中,文件系统是直接构建于物理分区之上的,物理分区的大小就决定了其上文件系统的存储容量,因此对文件系统的存储容量的调整就变得比较繁琐。而 LVM 设计的主要目标就是实现文件系统存储容量的可扩展性,使对容量的调整更为简易。
LVM 的架构图如下:
LVM(Logical Volume Manager)逻辑卷管理,是在硬盘分区和文件系统之间添加的一个逻辑层,为文件系统屏蔽下层硬盘分区布局,并提供一个抽象的盘卷,在盘卷上建立文件系统。管理员利用LVM可以在硬盘不用重新分区的情况下动态调整文件系统的大小,并且利用LVM管理的文件系统可以跨越物理硬盘。当服务器添加了新的硬盘后,管理员不必将原有的文件移动到新的硬盘上,而是通过LVM直接扩展文件系统来跨越物理硬盘。LVM就是通过将底层的物理硬盘封装,然后以逻辑卷的方式呈现给上层应用。当我们对底层的物理硬盘进行操作时,不再是针对分区进行操作,而是通过逻辑卷对底层硬盘进行管理操作。
LVM 在每个物理卷头部都维护了一个 metadata,每个 metadata 中都包含了整个 VG 的信息,包括每个 VG 的布局配置、PV 的编号、LV 的编号,以及每个 PE 到 LE 的映射关系。同一个 VG 中的每个 PV 头部的信息是相同的,这样有利于故障时进行数据恢复。
LVM 对上层文件系统提供 LV 层,隐藏了操作细节。对文件系统而言,对 LV 的操作与原先对 Partition 的操作没有差别。当对 LV 进行写入操作时,LVM 定位相应的 LE,通过 PV 头部的映射表,将数据写入到相应的 PE 上。
LVM 实现的关键在于在 PE 和 LE 间建立映射关系,不同的映射规则决定了不同的 LVM 存储模型。LVM 支持多个 PV 的 Stripe 和 Mirror,这点和软阵列的实现十分相似。
优点:
LVM通常用于装备大量磁盘的系统,但它同样适于仅有一、两块硬盘的小系统。小系统使用LVM的益处:传统的文件系统是基于分区的,一个文件系统对应一个分区。这种方式比较直观,但不易改变。
1.不同的分区相对独立,无相互联系,各分区空间很易利用不平衡,空间不能充分利用。
2.当一个文件系统/分区已满时,无法对其扩充,只能采用重新分区/建立文件系统,非常麻烦;或把分区中的数据移到另一个更大的分区中;或采用符号连接的方式使用其它分区的空间。
3.如果要把硬盘上的多个分区合并在一起使用,只能采用再分区的方式,这个过程需要数据的备份与恢复。
当采用LVM时,情况有所不同:
1.硬盘的多个分区由LVM统一为卷组管理,可以方便的加入或移走分区以扩大或减小卷组的可用容量,充分利用硬盘空间。
2.文件系统建立在逻辑卷上,而逻辑卷可根据需要改变大小(在卷组容量范围内)以满足要求。
3.文件系统建立在LVM上,可以跨分区,方便使用。
大系统使用LVM的益处:
1、在使用很多硬盘的大系统中,使用LVM主要是方便管理、增加了系统的扩展性。
2、在一个有很多不同容量硬盘的大型系统中,对不同的用户的空间分配是一个技巧性的工作,要在用户需求与实际可用空间中寻求平衡。
3、用户/用户组的空间建立在LVM上,可以随时按要求增大,或根据使用情况对各逻辑卷进行调整。当系统空间不足而加入新的硬盘时,不必把用户的数据从原硬盘迁移到新硬盘,而只须把新的分区加入卷组并扩充逻辑卷即可。另外使用LVM可以在不停服务的情况下把用户数据从旧硬盘转移到新硬盘空间中去。
具体来讲,其功能特点大致有:
使用 LVM 的优势:
文件系统可以跨多个磁盘,因此大小不会受物理磁盘的限制。
可以在系统运行状态下动态地扩展文件系统大小。
可以增加新磁盘到 LVM 的存储池中。
可以以镜像的方式冗余重要数据到多个物理磁盘上。
可以很方便地导出整个卷组,并导入到另外一台机器上。
使用 LVM 的限制:
在从卷组中移除一个磁盘时必须使用 reducevg,否则会出问题。
当卷组中的一个磁盘损坏时,整个卷组都会受影响。
不能减小文件系统大小(受文件系统类型限制)。
因为加入了额外的操作,存储性能会受一些影响(使用 Stripe 的情况另当别论)。
使用 LVM 将获得更好的可扩展性和可操作性,但却损失了可靠性和存储性能,总的说来就是在这两者之间做选择。
卷组VG可以使多个硬盘空间看起来像是一个大硬盘。
逻辑卷LV可以创建跨多个硬盘空间的分区。
在使用逻辑卷LV时,可以在空间不足时动态调整大小,不需要考虑逻辑卷LV在硬盘上的位置,不用担心没有可用的连续的空间。
可以在线对卷组VG、逻辑卷LV进行创建、删除、调整大小等操作。但LVM上的文件系统也需要重新调整大小。
LVM允许创建快照,用来保存文件系统的备份。
注意:LVM是软件的卷管理方式,RAID是磁盘管理的方法。对于重要的数据使,用RAID保护物理硬盘不会因为故障而中断业务,再用LVM来实现对卷的良性管理,更好的利用硬盘资源。
LVM有两种写入机制:线性(写完一个PV再写下一个PV,默认)、条带(平均)、新版本还有镜像模式可选,在linux下lvm2为LVM体系的软件包名。
二、 基本概念
物理存储介质(The physical media):LVM存储介质,可以是硬盘分区、整个硬盘、raid阵列或SAN硬盘。设备必须初始化为LVM物理卷,才能与LVM结合使用。
1、物理卷--PV(Physical Volume)
物理卷在逻辑卷管理中处于最底层,物理卷就是LVM的基本存储逻辑块,但和基本的物理存储介质比较却包含与LVM相关的管理参数,创建物理卷可以用硬盘分区,也可以用硬盘本身。
2、卷组--VG(Volumne Group)
卷组建立在物理卷之上,一个卷组中至少要包括一个物理卷,在卷组建立之后可动态添加物理卷到卷组中。一个逻辑卷管理系统工程中可以只有一个卷组,也可以拥有多个卷组。
3、逻辑卷--LV(Logical Volume)
逻辑卷建立在卷组之上,卷组中的未分配空间可以用于建立新的逻辑卷,逻辑卷建立后可以动态地扩展和缩小空间。系统中的多个逻辑卷要以属于同一个卷组,也可以属于不同的多个卷组。在逻辑卷LV之上建立文件系统。
4、物理区域--PE(Physical Extent)
物理区域是物理卷中可用于分配的最小存储单元,物理区域的大小可根据实际情况在建立物理卷时指定。物理区域大小一旦确定将不能更改,同一卷组中的所有物理卷的物理区域大小需要一致,PE的大小可以指定,默认为4MB。
5、逻辑区域--LE(Logical Extent)
逻辑区域是逻辑卷中可用于分配的最小存储单元,逻辑区域的大小取决于逻辑卷所在卷组中的物理区域的大小。
6、卷组描述区域--(Volume Group Descriptor Area)
卷组描述区域存在于每个物理卷中,用于描述物理卷本身、物理卷所属卷组、卷组中的逻辑卷及逻辑卷中物理区域的分配等所有信息,卷组描述区域是在使用pvcreate建立物理卷时建立的,在同一卷组VG中LE的大小和PE是相同的,并且一一相对。
相关名词简写:
PP,物理分区(Physical Partition),如硬盘的分区,或 RAID 分区。
PV,物理卷(Physical Volume),是 PP 的 LVM 抽象,它维护了 PP 的结构信息,是组成 VG 的基本逻辑单元,一般一个 PV 对应一个 PP。
PE,物理扩展单元(Physical Extends),每个 PV 都会以 PE 为基本单元划分。
VG,卷组(Volume Group),即 LVM 卷组,它可由一个或数个 PV 组成,相当于 LVM 的存储池。
LE,逻辑扩展单元(Logical Extends),组成 LV 的基本单元,一个 LE 对应一个 PE。
LV,逻辑卷(Logical Volume),它建立在 VG 之上,文件系统之下,由若干个 LE 组成。
总结:多个磁盘/分区/raid-->多个物理卷PV-->合成卷组VG-->从VG划分出逻辑卷LV-->格式化LV,挂载使用。老版本的Linux在创建PV时,需要将分区类型改为Linux LVM(8e);但新的系统已经非常智能,即使默认的Linux分区(83),也可以创建PV。
三、 常用命令
功能 | PV管理命令 | VG管理命令 | LV管理命令 |
scan 扫描 | pvscan | vgscan | lvscan |
create 创建 | pvcreate | vgcreate | lvcreate |
display 显示 | pvdisplay | vgdisplay | lvdisplay |
remove 移除 | pvremove | vgremove | lvremove |
extend 扩展 | vgextend | lvextend | |
reduce 减少 | vgreduce | lvreduce |
1、物理卷命令
pvscan #在系统的所有磁盘中搜索已存在的物理卷
pvdisplay 物理卷全路径名称 #用于显示指定物理卷的属性。
pvdata 物理卷全路径名称 #用于显示物理卷的卷组描述区域信息,用于调试目的。
pvchange Cx|--allocation {y|n} 物理卷全路径名 #用于改变物理卷的分配许可设置物理卷的创建与删除命令
pvcreate 设备全路径名 #用于在磁盘或磁盘分区上创建物理卷初始化信息,以便对该物理卷进行逻辑卷管理。
pvmove 源物理卷全路径我[目的物理卷全路径名] #用于把某物理卷中的数据转移到同卷组中其他的特刊卷中。
2、卷组命令
vgscan #检测系统中所有磁盘
vgck [卷组名] #用于检查卷组中卷组描述区域信息的一致性。
vgdisplay [卷组名] #显示卷组的属性信息
vgrename 原卷组名 新卷组名
vgchange -a y|n [卷组名] #改变卷组的相应属性。是否可分配
vgchange -l 最大逻辑卷数 #卷组可容纳最大逻辑卷数
vgchange -x y|n [卷组名] #卷是否有效
vgmknodes [卷组名|卷组路径] #用于建立(重新建立)已有卷组目录和其中的设备文件卷组配置的备份与恢复命令
vgcfgbackup [卷组名] #把卷组中的VGDA信息备份到“/etc/lvmconf”目录中的文件
vgcfgrestore -n 卷组名 物理卷全路命名 #从备份文件中必得指定物理卷的信息卷组的建立与删除命令
vgcreate 卷组名 物理卷全路径名[物理卷全路径名]
vgmove 卷组名
卷组的扩充与缩小命令
vgextend 卷组名 物理卷全路径名[物理卷全路径名]
vgreduce 卷组名 物理卷全路径名[物理卷全路径名]
卷组的合并与拆分
vgmerge 目的卷组名 源卷组名 #合并两个已经存在的卷组,要求两个卷组的物理区域大小相等且源卷组是非活动的。
vgsplit 现有卷组 新卷组 物理卷全路径名[物理卷全路径名]
卷组的输入与输出命令
vgexport 卷组名
vgimport 卷组名 卷组中的物理卷[卷组中的物理卷]
3、逻辑卷命令
lvscan
lvdisplay 逻辑卷全路径名[逻辑卷全路径名]
lvrename 旧逻辑卷全路径名 新逻辑卷全路径名
lvrename 卷组名 旧逻辑卷名 新逻辑卷名
lvchange
e2fsadm -L +|- 逻辑卷增减量 逻辑卷全路径名
逻辑卷的创建与删除命令
lvcreate
lvremove
逻辑卷的扩充与缩小命令
lvextend -L|--size +逻辑卷大小增量 逻辑卷全路径名
lvreduce q -L|--size +逻辑卷减小量 逻辑卷全路径名
4、逻辑卷管理命令
lvmdiskscan #检测所有的SCSI、IDE等存储设备
lvmchange -R|--reset #复位逻辑卷管理器
lvmsadc [日志文件全路径名] #收信逻辑卷管理器读写统计信息,保存到日志文件中。
lvmsar 日志文件全路径名 #从lvmsadc命令生成的日志文件中读取并报告逻辑卷管理器的读写统计信息。
5、命令小结
查看命令有scan、display和s(pvs、vgs、lvs)。s是简单查看对应卷信息,display是详细查看对应卷信息。而scan是扫描所有的相关的对应卷。
pvdisplay:查看系统里的所有物理卷。
pvs:简略直观地查看系统里的物理卷。
vgcreate:创建卷组,后面先跟卷组名,再跟对应的多个物理卷名。
使用vgcreate创建卷组VG,并且此处可以-s选项指定PE(LE)的大小,(默认PE大小4M)
vgcreate -s 16M vg2 /dev/sdb2
vgdisplay:查看物理卷组的信息。
...
PE Size 4.00 MiB //可以看到vg1的PE大小为4M
Total PE 511
Alloc PE / Size 0 / 0
Free PE / Size 511 / <2.00 GiB
注意:PE大,读取速度快,但浪费空间;反之亦然。
vgs:简略直观地查看物理卷组的信息。
vgremove:删除卷组的命令。
有了卷组以后就可以创建逻辑卷(LV)了,使用命令:
lvcreate -n lvname -L lvsize(M,G)|-l LEnumber vgname
lvdisplay:查看逻辑卷大小。
lvs:简略直观地查看逻辑卷大小。
卷的大小调整
在格式化之前需要知道xfs文件系统只支持增大,不支持减小;包括ext系列在内其它新版本的文件系统无此限制。
卷组VG扩容
卷组的扩容相当简单,一般是LVM空间不够时,添加一块新硬盘时使用。
卷组VG缩小
卷组VG的缩小,要查看物理卷PV是否被使用,因为无法缩减一个正在使用的物理卷PV
vgreduce vg1 /dev/sdb2
Physical volume "/dev/sdb2" still in use
此时想要将/dev/sdb2移出vg1,可以使用pvmove命令移动数据,但前提是要该vg中必须有足够的空间来承接sdb2中现有的数据
pvmove /dev/sdb2 /dev/sdc2
/dev/sdb2: Moved: 10.00%
/dev/sdb2: Moved: 100.00%
完成迁移后再执行上述的vgreduce指令便可成功:
vgreduce vg1 /dev/sdb2
Removed "/dev/sdb2" from volume group "vg1"
该操作多用于更换旧硬盘的场景。
逻辑卷LV扩容(动态扩容)
首先需要确认是否有可用的扩容空间,逻辑卷LV是从卷组VG中创建的,所有在逻辑卷LV扩容前需要查看卷组VG的空间使用情况(使用vgs查看)。
在确认有多余空间的情况下,使用lvextend命令的-L选项扩展逻辑卷LV的大小:
lvextend -L +100M /dev/vg1/lv1 // +100M是增加100M
Size of logical volume vg1/lv1 changed from 64.00 MiB (16 extents) to 164.00 MiB (41 extents).
Logical volume vg1/lv1 successfully resized.
lvextend -L 100M /dev/vg1/lv2 //直接使用100M是增加到100M
Size of logical volume vg1/lv2 changed from 64.00 MiB (16 extents) to 100.00 MiB (25 extents).
Logical volume vg1/lv2 successfully resized.
但是再使用df命令查看时,会发现文件系统并没有扩展。这是因为文件系统也需要进行对应的扩容。ext4文件系统扩容使用"resize2fs [逻辑卷名称]",xfs文件系统扩容使用"xfs_growfs 挂载点"来实现容量刷新(扩容)。
在CentOS7中还可以使用lvextend命令的-r选项来使文件系统在调整的过程中自动扩容。
lvextend -L 200M -r /dev/vg1/lv1 //LV扩容到200M并且文件系统自动扩容
Size of logical volume vg1/lv1 changed from 164.00 MiB (41 extents) to 200.00 MiB (50 extents).
Logical volume vg1/lv1 successfully resized. //可以看到文件系统扩容成功
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mapper/vg1-lv1 is mounted on /lv1; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 2
The filesystem on /dev/mapper/vg1-lv1 is now 204800 blocks long.
对于使用xfs文件系统来说可使用lvextend和xfs_growfs来实现动态扩容,使用lvreduce的-r选项尝试缩减空间(经过实测发现xfs是不能被缩减的)。
注意:xfs_growfs必须是挂载情况下才能使用。
删除LVM
删除LVM步骤:umount卸载-->lvremove移除逻辑卷LV-->vgremove移除卷组VG-->pvremove移除物理卷PV。这是一个自下而上的操作过程。
Creating Logical Volumes(创建逻辑卷,这里主要解决其类型)
Now that we have a Volume Group we can start creating Logical Volumes (LVs). There are three types of Logical Volume Supported:
Linear(线性模式)
Mirror(镜像模式)
Stripe(条带模式)
Linear LVs are sometimes described as Concatenated LVs. PVs are filled up sequentially rather than in parallel. So if a VG was comprised of 4 PVs and a single linear LV was created using the whole VG resource the first PV would be populated and then the second, then the third and then the fourth.
Mirrored LVs provide fault tolerance through one or more replicas. A minimum configuration would be a two mirror LV. Each mirror would be made up from Physical Extents (totalling the specified size of the LV) on different PVs. If one mirror were to fail the good mirror would still be available enable the file system or swap device contained within it to remain in operation.
Striped LVs provide improved performance by distributing data across multiple PVs. A stripe unit defines how much data is written to each PV in one go and writes are alternated between the PVs.
Creating a Striped Logical Volume
In the following example we will create a two stripe (-i2) Striped LV called FreeoaLV1. The strip unit size is 64KB and the volume size is 5G (2.5G from each of PVs selected,total 2 pv).
# lvcreate -L 5G -i2 -I64 -nFreeoaLV1 FreeVG
The FreeVG Volume Group only has two Physical Volumes allocated to it so the striped Logical Volume will be striping across both disks.
Creating a Mirrored Logical Volume
In the following example we create a two mirror LV called FreeoaLV2. The mirror volume has one mirror meaning that there will be two replicas of the data and the volume size is 5G which means that there will be two replicas of 5G size.
# lvcreate -L 5G -m1 -nFreeoaLV2 FreeVG
We have not specified which PVs to use so LVM will select two PVs with available Physical Extents. It is possible to specify which PVs to take Physical Extents from. The FreeVG Volume Group only has two Physical Volumes allocated to it so both will be used for a replica here.
四、使用教程
一些简单的LVM设置,仅供参考:
教程一
1、安装lvm2
apt-get install lvm2
2、在硬盘上创建一个LVM分区。使用fdisk或者其它的分区工具来创建一个LVM分区,Linux LVM的分区类型为8e
fdisk /dev/sdb
fdisk /dev/sdc
3、创建一个物理卷
pvcreate /dev/sdb1 /dev/sdc1
4、创建一个卷组
vgcreate vg_iscsi /dev/sdb1 /dev/sdc1
5、 激活卷组
vgchange -ay vg_iscsi
6、建立逻辑卷
# lvcreate -L 2G -n data1 vg_iscsi
格式化
# mke2fs -j /dev/vg_iscsi/data1
挂载
# mkdir /data1
# mount /dev/vg_iscsi/data1 /data1
7、扩展逻辑卷
# umount /data1
# lvextend -L+3G /dev/vg_iscsi/data1
# mount /dev/vg_iscsi/data1 /data1
debian:~# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 7.5G 795M 6.4G 11% /
tmpfs 46M 0 46M 0% /lib/init/rw
udev 10M 100K 10M 1% /dev
tmpfs 46M 0 46M 0% /dev/shm
/dev/mapper/vg_iscsi-data1 2.0G 68M 1.9G 4% /data1
显示不对现在应该是5G
====
# umount /data1
# e2fsck -f /dev/vg_iscsi/data1
# resize2fs /dev/vg_iscsi/data1
# mount /dev/vg_iscsi/data1 /data1
debian:~# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 7.5G 795M 6.4G 11% /
tmpfs 46M 0 46M 0% /lib/init/rw
udev 10M 100K 10M 1% /dev
tmpfs 46M 0 46M 0% /dev/shm
/dev/mapper/vg_iscsi-data1 5.0G 69M 4.7G 2% /data1
以上显示正确
====
# resize2fs /dev/mapper/vg0-lv0_home
resize2fs 1.39 (29-May-2006)
Please run 'e2fsck -f /dev/mapper/vg0-lv0_home' first.
# e2fsck -f /dev/mapper/vg0-lv0_home
e2fsck 1.39 (29-May-2006)
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/mapper/vg0-lv0_home: 8797/507904 files (5.6% non-contiguous), 92009/507904 blocks
# resize2fs /dev/mapper/vg0-lv0_home
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/mapper/vg0-lv0_home to 6569984 (4k) blocks.
The filesystem on /dev/mapper/vg0-lv0_home is now 6569984 blocks long.
8、减小逻辑卷
注:应注意逻辑卷LE和PE的大小
# umount /data1
# e2fsck -f /dev/vg_iscsi/data1
# resize2fs /dev/vg_iscsi/data1 3612M #把当前逻辑卷减少至 3612M (减少了500M)
注:500M/4M=125 个PE,每个PE大小是4M (1028PE-125PE)X 4M=3612M
# lvresize -L 3612M /dev/vg_iscsi/data1
# mount /dev/vg_iscsi/data1 /data1
# df -h
====
debian:/# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 7.5G 796M 6.4G 11% /
tmpfs 46M 0 46M 0% /lib/init/rw
udev 10M 104K 9.9M 2% /dev
tmpfs 46M 0 46M 0% /dev/shm
/dev/mapper/vg_iscsi-data2 3.5G 162M 3.2G 5% /data1
9、更多参考man lvm
delete volume groupe
1.vgchange -a n /dev/vg01
2.vgremove /dev/vg01
extend
1.lvextend -L+1G /dev/vg01/lv01
2.umount /mountingpoint
3.resize2fs /dev/vg01/lv01
4.mount /dev/vg01/lv01 /mountingpoint
reduce:
1.umount /mountingpoint
2.resize2fs /dev/vg01/lv01
lvreduce -L-1G /dev/vg01/lv01
mount /dev/vg01/lv01 /mountingpoint
listing: lvmdiskscan,pvscan,vgscan,lvscan,vgcreate,lvcreate,vgextend,vgreduce,lvextend,lvreduce,pvdiplay,vgdisplay,lvdisplay,lvremove,lvrename,vgremove,vgchange
教程二
一、准备物理硬盘和分区
1、cfdisk /dev/sdb
2、将该硬盘做成extended分区,并write分区表
3、使用fdisk -l就可以看到该硬盘了。
二、安装并配置lvm
1、aptitude lvm2
2、初始化物理卷
# pvcreate /dev/sdb5
3、创建卷组
# vgcreate volgrp /dev/sdb5
4、激活卷组
# vgscan
5、创建逻辑卷
# lvcreate -n software --size 500M volgrp
6、格式化并mount该逻辑卷
# mkfs.ext3 /dev/volgrp/software
# mkdir /home/software
# mount -t ext3 /dev/volgrp/software /home/software
7、查看逻辑卷
# lvdisplay
8、改变逻辑卷的大小
# umount /home/software # lvextended -L+500M /dev/volgrp/software 改变之后,在lvdisplay可以看到大小已经改变,但是实际的文件系统还是500M,所以还需要做以下的操作
9、改变物理卷大小
# e2fsck -f /dev/volgrp/software # resize2fs /dev/volgrp/software
10、重新mount该逻辑卷
# mount -t ext3 /dev/volgrp/software /home/software # df -h 使用df -h就可以看到该分区的大小了
11、去除逻辑卷
# lvremove /dev/volgrp/software 前提:先umount该逻辑卷
12、扩展物理卷
用于在物理空间不足时,将新增一块硬盘加入到lvm中,首先用'fdisk'为其分好区。
先看下当前卷组信息:
# vgdisplay
--- Volume group ---
VG Name xde
System ID
Format lvm2
...
VG Size 5.10 GiB
PE Size 4.00 MiB
Total PE 1306
Alloc PE / Size 854 / 3.34 GiB
Free PE / Size 452 / 1.77 GiB
将'sdb1'加入到卷中。
# vgextend xde /dev/sdb1
No physical volume label read from /dev/sdb1
Physical volume "/dev/sdb1" successfully created
Volume group "xde" successfully extended
加入后,再看一下(应该多了10G的空间):
# vgdisplay
--- Volume group ---
VG Name xde
System ID
Format lvm2
...
VG Size 15.10 GiB
PE Size 4.00 MiB
Total PE 3865
Alloc PE / Size 854 / 3.34 GiB
Free PE / Size 3011 / 11.76 GiB
教程三
Step 1: Create partitions for PVs
First you have to create a partition on your disk to use as a LVM physical volume (PV). This means using fdisk, cfdisk or some other tool like that. This is done as usual, please see the Gentoo Handbook for details on how you use fdisk. Note that the partition type should be for LVM PV partitions.
Step 2: Create PVs
This is easy, you use the tool pvcreate. Say for example you want to make /dev/sda3 and /dev/sdb2 PVs then you would run:
pvcreate /dev/sda3 /dev/sdb2
Step 3: Create VGs
Next you must create a volume group. This is done using vgcreate. If you want to add /dev/sda3 and /dev/sdb2 to a volume group named my_first_vg you would write:
vgcreate my_first_vg /dev/sda3 /dev/sdb2
Step 4: Create LVs
Next you need to create the actual "virtual" partitions, called logical volumes, that you want to use.
If you want create a LV called "my_lv" that is 5 GB on my_first_vg you would run this:
lvcreate -L5G -nmy_lv my_first_vg
The name is completely up to you, but reasonable names could be things like:
usr - for /usr
var - for /var
home - for /home
root - for / (note not recommended, you would need to use initramfs to be able to boot)
portage - for /usr/portage
You figure out what names works for you.
Alternative configuration
LVM2 natively supports creating volumens with RAID1 (mirror) and RAID4/5/6 for reduncancy.
To setup a RAID1 mirror with two devices you would issue:
lvcreate --target RAID1 -m1 -L5G -nmy_lv my_first_vg
Step 5: Create file systems on your LVs
You create the file systems more or less like usual. The tricky bit could be finding out where the device file is, so here is the naming scheme:
/dev/<name of volume group>/<name of logical volume>
So for the partition we created above we would use this command to create an xfs file system on it:
mkfs.xfs /dev/my_first_vg/my_lv
Step 6: Add to /etc/fstab
This is done as usual. Just remember the format of the device paths mentioned in last section.
Administration tasks
Maybe your LVs weren't large enough? Or you want to add another PV to a VG? Well easy to solve:
Administering PVs
How to create PVs was described above. There is really no other "trivial" administration task, thus this is mostly outside the scope of this document.
Administrating VGs
Adding a new PV to an existing VG
If you want to add the PV /dev/sdc1 to the VG my_first_vg then you would run this:
vgextend my_first_vg /dev/sdc1
Removing a PV from an existing VG
If you have to remove on PV from the VG (e.g. you want to remove the hard drive), you must first add enough additional storage space to the VG. See the previous paragraph for this purpose. To move all data away from the PV /dev/sdc1, run this:
pvmove -v /dev/sdc1
After the PV has been freed from all data, you can reduce the VG my_first_vg by that PV /dev/sdc1:
vgreduce my_first_vg /dev/sdc1
Now you can wipe the header in the PV so it won't be recogniced as PV any more:
pvremove /dev/sdc1
Listing information about VGs
To list all VGs on a system:
vgs
To list information about all VGs:
vgdisplay
To list information about the VG my_first_vg:
vgdisplay my_first_vg
Administrating LVs
Listing LVs
To list all LVs on a system use:
lvs
To show information about all LVs use:
lvdisplay
To show information about the LV my_first_lv in the VG my_first_vg:
lvdisplay /dev/my_first_vg/my_first_lv
Creating a new LV
This is done like you did when you created your first LVs. To create a 10GB LV named my_other_lv in the VG my_first_vg:
lvcreate -L10G -nmy_other_lv my_first_vg
Don't forget to create a file system on it!
Removing a LV
First unmount the LV. Then run lvremove. For example if you want to remove my_other_lv that is found in the VG my_first_vg:
lvremove /dev/my_first_vg/my_other_lv
Growing a LV
This depends on the file system you selected, and if you want to shrink or grow it. From the LVM point of view it is all the same, but not for the file systems. For example not all file systems can be shrunk, nor can all be grown. Some can be grown without unmounting, but not all.
To extend the logical volume my_first_lv in the volume group my_first_vg to be 10 GB you would run:
lvextend -L10G /dev/my_first_vg/my_first_lv
To extend the logical volume my_first_lv in the volume group my_first_vg with another 1 GB (relative the current size) you would run:
lvextend -L+1G /dev/my_first_vg/my_first_lv
The next step (resizing the actual file system) depends on what file system you use:
ext2/ext3
First unmount the file system. Then run:
resize2fs /dev/my_first_vg/my_first_lv
Then remount the file system.
ext4
You don't need to unmount if you don't want. Just run:
resize2fs /dev/my_first_vg/my_first_lv
# Unmount the filesystem and check its' LV
umount /mnt/foo
e2fsck -f /dev/mapper/vg0-foo
# Shrink ext4 and then the LV to the desired size
resize2fs -p /dev/mapper/vg0-foo 40G
lvreduce -L 40G /dev/mapper/vg0-foo
# Before continuing, run e2fsck. If it bails because the partition
# is too small, don't panic! The LV can still be extended with
# lvextend until e2fsck succeeds, e.g.:
# lvextend -L +1G /dev/mapper/vg0-foo
e2fsck -f /dev/mapper/vg0-foo
# Resize the filesystem to match the LVs size, check and mount it
resize2fs -p /dev/mapper/vg0-foo
e2fsck -f /dev/mapper/vg0-foo
mount /mnt/foo
That was the tricky part. The rest is pretty straight forward:
unmount the filesystem,
extend the logical volume and
expand the filesystem afterwards.
umount /mnt/freeoa
# Extend the LV to use all free space
lvextend -l +100%FREE /dev/mapper/vg0-freeoa
e2fsck -f /dev/mapper/vg0-freeoa
# Resize the partition to fill the LV
resize2fs -p /dev/mapper/vg0-freeoa
e2fsck -f /dev/mapper/vg0-freeoa
mount /mnt/freeoa
reiserfs (reiser3)
You don't need to unmount if you don't want. Here is what to run:
resize_reiserfs /dev/my_first_vg/my_first_lv
If the file system is mounted, add the switch -f to the above command.
xfs
For XFS file systems you must have them mounted to be able to resize.
If /dev/my_first_vg/my_first_lv is mounted on /quux you would run:
xfs_growfs /quux
btrfs
For btrfs file systems you must have them mounted to be able to resize.
If /dev/my_first_vg/my_first_lv is mounted on /quux and you want to grow by ?GB you would run:
btrfsctl -r +?g /quux
For other file systems please see the relevant documentation.
Shrinking a LV
Warning: Shrinking file systems is much more error prone than growing them, as you need to first shrink the file system using the relevant tools, then shrink the LVM partition to the right size. It is your own fault if you try this and mess it up.
Note: /dev/my_first_vg/my_first_lv is used as an example again.
ext2/ext3
First calculate the new size in blocks you want, as well as how much that would be in MB or GB. Then unmount. Then run:
resize2fs /dev/my_first_vg/my_first_lv 12345678
lvreduce -L-1G /dev/my_first_vg/my_first_lv
where 12345678 is the size in blocks you calculated and -L-1G is the the same size in "normal" units.
reiserfs (reiser3)
First unmount, then if you want to shrink with 1 GB you would run:
resize_reiserfs -s-1G /dev/my_first_vg/my_first_lv
lvreduce -L-1G /dev/my_first_vg/my_first_lv
btrfs
Keep the filesystem mounted. If e.g. dev/my_first_vg/my_first_lv is mounted on /mnt, then to shrink by 1GB run:
btrfsctl -r -1g /mnt
lvreduce -L-1G dev/my_first_vg/my_first_lv
ext4
下文有所涉及。
lvm分区下的各种文件系统缩减操作要比扩大要困难一些,有些文件就不支持缩减操作,像jfs、xfs等。对于像ext4这种新文件系统的操作,记录如下:
# umount /home/
# e2fsck -f /dev/mapper/vg0-lv_home
e2fsck 1.41.12 (17-May-2010)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/mapper/vg0-lv_home: 15/65808 files (0.0% non-contiguous), 12663/263168 blocks
# resize2fs -p /dev/mapper/vg0-lv_home 512M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/vg0-lv_home to 131072 (4k) blocks.
Begin pass 2 (max = 8192)
正在重定位块 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 3 (max = 9)
正在扫描inode表 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/mapper/vg0-lv_home is now 131072 blocks long.
# lvreduce -L 512M /dev/mapper/vg0-lv_home
WARNING: Reducing active logical volume to 512.00 MiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_home? [y/n]: y
Reducing logical volume lv_home to 512.00 MiB
Logical volume lv_home successfully resized
]# resize2fs -p /dev/mapper/vg0-lv_home
resize2fs 1.41.12 (17-May-2010)
The filesystem is already 131072 blocks long. Nothing to do!
# e2fsck -f /dev/mapper/vg0-lv_home
e2fsck 1.41.12 (17-May-2010)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/mapper/vg0-lv_home: 15/29248 files (6.7% non-contiguous), 10236/131072 blocks
# mount /dev/mapper/vg0-lv_home /home
# df -h
文件系统 容量 已用 可用 已用%% 挂载点
/dev/mapper/vg0-lv_root 4.9G 1.1G 3.6G 23% /
tmpfs 499M 0 499M 0% /dev/shm
/dev/sda1 248M 24M 213M 10% /boot
/dev/mapper/vg0-lv_home 505M 33M 447M 7% /home
可以使用dumpe2fs工具来得到相关分区的具体信息:
# dumpe2fs -h /dev/mapper/vg0-lv_home
...
Note that you can't shrink xfs or jfs.
lvreduce指令(收缩逻辑卷空间)
语法:lvreduce [选项] [参数]
功能介绍:lvreduce指令用于减少LVM逻辑卷占用的空间大小。
选项说明
选项 功能
-L指定逻辑卷的大小,单位为"kKmMgGtT"字节
-l指定逻辑卷的大小(LE数)
参数说明
参数 功能
逻辑卷 指定要操作的逻辑卷对应的设备文件
使用lvreduce指令收缩逻辑卷的空间大小有可能会删除逻辑卷上已有的数据,所以在操作前必须进行确认。减少文件的大小一定需要按照规定动作顺序来做,在缩减LV大小前要缩减filesystem的大小,否则将导致文件系统破坏等恶劣影响,具体步骤如下:
使用lvreduce指令减少指定的逻辑卷的空间大小,在命令行中输入下面的命令:
# lvreduce -L -99M /dev/vg0/lv-freeoa
#将逻辑卷的空间大小减少99M
输出信息如下:
......省略部分输出内容......
Do you really want to reduce lv-freeoa? [y/n]: y #确认操作
Reducing logical volume lv-freeoa to 252.00 MB
Logical volume lv-freeoa successfully resized
操作失败,提示需要先fsck文件系统。到此文件系统缩减大小失败,补救的方法是将lv修改为改变前的大小。
lvreduce -v -r -L -1G /dev/vg0/lv_apps
操作的大致步骤
# df -h //查看分区(大小)
# lvreduce -L 100G /dev/mapper/VolGroup-lv_home //释放空间
# vgdisplay //查看可用空间大小
先umount才能resize2fs
resize2fs -p /dev/mapper/VolGroup-lv_home 100G //重定大小
lvreduce -L 100G /dev/mapper/VolGroup-lv_home //释放空间
新版本的增大或减小建议使用lvresize工具来调整:
lvresize -L|--size [+|-]Size[m|UNIT] LV
示例1:将lv_apps减小1G的空间(并自动更新文件系统信息)
# vgdisplay
--- Volume group ---
VG Name vg0
VG Size <100.00 GiB
PE Size 4.00 MiB
Total PE 25599
Alloc PE / Size 25599 / <100.00 GiB
Free PE / Size 0 / 0
# lvreduce -v -r -L -1G /dev/vg0/lv_apps
Executing: /usr/sbin/fsadm --verbose check /dev/vg0/lv_apps
fsadm: "ext4" filesystem found on "/dev/mapper/vg0-lv_apps".
fsadm: Skipping filesystem check for device "/dev/mapper/vg0-lv_apps" as the filesystem is mounted on /apps
/usr/sbin/fsadm failed: 3
Executing: /usr/sbin/fsadm --verbose resize /dev/vg0/lv_apps 30404608K
fsadm: "ext4" filesystem found on "/dev/mapper/vg0-lv_apps".
fsadm: Device "/dev/mapper/vg0-lv_apps" size is 32208060416 bytes
fsadm: Parsing tune2fs -l "/dev/mapper/vg0-lv_apps"
fsadm: resize2fs needs unmounted filesystem
Do you want to unmount "/apps" ? [Y|n] y
fsadm: Executing umount /apps
umount: /apps: target is busy.
(In some cases useful info about processes that use the device is found by lsof(8) or fuser(1))
fsadm: Cannot proceed with mounted filesystem "/apps".
/usr/sbin/fsadm failed: 1
Filesystem resize failed.
有进程在使用分区会导致失败,当没有进程访问时才可能成功。
# lvreduce -v -r -L -1G /dev/vg0/lv_apps
Executing: /usr/sbin/fsadm --verbose check /dev/vg0/lv_apps
fsadm: "ext4" filesystem found on "/dev/mapper/vg0-lv_apps".
fsadm: Skipping filesystem check for device "/dev/mapper/vg0-lv_apps" as the filesystem is mounted on /apps
/usr/sbin/fsadm failed: 3
Executing: /usr/sbin/fsadm --verbose resize /dev/vg0/lv_apps 30404608K
fsadm: "ext4" filesystem found on "/dev/mapper/vg0-lv_apps".
fsadm: Device "/dev/mapper/vg0-lv_apps" size is 32208060416 bytes
fsadm: Parsing tune2fs -l "/dev/mapper/vg0-lv_apps"
fsadm: resize2fs needs unmounted filesystem
Do you want to unmount "/apps" ? [Y|n] y
fsadm: Executing umount /apps
fsadm: Executing fsck -f -p /dev/mapper/vg0-lv_apps
fsck from util-linux 2.23.2
/dev/mapper/vg0-lv_apps: 13/1966080 files (0.0% non-contiguous), 3566865/7863296 blocks
fsadm: Resizing filesystem on device "/dev/mapper/vg0-lv_apps" to 31134318592 bytes (7863296 -> 7601152 blocks of 4096 bytes)
fsadm: Executing resize2fs /dev/mapper/vg0-lv_apps 7601152
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/mapper/vg0-lv_apps to 7601152 (4k) blocks.
The filesystem on /dev/mapper/vg0-lv_apps is now 7601152 blocks long.
fsadm: Remounting unmounted filesystem back
fsadm: Executing mount /dev/mapper/vg0-lv_apps /apps
Archiving volume group "vg0" metadata (seqno 3).
Reducing logical volume vg0/lv_apps to <29.00 GiB
Size of logical volume vg0/lv_apps changed from <30.00 GiB (7679 extents) to <29.00 GiB (7423 extents).
Loading table for vg0-lv_apps (253:2).
Suspending vg0-lv_apps (253:2) with device flush
dev_open(/dev/sdb1) called while suspended
Resuming vg0-lv_apps (253:2).
Creating volume group backup "/etc/lvm/backup/vg0" (seqno 4).
Logical volume vg0/lv_apps successfully resized.
查看vg中是否有减小的空间。
# vgdisplay
Free PE / Size 256 / 1.00 GiB
为lv_data减小2G的空间。
# lvdisplay
--- Logical volume ---
LV Path /dev/vg0/lv_data
LV Name lv_data
VG Name vg0
LV Size 70.00 GiB
Current LE 17920
# lvreduce -v -r -L -2G /dev/vg0/lv_data
...
# lvdisplay
LV Size 68.00 GiB
示例2:使用百分比来减少空间
-l, --extents [-]LogicalExtentsNumber[%{VG|LV|FREE|ORIGIN}]
-l|--extents [-]Number[PERCENT]
Specifies the new size of the LV in logical extents. The --size and --extents options are alternate methods of specifying size. The total number of physical extents used will be greater when redundant data is needed for RAID levels. An alternate syntax allows the size to be determined indirectly as a percentage of the size of a related VG, LV, or set of PVs. The suffix %VG denotes the total size of the VG, the suffix %FREE the remaining free space in the VG, and the suffix %PVS the free space in the specified PVs. For a snapshot, the size can be expressed as a percentage of the total size of the origin LV with the suffix %ORIGIN (100%ORIGIN provides space for the whole origin). When expressed as a percentage, the size defines an upper limit for the number of logical extents in the new LV. The precise number of logical extents in the new LV is not determined until the command has completed. When the plus + or minus - prefix is used, the value is not an absolute size, but is relative and added or subtracted from the current size.
# lvreduce -v -r -l -10%ORIGIN /dev/vg0/lv_apps
# lvreduce -v -r -l -10%ORIGIN /dev/vg0/lv_apps
Specified LV does not have an origin LV.
# lvreduce -v -r -l -10% /dev/vg0/lv_apps
Specified % is unknown.
Invalid argument for --extents: -10%
Error during parsing of command line.
似乎不支持在缩减上使用百分比,还是建议指定有文件单位(MGT),友好可读直观。
示例3:使用LE:[逻辑扩展单元(Logical Extends)]来减少空间的使用
# lvdisplay
--- Logical volume ---
LV Path /dev/vg0/lv_apps
LV Name lv_apps
VG Name vg0
LV Size <29.00 GiB
Current LE 7423
减去23个(logical extents)
# lvreduce -v -r -l -23 /dev/vg0/lv_apps
Executing: /usr/sbin/fsadm --verbose check /dev/vg0/lv_apps
fsadm: "ext4" filesystem found on "/dev/mapper/vg0-lv_apps".
fsadm: Skipping filesystem check for device "/dev/mapper/vg0-lv_apps" as the filesystem is mounted on /apps
/usr/sbin/fsadm failed: 3
Executing: /usr/sbin/fsadm --verbose resize /dev/vg0/lv_apps 30310400K
fsadm: "ext4" filesystem found on "/dev/mapper/vg0-lv_apps".
fsadm: Device "/dev/mapper/vg0-lv_apps" size is 31134318592 bytes
fsadm: Parsing tune2fs -l "/dev/mapper/vg0-lv_apps"
fsadm: resize2fs needs unmounted filesystem
Do you want to unmount "/apps" ? [Y|n] y
fsadm: Executing umount /apps
fsadm: Executing fsck -f -p /dev/mapper/vg0-lv_apps
fsck from util-linux 2.23.2
/dev/mapper/vg0-lv_apps: 13/1900544 files (0.0% non-contiguous), 3562753/7601152 blocks
fsadm: Resizing filesystem on device "/dev/mapper/vg0-lv_apps" to 31037849600 bytes (7601152 -> 7577600 blocks of 4096 bytes)
fsadm: Executing resize2fs /dev/mapper/vg0-lv_apps 7577600
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/mapper/vg0-lv_apps to 7577600 (4k) blocks.
The filesystem on /dev/mapper/vg0-lv_apps is now 7577600 blocks long.
fsadm: Remounting unmounted filesystem back
fsadm: Executing mount /dev/mapper/vg0-lv_apps /apps
Archiving volume group "vg0" metadata (seqno 5).
Reducing logical volume vg0/lv_apps to <28.91 GiB
Size of logical volume vg0/lv_apps changed from <29.00 GiB (7423 extents) to <28.91 GiB (7400 extents).
Loading table for vg0-lv_apps (253:2).
Suspending vg0-lv_apps (253:2) with device flush
dev_open(/dev/sdb1) called while suspended
Resuming vg0-lv_apps (253:2).
Creating volume group backup "/etc/lvm/backup/vg0" (seqno 6).
Logical volume vg0/lv_apps successfully resized.
# lvdisplay
--- Logical volume ---
LV Path /dev/vg0/lv_apps
LV Name lv_apps
VG Name vg0
LV Size <28.91 GiB
Current LE 7400
达到预期。
示例4:对swap进行缩减(减去15G)
[root@freeoa /]# lvreduce -v -r -L -15G /dev/centos/swap
Executing: /usr/sbin/fsadm --verbose check /dev/centos/swap
fsadm: "swap" filesystem found on "/dev/mapper/centos-swap".
fsadm: Filesystem "swap" on device "/dev/mapper/centos-swap" is not supported by this tool.
/usr/sbin/fsadm failed: 1
Filesystem check failed.
[root@freeoa /]# lvreduce -v -L -15G /dev/centos/swap
WARNING: Reducing active logical volume to 16.00 GiB.
HIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce centos/swap? [y/n]: y
Accepted input: [y]
Archiving volume group "centos" metadata (seqno 6).
Reducing logical volume centos/swap to 16.00 GiB
Size of logical volume centos/swap changed from 31.00 GiB (7936 extents) to 16.00 GiB (4096 extents).
Loading table for centos-swap (253:1).
Suspending centos-swap (253:1) with device flush
Resuming centos-swap (253:1).
Creating volume group backup "/etc/lvm/backup/centos" (seqno 7).
Logical volume centos/swap successfully resized.
Troubleshooting
Re-read partitions
If fdisk tells you:
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.
Syncing disks.
after you write the partition table to disk, and you don't feel like rebooting,
blockdev --rereadpt /dev/disk
or if sys-apps/parted is already emerged
partprobe
Both commands force the kernel to reread the partition tables on your disks.
mount lvm 分区
磁盘如果是普通的分区,比如:ntfs、ext3等,可以直接用 mount -t <type>来制定文件系统类型来挂载,但有时候磁盘是采用的lvm卷的分区,要挂载这类磁盘,就应该按照以下方式:
1. 扫描逻辑卷:
# vgscan
2. 激活扫描到的卷:
# vgchange -a y vg0
3. 挂载
# mount /dev/vg0/lvm_root /mnt
注:/dev/VolGroup00文件夹下面可能会有几个:lvm_root、lvm_swap、lvm_home、lvm_var,根据自己的需要来挂载。
因分区表信息不对导致对硬盘不能加入到LVM
[root@localhost ~]# pvcreate /dev/vdc -v
Wiping internal VG cache
Wiping cache of LVM-capable devices
Device /dev/vdc excluded by a filter.
在LVM的配置文件(/etc/lvm/lvm.conf)中没有定义过滤规则(filter 与 global_filter 行),可以加上'-vvv'参数来展示更多的调试信息。这种问题多发生在GPT分区格式的硬盘,这需要将GPT格式分区表信息清除一次,就可以通过LVM的检测了。
Run wipefs -a /dev/vdc first to remove any FS/GPT signatures.
[root@localhost ~]# wipefs -a /dev/vdc
/dev/vdc: 8 bytes were erased at offset 0x00000200 (gpt): 45 46 49 20 50 41 52 54
/dev/vdc: 8 bytes were erased at offset 0x1f3fffffe00 (gpt): 45 46 49 20 50 41 52 54
/dev/vdc: 2 bytes were erased at offset 0x000001fe (PMBR): 55 aa
/dev/vdc: calling ioclt to re-read partition table: Success
再试一次:
[root@localhost ~]# pvcreate /dev/vdc -v
Wiping internal VG cache
Wiping cache of LVM-capable devices
Wiping signatures on new PV /dev/vdc.
Set up physical volume for "/dev/vdc" with 4194304000 available sectors.
Zeroing start of device /dev/vdc.
Writing physical volume data to disk "/dev/vdc".
Physical volume "/dev/vdc" successfully created.
平衡使用空间,将swap分布在多组硬盘上
由于业务需要,最大地使用硬盘IO,机器上有8个盘位,做了4组raid1,将系统安装在了sda上,根分区划了16G(centos5不能将根分区格式化为xfs),其余划给了home;这样剩下了三组硬盘,依次将它们各分成两个分区,第一个分区大小与home一样,将三组硬盘第二个分区打包后划给swap使用。下面是具体的过程:
先建一个三个分区的pv。
# vgcreate db_vgrp -v /dev/sdb2 /dev/sdc2 /dev/sdd2
Wiping cache of LVM-capable devices
Wiping cache of LVM-capable devices
Adding physical volume '/dev/sdb2' to volume group 'db_vgrp'
Adding physical volume '/dev/sdc2' to volume group 'db_vgrp'
Adding physical volume '/dev/sdd2' to volume group 'db_vgrp'
Archiving volume group "db_vgrp" metadata (seqno 0).
Writing physical volume data to disk "/dev/sdb2"
Physical volume "/dev/sdb2" successfully written
Writing physical volume data to disk "/dev/sdc2"
Physical volume "/dev/sdc2" successfully written
Writing physical volume data to disk "/dev/sdd2"
Physical volume "/dev/sdd2" successfully written
Creating volume group backup "/etc/lvm/backup/db_vgrp" (seqno 1).
Volume group "db_vgrp" successfully created
# lvcreate -n swap --size 46G db_vgrp
Logical volume "swap" created.
# mkswap /dev/db_vgrp/swap
mkswap: /dev/db_vgrp/swap: warning: don't erase bootbits sectors on whole disk. Use -f to force.
Setting up swapspace version 1, size = 48234492 KiB
no label, UUID=f3e681b9-28bd-4f6f-98a6-31fcbe3648ea
# mkswap -L swap -f /dev/db_vgrp/swap
Setting up swapspace version 1, size = 48234492 KiB
LABEL=swap, UUID=03267eec-bf57-4d8d-8f4a-c1a38e9caea9
-f:Force - go ahead even if the command is stupid.This allows the creation of a swap area larger than the file or partition it resides on.Without this option mkswap will refuse to erase the first block on a device with a partition table or on whole disk (e.g. /dec/sda).
# vgdisplay
--- Volume group ---
VG Name db_vgrp
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 2
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 46.09 GiB
PE Size 4.00 MiB
Total PE 11799
Alloc PE / Size 11776 / 46.00 GiB
Free PE / Size 23 / 92.00 MiB
开启swap
# swapon -va
查看swap
# cat /proc/swaps
Filename Type Size Used Priority
/dev/dm-0 partition 48234492 0 -1
# swapon -s
Filename Type Size Used Priority
/dev/dm-0 partition 48234492 0 -1
in fstab file
LABEL=swap swap swap defaults 0 0
通过lvextend将swap分区扩至4G
以某台机器上为例,查看现在的交换分区大小:
# lvdisplay
LV Path /dev/VolGroup/lv_swap
..
LV Size 1.00 GiB
将划出的vda3分区加入到VolGroup这个卷组中后,再查看一次:
# pvcreate /dev/vda3 && vgextend -v VolGroup /dev/vda3 && pvs
回写清空交换分区并关闭swap
# echo 3 >/proc/sys/vm/drop_caches && swapoff -a
将lv_swap这个逻辑卷扩到4G:
# lvextend -v -L +3G /dev/VolGroup/lv_swap
Finding volume group VolGroup
Archiving volume group "VolGroup" metadata (seqno 4).
Extending logical volume VolGroup/lv_swap to 4.00 GiB
Size of logical volume VolGroup/lv_swap changed from 1.00 GiB (256 extents) to 4.00 GiB (1024 extents).
Loading VolGroup-lv_swap table (253:1)
Suspending VolGroup-lv_swap (253:1) with device flush
Resuming VolGroup-lv_swap (253:1)
Creating volume group backup "/etc/lvm/backup/VolGroup" (seqno 5).
Logical volume lv_swap successfully resized
重做一次该swap空间
# mkswap /dev/VolGroup/lv_swap
mkswap: /dev/VolGroup/lv_swap: warning: don't erase bootbits sectors on whole disk. Use -f to force.
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=e56f4c9d-9690-44ff-a7eb-85416b3cf673
重开swap
# swapon -a
利用free指令来检查对交换空间(swap)是否达到预期。
教程四
在一台mongodb机器上加一块大硬盘
对这块硬盘分区好后加入(gpt分区):parted -l /dev/vde
加入到专用的存储卷组上
[root@freeoa ~]# vgextend -v vg01 /dev/vde1
Wiping internal VG cache
Wiping cache of LVM-capable devices
Wiping signatures on new PV /dev/vde1.
Set up physical volume for "/dev/vde1" with 8388606014 available sectors.
Zeroing start of device /dev/vde1.
Writing physical volume data to disk "/dev/vde1".
Physical volume "/dev/vde1" successfully created.
Archiving volume group "vg01" metadata (seqno 8).
Adding physical volume '/dev/vde1' to volume group 'vg01'
Volume group "vg01" will be extended by 1 new physical volumes
Creating volume group backup "/etc/lvm/backup/vg01" (seqno 9).
Volume group "vg01" successfully extended
查看物理卷及卷组信息
[root@freeoa ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 centos lvm2 a-- 99.53g 0
/dev/vdb1 vg01 lvm2 a-- 1.95t 0
/dev/vdc1 vg01 lvm2 a-- 1.95t 0
/dev/vdd1 vg01 lvm2 a-- 1.95t 0
/dev/vde1 vg01 lvm2 a-- <3.91t <3.91t
[root@freeoa ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 3 0 wz--n- 99.53g 0
vg01 4 2 0 wz--n- <9.77t <3.91t
[root@freeoa ~]# vgdisplay
--- Volume group ---
VG Name vg01
System ID
Formatlvm2
Metadata Areas4
Metadata Sequence No 9
VG Access read/write
VG Status resizable
MAX LV0
Cur LV2
Open LV 2
Max PV0
Cur PV4
Act PV4
VG Size <9.77 TiB
PE Size 64.00 MiB
Total PE 159996
Alloc PE / Size 95997 / <5.86 TiB
Free PE / Size 63999 / <3.91 TiB
VG UUID ANEfX2-mMTg-DKiY-AcYf-WK9e-SqxA-DKyHJV
对逻辑卷开始扩容(全部)及刷新文件系统
[root@freeoa ~]# lvextend -l +100%FREE /dev/mapper/vg01-lv_data && resize2fs /dev/mapper/vg01-lv_data
或直接在lvextend中刷新文件系统元数据
[root@freeoa ~]# lvextend -v -r -l +100%FREE /dev/mapper/vg01-lv_data
Converted 100%FREE into at most 63999 physical extents.
Executing: /usr/sbin/fsadm --verbose check /dev/vg01/lv_data
fsadm: "ext4" filesystem found on "/dev/mapper/vg01-lv_data".
fsadm: Skipping filesystem check for device "/dev/mapper/vg01-lv_data" as the filesystem is mounted on /data
/usr/sbin/fsadm failed: 3
Archiving volume group "vg01" metadata (seqno 9).
Extending logical volume vg01/lv_data to up to 9.57 TiB
Size of logical volume vg01/lv_data changed from 5.66 TiB (92797 extents) to 9.57 TiB (156796 extents).
Loading vg01-lv_data table (253:4)
Suspending vg01-lv_data (253:4) with device flush
Resuming vg01-lv_data (253:4)
Creating volume group backup "/etc/lvm/backup/vg01" (seqno 10).
Logical volume vg01/lv_data successfully resized.
Executing: /usr/sbin/fsadm --verbose resize /dev/vg01/lv_data 10275782656K
fsadm: "ext4" filesystem found on "/dev/mapper/vg01-lv_data".
fsadm: Device "/dev/mapper/vg01-lv_data" size is 10522401439744 bytes
fsadm: Parsing tune2fs -l "/dev/mapper/vg01-lv_data"
fsadm: Resizing filesystem on device "/dev/mapper/vg01-lv_data" to 10522401439744 bytes (1520386048 -> 2568945664 blocks of 4096 bytes)
fsadm: Executing resize2fs /dev/mapper/vg01-lv_data 2568945664
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mapper/vg01-lv_data is mounted on /data; on-line resizing required
old_desc_blocks = 725, new_desc_blocks = 1225
The filesystem on /dev/mapper/vg01-lv_data is now 2568945664 blocks long.
查看是否加入成功
[root@freeoa ~]# df -h
FilesystemSize Used Avail Use% Mounted on
/dev/mapper/centos-root46G 10G 34G 23% /
devtmpfs 16G 0 16G 0% /dev
tmpfs 16G 0 16G 0% /dev/shm
tmpfs 16G 585M 16G 4% /run
tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/vda1 453M 114M 312M 27% /boot
/dev/mapper/vg01-lv_apps 197G 433M 187G 1% /apps
/dev/mapper/vg01-lv_data 5.6T 3.2T 2.3T 59% /data
/dev/mapper/centos-home23G 45M 22G 1% /home
教程五
移除lvm格式的分区时问题
前提是相关的分区已经被卸载。
# lvremove /dev/freeoa/home
Logical volume freeoa/home contains a filesystem in use.
将已经激活的逻辑卷关闭
# lvchange -an -v /dev/freeoa/home
Deactivating logical volume freeoa/home.
Logical volume freeoa/home contains a filesystem in use.
强制的处理一次呢
# lvremove -vf /dev/freeoa/home
还是不行,不会是我虽然已经umount了相关的分区目录,但还是有程序在用那个目录吧。先看一下:
# lsof | grep home
果然有进程还记得之前的占用分区,是oracle一个进程,动手将其处理掉:
# fuser -kuc /dev/freeoa/home
再来操作一次:
# lvchange -an -v /dev/freeoa/home
Deactivating logical volume freeoa/home.
Removing freeoa-home (253:2)
# lvremove -vf /dev/freeoa/home
Archiving volume group "freeoa" metadata (seqno 4).
Releasing logical volume "home"
Creating volume group backup "/etc/lvm/backup/freeoa" (seqno 5).
Logical volume "home" successfully removed
逻辑卷移除成功,可见分区能被卸载不能代表没有进程在之前被打开的分区上工作。