nprogram’s blog

気ままに、プログラミングのトピックについて書いていきます

「Webプログラミングが面白いほどわかる本」を見て仮想環境を作成しました

はじめに

「Webプログラミングが面白いほどわかる本」を見て、仮想環境(Linux OS)の作成したので、その手順を記載します。

本は、こちらです。

https://www.kadokawa.co.jp/product/321712000860

本の通りに、Linux環境構築しようとする

Linux 環境構築時に、長いコマンドを入力する必要があるのですが、コマンド集を用意してくれています。 * GitHub - progedu/commands: 入門コース内で入力が大変なコマンド/URL集

環境構築時には、まずはじめにフォルダーの作成と移動を忘れてはいけません。自分は忘れました。(笑)

  • mkdir %USERPROFILE%\vagrant\ubuntu64_16

  • cd %USERPROFILE%\vagrant\ubuntu64_16

vagrant up時に問題発生 (VBoxManage.exe: error: Details: code E_FAIL (0x80004005))

以下に解決策が記載されていました。 https://www.nnn.ed.nico/questions/7722

(1) Windows10のアップデートをしっかりと行う。
(2) 一度VagrantとVirtualBoxをコントロールパネルのアプリの追加と削除から削除の後、再度教材で指定されたバージョンをインストールし、一度マシン自体を再起動してubuntu64_16フォルダを削除してからやってみてください。
(3) それでもダメであればまたアンインストールして最新のVirtualBoxとVagrantを再インストールの後、再起動してやってみてください。
(4) それでもダメな場合には、Windowsで別な管理者ユーザーを作ってやってみることをおすすめします。Windowsはレジストリ状況がほかのアプリケーションと干渉し正しくアプリケーションが動作しないことが非常に多いため
(5) またそれでもダメな場合にはubuntuの公式サイトからUbuntu16のisoイメージをダウンロードしてVirtualBoxで構築

C:\Users\uv2ut\vagrant\ubuntu64_16>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/xenial64'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: ubuntu64_16_default_1543518701958_70650
Vagrant is currently configured to create VirtualBox synced folders with
the `SharedFoldersEnableSymlinksCreate` option enabled. If the Vagrant
guest is not trusted, you may want to disable this option. For more
information on this option, please refer to the VirtualBox manual:

  https://www.virtualbox.org/manual/ch04.html#sharedfolders

This option can be disabled globally with an environment variable:

  VAGRANT_DISABLE_VBOXSYMLINKCREATE=1

or on a per folder basis within the Vagrantfile:

  config.vm.synced_folder '/host/path', '/guest/path', SharedFoldersEnableSymlinksCreate: false
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.

Command: ["startvm", "7468a4a8-3159-473c-9b37-ea90ff673d8d", "--type", "headless"]

Stderr: VBoxManage.exe: error: The virtual machine 'ubuntu64_16_default_1543518701958_70650' has terminated unexpectedly during startup with exit code 1 (0x1).  More details may be available in 'C:\Users\uv2ut\VirtualBox VMs\ubuntu64_16_default_1543518701958_70650\Logs\VBoxHardening.log'
VBoxManage.exe: error: Details: code E_FAIL (0x80004005), component MachineWrap, interface IMachine

簡単に仮想環境を作れると思っていたのだけど・・・。(´;ω;`)

最新のVirtual Box, Vagrant, Linux OSイメージを使用したいため、すべて用意しなおすことにした

以下の手順で実施しました。

[手順1]モジュールを用意する

最新のVirtual BoxとVagrantとBoxファイルを導入します

  • vagrantのバージョン
    • Vagrant 2.2.1
  • virtual boxのバージョン
    • 5.2.22 r126460 (Qt5.6.2)
  • Linux OS

    • 'ubuntu/trusty64' (v20181103.0.0)
  • パス(URL)を指定してBoxファイルを追加する (ここではubuntu/trusty64を追加する)

    • vagrant box add "ubuntu/trusty64
  • boxファイルの確認
  • vagrant box list
  • ubuntu/trusty64 (virtualbox, 20181103.0.0)

[手順2]Vagrant Fileを編集する

Vagrantfileは、vagrant initを実施したディレクトリ内に作成されます。
デフォルトでは修正前のようになっているので、以下のように修正します。

[修正前]

Vagrant.configure("2") do |config|
  config.vm.box = "base"
end

[修正後]

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/trusty64"
end

[手順3] トラブルシューティングを行う・・・

以下のトラブルシューティングを行いました。

[問題1] コンソールのvagrant upコマンドでOS起動失敗

仕方がないので、VirtualBoxのUIからOS起動を試みる。
「仮想化支援機構(VT-x/AMD-V)を有効化できません。」と怒られる。

f:id:nprogram:20181202151844p:plain

[対処方法1]
* VT-x/AMD-Vを有効化 - これは、BIOSで仮想環境のための設定がされていないことが原因。64bitOSをゲストOSにする場合はCPUがVT-xに対応している必要がある。
- https://futurismo.biz/archives/1647/

[問題2] VirtualBoxのUIからOS起動を再び試みる。また怒られる。
f:id:nprogram:20181202153821p:plain

[対処方法2] Windows で Virtualbox が起動しないので、原因を調べたところ、Virtualbox は Hyper-V が有効になっていると、起動できないと言うことがわかりました。
* Vagrant 事始め 番外編 01 - Virtualbox が起動しない - Qiita

[問題3] ようやく直ったと思ったので、次は、コンソールのvagrant upコマンドを実施。以下のコマンドで詰まる・・・。

C:\Users\uv2ut\vagrant\ubuntu64>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'ubuntu/trusty64' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Vagrant has detected a configuration issue which exposes a
==> default: vulnerability with the installed version of VirtualBox. The
==> default: current guest is configured to use an E1000 NIC type for a
==> default: network adapter which is vulnerable in this version of VirtualBox.
==> default: Ensure the guest is trusted to use this configuration or update
==> default: the NIC type using one of the methods below:
==> default:
==> default:   https://www.vagrantup.com/docs/virtualbox/configuration.html#default-nic-type
==> default:   https://www.vagrantup.com/docs/virtualbox/networking.html#virtualbox-nic-type
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (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
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.

If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.

If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.

If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.

[対処方法3] vagrant initコマンドで、vagrant fileを作成しなおして、再度vagrant upコマンドを入力することでうまく行きました。
* private_keyとVMのイメージがずれていることが原因とのこと。そういえば、Virtual BoxとVagrantを削除後、インストールし直したか・・・。 - https://www.nnn.ed.nico/questions/6392

C:\Users\uv2ut\vagrant\ubuntu64>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/trusty64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'ubuntu/trusty64' is up to date...
==> default: Setting the name of the VM: ubuntu64_default_1543734011399_42011
==> default: Clearing any previously set forwarded ports...
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Vagrant has detected a configuration issue which exposes a
==> default: vulnerability with the installed version of VirtualBox. The
==> default: current guest is configured to use an E1000 NIC type for a
==> default: network adapter which is vulnerable in this version of VirtualBox.
==> default: Ensure the guest is trusted to use this configuration or update
==> default: the NIC type using one of the methods below:
==> default:
==> default:   https://www.vagrantup.com/docs/virtualbox/configuration.html#default-nic-type
==> default:   https://www.vagrantup.com/docs/virtualbox/networking.html#virtualbox-nic-type
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2200 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2200
    default: SSH username: vagrant
    default: SSH auth method: private key
    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: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.3.36
    default: VirtualBox Version: 5.2
==> default: Mounting shared folders...
    default: /vagrant => C:/Users/uv2ut/vagrant/ubuntu64

これでようやく仮想環境が作成できました。

起動したUbuntuにアクセスする

Windowsでは、SSHクライアントと呼ばれるソフトウェアを用いることで、利用できます。

Windowsの場合は、追加で、SSHクライアントを用意する必要があります。

私はRLoginというソフトウェアを使用しました。 なお、上記ソフトウェアで接続するために必要な情報は以下のコマンドvagrant ssh-configで取得可能です。

C:\Users\uv2ut\vagrant\ubuntu64>vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2200
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile C:/Users/uv2ut/vagrant/ubuntu64/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

詳しい設定方法は、本に記載してあります。この本はわかりやすいと思います。

「Webプログラミングが面白いほどわかる本」 https://www.kadokawa.co.jp/product/321712000860

以下はイメージです。
f:id:nprogram:20181202164006p:plain

(補足) VagrantとVirtualBoxが作成したファイル、どこに行った?

C:\Users\ユーザー名.vagrant.d\boxes\以下
vagrant box addを実行すると、VMDK形式のファイルがコピーされる。
vagrant box removeを実行すると、VMDK形式のファイルが削除される。

C:\Users\ユーザー名\VirtualBox VMs\以下
vagrant upを実行すると、VMDK形式のファイルがコピーされる。

あとがき

Windows上で、仮想環境を作ろうとすると、相当大変であることがわかりました・・・。