目次

virt-resizeによるディスクサイズ拡張の実行例

ゲストOSはCentOS 7で、LVM使用、ファイルシステムはXFS。 ディスクイメージはqcow2形式で、サイズを8Gから10Gに変更。

実行前のゲストの状態

[root@guest ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 988M     0  988M   0% /dev
tmpfs                   1000M     0 1000M   0% /dev/shm
tmpfs                   1000M  8.5M  992M   1% /run
tmpfs                   1000M     0 1000M   0% /sys/fs/cgroup
/dev/mapper/centos-root  6.2G  1.3G  5.0G  20% /
/dev/vda1               1014M  137M  878M  14% /boot
tmpfs                    200M     0  200M   0% /run/user/0
[root@guest ~]# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <7.00 GiB
  PE Size               4.00 MiB
  Total PE              1791
  Alloc PE / Size       1791 / <7.00 GiB
  Free  PE / Size       0 / 0
  VG UUID               Jq1KXV-oAJ4-QChG-3u3w-Rrwi-GvXH-0UaKhf

[root@guest ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                n3c1Xq-YvEF-8uYb-x5lA-cssY-P8vG-JE3dwm
  LV Write Access        read/write
  LV Creation host, time localhost, 2022-04-29 05:14:00 +0900
  LV Status              available
  # open                 2
  LV Size                820.00 MiB
  Current LE             205
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                g8yMUy-1eYI-BIN2-9khy-7cjA-n8fn-dOUcRa
  LV Write Access        read/write
  LV Creation host, time localhost, 2022-04-29 05:14:00 +0900
  LV Status              available
  # open                 1
  LV Size                <6.20 GiB
  Current LE             1586
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

[root@guest ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/vda2
  VG Name               centos
  PV Size               <7.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              1791
  Free PE               0
  Allocated PE          1791
  PV UUID               U8CR49-1Dmk-gNed-Qb8W-UBem-aUIT-npeWV8

サイズを拡張するLVのLV Pathが必要。

サイズを拡張したディスクイメージを作成

仮想マシンを停止してディスクの状態表示。

[root@kvmhost ~]# virsh shutdown centos7
[root@kvmhost ~]# virsh dumpxml centos7 | xpath /domain/devices/disk/source
Found 1 nodes:
-- NODE --
<source file="/var/lib/libvirt/images/centos7.qcow2" />
[root@kvmhost ~]# virt-filesystems --long --parts --blkdevs -h -a /var/lib/libvirt/images/centos7.qcow2
Name       Type       MBR  Size  Parent
/dev/sda1  partition  83   1.0G  /dev/sda
/dev/sda2  partition  8e   7.0G  /dev/sda
/dev/sda   device     -    8.0G  -

新しいディスクイメージを作成して、サイズを拡張しながらディスクの中身をコピー。

[root@kvmhost ~]# qemu-img create -f qcow2 -o preallocation=metadata /var/lib/libvirt/images/centos7-new.qcow2 10G
Formatting '/var/lib/libvirt/images/centos7-new.qcow2', fmt=qcow2 size=10737418240 encryption=off cluster_size=65536 preallocation='metadata' lazy_refcounts=off
[root@kvmhost ~]# virt-resize --expand /dev/sda2 --LV-expand /dev/centos/root /var/lib/libvirt/images/centos7.qcow2 /var/lib/libvirt/images/centos7-new.qcow2
[   0.0] Examining /var/lib/libvirt/images/centos7.qcow2
**********

Summary of changes:

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

/dev/sda2: This partition will be resized from 7.0G to 9.0G.  The LVM PV on
/dev/sda2 will be expanded using the ‘pvresize’ method.

/dev/centos/root: This logical volume will be expanded to maximum size.
The filesystem xfs on /dev/centos/root will be expanded using the
‘xfs_growfs’ method.

**********
[   5.6] Setting up initial partition table on /var/lib/libvirt/images/centos7-new.qcow2
[   6.2] Copying /dev/sda1
[   9.4] Copying /dev/sda2
[  33.5] Expanding /dev/sda2 using the ‘pvresize’ method
[  33.5] Expanding /dev/centos/root using the ‘xfs_growfs’ method

Resize operation completed with no errors.  Before deleting the old disk,
carefully check that the resized disk boots and works correctly.
[root@kvmhost ~]# virt-filesystems --long --parts --blkdevs -h -a /var/lib/libvirt/images/centos7-new.qcow2
Name       Type       MBR  Size  Parent
/dev/sda1  partition  83   1.0G  /dev/sda
/dev/sda2  partition  8e   9.0G  /dev/sda
/dev/sda   device     -    10G   -

仮想マシンの変更

仮想マシンが使用するディスクイメージを新しい方に変更して起動。

[root@kvmhost ~]# virsh edit centos7
[root@kvmhost ~]# virsh start centos7

変更後のゲストの状態

[root@guest ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 988M     0  988M   0% /dev
tmpfs                   1000M     0 1000M   0% /dev/shm
tmpfs                   1000M  8.5M  992M   1% /run
tmpfs                   1000M     0 1000M   0% /sys/fs/cgroup
/dev/mapper/centos-root  8.2G  1.3G  7.0G  15% /
/dev/vda1               1014M  137M  878M  14% /boot
tmpfs                    200M     0  200M   0% /run/user/0
[root@guest ~]# lvdisplay /dev/centos/root
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                g8yMUy-1eYI-BIN2-9khy-7cjA-n8fn-dOUcRa
  LV Write Access        read/write
  LV Creation host, time localhost, 2022-04-29 05:14:00 +0900
  LV Status              available
  # open                 1
  LV Size                <8.20 GiB
  Current LE             2098
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0