Score:1

Differences between sfdisk and fdisk on LVM disk extend

cn flag

In order to semi-automate LVM partitions extend on Debian 11 VMs clones from a template, I feel a bit stuck when I try to script it through sfdisk compared to interactive fdisk.

  1. Here's the original partitionning
root# sudo fdisk /dev/sda

Commande (m pour l'aide) : p
Disque /dev/sda : 45 GiB, 48318382080 octets, 94371840 secteurs
Modèle de disque : VDISK
Unités : secteur de 1 × 512 = 512 octets
Taille de secteur (logique / physique) : 512 octets / 4096 octets
taille d'E/S (minimale / optimale) : 4096 octets / 1048576 octets
Type d'étiquette de disque : dos
Identifiant de disque : 0x919d40af

Périphérique Amorçage    Début      Fin Secteurs Taille Id Type
/dev/sda1    *            2048  2000895  1998848   976M 83 Linux
/dev/sda2              2000896 86962175 84961280  40,5G 8e LVM Linux
/dev/sda3             86964222 94369791  7405570   3,5G  5 Étendue
/dev/sda5             86964224 94369791  7405568   3,5G 83 Linux
  1. I perform the following tasks through fdisk to extend LVM's VGs : Note: sda3 and sda5 aren't used, that's why i delete them.
  • delete partition 5 : "d", 5
  • delete partition 3 : "d", 3
  • delete partition 2 : "d", 2
  • create new extended part 2 with default start/end sectors and type is LVM: "n", "e", "2", "t", "lvm"

the result is :

Commande (m pour l'aide) : p
Disque /dev/sda : 45 GiB, 48318382080 octets, 94371840 secteurs
Modèle de disque : VDISK
Unités : secteur de 1 × 512 = 512 octets
Taille de secteur (logique / physique) : 512 octets / 4096 octets
taille d'E/S (minimale / optimale) : 4096 octets / 1048576 octets
Type d'étiquette de disque : dos
Identifiant de disque : 0x919d40af

Périphérique Amorçage   Début      Fin Secteurs Taille Id Type
/dev/sda1    *           2048  2000895  1998848   976M 83 Linux
/dev/sda2             2000896 94371839 92370944    44G 8e LVM Linux

Next, after a VM disk grow from 45=>60GB, i'm able to refresh sda2 with sudo pvresize /dev/sda2

Then, extend my LVM's VGs with sudo lvextend -L +5G /dev/vg/my_vg_name

Finally, resize file system with sudo resize2fs /dev/vg/my_vg_name and successfully reboot.

  1. I want to script those actions (with human validation requirement for sure) so my first idea was to go with sfdisk. Before put the command into a script, i test them manually so i'll do :
  • delete partition 5 : sudo sfdisk /dev/sda 5 --delete --force --lock;
  • delete partition 3 : sudo sfdisk /dev/sda 3 --delete --force --lock;
  • delete partition 2 : sudo sfdisk /dev/sda 2 --delete --force --lock;
  • create new extended part 2 with default start/end sectors and type is LVM:
sudo sfdisk /dev/sda 2 --force --lock << EOF
,,V
write
EOF  

Result seems good, sda2 size grew from 45 to ~60GB :

Disque /dev/sda : 60 GiB, 64424509440 octets, 125829120 secteurs
Modèle de disque : VDISK
Unités : secteur de 1 × 512 = 512 octets
Taille de secteur (logique / physique) : 512 octets / 4096 octets
taille d'E/S (minimale / optimale) : 4096 octets / 1048576 octets
Type d'étiquette de disque : dos
Identifiant de disque : 0x919d40af

Périphérique Amorçage   Début       Fin  Secteurs Taille Id Type
/dev/sda1    *           2048   2000895   1998848   976M 83 Linux
/dev/sda2             2000896 125829119 123828224    59G 8e LVM Linux

Now let's refresh sda2 then extend one VG and it goes bad :

root# sudo pvresize /dev/sda2
  Physical volume "/dev/sda2" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized

root# sudo lvextend -L +5G /dev/vg/var
  Insufficient free space: 1280 extents needed, but only 0 available

It seems that partition table isn't up to date with sfdisk whereas it is with fdisk.

Is it a "normal" behaviour of sfdisk or do i missed something there ?

Also, if you have easier ideas or any advices, feel free to tell it if it's constructive.

Romeo Ninov avatar
in flag
Why do not create new partition, make it PV and add to VG?
inframan avatar
cn flag
Because there's a lot of growing requests for VMs, for simplicity matters, it's easier for the team to simply extend rather than have a lot of PVs (i imagine after 10-15x growing request we'll have too much PVs and error may happen very fast for reconfiguration.. and as people doesn't have the same skill level about Linux.. this solution seems the best for us :)
Score:0
cn flag

Someone from another forum found the solution i'll sum up below: It seems that sfdisk need an explicit partprobe or kpartx -uv /dev/sda after writing changes.

It means I have to perform the following tasks :

  • Rewrite sda 2 with new size as usual
sudo sfdisk /dev/sda 2 --force --lock << EOF
,,V
write
EOF
  • then "refresh" sda2
sudo pvresize /dev/sda2
  • finally launch a partprobe
sudo partprobe

Now i'm able to extend and resize my LVM part with

sudo lvextend -L +5G /dev/vg/var
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.