Some NetworkManager names, for convenience
NetworkManager device types projects.gnome.org/NetworkManager/developers/spec-08.html
# File lib/virt-p2v/netdevice.rb, line 71 def self.[](name) @@devices[name] end
# File lib/virt-p2v/netdevice.rb, line 67 def self.add_listener(cb) @@listeners.push(cb) end
# File lib/virt-p2v/netdevice.rb, line 63 def self.all_devices() @@devices.values end
# File lib/virt-p2v/netdevice.rb, line 39 def initialize(obj, device, props) device.default_iface = WIRED @nm_obj = obj @name = props.Get(DEVICE, 'Interface')[0] @mac = props.Get(WIRED, 'HwAddress')[0] state = props.Get(DEVICE, 'State')[0] # Lookup by name @@devices[@name] = self state_updated(state) # Register a listener for state changes device.on_signal('PropertiesChanged') { |props| if props.has_key?('State') then state_updated(props['State']) # Notify registered state change handlers @@listeners.each { |cb| cb.call(self) } end } end
# File lib/virt-p2v/netdevice.rb, line 75 def activate(auto, ip, prefix, gateway, dns) # Get an IP config dependent on whether ip is IPv4 or IPv6 ip_config = auto ? get_config_auto : ip.ipv4? ? get_config_ipv4(ip, prefix, gateway, dns) : get_config_ipv6(ip, prefix, gateway, dns) # Create a new NetworkManager connection object settings = @@nm_service.object(SETTINGS_PATH) settings.introspect() settings.default_iface = SETTINGS uuid = %xuuidgen`.chomp conn = settings.AddConnection( 'connection' => { 'uuid' => uuid, 'id' => 'P2V', 'type' => '802-3-ethernet', 'autoconnect' => false }, '802-3-ethernet' => {}, 'ipv4' => ip_config['ipv4'], 'ipv6' => ip_config['ipv6'] ) nm = @@nm_service.object('/org/freedesktop/NetworkManager') nm.introspect nm.default_iface = NETWORKMANAGER if NM_API_09 nm.ActivateConnection(conn[0], @nm_obj, '/') else # Find the connection we just created # NM before version 0.9 didn't provide a sensible way to get the # path of the connection object we just created conn = settings.ListConnections()[0].each { |i| conn = @@nm_service.object(i) conn.introspect conn.default_iface = CONNECTION break i if conn.GetSettings()[0]['connection']['uuid'] == uuid } # XXX: mbooth@redhat.com - 22/7/2011 The first time this code runs # on a RHEL 6 system (NetworkManager-0.8.1-9.el6_1.1.i686), conn # will be an array containing a single element: the connection. This # will cause ActivateConnection below to return an error, and the # p2v client to crash. If you run p2v client a second time, conn # will be a simple value, not a single element array, and # ActivateConnection works fine. I assume this is a bug in # NetworkManager. I don't see this behaviour in F14. conn = conn[0] if conn.kind_of?(Array) nm.ActivateConnection( 'org.freedesktop.NetworkManagerSystemSettings', conn, @nm_obj, '/' ) end end
# File lib/virt-p2v/netdevice.rb, line 148 def get_config_auto { 'ipv4' => { 'method' => 'auto' }, 'ipv6' => { 'method' => 'ignore' } } end
# File lib/virt-p2v/netdevice.rb, line 163 def get_config_ipv4(ip, prefix, gateway, dns) addresses = [[ ipv4_to_nm(ip), prefix, ipv4_to_nm(gateway) ]] dns_nm = [] dns.each{ |ipaddr| # Only use IPv4 DNS servers next unless ipaddr.ipv4? dns_nm.push(ipv4_to_nm(ipaddr)) } { 'ipv4' => { 'method' => 'manual', 'addresses' => [ 'aau', addresses ], 'dns' => [ 'au', dns_nm ] }, 'ipv6' => { 'method' => 'ignore' } } end
# File lib/virt-p2v/netdevice.rb, line 189 def get_config_ipv6(ip, prefix, gateway, dns) dns_nm = [] dns.each { |ipaddr| # Only use IPv6 DNS servers next unless ipaddr.ipv6? dns_nm.push(ipv6_to_nm(ipaddr)) } { 'ipv4' => { 'method' => 'disabled' }, 'ipv6' => { 'method' => 'manual', 'addresses' => [ 'a(ayu)', [[ ipv6_to_nm(ip), prefix ]] ], 'routes' => [ 'a(ayuayu)', [[ [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 0, ipv6_to_nm(gateway), 1024 ]] ], 'dns' => [ 'aay', dns_nm ] } } end
# File lib/virt-p2v/netdevice.rb, line 159 def ipv4_to_nm(ipaddr) ipaddr.hton().unpack("I")[0] end
# File lib/virt-p2v/netdevice.rb, line 185 def ipv6_to_nm(ipaddr) ipaddr.hton().unpack("c*") end
# File lib/virt-p2v/netdevice.rb, line 135 def state_updated(state) @connected = state > STATE_UNAVAILABLE @state = STATES[state] if state == STATE_ACTIVATED then @activated = true elsif state == STATE_FAILED then @activated = false else @activated = nil end end