0

I'm currently using XCP-ng 7.6.3, and wanting to add a little security and take advantage of traffic shaping, I decided to PCI pass-through the NICs. In order to be able to reach the dom0, I created a SSPN shared only between the dom0 and the firewall sitting in a domU. Starting the domU spawns a virtual interface vif<X>.<Y> with an attached xapi<Z>, and by manually assigning an IP to the latter, I'm enable to remote in the management. So far, so good, and everything works as intended.

The problem is that I found no way to let the dom0 automatically assign an IP for itself when bringing up the virtual interface. I've been looking at XCP, Xen and XenServer documentation for a long time, but can only find ways to automatically assign an IP to the domU on the other end, not the dom0. Is it possible to do so automatically?

If not through xapi/XCP configuration, is there a way (even scripted) to attach a static IP to the spawned interface programmatically, even when the interface spawns with a different name?

Any pointer is appreciated.

0

After a few days of tinkering I concluded that it's possible. I crafted a service in python with the help of the XenAPI python module that is present in the dom0. Repository here, info and manual references here.

In short, a snippet like the following:

with xenapi_session() as x:
    vms = x.VM.get_all_records()  # Get a list of VMs for multiple uses

    # Find the ObscureRef of the target VM
    vmref = [k for k in vms.keys() if vms[k]['name_label'] == VMNAME][0]

    # Non-blocking listen for VM events
    token = ''  # Initial token
    while bEndless:
        output = x.event_from(['VM'], token, EVT_TIMEOUT)
        token = output['token']  # Current token

        for event in output['events']:
            # Check the IP assigned to the VIFs of the target VM, if it's running
            if (('add' == event['operation']) or
                    ('mod' == event['operation'])) and \
                    (vmref == event['ref']) and \
                    ('Running' == x.VM.get_power_state(vmref)):
                if 'snapshot' not in event:
                    continue
                vifs = event['snapshot']['VIFs']  # Virtual interfaces list
                for vif in vifs:
                    net = x.VIF.get_network(vif)  # Network ref
                    netrec = x.network.get_record(net)  # Network record
                    if SSPNNAME != netrec['name_label']:
                        continue

                    # netrec['bridge'] holds the xapiN identifier that can
                    # be used with "ip addr add"
                    rebind_ip_address(netrec['bridge'])

is (almost) all you need to craft a service that does what I initially asked. In the end, I added a few more useful functionalities in my server, like auto NFS/CIFS SR remount, but I won't digress here as it would go off topic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.