LVM:logical volume manager(逻辑卷管理);LVM屏蔽了底层磁盘布局,方便于动态调整磁盘容量。
一、创建逻辑卷的步骤:
1)通过fdisk 工具将磁盘转换为linux分区
2)通过pvcreate命令将linux分区转换成物理卷(PV);
3)通过vgcreate命令将创建好的物理卷处理成卷组(VG);
4)通过lvcreate命令将卷组分成若干个逻辑卷(LV);
5)对逻辑卷进行格式化,挂载,动态调整逻辑卷的大小,并且该操作不会影响逻辑卷(Lv)上的数据。
二、物理卷(PV)创建及管理具体操作步骤:
1)先查看linux分区,将未使用空间转换为物理卷(先使用fdisk建立普通分区)
[[email protected] ~]# fdisk -l /dev/sdb #查看linux分区情况
Disk /dev/sdb: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 500 4016218+ 83 Linux
/dev/sdb2 501 1000 4016250 83 Linux
/dev/sdb3 1001 1500 4016250 83 Linux
/dev/sdb4 1501 2610 8916075 5 Extended
/dev/sdb5 1501 2610 8916043+ 83 Linux
备注:/dev/sdb是一块新增加的磁盘,上面没有任何数据,也未挂载
2)将linux物理分区转变为物理卷
[[email protected] ~]# pvcreate /dev/sdb{1,2} #将物理分区/dev/sdb{1,2}转变为物理卷
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdb2" successfully created
3)#使用Pvscan查看物理卷信息
[[email protected] ~]# pvscan #查看物理卷信息,会显示所有物理卷信息
PV /dev/sda2 VG VolGroup00 lvm2 [39.88 GB / 0 free]
PV /dev/sdb1 lvm2 [3.83 GB]
PV /dev/sdb2 lvm2 [3.83 GB]
Total: 3 [47.54 GB] / in use: 1 [39.88 GB] / in no VG: 2 [7.66 GB]
4)使用pvdisplay查看各物理卷详细参数
[[email protected] ~]# pvdisplay #查看各物理卷详细参数
--- Physical volume ---
PV Name /dev/sda2
VG Name VolGroup00
PV Size 39.90 GB / not usable 20.79 MB
Allocatable yes (but full)
PE Size (KByte) 32768
Total PE 1276
Free PE 0
Allocated PE 1276
PV UUID aJlaad-NHPT-Cgg3-7yu4-a2RJ-kJJ1-qxSFgD
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 3.83 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID v2VajD-yS53-SiQA-yTzu-KOiD-RyT3-p0wTvt
--- NEW Physical volume ---
PV Name /dev/sdb2
VG Name
PV Size 3.83 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID iOoK3V-yuww-ZlLF-cRLq-v7hC-CL7c-0bQU1x
----------------------------------------------------------------------
当物理卷没有被使用时可删除物理卷
[[email protected] /]# pvremove /dev/sdb2 #删除物理卷,
Labels on physical volume "/dev/sdb2" successfully wiped
----------------------------------------------------------------------
三、卷组(VG)创建及管理具体操作步骤:
1)使用vgcreate将物理卷转化为卷组
[[email protected] /]# vgcreate vg01 /dev/sdb{1,2} #将已经是物理卷的/dev/sdb{1,2}转化为卷组名为vg01的卷组
Volume group "vg01" successfully created
备注:以上未加参数,扩展块(PE)大小默认4M,若通过 vgcreate -s 8M vg01 /dev/sdb{1,2},则指定了扩展块大小为8M
2)使用vgdisplay 查看所有卷组详细信息
[[email protected] /]# vgdisplay #看所有卷组详细信息
--- Volume group ---
VG Name vg01
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 7.66 GB
PE Size 4.00 MB
Total PE 1960
Alloc PE / Size 0 / 0
Free PE / Size 1960 / 7.66 GB
VG UUID 1g8QL0-0cGM-TJji-Q98P-LJ3f-PhDN-2ouSM3
--- Volume group ---
VG Name VolGroup00
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 39.88 GB
PE Size 32.00 MB
Total PE 1276
Alloc PE / Size 1276 / 39.88 GB
Free PE / Size 0 / 0
VG UUID AhhisY-vDrc-s4jx-XIsn-QmCp-wMiT-2v01YZ
备注:也可以通过 [[email protected] /]# vgdisplay -v /dev/vg01 查看具体某一卷组详细信息
3)查看卷组信息
[[email protected] /]# vgscan #查看卷组信息
Reading all physical volumes. This may take a while...
Found volume group "vg01" using metadata type lvm2
Found volume group "VolGroup00" using metadata type lvm2
4)扩展卷组vgextend,将某个物理卷添加到已存在的卷组中
[[email protected] /]# pvcreate /dev/sdb3 #创建一个新的物理卷
Physical volume "/dev/sdb3" successfully created
[[email protected] /]# vgextend vg01 /dev/sdb3 #将新增的物理卷添加到vg01卷组中
Volume group "vg01" successfully extended
-----------------------------------------------------------------------
使用vgremove删除卷组
[[email protected] /]# vgremove /dev/vg01
Volume group "vg01" successfully removed
-----------------------------------------------------------------------
四、逻辑卷(LV)创建及管理具体操作步骤:
1)创建逻辑卷大小为6G卷名为data,从vg01生成
[[email protected] /]# lvcreate -L 6G -n data vg01 #从卷组vg01上划分6G的空间为逻辑卷data
Logical volume "data" created
2)对划分的逻辑卷进行格式化
[[email protected] /]# mkfs -t ext3 /dev/vg01/data #以ext3的文件格式化逻辑卷
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
786432 inodes, 1572864 blocks
78643 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1610612736
48 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
备注:也可通过 [[email protected] /]# mkfs.ext3 /dev/vg01/data 格式化
3)使用lvsacn查看逻辑卷的信息
[[email protected] /]# lvscan #查看逻辑卷的信息
ACTIVE ‘/dev/vg01/data‘ [6.00 GB] inherit
ACTIVE ‘/dev/VolGroup00/LogVol00‘ [38.88 GB] inherit
ACTIVE ‘/dev/VolGroup00/LogVol01‘ [1.00 GB] inherit
4)使用lvdisplay查看逻辑卷的具体参数:
[[email protected] /]# lvdisplay #查看逻辑卷的具体参数
--- Logical volume ---
LV Name /dev/vg01/data
VG Name vg01
LV UUID QUmuTB-ofgI-9BbG-1DvN-gWzo-7Vqb-Twmf45
LV Write Access read/write
LV Status available
# open 0
LV Size 6.00 GB
Current LE 1536
Segments 2
Allocation inherit
Read ahead sectors 0
Block device 253:2
--- Logical volume ---
LV Name /dev/VolGroup00/LogVol00
VG Name VolGroup00
LV UUID SrNP2L-bOWm-4clq-22Lh-Fg10-ydeg-7dNpdH
LV Write Access read/write
LV Status available
# open 1
LV Size 38.88 GB
Current LE 1244
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 253:0
--- Logical volume ---
LV Name /dev/VolGroup00/LogVol01
VG Name VolGroup00
LV UUID e7u6Wx-MXhq-Nc2o-lrF9-yea1-Hia5-Cv7d7e
LV Write Access read/write
LV Status available
# open 1
LV Size 1.00 GB
Current LE 32
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 253:1
备注:也可使用 [[email protected] /]# lvdisplay -v /dev/vg01/data 查看某一逻辑卷详细参数
5)使用lvextend增大逻辑卷大小,在线扩容
[[email protected] /]# lvextend -L +1G /dev/vg01/data #从卷组vg01上对逻辑卷/dev/vg01/data进行扩容,逻辑卷大小变为7GB
Extending logical volume data to 7.00 GB
Logical volume data successfully resized
6)使用resize2fs命令更新系统识别的文件系统大小,立即生效
[[email protected] /]# resize2fs /dev/vg01/data #使增加的逻辑卷大小立即生效
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/vg01/data to 1835008 (4k) blocks.
The filesystem on /dev/vg01/data is now 1835008 blocks long.
7)使用lvreduce减小逻辑卷大小,必须是离线方式(即先卸载文件系统)
[[email protected] /]# lvreduce -L -1G /dev/vg01/data #将逻辑卷/dev/vg01/data容量减小1GB
/dev/cdrom: open failed: Read-only file system
WARNING: Reducing active logical volume to 6.00 GB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce data? [y/n]: y
Reducing logical volume data to 6.00 GB
Logical volume data successfully resized
[[email protected] /]# resize2fs /dev/vg01/data #使减少的逻辑卷大小立即生效
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/vg01/data to 1572864 (4k) blocks.
resize2fs: Can‘t read an block bitmap while trying to resize /dev/vg01/data
备注:缩小逻辑卷通常要先卸载文件系统,并且缩小后空间容量必须大于等于文件当前占用的容量,若操作不当,会导致数据丢失,须谨慎。
[[email protected] /]# lvscan #查看逻辑卷大小变为6GB
ACTIVE ‘/dev/vg01/data‘ [6.00 GB] inherit
ACTIVE ‘/dev/VolGroup00/LogVol00‘ [38.88 GB] inherit
ACTIVE ‘/dev/VolGroup00/LogVol01‘ [1.00 GB] inherit
备注:
--------------------------------------------------------------------
删除逻辑卷
[[email protected] /]#lvremove /dev/vg01/data
--------------------------------------------------------------------
五、挂载逻辑卷
1)将逻辑卷挂载到/quota目录
[[email protected] /]# mount /dev/vg01/data /quota/ #将逻辑卷挂载到/quota
[[email protected] /]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
ext3 38G 11G 26G 29% /
/dev/sda1 ext3 99M 12M 82M 13% /boot
tmpfs tmpfs 233M 0 233M 0% /dev/shm
/dev/hdc iso9660 224M 224M 0 100% /media/cdrom
/dev/mapper/vg01-data
ext3 6.9G 142M 6.5G 3% /quota
2)设置开机自动挂载
[[email protected] /]#vi /etc/fstab #设置开机自动挂载
/dev/VolGroup00/LogVol00 / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
devpts /dev/pts devpts gid=5,mode=620 0 0
tmpfs /dev/shm tmpfs defaults 0 0
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
/dev/VolGroup00/LogVol01 swap swap defaults 0 0
/dev/vg01/data /quota ext3 defaults 0 0
六、逻辑卷快照管理功能,可以把卷中的数据冻结起来,类似于为这些数据拍一张照片,可以永久保存建立快照当时的状态。
1)创建卷快照
[[email protected] ~]# lvcreate -L 1G -s -n snaplv1 /dev/vg01/data # lvcreate -L 原始逻辑卷大小的15%-20% -s -n 快照名 源逻辑卷名
Logical volume "snaplv1" created
备注:和创建逻辑卷差不多,只是加了参数 -s
[[email protected] ~]# lvscan #查看状态为 snapshot
ACTIVE Original ‘/dev/vg01/data‘ [6.00 GB] inherit
ACTIVE Snapshot ‘/dev/vg01/snaplv1‘ [1.00 GB] inherit
ACTIVE ‘/dev/VolGroup00/LogVol00‘ [38.88 GB] inherit
ACTIVE ‘/dev/VolGroup00/LogVol01‘ [1.00 GB] inherit
2)创建好快照后也需要挂载点
[[email protected] ~]# mkdir /snap #创建快照挂载点
[[email protected] ~]# mount /dev/vg01/snaplv1 /snap #挂载快照到/snap
备注:snap中的文件与/quota下一样,即使在/quota下新增或删除文件,/snap仍然保持不变,此时可对/snap进行备份。
3)由于每产生一个卷快照就会占用一部分卷组空间,因此产生的卷快照越多,卷组可用的空间就会越来越少。所以在完成备份后就可删除快照了。
[[email protected] quota]# umount /snap/ #卸载快照
[[email protected] quota]# lvremove /dev/vg01/snaplv1 #删除快照
/dev/cdrom: open failed: Read-only file system
Do you really want to remove active logical volume "snaplv1"? [y/n]: y
Logical volume "snaplv1" successfully removed
七、当有一天实体磁盘的分区出现了故障,必须要更换硬盘时,该如何处理?LVM提供了pvmove工具,可以将一个物理卷上的数据转移到另外一个物理卷上。
1)将新的物理卷加入到卷组中
[[email protected] /]# pvcreate /dev/sdc1 #将linux分区转换为物理卷
Physical volume "/dev/sdc1" successfully created
[[email protected] /]# vgextend vg01 /dev/sdc1 #将新增的物理卷添加到vg01卷组中
Volume group "vg01" successfully extended
2)移动物理卷上的数据到新加入的物理卷上
[[email protected] ~]# pvmove /dev/sdb1 /dev/sdc1 #将/dev/sdb1的数据移动到/dev/sdc1上
/dev/sdb1: Moved: 41.7%
/dev/sdb1: Moved: 84.2%
/dev/sdb1: Moved: 100.0%
备注:此时可使用pvscan查看变化
3)将旧的物理卷从卷组中卸载
[[email protected] ~]# vgreduce vg01 /dev/sdb1 #使用vgreduce将/dev/sdb1从卷组vg01中分离
Removed "/dev/sdb1" from volume group "vg01"
[[email protected] ~]# pvremove /dev/sdb1 #最后要能够取下物理磁盘维修,则需要删除此物理卷;若物理磁盘划分成了多个物理卷,则需要将所有物理卷都删除才可以。
Labels on physical volume "/dev/sdb1" successfully wiped
八、当有一天需要将整个LVM的磁盘迁移到另外一台计算机中,则需要按以下步骤:
1)在原先的计算机上导出卷组
[[email protected] ~]#umount /dev/vg01/data #导出卷组前要先卸载该卷组中所有逻辑卷
[[email protected] ~]#vgchange -a n vg01 #使用vgchange将卷组修改为非有效(inactive)配置
[[email protected] ~]#vgexport vg01 #使用vgexport导出卷组
2)将LVM磁盘安装到目的计算机上
3)在目的计算机上导入卷组
[[email protected] ~]#pvscan #使用pvscan扫描所有物理卷,以便让linux可以驱动这些物理卷
[[email protected] ~]#vgimport vg01 #导入卷组
[[email protected] ~]#vgchange -a y vg01 #将卷组修改为有效(active)配置
4)挂载逻辑卷
[[email protected] ~]#mount /devv/vg01/data /quota #将逻辑卷挂载到文件系统
九、 以上是对新增磁盘进行LVM划分。而通常在安装系统时就采用了LVM划分磁盘,按需调整磁盘空间。当有一天,你发现某个文件系统空间不够时,此时需要扩容。
比如说:现在有需求要将weblogic92安装在 /weblogic下。而 / 空间不够。这样就需要将新增的逻辑卷挂载到/weblogic就OK了。
1)使用df查看各文件系统大小
[[email protected] /]# df -hT #查看各文件系统大小
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vg00-LogVol00
ext3 3.0G 2.0G 820M 71% /
/dev/mapper/vg00-lvopt
ext3 3.0G 69M 2.7G 3% /opt
/dev/mapper/vg00-lvusr
ext3 6.8G 4.3G 2.2G 67% /usr
/dev/mapper/vg00-lvhome
ext3 3.0G 75M 2.7G 3% /home
/dev/mapper/vg00-lvpublic
ext3 20G 1.8G 17G 10% /public
/dev/mapper/vg00-lvtmp
ext3 3.0G 70M 2.7G 3% /tmp
/dev/mapper/vg00-lvvar
ext3 3.0G 177M 2.6G 7% /var
/dev/sda1 ext3 99M 20M 75M 21% /boot
tmpfs tmpfs 7.9G 0 7.9G 0% /dev/shm
2)使用lvscan查看可知道卷组名字为vg00
[[email protected] /]# lvscan #使用lvscan查看
ACTIVE ‘/dev/vg00/LogVol00‘ [3.00 GB] inherit
ACTIVE ‘/dev/vg00/lvopt‘ [3.00 GB] inherit
ACTIVE ‘/dev/vg00/lvusr‘ [7.00 GB] inherit
ACTIVE ‘/dev/vg00/lvhome‘ [3.00 GB] inherit
ACTIVE ‘/dev/vg00/lvpublic‘ [20.00 GB] inherit
ACTIVE ‘/dev/vg00/lvtmp‘ [3.00 GB] inherit
ACTIVE ‘/dev/vg00/lvvar‘ [3.00 GB] inherit
ACTIVE ‘/dev/vg00/LogVol01‘ [17.62 GB] inherit
3) [[email protected] /]#lvcreate -L 20G -n lvweblogic vg00 #从卷组vg00上划分20G的空间为逻辑卷lvweblogic
Logical volume "lvweblogic" created
4)对划分的逻辑卷进行格式化
[[email protected] /]# mkfs -t ext3 /dev/vg00/lvweblogic #以ext3的文件格式化逻辑卷
备注:注意,接下来是挂载,为了使开机自动挂载,需要修改/etc/fstab,可参照上面逻辑卷创建方法。
十、逻辑卷创建及删除推荐步骤
创建逻辑卷(LV)的顺序:Linux分区---物理卷(PV)---卷组(VG)---逻辑卷(LV)---挂载到文件系统
删除逻辑卷(LV)的顺序:卸载文件系统----逻辑卷(LV)---卷组(VG)----物理卷(PV)---Linux分区
本文出自 “唐光辉” 博客,请务必保留此出处http://firefly222.blog.51cto.com/899793/413515
LVM---动态调整磁盘容量