動かざることバグの如し

近づきたいよ 君の理想に

Vagrantのディスク容量を増やす

環境

  • vagrant 2
    • 以前のバージョンでも同様にいける
  • 使用したbox: bento/ubuntu-16.04

仮想ハードディスクの拡張

まずはゲストOS側(仮想側)で確認。デフォルトだと38GがMAXである。(/dev/mapper/vagrant--vg-rootの項目)ログとか貯まるとすぐに溢れそう

vagrant@vagrant:~$ df -h
Filesystem                    Size  Used Avail Use% Mounted on
udev                          465M     0  465M   0% /dev
tmpfs                          97M  3.1M   94M   4% /run
/dev/mapper/vagrant--vg-root   38G  1.4G   35G   4% /
tmpfs                         485M     0  485M   0% /dev/shm
tmpfs                         5.0M     0  5.0M   0% /run/lock
tmpfs                         485M     0  485M   0% /sys/fs/cgroup
/dev/sda1                     472M   57M  391M  13% /boot
vagrant                       4.3T  852G  3.5T  20% /vagrant
tmpfs                          97M     0   97M   0% /run/user/1000

ゲストOSをシャットダウンしてホスト側で作業開始。ゲストOSのディスクを拡張したいが、なんとvmdk形式では拡張できないクソ仕様なので、まずはVBoxManageコマンドでvdi形式に変換する。

$ VBoxManage clonehd ubuntu-16.04-amd64-disk001.vmdk tmp-disk.vdi --format vdi
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone medium created in format 'vdi'. UUID: b84593dc-3ba3-4d2c-9151-b6d80f3bbc7c

でリサイズ。100GBにしたい場合は--resize 100000

$ VBoxManage modifyhd tmp-disk.vdi --resize 100000
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

変換が終わったら再びvmdk形式に戻す。--variant Fixedをつけると固定サイズになって容量は喰うがゲストOSのパフォーマンスは上がる。

$ VBoxManage clonehd tmp-disk.vdi ubuntu-16.04-amd64-disk001_resized.vmdk --format vdi --variant Fixed
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone medium created in format 'vdi'. UUID: 5a3db394-a983-480c-82a2-ac07db2be84b

変換後に表示されたUUIDは後で使うので控えておく(ここでは5a3db394-a983-480c-82a2-ac07db2be84bを指す)

ディスクの入れ替え

次に拡張したディスクを拡張前のディスクと入れ替える作業。まずは対象ゲストOSのUUIDを調べる

$ VBoxManage list vms

ここではUUID=ccbee66b-ff49-4676-a6ba-f2e7e8904d74とする。

ディスクを外して、

$ VBoxManage storageattach ccbee66b-ff49-4676-a6ba-f2e7e8904d74 --storagectl 'SATA Controller' --port 0 --device 0 --type hdd --medium none

つける

$ VBoxManage storageattach ccbee66b-ff49-4676-a6ba-f2e7e8904d74 --storagectl 'SATA Controller' --port 0 --device 0 --type hdd --medium 5a3db394-a983-480c-82a2-ac07db2be84b

これでvagrant upできればおk

パーティションの拡張

なんとdfしてもまだ38GBのまま。。。これでオシマイ!ではなく、ゲストOS側でパーティションの拡張作業が必要

fdiskをつかってまずは既存のパーティションを削除する

# fdisk /dev/sda

Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

pでまずは現状のパーティションを表示

Command (m for help): p
Disk /dev/sda: 97.7 GiB, 104857600000 bytes, 204800000 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: 0xe0816098

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sda1  *       2048   999423   997376  487M 83 Linux
/dev/sda2       1001470 83884031 82882562 39.5G  5 Extended
/dev/sda5       1001472 83884031 82882560 39.5G 8e Linux LVM

ここでは/dev/sda2を削除

Command (m for help): d
Partition number (1,2,5, default 5): 2

Partition 2 has been deleted.

再度表示/dev/sda2にぶら下がっていた/dev/sda5も消えてくれている

Command (m for help): p
Disk /dev/sda: 97.7 GiB, 104857600000 bytes, 204800000 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: 0xe0816098

Device     Boot Start    End Sectors  Size Id Type
/dev/sda1  *     2048 999423  997376  487M 83 Linux

新しいパーティションで/dev/sda2を作成 First sector、Last sectorはエンターキーを押すだけで勝手に最大化してくれる

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): e
Partition number (2-4, default 2): 
First sector (999424-204799999, default 999424): 
Last sector, +sectors or +size{K,M,G,T,P} (999424-204799999, default 204799999): 

Created a new partition 2 of type 'Extended' and of size 97.2 GiB.

/dev/sda5も同様に

Command (m for help): n   
All space for primary partitions is in use.
Adding logical partition 5
First sector (1001472-204799999, default 1001472): 
Last sector, +sectors or +size{K,M,G,T,P} (1001472-204799999, default 204799999): 

Created a new partition 5 of type 'Linux' and of size 97.2 GiB.

確認 ちゃんと元あったパーティションが拡張されている

Command (m for help): p
Disk /dev/sda: 97.7 GiB, 104857600000 bytes, 204800000 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: 0xe0816098

Device     Boot   Start       End   Sectors  Size Id Type
/dev/sda1  *       2048    999423    997376  487M 83 Linux
/dev/sda2        999424 204799999 203800576 97.2G  5 Extended
/dev/sda5       1001472 204799999 203798528 97.2G 83 Linux

wで書き込み

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: 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).

再起動するのでホストOS側でvagrant reloadする

LVMの拡張

なんとこれでもまだ作業は終わっていなく、パーティションレベルの次はLVMレベルでの拡張が必要になる

PVの拡張

root@vagrant:~# pvresize /dev/sda5
  Physical volume "/dev/sda5" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized

LVを最大化!

root@vagrant:~# lvextend -l +100%FREE /dev/mapper/vagrant--vg-root
  Size of logical volume vagrant-vg/root changed from 38.52 GiB (9861 extents) to 96.18 GiB (24621 extents).
  Logical volume root successfully resized.

resize2fsを最後に実行!

root@vagrant:~# resize2fs /dev/mapper/vagrant--vg-root
resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/mapper/vagrant--vg-root is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 7
The filesystem on /dev/mapper/vagrant--vg-root is now 25211904 (4k) blocks long.

確認。/dev/mapper/vagrant--vg-rootがたしかに95GBに増えている。

root@vagrant:~# df -h
Filesystem                    Size  Used Avail Use% Mounted on
udev                          465M     0  465M   0% /dev
tmpfs                          97M  3.1M   94M   4% /run
/dev/mapper/vagrant--vg-root   95G  1.4G   89G   2% /
tmpfs                         485M     0  485M   0% /dev/shm
tmpfs                         5.0M     0  5.0M   0% /run/lock
tmpfs                         485M     0  485M   0% /sys/fs/cgroup
/dev/sda1                     472M   57M  391M  13% /boot
vagrant                       4.3T  952G  3.4T  22% /vagrant
tmpfs                          97M     0   97M   0% /run/user/1000

やったね!