VirtualBoxにインストールしたVMからvagrant boxを自作する
vagrantのVMイメージはいつもA list of base boxes for Vagrant - Vagrantbox.esから選んでローカルのBoxに追加していましたが、誰かが作成したBoxをそのまま使用するのはセキュリティ的に芳しくないと思っていたため、信頼できるサイトからisoファイルとしてダウンロードしてきたものからboxを自作してみることにしました。
Virtualboxにインストール
そのまま通常通りに進めていけば良いので、詳しい方法は省略します。下記等を参考にしてください。
CentOS 6.5をnetinstall.isoを使用して最小構成でインストールする - Symfoware
isoファイルはCentOS6.6を選択して以下から取得しました。
ホストからsshログイン
virtualboxの設定からネットワーク>アダプター2 ネットワークアダプターを有効化にチェックし、割り当てをホストオンリーアダプターを選択します。 このときMacアドレスをメモします。
Virtaulboxのコンソールにてeth1の設定をします。
# vi /etc/sysconfig/network-scripts/ifcfg-eth1
以下を設定します。
DEVICE=eth1 TYPE=Ethernet NM_CONTROLLED=no ONBOOT=yes BOOTPROTO=none HWADDR=08:00:27:9D:50:67 #ご自身のMacアドレスを設定します。 NAME="eth1" IPADDR=192.168.33.20 NETMASK=255.255.255.0 PEERDNS=no
eh1を起動します。
# ifup eth1
rootユーザでsshログインをします。
$ ssh root@192.168.33.20
ログイン後にvagrantユーザを作成します。
# useradd -m vagrant # passwd vagrant
今度はvagrantユーザでログインします。
$ ssh vagrant@192.168.33.20
sshキーの作成
ホスト側でsshキーのペアを作成します。このときキーフレーズにはなにも入れません。
$ ssh-keygen -t rsa
scpで公開キーを送信します。
$ scp id_rsa.pub vagrant@192.168.33.20:~/
公開キーを登録します。パーミッションに注意してください。これを間違えると、sshログインのときにパスワードを聞かれてしまいます。
$ mkdir .ssh $ chmod 700 .ssh $ ch .ssh $ cat id_rsa.pub >> authorized_keys $ chmod 600 authorized_keys
sshログインでパスワードを聞かれなくなりました。
$ ssh vagrant@192.168.33.20
Vagrant boxを作成
ホストで以下のコマンドを実行します。centos66は仮想環境の名前です。
$ vagrant package --base centos66
package.boxが作成されます。
vagrant upする
作成したboxを追加して、Vagrantfileを作成し、vagrant upしてみました。
$ vagrant box add centos66 package.box $ vagrant init
Vagrantfile
Vagrant.configure(2) do |config| config.vm.box = "centos66" config.vm.network "private_network", ip: "192.168.33.10" ...
vagrant upすると、エラーが起きました。
Authentication failureのエラー
1つ目は、Authentication failureと繰り返し表示される、以下のエラーです。
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: Connection timeout. Retrying... default: Warning: Remote connection disconnect. Retrying... default: Warning: Remote connection disconnect. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying...
これは、ゲストOSに以下のauthorized_keyを追加することで解決しました。
$ wget https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub -O .ssh/authorized_keys
sudoとttyのエラー
2つ目は、以下のようなエラーです。
default: in which case you may ignore this message. ==> default: Configuring and enabling network interfaces... The following SSH command responded with a non-zero exit status. Vagrant assumes that this means the command failed! cat /etc/redhat-release Stdout from the command: Stderr from the command: sudo: sudo を実行するには tty がなければいけません。すみません
ttyなしでsudoできるように、visudoでDefaults requirettyをコメントアウトします。
# visudo # # Disable "ssh hostname sudo", because it will show the password in clear. # You have to run "ssh -t hostname sudo ". # # Defaults requiretty
vagrant haltをして、再度起動しようとすると、以下のようなエラーもでました。
$ vagrant halt ==> default: Attempting graceful shutdown of VM... The following SSH command responded with a non-zero exit status. Vagrant assumes that this means the command failed! shutdown -h now Stdout from the command: Stderr from the command: sudo: 端末 (tty) が存在せず、パスワードを尋ねる (askpass) プログラムが指定されていません
visudoして、以下を追加しました。
vagrant ALL=(ALL) NOPASSWD: ALL
デバイス eth1 は存在しないと言われるエラー
vagrant haltして、vagrant upします。
$ vagrant halt ==> default: Attempting graceful shutdown of VM... $ vagrant up ==> default: Configuring and enabling network interfaces... The following SSH command responded with a non-zero exit status. Vagrant assumes that this means the command failed! ARPCHECK=no /sbin/ifup eth1 2> /dev/null Stdout from the command: デバイス eth1 は存在しないようですので、初期化を遅らせます。 Stderr from the command:
今度は上記のようなエラーが発生しました。下記を参考にしました。 Vagrantのネットワークが起動しないときは - Thinking-megane
パッケージングする前のゲストOSでMACアドレスとのマッピングを無効にします。
# sudo ln -s -f /dev/null /etc/udev/rules.d/70-persistent-net.rules
guest additionsのエラー
再度パッケージングしなおして、vagrant upしてみます。(このとき、いままでの上記のエラーをださないために、それぞれの上記対策も一緒にしておきました。)
$ vagrant destroy $ vagrant box remove centos66 $ rm -rf package.box $ vagrant package --base centos66 $ vagrant box add centos66 package.box $ vagrant up .... ==> default: Checking for guest additions in VM... default: No guest additions were detected on the base box for this VM! Guest default: additions are required for forwarded ports, shared folders, host only default: networking, and more. If SSH fails on this machine, please install default: the guest additions and repackage the box to continue. default: default: This is not an error message; everything may continue to work properly, default: in which case you may ignore this message. ==> default: Configuring and enabling network interfaces... ==> default: Mounting shared folders... default: /vagrant => /Users/xxx/centos 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: mount: 未知のファイルシステムタイプ 'vboxsf'
eth1のエラーはでなくなりましたが、どうやら、guest additionsをインストールしなければいけないみたいです。
下記を参考に元のゲストOSにquest addtitionsをインストールします。
VagrantのBoxを新しく作成する方法(VirtualBox / CentOS 6.6 x86_64) - TASK NOTES
guest additionsのセットアップに必要なモジュールをインストールします。
# yum -y install gcc # yum -y install kernel-devel # yum -y install dkms # yum -y install perl # reboot
guest addtionsをダウンロード、そしてマウントします。
# wget http://download.virtualbox.org/virtualbox/4.3.20/VBoxGuestAdditions_4.3.20.iso # mkdir /media/VBoxGuestAdditions # mount -o loop,ro VBoxGuestAdditions_4.3.20.iso /media/VBoxGuestAdditions # sh /media/VBoxGuestAdditions/VBoxLinuxAdditions.run # rm VBoxGuestAdditions_4.3.20.iso # umount /media/VBoxGuestAdditions # rmdir /media/VBoxGuestAdditions
再度パッケージングして、vagrant upします。
ようやく成功
$ vagrant destroy $ vagrant box remove centos66 $ rm -rf package.box $ vagrant package --base centos66 $ vagrant box add centos66 package.box $ vagrant up .... ==> default: Mounting shared folders... default: /vagrant => /Users/xxx/centos
やっと成功しました。
今後はこの作成したboxを使っていきたいと思います。
*BIOSとは?
Linuxのインストール時の調査で色々調べているうちに、BIOSという言葉が多くでてきました。BIOSとは、マザーボードに組み込まれたソフトウェアで、PCの起動時に実行され、ハードウェアを初期化して、OSを起動させるものらしいです。
- 作者: Mitchell Hashimoto,Sky株式会社玉川竜司
- 出版社/メーカー: オライリージャパン
- 発売日: 2014/02/21
- メディア: 単行本(ソフトカバー)
- この商品を含むブログ (9件) を見る