初识LVM及ECS上LVM分区扩容

请务必做好快照备份!!!
请务必做好快照备份!!!
请务必做好快照备份!!!

一. LVM简介

二. LVM基本术语

三. 安装LVM

四. 创建和管理LVM

    1. 创建PV
    1. 创建VG
    1. 创建LV
    1. LV格式化及挂载

五. ECS上LVM扩容实战(磁盘扩容)

一. 简介

LVM是逻辑卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的逻辑层,来提高磁盘分区管理的灵活性。

LVM的工作原理其实很简单,它就是通过将底层的物理磁盘抽象的封装起来,然后以逻辑卷的方式呈现给上层应用。在传统的磁盘管理机制中,我们的上层应用是直接访问文件系统,从而对底层的物理硬盘进行读取,而在LVM中,其通过对底层的硬盘进行封装,当我们对底层的物理硬盘进行操作时,其不再是针对于分区进行操作,而是通过逻辑卷来对其进行底层的磁盘管理操作。比如说我增加一个物理硬盘,这个时候上层的服务是感觉不到的,因为呈现给上层服务的是以逻辑卷的方式。

LVM最大的特点就是可以对磁盘进行动态管理。因为逻辑卷的大小是可以动态调整的,而且不会丢失现有的数据。如果我们新增加了硬盘,其也不会改变现有上层的逻辑卷。作为一个动态磁盘管理机制,逻辑卷技术大大提高了磁盘管理的灵活性。

二. LVM基本术语

物理磁盘
物理存储介质(The physical media):这里指系统的存储设备:硬盘,如:/dev/xvda、/dev/vdb等等,是存储系统最低层的存储单元。

PV(Physical Volume)- 物理卷
物理卷在逻辑卷管理中处于最底层,它可以是实际物理硬盘上的分区,也可以是整个物理硬盘

VG(Volumne Group)- 卷组
卷组建立在物理卷之上,一个卷组中至少要包括一个物理卷,在卷组建立之后可动态添加物理卷到卷组中。一个逻辑卷管理系统工程中可以只有一个卷组,也可以拥有多个卷组。

LV(Logical Volume)- 逻辑卷
逻辑卷建立在卷组之上,卷组中的未分配空间可以用于建立新的逻辑卷,逻辑卷建立后可以动态地扩展和缩小空间。系统中的多个逻辑卷可以属于同一个卷组,也可以属于不同的多个卷组。
LVM使用分层结构,如下图所示:
初识LVM及ECS上LVM分区扩容

三. 安装LVM

3.1 检查系统内是否安装了LVM管理工具

# rpm -qa |grep lvm
lvm2-2.02.143-12.el6_9.1.x86_64
lvm2-libs-2.02.143-12.el6_9.1.x86_64
lvm2-devel-2.02.143-12.el6_9.1.x86_64
lvm2-cluster-2.02.143-12.el6_9.1.x86_64

注:目前线上的镜像基本都集成了该工具

3.2 如果没有安装,可以使用yum的方式安装

# yum -y install lvm2*

四. 创建和管理LVM

4.1 创建一个 LVM 分区
fdisk的参数(n/p/1/回车/回车/t/8e/w)

# fdisk /dev/xvdb

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): p

Disk /dev/xvdb: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 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: 0xa2228253

    Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-6527, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-6527, default 6527): 
Using default value 6527

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/xvdb: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 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: 0xa2228253

    Device Boot      Start         End      Blocks   Id  System
/dev/xvdb1               1        6527    52428096   8e  Linux LVM

Command (m for help): w 
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

注:8e代表是lvm的分区

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      41  PPC PReP Boot   85  Linux extended  c7  Syrinx         
 5  Extended        42  SFS             86  NTFS volume set da  Non-FS data    
 6  FAT16           4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS       4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility   
 8  AIX             4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    50  OnTrack DM      93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       52  CP/M            9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 61  SpeedStor       a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    64  Novell Netware  af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 65  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  70  DiskSecure Mult b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 75  PC/IX           bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 80  Old Minix       be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1

4.2 创建 PV

# pvcreate /dev/xvdb1
Physical volume "/dev/xvdb1" successfully created

查看 PV 信息,可以看到有 30G 的容量

# pvdisplay
--- NEW Physical volume ---
  PV Name               /dev/xvdb1
  VG Name               
  PV Size               30.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               fuWBVL-laFd-0Bgm-q1TG-EzuP-xnet-7qwHEo

注:有些信息是后补的,因此可能会有前后信息不一致的情况,请见谅

使用下列命令可以删除物理卷:

# pvremove /dev/xvdb1

4.3 创建 VG

# vgcreate vg1 /dev/xvdb1
Volume group "vg1" successfully created

查看卷组信息

# vgdisplay  
 --- Volume group ---
  VG Name               vg1
  System ID             
  Format                lvm2
  Metadata Areas        1
  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                1
  Act PV                1
  VG Size               30.00 GiB
  PE Size               4.00 MiB
  Total PE              7679
  Alloc PE / Size       0 / 0   
  Free  PE / Size       7679 / 30.00 GiB
  VG UUID               7lh871-eolr-jXhX-OD3F-kwIo-s9nU-RtSwmh
  

从输出中,我们可以看见卷组的使用量/总量,物理卷给卷组提供空间,只要在这个卷组中还有可用空间,我们就可以创建/扩展逻辑卷。

使用下列命令删除卷组:

# vgremove vg1

4.4 创建 LV

# lvcreate -L 30G -n lv1 vg1

  Volume group "vg1" has insufficient free space (7679 extents): 7680 required.

vgdisplay看有30g,但是实际创建的时候报错了,
free PE 7679 * pe size 4M /1024=29.99 ,先创建一个29G的lv出来再扩展试下

4.5 扩容 LV

# vgdisplay (看下剩下多少)

可以看到还剩下1020M,添加1020M到lv1里面

#  lvextend -L +1020M /dev/vg1/lv1

注:这个扩容是VG本身还有余量,给LV扩容余量而非物理磁盘的扩容

4.6 格式化+挂载 LVM 分区

# mkfs.ext4 /dev/vg1/lv1

挂载 LVM

# mount /dev/vg1/lv1 /lvm

查看容量信息

# df -h

五. ECS上LVM扩容实战(磁盘扩容)

5.1 控制台扩容磁盘(这里不做详细图示了)

ECS 控制台--实例列表--管理--本实例磁盘--更多--磁盘扩容

设置目标容量,确定扩容

扩容完成后需要控制台重启一下ecs实例才可以

注:如果是待挂载状态则挂载后系统内看到的就是扩容后的容量了

5.2 登陆扩容

#fdisk -l

注:查看一下磁盘信息,可以看到,数据盘已经扩容到50g,但是lvm分区还是30g

Disk /dev/xvdb: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 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: 0xa2228253

    Device Boot      Start         End      Blocks   Id  System
/dev/xvdb1               1        3916    31455238+  8e  Linux LVM

Disk /dev/mapper/vg1-lv1: 32.2 GB, 32208060416 bytes
255 heads, 63 sectors/track, 3915 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: 0x00000000

因为是物理磁盘级别的扩容,因此需要卸载挂载的分区,并取消分区卷组的激活

#  umount /lvm

# vgchange -an vg1
  0 logical volume(s) in volume group "vg1" now active

注:取消激活后,可以尝试mount /dev/vg(tab键补充)就会发现已经没有vg的路径了

将扩容后的空闲磁盘创建一个分区出来

 fdisk /dev/xvdb

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): p

Disk /dev/xvdb: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 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: 0xa2228253

    Device Boot      Start         End      Blocks   Id  System
/dev/xvdb1               1        3916    31455238+  8e  Linux LVM

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (3917-6527, default 3917): 
Using default value 3917
Last cylinder, +cylinders or +size{K,M,G} (3917-6527, default 6527): 
Using default value 6527

Command (m for help): p

Disk /dev/xvdb: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 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: 0xa2228253

    Device Boot      Start         End      Blocks   Id  System
/dev/xvdb1               1        3916    31455238+  8e  Linux LVM
/dev/xvdb2            3917        6527    20972857+  83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

将新增的分区加到卷组里面

# vgextend vg1 /dev/xvdb2
  Physical volume "/dev/xvdb2" successfully created
  Volume group "vg1" successfully extended

查看卷组信息可以看到FREE PE多了出来

# vgdisplay
  --- Volume group ---
  VG Name               vg1
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  7
  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               50.00 GiB
  PE Size               4.00 MiB
  Total PE              12799
  Alloc PE / Size       7679 / 30.00 GiB
  Free  PE / Size       5120 / 20.00 GiB
  VG UUID               9dBOCJ-KiSM-r6vK-pICt-n2tQ-fqBT-SLluiM

扩容前重新激活一下逻辑卷

# vgchange -ay vg1
  1 logical volume(s) in volume group "vg1" now active

将这20G空间扩容到逻辑卷上

# lvextend -L +20G /dev/vg1/lv1 
  Size of logical volume vg1/lv1 changed from 30.00 GiB (7679 extents) to 50.00 GiB (12799 extents).
  Logical volume lv1 successfully resized.

查看卷组信息,已经都分配完了

# vgdisplay
  --- Volume group ---
  VG Name               vg1
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  8
  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               50.00 GiB
  PE Size               4.00 MiB
  Total PE              12799
  Alloc PE / Size       12799 / 50.00 GiB
  Free  PE / Size       0 / 0   
  VG UUID               9dBOCJ-KiSM-r6vK-pICt-n2tQ-fqBT-SLluiM

重置一下逻辑卷的大小并挂载

# mount /dev/vg1/lv1 /lvm
# resize2fs /dev/vg1/lv1 
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg1/lv1 is mounted on /lvm; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 4
Performing an on-line resize of /dev/vg1/lv1 to 13106176 (4k) blocks.
The filesystem on /dev/vg1/lv1 is now 13106176 blocks long.

# df -Th
Filesystem          Type   Size  Used Avail Use% Mounted on
/dev/xvda1          ext4    20G   18G  1.4G  93% /
tmpfs               tmpfs  498M     0  498M   0% /dev/shm
/dev/mapper/vg1-lv1 ext4    50G  180M   47G   1% /lvm

# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg1/lv1
  LV Name                lv1
  VG Name                vg1
  LV UUID                627piC-rDhg-M3eE-13XC-Ed8p-HCfs-PDY0c4
  LV Write Access        read/write
  LV Creation host, time lvm, 2018-03-21 07:21:18 +0000
  LV Status              available
  # open                 0
  LV Size                50.00 GiB
  Current LE             12799
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

注:实际操作中如果df -h看不到扩容后的大小,看下resize2fs是不是有提示执行e2fsck的提示

# resize2fs /dev/vg1/lv1 
resize2fs 1.41.12 (17-May-2010)
Please run 'e2fsck -f /dev/vg1/lv1' first.

区别应是我前面挂载了所以直接resize成功,不挂载则需要检查一下磁盘信息

参考文献:

https://help.aliyun.com/document_detail/35097.html

https://linux.cn/article-3218-1.html

http://www.cnblogs.com/gaojun/archive/2012/08/22/2650229.html

http://www.cnblogs.com/mchina/p/linux-centos-logical-volume-manager-lvm.html

上一篇:gradle编译出错:Execution failed for task ':app:compileTestDebugJava'


下一篇:linux分区扩容