全部博文(535)
分类: LINUX
2012-08-20 09:33:13
Linux LVM逻辑卷管理技术
在使用LVM管理Linux的磁盘时,主要分为以下几个步骤:
1)在磁盘上创建LVM分区
2)在LVM分区上创建物理卷PV
3)在物理卷PV上创建卷组VG
4)在卷组VG上创建逻辑卷LV
5)为逻辑卷LV创建文件系统
下面将具体讲解各个步骤:
1)在磁盘上创建LVM分区
[root@ ~]# fdisk /dev/sdb1//将硬盘的sdb1分区创建为LVM分区,也可将整个硬盘创建为LVM分区,例如可替换为sdb
这里要注意选择创建LVM分区(8e)
如下
一、添加磁盘
本例中我们会添加两个200M的主分区,其它为扩展分区,在扩展分区中我们添加两个200M大小的逻辑分区;
Command (m for help): p 注:列出分区表;
Disk /dev/sda: 1035
MB, 1035730944 bytes
256 heads, 63 sectors/track, 125 cylinders
Units = cylinders of 16128 * 512 = 8257536 bytes
Device Boot Start End Blocks Id System
Command (m for
help): n 注:添加分区;
Command action
e extended
p primary partition (1-4)
p 注:添加主分区;
Partition number (1-4): 1 注:添加主分区1;
First cylinder (1-125, default 1): 注:直接回车,主分区1的起始位置;默认为1,默认就好;
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-125, default 125):
+200M 注:指定分区大小,用+200M来指定大小为200M
Command (m for
help): n 注:添加新分区;
Command action
e extended
p primary partition (1-4)
p 注:添加主分区
Partition number (1-4): 2 注:添加主分区2;
First cylinder (26-125, default 26):
Using default value 26
Last cylinder or +size or +sizeM or +sizeK (26-125, default 125): +200M 注:指定分区大小,用+200M来指定大小为200M
Command (m for
help): n
Command action
e extended
p primary partition (1-4)
e 注:添加扩展分区;
Partition number (1-4): 3 注:指定为3 ,因为主分区已经分了两个了,这个也算主分区,从3开始;
First cylinder (51-125, default 51): 注:直接回车;
Using default value 51
Last cylinder or +size or +sizeM or +sizeK (51-125, default 125): 注:直接回车,把其余的所有空间都给扩展分区;
Using default value 125
Command (m for help): p
Disk /dev/sda: 1035
MB, 1035730944 bytes
256 heads, 63 sectors/track, 125 cylinders
Units = cylinders of 16128 * 512 = 8257536 bytes
Device
Boot
Start End
Blocks Id System
/dev/sda1
1
25 201568+ 83 Linux
/dev/sda2
26
50 201600 83 Linux
/dev/sda3
51
125 604800 5 Extended
Command (m for
help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l 注:添加逻辑分区;
First cylinder (51-125, default 51):
Using default value 51
Last cylinder or +size or +sizeM or +sizeK (51-125, default 125): +200M 注:添加一个大小为200M大小的分区;
Command (m for
help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l 注:添加一个逻辑分区;
First cylinder (76-125, default 76):
Using default value 76
Last cylinder or +size or +sizeM or +sizeK (76-125, default 125): +200M 注:添加一个大小为200M大小的分区;
Command (m for help): p 列出分区表;
Disk /dev/sda: 1035
MB, 1035730944 bytes
256 heads, 63 sectors/track, 125 cylinders
Units = cylinders of 16128 * 512 = 8257536 bytes
Device
Boot
Start
End Blocks Id System
/dev/sda1
1
25 201568+ 83 Linux
/dev/sda2
26
50 201600 83 Linux
/dev/sda3
51
125 604800 5 Extended
/dev/sda5
51
75 201568+ 83 Linux
/dev/sda6
76
100 201568+ 83 Linux
然后我们根据前面所说通过t指令来改变分区类型; 最后不要忘记w保存退出;
2)在LVM分区上创建物理卷PV
[root@ ~]# pvcreate /dev/sdb1
[root@localhost ~]# pvcreate /dev/sdc1
Physical volume "/dev/sdc1" successfully created
[root@localhost ~]#
以上语句是将整个LVM分区创建为物理卷,也可以创建指定大小的物理卷,用如下语句
[root@ ~]# pvcreate --setphysicalvolumesize 40G /dev/sdb1
pvcreate –s 40G /dev/sdb1
[root@ ~]# pvresize --setphysicalvolumesize 20G /dev/sdb1 更改pv大小
或是pvresize --s 400M /dev/sdb1 更改pv大小
关于物理卷管理方面的命令主要有以下常用的几个,其它不常用的可以自己去查询:
[root@ ~]# pvdisplay /dev/sdb1 //列出物理卷的相关信息
[root@localhost ~]# pvdisplay #列出所有的pv
--- Physical volume ---
PV Name /dev/sda2
VG Name VolGroup00
PV Size 27.90 GB / not usable 24.72 MB
Allocatable yes (but full)
PE Size (KByte) 32768
Total PE 892
Free PE 0
Allocated PE 892
PV UUID rvH05M-yzPp-8N7c-g2Hi-HA96-cvwA-Hlbikp
"/dev/sdc1" is a new physical volume of "792.27 MB"
--- NEW Physical volume ---
PV Name /dev/sdc1
VG Name
PV Size 792.27 MB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID qz3FEY-VKUL-tL4k-0RW6-IChA-d7jB-SqJFdL
[root@localhost ~]# pvresize --setphysicalvolumesize 700M /dev/sdc1 //改物理卷的大小
Physical volume "/dev/sdc1" changed
1 physical volume(s) resized / 0 physical volume(s) not resized //改物理卷的大小
再用pvdisplay 可以显示大小的
[root@ ~]# pvscan /dev/sdb1 //扫描磁盘的物理卷
[root@localhost ~]# pvscan /dev/sdb
PV /dev/sda2 VG VolGroup00 lvm2 [27.88 GB / 0 free]
PV /dev/sdc1 lvm2 [700.00 MB]
Total: 2 [28.56 GB] / in use: 1 [27.88 GB] / in no VG: 1 [700.00 MB]
[root@localhost ~]# //扫描磁盘的物理卷
[root@localhost ~]# pvcreate --setphysicalvolumesize 40M /dev/sdc1
Physical volume "/dev/sdc1" successfully created
#创建一个物理卷/dev/sdc1大小是40M
[root@ ~]# pvremove /dev/sdb1 //删除物理卷
3)在物理卷PV上创建卷组VG
[root@ ~]# vgcreate VG /dev/sdb1
也可以在多个物理卷上创建一个卷组,用如下命令
[root@ ~]# vgcreate VG /dev/sdb1 /dev/sda1
[root@localhost ~]# vgcreate datavg2 /dev/sdc1 /dev/sdc5
Volume group "datavg2" successfully created
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name datavg2
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 72.00 MB
PE Size 4.00 MB
Total PE 18
Alloc PE / Size 0 / 0
Free PE / Size 18 / 72.00 MB
VG UUID KAfvdE-XmpQ-izDv-TmG1-FrJ7-JfRW-tS21E8
--- 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 27.88 GB
PE Size 32.00 MB
Total PE 892
Alloc PE / Size 892 / 27.88 GB
Free PE / Size 0 / 0
VG UUID zZU0z1-u4NZ-0Lo9-S7Gm-Ruh0-A9kv-1dpfDT
用于管理卷组的常用命令有如下几个:
[root@ ~]# vgdisplay //显示卷组的相关信息
[root@ ~]# vgscan //扫描卷组
fdisk -l 显示磁盘和分区
[root@localhost ~]# vgextend datavg2 /dev/sdc6
No physical volume label read from /dev/sdc6
Physical volume "/dev/sdc6" successfully created
Volume group "datavg2" successfully extended
[root@ ~]# vgextend VG /dev/sda2 //扩大卷组
[root@ ~]# vgreduce VG
/dev/sda2 //缩小卷组
[root@ ~]# vgremove VG //删除卷组
[root@ ~]# vgrename VG NEW_VG //改变卷组名称
[root@ ~]# vgmerge VG1 VG2 //合并卷组,后者(VG2)被前者(VG1)合并,前者(VG1)处于活动状态
[root@localhost ~]# vgmerge VolGroup00 datavg3
Extent sizes differ: 65536 (VolGroup00) and 8192 (datavg3)
[root@ ~]# vgchange -a y VG //激活卷组,对卷组进行某个操作后,可用该命令使其立即生效,不用重启电脑
[root@ ~]# vgchange -a n VG //使卷组处于非活动状态,该命令可用于删除命令时,即先使卷组处于非活动状态,然后删除
4)在卷组VG上创建逻辑卷LV
[root@localhost ~]# lvcreate -noracle_lv -L40M datavg3
Logical volume "oracle_lv" created
[root@ ~]# lvcreate -ncentos6
-L20G /dev/VG
以上命令在VG卷组上创建了一个名为centos6大小为20G的逻辑卷
–j /dev/myvg/mylv
用于逻辑卷管理的常用命令有如下几个:
[root@ ~]# lvdisplay //列出所有逻辑卷的信息
[root@ ~]# lvdisplay /dev/VG/centos6 //列出centos6逻辑卷的信息
[root@localhost ~]# lvdisplay /dev/datavg3/oracle_lv
[root@ ~]# lvscan //扫描逻辑卷
[root@ ~]# lvrename
/dev/VG/centos6 /dev/VG/c6 //将逻辑卷centos6更名为c6
[root@localhost ~]# lvrename /dev/datavg3/oracle_lv /dev/datavg3/oracle_lv2
Renamed "oracle_lv" to "oracle_lv2" in volume group "datavg3"
[root@localhost ~]#
[root@ ~]# lvextend -L 30G
/dev/VG/centos6 //将逻辑卷centos6扩大为30G,即增加了10G
[root@ ~]# lvreduce -L 15G /dev/VG/centos6 //将逻辑卷centos6缩小为15G,即缩小了5G
[root@localhost ~]# lvreduce -L 20M /dev/datavg3/oracle_lv2
WARNING: Reducing active logical volume to 20.00 MB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce oracle_lv2? [y/n]: y
Reducing logical volume oracle_lv2 to 20.00 MB
Logical volume oracle_lv2 successfully resized
[root@ ~]# lvextend -L +20G /dev/VG/centos6 //将逻辑卷centos6增加20G;原先为20G,又增加20G,总大小为40G
umount /dev/VG/centos6 取消挂载
e2fsck -f /dev/VG/centos6 //调整大小前要做的步骤
[root@ ~]# lvreduce -L -10G /dev/VG/centos6 //将逻辑卷centos6缩小10G;原先为20G,缩小10G,即缩小后大小为10G
resize2fs /dev/VG/centos6 //调整大小前要做的步骤
[root@ ~]# lvremove /dev/VG/centos6 //删除逻辑卷centos6
5)为逻辑卷LV创建文件系统
[root@ ~]# mkfs -t ext4
/dev/VG/centos6
[root@localhost ~]# mkfs -t ext3 /dev/datavg3/oracle_lv2
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
8192 inodes, 32768 blocks
1638 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33554432
4 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 32 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]#
mkdir /oracle_lv2 建一个挂载点
mount -t ext3 /dev/datavg3/oracle_lv2 /oracle_lv2/ 把文件系统挂载起来
df –h 命令就可 用看到了
如果想开机时自动挂载,就把这个命令写到/etc/rc.d/rc.local
echo “mount -t ext3 /dev/datavg3/oracle_lv2 /oracle_lv2/ “ >> /etc/rc.d/rc.local
或是用以下命令
[root@ ~]# mkfs.ext4
/dev/VG/centos6
这个只能在 2,6.30 以上内核上才支持
[root@localhost ~]# mkfs.ext3 /dev/datavg3/oracle_lv2
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
8192 inodes, 32768 blocks
1638 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33554432
4 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 32 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
一:建立物理卷
# /dev/sda
//在此我们创建一个3G的分区和一个7G的分区,当然大小可根据自己需要来设定,
另,分区的号一定要改为8e(LVM分区)
#pvcreate /dev/sda5 /dev/sda6
//将我们新建的2个分区创建为物理卷
#vgcreate –s 8M myvg /dev/sda5 /dev/sda6
//将2个物理卷创建为叫myvg的卷组 –s为指定pe大小
#lvcreate –L 5G –n mylv myvg
//创建一个5G大小的逻辑卷 -L指定逻辑卷大小,-n指定逻辑卷名字
#lvdisplay
//查看当前系统上的逻辑卷
# –j /dev/myvg/mylv
// 将其格式化t3的文件系统,此时就可正常使用了
# /data
#mount /dev/myvg/mylv /dat
//挂载到data目录,查看其内容,如有lost+found目录,此分区即可使用
二:扩展逻辑卷
#lvextend –L +2G /dev/myvg/mylv
//此为扩展逻辑卷的大小, -L +2G 表示增加2G 如没有+号 表示扩展到多大
#2fs /dev/myvg/mylv
//此为扩展文件系统的大小,此步结束才能正常使用扩展后的逻辑卷
三:缩减逻辑卷
# /dev/myvg/mylv
#e2 –f /dev/myvg/mylv
//强行检测文件系统,确保文件不会丢失
#resize2fs /dev/myvg/mylv 3G
//缩减逻辑卷为3G
#lvrece –L 3G /dev/myvg/mylv
#mount /dev/myvg/mylv /data
//挂载,查看文件系统
四:创建快照
#lvcreate –L 128M –s –p r –n snap_mylv /dev/myvg/mylv
// -L指定快照卷大小(快照卷只保存,数据变化的内容,所以不必很大)
-s指定为快照卷
–p指定快照卷的权限,r为只读
–n指定快照卷的名字
#mkdir /back
#mount /dev/myvg/sanp_mylv /back
//下面可查看目录/back的内容,应和/data目录中的内容是一致的
五:备份快照内容(做快照的重要功能就是以实现数据的备份)
# /back
#tar jcf /tmp/ccle.tar.bz2 *
//将当前目录所有内容备份/tmp/ccle.tar.bz2
到此物理卷的创建及备份都已结束,具体的操作根据自己的实际情况处理即可。