Debian: Estendere un disco EXT4 di una VM

Se avete la fortuna di utilizzare un sistema Linux virtualizzato, potrete struttare una serie di funzionalità interessanti, come ad esempio espandere al volo il disco di sistema.

Questa volta parlo di una VM con FileSystem Ext4, ma senza LVM. Non si può quindi seguire la guida per espandere un disco LVM.

Ipotizziamo un disco (/dev/sda) da 15GB che poi viene incrementato a 40GB lato host.

Per vedere il cambiamento del disco senza riavviare, digitiamo:

echo 1 > /sys/block/sda/device/rescan

In elenco le procedure da seguire sono:

  1. Prendere nota dell’attuale distribuzione dello spazio
  2. Eliminare la partizione primaria
  3. Eliminare la partizione estesa
  4. Creare una nuova partiziona primaria iniziando dallo stesso settore di quella precedente e lasciare un po’ di spazio per lo swap
  5. Creare una partizione estesta con una partizione logica da usare come swap

Bene, procediamo.

ATTENZIONE: Benché la procedura sia semplice e collaudata, è molto facile fare danni. Consiglio vivamente uno snapshop da ripristinare all’occorrenza.

~# fdisk /dev/sda
Command (m for help): p
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x42e4aa75

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048 32088063 32086016 15,3G 83 Linux
/dev/sda2       32090110 33552383  1462274  714M  5 Extended
/dev/sda5       32090112 33552383  1462272  714M 82 Linux swap / Solaris

Command (m for help): d
Partition number (1-5): 1
 
Command (m for help): d
Partition number (1-5): 2
 
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1):
Using default value 1
First sector (2048-83886079, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-83886079, default 83886079): 83886079
 
Command (m for help): p

Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x42e4aa75
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    83886079    82421760   83  Linux
 
Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): e
Partition number (1-4, default 2): 2
First sector (82423808-83886079, default 82423808):
Using default value 82423808
Last sector, +sectors or +size{K,M,G} (82423809-83886079, default 83886079):
Using default value 83886079
 
Command (m for help): p
 
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x42e4aa75
 
Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1           2048 82423807 82421760 39,3G 83 Linux
/dev/sda2       82423808 83886079  1462272  714M  5 Extended
 
Command (m for help): n
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (82425856-83886079, default 82425856):
Using default value 82425856
Last sector, +sectors or +size{K,M,G} (82425857-83886079, default 83886079):
Using default value 83886079
 
Command (m for help): p
 
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x42e4aa75
 
/dev/sda1           2048 82423807 82421760 39,3G 83 Linux
/dev/sda2       82423808 83886079  1462272  714M  5 Extended
/dev/sda5       82425856 83886079  1460224  713M 83  Linux
 
Command (m for help): t
Partition number (1-5): 5
 
Hex code (type L to list codes): 82
Changed system type of partition 5 to 82 (Linux swap / Solaris)
 
Command (m for help): p
 
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x42e4aa75

/dev/sda1           2048 82423807 82421760 39,3G 83 Linux
/dev/sda2       82423808 83886079  1462272  714M  5 Extended
/dev/sda5       82425856 83886079  1460224  713M 82 Linux swap / Solaris

Command (m for help): w
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
 
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

~# reboot

Ora passiamo all’estensione del File System Ext4.

~# df -h
File system                 Dim. Usati Dispon. Uso% Montato su
/dev/sda1                    15G  3,5G     11G  25% /
udev                         10M     0     10M   0% /dev
tmpfs                       3,0G  8,7M    3,0G   1% /run

~# resize2fs /dev/sda1
resize2fs 1.42.5 (29-Jul-2012)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 6, new_desc_blocks = 16
The filesystem on /dev/sda1 is now 82421760 blocks long.

~# df -h
File system                  Dim. Usati Dispon. Uso% Montato su
/dev/sda1                    39G  3,5G     35G  10% /
udev                         10M     0     10M   0% /dev
tmpfs                       3,0G  8,7M    3,0G   1% /run

Bene, per concludere formattiamo lo swap:

~# mkswap /dev/sda5
Setting up swapspace version 1, size = 8384508 KiB
no label, UUID=141d401a-b49d-4a96-9b85-c130cb0de40a
~# swapon -a

e poi ricordiamoci di modificare il file /etc/fstab per usare UUID=141d401a-b49d-4a96-9b85-c130cb0de40a (ad esempio nel nostro caso) per il corretto montaggio in fase di boot.

Nota: se vi siete persi l’UUID della partizione /dev/sda5, basterà digitare:

lsblk -dno UUID /dev/sda5

Nota GNU/Debian Stretch:
Ho effettuato la procedura su una nuova GNU/Debian Stretch ed al riavvio ha iniziato a visualizzare una serie di righe come segue, rallentando la procedura:

…
blk_update_request: I/O error, dev fd0, sector 0
…

In questo caso, basta eseguire un:

update-initramfs -u

Se per caso viene visualizzato un messaggio tipo:

W: initramfs-tools configuration sets RESUME=UUID=XXXXXXXXXX
W: but no matching swap device is available.

bisogna commentare la riga nel file /etc/initramfs-tools/conf.d/resume e poi rieseguire:

update-initramfs -u

ed infine riavviare.

enjoy!

Ti interessa acquistare un dominio a prezzi ultraconvenienti? clicca qui

Se hai trovato utili le informazioni su questo blog,
Fai una donazione!
Clicca sul bottone qui sotto o almeno clicca sul banner pubblicitario 🙂



Commenta