2015年9月20日日曜日

Vagrantを使ってBentoからダウンロードしたbox(OSイメージ)の仮想マシンを起動する


Vagrantを使ってBentoからダウンロードしておいたOSイメージの仮想マシンを作ってみましょう。

まずはvagrantにOSイメージを認識させるため、initコマンドを投入します。

vagrant init <OSイメージファイル名>




C:\Users\aa255620\.vagrant.d\boxes>vagrant init opscode-centos-6.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.


initコマンドを実行すると、カレントディレクトリにVagrantfileという設定ファイルが作成されます。




C:\Users\aa255620\.vagrant.d\boxes>dir
 ドライブ C のボリューム ラベルがありません。
 ボリューム シリアル番号は 5CDA-748F です

 C:\Users\aa255620\.vagrant.d\boxes のディレクトリ

2015/09/20  09:05    <DIR>          .
2015/09/20  09:05    <DIR>          ..
2015/09/20  08:56    <DIR>          opscode-centos-6.7
2015/09/20  09:05             3,101 Vagrantfile
               1 個のファイル               3,101 バイト
               3 個のディレクトリ   7,088,521,216 バイトの空き領域

この設定ファイルを変更してみましょう。

ネットワーク接続設定をコメントアウトするだけです。

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"
config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"
config.vm.network "public_network"



仮想マシンを起動してみましょう。

vagrant upコマンドを投入します。

 C:\Users\aa255620\.vagrant.d\boxes>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'opscode-centos-6.7'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: boxes_default_1442708194182_3797
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => C:/Users/aa255620/.vagrant.d/boxes

C:\Users\aa255620\.vagrant.d\boxes>


デフォルトでは作成した仮想マシンへのアクセスはポートフォワーディングによって行われます。

    default: SSH address: 127.0.0.1:2222

IPアドレスを付与したい場合は以下のように設定ファイルを編集する必要があります。

C:\Users\aa255620\.vagrant.d\boxes>notepad Vagrantfile


config.vm.network "private_network", ip: "192.168.0.111"

サンプルがコメントアウトされているので、行をコピーして編集すると簡単で間違いがありません。

 IPアドレスをホストと同じサブネットにしたら警告が出てしまいました。

C:\Users\aa255620\.vagrant.d\boxes>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'opscode-centos-6.7'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: boxes_default_1442708380926_67825
==> default: Clearing any previously set network interfaces...
The specified host network collides with a non-hostonly network!This will cause your specified IP to be inaccessible. Please change
the IP or name of your host only network so that it no longer matches that of
a bridged or non-hostonly network.



ホストと違うサブネットに変更して、再度起動してみた。
config.vm.network "private_network", ip: "172.18.3.11"



C:\Users\aa255620\.vagrant.d\boxes>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => C:/Users/aa255620/.vagrant.d/boxes
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

netstat -rコマンドでルーティングテーブルを確認したところ、172.18.0ネットワークが作成されていました。

IPv4 ルート テーブル
===========================================================================
アクティブ ルート:
ネットワーク宛先        ネットマスク          ゲートウェイ       インターフェイス  メトリック
          0.0.0.0          0.0.0.0      192.168.0.1     192.168.0.12     25
        127.0.0.0        255.0.0.0            リンク上         127.0.0.1    306
        127.0.0.1  255.255.255.255            リンク上         127.0.0.1    306
  127.255.255.255  255.255.255.255            リンク上         127.0.0.1    306
      169.254.0.0      255.255.0.0      192.168.0.8     192.168.0.12     26
      169.254.0.0      255.255.0.0            リンク上      192.168.56.1    306
  169.254.255.255  255.255.255.255            リンク上      192.168.56.1    266
       172.18.0.0    255.255.255.0            リンク上        172.18.0.1    266
       172.18.0.1  255.255.255.255            リンク上        172.18.0.1    266
     172.18.0.255  255.255.255.255            リンク上        172.18.0.1    266
       172.18.3.0    255.255.255.0            リンク上        172.18.3.1    266
       172.18.3.1  255.255.255.255            リンク上        172.18.3.1    266

     172.18.3.255  255.255.255.255            リンク上        172.18.3.1    266
      192.168.0.0    255.255.255.0            リンク上      192.168.0.12    281
     192.168.0.12  255.255.255.255            リンク上      192.168.0.12    281
    192.168.0.255  255.255.255.255            リンク上      192.168.0.12    281
     192.168.56.0    255.255.255.0            リンク上      192.168.56.1    266
     192.168.56.1  255.255.255.255            リンク上      192.168.56.1    266
   192.168.56.255  255.255.255.255            リンク上      192.168.56.1    266
        224.0.0.0        240.0.0.0            リンク上         127.0.0.1    306
        224.0.0.0        240.0.0.0            リンク上      192.168.56.1    266
        224.0.0.0        240.0.0.0            リンク上      192.168.0.12    281
        224.0.0.0        240.0.0.0            リンク上        172.18.0.1    266
        224.0.0.0        240.0.0.0            リンク上        172.18.3.1    266
  255.255.255.255  255.255.255.255            リンク上         127.0.0.1    306
  255.255.255.255  255.255.255.255            リンク上      192.168.56.1    266
  255.255.255.255  255.255.255.255            リンク上      192.168.0.12    281
  255.255.255.255  255.255.255.255            リンク上        172.18.0.1    266
  255.255.255.255  255.255.255.255            リンク上        172.18.3.1    266

===========================================================================

VirtualBoxを起動してみると、一つ稼動マシンが実行中になっていました。



VirtualBoxの仮想マシンコンソールは左側の仮想マシン名をダブルクリックすると開きます。
ログインプロンプトが表示されたら、rootでログインします。パスワードはvagrantです。






0 件のコメント:

コメントを投稿