Martnn 2019-06-25
本人使用vagrant搭建了centOS6.7虚拟机开发环境,用作web开发。mac电脑选用公司的网线上网,也就是使用网卡“en0: 以太网”。
但最近碰到一个棘手的问题。
注:关于vagrant搭建可以参考github:https://github.com/astaxie/Go...
想选择桥接模式让虚拟机上网,于是在mac端编辑Vagrantfile,添加 config.vm.network "public_network",并将其他的network设置注释掉。vagrant up启动虚拟机之后,看到如下的输出:
Bringing machine 'default' up with 'virtualbox' provider... ==> default: Clearing any previously set network interfaces... ==> default: Available bridged network interfaces: 1) en0: 以太网 2) en1: Wi-Fi (AirPort) 3) en2: Thunderbolt 1 4) en3: Thunderbolt 2 5) p2p0 6) awdl0 7) bridge0 ==> default: When choosing an interface, it is usually the one that is ==> default: being used to connect to the internet. default: Which interface should the network bridge to?
因本人使用网线上网,于是选择了1) en0: 以太网,选择1之后看到如下输出(后面还有很多报错省略了):
==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat default: Adapter 2: bridged /opt/vagrant/embedded/gems/gems/vagrant-1.7.4/lib/vagrant/util/subprocess.rb:28:in `encode': "\xE4" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError) from /opt/vagrant/embedded/gems/gems/vagrant-1.7.4/lib/vagrant/util/subprocess.rb:28:in `block in initialize' from /opt/vagrant/embedded/gems/gems/vagrant-1.7.4/lib/vagrant/util/subprocess.rb:28:in `map'
仔细查看报错信息:
/opt/vagrant/embedded/gems/gems/vagrant-1.7.4/lib/vagrant/util/subprocess.rb:28:in `encode': "\xE4" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError)
- 1、看到上面的报错我是崩溃的,在google和百度找了很久,没找到答案。经过几番测试,我发现只有选择1) en0: 以太网才会报类似的错误,选择其他的选项时,虽然连不联网,但是不会报这样的错。我怀疑可能是编码问题!
@command = @command.map { |s| s.encode(Encoding.default_external) }
以上是Ruby语法,本人也不懂ruby,刚开始束手无策,google了一下ruby编码转换的问题,尝试把上面这行改动了一下,改为以下内容:
@command = @command.map { |s| s.force_encoding('UTF-8') }
注意:不知道ruby对空格要求严不严格,改动后的代码两边的大括号保留了空格!
这个应该属于vagrant的一枚bug!!