DRYな備忘録

Don't Repeat Yourself.

【追記あり】【解決】Mac上のVagrantでFreeBSDが動かない

2017/11/28 追記

このVagrantfileですべてが動くことを確認できたので【解決】としました。

github.com

2017/11/16 追記

どうやらFreeBSDにおけるsudoの実装が内包しているバグだった模様で、公式が「数日内にバグフィックス出すよ」と言っている。とりあえず全裸待機。


以下原文


目的

環境

% vagrant --version
Vagrant 2.0.0
% VBoxManage --version
5.1.30r118389
% system_profiler SPSoftwareDataType | grep Version
      System Version: macOS 10.12.6 (16G29)
      Kernel Version: Darwin 16.7.0
%

公式ドキュメントに書かれたやりかた

Vagrant CloudにあるFreeBSDのbox

% mkdir -p ~/tmp/vagrant-freebsd-test
% cd ~/tmp/vagrant-freebsd-test
% vagrant init freebsd/FreeBSD-10.4-STABLE
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
% ls
Vagrantfile

で、

% vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'freebsd/FreeBSD-10.4-STABLE'...
No base MAC address was specified. This is required for the NAT networking
to work properly (and hence port forwarding, SSH, etc.). Specifying this
MAC address is typically up to the box and box maintainer. Please contact
the relevant person to solve this issue.

ふぇぇ

No base MAC address was specified.

FreeBSD側で書いてあったVagrantfile

base_macもコピペなので動かねえんじゃねえの?という予想がありつつ、

Vagrant.configure("2") do |config|
  config.vm.guest = :freebsd
  config.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true
  config.vm.box = "freebsd/FreeBSD-10.4-STABLE"
  config.ssh.shell = "sh"
  config.vm.base_mac = "080027D14C66"

  config.vm.provider :virtualbox do |vb|
    vb.customize ["modifyvm", :id, "--memory", "1024"]
    vb.customize ["modifyvm", :id, "--cpus", "1"]
    vb.customize ["modifyvm", :id, "--hwvirtex", "on"]
    vb.customize ["modifyvm", :id, "--audio", "none"]
    vb.customize ["modifyvm", :id, "--nictype1", "virtio"]
    vb.customize ["modifyvm", :id, "--nictype2", "virtio"]
  end
end
% vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'freebsd/FreeBSD-10.4-STABLE'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'freebsd/FreeBSD-10.4-STABLE' is up to date...
==> default: Setting the name of the VM: vagrant-freebsd-test_default_1509123869695_7924
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> 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: Warning: Remote connection disconnect. Retrying...
    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Connection reset. Retrying...
#
# これのクッソ繰り返し
#
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it is present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 5.2.0
    default: VirtualBox Version: 5.1

なんかSSH Connectionのリトライめっちゃしているように見えるが、Machine booted and ready!と自信満々に言われたので、とりあえずsshを試す。

% vagrant ssh
FreeBSD 10.4-STABLE (GENERIC) #0 r324749: Thu Oct 19 15:55:47 UTC 2017

Welcome to FreeBSD!

Release Notes, Errata: https://www.FreeBSD.org/releases/
Security Advisories:   https://www.FreeBSD.org/security/
FreeBSD Handbook:      https://www.FreeBSD.org/handbook/
FreeBSD FAQ:           https://www.FreeBSD.org/faq/
Questions List: https://lists.FreeBSD.org/mailman/listinfo/freebsd-questions/
FreeBSD Forums:        https://forums.FreeBSD.org/

Documents installed with the system are in the /usr/local/share/doc/freebsd/
directory, or can be installed later with:  pkg install en-freebsd-doc
For other languages, replace "en" with a language code like de or fr.

Show the version of FreeBSD installed:  freebsd-version ; uname -a
Please include that output and any error messages when posting questions.
Introduction to manual pages:  man man
FreeBSD directory layout:      man hier

Edit /etc/motd to change this login announcement.
"man firewall" will give advice for building a FreeBSD firewall
        -- David Scheidt <dscheidt@tumbolia.com>
vagrant@:~ % uname -a
FreeBSD  10.4-STABLE FreeBSD 10.4-STABLE #0 r324749: Thu Oct 19 15:55:47 UTC 2017     root@releng1.nyi.freebsd.org:/usr/obj/usr/src/sys/GENERIC  amd64
vagrant@:~ %
vagrant@:~ % echo "Hello, FreeBSD!"
Hello, FreeBSD!
vagrant@:~ %

まあsshはできてる。

再現性を確認するために、お掃除しとく。

% vagrant destroy
    default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Forcing shutdown of VM...
==> default: Destroying VM and associated drives...
% ls
Vagrantfile
%

Provisionを試みる: Segmentation fault

Vagrantfile編集

Vagrant.configure("2") do |config|
  config.vm.guest = :freebsd
  config.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true
  config.vm.box = "freebsd/FreeBSD-10.4-STABLE"
  config.ssh.shell = "sh"
  config.vm.base_mac = "080027D14C66"

  config.vm.provider :virtualbox do |vb|
    vb.customize ["modifyvm", :id, "--memory", "1024"]
    vb.customize ["modifyvm", :id, "--cpus", "1"]
    vb.customize ["modifyvm", :id, "--hwvirtex", "on"]
    vb.customize ["modifyvm", :id, "--audio", "none"]
    vb.customize ["modifyvm", :id, "--nictype1", "virtio"]
    vb.customize ["modifyvm", :id, "--nictype2", "virtio"]
  end
+
+  config.vm.provision "shell", inline: "echo hello"
+
end

で、

% vagrant up
# 中略
==> default: Running provisioner: shell...
    default: Running: inline script
The SSH command responded with a non-zero exit status. Vagrant
assumes that this means the command failed. The output for this command
should be in the log above. Please read the output to determine what
went wrong.
%

Vagrant assumes that this means the command failed.

って、echoが失敗してるとでも言うのかお前は。ためしに、

% vagrant ssh --command "echo hgoeeee"
hgoeeee
Connection to 127.0.0.1 closed.
%

Vagrantのログは、providerがVirtualBoxの場合(VirtualBoxのインストールディレクトリにもよるとは思うけれど)、
~/VirtualBox VMs/vagrant-freebsd-test_default_1509154848753_31244/Logs
にあるっぽい。

他には、

% vagrant destroy -f && vagrant up --debug

で得られるものもある。で、それっぽいエラー箇所抜粋↓

DEBUG virtualbox_5_1:   - [1, "ssh", 2222, 22, "127.0.0.1"]
DEBUG ssh: Checking key permissions: /Users/otiai10/tmp/vagrant-freebsd-test/.vagrant/machines/default/virtualbox/private_key
DEBUG ssh: Re-using SSH connection.
 INFO ssh: Execute: chown -R vagrant /tmp/vagrant-shell (sudo=true)
DEBUG ssh: stderr: Segmentation fault

DEBUG ssh: Exit status: 139
DEBUG ssh: Uploading: /var/folders/__/xxcbtw6j0tb681fjv1lbnlg40000gn/T/vagrant-shell20171028-68133-1nvns04.ps1 to /tmp/vagrant-shell
DEBUG ssh: Re-using SSH connection.
 INFO interface: detail: Running: inline script
 INFO interface: detail:     default: Running: inline script
    default: Running: inline script
DEBUG ssh: Re-using SSH connection.
 INFO ssh: Execute: chmod +x '/tmp/vagrant-shell' && /tmp/vagrant-shell (sudo=true)
DEBUG ssh: stderr: Segmentation fault

DEBUG ssh: Exit status: 139

Segmentation fault

ふむ。

sudoerではないprovisionの実行は成功

ためしに、sudo=falseとしてみる。Vagrantfileで、

     vb.customize ["modifyvm", :id, "--nictype2", "virtio"]
   end

-  config.vm.provision "shell", inline: "echo hello"
+  config.vm.provision "shell", inline: "echo hello", privileged: false
 end

そんでふたたび、

% vagrant destroy -f && vagrant up --debug

とすると、

f:id:otiai10:20171028105826p:plain

ProvisionにおけるユーザのPrivilegeの問題であることが切り分けられた。

provisionにおけるユーザのsudoについて

前述の

をちゃんと読む

Seems like sudo will segfault on official FreeBSD STABLE (but not RELEASE) Vagrant boxes if a hostname isn't set. Problem is Vagrant uses sudo to set any hostname specified in the Vagrantfile or run any shell commands.

Setting it manually worked for me:

% vagrant ssh
vagrant@:~ % su
root@:/home/vagrant # sysrc hostname=myhost.local (modifies rc.conf)
root@:/home/vagrant # halt -p
% vagrant up --provision

f:id:otiai10:20171028111520p:plain

うーん、動いたけど、vagrant sshしてる時点でプロビジョニングツールとしてどうなんだという感じ。

Seems like sudo will segfault on official FreeBSD STABLE (but not RELEASE) Vagrant boxes if a hostname isn't set.

ほんまかいな。-RELEASE使ったろ。

Vagrant.configure("2") do |config|
  config.vm.guest = :freebsd
  config.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true
-  config.vm.box = "freebsd/FreeBSD-10.4-STABLE"
+  config.vm.box = "freebsd/FreeBSD-10.4-RELEASE"
  config.ssh.shell = "sh"
  config.vm.base_mac = "080027D14C66"

で、

% vagrant destroy -f && vagrant up
DEBUG virtualbox_5_1:   - [1, "ssh", 2222, 22, "127.0.0.1"]
DEBUG ssh: Checking key permissions: /Users/otiai10/tmp/vagrant-freebsd-test/.vagrant/machines/default/virtualbox/private_key
DEBUG ssh: Re-using SSH connection.
 INFO ssh: Execute: chown -R vagrant /tmp/vagrant-shell (sudo=true)
DEBUG ssh: stderr: Segmentation fault

DEBUG ssh: Exit status: 139
DEBUG ssh: Uploading: /var/folders/__/xxcbtw6j0tb681fjv1lbnlg40000gn/T/vagrant-shell20171028-72476-1wwln9a.ps1 to /tmp/vagrant-shell
DEBUG ssh: Re-using SSH connection.
 INFO interface: detail: Running: inline script
 INFO interface: detail:     default: Running: inline script
    default: Running: inline script
DEBUG ssh: Re-using SSH connection.
 INFO ssh: Execute: chmod +x '/tmp/vagrant-shell' && /tmp/vagrant-shell (sudo=true)
DEBUG ssh: stderr: Segmentation fault

ふつうにSegmentation fault出るやん。一応検証のため、

% vagrant ssh
vagrant@:~ % su
root@:/home/vagrant # sysrc hostname=testtest.local
root@:/home/vagrant # halt -p
Connection to 127.0.0.1 closed by remote host.
Connection to 127.0.0.1 closed.
% vagrant up --provision

f:id:otiai10:20171028112741p:plain

んなぁ〜動くなぁ〜。

Vagrantfileによるhostnameの指定は可能か?

config.vm.hostname - The hostname the machine should have. Defaults to nil. If nil, Vagrant will not manage the hostname. If set to a string, the hostname will be set on boot.

いけるっぽいぞ。ためしに、Vagrantfileを、

Vagrant.configure("2") do |config|
  config.vm.guest = :freebsd
  config.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true
  config.vm.box = "freebsd/FreeBSD-10.4-RELEASE"
  config.ssh.shell = "sh"
  config.vm.base_mac = "080027D14C66"
+
+  config.vm.hostname = "testtest.local"
+

としてみるものの、

f:id:otiai10:20171028113445p:plain

hostnameを実行しようとするとき、同様にSegmentation faultが出ている。

ここまでの結論

  • sudoしようとするときにSegmentation faultになる
  • sshしてhostnameを設定すると解決する
  • 再現可能

Next Action