在系统运维和服务器管理过程中,经常遇到服务器磁盘容量不足,需要在线扩容的情况。普通磁盘分区的管理方式在逻辑分区划好之后就无法改变其大小。而LVM可以实现Linux服务器下面磁盘空间的在线扩容和动态管理,相对于普通的磁盘分区有很大的灵活性。
一、LVM简介
LVM是 Logical VolumeManager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制,它由Heinz Mauelshagen在Linux 2.4内核上实现。LVM将一个或多个硬盘的分区在逻辑上进行组合,做为一个大的硬盘空间来使用,当硬盘的剩余空间不够的时候,可以将其它的硬盘加入到分区当中,这样可以实现磁盘空间的动态管理。
二、LVM基本术语
物理卷(physical volume,PV):物理卷就是指硬盘分区,也可以是整个硬盘或已创建的RAID ,是LVM的基本存储设备,与普通物理存储介质的区别是该设备包含有LVM相关的管理参数。
卷组(volume group,VG):卷组是由一个或多个物理卷所组成的存储池,在卷组上能创建一个或多个“LVM分区”(逻辑卷)。
逻辑卷(logical volume,LV):LVM的逻辑卷类似于非LVM系统中的硬盘分区,它建立在卷组之上,是一个标准的块设备,在逻辑卷之上可以建立文件系统。
物理块(physical extent,PE):物理卷以大小相等的物理块为存储的基本单位,同时也是LVM寻址的最小单元。
逻辑块(logical extent,LE):逻辑卷以大小相等的逻辑块为存储的基本单位,在同一个卷组中,LE的大小和PE是相等的,并且一一对应。
简单来说就是:PV:是物理的磁盘分区。VG:LVM中的物理的磁盘分区,也就是PV,必须加入VG,可以将VG理解为一个仓库或者是几个大的硬盘。LV:也就是从VG中划分的逻辑分区 。
三、通过LVM分区方式安装Centos
1.选择安装类型,选择最后一个复选框,即创建自定义布局。
2.首先划分/boot分区,/boot分区建议使用物理分区。
3.将剩余空间划分为一个物理卷(PV)。
4.创建一个卷组(VG)。默认卷组名为VolGroup00,修改卷组名为vg51cto-lv。
5.在VG上面创建逻辑卷(LV)。逻辑卷默认为LogVol00,LogVol01……末两位由00开始依次递增。
6.LVM分区完成。
7.df -h 查看磁盘空间。
[[email protected] ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg51cto_lv-LogVol01 9.9G 1.2G 8.2G 13% / tmpfs 495M 0 495M 0% /dev/shm /dev/sda1 194M 29M 155M 16% /boot /dev/mapper/vg51cto_lv-LogVol03 20G 172M 19G 1% /data /dev/mapper/vg51cto_lv-LogVol02 5.0G 138M 4.6G 3% /home /dev/mapper/vg51cto_lv-LogVol04 23G 173M 22G 1% /usr/local
四、LVM管理
第一部分:卷组(volume group ,VG)相关的操作。
1.查看卷组(VG)名,卷组名为vg51cto_lv。
[[email protected] ~]# vgscan Reading allphysical volumes. This may take awhile... Foundvolume group "vg51cto_lv" using metadata type lvm2
2.查看卷组(VG)包含的PV、LV信息。
[[email protected] ~]# vgdisplay -v vg51cto_lv Usingvolume group(s) on command line Findingvolume group "vg51cto_lv" --- Volumegroup --- VGName vg51cto_lv SystemID Format lvm2 MetadataAreas 1 MetadataSequence No 6 VGAccess read/write VGStatus resizable MAX LV 0 Cur LV 5 OpenLV 5 Max PV 0 Cur PV 1 Act PV 1 VGSize 59.80 GiB PESize 4.00 MiB TotalPE 15309 Alloc PE /Size 15309 / 59.80 GiB Free PE / Size 0 / 0 VGUUID uF4S1y-21AZ-KOfs-rNIF-lFmN-wQ75-2sX7F1 --- Logicalvolume --- LVPath /dev/vg51cto_lv/LogVol04 LVName LogVol04 VGName vg51cto_lv LVUUID rvOZeO-Oz2G-wx7Z-u0b6-CvXj-FKEj-RwCLXO LV WriteAccess read/write LV Creationhost, time hadoop01, 2015-11-05 17:30:51 +0800 LVStatus available # open 1 LVSize 22.80 GiB CurrentLE 5837 Segments 1 Allocation inherit Read aheadsectors auto - currentlyset to 256 Blockdevice 253:2 --- Logicalvolume --- LVPath /dev/vg51cto_lv/LogVol01 LVName LogVol01 VGName vg51cto_lv LVUUID d6YKrd-fWMJ-x61I-cfWG-v2me-Hn8V-xdm96L LV WriteAccess read/write LV Creationhost, time hadoop01, 2015-11-05 17:30:56 +0800 LVStatus available # open 1 LVSize 10.00 GiB CurrentLE 2560 Segments 1 Allocation inherit Read aheadsectors auto - currentlyset to 256 Blockdevice 253:0 --- Logicalvolume --- LVPath /dev/vg51cto_lv/LogVol00 LVName LogVol00 VGName vg51cto_lv LVUUID hSyH2r-mvMV-th7q-geKg-sVpm-O1zJ-D6DwT8 LV WriteAccess read/write LV Creationhost, time hadoop01, 2015-11-05 17:30:58 +0800 LVStatus available # open 1 LVSize 2.00 GiB CurrentLE 512 Segments 1 Allocation inherit Read aheadsectors auto - currentlyset to 256 Blockdevice 253:1 --- Logicalvolume --- LVPath /dev/vg51cto_lv/LogVol03 LVName LogVol03 VGName vg51cto_lv LVUUID gKHLfz-O5Lm-TGMq-2LcF-xcH1-ASea-QCVVNc LV WriteAccess read/write LV Creationhost, time hadoop01, 2015-11-05 17:30:58 +0800 LVStatus available # open 1 LVSize 20.00 GiB CurrentLE 5120 Segments 1 Allocation inherit Read aheadsectors auto - currentlyset to 256 Blockdevice 253:3 --- Logicalvolume --- LVPath /dev/vg51cto_lv/LogVol02 LVName LogVol02 VGName vg51cto_lv LVUUID lkqKmO-Dt0u-3CnB-UqeD-d6gc-2rmP-gWtKVp LV WriteAccess read/write LV Creationhost, time hadoop01, 2015-11-05 17:31:00 +0800 LVStatus available # open 1 LVSize 5.00 GiB CurrentLE 1280 Segments 1 Allocation inherit Read aheadsectors auto - currentlyset to 256 Blockdevice 253:4 ---Physical volumes --- PV Name /dev/sda2 PVUUID pKC6SA-fkfe-OAl2-scVr-tq0h-EAXD-AWpwgX PVStatus allocatable Total PE /Free PE 15309 / 0
3.修改卷组名,由vg51cto_lv修改成vg51cto。
1)修改卷组名的命令为vgrename,语法如下:
vgrename OldVolumeGroupNameNewVolumeGroupName
[[email protected] ~]# vgrename vg51cto_lv vg51cto Volumegroup "vg51cto_lv" successfully renamed to "vg51cto"
2)修改/etc/fstab配置文件,把vg51cto_lv修改成vg51cto。
[[email protected] ~]# vim /etc/fstab # # /etc/fstab # Created by anaconda on Thu Nov 5 17:33:46 2015 # # Accessible filesystems, by reference, aremaintained under ‘/dev/disk‘ # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info # /dev/mapper/vg51cto-LogVol01 / ext4 defaults 1 1 UUID=9c02e519-3b85-4918-b9fd-395535cab496/boot ext4 defaults 1 2 /dev/mapper/vg51cto-LogVol03 /data ext4 defaults 1 2 /dev/mapper/vg51cto-LogVol02 /home ext4 defaults 1 2 /dev/mapper/vg51cto-LogVol04 /usr/local ext4 defaults 1 2 /dev/mapper/vg51cto-LogVol00 swap swap defaults 0 0 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
3)修改grub.conf配置文件,把vg51cto_lv修改成vg51cto。
[[email protected] ~]# vim /etc/grub.conf # grub.conf generated by anaconda # # Note that you do not have to rerun grub aftermaking changes to this file # NOTICE: You have a /boot partition. Thismeans that # all kernel and initrd paths are relative to /boot/, eg. # root (hd0,0) # kernel /vmlinuz-version ro root=/dev/mapper/vg51cto_lv-LogVol01 # initrd /initrd-[generic-]version.img #boot=/dev/sda default=0 timeout=5 splashimage=(hd0,0)/grub/splash.xpm.gz hiddenmenu title CentOS (2.6.32-431.el6.x86_64) root(hd0,0) kernel/vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/mapper/vg51cto-LogVol01 rd_NO_LUKSrd_LVM_LV=vg51cto/LogVol01 rd_LVM_LV=vg51cto/LogVol00 rd_NO_MD crashkernel=autoLANG=zh_CN.UTF-8 KEYBOARDTYPE=pcKEYTABLE=us rd_NO_DM rhgb quiet initrd/initramfs-2.6.32-431.el6.x86_64.img
4)reboot重启后df查看。
[[email protected] ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg51cto-LogVol01 9.9G 1.2G 8.2G 13% / tmpfs 495M 0 495M 0% /dev/shm /dev/sda1 194M 29M 155M 16% /boot /dev/mapper/vg51cto-LogVol03 20G 172M 19G 1% /data /dev/mapper/vg51cto-LogVol02 5.0G 138M 4.6G 3% /home /dev/mapper/vg51cto-LogVol04 23G 173M 22G 1% /usr/local
第二部分:逻辑卷(logical volume,LV)相关的操作。
1.查看逻辑卷(LV)名。
使用LVM时安装系统是,默认的卷组名为VolGroup00,卷组内的逻辑卷为LogVol00,LogVol01……末两位由00开始依次递增。
[[email protected] ~]# lvscan ACTIVE ‘/dev/vg51cto/LogVol04‘ [22.80 GiB]inherit ACTIVE ‘/dev/vg51cto/LogVol01‘ [10.00 GiB]inherit ACTIVE ‘/dev/vg51cto/LogVol00‘ [2.00 GiB]inherit ACTIVE ‘/dev/vg51cto/LogVol03‘ [20.00 GiB]inherit ACTIVE ‘/dev/vg51cto/LogVol02‘ [5.00 GiB]inherit
2.查看逻辑卷详细信息
lvdisplay命令查看所有逻辑卷的详细信息
lvdisplay 逻辑卷名称
[[email protected] ~]# lvdisplay /dev/vg51cto/LogVol00 --- Logicalvolume --- LVPath /dev/vg51cto/LogVol00 LVName LogVol00 VGName vg51cto LVUUID hSyH2r-mvMV-th7q-geKg-sVpm-O1zJ-D6DwT8 LV WriteAccess read/write LV Creationhost, time hadoop01, 2015-11-05 17:30:58 +0800 LVStatus available # open 1 LVSize 2.00 GiB CurrentLE 512 Segments 1 Allocation inherit Read aheadsectors auto - currentlyset to 256 Block device 253:1
3.修改逻辑卷名
1)修改逻辑卷命令为lvrename,语法如下:
lvrename OldLogicalVolumePath NewLogicalVolumePath [[email protected] ~]# lvrename /dev/vg51cto/LogVol00 /dev/vg51cto/lv_swap Renamed"LogVol00" to "lv_swap" in volume group "vg51cto" [[email protected] ~]# lvrename /dev/vg51cto/LogVol01 /dev/vg51cto/lv_root Renamed"LogVol01" to "lv_root" in volume group "vg51cto" [[email protected] ~]# lvrename /dev/vg51cto/LogVol02 /dev/vg51cto/lv_home Renamed"LogVol02" to "lv_home" in volume group "vg51cto" [[email protected] ~]# lvrename /dev/vg51cto/LogVol03 /dev/vg51cto/lv_data Renamed"LogVol03" to "lv_data" in volume group "vg51cto" [[email protected] ~]# lvrename /dev/vg51cto/LogVol04 /dev/vg51cto/lv_usr Renamed"LogVol04" to "lv_usr" in volume group "vg51cto"
2)修改/etc/fstab配置文件。
[[email protected] ~]# vim /etc/fstab # # /etc/fstab # Created by anaconda on Thu Nov 5 17:33:46 2015 # # Accessible filesystems, by reference, aremaintained under ‘/dev/disk‘ # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info # /dev/mapper/vg51cto-lv_root / ext4 defaults 1 1 UUID=9c02e519-3b85-4918-b9fd-395535cab496/boot ext4 defaults 1 2 /dev/mapper/vg51cto-lv_data /data ext4 defaults 1 2 /dev/mapper/vg51cto-lv_home /home ext4 defaults 1 2 /dev/mapper/vg51cto-lv_usr /usr/local ext4 defaults 1 2 /dev/mapper/vg51cto-lv_swap swap swap defaults 0 0 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
3)修改grub.conf配置文件。
[[email protected] ~]# vim /etc/grub.conf # grub.conf generated by anaconda # # Note that you do not have to rerun grub aftermaking changes to this file # NOTICE: You have a /boot partition. Thismeans that # all kernel and initrd paths are relative to /boot/, eg. # root (hd0,0) # kernel /vmlinuz-version ro root=/dev/mapper/vg51cto_lv-LogVol01 # initrd /initrd-[generic-]version.img #boot=/dev/sda default=0 timeout=5 splashimage=(hd0,0)/grub/splash.xpm.gz hiddenmenu title CentOS (2.6.32-431.el6.x86_64) root(hd0,0) kernel/vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/mapper/vg51cto-lv_root rd_NO_LUKSrd_LVM_LV=vg51cto/lv_root rd_LVM_LV=vg51cto/lv_swap rd_NO_MD crashkernel=autoLANG=zh_CN.UTF-8 KEYBOARDTYPE=pc KEYTABLE=usrd_NO_DM rhgb quiet initrd/initramfs-2.6.32-431.el6.x86_64.img
第三部分:物理卷(physical volume,PV)相关操作。
1.查看物理卷(PV)名,这里为/dev/sda2,linux中SCSI的第1个硬盘/dev/sda,第2个硬盘/dev/sdb依此类推。
[[email protected] ~]# pvscan PV /dev/sda2 VG vg51cto lvm2 [59.80 GiB / 0 free] Total: 1[59.80 GiB] / in use: 1 [59.80 GiB] / in no VG: 0 [0 ]
2.查看物理卷详细信息。
[[email protected] ~]# pvdisplay ---Physical volume --- PVName /dev/sda2 VGName vg51cto PVSize 59.80 GiB / not usable 3.00 MiB Allocatable yes (butfull) PESize 4.00 MiB TotalPE 15309 FreePE 0 AllocatedPE 15309 PVUUID pKC6SA-fkfe-OAl2-scVr-tq0h-EAXD-AWpwgX
四、LVM空间管理
五、LVM 在线扩容