あーる学習帳

自分が勉強したことや気になることなど、99%自分用です。コードを書いてるのでPCから閲覧を推奨。

仮想環境構築①

放置していたドットインストールくん、またよろしく。

 

ローカル開発環境の構築(Windows10)

Webでなんやかんやしようとするとき、レンタルサーバーを借りるのはお金がかかるので自分のPCに仮想環境を作ってしまおうというお話。

VirtualBoxインストール(Ver5.2.12→5.1.38→5.1.30)

Vagrantインストール(Ver2.1.1→2.0.1)

 サーバー1つに付きVagrantでは1フォルダ必要。

CentOSを使う→「MyCentOS」というフォルダを作ってその中にサーバー立ち上げ。

自分はC:\(自分のフォルダ)\MyVagrantに作ることにする。

 

VagrantのコマンドでIPアドレスとサーバーの設定ファイルを持たせる。

普通のPCと同じように、IPは他と重複しないようにする。

以下は動画で使用していたコマンド。Pathは自分のものに合わせる。

# 今後複数の仮想マシンを作ることを想定して、それらをまとめるフォルダ(MyVagrant)を作る
mkdir MyVagrant

# MyVagrantに移動する
cd MyVagrant

# 仮想マシンを作るフォルダを作る(MyCentOS)
mkdir MyCentOS

# MyCentOSに移動する
cd MyCentOS

# 仮想マシン設定用のVagrantfileを作る
vagrant init bento/centos-6.8
→下記のエラー。
 エラー内に示されていた「vagrant plugin Update」を実行し、再度コマンドを入れたら解消

Vagrant failed to initialize at a very early stage:

The plugins failed to initialize correctly. This may be due to manual
modifications made within the Vagrant home directory. Vagrant can
attempt to automatically correct this issue by running:

vagrant plugin repair

If Vagrant was recently updated, this error may be due to incompatible
versions of dependencies. To fix this problem please remove and re-install
all plugins. Vagrant can attempt to do this automatically by running:

vagrant plugin expunge --reinstall

Or you may want to try updating the installed plugins to their latest
versions:

vagrant plugin update

Error message given during initialization: Unable to resolve dependency: user requested 'vagrant-vbguest (> 0)' 


# Vagrantfileを編集して仮想マシンのIPアドレスを192.168.33.10にする vagrantfileをエディタで開き、
# config.vm.network "private_network", ip: "192.168.33.10"
の行のコメントアウトを外す(#を外す)
動画ではatomを使っていた。今のところVisualStudioCodeで代用。 # 仮想マシンを起動する(少し時間かかります) vagrant up
→エラーがたくさん。まずはエラー1.

→その後、VirtualBoxとVagrantを再インストールするとエラー2が発生。
→エラー2は書かれたPathにあったIndexとIndex.Lockというファイルを消したら正常に動くようになった。
→その後はvagrant upを実行できた。やたらインストールやらアップデートやら走ってるけど大丈夫か。
→またエラー。問題山積。エラー3を参照。

エラー1

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'bento/centos-6.8'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'bento/centos-6.8' is up to date...
==> default: A newer version of the box 'bento/centos-6.8' for provider 'virtualbox' is
==> default: available! You currently have version '2.3.1'. The latest is version
==> default: '2.3.4'. Run `vagrant box update` to update.
==> default: Setting the name of the VM: MycentOS_default_1527516530723_78621
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: Adapter 2: hostonly
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> 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", "c57744d7-5b10-49cd-a31c-73e9893f1ac0", "--type", "headless"]

Stderr: VBoxManage.exe: error: Raw-mode is unavailable courtesy of Hyper-V. (VERR_SUPDRV_NO_RAW_MODE_HYPER_V_ROOT)
VBoxManage.exe: error: Details: code E_FAIL (0x80004005), component ConsoleWrap, interface IConsole

これはHyper-Vの設定をしてやると解消。

エラー2

The machine index which stores all required information about
running Vagrant environments has become corrupt. This is usually
caused by external tampering of the Vagrant data folder.

Vagrant cannot manage any Vagrant environments if the index is
corrupt. Please attempt to manually correct it. If you are unable
to manually correct it, then remove the data file at the path below.
This will leave all existing Vagrant environments "orphaned" and
they'll have to be destroyed manually.

Path: C:/Users/(ユーザー名)/.vagrant.d/data/machine-index/index

最後に書かれたフォルダにあったIndexとIndex.lockを削除。

その後最後実行したら解決。

エラー3

/sbin/mount.vboxsf: mounting failed with the error: No such device

仮想環境の起動は成功しているっぽいけど上記エラーが出る。

共有の設定だと思われるが…。

エラー3が解消できなかったので、これは明日へ持ち越し。

実行したいコマンドがあと1個残っていたが、これを叩けるのはいつの話になるだろうか…。

# 仮想マシンの状態を確認する
vagrant status

 

明日も続けて頑張る。水曜までには仮想環境構築のレッスンを終えたい。

 

※参照

今夜の教材 ドットインストール ローカル開発環境の構築(Win編) #1~#5

https://dotinstall.com/lessons/basic_localdev_win_v2

エラー1、エラー2への対処

[vagrant][windows10]Windows + VirtualBox (+ vagrant) でvagrant upできなくなったとき

Win10にVirtualBox+Vagrantを用いた環境構築する為の下準備

エラー3への対処

Windows上でVirtualBox+Vagrant+CentOSによる仮想環境構築