J'avais une machine virtuelle sous Ubuntu dont le disque avait besoin d'être agrandit car la partition /var était pleine.
Je me suis mis au défi de réaliser cette opération à chaud, sans coupure de service et donc sans avoir à redémarrer.
⚠️ Cette opération a été réalisée par un professionnel (moi), il faut bien prendre en compte que la modification de la table de partition peut entraîner la perte complète des données. Il est donc important d'avoir une sauvegarde complète de sa machine avant d'effectuer ces modifications.⚠️
Comme expliqué la partition /var était remplie donc j'avais besoin de l'étendre.
La première chose à réaliser est d'augmenter la taille du disque virtuel. Dans cet exemple j'augmenterai le disque virtuel de 20G avec 4G dédiés à ma partition /var. Je garderai les 16G restants comme réserve pour une utilisation future.
⚠️ Juste au cas ou, je recommande à ce niveau de réaliser un snapshot de la machine virtuelle.⚠️
user@ubuntu:~$ sudo gdisk -l /dev/sda
GPT fdisk (gdisk) version 1.0.5
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Disk /dev/sda: 125829120 sectors, 60.0 GiB
odel: Virtual disk
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 76AC5877-F92F-4C0E-BC53-504F7E90466A
Partition table holds up to 128 entries
ain partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 125829086
Partitions will be aligned on 2048-sector boundaries
Total free space is 4029 sectors (2.0 MiB)
Number Start (sector) End (sector) Size Code Name
1 2048 1050623 512.0 MiB EF00
2 1050624 3147775 1024.0 MiB 8300
3 3147776 125827071 58.5 GiB 8300
user@ubuntu:~$ echo 1 | sudo tee /sys/block/sda/device/rescan
user@ubuntu:~$ sudo gdisk -l /dev/sda
GPT fdisk (gdisk) version 1.0.5
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Disk /dev/sda: 167772160 sectors, 80.0 GiB
odel: Virtual disk
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 76AC5877-F92F-4C0E-BC53-504F7E90466A
Partition table holds up to 128 entries
ain partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 125829086
Partitions will be aligned on 2048-sector boundaries
Total free space is 4029 sectors (2.0 MiB)
Number Start (sector) End (sector) Size Code Name
1 2048 1050623 512.0 MiB EF00
2 1050624 3147775 1024.0 MiB 8300
3 3147776 125827071 58.5 GiB 8300
user@ubuntu:~$ sudo gdisk /dev/sda
GPT fdisk (gdisk) version 1.0.5
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help): w
Warning! Secondary header is placed too early on the disk! Do you want to
correct this problem? (Y/N): Y
Have moved second header and partition table to correct location.
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/sda.
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.
user@ubuntu:~$ sudo gdisk -l /dev/sda
GPT fdisk (gdisk) version 1.0.5
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Disk /dev/sda: 167772160 sectors, 80.0 GiB
odel: Virtual disk
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 76AC5877-F92F-4C0E-BC53-504F7E90466A
Partition table holds up to 128 entries
ain partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 167772126
Partitions will be aligned on 2048-sector boundaries
Total free space is 41947069 sectors (20.0 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 1050623 512.0 MiB EF00
2 1050624 3147775 1024.0 MiB 8300
3 3147776 125827071 58.5 GiB 8300
user@ubuntu:~$ sudo gdisk /dev/sda
Command (? for help): d
Partition number (1-3): 3
Command (? for help): n
Partition number (3-128, default 3): 3
First sector (34-125829086, default = 3147776) or {+-}size{KMGTP}: 3147776
Last sector (3147776-125829086, default = 167772126) or {+-}size{KMGTP}:
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/sda.
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.
user@ubuntu:~$ sudo partprobe
user@ubuntu:~$ sudo pvdisplay /dev/sda3
--- Physical volume ---
PV Name /dev/sda3
VG Name ubuntu-vg
PV Size <58.50 GiB / not usable 2.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 14975
Free PE 0
Allocated PE 14975
PV UUID ThZ9VT-3zCa-SQrB-6s9a-ZiWL-DbqP-TqRNxM
user@ubuntu:~$ sudo pvresize /dev/sda3
Physical volume "/dev/sda3" changed
1 physical volume(s) resized or updated / 0 physical volume(s) not resized
user@ubuntu:~$ sudo pvdisplay /dev/sda3
--- Physical volume ---
PV Name /dev/sda3
VG Name ubuntu-vg
PV Size <78.50 GiB / not usable 1.98 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 20095
Free PE 5120
Allocated PE 14975
PV UUID ThZ9VT-3zCa-SQrB-6s9a-ZiWL-DbqP-TqRNxM
Dernière étape, nous avons maintenant besoin de redimensionner notre partition /var.
user@ubuntu:~$ sudo lvextend -r -L+4G /dev/mapper/ubuntu--vg-lvvar
Size of logical volume ubuntu-vg/lvvar changed from 4.00 GiB (1024 extents) to 8.00 GiB (2048 extents).
Logical volume ubuntu-vg/lvvar successfully resized.
resize2fs 1.45.5 (07-Jan-2020)
Filesystem at /dev/mapper/ubuntu--vg-lvvar is mounted on /var; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mapper/ubuntu--vg-lvvar is now 2097152 (4k) blocks long.
user@ubuntu:~$ df -h | grep var
/dev/mapper/ubuntu--vg-lvvar 7.9G 5.3G 2.2G 71% /var
Contact :