在系统运维和服务器管理过程中,经常遇到服务器磁盘容量不足,需要在线扩容的情况。普通磁盘分区的管理方式在逻辑分区划好之后就无法改变其大小。而LVM可以实现Linux服务器下面磁盘空间的在线扩容和动态管理,相对于普通的磁盘分区有很大的灵活性。
一、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是相等的,并且一一对应。
三、通过LVM分区方式安装Centos
1.选择安装类型,选择最后一个复选框,即创建自定义布局。
2.首先划分/boot分区,/boot分区建议使用物理分区。
3.将剩余空间划分为一个物理卷(PV)。
4.创建一个卷组(VG)。默认卷组名为VolGroup00,修改卷组名为vg51cto_lv。
5.在VG上面创建逻辑卷(LV)。逻辑卷默认为LogVol00,LogVol01……末两位由00开始依次递增。
6.LVM分区完成。
7.df -h 查看磁盘空间。
[root@hadoop01 ~]# 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。
[root@hadoop01 ~]# vgscan Reading allphysical volumes. This may take awhile... Foundvolume group "vg51cto_lv" using metadata type lvm2
2.查看卷组(VG)包含的PV、LV信息。
[root@hadoop01 ~]# 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 OldVolumeGroupNameNew VolumeGroupName
[root@hadoop01 ~]# vgrename vg51cto_lv vg51cto Volumegroup "vg51cto_lv" successfully renamed to "vg51cto"
2)修改/etc/fstab配置文件,把vg51cto_lv修改成vg51cto。
[root@hadoop01 ~]# 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。
[root@hadoop01 ~]# 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 -h查看。
[root@hadoop01 ~]# 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开始依次递增。
[root@hadoop01 ~]# 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 逻辑卷名称
[root@hadoop01 ~]# 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 [root@hadoop01 ~]# lvrename /dev/vg51cto/LogVol00 /dev/vg51cto/lv_swap Renamed"LogVol00" to "lv_swap" in volume group "vg51cto" [root@hadoop01 ~]# lvrename /dev/vg51cto/LogVol01 /dev/vg51cto/lv_root Renamed"LogVol01" to "lv_root" in volume group "vg51cto" [root@hadoop01 ~]# lvrename /dev/vg51cto/LogVol02 /dev/vg51cto/lv_home Renamed"LogVol02" to "lv_home" in volume group "vg51cto" [root@hadoop01 ~]# lvrename /dev/vg51cto/LogVol03 /dev/vg51cto/lv_data Renamed"LogVol03" to "lv_data" in volume group "vg51cto" [root@hadoop01 ~]# lvrename /dev/vg51cto/LogVol04 /dev/vg51cto/lv_usr Renamed"LogVol04" to "lv_usr" in volume group "vg51cto"
2)修改/etc/fstab配置文件。
[root@hadoop01 ~]# 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配置文件。
[root@hadoop01 ~]# 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,依此类推。
[root@hadoop01 ~]# 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.查看物理卷详细信息。
[root@hadoop01 ~]# 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空间管理
1.LVM减少LV空间
举例:减少/data空间,从20G减少到10G。
1)卸载设备(Unmount thefilesystem)
[root@hadoop01 ~]# umount /data/
2)e2fsck命令检查文件系统(Checkthe filesystem)
[root@hadoop01 ~]# e2fsck -f/dev/mapper/vg51cto-lv_data e2fsck 1.41.12 (17-May-2010) 第一步: 检查inode,块,和大小 第二步: 检查目录结构 第3步: 检查目录连接性 Pass 4: Checking reference counts 第5步: 检查簇概要信息 /dev/mapper/vg51cto-lv_data: 11/1310720files (0.0% non-contiguous), 126289/5242880 blocks
3)resize2fs命令调整lv大小,10G表示调整后/data剩余空间。
[root@hadoop01 ~]# resize2fs/dev/mapper/vg51cto-lv_data 10G resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on/dev/mapper/vg51cto-lv_data to 2621440 (4k) blocks. The filesystem on/dev/mapper/vg51cto-lv_data is now 2621440 blocks long.
4)lvreduce命令减少文件系统大小(Resizethe filesystem),表示将/data的空间减少9G。
[root@hadoop01 ~]# lvreduce -L -9G /dev/mapper/vg51cto-lv_data WARNING: Reducing active logical volume to 11.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce lv_data?[y/n]: y Reducing logical volume lv_data to 11.00 GiB Logical volume lv_data successfully resized
5)vgs命令查看卷组剩余空间为9G。
6)mount 文件系统并查看磁盘空间剩余空间。
2.LVM增加LV空间
举例:将刚才9G空间增加到/home分区。
1)vgs命令查看是否有空闲空间可以扩容。
[root@hadoop01 ~]# vgs VG #PV #LV #SN Attr VSize VFree vg51cto 1 5 0wz--n- 59.80g 9.00g
2)df -h查看当前磁盘空间
3)lvextend命令将home分区空间增加9G。
[root@hadoop01 ~]# lvextend -L +9G -f -r /dev/mapper/vg51cto-lv_home Extending logical volume lv_home to 14.00 GiB Logical volume lv_home successfully resized resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/mapper/vg51cto-lv_homeis mounted on /home; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of/dev/mapper/vg51cto-lv_home to 3670016 (4k) blocks. The filesystem on/dev/mapper/vg51cto-lv_home is now 3670016 blocks long.
4)查看分区是否增加成功。
五、LVM 在线扩容
服务器磁盘空间不足,添加新的硬盘后,需要把空间添加当前分区当中。
linux中SCSI的第1个硬盘/dev/sda,第2个硬盘/dev/sdb依此类推。其中服务器第一块硬盘/dev/sda已经划分为LVM分区。/dev/sdb是新加的硬盘。
1) 用pvcreate /dev/sdb命令初始化分区sdb为物理卷(PV)。
[root@hadoop01 ~]# pvcreate /dev/sdb Physical volume "/dev/sdb" successfully created
2)pvdisplay命令显示物理卷的信息
[root@hadoop01 ~]# pvdisplay ---Physical volume --- PVName /dev/sda2 VGName vg51cto PVSize 59.80 GiB / not usable3.00 MiB Allocatable yes (butfull) PESize 4.00 MiB Total PE 15309 Free PE 0 Allocated PE 15309 PVUUID pKC6SA-fkfe-OAl2-scVr-tq0h-EAXD-AWpwgX "/dev/sdb" is a new physical volume of "40.00 GiB" ---NEW Physical volume --- PVName /dev/sdb VGName PVSize 40.00 GiB Allocatable NO PESize 0 Total PE 0 Free PE 0 Allocated PE 0 PVUUID dMeOLS-Sks3-8k3q-pVWp-Iuz2-0Hh2-8njjhY
3)vgextend命令将pv加入到vg,vgcreate命令为新创建vg(vgcreate vg51cto /dev/sdb),这里的vg51cto已经存在,只需要用vgextend加入即可。
[root@hadoop01 ~]# vgextend vg51cto/dev/sdb Volume group "vg51cto" successfully extended
4)用vgdisplay命令查看vg信息,看到已经有40G空闲PE。
5)lvextend命令扩展分区,将40G空间全部添加到/data分区。
直接增加40G空间不足,报下面的错误,因此添加39.9G的空间。
Insufficient free space:10240 extents needed, but only 10239 available
[root@hadoop01 ~]# lvextend -L +39.99G/dev/mapper/vg51cto-lv_data Rounding size to boundary between physical extents: 39.99 GiB Extending logical volume lv_data to 50.99 GiB Logicalvolume lv_data successfully resized
6)上一步操作只是增加了逻辑卷的大小,用resize2fs命令重新定义文件系统的大小。
[root@hadoop01 ~]# resize2fs /dev/mapper/vg51cto-lv_data resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/mapper/vg51cto-lv_datais mounted on /data; on-line resizing required old desc_blocks = 1, new_desc_blocks = 4 Performing an on-line resize of/dev/mapper/vg51cto-lv_data to 13367296 (4k) blocks. The filesystem on/dev/mapper/vg51cto-lv_data is now 13367296 blocks long.
7)查看磁盘在线扩容是否正常。
总结:
)/分区不能umount,说明/分区只能扩容,不能减少/分区的空间。
)减少分区的时候需要卸载分区,进行umount操作。
)扩容的分区的时候不需要卸载分区。
4)重命名vg,lv命名后,需要修改/etc/fstab和/etc/grub.conf引导文件。