欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

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

程序员文章站 2023-11-13 21:55:34
前言 最近手头一台运行在 hyper-v 下面抓数据的服务器 (ubuntu 16.04) 磁盘空间不够了,之前也没有把数据单独放到一个分区,所以只能想办法把根目录给搞大...

前言

最近手头一台运行在 hyper-v 下面抓数据的服务器 (ubuntu 16.04) 磁盘空间不够了,之前也没有把数据单独放到一个分区,所以只能想办法把根目录给搞大一点。之前没有处理过这样的问题,网上搜了很多资料,现在把拓展方法记录,分享出来。

建议在操作之前做好备份工作

步骤概览

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

调大物理磁盘

虚拟机关机,然后直接在虚拟机管理里面操作。

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

现在把磁盘从 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

再查看一下 volumn 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,就说明现在 logic volumn 的总大小还不对,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-2015)
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

大功告成!

参考:

总结

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