lvm是逻辑卷管理的简称,它将一个或多个物理硬盘分区(PV)组成一个逻辑硬盘(VG)来使用, 然后从这个VG中划分出逻辑分区(LV), 以上概念是我理解的东西,可能和书上的不一样。
以下所有命令都是在CENTOS6.5上进行操作的
1:对磁盘进行分区
使用fdisk对磁盘进行分区。注意分区的格式,LVM对应的是8e, 创建完成后记得重启电脑。
Command (m for help): p Disk /dev/vda: 85.9 GB, 85899345920 bytes 16 heads, 63 sectors/track, 166440 cylinders Units = cylinders of 1008 * 512 = 516096 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00035557 Device Boot Start End Blocks Id System /dev/vda1 * 3 1018 512000 83 Linux Partition 1 does not end on cylinder boundary. /dev/vda2 1018 41611 20458496 8e Linux LVM Partition 2 does not end on cylinder boundary. /dev/vda3 41611 166440 62914240 8e Linux LVM
2: 创建PV
pvcreate使用磁盘分区来创建PV
[root@centos-vm-73 ~]# pvcreate /dev/vda3 Physical volume "/dev/vda3" successfully created [root@centos-vm-73 ~]# 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 4994 Free PE 0 Allocated PE 4994 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 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID 5B99Bs-waNc-QSnO-YWu5-XdMH-xRB6-epWFzJ
3:创建VG
如果已经有VG的话那么不需要创建新的VG,直接把PV添加到VG中就可以了。
[root@centos-vm-73 ~]# vgdisplay --- Volume group --- VG Name vg_centos6 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 19.51 GiB PE Size 4.00 MiB Total PE 4994 Alloc PE / Size 4994 / 19.51 GiB Free PE / Size 0 / 0 VG UUID wyjVpO-L0jl-ckoF-SU4K-JTNd-p3Nb-YwMgO9 [root@centos-vm-73 ~]# vgcreate vg_centos6_1 /dev/vda3
创建vg的使用后面可以跟多个PV(pv和磁盘分区同名)。
如果想把PV添加到已经存在的vg中使用下面的命令:
[root@centos-vm-73 ~]# vgextend vg_centos6 /dev/vda3 Volume group "vg_centos6" successfully extended
3:创建LV
显示当前的lv
[root@centos-vm-73 ~]# 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.5, 2014-06-11 12:13:40 +0800 LV Status available # open 1 LV Size 17.54 GiB Current LE 4490 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 --- 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.5, 2014-06-11 12:13:43 +0800 LV Status available # open 1 LV Size 1.97 GiB Current LE 504 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:1
创建一个新的LV
lvcreate -L 10G -n data vg_centos6
-L指定分区的大小,-n指定分区名,vg_centos6是vg名字
扩展LV,有时不想添加新的分区,那么可以扩展该LV
[root@centos-vm-73 ~]# 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-73 ~]# resize2fs /dev/vg_centos6/lv_root resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/vg_centos6/lv_root is mounted on /; on-line resizing required old desc_blocks = 4, new_desc_blocks = 5 Performing an on-line resize of /dev/vg_centos6/lv_root to 20185088 (4k) blocks. The filesystem on /dev/vg_centos6/lv_root is now 20185088 blocks long. [root@centos-vm-73 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_centos6-lv_root 76G 4.9G 71G 7% / tmpfs 940M 72K 940M 1% /dev/shm /dev/vda1 485M 40M 421M 9% /boot
注意-L的参数是 +18G,在扩展LV的容量后需要resize2fs是容量生效
4: 格式化文件系统
如果生成了新的LV,那么需要对分区进行文件系统格式化
mkfs.ext4 /dev/vg_centos6/lv_root
然后mout,最后修改/etc/fstab.