lvm howto
lvm(logic volume manager)原先是ibm aix系统提供的弹性管理硬盘工具。后来ibm将其迁移到了linux下。RHEL3以前提供的是lvm1,RHEL4之后提供的是lvm2。lvm2改进是:可以在线调整卷大小;可以以可读可写方式建立卷快照,lvm1只能对只读文件系统建立快照。LVM组成:物理卷(PV)、卷组(VG)、逻辑卷(LV)
PV:物理卷就是硬盘分区,只不过是作为lvm的物理卷分区,系统识别码必须为“8e Linux LVM”。
- Device Boot Start End Blocks Id System
- /dev/hdb1 1 1985 1000408+ 8e Linux LVM
- /dev/hdb2 1986 4161 1096704 8e Linux LVM
Device Boot Start End Blocks Id System /dev/hdb1 1 1985 1000408+ 8e Linux LVM /dev/hdb2 1986 4161 1096704 8e Linux LVM
VG:由一个或多个PV组成的卷组。
LV:逻辑卷的空间由VG提供,LV当做原本的分区使用,即文件系统是建立在LV上的。
lvm常用管理工具:
——物理卷——-卷组——-逻辑卷
扫描 | pvscan | vgscan | lvscan
查看 | pvdisplay | vgdisplay | lvdisplay
新建 | pvcreat | vgcreat | lvcreat
卸载 | pvremove | vgremove | lvremove
增大 | 无 | vgextend | lvextend
减小 | 无 | vgreduce | lvreduce
一、lvm卷的建立
lvm的建立分为三部,先建立PV,然后在PV上建立VG,最后在VG上建立LV,具体步骤如下:
1、建立PV
属于PV的分区系统识别码必须为“8e Linux LVM”。可以用fdisk或parted建立。
- pvcreate /dev/hd[bd]1
- #把/dev/hdb1、/dev/hdd1建为PV
pvcreate /dev/hd[bd]1 #把/dev/hdb1、/dev/hdd1建为PV
2、建立VG
- vgcreate 1stVG /dev/hd[bd]1
- #在PV /dev/hdb1、/dev/hdd1上建立名为1stVG的卷组
vgcreate 1stVG /dev/hd[bd]1 #在PV /dev/hdb1、/dev/hdd1上建立名为1stVG的卷组
3、建立LV
- lvcreate -L 1024M -n LV1 1stVG
- #在VG 1stVG上建立名为LV1,大小为1024M的LV
- #-L SIZE
- #-n LVNAME
- mkfs.ext3 /dev/1stVG/LV1
- #格式化该LV为ext3文件系统
lvcreate -L 1024M -n LV1 1stVG #在VG 1stVG上建立名为LV1,大小为1024M的LV #-L SIZE #-n LVNAME mkfs.ext3 /dev/1stVG/LV1 #格式化该LV为ext3文件系统
在没有建立LV时,系统没有/dev/1stVG/这个设备,只有建立了LV后才会出现/dev/1stVG/LV1。这个设备可以当做分区使用了。
二、管理lvm
1、查看信息
查看PV信息:
- pvs
- #简洁信息
- PV VG Fmt Attr PSize PFree
- /dev/hdb1 1stVG lvm2 a- 976.00M 0
- /dev/hdd1 1stVG lvm2 a- 976.00M 928.00M
- pvdisplay
- #较详细信息
- --- Physical volume ---
- PV Name /dev/hdb1
- VG Name 1stVG
- PV Size 976.96 MB / not usable 984.50 KB
- Allocatable yes (but full)
- PE Size (KByte) 4096
- Total PE 244
- Free PE 0
- Allocated PE 244
- PV UUID asIP1o-Fct0-IB3t-J2E2-L8ed-fe4B-8ZfuD1
- --- Physical volume ---
- PV Name /dev/hdd1
- VG Name 1stVG
- PV Size 976.96 MB / not usable 984.50 KB
- Allocatable yes
- PE Size (KByte) 4096
- Total PE 244
- Free PE 232
- Allocated PE 12
- PV UUID UFRnbH-BSp1-2aFF-SHHB-tIAN-GJTk-6mrfA9
pvs #简洁信息 PV VG Fmt Attr PSize PFree /dev/hdb1 1stVG lvm2 a- 976.00M 0 /dev/hdd1 1stVG lvm2 a- 976.00M 928.00M pvdisplay #较详细信息 --- Physical volume --- PV Name /dev/hdb1 VG Name 1stVG PV Size 976.96 MB / not usable 984.50 KB Allocatable yes (but full) PE Size (KByte) 4096 Total PE 244 Free PE 0 Allocated PE 244 PV UUID asIP1o-Fct0-IB3t-J2E2-L8ed-fe4B-8ZfuD1 --- Physical volume --- PV Name /dev/hdd1 VG Name 1stVG PV Size 976.96 MB / not usable 984.50 KB Allocatable yes PE Size (KByte) 4096 Total PE 244 Free PE 232 Allocated PE 12 PV UUID UFRnbH-BSp1-2aFF-SHHB-tIAN-GJTk-6mrfA9
查看VG信息:
- vgs
- #简洁信息
- VG #PV #LV #SN Attr VSize VFree
- 1stVG 2 1 0 wz--n- 1.91G 928.00M
- vgdisplay
- #较详细信息
- --- Volume group ---
- VG Name 1stVG
- System ID
- Format lvm2
- Metadata Areas 2
- Metadata Sequence No 2
- VG Access read/write
- VG Status resizable
- MAX LV 0
- Cur LV 1
- Open LV 0
- Max PV 0
- Cur PV 2
- Act PV 2
- VG Size 1.91 GB
- PE Size 4.00 MB
- Total PE 488
- Alloc PE / Size 256 / 1.00 GB
- Free PE / Size 232 / 928.00 MB
- VG UUID kzKK4Z-6wAX-BDX1-tLAY-6jHR-nBL2-khW27N
- vgdisplay -v
- #更详细的信息
- Finding all volume groups
- Finding volume group "1stVG"
- --- Volume group ---
- VG Name 1stVG
- System ID
- Format lvm2
- Metadata Areas 2
- Metadata Sequence No 2
- VG Access read/write
- VG Status resizable
- MAX LV 0
- Cur LV 1
- Open LV 0
- Max PV 0
- Cur PV 2
- Act PV 2
- VG Size 1.91 GB
- PE Size 4.00 MB
- Total PE 488
- Alloc PE / Size 256 / 1.00 GB
- Free PE / Size 232 / 928.00 MB
- VG UUID kzKK4Z-6wAX-BDX1-tLAY-6jHR-nBL2-khW27N
- --- Logical volume ---
- LV Name /dev/1stVG/LV1
- VG Name 1stVG
- LV UUID n1wmJJ-aKLB-iF8T-MYz3-dGgU-yOJg-IcslE8
- LV Write Access read/write
- LV Status available
- # open 0
- LV Size 1.00 GB
- Current LE 256
- Segments 2
- Allocation inherit
- Read ahead sectors auto
- - currently set to 256
- Block device 253:0
- --- Physical volumes ---
- PV Name /dev/hdb1
- PV UUID asIP1o-Fct0-IB3t-J2E2-L8ed-fe4B-8ZfuD1
- PV Status allocatable
- Total PE / Free PE 244 / 0
- PV Name /dev/hdd1
- PV UUID UFRnbH-BSp1-2aFF-SHHB-tIAN-GJTk-6mrfA9
- PV Status allocatable
- Total PE / Free PE 244 / 232
vgs #简洁信息 VG #PV #LV #SN Attr VSize VFree 1stVG 2 1 0 wz--n- 1.91G 928.00M vgdisplay #较详细信息 --- Volume group --- VG Name 1stVG System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 2 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 0 Max PV 0 Cur PV 2 Act PV 2 VG Size 1.91 GB PE Size 4.00 MB Total PE 488 Alloc PE / Size 256 / 1.00 GB Free PE / Size 232 / 928.00 MB VG UUID kzKK4Z-6wAX-BDX1-tLAY-6jHR-nBL2-khW27N vgdisplay -v #更详细的信息 Finding all volume groups Finding volume group "1stVG" --- Volume group --- VG Name 1stVG System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 2 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 0 Max PV 0 Cur PV 2 Act PV 2 VG Size 1.91 GB PE Size 4.00 MB Total PE 488 Alloc PE / Size 256 / 1.00 GB Free PE / Size 232 / 928.00 MB VG UUID kzKK4Z-6wAX-BDX1-tLAY-6jHR-nBL2-khW27N --- Logical volume --- LV Name /dev/1stVG/LV1 VG Name 1stVG LV UUID n1wmJJ-aKLB-iF8T-MYz3-dGgU-yOJg-IcslE8 LV Write Access read/write LV Status available # open 0 LV Size 1.00 GB Current LE 256 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 --- Physical volumes --- PV Name /dev/hdb1 PV UUID asIP1o-Fct0-IB3t-J2E2-L8ed-fe4B-8ZfuD1 PV Status allocatable Total PE / Free PE 244 / 0 PV Name /dev/hdd1 PV UUID UFRnbH-BSp1-2aFF-SHHB-tIAN-GJTk-6mrfA9 PV Status allocatable Total PE / Free PE 244 / 232
查看LV信息:
- lvs
- #简洁信息
- LV VG Attr LSize Origin Snap% Move Log Copy% Convert
- LV1 1stVG -wi-a- 1.00G
- lvdisplay
- #较详细信息
- --- Logical volume ---
- LV Name /dev/1stVG/LV1
- VG Name 1stVG
- LV UUID n1wmJJ-aKLB-iF8T-MYz3-dGgU-yOJg-IcslE8
- LV Write Access read/write
- LV Status available
- # open 0
- LV Size 1.00 GB
- Current LE 256
- Segments 2
- Allocation inherit
- Read ahead sectors auto
- - currently set to 256
- Block device 253:0
lvs #简洁信息 LV VG Attr LSize Origin Snap% Move Log Copy% Convert LV1 1stVG -wi-a- 1.00G lvdisplay #较详细信息 --- Logical volume --- LV Name /dev/1stVG/LV1 VG Name 1stVG LV UUID n1wmJJ-aKLB-iF8T-MYz3-dGgU-yOJg-IcslE8 LV Write Access read/write LV Status available # open 0 LV Size 1.00 GB Current LE 256 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0
2、调整卷大小
调整VG:
- vgs
- #1stVG有2个PV
- VG #PV #LV #SN Attr VSize VFree
- 1stVG 2 1 0 wz--n- 1.91G 928.00M
- #增大VG:用PV /dev/hdb2、/dev/hdd2增大1stVG
- vgextend 1stVG /dev/hd[bd]2
- vgs
- #1stVG的PV增加为4个
- VG #PV #LV #SN Attr VSize VFree
- 1stVG 4 1 0 wz--n- 3.99G 2.99G
- #减小VG:从1stVG中删除PV /dev/hdd2
- vgreduce 1stVG /dev/hdd2
- vgs
- #1stVG的PV变成3个
- VG #PV #LV #SN Attr VSize VFree
- 1stVG 3 1 0 wz--n- 2.95G 1.95G
vgs #1stVG有2个PV VG #PV #LV #SN Attr VSize VFree 1stVG 2 1 0 wz--n- 1.91G 928.00M #增大VG:用PV /dev/hdb2、/dev/hdd2增大1stVG vgextend 1stVG /dev/hd[bd]2 vgs #1stVG的PV增加为4个 VG #PV #LV #SN Attr VSize VFree 1stVG 4 1 0 wz--n- 3.99G 2.99G #减小VG:从1stVG中删除PV /dev/hdd2 vgreduce 1stVG /dev/hdd2 vgs #1stVG的PV变成3个 VG #PV #LV #SN Attr VSize VFree 1stVG 3 1 0 wz--n- 2.95G 1.95G
调整LV:
调整LV需要注意:先放大LV再放大文件系统,先缩小文件系统再缩小LV。调整ext文件系统大小的工具resize2fs只能离线(umount)缩小文件系统。
调整文件系统本来就有风险,必须先备份资料才能调整。
- lvs
- #增加LV1前1G
- LV VG Attr LSize Origin Snap% Move Log Copy% Convert
- LV1 1stVG -wi-a- 1.00G
- tune2fs -l /dev/1stVG/LV1 |grep Block
- #增加LV1前文件系统大小262144*4096=1G
- Block count: 262144
- Block size: 4096
- Blocks per group: 32768
- #增大LV:增大LV1,-L +SIZE表示增加SIZE
- lvextend -L +512M /dev/1stVG/LV1
- lvs
- #增加LV1后1.5G
- LV VG Attr LSize Origin Snap% Move Log Copy% Convert
- LV1 1stVG -wi-a- 1.50G
- tune2fs -l /dev/1stVG/LV1 |grep Block
- #增加LV1后文件系统大小仍然为262144*4096=1G
- Block count: 262144
- Block size: 4096
- Blocks per group: 32768
- #调整文件系统大小:增大LV1为分区大小
- resize2fs /dev/1stVG/LV1
- tune2fs -l /dev/1stVG/LV1 |grep Block
- #调整LV1文件系统大小后为393216*4096=1.5G
- Block count: 393216
- Block size: 4096
- Blocks per group: 32768
- #调整文件系统大小:减小LV1为1024M
- resize2fs /dev/1stVG/LV1 1024M
- #减小LV:减小LV1,-L -SIZE表示减小SIZE
- lvreduce -L -512M /dev/1stVG/LV1
- tune2fs -l /dev/1stVG/LV1 |grep Block
- #减小LV1文件系统大小后为262144*4096=1G
- Block count: 262144
- Block size: 4096
- Blocks per group: 32768
- lvs
- #减小LV1后为1G
- LV VG Attr LSize Origin Snap% Move Log Copy% Convert
- LV1 1stVG -wi-a- 1.00G
lvs #增加LV1前1G LV VG Attr LSize Origin Snap% Move Log Copy% Convert LV1 1stVG -wi-a- 1.00G tune2fs -l /dev/1stVG/LV1 |grep Block #增加LV1前文件系统大小262144*4096=1G Block count: 262144 Block size: 4096 Blocks per group: 32768 #增大LV:增大LV1,-L +SIZE表示增加SIZE lvextend -L +512M /dev/1stVG/LV1 lvs #增加LV1后1.5G LV VG Attr LSize Origin Snap% Move Log Copy% Convert LV1 1stVG -wi-a- 1.50G tune2fs -l /dev/1stVG/LV1 |grep Block #增加LV1后文件系统大小仍然为262144*4096=1G Block count: 262144 Block size: 4096 Blocks per group: 32768 #调整文件系统大小:增大LV1为分区大小 resize2fs /dev/1stVG/LV1 tune2fs -l /dev/1stVG/LV1 |grep Block #调整LV1文件系统大小后为393216*4096=1.5G Block count: 393216 Block size: 4096 Blocks per group: 32768 #调整文件系统大小:减小LV1为1024M resize2fs /dev/1stVG/LV1 1024M #减小LV:减小LV1,-L -SIZE表示减小SIZE lvreduce -L -512M /dev/1stVG/LV1 tune2fs -l /dev/1stVG/LV1 |grep Block #减小LV1文件系统大小后为262144*4096=1G Block count: 262144 Block size: 4096 Blocks per group: 32768 lvs #减小LV1后为1G LV VG Attr LSize Origin Snap% Move Log Copy% Convert LV1 1stVG -wi-a- 1.00G
3、卸载卷
卸载卷时需要注意:卸载LV前必须先umount,并做好备份。一旦卸载,所以文件将消失。卸载VG前必须卸载该卷组的所有LV。卸载PV前必须确保此物理卷不属于任何VG。
卸载LV:
- lvremove /dev/1stVG/LV1
lvremove /dev/1stVG/LV1
卸载VG:
- vgremove 1stVG
vgremove 1stVG
卸载PV:
- pvremove /dev/hdd2
pvremove /dev/hdd2
三、lvm的其他功能
1、快照
快照可以保存某一时刻的数据,就像给磁盘拍了张照片。
- #建立LV1的快照,参数 -s。
- lvcreate -L 512M -s -n S_LV1 /dev/1stVG/LV1
- lvs
- LV VG Attr LSize Origin Snap% Move Log Copy% Convert
- LV1 1stVG owi-a- 1.00G
- S_LV1 1stVG swi-a- 512.00M LV1 0.00
- #读取快照,只需将其挂载即可。
- mount /dev/1stVG/S_LV1 /mnt
- #删除快照,如果删除一个LV,则该LV的快照都会被删除。
- lvremove /dev/1stVG/S_LV1
#建立LV1的快照,参数 -s。 lvcreate -L 512M -s -n S_LV1 /dev/1stVG/LV1 lvs LV VG Attr LSize Origin Snap% Move Log Copy% Convert LV1 1stVG owi-a- 1.00G S_LV1 1stVG swi-a- 512.00M LV1 0.00 #读取快照,只需将其挂载即可。 mount /dev/1stVG/S_LV1 /mnt #删除快照,如果删除一个LV,则该LV的快照都会被删除。 lvremove /dev/1stVG/S_LV1
2、移动卷
更换硬盘时常常要将旧硬盘上PV中的数据移动到新硬盘的PV中,lvm提供了一个pv移动工具pvmove
- #加入新PV
- pvcreate /dev/hdd2
- #增大VG
- vgextend 1stVG /dev/hdd2
- pvs
- #/dev/hdd2是新加入的PV,空间全部空闲。
- PV VG Fmt Attr PSize PFree
- /dev/hdb1 1stVG lvm2 a- 976.00M 976.00M
- /dev/hdb2 1stVG lvm2 a- 1.04G 556.00M
- /dev/hdd1 1stVG lvm2 a- 976.00M 976.00M
- /dev/hdd2 1stVG lvm2 a- 1.04G 1.04G
- #移动卷:把PV /dev/hdb2的数据移动到PV /dev/hdd2中
- pvmove /dev/hdb2 /dev/hdd2
- pvs
- #移动后,PV /dev/hdb2空,数据移动到了/dev/hdd2上。
- PV VG Fmt Attr PSize PFree
- /dev/hdb1 1stVG lvm2 a- 976.00M 976.00M
- /dev/hdb2 1stVG lvm2 a- 1.04G 1.04G
- /dev/hdd1 1stVG lvm2 a- 976.00M 976.00M
- /dev/hdd2 1stVG lvm2 a- 1.04G 556.00M
#加入新PV pvcreate /dev/hdd2 #增大VG vgextend 1stVG /dev/hdd2 pvs #/dev/hdd2是新加入的PV,空间全部空闲。 PV VG Fmt Attr PSize PFree /dev/hdb1 1stVG lvm2 a- 976.00M 976.00M /dev/hdb2 1stVG lvm2 a- 1.04G 556.00M /dev/hdd1 1stVG lvm2 a- 976.00M 976.00M /dev/hdd2 1stVG lvm2 a- 1.04G 1.04G #移动卷:把PV /dev/hdb2的数据移动到PV /dev/hdd2中 pvmove /dev/hdb2 /dev/hdd2 pvs #移动后,PV /dev/hdb2空,数据移动到了/dev/hdd2上。 PV VG Fmt Attr PSize PFree /dev/hdb1 1stVG lvm2 a- 976.00M 976.00M /dev/hdb2 1stVG lvm2 a- 1.04G 1.04G /dev/hdd1 1stVG lvm2 a- 976.00M 976.00M /dev/hdd2 1stVG lvm2 a- 1.04G 556.00M
3、将lvm迁移到其他系统
迁移lvm分3部:导出卷,物理迁移硬盘,导入卷。
导出卷:
- #下线VG
- vgchange -a n [VGNAME]
- #导出VG
- vgexport -a [VGNAME]
#下线VG vgchange -a n [VGNAME] #导出VG vgexport -a [VGNAME]
导入卷:
- #扫描PV
- pvscan
- #导入VG
- vgimport -a [VGNAME]
- #上线VG
- vgchange -a y [VGNAME]
#扫描PV pvscan #导入VG vgimport -a [VGNAME] #上线VG vgchange -a y [VGNAME]
四、参考资料
lvm howto文档:http://tldp.org/HOWTO/LVM-HOWTO/
radhat lvm文档:http://www.redhat.com/docs/en-US/Red_Hat_Enterprise_Linux/5.4/html/Logical_Volume_Manager_Administration/index.html