Class | VirtP2V::NetworkDevice |
In: |
lib/virt-p2v/netdevice.rb
lib/virt-p2v/netdevice.rb |
Parent: | Object |
DEVICE | = | 'org.freedesktop.NetworkManager.Device'.freeze | Some NetworkManager names, for convenience | |
NETWORKMANAGER | = | 'org.freedesktop.NetworkManager'.freeze | ||
PROPERTIES | = | 'org.freedesktop.DBus.Properties'.freeze | ||
WIRED | = | 'org.freedesktop.NetworkManager.Device.Wired'.freeze | ||
TYPE_UNKNOWN | = | 0 | NetworkManager device types projects.gnome.org/NetworkManager/developers/spec-08.html | |
TYPE_ETHERNET | = | 1 | ||
TYPE_WIFI | = | 2 | ||
TYPE_GSM | = | 3 | ||
TYPE_CDMA | = | 4 | ||
DEVICE | = | 'org.freedesktop.NetworkManager.Device'.freeze | Some NetworkManager names, for convenience | |
NETWORKMANAGER | = | 'org.freedesktop.NetworkManager'.freeze | ||
PROPERTIES | = | 'org.freedesktop.DBus.Properties'.freeze | ||
WIRED | = | 'org.freedesktop.NetworkManager.Device.Wired'.freeze | ||
TYPE_UNKNOWN | = | 0 | NetworkManager device types projects.gnome.org/NetworkManager/developers/spec-08.html | |
TYPE_ETHERNET | = | 1 | ||
TYPE_WIFI | = | 2 | ||
TYPE_GSM | = | 3 | ||
TYPE_CDMA | = | 4 |
activated | [R] | |
activated | [R] | |
connected | [R] | |
connected | [R] | |
mac | [R] | |
mac | [R] | |
name | [R] | |
name | [R] | |
state | [R] | |
state | [R] |
# File lib/virt-p2v/netdevice.rb, line 66 66: def self.add_listener(cb) 67: @@listeners.push(cb) 68: end
# File lib/virt-p2v/netdevice.rb, line 66 66: def self.add_listener(cb) 67: @@listeners.push(cb) 68: end
# File lib/virt-p2v/netdevice.rb, line 38 38: def initialize(obj, device, props) 39: device.default_iface = WIRED 40: 41: @nm_obj = obj 42: @name = props.Get(DEVICE, 'Interface')[0] 43: @mac = props.Get(WIRED, 'HwAddress')[0] 44: state = props.Get(DEVICE, 'State')[0] 45: 46: # Lookup by name 47: @@devices[@name] = self 48: 49: state_updated(state) 50: 51: # Register a listener for state changes 52: device.on_signal('PropertiesChanged') { |props| 53: if props.has_key?('State') then 54: state_updated(props['State']) 55: 56: # Notify registered state change handlers 57: @@listeners.each { |cb| cb.call(self) } 58: end 59: } 60: end
# File lib/virt-p2v/netdevice.rb, line 38 38: def initialize(obj, device, props) 39: device.default_iface = WIRED 40: 41: @nm_obj = obj 42: @name = props.Get(DEVICE, 'Interface')[0] 43: @mac = props.Get(WIRED, 'HwAddress')[0] 44: state = props.Get(DEVICE, 'State')[0] 45: 46: # Lookup by name 47: @@devices[@name] = self 48: 49: state_updated(state) 50: 51: # Register a listener for state changes 52: device.on_signal('PropertiesChanged') { |props| 53: if props.has_key?('State') then 54: state_updated(props['State']) 55: 56: # Notify registered state change handlers 57: @@listeners.each { |cb| cb.call(self) } 58: end 59: } 60: end
# File lib/virt-p2v/netdevice.rb, line 74 74: def activate(auto, ip, prefix, gateway, dns) 75: # Get an IP config dependent on whether ip is IPv4 or IPv6 76: ip_config = auto ? get_config_auto : 77: ip.ipv4? ? get_config_ipv4(ip, prefix, gateway, dns) : 78: get_config_ipv6(ip, prefix, gateway, dns) 79: 80: # Create a new NetworkManager connection object 81: settings = @@nm_service.object(SETTINGS_PATH) 82: settings.introspect() 83: settings.default_iface = SETTINGS 84: 85: uuid = `uuidgen`.chomp 86: conn = settings.AddConnection( 87: 'connection' => { 88: 'uuid' => uuid, 89: 'id' => 'P2V', 90: 'type' => '802-3-ethernet', 91: 'autoconnect' => false 92: }, 93: '802-3-ethernet' => {}, 94: 'ipv4' => ip_config['ipv4'], 95: 'ipv6' => ip_config['ipv6'] 96: ) 97: 98: nm = @@nm_service.object('/org/freedesktop/NetworkManager') 99: nm.introspect 100: nm.default_iface = NETWORKMANAGER 101: 102: if NM_API_09 103: nm.ActivateConnection(conn[0], @nm_obj, '/') 104: else 105: # Find the connection we just created 106: # NM before version 0.9 didn't provide a sensible way to get the 107: # path of the connection object we just created 108: conn = settings.ListConnections()[0].each { |i| 109: conn = @@nm_service.object(i) 110: conn.introspect 111: conn.default_iface = CONNECTION 112: break i if conn.GetSettings()[0]['connection']['uuid'] == uuid 113: } 114: 115: # XXX: mbooth@redhat.com - 22/7/2011 The first time this code runs 116: # on a RHEL 6 system (NetworkManager-0.8.1-9.el6_1.1.i686), conn 117: # will be an array containing a single element: the connection. This 118: # will cause ActivateConnection below to return an error, and the 119: # p2v client to crash. If you run p2v client a second time, conn 120: # will be a simple value, not a single element array, and 121: # ActivateConnection works fine. I assume this is a bug in 122: # NetworkManager. I don't see this behaviour in F14. 123: conn = conn[0] if conn.kind_of?(Array) 124: 125: nm.ActivateConnection( 126: 'org.freedesktop.NetworkManagerSystemSettings', 127: conn, @nm_obj, '/' 128: ) 129: end 130: end
# File lib/virt-p2v/netdevice.rb, line 74 74: def activate(auto, ip, prefix, gateway, dns) 75: # Get an IP config dependent on whether ip is IPv4 or IPv6 76: ip_config = auto ? get_config_auto : 77: ip.ipv4? ? get_config_ipv4(ip, prefix, gateway, dns) : 78: get_config_ipv6(ip, prefix, gateway, dns) 79: 80: # Create a new NetworkManager connection object 81: settings = @@nm_service.object(SETTINGS_PATH) 82: settings.introspect() 83: settings.default_iface = SETTINGS 84: 85: uuid = `uuidgen`.chomp 86: conn = settings.AddConnection( 87: 'connection' => { 88: 'uuid' => uuid, 89: 'id' => 'P2V', 90: 'type' => '802-3-ethernet', 91: 'autoconnect' => false 92: }, 93: '802-3-ethernet' => {}, 94: 'ipv4' => ip_config['ipv4'], 95: 'ipv6' => ip_config['ipv6'] 96: ) 97: 98: nm = @@nm_service.object('/org/freedesktop/NetworkManager') 99: nm.introspect 100: nm.default_iface = NETWORKMANAGER 101: 102: if NM_API_09 103: nm.ActivateConnection(conn[0], @nm_obj, '/') 104: else 105: # Find the connection we just created 106: # NM before version 0.9 didn't provide a sensible way to get the 107: # path of the connection object we just created 108: conn = settings.ListConnections()[0].each { |i| 109: conn = @@nm_service.object(i) 110: conn.introspect 111: conn.default_iface = CONNECTION 112: break i if conn.GetSettings()[0]['connection']['uuid'] == uuid 113: } 114: 115: # XXX: mbooth@redhat.com - 22/7/2011 The first time this code runs 116: # on a RHEL 6 system (NetworkManager-0.8.1-9.el6_1.1.i686), conn 117: # will be an array containing a single element: the connection. This 118: # will cause ActivateConnection below to return an error, and the 119: # p2v client to crash. If you run p2v client a second time, conn 120: # will be a simple value, not a single element array, and 121: # ActivateConnection works fine. I assume this is a bug in 122: # NetworkManager. I don't see this behaviour in F14. 123: conn = conn[0] if conn.kind_of?(Array) 124: 125: nm.ActivateConnection( 126: 'org.freedesktop.NetworkManagerSystemSettings', 127: conn, @nm_obj, '/' 128: ) 129: end 130: end