Merge pull request #13004 from chrisroberts/vbox-hostnet-dhcp

VirtualBox hostnet addressing
This commit is contained in:
Chris Roberts 2022-12-08 11:33:05 -08:00 committed by GitHub
commit 2eb393aaee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 9 deletions

View File

@ -30,8 +30,14 @@ module VagrantPlugins
# Prune any hostonly interfaces in the list
ifaces.delete_if { |i|
addr = IPAddr.new(i[:ip]).mask(i[:netmask])
hostonly_ifaces.include?(addr)
addr = begin
IPAddr.new(i[:ip]).mask(i[:netmask])
rescue IPAddr::Error => err
@logger.warn("skipping bridged interface due to parse error #{err} (#{i}) ")
nil
end
addr.nil? ||
hostonly_ifaces.include?(addr)
}
ifaces
@ -154,21 +160,32 @@ module VagrantPlugins
# reformat the information to line up with how
# the interfaces is structured
read_host_only_networks.map do |net|
addr = IPAddr.new(net[:lowerip])
addr = begin
IPAddr.new(net[:lowerip])
rescue IPAddr::Error => err
@logger.warn("invalid host only network lower IP encountered: #{err} (#{net})")
next
end
# Address of the interface will be the lower bound of the range or
# the first available address in the subnet
if addr == addr.mask(net[:networkmask])
addr = addr.succ
end
net[:netmask] = net[:networkmask]
if addr.ipv4?
net[:ip] = addr.mask(net[:netmask]).succ.to_s
net[:ip] = addr.to_s
net[:ipv6] = ""
else
net[:ip] = ""
net[:ipv6] = addr.mask(net[:netmwask]).succ.to_s
net[:ipv6] = addr.to_s
net[:ipv6_prefix] = net[:netmask]
end
net[:status] = net[:state] == "Enabled" ? "Up" : "Down"
net
end
end.compact
end
def read_network_interfaces

View File

@ -164,12 +164,14 @@ memory=1024)
end
describe "#read_bridged_interfaces" do
let(:bridgedifs) { VBOX_BRIDGEDIFS }
before do
allow(subject).to receive(:execute).and_call_original
expect(subject).
to receive(:execute).
with("list", "bridgedifs").
and_return(VBOX_BRIDGEDIFS)
and_return(bridgedifs)
end
context "when hostonlynets are not enabled" do
@ -218,6 +220,14 @@ memory=1024)
end
end
end
context "when address is empty" do
let(:bridgedifs) { VBOX_BRIDGEDIFS.sub("0.0.0.0", "") }
it "should not raise an error" do
expect { subject.read_bridged_interfaces }.to_not raise_error
end
end
end
describe "#delete_unused_host_only_networks" do
@ -566,8 +576,19 @@ memory=1024)
it "should assign the address as the first in the subnet" do
result = subject.read_host_only_interfaces
ip = IPAddr.new(result.first[:lowerip]).mask(result.first[:networkmask])
expect(result.first[:ip]).to eq(ip.succ.to_s)
expect(result.first[:ip]).to eq(IPAddr.new(result.first[:lowerip]).succ.to_s)
end
context "when dhcp range is set" do
before do
allow(subject).to receive(:execute).with("list", "hostonlynets", any_args).
and_return(VBOX_RANGE_HOSTONLYNETS)
end
it "should assign the address as the first in the dhcp range" do
result = subject.read_host_only_interfaces
expect(result.first[:ip]).to eq(result.first[:lowerip])
end
end
end
end
@ -767,6 +788,42 @@ LowerIP: 192.168.22.0
UpperIP: 192.168.22.0
VBoxNetworkName: hostonly-vagrantnet-vbox2)
VBOX_HOSTONLYNETS=%(Name: vagrantnet-vbox1
GUID: 10000000-0000-0000-0000-000000000000
State: Enabled
NetworkMask: 255.255.255.0
LowerIP: 192.168.61.0
UpperIP: 192.168.61.0
VBoxNetworkName: hostonly-vagrantnet-vbox1
Name: vagrantnet-vbox2
GUID: 20000000-0000-0000-0000-000000000000
State: Enabled
NetworkMask: 255.255.255.0
LowerIP: 192.168.22.0
UpperIP: 192.168.22.0
VBoxNetworkName: hostonly-vagrantnet-vbox2)
VBOX_RANGE_HOSTONLYNETS=%(Name: vagrantnet-vbox1
GUID: 10000000-0000-0000-0000-000000000000
State: Enabled
NetworkMask: 255.255.255.0
LowerIP: 192.168.61.10
UpperIP: 192.168.61.100
VBoxNetworkName: hostonly-vagrantnet-vbox1
Name: vagrantnet-vbox2
GUID: 20000000-0000-0000-0000-000000000000
State: Enabled
NetworkMask: 255.255.255.0
LowerIP: 192.168.22.0
UpperIP: 192.168.22.0
VBoxNetworkName: hostonly-vagrantnet-vbox2)
VBOX_GUEST_HOSTONLYVNETS_INFO=%(
natnet1="nat"
macaddress1="080027BB1475"