diff --git a/plugins/guests/netbsd/cap/change_host_name.rb b/plugins/guests/netbsd/cap/change_host_name.rb new file mode 100644 index 000000000..826ffe8a6 --- /dev/null +++ b/plugins/guests/netbsd/cap/change_host_name.rb @@ -0,0 +1,18 @@ +module VagrantPlugins + module GuestNetBSD + module Cap + class ChangeHostName + def self.change_host_name(machine, name) + if !machine.communicate.test("hostname -s | grep '^#{name}$'") + machine.communicate.sudo(< "sh"}) +set -e +sed -e 's/^hostname=.*$/hostname=#{name}/' /etc/rc.conf > /tmp/rc.conf.vagrant_changehostname_#{name} +mv /tmp/rc.conf.vagrant_changehostname_#{name} /etc/rc.conf +hostname #{name} +CMDS + end + end + end + end + end +end diff --git a/plugins/guests/netbsd/cap/configure_networks.rb b/plugins/guests/netbsd/cap/configure_networks.rb new file mode 100644 index 000000000..80750658c --- /dev/null +++ b/plugins/guests/netbsd/cap/configure_networks.rb @@ -0,0 +1,53 @@ +require "tempfile" + +require "vagrant/util/template_renderer" + +module VagrantPlugins + module GuestNetBSD + module Cap + class ConfigureNetworks + include Vagrant::Util + + def self.configure_networks(machine, networks) + + # setup a new rc.conf file + newrcconf = "/tmp/rc.conf.vagrant_configurenetworks" + machine.communicate.sudo("sed -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/rc.conf > #{newrcconf}") + + networks.each do |network| + + # create an interface configuration file fragment + entry = TemplateRenderer.render("guests/netbsd/network_#{network[:type]}", + :options => network) + + temp = Tempfile.new("vagrant") + temp.binmode + temp.write(entry) + temp.close + + # upload it and append it to the new rc.conf file + machine.communicate.upload(temp.path, "/tmp/vagrant-network-entry") + machine.communicate.sudo("cat /tmp/vagrant-network-entry >> #{newrcconf}") + machine.communicate.sudo("rm /tmp/vagrant-network-entry") + + ifname = "wm#{network[:interface]}" + # remove old configuration + machine.communicate.sudo("/sbin/dhcpcd -x #{ifname}", { :error_check => false }) + machine.communicate.sudo("/sbin/ifconfig #{ifname} inet delete", { :error_check => false }) + + # live new configuration + if network[:type].to_sym == :static + machine.communicate.sudo("/sbin/ifconfig #{ifname} media autoselect up;/sbin/ifconfig #{ifname} inet #{network[:ip]} netmask #{network[:netmask]}") + elsif network[:type].to_sym == :dhcp + machine.communicate.sudo("/sbin/dhcpcd -n -q #{ifname}") + end + end + + # install new rc.conf + machine.communicate.sudo("install -c -o 0 -g 0 -m 644 #{newrcconf} /etc/rc.conf") + + end + end + end + end +end diff --git a/plugins/guests/netbsd/cap/halt.rb b/plugins/guests/netbsd/cap/halt.rb new file mode 100644 index 000000000..3638bd223 --- /dev/null +++ b/plugins/guests/netbsd/cap/halt.rb @@ -0,0 +1,16 @@ +module VagrantPlugins + module GuestNetBSD + module Cap + class Halt + def self.halt(machine) + begin + machine.communicate.sudo("/sbin/shutdown -p -h now") + rescue IOError + # Do nothing, because it probably means the machine shut down + # and SSH connection was lost. + end + end + end + end + end +end diff --git a/plugins/guests/netbsd/cap/mount_nfs_folder.rb b/plugins/guests/netbsd/cap/mount_nfs_folder.rb new file mode 100644 index 000000000..d9a31d625 --- /dev/null +++ b/plugins/guests/netbsd/cap/mount_nfs_folder.rb @@ -0,0 +1,17 @@ +module VagrantPlugins + module GuestNetBSD + module Cap + class MountNFSFolder + def self.mount_nfs_folder(machine, ip, folders) + folders.each do |name, opts| + machine.communicate.sudo(< "sh"}) +set -e +mkdir -p #{opts[:guestpath]} +/sbin/mount -t nfs '#{ip}:#{opts[:hostpath]}' '#{opts[:guestpath]}' +CMDS + end + end + end + end + end +end diff --git a/plugins/guests/netbsd/guest.rb b/plugins/guests/netbsd/guest.rb new file mode 100644 index 000000000..b0cbd35d6 --- /dev/null +++ b/plugins/guests/netbsd/guest.rb @@ -0,0 +1,11 @@ +require "vagrant" + +module VagrantPlugins + module GuestNetBSD + class Guest < Vagrant.plugin("2", :guest) + def detect?(machine) + machine.communicate.test("uname -s | grep NetBSD") + end + end + end +end diff --git a/plugins/guests/netbsd/plugin.rb b/plugins/guests/netbsd/plugin.rb new file mode 100644 index 000000000..ca55706fc --- /dev/null +++ b/plugins/guests/netbsd/plugin.rb @@ -0,0 +1,35 @@ +require "vagrant" + +module VagrantPlugins + module GuestNetBSD + class Plugin < Vagrant.plugin("2") + name "NetBSD guest" + description "NetBSD guest support." + + guest("netbsd") do + require File.expand_path("../guest", __FILE__) + Guest + end + + guest_capability("netbsd", "change_host_name") do + require_relative "cap/change_host_name" + Cap::ChangeHostName + end + + guest_capability("netbsd", "configure_networks") do + require_relative "cap/configure_networks" + Cap::ConfigureNetworks + end + + guest_capability("netbsd", "halt") do + require_relative "cap/halt" + Cap::Halt + end + + guest_capability("netbsd", "mount_nfs_folder") do + require_relative "cap/mount_nfs_folder" + Cap::MountNFSFolder + end + end + end +end diff --git a/templates/guests/netbsd/network_dhcp.erb b/templates/guests/netbsd/network_dhcp.erb new file mode 100644 index 000000000..51fac1030 --- /dev/null +++ b/templates/guests/netbsd/network_dhcp.erb @@ -0,0 +1,3 @@ +#VAGRANT-BEGIN +ifconfig_wm<%= options[:interface] %>=dhcp +#VAGRANT-END diff --git a/templates/guests/netbsd/network_static.erb b/templates/guests/netbsd/network_static.erb new file mode 100644 index 000000000..8a5a70e9b --- /dev/null +++ b/templates/guests/netbsd/network_static.erb @@ -0,0 +1,3 @@ +#VAGRANT-BEGIN +ifconfig_wm<%= options[:interface] %>="media autoselect up;inet <%= options[:ip] %> netmask <%= options[:netmask] %>" +#VAGRANT-END