Merge pull request #8407 from gh2k/gentoo-systemd
Add support for gentoo guests that use systemd
This commit is contained in:
commit
e52821364a
|
@ -9,8 +9,14 @@ module VagrantPlugins
|
||||||
basename = name.split(".", 2)[0]
|
basename = name.split(".", 2)[0]
|
||||||
comm.sudo <<-EOH.gsub(/^ {14}/, "")
|
comm.sudo <<-EOH.gsub(/^ {14}/, "")
|
||||||
# Set the hostname
|
# Set the hostname
|
||||||
hostname '#{basename}'
|
|
||||||
echo "hostname=#{basename}" > /etc/conf.d/hostname
|
# Use hostnamectl on systemd
|
||||||
|
if [[ `systemctl` =~ -\.mount ]]; then
|
||||||
|
systemctl set-hostname '#{name}'
|
||||||
|
else
|
||||||
|
hostname '#{basename}'
|
||||||
|
echo "hostname=#{basename}" > /etc/conf.d/hostname
|
||||||
|
fi
|
||||||
|
|
||||||
# Remove comments and blank lines from /etc/hosts
|
# Remove comments and blank lines from /etc/hosts
|
||||||
sed -i'' -e 's/#.*$//' /etc/hosts
|
sed -i'' -e 's/#.*$//' /etc/hosts
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
require "tempfile"
|
require "tempfile"
|
||||||
|
require "ipaddr"
|
||||||
|
|
||||||
require_relative "../../../../lib/vagrant/util/template_renderer"
|
require_relative "../../../../lib/vagrant/util/template_renderer"
|
||||||
|
|
||||||
|
@ -14,35 +15,70 @@ module VagrantPlugins
|
||||||
commands = []
|
commands = []
|
||||||
interfaces = machine.guest.capability(:network_interfaces, "/bin/ip")
|
interfaces = machine.guest.capability(:network_interfaces, "/bin/ip")
|
||||||
|
|
||||||
# Remove any previous network additions to the configuration file.
|
networks.map! { |n| n[:device] = interfaces[n[:interface]]; n }
|
||||||
commands << "sed -i'' -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/conf.d/net"
|
|
||||||
|
|
||||||
networks.each_with_index do |network, i|
|
if comm.test('[[ `systemctl` =~ -\.mount ]]')
|
||||||
network[:device] = interfaces[network[:interface]]
|
# Configure networking for Systemd
|
||||||
|
|
||||||
entry = TemplateRenderer.render("guests/gentoo/network_#{network[:type]}",
|
# convert netmasks to CIDR by converting to a binary string and counting the '1's
|
||||||
options: network,
|
networks.map! { |n| n[:netmask] = IPAddr.new(n[:netmask]).to_i.to_s(2).count("1"); n }
|
||||||
)
|
|
||||||
|
|
||||||
remote_path = "/tmp/vagrant-network-#{network[:device]}-#{Time.now.to_i}-#{i}"
|
# glob networks by device, so that we can write one file per device
|
||||||
|
# (result is hash[devicename] = [net, net, net...])
|
||||||
|
networks = networks.map { |n| [n[:device], n] }.reduce({}) { |h, (k, v)| (h[k] ||= []) << v; h }
|
||||||
|
|
||||||
Tempfile.open("vagrant-gentoo-configure-networks") do |f|
|
# Write one .network file out for each device
|
||||||
f.binmode
|
networks.each_pair do |device_name, device_networks|
|
||||||
f.write(entry)
|
entry = TemplateRenderer.render('guests/gentoo/network_systemd', networks: device_networks)
|
||||||
f.fsync
|
|
||||||
f.close
|
filename = "50_vagrant_#{device_name}.network"
|
||||||
comm.upload(f.path, remote_path)
|
tmpfile = "/tmp/#{filename}"
|
||||||
|
destfile = "/etc/systemd/network/#{filename}"
|
||||||
|
|
||||||
|
Tempfile.open('vagrant-gentoo-configure-networks') do |f|
|
||||||
|
f.binmode
|
||||||
|
f.write(entry)
|
||||||
|
f.fsync
|
||||||
|
f.close
|
||||||
|
comm.upload(f.path, tmpfile)
|
||||||
|
end
|
||||||
|
|
||||||
|
commands << "mv #{tmpfile} #{destfile} && chmod 644 #{destfile}"
|
||||||
end
|
end
|
||||||
|
|
||||||
commands << <<-EOH.gsub(/^ {14}/, '')
|
# tell systemd to reload the networking config
|
||||||
ln -sf /etc/init.d/net.lo /etc/init.d/net.#{network[:device]}
|
commands << 'systemctl daemon-reload && systemctl restart systemd-networkd.service'
|
||||||
/etc/init.d/net.#{network[:device]} stop || true
|
else
|
||||||
|
# Configure networking for OpenRC
|
||||||
|
|
||||||
cat '#{remote_path}' >> /etc/conf.d/net
|
# Remove any previous network additions to the configuration file.
|
||||||
rm -f '#{remote_path}'
|
commands << "sed -i'' -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/conf.d/net"
|
||||||
|
|
||||||
/etc/init.d/net.#{network[:device]} start
|
networks.each_with_index do |network, i|
|
||||||
EOH
|
entry = TemplateRenderer.render("guests/gentoo/network_#{network[:type]}",
|
||||||
|
options: network,
|
||||||
|
)
|
||||||
|
|
||||||
|
remote_path = "/tmp/vagrant-network-#{network[:device]}-#{Time.now.to_i}-#{i}"
|
||||||
|
|
||||||
|
Tempfile.open("vagrant-gentoo-configure-networks") do |f|
|
||||||
|
f.binmode
|
||||||
|
f.write(entry)
|
||||||
|
f.fsync
|
||||||
|
f.close
|
||||||
|
comm.upload(f.path, remote_path)
|
||||||
|
end
|
||||||
|
|
||||||
|
commands << <<-EOH.gsub(/^ {14}/, '')
|
||||||
|
ln -sf /etc/init.d/net.lo /etc/init.d/net.#{network[:device]}
|
||||||
|
/etc/init.d/net.#{network[:device]} stop || true
|
||||||
|
|
||||||
|
cat '#{remote_path}' >> /etc/conf.d/net
|
||||||
|
rm -f '#{remote_path}'
|
||||||
|
|
||||||
|
/etc/init.d/net.#{network[:device]} start
|
||||||
|
EOH
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
comm.sudo(commands.join("\n"))
|
comm.sudo(commands.join("\n"))
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
[Match]
|
||||||
|
Name=<%= networks[0][:device] %>
|
||||||
|
|
||||||
|
[Network]
|
||||||
|
<%- gateway = nil %>
|
||||||
|
<%- networks.each do |net| %>
|
||||||
|
<%- if net[:type] == 'dhcp' %>
|
||||||
|
DHCP=yes
|
||||||
|
<%- elsif net[:ip] %>
|
||||||
|
Address=<%= net[:ip] %>/<%= net[:netmask] %>
|
||||||
|
<%- end %>
|
||||||
|
<%- gateway ||= net[:gateway] %>
|
||||||
|
<%- end %>
|
||||||
|
<%- if gateway %>
|
||||||
|
Gateway=<%= gateway %>
|
||||||
|
<%- end %>
|
|
@ -0,0 +1,73 @@
|
||||||
|
require_relative "../../../base"
|
||||||
|
|
||||||
|
require "vagrant/util/template_renderer"
|
||||||
|
|
||||||
|
describe "templates/guests/gentoo/network_systemd" do
|
||||||
|
let(:template) { "guests/gentoo/network_systemd" }
|
||||||
|
|
||||||
|
it "renders the template with a static ip" do
|
||||||
|
result = Vagrant::Util::TemplateRenderer.render(template, networks: [{
|
||||||
|
device: "eth0",
|
||||||
|
type: "dhcp",
|
||||||
|
}])
|
||||||
|
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
|
||||||
|
[Match]
|
||||||
|
Name=eth0
|
||||||
|
|
||||||
|
[Network]
|
||||||
|
DHCP=yes
|
||||||
|
EOH
|
||||||
|
end
|
||||||
|
|
||||||
|
it "renders the template with multiple ips" do
|
||||||
|
result = Vagrant::Util::TemplateRenderer.render(template, networks: [{
|
||||||
|
device: "eth0",
|
||||||
|
ip: "1.1.1.1",
|
||||||
|
netmask: "16",
|
||||||
|
},{
|
||||||
|
device: "eth0",
|
||||||
|
ip: "1.1.2.2",
|
||||||
|
netmask: "16",
|
||||||
|
}])
|
||||||
|
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
|
||||||
|
[Match]
|
||||||
|
Name=eth0
|
||||||
|
|
||||||
|
[Network]
|
||||||
|
Address=1.1.1.1/16
|
||||||
|
Address=1.1.2.2/16
|
||||||
|
EOH
|
||||||
|
end
|
||||||
|
|
||||||
|
it "renders the template with a static ip" do
|
||||||
|
result = Vagrant::Util::TemplateRenderer.render(template, networks: [{
|
||||||
|
device: "eth0",
|
||||||
|
ip: "1.1.1.1",
|
||||||
|
netmask: "16",
|
||||||
|
}])
|
||||||
|
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
|
||||||
|
[Match]
|
||||||
|
Name=eth0
|
||||||
|
|
||||||
|
[Network]
|
||||||
|
Address=1.1.1.1/16
|
||||||
|
EOH
|
||||||
|
end
|
||||||
|
|
||||||
|
it "includes the gateway" do
|
||||||
|
result = Vagrant::Util::TemplateRenderer.render(template, networks: [{
|
||||||
|
device: "eth0",
|
||||||
|
ip: "1.1.1.1",
|
||||||
|
netmask: "16",
|
||||||
|
gateway: "1.2.3.4",
|
||||||
|
}])
|
||||||
|
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
|
||||||
|
[Match]
|
||||||
|
Name=eth0
|
||||||
|
|
||||||
|
[Network]
|
||||||
|
Address=1.1.1.1/16
|
||||||
|
Gateway=1.2.3.4
|
||||||
|
EOH
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue