Previous page

Next page

Locate page in Contents

Adding a Network Adapter

To add a new virtual network adapter to a virtual machine, the following steps must be taken:

  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.

Example

"""

    Add a network adapter to the virtual machine.

    @param vm: An instance of prlsdkapi.Vm class identifying the

               virtual machine.

    @param networking_type: Host-only/shared/bridged. Use one of the

                            consts.PNA_xxx constants.

    @param bound_default: Used with bridged networking only.

                          Specify True to bound a new adapter to the

                          default physical adapter. If False is passed,

                          the adapter will be bound to a specific physical

                          adapter (in this example, the adapter is

                          chosen randomly).

"""

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

  

    # Begin the virtual machine editing operation.

    try:

        vm.begin_edit().wait()

    except prlsdkapi.PrlSDKError, e:

        print "Error: %s" % e

        return

  

    # Obtain the VmConfig object containing the virtual machine

    # configuration information.

    vm_config = vm.get_config()    

  

    # Create an instance of the prlsdkapi.VmNet class.

    net_adapter = vm_config.create_vm_dev(consts.PDE_GENERIC_NETWORK_ADAPTER)    

    

    # Set the emulation type to the specified value.

    net_adapter.set_emulated_type(networking_type)

  

    # For bridged netowkring mode, we'll have to bind the

    # new adapter to a network adapter on the host machine.

    if networking_type == consts.PNA_BRIDGED_ETHERNET:

  

        # To use the default adapter, simply set the

        # adapter index to -1.

        if bound_default == True:

            net_adapter.set_bound_adapter_index(-1)

        else:

            # To use a specific adapter, first obtain the

            # list of the adapters from the host machine.

            

            # Obtain an instance of prlsdkapi.ServerConfig containing

            # the host configuration information.

            try:

                result = server.get_srv_config().wait()

            except prlsdkapi.PrlSDKError, e:

                print "Error: %s" % e

  

            srv_config = result.get_param()

  

            # Iterate through the list of the host network adapters.

            # In this example, we are simply selecting the first

            # adapter in the list and binding the virtual adapter to it.

            # The adapter is identified by its name.

            for i in range(srv_config.get_net_adapters_count()):

                hw_net_adapter = srv_config.get_net_adapter(i)

                hw_net_adapter_name = hw_net_adapter.get_name()

                net_adapter.set_bound_adapter_name(hw_net_adapter_name)

                exit

  

    # Connect and enable the new virtual adapter.

    net_adapter.set_connected(True)

    net_adapter.set_enabled(True)

  

    # Commit the changes.

    try:

       vm.commit().wait()

    except prlsdkapi.PrlSDKError, e:

        print "Error: %s" % e

        return

  

    print("Virtual network adapter created successfully")