2

I am trying to write a script to programmatically expand a disk (with LVM enabled).

The ubuntu server 22.04 image will be used to create automatically provisioned VM's on a host.

I know of the sudo lvextend -L +[size] [partition] command, but I want to automatically query the free space on /dev/sda and then pass it to lvextend.

In all scenarios, there will only be 1 disk with variable free space, and I want to automatically expand the linux partition to take up all of it.

My df -h looks like this:

runner@hya-worker-temp:~$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                               91M  928K   90M   2% /run
/dev/mapper/ubuntu--vg-ubuntu--lv  7.6G  3.9G  3.3G  55% /
tmpfs                              453M     0  453M   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          1.7G  126M  1.5G   8% /boot
/dev/sda1                          537M  5.3M  532M   1% /boot/efi
tmpfs                               91M  4.0K   91M   1% /run/user/1001

How can I query the free space and pass it on to lvextend from a .sh script, or is there an even simpler way to expand the storage.

4
  • Are you absolutely sure you want to automate something so dangerous as partition operations? If so, please include a dd backup function into your script. Jul 31, 2022 at 5:00
  • @mashuptwice how would I set that up?
    – YaRmgl
    Jul 31, 2022 at 8:01
  • To be honest, I would not. Do you have an option to provision the VMs with the right partition layout right from the beginning? Also: "My fdisk -l looks like this:" but you've added the output of df -h Jul 31, 2022 at 9:40
  • @mashuptwice well the image is built with a 15 GB disk partition, unless there is a way to automatically take up all the space on the final disk it's my only option
    – YaRmgl
    Jul 31, 2022 at 9:53

2 Answers 2

0

I created the following script that does exactly what I need:

growpart /dev/sda 3
pvresize /dev/sda3
lvresize -l+100%FREE --resizefs /dev/mapper/ubuntu--vg-ubuntu--lv
0

I think whether your sized disk automatically extend that depends on which allocated storage type you set.

Just use VirtualBox for an example, if you choose fixed size storage, that disk size do not automatically expand; if choose Dynamically allocated storage, the disk do automatically expand.

But not matter where you set dynamically allocated, in VirtualBox, or even Hy-V, that disk has the maximum size. E.g: Debian/Ubuntu's max size of dynamical space is 12GB.

So, when your disk has been used up to 12GB, you need to manually expand the VM's disk space again.

Recently, I just set my VM Debian to dynamically expand, just like you do, and set the space size of VM up 12GB, but a few months later, all of my VM Debian's disk space has been used up to 100%, so I have to spend a week to learn how to manually expand my VM's disk size, because my VM says Low disk space, only less than 100MB free!. Oh, my past dear days!

Notice: if the used percent of a disk up to 80, that is the 80% used disk, that disk is in dangerous status.

In all, I suggest you enable more space to your VM, e.g: 30GB or even 60GB.

OVER!

1
  • 1
    I am hosting the VM on cloud for server purposes, the disk is set up with LVM, I have put an answer with the script, but thanks for the information
    – YaRmgl
    Oct 24, 2022 at 4:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .