虚拟机硬盘扩容记录

LVM概念

啥是LVM?。Logical Volume Manager中文名叫逻辑卷管理器,简称LVM,它是Linux下对逻辑卷进行管理的一套程序,LVM分区,就是LVM管理下的一个逻辑卷。LVM通常涉及下面几个术语:

  • PP(physical Partition)
    物理分区,通常指硬盘中的主分区,逻辑分区。严格来讲整个硬盘或者其他能存东西的物理介质包括网络存储也能作为一个物理分区,但一定是有实物的
  • PV(Physical Volume)
    物理卷,就是在PP(物理分区)的基础上,使用pvcreate指令写入一些数据,转换为可供LVM识别后的物理介质。
  • VG(Volumes Group)
    卷组,由一个或多个PV组成的一个分组,形成一大块存储空间
  • LV(Logical Volume)
    逻辑卷,也就是LVM分区,在VG上划分一块空间称为逻辑卷,一个VG上只要还有空间就可以建立多个LV。在逻辑卷之上可以建立文件系统(ext3、ext4等),然后即可把数据存到分区里
  • PE(Physical Extent)
    具有唯一编号的PE是可以被LVM寻址的最小单元,其大小是在创建VG时确定的,默认为4MB(可通过vgcreate -s指定)。一个VG包含N个PE,从VG上划分一块空间作为LV时就是把其中一部分数量的PE标记为该LV。

LVM分区扩容

LVM分区扩容实际上是扩容LV分区的大小,配置思路如下:

fdisk创建lvm类型分区 -> 使用该分区创建PV -> PV加入VG,扩容VG -> 扩容LV分区 -> 刷新分区大小

fdisk新建分区

在esxi后台对硬盘扩容后,并不会直接应用到系统中,需要将扩容的区块新建为系统分区。

配置思路: 创建主分区 -> 设置分区类型为8e(lvm类型) -> 保存分区

一些问题:如果创建的第一个分区没达到目标容量,是开始和结束的块选择到了非预期扩容的区域。取巧的方法可多创建几个分区(不能保存), 直到获得的目标分区符合实际大小,之后删除无用的分区即可。

关键命令介绍

Command (m for help): n使用n指令创建分区,选p创建主分区;
Partition number (3,4, default 3)设置主分区id为3,即sda3,这里我创建两个分区sda3 sda4,sda3不是目标分区后续要删除(因为要使用sd4的块就必须先创建sda3);
Command (m for help): t使用t指令修改分区的类型;
Partition number (1-5): 4选择要修改的分区id;
Hex code (type L to list codes): 8e选择分区的类型(8e就表示lvm),输入l可列出所有支持的类型;
Command (m for help): p现在打印一下当前分区表,确认是不是自己想要的;
Command (m for help): w确认分区表正确后,用w写入硬盘。
partprobe刷新分区表。

配置记录

root@debian:~# fdisk /dev/sda

Welcome to fdisk (util-linux 2.33.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): p
Disk /dev/sda: 60 GiB, 64424509440 bytes, 125829120 sectors
Disk model: Virtual disk    
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x745f4409

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sda1  *       2048   999423   997376  487M 83 Linux
/dev/sda2       1001470 41940991 40939522 19.5G  5 Extended
/dev/sda5       1001472 41940991 40939520 19.5G 8e Linux LVM

Command (m for help): n
Partition type
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): p
Partition number (3,4, default 3): 3
First sector (999424-125829119, default 999424): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (999424-1001469, default 1001469): 

Created a new partition 3 of type 'Linux' and of size 1023 KiB.

Command (m for help): p
Disk /dev/sda: 60 GiB, 64424509440 bytes, 125829120 sectors
Disk model: Virtual disk    
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x745f4409

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sda1  *       2048   999423   997376  487M 83 Linux
/dev/sda2       1001470 41940991 40939522 19.5G  5 Extended
/dev/sda3        999424  1001469     2046 1023K 83 Linux
/dev/sda5       1001472 41940991 40939520 19.5G 8e Linux LVM

Partition table entries are not in disk order.

Command (m for help): n
Partition type
   p   primary (2 primary, 1 extended, 1 free)
   l   logical (numbered from 5)
Select (default p): p

Selected partition 4
First sector (41940992-125829119, default 41940992): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (41940992-125829119, default 125829119): 

Created a new partition 4 of type 'Linux' and of size 40 GiB.

Command (m for help): 

Command (m for help): p

Disk /dev/sda: 60 GiB, 64424509440 bytes, 125829120 sectors
Disk model: Virtual disk    
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x745f4409

Device     Boot    Start       End  Sectors  Size Id Type
/dev/sda1  *        2048    999423   997376  487M 83 Linux
/dev/sda2        1001470  41940991 40939522 19.5G  5 Extended
/dev/sda3         999424   1001469     2046 1023K 83 Linux
/dev/sda4       41940992 125829119 83888128   40G 83 Linux
/dev/sda5        1001472  41940991 40939520 19.5G 8e Linux LVM

Partition table entries are not in disk order.

Command (m for help): d
Partition number (1-5, default 5): 3

Partition 3 has been deleted.

Command (m for help): p
Disk /dev/sda: 60 GiB, 64424509440 bytes, 125829120 sectors
Disk model: Virtual disk    
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x745f4409

Device     Boot    Start       End  Sectors  Size Id Type
/dev/sda1  *        2048    999423   997376  487M 83 Linux
/dev/sda2        1001470  41940991 40939522 19.5G  5 Extended
/dev/sda4       41940992 125829119 83888128   40G 83 Linux
/dev/sda5        1001472  41940991 40939520 19.5G 8e Linux LVM

Partition table entries are not in disk order.

Command (m for help): t
Partition number (1,2,4,5, default 5): 4
Hex code (type L to list all codes): 8e

Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): p
Disk /dev/sda: 60 GiB, 64424509440 bytes, 125829120 sectors
Disk model: Virtual disk    
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x745f4409

Device     Boot    Start       End  Sectors  Size Id Type
/dev/sda1  *        2048    999423   997376  487M 83 Linux
/dev/sda2        1001470  41940991 40939522 19.5G  5 Extended
/dev/sda4       41940992 125829119 83888128   40G 8e Linux LVM
/dev/sda5        1001472  41940991 40939520 19.5G 8e Linux LVM

Partition table entries are not in disk order.

Command (m for help): 

Command (m for help): w

The partition table has been altered.
Syncing disks.

root@debian:~# fdisk -l
Disk /dev/sda: 60 GiB, 64424509440 bytes, 125829120 sectors
Disk model: Virtual disk    
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x745f4409

Device     Boot    Start       End  Sectors  Size Id Type
/dev/sda1  *        2048    999423   997376  487M 83 Linux
/dev/sda2        1001470  41940991 40939522 19.5G  5 Extended
/dev/sda4       41940992 125829119 83888128   40G 8e Linux LVM
/dev/sda5        1001472  41940991 40939520 19.5G 8e Linux LVM

Partition table entries are not in disk order.

Disk /dev/mapper/debian--vg-root: 18.6 GiB, 19931332608 bytes, 38928384 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/mapper/debian--vg-swap_1: 980 MiB, 1027604480 bytes, 2007040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
root@debian:~# 
root@debian:~# partprobe 
root@debian:~# 

创建PV

/dev/sda4 为上一步新增的分区

root@debian:~# pvcreate /dev/sda4 
  Physical volume "/dev/sda4" successfully created.

PV表示的是一个个物理分区,需要加入到VG中,扩容VG的容量。

扩容VG

vgdisplay查看当前的VG详情

root@debian:~# vgdisplay
  --- Volume group ---
  VG Name               debian-vg
  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.52 GiB
  PE Size               4.00 MiB
  Total PE              4997
  Alloc PE / Size       4997 / <19.52 GiB
  Free  PE / Size       0 / 0   
  VG UUID               2FWOSr-RBUZ-lUd5-ATV5-Jf3i-mpeR-QQ325A

将创建的pv加入VG中。

root@debian:~# vgextend debian-vg /dev/sda4
  Volume group "debian-vg" successfully extended

查看VG容量增加了。

root@debian:~# vgdisplay
  --- Volume group ---
  VG Name               debian-vg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               <59.52 GiB
  PE Size               4.00 MiB
  Total PE              15237
  Alloc PE / Size       4997 / <19.52 GiB
  Free  PE / Size       10240 / 40.00 GiB
  VG UUID               2FWOSr-RBUZ-lUd5-ATV5-Jf3i-mpeR-QQ325A

扩容LV

查看现有的LV,同一个VG中可以有多个LV,这里扩容/dev/debian-vg/root

root@debian:~# lvdisplay -l 
lvdisplay: invalid option -- 'l'
  Error during parsing of command line.
root@debian:~# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/debian-vg/root
  LV Name                root
  VG Name                debian-vg
  LV UUID                WT5yLx-vEpt-IVDo-UEDw-W3mh-rGQl-xGo1Uo
  LV Write Access        read/write
  LV Creation host, time debian, 2021-12-19 11:06:39 +0800
  LV Status              available
  # open                 1
  LV Size                58.56 GiB
  Current LE             14992
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:0

  --- Logical volume ---
  LV Path                /dev/debian-vg/swap_1
  LV Name                swap_1
  VG Name                debian-vg
  LV UUID                eBkgqj-hFEo-sMOh-3tRc-rjef-EwTh-ulflUC
  LV Write Access        read/write
  LV Creation host, time debian, 2021-12-19 11:06:39 +0800
  LV Status              available
  # open                 2
  LV Size                980.00 MiB
  Current LE             245
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:1

扩容/dev/mapper/debian--vg-root

root@debian:~# lvextend -l +100%FREE /dev/mapper/debian--vg-root
  Size of logical volume debian-vg/root changed from 18.56 GiB (4752 extents) to 58.56 GiB (14992 extents).
  Logical volume debian-vg/root successfully resized.

刷新分区大小

使用resize2fs刷新对应分区即可。

root@debian:~# resize2fs /dev/mapper/debian--vg-root
resize2fs 1.44.5 (15-Dec-2018)
Filesystem at /dev/mapper/debian--vg-root is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 8
The filesystem on /dev/mapper/debian--vg-root is now 15351808 (4k) blocks long.

root@debian:~# df -h 
Filesystem                   Size  Used Avail Use% Mounted on
udev                         3.9G     0  3.9G   0% /dev
tmpfs                        798M  8.8M  790M   2% /run
/dev/mapper/debian--vg-root   58G  8.7G   47G  16% /
/dev/sda1                    470M   83M  363M  19% /boot

拓展

未分区的磁盘扩容

在esxi直接扩容虚拟机硬盘后,直接进入系统刷新磁盘大小即可。

root@debian:~# resize2fs  /dev/sdb
resize2fs 1.44.5 (15-Dec-2018)
Filesystem at /dev/sdb is mounted on /mnt/data; on-line resizing required
old_desc_blocks = 38, new_desc_blocks = 75
The filesystem on /dev/sdb is now 157286400 (4k) blocks long.
上一篇
下一篇