Linuxめも

VirtualBoxをインストール

https://www.virtualbox.org/wiki/Downloads

Vagrantをインストール

ベイグラントと呼ぶようです。

https://www.vagrantup.com/downloads.html

boxの登録

VirtualBox用boxファイルを指定して登録する。

http://www.vagrantbox.es

C:\Users\user>vagrant box add CentOS66 https://github.com/tommy-muehle/puppet-vagrant-boxes/releases/download/1.0.0/centos-6.6-x86_64.box
mkdir centos
cd centos
vagrant init CentOS66

フォルダ内に Vagrantfile ファイルが生成されていることを確認

Vargrantfileの以下のコメントを外す

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

CPU数を4個にしてメモリを4GB割り当てる場合

config.vm.provider "virtualbox" do |vb|
    vb.cpus = 4
    vb.memory = "4096"
end

起動

vargrant up

TeraTermで 192.168.33.10 へ接続

ID:vargrant
PW:vagrant

サーバ停止は

vagrant halt

もしくはCentOS側で

shutdown -h now

共有フォルダの設定

Vagrantfile ファイルを編集

config.vm.synced_folder "../home", "/home"

C\Users\user\home\vagrant フォルダを作成し、test.txt ファイルを作成

sshにて

cd /home/vagrant
ls -l

で test.txt ファイルが見えれば成功

$ cd
$ vi .profile
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi
$ source ~/.profile

日本語環境に変更

# vi /etc/sysconfig/i18n
LANG="ja_JP.UTF-8"
SYSFONT="latarcyrheb-sun16"
# source /etc/sysconfig/i18n

CentOS7の場合

# vi /etc/locale.conf
LANG="ja_JP.UTF-8"
# source /etc/locale.conf

日本時間に設定

# cp /usr/share/zoneinfo/Japan /etc/localtime
# vi /etc/sysconfig/clock
ZONE="Asia/Tokyo"

CentOS7の場合

# cp /usr/share/zoneinfo/Japan /etc/localtime

確認

# timedatectl status

ネットワークの改善

1行追加。

# vi /etc/resolv.conf
options single-request-reopen

WEB+PHP+PostgreSQL+MySQL構築

yumインストールで環境構築を参照

ホストOSとの時刻同期を停止

開発を行っていると強制的に未来の時間に変更して動作を確認したい時もあります。
VirtualBoxはデフォルトで時刻をホストOSと同期するらしく、この機能を停止する必要があります。

コマンドプロンプトで操作します。

>cd C:\Program Files\Oracle\VirtualBox
>VBoxManage setextradata "VM name" "VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled" 1

VM nameはCentOS66とかではなく、Virtual Box マネージャーで確認できる centos_default_\d+_\d+ のような名前です。

同期を有効にするには 引数を 0 にして上記のコマンドを実行します。

rsyncでファイル共有を行う

自作のプログラムだと気になることはなかったが、EC-CUBE3系を入れたあたりからブラウザでのアクセスで画面が表示されるまでが極端に遅くなった。
Magento2系を入れたらさらに遅く、使い物にならなくなってしまうほど。
CPU数やメモリ割り当てを上げても解決せず。
vagrantの共有ファイルシステムが遅いという問題が指摘されているのを見かけてこれが原因だと気がつく。
ファイル数が多いシステムだと共有が遅いので、その分遅くなるのだと。
nfsで共有するのが楽ちんぽいがWindowsなので不可。
Sambaは信頼性に欠けるようなのでrsyncを使うのがベターな様子。

MinGWをインストール

以下からmingw-get-setup.exeをダウンロード
https://sourceforge.net/projects/mingw/files/

何も考えずに C:\MinGW へデフォルトでインストールします。

sshとrsyncをインストール

コマンドラインにてインストールします。

> cd C:\MinGW\bin
> mingw-get.exe update
> mingw-get.exe install msys-openssh msys-rsync

パスを通す

エクスプローラにてPCを右クリックし、プロパティ
システムの詳細設定 - 環境変数 - システムの環境変数

Pathを編集して以下2つを追加

Vagrantfileファイルを変更

共有設定をrsyncに書き換えます。

rsync__exclude は共有から除外したい場合、指定します。

config.vm.synced_folder "../home", "/home", type:"rsync", group:"apache", owner:"apache", rsync__chown:false, rsync__exclude: ["vagrant/", ".git/", ".svn/", "log/", "logs/", "cache/", "vendor/", "var/", "media/", "static/", "templates_c/", "user_data/", "upload/"]

rsync-autoを実行する

コマンドラインにて vagrant up したのちに vagrant rsync-auto を実行して共有フォルダを監視状態にします。
これで共有フォルダ内を変更すると自動的にゲストOSへ反映されるようになるはずです。

default: Warning: Authentication failure. Retrying...

rsyncで共有するようにしてから default: Warning: Authentication failure. Retrying... が連発してしまうようになりました。

エラーが連発している間に以下の鍵ファイルを削除します。
centos\.vagrant\machines\default\virtualbox\private_key

sshでログインして作業します。

$ cd /home/vagrant
$ mkdir .ssh
$ chmod 700 .ssh
$ cd .ssh
$ wget https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub
$ mv vagrant.pub authorized_keys
$ chmod 600 authorized_keys

vagrant halt してから再度 vagrant up すると問題なく立ち上がるはずです。

default: Warning: Remote connection disconnect. Retrying.

vagrant destroy してから vagrant up するといけるかもしれません。
destroy は仮想マシンの削除なので取り扱いに注意です。

rsync-autoを実行するとエラーになる

rsync-autoを実行すると、以下のようなエラーが出て同期できなくなってしまいました。

>vagrant rsync-auto
==> default: Doing an initial rsync...
==> default: Rsyncing folder: /c/Users/user/home/ => /home
==> default:   - Exclude: [".vagrant/", "vagrant/", ".git/", ".svn/", "log/", "logs/", "cache/", "vendor/", "var/", "media/", "static/", "templates_c/", "user_data/", "upload/"]
There was an error when attempting to rsync a synced folder.
Please inspect the error message below for more info.

Host path: /c/Users/user/home/
Guest path: /home
Command: "rsync" "--verbose" "--archive" "--delete" "-z" "--copy-links" "--chmod=ugo=rwX" "--no-perms" "--no-owner" "--no-group" "--rsync-path" "sudo rsync" "-e" "ssh -p 2222 -o LogLevel=FATAL   -o IdentitiesOnly=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i 'C:/Users/user/centos/.vagrant/machines/default/virtualbox/private_key'" "--exclude" ".vagrant/" "--exclude" "vagrant/" "--exclude" ".git/" "--exclude" ".svn/" "--exclude" "log/" "--exclude" "logs/" "--exclude" "cache/" "--exclude" "vendor/" "--exclude" "var/" "--exclude" "media/" "--exclude" "static/" "--exclude" "templates_c/" "--exclude" "user_data/" "--exclude" "upload/" "/c/Users/user/home/" "vagrant@127.0.0.1:/home"
Error:       0 [main] rsync.EXE" 1652 fork_copy: linked dll data/bss pass 0 failed, 0x1AA000..0x1AA020, done 0, windows pid 9904, Win32 error 998
rsync: fork: Resource temporarily unavailable (11)
rsync error: error in IPC code (code 14) at /usr/src/rsync/rsync-3.0.8/pipe.c(63) [sender=3.0.8]

vagrant rsync を実行してから rsync-auto すると普通に同期できるようになりました。

rsyncを高速化するプラグインvagrant-gatling-rsync

rsync-autoだと3テンポくらい同期が遅いので、プラグインを導入してみました。
https://github.com/smerrill/vagrant-gatling-rsync

$ vagrant plugin install vagrant-gatling-rsync

Vagrantfileに以下を追加します。

  if Vagrant.has_plugin?("vagrant-gatling-rsync")
    config.gatling.latency = 0.2
    config.gatling.time_format = "%H:%M:%S"
  end
  config.gatling.rsync_on_startup = false
$ vagrant reload
$ vagrant gatling-rsync-auto

inaccessibleで立ち上がらなくなった

vagrant up しても inaccessible と表示されて立ち上がらくなってしまった。

Bringing machine 'default' up with 'virtualbox' provider...
Your VM has become "inaccessible." Unfortunately, this is a critical error
with VirtualBox that Vagrant can not cleanly recover from. Please open VirtualBox
and clear out your inaccessible virtual machines or find a way to fix them.

Virtual Box VMs 以下のフォルダ内のファイル

centos_default_1456133417390_64892.vbox-prev
centos_default_1456133417390_64892.vbox-tmp

ができていたので、.vbox-tmp ファイルを .vbox に拡張子変更したら立ち上がりました。

Windows updateすると立ち上がらなくなった

Windows10でupdateがかかった後に vagrant up すると以下のようなエラーが途中で出力されてWEBアクセスできなくなってしまいました。

The guest machine entered an invalid state while waiting for it
to boot. Valid states are 'starting, running'. The machine is in the
'unknown' state. Please verify everything is configured
properly and try again.

If the provider you're using has a GUI that comes with it,
it is often helpful to open that and watch the machine, since the
GUI often has more helpful error messages than Vagrant can retrieve.
For example, if you're using VirtualBox, run `vagrant up` while the
VirtualBox GUI is open.

The primary issue for this error is that the provider you're using
is not properly configured. This is very rarely a Vagrant issue.

原因はよくわかりませんが、VirtualBoxがインストールされているフォルダで以下のファイルを右クリックしてインストールすれば無事に接続できるようになりました。

Oracle\VirtualBox\drivers\vboxdrv\VBoxDrv.inf

参考

Windows updateすると立ち上がらなくなった その2

Windows10でupdateがかかった後に vagrant up すると以下のようなエラーが表示されて立ち上がらなくなりました。

==> 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", "82d7edbb-54d7-43c5-9e12-940fde13d46a", "--type", "headless"]

Stderr: VBoxManage.exe: error: The virtual machine 'centos2_default_1498052710035_14537' has terminated unexpectedly during startup with exit code -1073741819 (0xc0000005)
VBoxManage.exe: error: Details: code E_FAIL (0x80004005), component MachineWrap, interface IMachine

最新のVirtualBoxをインストールすれば立ち上がるようになりました。
場合によってはVagrantのバージョンアップも要求されるかもしれません。


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS