lvm是逻辑卷管理的简称,它将一个或多个物理硬盘分区(PV)组成一个逻辑硬盘(VG)来使用,  然后从这个VG中划分出逻辑分区(LV), 以上概念是我理解的东西,可能和书上的不一样。

以下所有命令都是在CENTOS6.5上进行操作的

1:对磁盘进行分区

使用fdisk对磁盘进行分区。注意分区的格式,LVM对应的是8e, 创建完成后记得重启电脑。

Command (m for help): p

Disk /dev/vda: 85.9 GB,  bytes
heads, sectors/track, cylinders
Units = cylinders of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
Disk identifier: 0x00035557 Device Boot Start End Blocks Id System
/dev/vda1 * Linux
Partition does not end on cylinder boundary.
/dev/vda2 8e Linux LVM
Partition does not end on cylinder boundary.
/dev/vda3 8e Linux LVM

2: 创建PV

pvcreate使用磁盘分区来创建PV

[root@centos-vm- ~]# pvcreate  /dev/vda3
Physical volume "/dev/vda3" successfully created
[root@centos-vm- ~]# pvdisplay
--- Physical volume ---
PV Name /dev/vda2
VG Name vg_centos6
PV Size 19.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE
Free PE
Allocated PE
PV UUID YzeBUf-Pxvv-5fct-I5Mz-6NDg-fIsI-Zjv7RT "/dev/vda3" is a new physical volume of "60.00 GiB"
--- NEW Physical volume ---
PV Name /dev/vda3
VG Name
PV Size 60.00 GiB
Allocatable NO
PE Size
Total PE
Free PE
Allocated PE
PV UUID 5B99Bs-waNc-QSnO-YWu5-XdMH-xRB6-epWFzJ

3:创建VG

如果已经有VG的话那么不需要创建新的VG,直接把PV添加到VG中就可以了。

[root@centos-vm- ~]# vgdisplay
--- Volume group ---
VG Name vg_centos6
System ID
Format lvm2
Metadata Areas
Metadata Sequence No
VG Access read/write
VG Status resizable
MAX LV
Cur LV
Open LV
Max PV
Cur PV
Act PV
VG Size 19.51 GiB
PE Size 4.00 MiB
Total PE
Alloc PE / Size / 19.51 GiB
Free PE / Size /
VG UUID wyjVpO-L0jl-ckoF-SU4K-JTNd-p3Nb-YwMgO9 [root@centos-vm- ~]# vgcreate vg_centos6_1 /dev/vda3

创建vg的使用后面可以跟多个PV(pv和磁盘分区同名)。

如果想把PV添加到已经存在的vg中使用下面的命令:

[root@centos-vm- ~]# vgextend vg_centos6 /dev/vda3
Volume group "vg_centos6" successfully extended

3:创建LV

显示当前的lv

[root@centos-vm- ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vg_centos6/lv_root
LV Name lv_root
VG Name vg_centos6
LV UUID 4QMRdu-2JYb-a7BM-OSw9-nRrz-nGmV-Z80yeO
LV Write Access read/write
LV Creation host, time centos6., -- :: +
LV Status available
# open
LV Size 17.54 GiB
Current LE
Segments
Allocation inherit
Read ahead sectors auto
- currently set to
Block device : --- Logical volume ---
LV Path /dev/vg_centos6/lv_swap
LV Name lv_swap
VG Name vg_centos6
LV UUID 8awkxq-UpsI-mPml-GTrx-tWDu-MkhI-tA3OhS
LV Write Access read/write
LV Creation host, time centos6., -- :: +
LV Status available
# open
LV Size 1.97 GiB
Current LE
Segments
Allocation inherit
Read ahead sectors auto
- currently set to
Block device :

创建一个新的LV

 lvcreate -L  10G -n data  vg_centos6

-L指定分区的大小,-n指定分区名,vg_centos6是vg名字

扩展LV,有时不想添加新的分区,那么可以扩展该LV

[root@centos-vm- ~]# lvextend -L +18G   /dev/vg_centos6/lv_root
Extending logical volume lv_root to 77.00 GiB
Logical volume lv_root successfully resized
[root@centos-vm- ~]# resize2fs /dev/vg_centos6/lv_root
resize2fs 1.41. (-May-)
Filesystem at /dev/vg_centos6/lv_root is mounted on /; on-line resizing required
old desc_blocks = , new_desc_blocks =
Performing an on-line resize of /dev/vg_centos6/lv_root to (4k) blocks.
The filesystem on /dev/vg_centos6/lv_root is now blocks long. [root@centos-vm- ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_centos6-lv_root 76G .9G 71G % /
tmpfs 940M 72K 940M % /dev/shm
/dev/vda1 485M 40M 421M % /boot

注意-L的参数是 +18G,在扩展LV的容量后需要resize2fs是容量生效

4: 格式化文件系统

如果生成了新的LV,那么需要对分区进行文件系统格式化

mkfs.ext4   /dev/vg_centos6/lv_root

然后mout,最后修改/etc/fstab.

05-11 21:54