Linux LVM 虚拟机分区扩容(添加新硬盘并扩容至已有分区)

之前实验过同一块硬盘的扩容,参见

Linux LVM 虚拟机分区扩容(同一块硬盘),基于 zStack KVM
https://www.4wei.cn/archives/1003008

整体操作流程

  1. 创建 PV (物理卷, Physical volume)
  2. 创建/扩展 VG (逻辑卷组, Volume group)
  3. 创建/扩展 LV (逻辑卷, Logical volume)
  4. 扩展分区

旧的磁盘信息

vda 为200G磁盘

[root@dop-02 ~]# fdisk -l
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

磁盘 /dev/vda:214.7 GB, 214748364800 字节,419430400 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:gpt
Disk identifier: 35467CA6-33D5-4B2B-8204-018728026059

#         Start          End    Size  Type            Name
 1         2048       411647    200M  EFI System      EFI System Partition
 2       411648      2508799      1G  Microsoft basic
 3      2508800    419428351  198.8G  Linux LVM

磁盘 /dev/mapper/centos-root:182.5 GB, 182536110080 字节,356515840 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

磁盘 /dev/mapper/centos-swap:8589 MB, 8589934592 字节,16777216 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

磁盘 /dev/mapper/centos-home:22.3 GB, 22330474496 字节,43614208 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

添加新的磁盘


磁盘 /dev/vdb:536.9 GB, 536870912000 字节,1048576000 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

新磁盘分区操作,并创建物理卷

操作过程依次为 n-p-^-^-t-8e-w
pvcreate /dev/vdb1

[root@dop-02 ~]# fdisk /dev/vdb
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。

Device does not contain a recognized partition table
使用磁盘标识符 0x13099513 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
分区号 (1-4,默认 1):
起始 扇区 (2048-1048575999,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-1048575999,默认为 1048575999):
将使用默认值 1048575999
分区 1 已设置为 Linux 类型,大小设为 500 GiB

命令(输入 m 获取帮助):t
已选择分区 1
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM”

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@dop-02 ~]# fdisk -l

磁盘 /dev/vda:751.6 GB, 751619276800 字节,1468006400 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x00000000

   设备 Boot      Start         End      Blocks   Id  System
/dev/vda1               1   419430399   209715199+  ee  GPT

磁盘 /dev/mapper/centos-root:182.5 GB, 182536110080 字节,356515840 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

磁盘 /dev/mapper/centos-swap:8589 MB, 8589934592 字节,16777216 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

磁盘 /dev/mapper/centos-home:22.3 GB, 22330474496 字节,43614208 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

磁盘 /dev/vdb:536.9 GB, 536870912000 字节,1048576000 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x13099513

   设备 Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048  1048575999   524286976   8e  Linux LVM
[root@dop-02 ~]# lsblk
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
vdb             252:16   0   500G  0 disk
└─vdb1          252:17   0   500G  0 part
sr0              11:0    1  1024M  0 rom
vda             252:0    0   700G  0 disk
├─vda2          252:2    0     1G  0 part /boot
├─vda3          252:3    0 198.8G  0 part
│ ├─centos-swap 253:1    0     8G  0 lvm  [SWAP]
│ ├─centos-home 253:2    0  20.8G  0 lvm  /home
│ └─centos-root 253:0    0   170G  0 lvm  /
└─vda1          252:1    0   200M  0 part /boot/efi

[root@dop-02 ~]# pvcreate /dev/vdb1
  Physical volume "/dev/vdb1" successfully created.
[root@dop-02 ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/vda3
  VG Name               centos
  PV Size               198.80 GiB / not usable 2.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              50893
  Free PE               1
  Allocated PE          50892
  PV UUID               QwH9kW-Hu66-nYQM-GEYH-PHAO-35Qi-CV3hcs

  "/dev/vdb1" is a new physical volume of "<500.00 GiB"

将物理卷PV加入卷组VG,并扩容分区

  1. 使用 vgdisplay 查看卷组情况
  2. 使用 vgextend 将磁盘分区加入卷组,将新的分区/dev/sdb1加入到卷组中,这里的卷组名为centos
  3. 使用 lvextend 扩容分区
  4. 使用 xfs_groupfs 进行在线调整xfs格式的文件系统大小
[root@dop-02 ~]# vgextend centos /dev/vdb1
  Volume group "centos" successfully extended
[root@dop-02 ~]# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               <698.80 GiB
  PE Size               4.00 MiB
  Total PE              178892
  Alloc PE / Size       50892 / <198.80 GiB
  Free  PE / Size       128000 / 500.00 GiB
  VG UUID               Y90gF3-gsS1-yeo9-1ajp-Tchw-y4hU-A29qAA
[root@dop-02 ~]# lvextend -l +100%FREE /dev/mapper/centos-root
  Size of logical volume centos/root changed from 170.00 GiB (43520 extents) to 670.00 GiB (171520 extents).
  Logical volume centos/root successfully resized.
[root@dop-02 ~]# lvdisplay /dev/mapper/centos-root
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                7fwPTx-Ic7T-fROA-rbxk-bi9Y-NPbm-PZaDhZ
  LV Write Access        read/write
  LV Creation host, time localhost, 2022-07-22 16:49:39 +0800
  LV Status              available
  # open                 1
  LV Size                670.00 GiB
  Current LE             171520
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
[root@dop-02 ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=11141120 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=44564480, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=21760, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 44564480 to 175636480

因为分区的文件系统是 xfs,所以使用 xfs_growfs,如果是 ext4 则需要使用 resize2fs。

发表评论