Class: Vmit::BridgedNetwork
- Inherits:
-
Network
- Object
- RefcountedResource
- Network
- Vmit::BridgedNetwork
- Defined in:
- lib/vmit/network.rb
Overview
Implementation of networking with a bridge with optional NAT to the host interface.
Constant Summary collapse
- DEFAULT_NETWORK =
'192.168.58.254/24'
Instance Attribute Summary
Attributes inherited from RefcountedResource
#lockfile_dir, #lockfile_path, #name
Instance Method Summary collapse
- #connect_interface(device) ⇒ Object
-
#disconnect_interface(device) ⇒ Object
reimplemented from RefcountedResource.
- #dnsmasq_pid ⇒ Object
- #dnsmasq_pidfile ⇒ Object
-
#initialize(address) ⇒ BridgedNetwork
constructor
A new instance of BridgedNetwork.
- #kill_dnsmasq ⇒ Object
-
#on_acquire ⇒ Object
reimplemented from RefcountedResource.
-
#on_down ⇒ Object
reimplemented from RefcountedResource.
-
#on_release ⇒ Object
reimplemented from RefcountedResource.
-
#on_up ⇒ Object
reimplemented from RefcountedResource.
- #start_dnsmasq ⇒ Object
- #to_s ⇒ Object
Methods inherited from Network
create, default, from_alias, from_config, #resource_class
Methods inherited from RefcountedResource
#auto, make_temp, #resource_class
Constructor Details
#initialize(address) ⇒ BridgedNetwork
Returns a new instance of BridgedNetwork.
75 76 77 78 79 80 |
# File 'lib/vmit/network.rb', line 75 def initialize(address) @address = IPAddress(address).network brdevice = 'br0' @brdevice = brdevice super("#{@brdevice}-#{@address.to_u32}") end |
Instance Method Details
#connect_interface(device) ⇒ Object
107 108 109 110 111 |
# File 'lib/vmit/network.rb', line 107 def connect_interface(device) Vmit.logger.info " Connecting #{device} --> #{@brdevice}" #Vmit::Utils.run_command(*['ovs-vsctl', 'add-port', SWITCH, ARGV[0]]) Cheetah.run '/sbin/brctl', 'addif', @brdevice, device end |
#disconnect_interface(device) ⇒ Object
reimplemented from RefcountedResource
125 126 127 128 129 |
# File 'lib/vmit/network.rb', line 125 def disconnect_interface(device) Vmit.logger.info " Disconnecting #{device} -X-> #{@brdevice}" #Vmit::Utils.run_command(*['ovs-vsctl', 'del-port', SWITCH, ARGV[0]]) Cheetah.run '/sbin/brctl', 'delif', @brdevice, device end |
#dnsmasq_pid ⇒ Object
151 152 153 |
# File 'lib/vmit/network.rb', line 151 def dnsmasq_pid File.read(dnsmasq_pidfile).strip.to_i end |
#dnsmasq_pidfile ⇒ Object
155 156 157 |
# File 'lib/vmit/network.rb', line 155 def dnsmasq_pidfile File.join(lockfile_dir, 'dnsmasq.pid') end |
#kill_dnsmasq ⇒ Object
146 147 148 149 |
# File 'lib/vmit/network.rb', line 146 def kill_dnsmasq Vmit.logger.info "Killing dnsmasq (#{dnsmasq_pid})" Process.kill('SIGTERM', dnsmasq_pid) end |
#on_acquire ⇒ Object
reimplemented from RefcountedResource
132 133 |
# File 'lib/vmit/network.rb', line 132 def on_acquire end |
#on_down ⇒ Object
reimplemented from RefcountedResource
114 115 116 117 118 119 120 121 122 |
# File 'lib/vmit/network.rb', line 114 def on_down Vmit.logger.info "Bringing down bridged network #{@address.to_string} on #{@brdevice}" Vmit.logger.info " `-> managed by #{lockfile_path}" Cheetah.run '/sbin/ifconfig', @brdevice, 'down' Cheetah.run '/sbin/brctl', 'delbr', @brdevice Cheetah.run 'iptables', '-t', 'nat', '-D', 'POSTROUTING', '-s', @address.network.to_string, '!', '-d', @address.network.to_string, '-j', 'MASQUERADE' kill_dnsmasq end |
#on_release ⇒ Object
reimplemented from RefcountedResource
136 137 |
# File 'lib/vmit/network.rb', line 136 def on_release end |
#on_up ⇒ Object
reimplemented from RefcountedResource
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/vmit/network.rb', line 87 def on_up Vmit.logger.info "Bringing up bridged network #{@address.to_string} on #{@brdevice}" Vmit.logger.info " `-> managed by #{lockfile_path}" # setup bridge # may be use 'ip', 'link', 'show', 'dev', devname to check if # the bridge is there? Cheetah.run '/sbin/brctl', 'addbr', @brdevice File.write("/proc/sys/net/ipv6/conf/#{@brdevice}/disable_ipv6", 1) File.write('/proc/sys/net/ipv4/ip_forward', 1) Cheetah.run '/sbin/brctl', 'stp', @brdevice, 'on' #Cheetah.run '/sbin/brctl', 'setfd', @brdevice, '0' rescue nil # setup network and dhcp on bridge Cheetah.run '/sbin/ifconfig', @brdevice, @address.network.hosts[0].to_s Cheetah.run '/sbin/ifconfig', @brdevice, 'up' Cheetah.run 'iptables', '-t', 'nat', '-A', 'POSTROUTING', '-s', @address.network.to_string, '!', '-d', @address.network.to_string, '-j', 'MASQUERADE' start_dnsmasq end |
#start_dnsmasq ⇒ Object
139 140 141 142 143 144 |
# File 'lib/vmit/network.rb', line 139 def start_dnsmasq dnsmasq_args = %W(dnsmasq -Z -x #{dnsmasq_pidfile} --strict-order --bind-interfaces --listen-address #{@address.network.hosts[0]} --dhcp-range #{@address.network.hosts[1]},#{@address.network.hosts.last}) Vmit.logger.debug "dnsmasq arguments: '#{dnsmasq_args.join(' ')}'" IO.popen(dnsmasq_args) #Vmit.logger.info " dnsmasq spawned with pid #{dnsmasq_pid}" end |
#to_s ⇒ Object
82 83 84 |
# File 'lib/vmit/network.rb', line 82 def to_s "#{@brdevice}:#{@address.to_string}" end |