socsvn commit: r305040 - in soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve: . action
litong at FreeBSD.org
litong at FreeBSD.org
Fri Jun 10 12:36:37 UTC 2016
Author: litong
Date: Fri Jun 10 12:36:34 2016
New Revision: 305040
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=305040
Log:
add create_switch and loader_kernel_module actions
Added:
soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/action/
soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/action/create_switch.rb
soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/action/load_kernel_module.rb
soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/action/load_os.rb
Modified:
soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/driver.rb
soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/errors.rb
soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/executor.rb
Added: soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/action/create_switch.rb
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/action/create_switch.rb Fri Jun 10 12:36:34 2016 (r305040)
@@ -0,0 +1,27 @@
+require "log4r"
+
+module VagrantPlugins
+ module ProviderBhyve
+ module Action
+ class LoadKernelModule
+
+ def initialize(app, env)
+ @logger = Log4r::Logger.new("vagrant_bhyve::action::create_switch")
+ @app = app
+ end
+
+ def call(env)
+ @machine = env[:machine]
+ @driver = @machine.provider.driver
+ switch_list = %w(vagrant-bhyve-default)
+ # The switch name is used as created bridge device's description
+ for switch in switch_list
+ driver.create_switch(switch)
+ end
+ @app.call(env)
+ end
+
+ end
+ end
+ end
+end
Added: soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/action/load_kernel_module.rb
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/action/load_kernel_module.rb Fri Jun 10 12:36:34 2016 (r305040)
@@ -0,0 +1,26 @@
+require "log4r"
+
+module VagrantPlugins
+ module ProviderBhyve
+ module Action
+ class LoadKernelModule
+
+ def initialize(app, env)
+ @logger = Log4r::Logger.new("vagrant_bhyve::action::load_kernel_module")
+ @app = app
+ end
+
+ def call(env)
+ @machine = env[:machine]
+ @driver = @machine.provider.driver
+ module_list = %w(vmm nmdm if_bridge if_tap)
+ for kernel_module in module_list
+ driver.load_module(kernel_module)
+ end
+ @app.call(env)
+ end
+
+ end
+ end
+ end
+end
Added: soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/action/load_os.rb
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/action/load_os.rb Fri Jun 10 12:36:34 2016 (r305040)
@@ -0,0 +1,25 @@
+require "log4r"
+
+module VagrantPlugins
+ module ProviderBhyve
+ module Action
+ class LoadKernelModule
+
+ def initialize(app, env)
+ @logger = Log4r::Logger.new("vagrant_bhyve::action::load_os")
+ @app = app
+ end
+
+ def call(env)
+ @machine = env[:machine]
+ @driver = @machine.provider.driver
+ firmware = @machine.box.metadata[:firmware]
+ loader = @machine.box.metadata[:loader]
+ @driver.loader(loader) if firmware == 'bios'
+ @app.call(env)
+ end
+
+ end
+ end
+ end
+end
Modified: soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/driver.rb
==============================================================================
--- soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/driver.rb Fri Jun 10 12:36:23 2016 (r305039)
+++ soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/driver.rb Fri Jun 10 12:36:34 2016 (r305040)
@@ -1,4 +1,3 @@
-require "vagrant/util/subprocess"
require "log4r"
module VagrantPlugins
@@ -9,26 +8,68 @@
# bhyve, dnsmasq and other shell utils used to get VM's state
attr_accessor :executor
- @@sudo = ''
-
def initialize(machine)
- @logger = Log4r::Logger.new("vagrant::bhyve::driver")
+ @logger = Log4r::Logger.new("vagrant_bhyve::driver")
@machine = machine
@executor = Executor::Exec.new
+
+ # if vagrant is excecuted by root (or with sudo) then the variable
+ # will be empty string, otherwise it will be 'sudo' to make sure we
+ # can run bhyve, bhyveload and pf with sudo privilege
+ if Process.uid == 0
+ @sudo = ''
+ @sudo = 'sudo'
+ end
+ end
+
+ def load_module(module_name)
+ result = execute(true, @sudo, "kldstat", "-qm", module_name, ">/dev/null", "2>&1")
+ if result != 0
+ result = execute(true, @sudo, "kldload", module_name, ">/dev/null", "2>&1")
+ result != 0 && raise Errors::UnableToLoadModule
+ end
+ end
+
+ def create_switch(switch_name)
+ return if switch_name.length == 0
+
+ # Check whether the switch has been created
+ desc = switch_name + '\$'
+ cmd = %w(ifconfig -a | grep -B 1).push(desc).push("|")
+ cmd += %w(head -n 1 | awk -F: '{print $1}')
+ result = execute(false, cmd)
+ return if result.length != 0
+
+ # Create new bridge device
+ bridge_name = execute(false, @sudo, "ifconfig", "bridge", "create")
+ raise Errors::UnableToCreateBridge if bridge_name.length == 0
+ # Add new created bridge device's description
+ execute(false, @sudo, "ifconfig", bridge_name, "description", switch_name, "up")
+ end
+
+ def loader(loader)
+ end
+
+ def bhyve
+ end
+
+ def state
+ # Prepare for other bhyve state which may be added in. For now, only
+ # running and not_running.
+ case
+ when running?
+ :running
+ else
+ :not_running
+ end
end
- # if vagrant is excecuted by root (or with sudo) then the variable
- # will be empty string, otherwise it will be 'sudo' to make sure we
- # can run bhyve, bhyveload and pf with sudo privilege
- def sudo
- @@sudo = '' if Process.uid == 0
- @@sudo = 'sudo'
+ def running?
+ execute(true, "test", "-e", "/dev/vmm/#{@machine.name}") == 0
end
- def state(&block)
- IO.popen("test -e #{name}").tap { |f| f.read }.close
- return :running if $?.success?
- return :not_running
+ def execute(*cmd, **opts, &block)
+ @executor.execute(*cmd, **opts, &block)
end
end
Modified: soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/errors.rb
==============================================================================
--- soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/errors.rb Fri Jun 10 12:36:23 2016 (r305039)
+++ soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/errors.rb Fri Jun 10 12:36:34 2016 (r305040)
@@ -7,14 +7,21 @@
error_namespace('vagrant_bhyve.errors')
end
- class HasNoRootPrivilege << VagrantBhyveError
+ class HasNoRootPrivilege < VagrantBhyveError
error_key(:has_no_root_privilege)
end
- class ExecuteError << VagrantBhyveError
+ class ExecuteError < VagrantBhyveError
error_key(:execute_error)
end
+ class UnableToLoadModule < VagrantError
+ error_key(:unable_to_load_module)
+ end
+
+ class UnableToCreateBridge < VagrantError
+ error_key(:unable_to_create_brighe)
+ end
end
end
end
Modified: soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/executor.rb
==============================================================================
--- soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/executor.rb Fri Jun 10 12:36:23 2016 (r305039)
+++ soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/executor.rb Fri Jun 10 12:36:34 2016 (r305040)
@@ -6,9 +6,10 @@
module Executor
# This class is used to execute commands as subprocess.
class Exec
- # When test is true, this method will return the executed command's
- # exit code. Otherwise it will return the result's stdout
- def execute(test, *cmd, **opts, &block)
+ # When we need the command's exit code we should set parameter
+ # exit_code to true, otherwise this method will return executed
+ # command's stdout
+ def execute(exit_code, *cmd, **opts, &block)
# Append in the options for subprocess
cmd << { notify: [:stdout, :stderr] }
@@ -19,7 +20,7 @@
::Vagrant::Util::Subprocess.execute(*cmd, &block)
end
- return result.exit_code if test
+ return result.exit_code if exit_code
result.stderr.gsub!("\r\n", "\n")
result.stdout.gsub!("\r\n", "\n")
More information about the svn-soc-all
mailing list