VagrantでCentOS7の仮想マシンを作成する。
勤め先の開発環境をvagrantとChefを使って作ろうと思う。
Install Vagrant(For OSX)
事前にHomebrewとHomebrew Caskがインストールされていること。
brew cask install vagrant brew cask install virtualbox
Install Vagrant plug-in
vagrant plugin install vagrant-vbguest vagrant plugin install vagrant-vbox-snapshot vagrant plugin install sahara
仮想マシンを作成
vagrant init
vagrant box add centos7 https://f0fff3908f081cb6461b407be80daf97f07ac418.googledrive.com/host/0BwtuV7VyVTSkUG1PM3pCeDJ4dVE/centos7.box md -p vms/centos7 && cd vms/centos7 vagrant init centos7
Vagrantfileの設定
Vagrantfile
ってのが作られる。
仮想マシンの設定ファイルとのことだ。
設定項目は多々あれど、とりあえずこんな感じ
# -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure(2) do |config| Encoding.default_external = 'UTF-8' config.vbguest.auto_update = false config.vm.define :centos7, primary: true do |node| node.vm.box = "centos7" node.vm.hostname = 'cos7' node.vm.network "private_network", ip: '10.0.0.10' node.ssh.forward_agent = true node.vm.provision :shell, :path => "provision/bootstrap.sh" config.vm.provider :virtualbox do |vb| vb.name = "cos7" vb.gui = false vb.memory = 1024 vb.cpus = 2 vb.customize ["modifyvm", :id, "--largepages", "on"] vb.customize ["modifyvm", :id, "--accelerate3d", "off"] vb.customize ["modifyvm", :id, "--accelerate2dvideo", "off"] vb.customize ["modifyvm", :id, "--natdnsproxy1", "off"] vb.customize ["modifyvm", :id, "--natdnshostresolver1", "off"] end end end
Vagrant 起動時に1回だけ実行するスクリプトを設定する | Z BLOG
を参考にしてprovision/bootstrap.sh
はこんな感じde書いた。
proxyの設定と、SELINUXとfirewallを無効にしてる。
#!/usr/bin/env bash # Proxy環境化の場合はtrueに設定すること ON_PROXY=true HTTP_PROXY="xxxx(host):xx(port)" HTTPS_PROXY="$HTTP_PROXY" HOST_ID="10.0.0.1" test -f /etc/bootstrapped && exit if $ON_PROXY ; then echo "Setting HTTP Proxy for /etc/environment" cat <<... >> /etc/environment http_proxy="http://$HTTP_PROXY/" https_proxy="https://$HTTPS_PROXY/" ... echo "Setting HTTP Proxy for /etc/yum.conf" cat <<... >> /etc/yum.conf proxy=http://$HTTP_PROXY ... echo "Setting HTTP Proxy for /etc/wgetrc" cat <<... >> /etc/wgetrc http_proxy=http://$HTTP_PROXY https_proxy=http://$HTTPS_PROXY ftp_proxy=http://$HTTP_PROXY ... echo "Setting HTTP Proxy for cURL in Vagrant user directory" if test -d /home/vagrant; then cat <<... >> /home/vagrant/.curlrc proxy = "http://$HTTP_PROXY" ... fi fi echo "Disable SELINUX & firewall" sudo systemctl stop firewalld sudo systemctl disable firewalld sudo sed -i -e s/SELINUX=enforcing/SELINUX=disabled/ /etc/selinux/config sudo setenforce 0 sudo systemctl restart network date > /etc/bootstrapped
vagrant up
最初はこんなエラーだった。
vagrant up # 省略 Failed to mount folders in Linux guest. This is usually because the "vboxsf" file system is not available. Please verify that the guest additions are properly installed in the guest and can work properly. The command attempted was: mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` vagrant /vagrant mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` vagrant /vagrant The error output from the last command was: /sbin/mount.vboxsf: mounting failed with the error: No such device
ほんで、vagrant
とvirtualbox
を最新にして
下記のpluginもインストールした。
vagrant plugin install vagrant-vbguest
したらエラーが警告に変わった。
vagrant up # 途中は省略 ( ´థ౪థ) GuestAdditions versions on your host (4.3.20) and guest (4.3.14) do not match. # 途中は省略 ( ◉◞౪◟◉) Installing Virtualbox Guest Additions 4.3.20 - guest version is 4.3.14 # 途中は省略 ( ◜◡^)っ✂╰⋃╯ An error occurred during installation of VirtualBox Guest Additions 4.3.20. Some functionality may not work as intended. In most cases it is OK that the "Window System drivers" installation failed.
GuestAdditionsのバージョンが違うらしい。
このままだと共有フォルダのマウントが出来ないので下記の解決方法を参考にした。
Vagrant can't mount shared folder in VirtualBox 4.3.10
vagrant ssh # /usr/lib配下にVBoxGuestAdditionsへのシンボリックリンクを貼る [vagrant@localhost ~]$ sudo ln -s /opt/VBoxGuestAdditions-4.3.20/lib/VBoxGuestAdditions /usr/lib/VBoxGuestAdditions [vagrant@localhost ~]$ exit # reloadすれば正常に起動する筈 vagrant reload
snapshotとっとく
正常に動作してめでたいのでsnapshotをとっておこう。
vagrant snapshot take v0_init
[参考]
Vagrant のネットワーク周りのあれこれ
Vagrant 起動時に1回だけ実行するスクリプトを設定する | Z BLOG
Vagrant can't mount shared folder in VirtualBox 4.3.10