2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Vagrantで既存のboxのHDDの容量を拡張したものを作ってみる

Last updated at Posted at 2016-09-01

概要

ubuntuのboxは、デフォルト40Gのものだったので、これを80Gにしたかった

試した環境は

ソフトウェア バージョン
Virtualbox 5.1.4
Vagrant 1.8.5

Vagrant 1.8.5 に関して

どうも、sshがいかないなどの問題があった。
調べたら問題があるようだった。

上のページにあるこれを適用したら、
問題は解決したようだ。

Vagrant 1.8.6 を待つのも良いかもしれない

結論

以前簡単にできたと思っていたが、

vagrantのバージョンが、1.7からは、少し工夫が必要だった。

vagrant init opscode-ubuntu-16.04

Vagrant 1.7以降では、以下の設定が必要らしい。Vagrantfileに記述する。

config.ssh.insert_key = false

起動してみる

vagrant up

この状態では

vagrant@vagrant:~$  date
Thu Sep  1 07:30:02 UTC 2016
vagrant@vagrant:~$  ls -lar .ssh
total 12
-rw------- 1 vagrant root     409 Apr 29 01:58 authorized_keys
drwxr-xr-x 4 vagrant vagrant 4096 Apr 29 01:58 ..
drwx------ 2 vagrant root    4096 Apr 29 01:58 .

authorized_keysの日付が昔。

書き換えない場合は、現在の日付になる。
(Vagrantが新しいペアをつくってくれる)

exitして一旦停止する

vagrant halt

VirtualBoxのディレクトリの、対象となるディレクトリに移動する。

vmdkからvdiに変換

VBoxManage clonehd ubuntu-16.04-amd64-disk1.vmdk ubuntu-16.04-amd64-disk1.vdi \
--format vdi

vdiを80Gにリサイズ

VBoxManage modifyhd ubuntu-16.04-amd64-disk1.vdi --resize 81920

vmdkにしようとするも、エラーになる

VBoxManage clonehd ubuntu-16.04-amd64-disk1.vdi ubuntu-16.04-amd64-disk1.vmdk \
--format vmdk

ログ

0%...
Progress state: VBOX_E_FILE_ERROR
VBoxManage: error: Failed to clone medium
VBoxManage: error: Could not create the clone medium '/home/manabu/VirtualBox VMs/ubuntu1604_80g_default_1472714935331_96198/ubuntu-16.04-amd64-disk1.vmdk'.
VBoxManage: error: VMDK: could not create new file '/home/manabu/VirtualBox VMs/ubuntu1604_80g_default_1472714935331_96198/ubuntu-16.04-amd64-disk1.vmdk' (VERR_ALREADY_EXISTS)
VBoxManage: error: Details: code VBOX_E_FILE_ERROR (0x80bb0004), component MediumWrap, interface IMedium
VBoxManage: error: Context: "RTEXITCODE handleCloneMedium(HandlerArg*)" at line 928 of file VBoxManageDisk.cpp

確認する

VBoxManage showvminfo ubuntu1604_80g_default_1472714935331_96198

内容のうち必要なところ

Storage Controller Name (1):            SATA Controller
Storage Controller Type (1):            IntelAhci
Storage Controller Instance Number (1): 0
Storage Controller Max Port Count (1):  30
Storage Controller Port Count (1):      1
Storage Controller Bootable (1):        on
SATA Controller (0, 0): /home/manabu/VirtualBox VMs/ubuntu1604_80g_default_1472714935331_96198/ubuntu-16.04-amd64-disk1.vmdk (UUID: e3af0a88-1650-4ace-9e09-1c8c03993466)

hddをdetachする。
しかしそのようなコマンドはないので、attach noneする

VBoxManage storageattach ubuntu1604_80g_default_1472714935331_96198 \
 --storagectl 'SATA Controller' --port 0 --device 0  --medium none

vmdkにコンバートするがエラーになる

rm ubuntu-16.04-amd64-disk1.vmdk
VBoxManage clonehd ubuntu-16.04-amd64-disk1.vdi ubuntu-16.04-amd64-disk1.vmdk \
--format vmdk

ログ

0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...
Progress state: NS_ERROR_INVALID_ARG
VBoxManage: error: Failed to clone medium
VBoxManage: error: Cannot register the hard disk '/home/manabu/VirtualBox VMs/ubuntu1604_80g_default_1472714935331_96198/ubuntu-16.04-amd64-disk1.vmdk' {d946ef06-4c5c-4260-b8e7-a78076f8a059} because a hard disk '/home/manabu/VirtualBox VMs/ubuntu1604_80g_default_1472714935331_96198/ubuntu-16.04-amd64-disk1.vmdk' with UUID {e3af0a88-1650-4ace-9e09-1c8c03993466} already exists
VBoxManage: error: Details: code NS_ERROR_INVALID_ARG (0x80070057), component VirtualBoxWrap, interface IVirtualBox
VBoxManage: error: Context: "RTEXITCODE handleCloneMedium(HandlerArg*)" at line 928 of file VBoxManageDisk.cpp

まだディスクの情報が残っているということなので
以前のディスクをVirtualboxの管理から外す

VBoxManage closemedium disk e3af0a88-1650-4ace-9e09-1c8c03993466

もしあれば、vmdkファイルも消しておく

rm ubuntu-16.04-amd64-disk1.vmdk

もう1度vdiからvmdkへクローンをやり直す

VBoxManage clonehd ubuntu-16.04-amd64-disk1.vdi ubuntu-16.04-amd64-disk1.vmdk \
 --format vmdk

ログ

0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone medium created in format 'vmdk'. UUID: 8acb1751-1016-40ef-abbc-2f29d9230282

vmdkを仮想マシンをアタッチ
デタッチする時のヒストリーを編集する場合が多いと思うが、アタッチする時は --type hdd 忘れないこと

VBoxManage storageattach ubuntu1604_80g_default_1472714935331_96198 \
--storagectl 'SATA Controller' --port 0 --device 0 \
--type hdd --medium ubuntu-16.04-amd64-disk1.vmdk

起動してログイン

vagrant up
vagrant ssh

を参考にまずは、パーティションの内容を更新する。

vagrant@vagrant:~$ sudo parted /dev/sda
GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) unit s
(parted) p
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 167772160s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start     End        Size       Type      File system  Flags
 1      2048s     999423s    997376s    primary   ext2         boot
 2      1001470s  83884031s  82882562s  extended
 5      1001472s  83884031s  82882560s  logical                lvm

(parted) resizepart 2
End?  [83884031s]? 167772159
(parted) p
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 167772160s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start     End         Size        Type      File system  Flags
 1      2048s     999423s     997376s     primary   ext2         boot
 2      1001470s  167772159s  166770690s  extended
 5      1001472s  83884031s   82882560s   logical                lvm

(parted) resizepart 5
End?  [83884031s]? 167772159
(parted) p
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 167772160s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start     End         Size        Type      File system  Flags
 1      2048s     999423s     997376s     primary   ext2         boot
 2      1001470s  167772159s  166770690s  extended
 5      1001472s  167772159s  166770688s  logical                lvm

容量を確認、この時点では増えてない

$ sudo pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda5
  VG Name               vagrant-vg
  PV Size               39.52 GiB / not usable 2.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              10117
  Free PE               0
  Allocated PE          10117
  PV UUID               K4BQb1-TeRS-mnvs-mioU-hSe2-S3oO-LW8xMz

サイズ変更

$ sudo pvresize /dev/sda5
  Physical volume "/dev/sda5" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized

増えていることを確認

$ sudo pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda5
  VG Name               vagrant-vg
  PV Size               79.52 GiB / not usable 2.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              20357
  Free PE               10240
  Allocated PE          10117
  PV UUID               K4BQb1-TeRS-mnvs-mioU-hSe2-S3oO-LW8xMz

論理ボリュームを拡張したいので、まずは確認

$ sudo lvscan
  ACTIVE            '/dev/vagrant-vg/root' [39.02 GiB] inherit
  ACTIVE            '/dev/vagrant-vg/swap_1' [512.00 MiB] inherit

論理ボリュームを拡張

$ sudo lvextend -l +100%FREE /dev/vagrant-vg/root
  Size of logical volume vagrant-vg/root changed from 39.02 GiB (9989 extents) to 79.02 GiB (20229 extents).
  Logical volume root successfully resized.

再度拡張されたか確認

sudo lvscan
  ACTIVE            '/dev/vagrant-vg/root' [79.02 GiB] inherit
  ACTIVE            '/dev/vagrant-vg/swap_1' [512.00 MiB] inherit

ファイルシステムをresize。ext4なので、resize2fs

$ sudo resize2fs /dev/vagrant-vg/root
resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/vagrant-vg/root is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 5
The filesystem on /dev/vagrant-vg/root is now 20714496 (4k) blocks long.

ヒストリーを空にする

Vagrant などで、新しいbox を作る時、ヒストリーを空にする

export HISTSIZE=0
history -c
truncate -s 0 ~/.bash_history

shutdown

 sudo shutdown -h now

boxを作成

vagrant package --base "ubuntu1604_80g_default_1472714935331_96198" \
 --output ubuntu1604_80G.box

boxを登録

vagrant box add --name ubuntu1604_80G ubuntu1604_80G.box

適当なディレクトリを作って、新しく作ってみる

vagrant init ubuntu1604_80G
vagrant up
vagrant ssh

一回壊して、

vagrant destroy

鍵をそのままにしておく設定をすることを Vagrantfile にかく

  config.ssh.insert_key=false

起動してsshでログイン

vagrant up
vagrant ssh

ログインした後、.sshの中を確認。古いままである。

$ ls -lar .ssh
total 12
-rw------- 1 vagrant root     409 Apr 29 01:58 authorized_keys

鍵を変更しなかった時のエラー

鍵が変わってしまうと、新しくinitした時に、うまく立ち上がらなかった。
(原因が本当にこれだったかは、きちんとは確認していないが、多分合っている)

test kitchenだとこんな感じのエラーだった

           default: SSH username: vagrant
           default: SSH auth method: private key
           default: Warning: Authentication failure. Retrying...
           default: Warning: Authentication failure. Retrying...
           default: Warning: Authentication failure. Retrying...
           default: Warning: Authentication failure. Retrying...
           default: Warning: Authentication failure. Retrying...
           default: Warning: Authentication failure. Retrying...
           default: Warning: Authentication failure. Retrying...

参考にしたページ

鍵に関して

ディスクの拡張に関して

partedを使っての説明がわかりやすかったです。

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?