分类: LINUX
2013-05-18 15:43:06
实验环境
VMware? Workstation 8.0
Centos 6 x64
虚拟磁盘2块(容量各20G,其中一块安装centos6 x64)
一、新建物理卷,卷组,逻辑卷
1. 创建物理分区
划分磁盘分区,指定分区类型为Linux LVM,对应的ID为8e
[root@localhost ~]# fdisk /dev/sdb
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): n
Command action
e extended
p primary partition (1-4)
e
Partition number (1-4): 1
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610):
Using default value 2610
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa8e4670a
Device Boot Start End Blocks Id System
/dev/sdb1 1 2610 20964793+ 5 Extended
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +10G
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa8e4670a
Device Boot Start End Blocks Id System
/dev/sdb1 1 2610 20964793+ 5 Extended
/dev/sdb5 1 1306 10490382 83 Linux
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (1307-2610, default 1307):
Using default value 1307
Last cylinder, +cylinders or +size{K,M,G} (1307-2610, default 2610): +10G
Value out of range.
Last cylinder, +cylinders or +size{K,M,G} (1307-2610, default 2610):
Using default value 2610
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa8e4670a
Device Boot Start End Blocks Id System
/dev/sdb1 1 2610 20964793+ 5 Extended
/dev/sdb5 1 1306 10490382 83 Linux
/dev/sdb6 1307 2610 10474348+ 83 Linux
Command (m for help): t #修改分区类型ID为8e (Linux LVM)
Partition number (1-6): 5
Hex code (type L to list codes): 8e
Changed system type of partition 5 to 8e (Linux LVM)
Command (m for help): t
Partition number (1-6): 6
Hex code (type L to list codes): 8e
Changed system type of partition 6 to 8e (Linux LVM)
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa8e4670a
Device Boot Start End Blocks Id System
/dev/sdb1 1 2610 20964793+ 5 Extended
/dev/sdb5 1 1306 10490382 8e Linux LVM
/dev/sdb6 1307 2610 10474348+ 8e Linux LVM
2. 创建物理卷(PV)
将前面划分的所有磁盘分区创建为物理卷,如下:
[root@localhost ~]# pvcreate /dev/sdb5 /dev/sdb6
Physical volume "/dev/sdb5" successfully created
Physical volume "/dev/sdb6" successfully created
3. 创建卷组(VG)
创建卷组vg01,指定由磁盘分区sdb5,sdb6组成,如下:
[root@localhost ~]# vgcreate vg01 /dev/sdb5 /dev/sdb6
Volume group "vg01" successfully created
4. 激活卷组
通过vgchange命令激活卷组,无需重启系统
[root@localhost ~]# vgchange -a y vg01
0 logical volume(s) in volume group "vg01" now active
5. 查看卷组,物理卷属性信息
5.1 查看物理卷,如下:
[root@localhost ~]# pvdisplay /dev/sdb5
--- Physical volume ---
PV Name /dev/sdb5
VG Name vg01
PV Size 10.00 GiB / not usable 4.51 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 2560
Free PE 2560
Allocated PE 0
PV UUID 5QhkU0-fNAC-ULCi-clPa-JWCn-Lf0M-IG8gvQ
5.2 查看卷组,如下
[root@localhost ~]# vgdisplay vg01
--- 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.98 GiB
PE Size 4.00 MiB
Total PE 5116
Alloc PE / Size 0 / 0
Free PE / Size 5116 / 19.98 GiB
VG UUID mPWllt-cSUs-Rrhf-w2oP-Xk9J-3NJb-DYK2EY
6. 创建逻辑卷
6.1. 新建逻辑卷
[root@localhost ~]# lvcreate -L 5G -n lv001 vg01
Logical volume "lv001" created
[root@localhost ~]# vgdisplay vg01
--- Volume group ---
VG Name vg01
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 19.98 GiB
PE Size 4.00 MiB
Total PE 5116
Alloc PE / Size 1280 / 5.00 GiB
Free PE / Size 3836 / 14.98 GiB
VG UUID mPWllt-cSUs-Rrhf-w2oP-Xk9J-3NJb-DYK2EY
[root@localhost ~]# lvcreate -l 1280 -n lv002 vg01
Logical volume "lv002" created
[root@localhost ~]# lvcreate -l 1280 -n lv003 vg01
Logical volume "lv003" created
[root@localhost ~]# lvcreate -l 1276 -n lv004 vg01
Logical volume "lv004" created
6.2. 检查vg01剩余空间,如下:
[root@localhost ~]# vgdisplay vg01 | grep "Free PE"
Free PE / Size 0 / 0 #无剩余空间
6.3. 显示卷组vg01的逻辑卷,共有4个逻辑卷
[root@localhost ~]# lvdisplay vg01
6.4.格式化逻辑卷,创建文件系统
[root@localhost ~]# mkfs.ext4 /dev/vg01/lv001
[root@localhost ~]# mkfs.ext4 /dev/vg01/lv002
[root@localhost ~]# mkfs.ext4 /dev/vg01/lv003
[root@localhost ~]# mkfs.ext4 /dev/vg01/lv004
6.5. 建立挂载目录,挂载逻辑卷
[root@localhost ~]# mkdir /lv001
[root@localhost ~]# mkdir /lv002
[root@localhost ~]# mkdir /lv003
[root@localhost ~]# mkdir /lv004
[root@localhost ~]# mount /dev/vg01/lv001 /lv001
[root@localhost ~]# mount /dev/vg01/lv002 /lv002
[root@localhost ~]# mount /dev/vg01/lv003 /lv003
[root@localhost ~]# mount /dev/vg01/lv004 /lv004
二、添加新的物理卷到卷组
系统增加1块硬盘/dev/sdb,大小为20G,已经用fdisk进行了磁盘分区,如下:
[root@localhost /]# fdisk -l /dev/sdb
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa8e4670a
Device Boot Start End Blocks Id System
/dev/sdb1 1 2610 20964793+ 5 Extended
/dev/sdb5 1 1306 10490382 8e Linux LVM
/dev/sdb6 1307 2610 10474348+ 8e Linux LVM
1.创建物理卷
[root@localhost /]# pvcreate /dev/sdb5
Physical volume "/dev/sdb5" successfully created
[root@localhost /]# pvcreate /dev/sdb6
Physical volume "/dev/sdb6" successfully created
2. 查看本机的VG
[root@localhost /]# vgdisplay
--- Volume group ---
VG Name vg_hetesthost1
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.90 GiB
PE Size 4.00 MiB
Total PE 5094
Alloc PE / Size 5094 / 19.90 GiB
Free PE / Size 0 / 0
VG UUID uVQiQ9-OKh1-TUZT-S42W-lwc7-TJB4-fd45A0
3. 将新增的物理卷加到卷组vg_hetesthost1中
[root@localhost /]# vgextend vg_hetesthost1 /dev/sdb5
Volume group "vg_hetesthost1" successfully extended
[root@localhost /]# vgextend vg_hetesthost1 /dev/sdb6
Volume group "vg_hetesthost1" successfully extended
4. 检查是否成功加入到卷组vg_hetesthost1
[root@localhost /]# vgdisplay vg_hetesthost1 | grep "Free PE"
Free PE / Size 5116 / 19.98 GiB #已增加
5. 修改扩充逻辑卷的大小
LVM的功能就是能动态地调整分区的大小,修改逻辑卷需要用到的命令有lvextend(扩展逻辑卷),lvreduce(缩减逻辑卷),
5.1. 查看本机的分区信息
[root@localhost /]# df -h
文件系统 容量 已用 可用 已用%% 挂载点
/dev/mapper/vg_hetesthost1-LogVol02
8.1G 3.3G 4.4G 44% /
tmpfs 497M 0 497M 0% /dev/shm
/dev/sda1 97M 25M 68M 27% /boot
/dev/mapper/vg_hetesthost1-LogVol01
9.7G 4.5G 4.7G 49% /data
5.2. 扩充逻辑卷
本例扩充逻辑卷vg_hetesthost1-LogVol01(/data分区目录)
如下:
[root@localhost /]# lvextend -L +8G /dev/mapper/vg_hetesthost1-LogVol01
Extending logical volume LogVol01 to 17.77 GiB
Logical volume LogVol01 successfully resized
5.3. 刷新逻辑卷大小
刷新/dev/mapper/vg_hetesthost1-LogVol01的大小,不用umount哦,这里用resize2fs命令,
[root@localhost /]# resize2fs /dev/mapper/vg_hetesthost1-LogVol01
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg_hetesthost1-LogVol01 is mounted on /data; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/mapper/vg_hetesthost1-LogVol01 to 4657152 (4k) blocks.
The filesystem on /dev/mapper/vg_hetesthost1-LogVol01 is now 4657152 blocks long.
5.4. 查看逻辑卷大小
检查/dev/mapper/vg_hetesthost1-LogVol01大小,容量18G,扩展成功
[root@localhost /]# df -h
文件系统 容量 已用 可用 已用%% 挂载点
/dev/mapper/vg_hetesthost1-LogVol02
8.1G 3.3G 4.4G 44% /
tmpfs 497M 0 497M 0% /dev/shm
/dev/sda1 97M 25M 68M 27% /boot
/dev/mapper/vg_hetesthost1-LogVol01
18G 4.5G 13G 27% /data
三、删除逻辑卷与卷组
1. 卸载已经挂载的逻辑卷分区
[root@localhost /]# umount /lv001
[root@localhost /]# umount /lv002
[root@localhost /]# umount /lv003
[root@localhost /]# umount /lv004
2. 删除逻辑卷
[root@localhost /]# lvremove /dev/vg01/lv001
Do you really want to remove active logical volume lv001? [y/n]: y
Logical volume "lv001" successfully removed
[root@localhost /]# lvremove /dev/vg01/lv002
Do you really want to remove active logical volume lv002? [y/n]: y
Logical volume "lv002" successfully removed
[root@localhost /]# lvremove /dev/vg01/lv003
Do you really want to remove active logical volume lv003? [y/n]: y
Logical volume "lv003" successfully removed
[root@localhost /]# lvremove /dev/vg01/lv004
Do you really want to remove active logical volume lv004? [y/n]: y
Logical volume "lv004" successfully removed
3. 删除卷组
[root@localhost /]# vgreduce vg01 /dev/sdb5
Removed "/dev/sdb5" from volume group "vg01"
[root@he-test-host1 ~]# vgreduce vg01 /dev/sdb6
Can't remove final physical volume "/dev/sdb6" from volume group "vg01" #error, it’s the only PV in the VG
[root@localhost /]# vgremove -v vg01
Using volume group(s) on command line
Finding volume group "vg01"
Archiving volume group "vg01" metadata (seqno 10).
Removing physical volume "/dev/sdb6" from volume group "vg01"
Volume group "vg01" successfully removed
4. 删除物理卷
[root@localhost /]# pvremove /dev/sdb5
Labels on physical volume "/dev/sdb5" successfully wiped
[root@localhost /]# pvremove /dev/sdb6
Labels on physical volume "/dev/sdb6" successfully wiped