Vagrantで作成した仮想マシンのディスクを後から拡張する方法メモ - CentOS7編
参考にした手順
こちら https://gist.github.com/christopher-hopper/9755310 に記載されている手順が参考になった。ただし、今回利用したCentOS7のvagrant boxのイメージでは、ファイルシステムがCentOS7のデフォルトであるxfsとなっているため、最後のファイル拡張時には xfs_growf コマンドを利用する。
Vagrant使いなら、おそらくVagrantfile内でかっこよく処理して、最初から任意のサイズのディスクで仮想マシンを構成するんだろうが、ぼくはその方法を探せなかった。
vagrant boxをダウンロードする
vagrant boxは、A list of base boxes for Vagrant - Vagrantbox.es から CentOS-7.1.1503-x86_64-netboot.boxをダウンロードして利用した。
$ vagrant box add centos7 https://github.com/holms/vagrant-centos7-box/releases/download/7.1.1503.001/CentOS-7.1.1503-x86_64-netboot.box
$ mkdir ~/vagrant && cd ~/vagrant
$ vagrant box list
vagrant boxから仮想マシンインスタンスを起動させる。
$ vagrant init centos7
$ vagrant up
vagrant boxのディスク構成
今どきだとディスク容量が約10GBというのは小さい。これは、後から変更することを想定しているんだろうが、最初から変更できる方法は、あるのかもしれないが見つけられなかった。
やりたいこと
仮想マシンのディスクを、任意のサイズに変更したい。デフォルトの構成は以下の通り。
- controller: SATA
- filename: box-disk1.vmdk
- size: 9.90GB
起動したvagrant boxにログインして、実際に確認してみる。
[vagrant@localhost ~]$ sudo fdisk -l
Disk /dev/sda: 10.6 GB, 10632560640 bytes, 20766720 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト
Disk label type: dos
ディスク識別子: 0x000a56e0
デバイス ブート 始点 終点 ブロック Id システム
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 20766719 9870336 8e Linux LVM
Disk /dev/mapper/centos-root: 8996 MB, 8996782080 bytes, 17571840 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト
Disk /dev/mapper/centos-swap: 1065 MB, 1065353216 bytes, 2080768 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト
これを拡張する。
ディスク拡張の流れ
仮想マシンのディスクを拡張する作業の流れは以下の通り。
- 仮想マシンをシャットダウンする
- ディスクのクローン機能を利用してディスクフォーマットをVMDK(box-disk1.vmdk)からVDI形式(box-disk1.vdi)に変更したディスクを作成する
- クローンで作成したVDI形式のディスクをリサイズする
- リサイズしたディスク(box-disk1.vdi)に切り替える
- 仮想マシンを起動する
- 拡張されたディスクにパーティションを追加する
- 仮想マシンを再起動する
- LVMを利用して、VolumeGroupに追加したパーティションを追加する
- ルートファイルシステム用の論理ボリューム領域を拡張する
- xfsファイルシステムを拡張する
では、実際に拡張してみる。まずは仮想マシンを停止する。
$ vagrant halt
==> default: Attempting graceful shutdown of VM...
続いて、仮想マシンの実体があるディレクトリに移動する。
$ cd ~/VirtualBox\ VMs/test_default_1433612417803_41795
$ ll
total 2759840
drwx------ 3 foo staff 102B 6 7 02:40 Logs
-rw------- 1 foo staff 1.3G 6 7 02:49 box-disk1.vmdk
-rw------- 1 foo staff 7.9K 6 7 02:40 test_default_1433612417803_41795.vbox
-rw------- 1 foo staff 7.9K 6 7 02:40 test_default_1433612417803_41795.vbox-prev
続いて、VBoxManageコマンドを利用して、box-disk1.vmdkをVDI形式でクローンする。
$ VBoxManage clonehd box-disk1.vmdk box-disk1.vdi --format VDI
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone hard disk created in format 'VDI'. UUID: 89464f0b-0fa1-49bd-8fd5-ec8105ab6d26
クローンしたファイルを確認する。まだ拡張していないので、サイズは約10GBのまま。
$ VBoxManage showhdinfo box-disk1.vdi
UUID: 89464f0b-0fa1-49bd-8fd5-ec8105ab6d26
Parent UUID: base
State: created
Type: normal (base)
Location: /Users/foo/VirtualBox VMs/test_default_1433612417803_41795/box-disk1.vdi
Storage format: VDI
Format variant: dynamic default
Capacity: 10140 MBytes
Size on disk: 1369 MBytes
VDIファイル(box-disk1.vdi)をリサイズする。サイズの単位はMB。ここでは30GBに変更する。
$ VBoxManage modifyhd box-disk1.vdi --resize 30720
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
サイズが変更されたことを確認する。
$ VBoxManage showhdinfo box-disk1.vdi
UUID: 89464f0b-0fa1-49bd-8fd5-ec8105ab6d26
Parent UUID: base
State: created
Type: normal (base)
Location: /Users/foo/VirtualBox VMs/test_default_1433612417803_41795/box-disk1.vdi
Storage format: VDI
Format variant: dynamic default
Capacity: 30720 MBytes
Size on disk: 1369 MBytes
ディスクをbox-disk1.vmdkからbox-disk1.vdiに切り替える。VBoxManageコマンドを利用して、コントローラ削除->コントローラ追加->ディスク接続という流れで作業を実行する。
$ VBoxManage list vms
"test_default_1433612417803_41795" {dce4105d-4864-48d7-a18e-d60988617beb}
$ VBoxManage storagectl dce4105d-4864-48d7-a18e-d60988617beb --name "SATA Controller" --remove
$ VBoxManage storagectl dce4105d-4864-48d7-a18e-d60988617beb --name "SATA Controller" --add sata
$ VBoxManage storageattach dce4105d-4864-48d7-a18e-d60988617beb --storagectl "SATA Controller" --type hdd --medium box-disk1.vdi --port 0
30GBになっている。続いて仮想マシンを起動する。
$ cd ~/vagrant
$ vagrant up
仮想マシンにログインして、ディスクサイズが30GBになっていることを確認する。
$ vagrant ssh
[vagrant@localhost ~]$ sudo -i
[root@localhost ~]# fdisk -l
Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト
Disk label type: dos
ディスク識別子: 0x000a56e0
デバイス ブート 始点 終点 ブロック Id システム
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 20766719 9870336 8e Linux LVM
Disk /dev/mapper/centos-root: 8996 MB, 8996782080 bytes, 17571840 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト
Disk /dev/mapper/centos-swap: 1065 MB, 1065353216 bytes, 2080768 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト
基本パーティション3(TypeはLVM)を作成する。
[root@localhost ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
コマンド (m でヘルプ): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
パーティション番号 (3,4, default 3): 3
最初 sector (20766720-62914559, 初期値 20766720):
初期値 20766720 を使います
Last sector, +sectors or +size{K,M,G} (20766720-62914559, 初期値 62914559):
初期値 62914559 を使います
Partition 3 of type Linux and of size 20.1 GiB is set
コマンド (m でヘルプ): t
パーティション番号 (1-3, default 3): 3
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
コマンド (m でヘルプ): p
Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト
Disk label type: dos
ディスク識別子: 0x000a56e0
デバイス ブート 始点 終点 ブロック Id システム
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 20766719 9870336 8e Linux LVM
/dev/sda3 20766720 62914559 21073920 8e Linux LVM
コマンド (m でヘルプ): w
パーティションテーブルは変更されました!
ioctl() を呼び出してパーティションテーブルを再読込みします。
WARNING: Re-reading the partition table failed with error 16: デバイスもしくはリソースがビジー状態です.
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)
ディスクを同期しています。
作成したパーティションを利用するには再起動が必要であるため、仮想マシンを再起動する。
[root@localhost ~]# reboot
Connection to 127.0.0.1 closed by remote host.
Connection to 127.0.0.1 closed.
$ vagrant ssh
Last login: Sat Jun 6 18:23:14 2015 from 10.0.2.2
Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$ sudo -i
[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size 9.41 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 2409
Free PE 10
Allocated PE 2399
PV UUID 5tuLzr-3IXr-pecY-i6yv-JNgy-OeRa-7WrIML
追加した基本パーティション(/dev/sda3)をVolumeGroup(centos)に加える。
[root@localhost ~]# pvcreate /dev/sda3
[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size 9.41 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 2409
Free PE 10
Allocated PE 2399
PV UUID 5tuLzr-3IXr-pecY-i6yv-JNgy-OeRa-7WrIML
"/dev/sda3" is a new physical volume of "20.10 GiB"
--- NEW Physical volume ---
PV Name /dev/sda3
VG Name
PV Size 20.10 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID QotznM-3nYG-nCyj-UUEs-rowi-VFNl-liv55I
[root@localhost ~]# vgextend centos /dev/sda3
Volume group "centos" successfully extended
ルートファイルシステムを提供している論理ボリュームのデバイス名を確認する。
[root@localhost ~]# df
ファイルシス 1K-ブロック 使用 使用可 使用% マウント位置
/dev/mapper/centos-root 8775680 1063956 7711724 13% /
devtmpfs 227912 0 227912 0% /dev
tmpfs 234316 0 234316 0% /dev/shm
tmpfs 234316 4376 229940 2% /run
tmpfs 234316 0 234316 0% /sys/fs/cgroup
/dev/sda1 508588 84776 423812 17% /boot
ルートファイルシステム用の論理ボリュームデバイス( /dev/mapper/centos-root)のサイズを拡張する。
[root@localhost ~]# lvextend -l +100%FREE /dev/mapper/centos-root
Size of logical volume centos/root changed from 8.38 GiB (2145 extents) to 28.51 GiB (7299 extents).
Logical volume root successfully resized
この時点では、まだxfsファイルシステムとしては拡張されていない。
[root@localhost ~]# df -h
ファイルシス サイズ 使用 残り 使用% マウント位置
/dev/mapper/centos-root 8.4G 1.1G 7.4G 13% /
devtmpfs 223M 0 223M 0% /dev
tmpfs 229M 0 229M 0% /dev/shm
tmpfs 229M 4.3M 225M 2% /run
tmpfs 229M 0 229M 0% /sys/fs/cgroup
/dev/sda1 497M 83M 414M 17% /boot
最後にxfsファイルシステムを拡張する。
[root@localhost ~]# xfs_growfs /
meta-data=/dev/mapper/centos-root isize=256 agcount=4, agsize=549120 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=2196480, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 2196480 to 7474176
[root@localhost ~]# df -h
ファイルシス サイズ 使用 残り 使用% マウント位置
/dev/mapper/centos-root 29G 1.1G 28G 4% /
devtmpfs 223M 0 223M 0% /dev
tmpfs 229M 0 229M 0% /dev/shm
tmpfs 229M 4.3M 225M 2% /run
tmpfs 229M 0 229M 0% /sys/fs/cgroup
/dev/sda1 497M 83M 414M 17% /boot
これで完了。 VMDKファイル(box-disk1.vmdk)は、もう使わないと思うので、削除しておいてもいいかも。