Module: VagrantPlugins::QEMU::Network
- Defined in:
- lib/vagrant-qemu/network.rb,
lib/vagrant-qemu/network/tap.rb,
lib/vagrant-qemu/network/base.rb,
lib/vagrant-qemu/network/vmnet.rb,
lib/vagrant-qemu/network/socket.rb
Defined Under Namespace
Classes: Base, Socket, Tap, Vmnet
Class Method Summary collapse
-
.auto_detect ⇒ Base
Auto-detect the best backend for the current platform.
-
.backend_for(net_mode) ⇒ Base
Select the appropriate network backend based on net_mode and platform.
-
.build_network_config(mac0:, mac1:, ip:, netmask: "255.255.255.0") ⇒ String
Build cloud-init network-config v2 YAML for dual-NIC setup.
-
.generate_mac(vm_id, nic_index) ⇒ String
Generate a deterministic MAC address from vm_id and NIC index.
-
.nic_macs(vm_id, pn) ⇒ Array(String, String)
MAC pair for the dual-NIC setup; NIC 1 honors a user-specified MAC.
Class Method Details
.auto_detect ⇒ Base
Auto-detect the best backend for the current platform.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/vagrant-qemu/network.rb', line 31 def self.auto_detect case RbConfig::CONFIG['host_os'] when /darwin/ Vmnet.new when /linux/ Tap.new else Socket.new end end |
.backend_for(net_mode) ⇒ Base
Select the appropriate network backend based on net_mode and platform.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/vagrant-qemu/network.rb', line 14 def self.backend_for(net_mode) case net_mode when :vmnet_shared, :vmnet_host, :vmnet_bridged Vmnet.new when :tap Tap.new when :socket Socket.new when :auto auto_detect else raise Errors::ConfigError, err: "Unknown net_mode: #{net_mode}" end end |
.build_network_config(mac0:, mac1:, ip:, netmask: "255.255.255.0") ⇒ String
Build cloud-init network-config v2 YAML for dual-NIC setup. Only called when advanced_network is enabled and private_network is configured.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/vagrant-qemu/network.rb', line 73 def self.build_network_config(mac0:, mac1:, ip:, netmask: "255.255.255.0") require 'ipaddr' require 'yaml' prefix = IPAddr.new(netmask).to_i.to_s(2).count("1") config = { "network" => { "version" => 2, "ethernets" => { "user-nic" => { "match" => { "macaddress" => mac0 }, "dhcp4" => true }, "private-nic" => { "match" => { "macaddress" => mac1 }, "addresses" => ["#{ip}/#{prefix}"] } } } } config.to_yaml end |
.generate_mac(vm_id, nic_index) ⇒ String
Generate a deterministic MAC address from vm_id and NIC index. Uses 52:54:00 prefix (QEMU’s OUI).
48 49 50 51 52 |
# File 'lib/vagrant-qemu/network.rb', line 48 def self.generate_mac(vm_id, nic_index) require 'digest' hash = Digest::MD5.hexdigest("#{vm_id}-#{nic_index}") "52:54:00:#{hash[0..1]}:#{hash[2..3]}:#{hash[4..5]}" end |
.nic_macs(vm_id, pn) ⇒ Array(String, String)
MAC pair for the dual-NIC setup; NIC 1 honors a user-specified MAC. Single source of truth so the QEMU command line (driver) and the cloud-init network-config (CloudInitNetwork action) never diverge.
61 62 63 |
# File 'lib/vagrant-qemu/network.rb', line 61 def self.nic_macs(vm_id, pn) [generate_mac(vm_id, 0), (pn && pn[:mac]) || generate_mac(vm_id, 1)] end |