通过脚本使用parted扩展逻辑卷

通过脚本使用parted扩展逻辑卷

我想通过脚本扩展我的逻辑卷。

在我拥有带有 10 GB 硬盘的虚拟机之前,我将硬盘增加了 5 GB,然后总共 15 GB。我手动运行命令来扩展硬盘,如下所示:

sudo parted /dev/sda
Warning: Not all of the space available to /dev/sda appears to be used, you can fix the GPT to use all of the space (an extra 8388608 blocks) or continue with the current setting? 
Fix/Ignore? fix                                                           
Partition number? 3
End?  [17.2GB]? 100%FREE
Information: You may need to update /etc/fstab.
sudo pvresize /dev/sda3
sudo lvresize --extents +100%FREE --resizefs /dev/mapper/ubuntu--vg-ubuntu--lv

test-vm:~$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
udev                               447M     0  447M   0% /dev
tmpfs                               99M  1.1M   98M   2% /run
/dev/mapper/ubuntu--vg-ubuntu--lv  8.8G  4.6G  3.8G  55% /
tmpfs                              491M     0  491M   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
tmpfs                              491M     0  491M   0% /sys/fs/cgroup
/dev/sda2                          976M  106M  804M  12% /boot
/dev/loop1                          72M   72M     0 100% /snap/lxd/16099
/dev/loop2                          68M   68M     0 100% /snap/lxd/21835
/dev/loop3                          44M   44M     0 100% /snap/snapd/14295
/dev/loop4                          56M   56M     0 100% /snap/core18/2253
/dev/loop5                          62M   62M     0 100% /snap/core20/1270
tmpfs                               99M     0   99M   0% /run/user/1000
/dev/loop7                          56M   56M     0 100% /snap/core18/2284
/dev/loop8                          44M   44M     0 100% /snap/snapd/14549

添加额外的 5 GB 到 /dev/mapper/ubuntu--vg-ubuntu--lv

我想在脚本中运行上述内容,如下所示:

#!/bin/bash

sudo parted -s /dev/sda 'resizepart 3 100%'

sudo pvresize /dev/sda3

sudo lvresize --extents +100%FREE --resizefs /dev/mapper/ubuntu--vg-ubuntu--lv

在运行上面名为 hdd.sh 的脚本时,我得到以下输出:

test-vm:~ ./hdd.sh 
Warning: Not all of the space available to /dev/sda appears to be used, you can fix the GPT to use all of the space (an extra 10485760 blocks) or continue with the current setting? 
Error: Unable to satisfy all constraints on the partition.
  Physical volume "/dev/sda3" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized
  Size of logical volume ubuntu-vg/ubuntu-lv unchanged from <9.00 GiB (2303 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.
resize2fs 1.45.5 (07-Jan-2020)
The filesystem is already 2358272 (4k) blocks long.  Nothing to do!

所以第一个parted命令无法执行,因为我无法在脚本中提供输入“fix”,尝试了这个 sudo parted -s /dev/sda resizepart 3 Fix '100%'

我的目标是通过脚本扩展逻辑卷。任何意见都高度赞赏!

谢谢!

答案1

我不确定是否可以parted在脚本中处理这个极端情况(无需从头开始重新创建分区表)。

不幸的是,parted它首先是一个交互式程序,其次才是可编写脚本的程序。

您可以sgdisk --move-second-header改为使用,然后继续parted(或者sgdisk如果您愿意,可以使用脚本编写所有内容,但我认为没有sgdisk等效的 for resizepart)。

答案2

感谢您的输入。

我按照您提到的 sgdisk 添加了,然后脚本如下所示:

#!/bin/bash

sudo sgdisk --move-second-header /dev/sda
sudo parted -s /dev/sda 'resizepart 3 100%'

sudo pvresize /dev/sda3

sudo lvresize --extents +100%FREE --resizefs /dev/mapper/ubuntu--vg-ubuntu--lv

上述脚本的输出:

Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.
  Physical volume "/dev/sda3" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized
  Size of logical volume ubuntu-vg/ubuntu-lv changed from <9.00 GiB (2303 extents) to <14.00 GiB (3583 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.
resize2fs 1.45.5 (07-Jan-2020)
Filesystem at /dev/mapper/ubuntu--vg-ubuntu--lv is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 2
The filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv is now 3668992 (4k) blocks long.

如果我在运行上述命令后看到 df -hi 发现添加了 5 GB (/dev/mapper/ubuntu--vg-ubuntu--lv) :

Filesystem                         Size  Used Avail Use% Mounted on
udev                               447M     0  447M   0% /dev
tmpfs                               99M  1.1M   98M   2% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   14G  4.6G  8.5G  36% /
tmpfs                              491M     0  491M   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
tmpfs                              491M     0  491M   0% /sys/fs/cgroup
/dev/sda2                          976M  106M  804M  12% /boot
/dev/loop1                          72M   72M     0 100% /snap/lxd/16099
/dev/loop2                          68M   68M     0 100% /snap/lxd/21835
/dev/loop3                          44M   44M     0 100% /snap/snapd/14295
/dev/loop4                          56M   56M     0 100% /snap/core18/2253
/dev/loop5                          62M   62M     0 100% /snap/core20/1270
tmpfs                               99M     0   99M   0% /run/user/1000
/dev/loop7                          56M   56M     0 100% /snap/core18/2284
/dev/loop8                          44M   44M     0 100% /snap/snapd/14549

所以我想只需要重新启动,或者我应该需要运行其他命令吗?

谢谢你!

相关内容