当前位置: 首页 > 编程文章 >

Ubuntu系统下扩展LVM根目录的方法

Ubuntu系统下扩展LVM根目录的方法

作者:林安时间:2026-10-29 13:35:38浏览:496

本文介绍在Hyper-V环境下为运行Ubuntu 16.04的服务器扩展LVM根目录的操作流程。当物理磁盘从100G调整至300G后,需通过parted和fdisk工具扩展物理分区,利用vgextend将新空间加入Volume Group,最后使用lvresize和resize2fs更新逻辑卷及文件系统,确保数据完整并释放可用空间。

本文目录收起

Ubuntu系统下扩展LVM根目录的方法

前言

近期处理一台部署于 Hyper-V 平台的 Ubuntu 16.04 数据采集服务器时,发现磁盘容量不足。由于此前未规划独立数据分区,只能尝试扩大根目录空间。针对这一常见问题,整理相关操作步骤以供参考。

操作前务必对重要数据进行备份,以防万一。

步骤概览

  1. 调大物理磁盘
  2. 确定要操作的磁盘
  3. 扩展 LVM 逻辑分区所在的物理拓展分区
  4. 新增 LVM 逻辑分区
  5. 新分区合并到相应 Volumn Group
  6. 更新文件系统

调大物理磁盘

首先关闭虚拟机,随后在虚拟机管理界面中直接修改磁盘大小配置。

本次操作将磁盘容量由 100G 扩充至 300G。

确定要操作的磁盘

首先检查当前磁盘使用情况,执行以下命令:


root@vm003:~# df -h
Filesystem  Size Used Avail Use% Mounted on
udev   3.9G 0 3.9G 0% /dev
tmpfs   798M 8.6M 789M 2% /run
/dev/mapper/Ubuntu-root 94G 88G 1.9G 98% /
tmpfs   3.9G 0 3.9G 0% /dev/shm
tmpfs   5.0M 0 5.0M 0% /run/lock
tmpfs   3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda1  472M 382M 66M 86% /boot
tmpfs   100K 0 100K 0% /run/lxcfs/controllers
tmpfs   798M 0 798M 0% /run/user/0

尽管物理磁盘已调整为 300G,但根目录显示仍约为 100G,且使用率高达 98%。

运行命令


root@vm003:~# fdisk -l
Disk /dev/sda: 300 GiB, 322122547200 bytes, 629145600 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0xa88f1366

Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 999423 997376 487M 83 Linux
/dev/sda2 1001470 209713151 208711682 99.5G 5 Extended
/dev/sda5 1001472 209713151 208711680 99.5G 8e Linux LVM

Partition 2 does not start on physical sector boundary.


Disk /dev/mapper/Ubuntu-root: 95.5 GiB, 102563315712 bytes, 200318976 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/mapper/Ubuntu-swap_1: 4 GiB, 4294967296 bytes, 8388608 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

输出结果显示 /dev/sda 容量确实已变为 300GiB,只是系统尚未识别并利用这部分空间。

由此确认需要操作的对象为 /dev/sda。

扩大 LVM 逻辑分区所在的物理分区

运行命令


root@vm003:~# parted /dev/sda
GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print # 查看分区
Model: Msft Virtual Disk (scsi)
Disk /dev/sda: 322GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags:

Number Start End Size Type File system Flags
 1 1049kB 512MB 511MB primary ext2  boot
 2 513MB 107GB 107GB extended
 5 513MB 107GB 107GB logical  lvm

(parted) resizepart 2 # 调整 sda2 分区大小
End? [107GB]? -0 # 直接充满
(parted) print # 再次查看
Model: Msft Virtual Disk (scsi)
Disk /dev/sda: 322GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags:

Number Start End Size Type File system Flags
 1 1049kB 512MB 511MB primary ext2  boot
 2 513MB 322GB 322GB extended
 5 513MB 107GB 107GB logical  lvm

(parted) q # 完成退出
Information: You may need to update /etc/fstab.

此时已成功将 /dev/sda2 扩展完毕。

新增 LVM 逻辑分区

运行命令


root@vm003:~# fdisk /dev/sda

Welcome to fdisk (util-linux 2.27.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: 300 GiB, 322122547200 bytes, 629145600 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0xa88f1366

Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 999423 997376 487M 83 Linux
/dev/sda2 1001470 629145599 628144130 299.5G 5 Extended
/dev/sda5 1001472 209713151 208711680 99.5G 8e Linux LVM

Partition 2 does not start on physical sector boundary.

Command (m for help): n # 新增分区,选择逻辑分区,起止点看情况输入,默认值是填充满整个磁盘

All space for primary partitions is in use.
Adding logical partition 6
First sector (209715200-629145599, default 209715200):
Last sector, +sectors or +size{K,M,G,T,P} (209715200-629145599, default 629145599):

Created a new partition 6 of type 'Linux' and of size 200 GiB.

Command (m for help): p # 查看新增的分区
Disk /dev/sda: 300 GiB, 322122547200 bytes, 629145600 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0xa88f1366

Device Boot Start End Sectors Size Id Type
/dev/sda1 *  2048 999423 997376 487M 83 Linux
/dev/sda2  1001470 629145599 628144130 299.5G 5 Extended
/dev/sda5  1001472 209713151 208711680 99.5G 8e Linux LVM
/dev/sda6 209715200 629145599 419430400 200G 83 Linux

Partition 2 does not start on physical sector boundary.

Command (m for help): t # 改变分区类型为 Linux LVM
Partition number (1,2,5,6, default 6): 6 # sda6
Partition type (type L to list all types): 8e # LVM 类型的 Id 代码

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

Command (m for help): p # 再次查看分区情况
Disk /dev/sda: 300 GiB, 322122547200 bytes, 629145600 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0xa88f1366

Device Boot Start End Sectors Size Id Type
/dev/sda1 *  2048 999423 997376 487M 83 Linux
/dev/sda2  1001470 629145599 628144130 299.5G 5 Extended
/dev/sda5  1001472 209713151 208711680 99.5G 8e Linux LVM
/dev/sda6 209715200 629145599 419430400 200G 8e Linux LVM

Partition 2 does not start on physical sector boundary.

Command (m for help): wq # 确认没有问题,保存退出

The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Device or resource busy

The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).

新分区合并到相应 Volumn Group

运行命令


root@vm003:~# vgdisplay
 --- Volume group ---
 VG Name  Ubuntu
 System ID
 Format  lvm2

观察输出可知,目标 VG Name 为 Ubuntu,请记录该名称。

接着运行命令


root@vm003:~# vgextend Ubuntu /dev/sda6 # /dev/sda6 是刚刚增加的 LVM 分区
 Device /dev/sda6 not found (or ignored by filtering).
 Unable to add physical volume '/dev/sda6' to volume group 'Ubuntu'.

系统提示未找到 /dev/sda6 设备,建议重启虚拟机后再试。


root@vm003:~# reboot

重启后再次执行


root@vm003:~# vgextend Ubuntu /dev/sda6 # /dev/sda6 是刚刚增加的 LVM 分区
 Physical volume "/dev/sda6" successfully created
 Volume group "Ubuntu" successfully extended

再次查看 Volume Group 状态,运行命令


root@vm003:~# vgs
 VG #PV #LV #SN Attr VSize VFree
 Ubuntu 2 2 0 wz--n- 299.52g 200.00g

确认新分区已成功添加。

然后运行


root@vm003:~# lvdisplay
 --- Logical volume ---
 LV Path  /dev/Ubuntu/root
 LV Name  root
 VG Name  Ubuntu

得知 Ubuntu VG 的 LV Path 为 /dev/Ubuntu/root,请记录此路径。

然后运行


root@vm003:~# lvresize -l +100%FREE /dev/Ubuntu/root # /dev/Ubuntu/root 是 LV Path
 Size of logical volume Ubuntu/root changed from 95.52 GiB (24453 extents) to 295.52 GiB (75652 extents).
 Logical volume root successfully resized.

至此逻辑卷扩展成功。

警告:若操作中出现类似下方的 warning,表明当前逻辑卷总大小计算有误,resize 操作不仅无法增加空间,反而会导致空间缩小,继续操作将导致数据丢失。应立即停止并按 n 取消。

WARNING: Reducing active and open logical volume to 32.00 MiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce root? [y/n]*

更新文件系统

最后一步,运行命令


root@vm003:~# resize2fs -p /dev/mapper/Ubuntu-root # /dev/mapper/Ubuntu-root 是从 df 命令看到的文件系统信息
resize2fs 1.42.13 (17-May-2026)
Filesystem at /dev/mapper/Ubuntu-root is mounted on /; on-line resizing required
old_desc_blocks = 6, new_desc_blocks = 19

The filesystem on /dev/mapper/Ubuntu-root is now 77467648 (4k) blocks long.

该过程可能需要几分钟,请耐心等待完成。

随后运行 df 命令检查磁盘使用情况。


root@vm003:~# df -h
Filesystem  Size Used Avail Use% Mounted on
udev   3.9G 0 3.9G 0% /dev
tmpfs   798M 8.6M 789M 2% /run
/dev/mapper/Ubuntu-root 291G 88G 191G 32% /
tmpfs   3.9G 0 3.9G 0% /dev/shm
tmpfs   5.0M 0 5.0M 0% /run/lock
tmpfs   3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda1  472M 382M 66M 86% /boot
tmpfs   100K 0 100K 0% /run/lxcfs/controllers
tmpfs   798M 0 798M 0% /run/user/0

大功告成!

参考:

https://www.jb51.net/article/139565.htm

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对本站的支持。

上一篇
CentOS 7 水星无线网卡驱动安装与网络配置详解

本文介绍在 CentOS 7 系统中为水星 MERCURY 无线网卡(芯片 RTL8188eus)安装驱动程序并配置无线网络连接的完整流程。主要步骤包括通过 lsusb 识别设备、编译安装内核模块、使用 wireless_tools 和 wpa_supplicant 进行密钥验证,以及通过 dhclient 获取 IP 地址。此外,还包含将启动命令写入 rc.local 以实现开机自动联网的配置方法,解决重启后需手动执行指令的问题。

下一篇
PHP实现MySQL数据库自动备份的方法与代码

本文介绍利用PHP函数实现MySQL数据库自动备份的具体方法。通过fopen、fwrite等文件操作函数结合PDO查询,获取表结构和数据并生成标准SQL文件。该方法无需依赖mysqldump命令行工具,适合在Web环境中集成使用,确保数据安全。

推荐文章

更多
PHP时间戳与日期字符串相互转换方法详解

本文介绍PHP中时间数值(Unix时间戳)与日期字符串之间的相互转换方法。通过strtotime和date函数,实现将当前时间、指定日期转换为时间戳,以及将时间戳格式化为可读的日期时间字符串,并提供相关在线计算工具推荐。

2025.11.30 1,016
PHP跨域配置:怎么安全地允许指定域名而非使用通配符

在 PHP 中安全地允许指定域名跨域,核心是解析请求头中的 Origin ,验证其是否在白名单内,然后动态设置 Access-Control-Allow-Origin 响应头。不要使用通配符 * ,也不要硬编码单一域名。

2025.03.29 1,003
Apache开启.htaccess及使用方法详解

本文介绍如何在本地Apache环境中启用.htaccess文件,主要涉及修改httpd.conf中的AllowOverride参数。同时详细解析.htaccess的多种实用功能,包括自定义错误页面、目录密码保护、IP访问限制、URL自动转向、默认首页设置以及防止图片盗链等配置方法,并分析了使用.htaccess对服务器性能和安全性的潜在影响。

2026.05.09 1,001
JavaScript中利用replace结合正则实现replaceAll效果

JavaScript原生仅支持replace()进行单次或全局替换,缺乏类似Java的replaceAll()便捷方法。本文总结四种在JS中实现全量替换的方案:使用带g标志的正则、带gm标志的正则、扩展String原型以及split后join。通过对比代码示例,展示各方法的实现细节与适用场景,帮助开发者根据需求选择最优解。

2025.04.29 1,001
PHP日期转时间戳时区偏差怎么解决

在 PHP 中将日期字符串转换为时间戳时,若未显式指定时区,默认使用脚本配置的 date.timezone 或系统时区。当服务器时区与业务逻辑预期(如 UTC )不一致时,会产生偏差。解决核心是确保输入字符串携带明确时区信息,或在转换前统一设置时区上下文。

2026.10.24 1,000
PHP实现基础文件上传:HTML表单与$_FILES处理流程

在 HTML 表单中设置 enctype="multipart/form-data" ,并在 PHP 后端通过 $_FILES 数组接收文件,调用 move_uploaded_file() 将临时文件移动到目标目录即可完成上传。

2026.09.07 1,000

专题推荐

更多
PHP 类与对象定义及继承

先看类声明、对象实例与属性访问。理解对象的创建和销毁后,再读继承、方法重载和关键字对照。已有代码遇到继承行为差异时,可进一步查看 final 方法与反射实例。

PHP 定时任务配置与运行

先确定任务运行间隔以及是否允许重叠。固定时间触发先看 Cron 案例,框架内和常驻服务再读对应示例。

Yii2 队列配置与常驻运行

先用一个最小任务跑通入队和消费,再检查 Web 与 Console 是否加载了相同的队列配置。确认命令能手动运行后再配置 Supervisor,最后验证进程重启时的处理情况。

PHP Session 过期时间设置方法

浏览器关闭、服务端回收和业务规定的闲置超时,可能在不同时间发生。对照配置项与应用层时间戳的做法,检查会话何时仍可读、何时应要求重新登录。

PHP URL 编码和解码方法

URL 路径片段和查询参数对空格的处理方式不同。先比较 urlencode 与 rawurlencode,再沿解码过程核对中文、加号和百分号,检查地址是否被重复编码或提前解码。

在线工具箱

更多
键码查询
键码查询

按下键盘按键时查看浏览器记录的键名和事件信息。

.env 配置检查
.env 配置检查

本地检查 .env 键值语法、引号、重复键及空值,预览解析结果,辅助排查 PHP 和 Laravel 环境配置。

进制转换
进制转换

转换二进制、十进制、十六进制等数值表示,辅助程序调试与进制学习。

HTML 标签安全清理
HTML 标签安全清理

清理 HTML 片段中的标签或危险内容,辅助整理待展示的文本。

文件 Base64 转换
文件 Base64 转换

把文件转为 Base64 文本,也可将编码内容还原为文件,方便接口传输与调试。