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
.
- 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
- 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.
- 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.
Someone from another forum found the solution i'll sum up below: It seems that sfdisk need an explicit
partprobe
orkpartx -uv /dev/sda
after writing changes.It means I have to perform the following tasks :
Now i'm able to extend and resize my LVM part with