CLOVER🍀

That was when it all began.

vagrant-libvirtで仮想マシンのディスクサイズを設定する

これは、なにをしたくて書いたもの?

自分はVagrantではvagrant-libvirtをProviderとして使っているのですが、ディスクのサイズを大きくしたいなと思うことが
時々あったのでやり方を調べてみました。

結果ですが、最初の起動時であれば大きくできるようです。

machine_virtual_size

vagrant-libvirtでディスクサイズを大きくするには、machine_virtual_sizeという設定を使います。

machine_virtual_size - Sets the disk size in GB for the machine overriding the default specified in the box. Allows boxes to defined with a minimal size disk by default and to be grown to a larger size at creation time. Will ignore sizes smaller than the size specified by the box metadata. Note that currently there is no support for automatically resizing the filesystem to take advantage of the larger disk.

Configuration / Domain Specific Options

なのですが、これが有効なのは最初の起動時だけで後から大きくすることはできなさそうです(ムリにやろうとすると
仮想マシンが起動しなくなりました)。

とはいえ、知っておけば利用できるので今回は試してみます。

環境

今回の環境はこちら。

$ vagrant version
Installed Version: 2.4.3
Latest Version: 2.4.3

You're running an up-to-date version of Vagrant!


$ vagrant plugin list
vagrant-libvirt (0.12.2, global)

vagrant-libvirtでディスクサイズを設定する

ひとまず、Vagrant仮想マシンを起動してSSHで入ってみます。

$ vagrant init cloud-image/ubuntu-24.04
$ vagrant up
$ vagrant ssh

このBoxは、ディスクサイズが9G弱です。

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
tmpfs            46M  972K   45M   3% /run
/dev/vda1       8.7G  1.5G  7.2G  17% /
tmpfs           230M     0  230M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/vda16      881M   61M  758M   8% /boot
/dev/vda15      105M  6.1M   99M   6% /boot/efi
tmpfs            46M   12K   46M   1% /run/user/1000

1度仮想マシンを破棄。

$ vagrant destroy -f

次にVagrantfileを修正して、config.vm.providerを以下のように設定します。今回はディスクサイズを40Gにしました。

  config.vm.provider "libvirt" do |libvirt|
    libvirt.machine_virtual_size = 40
  end

仮想マシンを作成し、SSHログイン。

$ vagrant up
$ vagrant ssh

すると、ディスクサイズが増えました。

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
tmpfs            46M  972K   45M   3% /run
/dev/vda1        38G  1.5G   37G   4% /
tmpfs           230M     0  230M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/vda16      881M   61M  758M   8% /boot
/dev/vda15      105M  6.1M   99M   6% /boot/efi
tmpfs            46M   12K   46M   1% /run/user/1000

よさそうですね。

ちなみに、仮想マシンを起動した後にmachine_virtual_sizeを設定して仮想マシンを再起動しても、拡張したディスクを
認識しません(machine_virtual_sizeを指定する前と同じままです)。

それではとsgdiskでソートしてgrowpartで拡張しようとしたり、cfdiskで拡張しようとした場合、コマンド実行後に仮想マシン
再起動すると、なぜか起動しなくなります…。

==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 192.168.121.36:22
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Host unreachable. Retrying...
    default: Warning: Host unreachable. Retrying...
    default: Warning: Host unreachable. Retrying...
    default: Warning: Host unreachable. Retrying...
    default: Warning: Host unreachable. Retrying...

この時、ゲストOSがCPUを大きく使っているのですが、原因不明でした…。

おわりに

vagrant-libvirt仮想マシンのディスクサイズを設定してみました。

途中で大きくできないのはちょっと残念なのですが、できないよりは全然いいので今後は仮想マシンのディスクサイズを
大きくしそうな時はあらかじめmachine_virtual_sizeを大きく取っておくことにしましょう。

machine_virtual_sizeを広げるのを忘れていたとしても、仮想マシン間の移行であればなんとかなるでしょう…。