Libvirt Functions
PHP Manual

libvirt_connect

(php-libvirt 0.1)

libvirt_connectGet a connection to the Hypervisor

Description

resource libvirt_connect ([ string $url= NULL [, bool $readonly= true [, array $credentials= Array() ]]] )

This function should be called first to get a connection to the Hypervisor. If necessary, authentication will be performed using supplied credentials.

Parameters

url

URL of the hypervisor to connect to. Can be for example qemu:///system or qemu+tcp:///system .

readonly

If TRUE (default) connection is made readonly.

credentials

Credentials to authenticate with. See authentication for more details.

Return Values

Returns FALSE on failure and connection resource on success. This connection resource must be used for all subsequent calls.

Errors/Exceptions

This function returns errors via PHP E_* error mechanism.

Notes

Note: Libvirt version
Version prior to 0.6.2 are not supported and using phplibvirt with libvirt prior to 0.6.2 will probably cause problems. libvirt_connect() will refuse to connect to libvirt daemon if the version is not at least 0.6.2.

Note: Authentication
You can authenticate to the libvirt daemon in several ways. If you are using policy kit and you are connecting locally, you can set it up to allow nonroot users. Just add to /etc/PolicyKit/PolicyKit.conf:

<match action="org.libvirt.unix.manage">
    <match user="httpduser">
      <return result="yes"/>
    </match>
</match>


If you are connecting to the TCP socket, you need to provide credentials. These credentials must be set beforehand using SASL. See » http://libvirt.org/auth.html#ACL_server_username for more details. You can You can specify the creentials as third argument. It is supposed to be an array in form of credential_type=>credential value. In example:

Array(VIR_CRED_AUTHNAME=>"fred",VIR_CRED_PASSPHRASE=>"fred");


Examples

Example #1 libvirt_connect() example

This example connects to the hypervisor in various ways.

<?php
//Anonymous readonly connection
$res1=libvirt_connect("qemu:///system");
print_r($res1); printf ("\n");

//Anonymous read-write connection
$res2=libvirt_connect("qemu:///system",false);
print_r($res2); printf ("\n");

//Read-write connection with authentication
$res3=libvirt_connect("qemu:///system",false,Array(VIR_CRED_AUTHNAME=>"fred",VIR_CRED_PASSPHRASE=>"fred"));
print_r($res3); printf ("\n");
?>

The above example will output something similar to:

Resource id #4
Resource id #5
Resource id #6


Libvirt Functions
PHP Manual