class VirtP2V::NetworkDevice

Constants

DEVICE

Some NetworkManager names, for convenience

NETWORKMANAGER
PROPERTIES
TYPE_CDMA
TYPE_ETHERNET
TYPE_GSM
TYPE_UNKNOWN

NetworkManager device types projects.gnome.org/NetworkManager/developers/spec-08.html

TYPE_WIFI
WIRED

Attributes

activated[R]
connected[R]
mac[R]
name[R]
state[R]

Public Class Methods

[](name) click to toggle source
# File lib/virt-p2v/netdevice.rb, line 70
def self.[](name)
    @@devices[name]
end
add_listener(cb) click to toggle source
# File lib/virt-p2v/netdevice.rb, line 66
def self.add_listener(cb)
    @@listeners.push(cb)
end
all_devices() click to toggle source
# File lib/virt-p2v/netdevice.rb, line 62
def self.all_devices()
    @@devices.values
end
new(obj, device, props) click to toggle source
# File lib/virt-p2v/netdevice.rb, line 38
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

Public Instance Methods

activate(auto, ip, prefix, gateway, dns) click to toggle source
# File lib/virt-p2v/netdevice.rb, line 74
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