http://blog.qq.com/qzone/232480/1308240550.htm
http://kerry.blog.51cto.com/172631/568968/
http://firefly222.blog.51cto.com/899793/413515
http://www.chinaunix.net/jh/4/910692.html
LVM:logical volume manager(逻辑卷管理);LVM屏蔽了底层磁盘布局,方便于动态调整磁盘容量。
一、创建逻辑卷的步骤:
1)通过fdisk 工具将磁盘转换为linux分区
2)通过pvcreate命令将linux分区转换成物理卷(PV);
3)通过vgcreate命令将创建好的物理卷处理成卷组(VG);
4)通过lvcreate命令将卷组分成若干个逻辑卷(LV);
5)对逻辑卷进行格式化,挂载,动态调整逻辑卷的大小,并且该操作不会影响逻辑卷(Lv)上的数据。
二、物理卷(PV)创建及管理具体操作步骤:
1)先查看linux分区,将未使用空间转换为物理卷(先使用fdisk建立普通分区)
[root@RHEL5 ~]# fdisk -l #查看linux分区情况
Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 1045 8289540 83 Linux
/dev/sda3 1046 1306 2096482+ 82 Linux swap / Solaris
/dev/sda4 1307 3916 20964825 5 Extended
/dev/sda5 1307 3916 20964793+ 83 Linux
Disk /dev/sdb: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdb doesn't contain a valid partition table
备注:/dev/sdb是一块新增加的磁盘,上面没有任何数据,也未挂载
创建物理分区sdb1,sdb2
The number of cylinders for this disk is set to 2610.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-2610, default 2610): +10000M
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (1218-2610, default 1218):
Using default value 1218
Last cylinder or +size or +sizeM or +sizeK (1218-2610, default 2610):
Using default value 2610
Command (m for help): w
The partition table has been altered!
[root@RHEL5 ~]# pvcreate /dev/sdb{1,2} #将物理分区/dev/sdb{1,2}转变为物理卷
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdb2" successfully created
3)#使用Pvscan查看物理卷信息
[root@RHEL5 ~]# pvscan #查看物理卷信息,会显示所有物理卷信息
PV /dev/sdb1 lvm2 [9.32 GB]
PV /dev/sdb2 lvm2 [10.67 GB]
Total: 2 [19.99 GB] / in use: 0 [0 ] / in no VG: 2 [19.99 GB]
4)使用pvdisplay查看各物理卷详细参数
[root@RHEL5 ~]# pvdisplay #查看各物理卷详细参数
"/dev/sdb1" is a new physical volume of "9.32 GB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 9.32 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID CZe58k-wniT-hCSS-Zufc-qnTI-CNIl-ZEer18
"/dev/sdb2" is a new physical volume of "10.67 GB"
--- NEW Physical volume ---
PV Name /dev/sdb2
VG Name
PV Size 10.67 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID srwJze-YUNc-aFnm-Ny4x-Z3SS-4p6e-myRyH2
----------------------------------------------------------------------
当物理卷没有被使用时可删除物理卷
[root@RHEL5 /]# pvremove /dev/sdb2 #删除物理卷,
Labels on physical volume "/dev/sdb2" successfully wiped
----------------------------------------------------------------------
三、卷组(VG)创建及管理具体操作步骤:
当多个物理卷组合成一个卷组后时,LVM会在所有的物理卷上做类似格式化的工作,将每个物理卷切成一块一块的空间,这一块一块
的空间就称为PE(Physical Extent ),它的默认大小是4MB。
由于受内核限制的原因,一个逻辑卷(Logic Volume)最多只能包含65536个PE(Physical Extent),所以一个PE的大小就决定了
逻辑卷的最大容量,4 MB 的PE决定了单个逻辑卷最大容量为 256 GB,若希望使用大于256G的逻辑卷,则创建卷组时需要指定更大
的PE。在Red Hat Enterprise Linux AS 4中PE大小范围为8 KB 到 16GB,并且必须总是 2 的倍数。
例如,如果希望使用 64 MB 的PE创建卷组,这样逻辑卷最大容量就可以为4 TB,命令如下:
vgcreate - 64MB VolGroup01 /dev/sdb1
1)使用vgcreate将物理卷转化为卷组
[root@RHEL5 /]# 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 查看所有卷组详细信息
[root@RHEL5 /]# 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 19.99 GB
PE Size 4.00 MB
Total PE 5117
Alloc PE / Size 0 / 0
Free PE / Size 5117 / 19.99 GB
VG UUID YYVrs9-D5eA-MxrT-3qUX-Gviz-L3dP-zOPut3
备注:也可以通过 [root@RHEL5 /]# vgdisplay -v /dev/vg01 查看具体某一卷组详细信息
3)查看卷组信息
[root@RHEL5 /]# 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,将某个物理卷添加到已存在的卷组中
[root@RHEL5 /]# pvcreate /dev/sdb3 #创建一个新的物理卷
Physical volume "/dev/sdb3" successfully created
[root@RHEL5 /]# vgextend vg01 /dev/sdb3 #将新增的物理卷添加到vg01卷组中
Volume group "vg01" successfully extended
-----------------------------------------------------------------------
使用vgremove删除卷组
[root@RHEL5 /]# vgremove /dev/vg01
Volume group "vg01" successfully removed
-----------------------------------------------------------------------
四、逻辑卷(LV)创建及管理具体操作步骤:
1)创建逻辑卷大小为6G卷名为data,从vg01生成
[root@RHEL5 /]# lvcreate -L 6G -n data vg01 #从卷组vg01上划分6G的空间为逻辑卷data
Logical volume "data" created
2)对划分的逻辑卷进行格式化
[root@RHEL5 /]# 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.
备注:也可通过 [root@RHEL5 /]# mkfs.ext3 /dev/vg01/data 格式化
3)使用lvsacn查看逻辑卷的信息
[root@RHEL5 /]# lvscan #查看逻辑卷的信息
ACTIVE '/dev/vg01/data' [6.00 GB] inherit
4)使用lvdisplay查看逻辑卷的具体参数:
[root@RHEL5 /]# lvdisplay #查看逻辑卷的具体参数
--- Logical volume ---
LV Name /dev/vg01/data
VG Name vg01
LV UUID Lo10Tf-nnuM-7c3P-oYws-YbQI-t2oK-b0NsSx
LV Write Access read/write
LV Status available
# open 0
LV Size 6.00 GB
Current LE 1536
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
备注:也可使用 [root@RHEL5 /]# lvdisplay -v /dev/vg01/data 查看某一逻辑卷详细参数
5)使用lvextend增大逻辑卷大小,在线扩容
[root@RHEL5 /]# 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命令更新系统识别的文件系统大小,立即生效
[root@RHEL5 /]# 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减小逻辑卷大小,必须是离线方式(即先卸载文件系统)
[root@RHEL5 /]# 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
[root@RHEL5 /]# 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
备注:缩小逻辑卷通常要先卸载文件系统,并且缩小后空间容量必须大于等于文件当前占用的容量,若操作不当,会导致数据丢失,须谨慎。
[root@RHEL5 /]# 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
备注:
--------------------------------------------------------------------
删除逻辑卷
[root@RHEL5 /]#lvremove /dev/vg01/data
--------------------------------------------------------------------
五、挂载逻辑卷
1)将逻辑卷挂载到/quota目录
[root@RHEL5 /]# mount /dev/vg01/data /data2/ #将逻辑卷挂载到/quota
[root@RHEL5 /]# df -hT
/dev/sda2 ext3 7.7G 4.6G 2.8G 63% /
/dev/sda5 ext3 20G 6.7G 12G 37% /data
/dev/sda1 ext3 99M 12M 82M 13% /boot
tmpfs tmpfs 501M 0 501M 0% /dev/shm
/dev/mapper/vg01-data
ext3 6.0G 141M 5.5G 3% /data2
2)设置开机自动挂载
[root@RHEL5 /]#vi /etc/fstab #设置开机自动挂载
LABEL=/ / ext3 defaults 1 1
LABEL=/data /data ext3 defaults 1 2
LABEL=/boot /boot ext3 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
LABEL=SWAP-sda3 swap swap defaults 0 0
/dev/vg01/data /data2 ext3 defaults 0 0
七、当有一天实体磁盘的分区出现了故障,必须要更换硬盘时,该如何处理?LVM提供了pvmove工具,可以将一个物理卷上的数据转移到另外一个物理卷上。
1)将新的物理卷加入到卷组中
[root@RHEL5 /]# pvcreate /dev/sdc1 #将linux分区转换为物理卷
Physical volume "/dev/sdc1" successfully created
[root@RHEL5 /]# vgextend vg01 /dev/sdc1 #将新增的物理卷添加到vg01卷组中
Volume group "vg01" successfully extended
2)移动物理卷上的数据到新加入的物理卷上
[root@RHEL5 ~]# 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)将旧的物理卷从卷组中卸载
[root@RHEL5 ~]# vgreduce vg01 /dev/sdb1 #使用vgreduce将/dev/sdb1从卷组vg01中分离
Removed "/dev/sdb1" from volume group "vg01"
[root@RHEL5 ~]# pvremove /dev/sdb1 #最后要能够取下物理磁盘维修,则需要删除此物理卷;若物理磁盘划分成了多个物理卷,则需要将所有物理卷都删除才可以。
Labels on physical volume "/dev/sdb1" successfully wiped
十、逻辑卷创建及删除推荐步骤
创建逻辑卷(LV)的顺序:Linux分区---物理卷(PV)---卷组(VG)---逻辑卷(LV)---挂载到文件系统
删除逻辑卷(LV)的顺序:卸载文件系统----逻辑卷(LV)---卷组(VG)----物理卷(PV)---Linux分区