一、创建逻辑卷的步骤:
fdisk 工具将磁盘转换为linux分区
pvcreate命令将linux分区转换成物理卷(PV);
vgcreate命令将创建好的物理卷处理成卷组(VG);
lvcreate命令将卷组分成若干个逻辑卷(LV);
对逻辑卷进行格式化,挂载,动态调整逻辑卷的大小,并且该操作不会影响逻辑卷(Lv)上的数据。
物理卷(PV)创建及管理具体操作步骤:
先查看linux分区,将未使用空间转换为物理卷(先使用fdisk建立普通分区)
# 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是一块新增加的磁盘,上面没有任何数据,也未挂载
将linux物理分区转变为物理卷
# pvcreate /dev/sdb{1,2} #将物理分区/dev/sdb{1,2}转变为物理卷
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdb2" successfully created
#使用Pvscan查看物理卷信息
# 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]
使用pvdisplay查看各物理卷详细参数
# 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
当物理卷没有被使用时可删除物理卷
# pvremove /dev/sdb2 #删除物理卷,
Labels on physical volume "/dev/sdb2" successfully wiped
卷组(VG)创建及管理具体操作步骤:
使用vgcreate将物理卷转化为卷组
# 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
使用vgdisplay 查看所有卷组详细信息
# 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
可通过vgdisplay -v /dev/vg01 查看具体某一卷组详细信息
查看卷组信息
# 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
扩展卷组vgextend,将某个物理卷添加到已存在的卷组中
# pvcreate /dev/sdb3 #创建一个新的物理卷
Physical volume "/dev/sdb3" successfully created
# vgextend vg01 /dev/sdb3 #将新增的物理卷添加到vg01卷组中
Volume group "vg01" successfully extended
使用vgremove删除卷组
# vgremove /dev/vg01
Volume group "vg01" successfully removed
逻辑卷(LV)创建及管理具体操作步骤:
创建逻辑卷大小为6G卷名为data,从vg01生成
]# lvcreate -L 6G -n data vg01 #从卷组vg01上划分6G的空间为逻辑卷data
Logical volume "data" created
对划分的逻辑卷进行格式化
# 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 格式化
使用lvsacn查看逻辑卷的信息
# 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
使用lvdisplay查看逻辑卷的具体参数:
# 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 查看某一逻辑卷详细参数
使用lvextend增大逻辑卷大小,在线扩容
# lvextend -L +1G /dev/vg01/data #从卷组vg01上对逻辑卷/dev/vg01/data进行扩容,逻辑卷大小变为7GB
Extending logical volume data to 7.00 GB
Logical volume data successfully resized
使用resize2fs命令更新系统识别的文件系统大小,立即生效
# 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.
使用lvreduce减小逻辑卷大小,必须是离线方式(即先卸载文件系统)
# 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
# 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
缩小逻辑卷通常要先卸载文件系统,并且缩小后空间容量必须大于等于文件当前占用的容量,若操作不当,会导致数据丢失,须谨慎。
# 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
删除逻辑卷
#lvremove /dev/vg01/data
挂载逻辑卷
将逻辑卷挂载到/quota目录
# mount /dev/vg01/data /quota/ #将逻辑卷挂载到/quota
# 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
设置开机自动挂载
#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
逻辑卷快照管理功能,可以把卷中的数据冻结起来,类似于为这些数据拍一张照片,可以永久保存建立快照当时的状态。
创建卷快照
# lvcreate -L 1G -s -n snaplv1 /dev/vg01/data # lvcreate -L 原始逻辑卷大小的15%-20% -s -n 快照名 源逻辑卷名
Logical volume "snaplv1" created
备注:和创建逻辑卷差不多,只是加了参数 -s
# 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
创建好快照后也需要挂载点
# mkdir /snap #创建快照挂载点
# mount /dev/vg01/snaplv1 /snap #挂载快照到/snap
snap中的文件与/quota下一样,即使在/quota下新增或删除文件,/snap仍然保持不变,此时可对/snap进行备份。
由于每产生一个卷快照就会占用一部分卷组空间,因此产生的卷快照越多,卷组可用的空间就会越来越少。所以在完成备份后就可删除快照了。
# umount /snap/ #卸载快照
# 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工具,可以将一个物理卷上的数据转移到另外一个物理卷上。
将新的物理卷加入到卷组中
# pvcreate /dev/sdc1 #将linux分区转换为物理卷
Physical volume "/dev/sdc1" successfully created
# vgextend vg01 /dev/sdc1 #将新增的物理卷添加到vg01卷组中
Volume group "vg01" successfully extended
2)移动物理卷上的数据到新加入的物理卷上
# 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)将旧的物理卷从卷组中卸载
# vgreduce vg01 /dev/sdb1 #使用vgreduce将/dev/sdb1从卷组vg01中分离
Removed "/dev/sdb1" from volume group "vg01"
# pvremove /dev/sdb1 #最后要能够取下物理磁盘维修,则需要删除此物理卷;若物理磁盘划分成了多个物理卷,则需要将所有物理卷都删除才可以。
Labels on physical volume "/dev/sdb1" successfully wiped
将整个LVM的磁盘迁移到另外一台计算机中,需要按以下步骤:
在原先的计算机上导出卷组
#umount /dev/vg01/data #导出卷组前要先卸载该卷组中所有逻辑卷
#vgchange -a n vg01 #使用vgchange将卷组修改为非有效(inactive)配置
#vgexport vg01 #使用vgexport导出卷组
将LVM磁盘安装到目的计算机上
在目的计算机上导入卷组
#pvscan #使用pvscan扫描所有物理卷,以便让linux可以驱动这些物理卷
#vgimport vg01 #导入卷组
#vgchange -a y vg01 #将卷组修改为有效(active)配置
挂载逻辑卷
#mount /devv/vg01/data /quota #将逻辑卷挂载到文件系统