Vagrant

Table of Contents

1 Getting Started

这篇文章可以作为很好的起点 http://blog.segmentfault.com/fenbox/1190000000264347

使用之前确保安装了virtualbox以及最新版本的vagrant (http://downloads.vagrantup.com/). 如果不是最新版本的vagrant可能会在读取box文件时候出错。

启动之后可以使用ssh vagrant@localhost -p2222连接上。默认帐号和密码都是vagrant, 并且是sudo权限。Vagrantfile所在的目录也会被挂载到/vagrant下面。

常用命令

  • vagrant box add <name> <url> # 添加虚拟机
  • vagrant init # 初始化,同时在本地目录产生Vagrantfile配置文件
  • vagrant up # 启动虚拟机
  • vagrant halt # 关闭虚拟机
  • vagrant reload # 重启虚拟机
  • vagrant ssh # SSH 至虚拟机
  • vagrant status # 查看虚拟机运行状态
  • vagrant destroy # 销毁当前虚拟机
  • vagrant package # 将当前虚拟机打包

2 启动多台虚拟机

http://docs.vagrantup.com/v2/multi-machine/index.html

启动多个虚拟机,并且每个虚拟机使用不同的环境

# Every Vagrant virtual environment requires a box to build off of.
config.vm.define "centos" do |m|
  m.vm.box = "centos"
end
config.vm.define "debian" do |m|
  m.vm.box = "debian"
end
config.vm.define "freebsd" do |m|
  m.vm.box = "freebsd"
end

操作这台机器的话只需要在命令之后指定机器名称即可,比如vagrant ssh centos

3 invalid byte sequence in UTF-8 (Argument Error)

添加box时候出现如下错误

➜  vm.centos  vagrant box add centos ~/utils/centos65-x86_64-20140116.box
==> box: Adding box 'centos' (v0) for provider:
    box: Downloading: file:///home/dirlt/utils/centos65-x86_64-20140116.box
/opt/vagrant/embedded/lib/ruby/2.0.0/rubygems/version.rb:163:in `=~': invalid byte sequence in UTF-8 (ArgumentError)
	from /opt/vagrant/embedded/lib/ruby/2.0.0/rubygems/version.rb:163:in `correct?'
	from /opt/vagrant/embedded/lib/ruby/2.0.0/rubygems/version.rb:192:in `initialize'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/box_collection.rb:273:in `new'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/box_collection.rb:273:in `block (2 levels) in find'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/box_collection.rb:270:in `map'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/box_collection.rb:270:in `block in find'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/box_collection.rb:416:in `block in with_collection_lock'
	from /opt/vagrant/embedded/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/box_collection.rb:415:in `with_collection_lock'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/box_collection.rb:263:in `find'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/box_collection.rb:81:in `block in add'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/box_collection.rb:148:in `call'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/box_collection.rb:148:in `block (3 levels) in add'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/box_collection.rb:429:in `with_temp_dir'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/box_collection.rb:130:in `block (2 levels) in add'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/box_collection.rb:429:in `with_temp_dir'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/box_collection.rb:111:in `block in add'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/box_collection.rb:416:in `block in with_collection_lock'
	from /opt/vagrant/embedded/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/box_collection.rb:415:in `with_collection_lock'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/box_collection.rb:100:in `add'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/action/builtin/box_add.rb:337:in `box_add'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/action/builtin/box_add.rb:130:in `add_direct'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/action/builtin/box_add.rb:106:in `call'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/action/warden.rb:34:in `call'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/action/builder.rb:116:in `call'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/action/runner.rb:66:in `block in run'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/util/busy.rb:19:in `busy'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/action/runner.rb:66:in `run'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/plugins/commands/box/command/add.rb:77:in `execute'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/plugins/commands/box/command/root.rb:61:in `execute'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/cli.rb:42:in `execute'
	from /opt/vagrant/embedded/gems/gems/vagrant-1.6.1/lib/vagrant/environment.rb:252:in `cli'
	from /opt/vagrant/bin/../embedded/gems/gems/vagrant-1.6.1/bin/vagrant:166:in `<main>'

解决办法是删除~/.vagrant.d/目录(或者是删除里面部分文件应该也行)

4 Connection timeout. Retrying

启动box时候出现如下错误,说明虚拟机一直没有成功启动

==> 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
    default: Error: Connection timeout. Retrying...
    default: Error: Connection timeout. Retrying...
    default: Error: Connection timeout. Retrying...
    default: Error: Connection timeout. Retrying...
    default: Error: Connection timeout. Retrying...
    default: Error: Connection timeout. Retrying...
    default: Error: Connection timeout. Retrying...
    default: Error: Connection timeout. Retrying...
    default: Error: Connection timeout. Retrying...
    default: Error: Connection timeout. Retrying...
    default: Error: Connection timeout. Retrying...
    default: Error: Connection timeout. Retrying...
    default: Error: Connection timeout. Retrying...

可以将virtualbox的GUI打开,看看具体是什么错误导致没有正常启动。在Vagrantfile里面添加

config.vm.provider :virtualbox do |vb|
  vb.gui = true
end
comments powered by Disqus