function get_network_res($network)
 {
     if ($network == false) {
         return false;
     }
     if (is_resource($network)) {
         return $network;
     }
     $tmp = libvirt_network_get($this->conn, $network);
     return $tmp ? $tmp : $this->_set_last_error();
 }
Ejemplo n.º 2
0
/**
 * connect a NIC to a network in a virtual machine on the virt host
 *
 * @todo FIX the VIRSH portion of this function
 *
 * @global  resource $host_conn           libvirt connection
 *
 * @param   string   $host_name           virt_id of host (to pass to virsh for activation only)
 * @param   string   $vm_virt_id          virt_id of the vm
 * @param   int      $vm_nic_instance     instance of NIC device
 * @param   string   $vm_nic_mac_address  mac address of the NIC
 * @param   string   $new_network_virt_id virt_id of network
 *
 * @return  bool                on success
 */
function ace_kvm_vm_nic_connect_network($host_name, $vm_virt_id, $vm_nic_instance, $vm_nic_mac_address, $new_network_virt_id)
{
    global $host_conn;
    $vm_res = libvirt_domain_lookup_by_name($host_conn, $vm_virt_id);
    ace_kvm_log_last_error();
    $vm_is_active = libvirt_domain_is_active($vm_res) == 1 ? TRUE : FALSE;
    ace_kvm_log_last_error();
    $old_network_virt_id = ace_kvm_vm_get_nic_network_by_mac($vm_virt_id, $vm_nic_mac_address);
    $old_network_res = libvirt_network_get($host_conn, $old_network_virt_id);
    ace_kvm_log_last_error();
    $old_virt_network_is_active = libvirt_network_get_active($old_network_res) == 1 ? TRUE : FALSE;
    ace_kvm_log_last_error();
    $new_network_res = libvirt_network_get($host_conn, $new_network_virt_id);
    ace_kvm_log_last_error();
    $new_virt_network_is_active = libvirt_network_get_active($new_network_res) == 1 ? TRUE : FALSE;
    ace_kvm_log_last_error();
    $link_state = $new_virt_network_is_active ? 'up' : 'down';
    $xml = "<interface type='network'>\n\t\t\t\t<mac address='{$vm_nic_mac_address}'/>\n\t\t\t\t<source network='{$new_network_virt_id}'/>\n\t\t\t\t<link state='{$link_state}'/>\n\t\t\t</interface>";
    $libvirt_flags = $vm_is_active ? 3 : 2;
    # config + live : config only
    # temporarily activate old and/or new networks (excluding _DISCONNECTED.._) if either are currently deactivated
    if (!$old_virt_network_is_active && $old_network_virt_id !== _DISCONNECTED_VIRT_NETWORK_ID_) {
        ace_kvm_network_activate_virsh($host_name, $old_network_virt_id);
    }
    if (!$new_virt_network_is_active && $new_network_virt_id !== _DISCONNECTED_VIRT_NETWORK_ID_) {
        ace_kvm_network_activate_virsh($host_name, $new_network_virt_id);
    }
    $virt_success = libvirt_domain_update_device($vm_res, $xml, $libvirt_flags);
    ace_kvm_log_last_error();
    # deactivate old and/or new networks (excluding _DISCONNECTED.._) if either was temporarily activated
    if (!$old_virt_network_is_active && $old_network_virt_id !== _DISCONNECTED_VIRT_NETWORK_ID_) {
        ace_kvm_network_deactivate_virsh($host_name, $old_network_virt_id);
    }
    if (!$new_virt_network_is_active && $new_network_virt_id !== _DISCONNECTED_VIRT_NETWORK_ID_) {
        ace_kvm_network_deactivate_virsh($host_name, $new_network_virt_id);
    }
    return $virt_success;
}