How to increase disk size of VM in VirtualBox and Vagrant

## The problem

I have a VM with default disk size set to 10Gb, it's too small after running some days, and I have to increase its' size to continue to work in it.

## What I have tried

I'm using Vagrant indeed, but not directly operate VirtualBox.

So first I tried `vagrant-disksize` plugin, with this:

```toml
  config.disksize.size = '30GB'
```

But failed to increase the disk size: 

```
==> default: Running 'pre-boot' VM customizations...
There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.

Command: ["modifymedium", "/Users/bin/VirtualBox VMs/vmm_default_1580704342870_60830/ubuntu-xenial-16.04-cloudimg.vmdk", "--resize", "30720"]

Stderr: 0%...
Progress state: VBOX_E_NOT_SUPPORTED
VBoxManage: error: Failed to resize medium
VBoxManage: error: Resizing to new size 32212254720 is not yet supported for medium '/Users/bin/VirtualBox VMs/vmm_default_1580704342870_60830/ubuntu-xenial-16.04-cloudimg.vmdk'
VBoxManage: error: Details: code VBOX_E_NOT_SUPPORTED (0x80bb0009), component MediumWrap, interface IMedium
VBoxManage: error: Context: "RTEXITCODE handleModifyMedium(HandlerArg *)" at line 816 of file VBoxManageDisk.cpp
```

After that I also tried using `VBoxManage modifyhd 49ae1c84-ceba-44c3-8ad3-8299474d89dd --resize 20480`, but failed too, with the same error messages:

```
0%...
Progress state: VBOX_E_NOT_SUPPORTED
VBoxManage: error: Failed to resize medium
VBoxManage: error: Resizing to new size 21474836480 is not yet supported for medium '/Users/bin/VirtualBox VMs/vmm_default_1580704342870_60830/ubuntu-xenial-16.04-cloudimg.vmdk'
VBoxManage: error: Details: code VBOX_E_NOT_SUPPORTED (0x80bb0009), component MediumWrap, interface IMedium
VBoxManage: error: Context: "RTEXITCODE handleModifyMedium(HandlerArg *)" at line 816 of file VBoxManageDisk.cpp
```

## The solution

So I had to do more research using Google, and find the right solution. The reason is that disk with `vmdk` format dose not support change disk size, first you should convert it to `vdi` format and then change the disk size(but only support for increasing), and finally convert it back to `vmdk` formt.

Here are all steps.

### Convert `vmdk` to `vdi` disk (by clone new disk)

```shell

$ VBoxManage clonehd "/Users/bin/VirtualBox VMs/vmm_default_1580704342870_60830/ubuntu-xenial-16.04-cloudimg.vmdk" "/Users/bin/VirtualBox VMs/vmm_default_1580704342870_60830/ubuntu-xenial-16.04-cloudimg.vdi" --format vdi
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone medium created in format 'vdi'. UUID: 72691a55-83e3-4f65-846b-0fec2c6599db
```

### Resize `vdi` disk

```shell

$ VBoxManage modifyhd "/Users/bin/VirtualBox VMs/vmm_default_1580704342870_60830/ubuntu-xenial-16.04-cloudimg.vdi" --resize 20480
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
```

### Convert `vdi` to `vmdk` disk (by clone new disk)

```shell

$ VBoxManage clonehd "/Users/bin/VirtualBox VMs/vmm_default_1580704342870_60830/ubuntu-xenial-16.04-cloudimg.vdi" "/Users/bin/VirtualBox VMs/vmm_default_1580704342870_60830/ubuntu-xenial-16.04-cloudimg-new.vmdk" --format vmdk
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone medium created in format 'vmdk'. UUID: 3ae8ed1e-d0fb-438a-8140-80625a043f81
```

### Confirm new disk size

Using `VBoxManage list hdds` command(only showing new created disk):

```shell
UUID:           3ae8ed1e-d0fb-438a-8140-80625a043f81
Parent UUID:    base
State:          locked write
Type:           normal (base)
Location:       /Users/bin/VirtualBox VMs/vmm_default_1580704342870_60830/ubuntu-xenial-16.04-cloudimg-new.vmdk
Storage format: vmdk
Capacity:       20480 MBytes
Encryption:     disabled

```

### Attach new created disk to VM

In VirtualBox main window, select the VM -> Settings -> Storage -> Disk, you will see something like this:

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2179/b202a782-a24f-a031-e5e5-4256871e28ed.png)

Click hard disk icon in the `Hard Disk` item, then click "Choose a disk file" to chose the new vmdk file, finally click Ok to save the settings.

After that using `vagrant up`,  after VM booted, ssh to it and check the disk size in VM:

```shell

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        20G  9.5G  9.9G  50% /
```

That's all.


## References

- [VirtualBox - 仮想ディスク（vmdkファイル）のサイズを変更（リサイズ）](https://pc-karuma.net/virtualbox-resize-vmdk-file/)
- [VirtualBox - 仮想マシンのストレージ（ディスク）を変更](https://pc-karuma.net/virtualbox-change-storage-disk/)