LoginSignup
9
5

More than 5 years have passed since last update.

Vagrant 事始め 02 - 仮想マシンを起動する

Last updated at Posted at 2018-04-10

前回からの続きです。

Vagrant box を探す

上記のページから利用した Vagrant box を探します。まだこの時点では、Vagrant box はダウンロードしません。
利用したい Vagrant box を見つけたら、box 名を控えておきます。

box 名と明示されていませんが、下図にある

  • ubuntu/trusty64
  • laravel/homestead
  • centos/7
  • debian/jessie64

などが box 名になります。

2018-04-10_12h15_28.png

Vagrant box を起動する

次の手順で、Vagrant の設定ファイルを生成して、Vagrant box のダウンロードから起動までを実行してみます。

Vagrant の設定ファイルを生成する

Vagrant の設定ファイルは任意の場所に生成することができます。今回は Windows 10 で C ドライブに生成してみます。

まずは設定ファイルを配置するディレクトリを作成します。ディレクトリ名は任意で命名できますが、どういう仮想環境を使うかわかるような名称がいいと思います。
今回は centos/7 という Vagrant box を利用するので、次のようにディレクトリを作成します。そして、作成したディレクトリ内に移動します。

> c:
> mkdir Vagrant\centos7 & cd Vagrant\centos7

そして、上記にある Vagrant box 名を指定して、Vagrant の設定ファイルを生成します。

> vagrant init centos/7
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

Vagrant のコマンドを実行した結果、vagrant up すれば仮想環境を起動できるとメッセージが表示されます。仮想環境を実行する前に、生成された設定ファイルを確認します。

下記の設定ファイルは、生成された Vagrantfile からコメントされている行を省いたものです。内容はとてもシンプルですが、これだけだと、運用に不便なので、後に必要な設定を入力します。

Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
end

仮想マシンを起動する

まずは上記の設定のままで、Vagrant box を起動してみます。

初めての起動時は Vagrant box をダウンロードして、起動後に Virtualbox Guest Additions をインストールするため、処理が完了するまでに少し時間がかかります。

> vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' is up to date...
==> default: Setting the name of the VM: centos7_default_1523336654307_34647
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...

...

Copy iso file C:\Program Files\Oracle\VirtualBox\VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso
Mounting Virtualbox Guest Additions ISO to: /mnt
mount: /dev/loop0 is write-protected, mounting read-only
Installing Virtualbox Guest Additions 5.2.6 - guest version is unknown
Verifying archive integrity...

仮想マシンが起動しない (1)

非英語圏で Windows 10 Fall Creators Update が適用された OS で発生するようです。

> vagrant up
...

    default: Creating and registering the VM...
An error occurred while executing a PowerShell script. This error
is shown below. Please read the error message and see if this is
a configuration error with your system. If it is not, then please
report a bug.

Script: import_vm_xml.ps1
Error:

Hyper-V\New-VM : Hyper-V で、"譌「螳壹・繧ケ繧、繝・メ" という名前の仮想スイッチが見つかりませんでした。
発生場所 C:\HashiCorp\Vagrant\embedded\gems\2.0.4\gems\vagrant-2.0.4\plugins\providers\hyperv\scripts\import_vm_xml.ps1
:127 文字:7
+ $vm = Hyper-V\New-VM @vm_params
+       ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-VM]、VirtualizationException
    + FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.NewVM

解決方法 (一時しのぎ) の方法が記載されていたので、「Hyper-VでVagrant upできない人はこれを見てみて」を参考にして対応しました。

仮想マシンが起動しない (2)

uninitialized constant VagrantPlugins::ProviderVirtualBox::Provider (NameError) は Virtualbox がインストールされていないと、エラーが発生します。
Virtualbox をインストールしましょう。

> vagrant vbguest --status
C:/Users/yourname/.vagrant.d/gems/2.4.4/gems/vagrant-vbguest-0.15.1/lib/vagrant-vbguest/vagrant_compat/vagrant_1_1/command.rb:19:in `check_runable_on': uninitialized constant VagrantPlugins::ProviderVirtualBox::Provider (NameError)
        from C:/Users/gecko/.vagrant.d/gems/2.4.4/gems/vagrant-vbguest-0.15.1/lib/vagrant-vbguest/command.rb:85:in `execute_on_vm'

Vagrant box の保存場所

Windows の場合、Vagrant box は C:\Users\user.name\VirtualBox VMs 下に保存されます。

> dir C:\Users\user.name\VirtualBox VMs\centos7
centos-7-1-1.x86_64.vmdk
centos7_default_1523336654307_34647.vbox
centos7_default_1523336654307_34647.vbox-prev
Logs/

仮想マシンを停止する

いったん仮想マシンを停止します。

> vagrant halt
==> default: Attempting graceful shutdown of VM...

これで仮想マシンは停止しました。

9
5
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
9
5