Beispiel #1
0
 def attach_interface(self, domain, network):
     """Attach new network interface on `domain` to `network`."""
     mac = generate_mac_address()
     self.run([
         'attach-interface', domain, 'network', network, '--mac', mac,
         '--model', 'virtio', '--config'
     ])
Beispiel #2
0
def get_lxd_nic_device(interface):
    """Convert a RequestedMachineInterface into a LXD device definition."""
    # LXD uses 'bridged' while MAAS uses 'bridge' so convert
    # the nictype.
    nictype = ("bridged" if interface.attach_type == InterfaceAttachType.BRIDGE
               else interface.attach_type)
    device = {
        "name": interface.ifname,
        "parent": interface.attach_name,
        "nictype": nictype,
        "type": "nic",
    }
    if interface.attach_type == InterfaceAttachType.SRIOV:
        device["hwaddr"] = generate_mac_address()
    if interface.attach_vlan is not None:
        device["vlan"] = str(interface.attach_vlan)
    return device
Beispiel #3
0
 def test_sriov(self):
     interface = RequestedMachineInterface(
         ifname=factory.make_name("ifname"),
         attach_name=factory.make_name("sriov"),
         attach_type=InterfaceAttachType.SRIOV,
     )
     generated_mac_address = generate_mac_address()
     mock_generate_mac = self.patch(lxd_module, "generate_mac_address")
     mock_generate_mac.return_value = generated_mac_address
     device = lxd_module.get_lxd_nic_device(interface)
     self.assertEqual(
         {
             "name": interface.ifname,
             "hwaddr": generated_mac_address,
             "parent": interface.attach_name,
             "nictype": "sriov",
             "type": "nic",
         },
         device,
     )
Beispiel #4
0
    def attach_interface(self, interface, domain, network):
        """Attach new network interface on `domain` to `network`."""
        mac = generate_mac_address()
        # If attachment type is not specified, default to network.
        if interface.attach_type in (None, InterfaceAttachType.NETWORK):
            # Set the network if we are explicity attaching a network
            # specified by the user.
            if interface.attach_type is not None:
                network = interface.attach_name
            return self.run([
                'attach-interface', domain, 'network', network, '--mac', mac,
                '--model', 'virtio', '--config'
            ])

        # For macvlans and bridges, we need to pass an XML template to
        # virsh's attach-device command since the attach-interface
        # command doesn't have a flag for setting the macvlan's mode.
        device_params = {
            'mac_address': mac,
            'attach_name': interface.attach_name,
        }
        if interface.attach_type == InterfaceAttachType.MACVLAN:
            device_params['attach_options'] = interface.attach_options
            device_xml = DOM_TEMPLATE_MACVLAN_INTERFACE.format(**device_params)
        elif interface.attach_type == InterfaceAttachType.BRIDGE:
            device_xml = DOM_TEMPLATE_BRIDGE_INTERFACE.format(**device_params)

        # Rewrite the XML in a temporary file to use with 'virsh define'.
        with NamedTemporaryFile() as f:
            f.write(device_xml.encode('utf-8'))
            f.write(b'\n')
            f.flush()
            output = self.run(['attach-device', domain, f.name, '--config'])
            if output.startswith('error:'):
                maaslog.error("%s: Failed to attach network device %s" %
                              (domain, interface.attach_name))
                return False
            maaslog.info("%s: Successfully attached network device %s" %
                         (domain, interface.attach_name))
            return True
Beispiel #5
0
def get_lxd_nic_device(name, interface, default_parent):
    """Convert a RequestedMachineInterface into a LXD device definition."""
    parent = interface.attach_name
    nictype = interface.attach_type
    if not parent:
        parent = default_parent.attach_name
        nictype = default_parent.attach_type

    # LXD uses 'bridged' while MAAS uses 'bridge' so convert the nictype.
    if nictype == InterfaceAttachType.BRIDGE:
        nictype = "bridged"

    device = {
        "name": name,
        "nictype": nictype,
        "parent": parent,
        "type": "nic",
    }
    if interface.attach_type == InterfaceAttachType.SRIOV:
        device["hwaddr"] = generate_mac_address()
    if interface.attach_vlan is not None:
        device["vlan"] = str(interface.attach_vlan)
    return device