Resize Guest Disk Virtual Machine

Ringkasan langkah:

  1. Pastikan lokasi disk image yang akan diperbesar kapasitasnya
  2. Tentukan cara yang akan digunakan untuk memperbesar VM nya
    • Lihat informasi partisi pada disk image,
  3. Rename disk image lama, buat Disk image baru dengan ukuran yang diinginkan
  4. Buat disk baru
  5. Jalankan perintah virt-resize

1. Informasi tentang disk image

# virsh dumpxml <GuestName>

contoh:

# virsh dumpxml Digitos_U22_5
<domain type='kvm'>
  <name>Digitos_U22_5</name>
  <uuid>0d737f69-3200-440f-8117-8506bacde339</uuid>
  <metadata xmlns:libosinfo="http://libosinfo.org/xmlns/libvirt/domain/1.0" xmlns:cockpit_machines="https://github.com/cockpit-project/cockpit-machines">
    <libosinfo:libosinfo>
      <libosinfo:os id="http://ubuntu.com/ubuntu/22.04"/>
    </libosinfo:libosinfo>
    <cockpit_machines:data>
      <cockpit_machines:has_install_phase>false</cockpit_machines:has_install_phase>
      <cockpit_machines:install_source_type>file</cockpit_machines:install_source_type>
      <cockpit_machines:install_source>/server_images/ubuntu-22.04.3-live-server-amd64.iso</cockpit_machines:install_source>
      <cockpit_machines:os_variant>ubuntu22.04</cockpit_machines:os_variant>
    </cockpit_machines:data>
  </metadata>
  <memory unit='KiB'>2097152</memory>
  <currentMemory unit='KiB'>2097152</currentMemory>
  <vcpu placement='static'>2</vcpu>
  <os>
    <type arch='x86_64' machine='pc-q35-rhel8.6.0'>hvm</type>
  </os>
  <features>
    <acpi/>
    <apic/>
    <vmport state='off'/>
  </features>
  <cpu mode='host-model' check='partial'/>
  <clock offset='utc'>
    <timer name='rtc' tickpolicy='catchup'/>
    <timer name='pit' tickpolicy='delay'/>
    <timer name='hpet' present='no'/>
  </clock>
  <on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <pm>
    <suspend-to-mem enabled='no'/>
    <suspend-to-disk enabled='no'/>
  </pm>
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='block' device='disk'>
      <driver name='qemu' type='raw' cache='none' io='native'/>
      <source dev='/dev/vmdiskpool/vmdisk5'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x04' slot='0x00' function='0x0'/>
    </disk>
...

Terlihat bahwa disk image ada di : /dev/vmdiskpool/vmdisk5

2. Lihat informasi partisi pada disk image

# virt-df -h

Misalnya :

# virt-df -h -a /dev/vmdiskpool/vmdisk5 
Filesystem                                Size       Used  Available  Use%
vmdisk5-100g:/dev/sda2                    1.7G       130M       1.5G    8%
vmdisk5-100g:/dev/ubuntu-vg/ubuntu-lv
                                          8.0G       4.2G       3.4G   53%

3. Rename disk image

lvrename /dev/vmdiskpool/vmdisk5 /dev/vmdiskpool/vmdisk5.backup

4. Buat Disk baru

# lvcreate -L <new-size>G -n <nama-disk> <disk-pool>/dev/vg_guests

contoh:

# lvcreate -L 100G -n vmdisk5 /dev/vmdiskpool

5. Resize partisi (optional) dan salin partisi lama ke disk image baru

Bagi yang belum terinstall virt-resize, untuk pengguna Ubuntu, Debian dapat memasangnya sbb:

# apt-get install libguestfs-tools

Berikut penggunaannya:

# virt-resize /dev/vmdiskpool/vmdisk5-backup /dev/vmdiskpool/vmdisk5 --resize /dev/sda2=1.8G  --expand /dev/sda3 --LV-expand /dev/ubuntu-vg/ubuntu-lv

Output nya kira-kira akan seperti ini:

[   0.0] Examining /dev/vmdiskpool/vmdisk5-backup
**********

Summary of changes:

/dev/sda1: This partition will be left alone.

/dev/sda2: This partition will be resized from 1.8G to 1.8G.  The 
filesystem ext4 on /dev/sda2 will be expanded using the ‘resize2fs’ 
method.

/dev/sda3: This partition will be resized from 8.2G to 98.2G.  The LVM PV 
on /dev/sda3 will be expanded using the ‘pvresize’ method.

/dev/ubuntu-vg/ubuntu-lv: This logical volume will be expanded to maximum 
size.  The filesystem ext4 on /dev/ubuntu-vg/ubuntu-lv will be expanded 
using the ‘resize2fs’ method.

**********
[   3.7] Setting up initial partition table on /dev/vmdiskpool/vmdisk5
[  14.9] Copying /dev/sda1
[  14.9] Copying /dev/sda2
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
[  18.7] Copying /dev/sda3
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
[  46.2] Expanding /dev/sda2 using the ‘resize2fs’ method
[  46.3] Expanding /dev/sda3 using the ‘pvresize’ method
[  46.3] Expanding /dev/ubuntu-vg/ubuntu-lv using the ‘resize2fs’ method

Resize operation completed with no errors.  Before deleting the old disk, 
carefully check that the resized disk boots and works correctly.

Reference: