From a4768c26ca33f6ce7dbe0360e69c4bf9d5e44f2e Mon Sep 17 00:00:00 2001 From: Kristof Willaert Date: Tue, 3 Jun 2014 17:53:26 +0200 Subject: [PATCH] Add fallback mechanism for platforms without attach support Some platforms (most notably CentOS and RHEL) use a kernel without 'attach' support. This patch detects this absence and falls back to the alternative ways of doing things like detection of IP address and halting the container. It does so by running the command "true" through lxc-attach. --- lib/vagrant-lxc/action.rb | 10 ++++++++-- lib/vagrant-lxc/driver.rb | 4 ++++ lib/vagrant-lxc/driver/cli.rb | 15 ++++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/vagrant-lxc/action.rb b/lib/vagrant-lxc/action.rb index 66269cc..19dae69 100644 --- a/lib/vagrant-lxc/action.rb +++ b/lib/vagrant-lxc/action.rb @@ -197,8 +197,14 @@ module Vagrant def self.action_fetch_ip Builder.new.tap do |b| b.use Builtin::ConfigValidate - b.use FetchIpWithLxcAttach - b.use FetchIpFromDnsmasqLeases + b.use Builtin::Call, Builtin::IsState, :running do |env, b2| + if env[:result] + b2.use FetchIpWithLxcAttach if env[:machine].provider.driver.supports_attach? + b2.use FetchIpFromDnsmasqLeases + else + b2.use Builtin::Message, I18n.t("vagrant_lxc.messages.not_running") + end + end end end diff --git a/lib/vagrant-lxc/driver.rb b/lib/vagrant-lxc/driver.rb index 6435c24..9ae5662 100644 --- a/lib/vagrant-lxc/driver.rb +++ b/lib/vagrant-lxc/driver.rb @@ -113,6 +113,10 @@ module Vagrant @cli.destroy end + def supports_attach? + @cli.supports_attach? + end + def attach(*command) @cli.attach(*command) end diff --git a/lib/vagrant-lxc/driver/cli.rb b/lib/vagrant-lxc/driver/cli.rb index d04a1d9..b9f6ee3 100644 --- a/lib/vagrant-lxc/driver/cli.rb +++ b/lib/vagrant-lxc/driver/cli.rb @@ -77,7 +77,7 @@ module Vagrant end def stop - attach '/sbin/halt' + attach '/sbin/halt' if supports_attach? run :stop, '--name', @name end @@ -126,6 +126,19 @@ module Vagrant end end + def supports_attach? + unless defined?(@supports_attach) + begin + @supports_attach = true + run(:attach, '--name', @name, 'true') + rescue LXC::Errors::ExecuteError + @supports_attach = false + end + end + + return @supports_attach + end + private def run(command, *args)