Previous page

Next page

Locate page in Contents

Print this page

Adding a Network Adapter

The following sample function demonstrates how to add a network adapter to a a virtual machine. The steps are:

  1. Mark the beginning of the editing operation.
  2. Create a device object representing the new adapter.
  3. Set the emulation type (host-only, shared, or bridged).
  4. If creating a bridged adapter, select the host adapter to bind the new adapter to.
  5. Commit the changes.

The sample function accepts the following parameters:

server - an object of class Server identifying the host.

vm - an object of type Vm identifying the virtual machine.

networking_type - specifies the emulation type (host-only, shared, or bridged). The emulation type must be specified using one of the constants with the PDE_ prefix.

bound_default - if using a bridged networking, this boolean value indicates whether the new adapter should be bound to the default host adapter.

def add_net_adapter(server, vm, networking_type, bound_default):

  

    # Begin the virtual machine editing operation.

    try:

        vm.begin_edit()

    except prlsdkapi.PrlSDKAsyncError, e:

        print "Unable to modify vm configuration: %s" % e

        return

  

    # Create the device object and set its emulation type.

    net_dev = vm.create_device(consts.PDE_GENERIC_NETWORK_ADAPTER)    

    net_dev.emulated_type = networking_type

  

    # For bridged ethernet, we'll have to bind the new adapter to

    # a network adapter on the host machine.

    if net_dev.emulated_type == consts.PNA_BRIDGED_ETHERNET:

  

        # To use a default adapter, simply set the adapter index to -1.

        if bound_default == True:

            net_dev.bound_adapter_index = -1

        else:

            # To use a specific adapter, obtain the list of the adapters from the host machine.

            # First, obtain an object of type prlsdkapi.SrvConfig.

            srv_config = server.server_config

            try:

                srv_config.wait()

            except prlsdkapi.PrlSDKAsyncError, e:

                print "Unable to obtain server configuration: %s" % e

  

            # Now, iterate through the adapter list. The srv_config.net_adapters sequence

            # contains objects of type SrvCfgNet.

            # In this example, we are simply going to use the first adapter in the list.

            for netd in srv_config.net_adapters:

                net_dev.bound_adapter_name = netd.name

                exit

  

    # Connect and enable the new adapter.

    net_dev.connected = True

    net_dev.enabled = True

  

    # Commit the changes.

    try:

       vm.commit().wait()

    except prlsdkapi.PrlSDKAsyncError, e:

        print "Unable to commit vm config changes: %s" % e

        return

  

    print("Virtual network adapter created successfully")

Please send us your feedback on this help page