前言

使用中的Debian12虚拟机的LVM根分区已经不够用了,在虚拟机设置中直接修改磁盘容量即可,启动后硬盘大小会自动识别, 需要通过创建新的物理卷和逻辑卷来扩充根分区大小

原理

LVM(Logical Volume Manager)是Linux下一种用于管理存储设备,它提供了对存储设备进行灵活管理的能力。LVM的主要原理和结构如下:

  1. 物理卷(Physical Volumes) :物理卷是LVM中的最底层,它可以是硬盘驱动器的整个物理设备,也可以是分区。LVM会将这些物理卷抽象为逻辑卷组。
  2. 逻辑卷组(Volume Group) :逻辑卷组是由一个或多个物理卷组成的逻辑单元,它为逻辑卷的创建提供了基础。逻辑卷组内的物理卷可以是同一硬盘上的不同分区,也可以是不同硬盘上的分区。逻辑卷组可以动态地扩展和收缩。
  3. 逻辑卷(Logical Volume) :逻辑卷是在逻辑卷组上创建的,它相当于传统分区的替代品。逻辑卷可以像物理分区一样被格式化并挂载到文件系统上使用。逻辑卷的大小可以根据需要进行动态调整,而无需重新分区或格式化磁盘。
lvm-volume-components

主要优势:可以动态地调整逻辑卷的大小,将多个物理卷组成一个逻辑卷组以提供更大的存储空间,以及在不中断服务的情况下扩展文件系统。

扩容分区

  • 查看磁盘基本状况
disk-components

扩充后的物理磁盘总大小为45G,根分区19.5G,将剩余的25G全部分配给根分区。

移除Swap分区(可选)

系统内存比较充裕可移除swap分区,全部给根分区

# 关闭swap
swapoff -a

# 删除swap分区挂载
sed -ri 's/.*swap.*/#&/' /etc/fstab

# 注释swap相关的行
vim /etc/initramfs-tools/conf.d/resume

# 更新引导
update-initramfs -u
update-grub
  • LVM分区还需移除swap逻辑卷
# 查看lvm逻辑卷
lvdisplay
swap-logic-volume
  • 移除swap逻辑分区,有提示直接输入y同意即可(重启一次)。
lvremove /dev/Debian-vg/swap_1
  • 将空间合并到根分区

    +100%FREE是指将剩余所有空间全部分配到根分区

lvextend -l +100%FREE /dev/Debian-vg/root

# 更新文件系统空间
resize2fs /dev/Debian-vg/root
  • log
root@Debian:~# lvextend -l +100%FREE /dev/Debian-vg/root
  Size of logical volume Debian-vg/root changed from 18.56 GiB (4752 extents) to <19.52 GiB (4997 extents).
  Logical volume Debian-vg/root successfully resized.

root@Debian:~# resize2fs /dev/Debian-vg/root
resize2fs 1.47.0 (5-Feb-2023)
Filesystem at /dev/Debian-vg/root is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 3
The filesystem on /dev/Debian-vg/root is now 5116928 (4k) blocks long.

扩容

虚拟机物理磁盘大小已经提前修改,剩下的磁盘空间需要通过创建新的物理卷和逻辑卷来进行扩充根分区。

# 进入根分区(lsblk查看)
fdisk /dev/sda
  • log
root@Debian:~# fdisk /dev/sda  #进入根分区

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

This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.


Command (m for help): p #打印分区信息

Disk /dev/sda: 45 GiB, 48318382080 bytes, 94371840 sectors
Disk model: Virtual disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 byte
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xe0aad630

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 (41940992-94371839, default 41940992): #默认
Last sector, +/-sectors or +/-size{K,M,G,T,P} (41940992-94371839, default 94371839): 默认

Created a new partition 3 of type 'Linux' and of size 25 GiB.

Command (m for help): t #分区的类型
Partition number (1-3,5, default 5): 3 #分区号
Hex code or alias (type L to list all): 8e #lvm分区类型

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

Command (m for help): w #保存修改
The partition table has been altered.
Syncing disks.

注解:

Command (m for help): n使用n指令创建分区,选p创建主分区(原有的分区为1,2,5;新的分区就建3就行)
Partition number (1-4): 3设置主分区id为3,即sda3
First cylinder (41940992-94371839, default 41940992):输入分区开始的第一个扇区,默认为空闲空间的第二个扇区41940992,直接回车默认
Last cylinder, +cylinders or +size{K,M,G} (41940992-94371839, default 94371839):直接回车默认
Command (m for help): t使用t指令修改分区的类型
Partition number (1-4): 3选择要修改的分区id
Hex code (type L to list codes): 8e选择分区的类型(8e就表示lvm),输入l可列出所有支持的类型
Command (m for help): p现在打印一下当前分区表,确认是不是自己想要的
Command (m for help): w确认分区表正确后,用w写入硬盘。出现警告y确认,重启一下

重启后,使用lvm管理命令操作

  • 注意有时候重启过盘号会变 lsblk查看一下
# 创建物理卷(确认盘号)
pvcreate /dev/sdb3

# 加入卷组(需使用vgdisplay查看VG Name)
# 根据以上信息确认根分区卷组为'Debian-vg'
vgextend Debian-vg /dev/sdb3

# 将空间全部划分到根分区
lvextend -l +100%FREE /dev/Debian-vg/root

# 文件系统刷新
resize2fs /dev/Debian-vg/root
  • log
root@Debian:~# vgdisplay #查看分组
  --- Volume group ---
  VG Name               Debian-vg  #卷组名
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  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               cqBC75-kvoJ-qcqY-PK3c-0Ghr-KTGF-h59LAW

root@Debian:~# pvcreate /dev/sdb3 
  Physical volume "/dev/sdb3" successfully created.
root@Debian:~# vgextend Debian-vg /dev/sdb3
  Volume group "Debian-vg" successfully extended
root@Debian:~# lvextend -l +100%FREE /dev/Debian-vg/root
  Size of logical volume Debian-vg/root changed from <19.52 GiB (4997 extents) to <44.52 GiB (11397 extents).
  Logical volume Debian-vg/root successfully resized.
root@Debian:~# resize2fs /dev/Debian-vg/root
resize2fs 1.47.0 (5-Feb-2023)
Filesystem at /dev/Debian-vg/root is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 6
The filesystem on /dev/Debian-vg/root is now 11670528 (4k) blocks long.

root@Debian:~# lsblk #查看扩容结果
NAME                MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda                   8:0    0    80G  0 disk /mnt/app
sdb                   8:16   0    45G  0 disk
├─sdb1                8:17   0   487M  0 part /boot
├─sdb2                8:18   0     1K  0 part
├─sdb3                8:19   0    25G  0 part
│ └─Debian--vg-root 254:0    0  44.5G  0 lvm  /
└─sdb5                8:21   0  19.5G  0 part
  └─Debian--vg-root 254:0    0  44.5G  0 lvm  /
sdc                   8:32   0 931.5G  0 disk /mnt/data
sdd                   8:48   0 119.2G  0 disk /mnt/usb

最后重启一次 完成,有了lvm磁盘管理后更像是一个存储池的概念,扩容就是将新的磁盘加入到存储池中,再创建新的物理、逻辑卷并将其加入到需要扩容的卷组;灵活性真的很好。

最后修改:2024 年 04 月 22 日
如果觉得我的文章对你有用,请随意赞赏