[libvirt] [libvirt-php PATCH 0/4] Add more API bindings.

This patch series adds more libvirt API bindings to libvirt-php. The first patch in the series is a repost of: https://www.redhat.com/archives/libvir-list/2014-May/msg00021.html Dawid Zamirski (4): implement libvirt_node_get_free_memory implement xml_to_native and xml_from_native implement libvirt_domain_send_key_api implement bindings for attaching/detaching devices src/libvirt-php.c | 256 +++++++++++++++++++++++++++++++++++++++++++++++++++++- src/libvirt-php.h | 6 ++ 2 files changed, 259 insertions(+), 3 deletions(-) -- 2.7.4

From: Dawid Zamirski <dzamirski@dattobackup.com> This patch adds support for virNodeGetFreeMemory which is available in libvirt since v0.3.3. While the php bindings alredy provide libvirt_node_get_mem_stats from which such info could be obtained, not all hypervisors support it, e.g. vbox and esx driver don't have it but they do implement virNodeGetFreeMemory. Since virNodeGetFreeMemory returns free bytes as unsigned long long which PHP cannot handle, I've added LONGLONG_RETURN_AS_STRING macro which returns the amount bytes as a string - similarly to how bcmath handles such numbers. --- src/libvirt-php.c | 30 ++++++++++++++++++++++++++++++ src/libvirt-php.h | 1 + 2 files changed, 31 insertions(+) diff --git a/src/libvirt-php.c b/src/libvirt-php.c index 6f6e137..0e9e6b5 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -664,6 +664,7 @@ static zend_function_entry libvirt_functions[] = { PHP_FE(libvirt_node_get_cpu_stats, arginfo_libvirt_conn_optcpunr) PHP_FE(libvirt_node_get_cpu_stats_for_each_cpu, arginfo_libvirt_conn_opttime) PHP_FE(libvirt_node_get_mem_stats, arginfo_libvirt_conn) + PHP_FE(libvirt_node_get_free_memory, arginfo_libvirt_conn) /* Nodedev functions */ PHP_FE(libvirt_nodedev_get, arginfo_libvirt_conn) PHP_FE(libvirt_nodedev_capabilities, arginfo_libvirt_conn) @@ -2089,6 +2090,11 @@ else \ add_index_long(out, key,in); \ } +#define LONGLONG_RETURN_AS_STRING(in) \ + snprintf(tmpnumber, 63, "%llu", in); \ + VIRT_RETURN_STRING(tmpnumber); + + /* Authentication callback function. Should receive list of credentials via cbdata and pass the requested one to libvirt */ static int libvirt_virConnectAuthCallback(virConnectCredentialPtr cred, unsigned int ncred, void *cbdata) { @@ -2578,6 +2584,30 @@ PHP_FUNCTION(libvirt_node_get_mem_stats) params = NULL; } +/* + * Function name: libvirt_node_get_free_memory + * Since version: 0.5.3 + * Description: Function is used to get free memory available on the node. + * Arguments: @conn [resource]: resource for connection. + * Returns: The available free memery in bytes as string or FALSE for error. + */ +PHP_FUNCTION(libvirt_node_get_free_memory) +{ + php_libvirt_connection *conn = NULL; + zval *zconn; + unsigned long long ret; + LONGLONG_INIT; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + if ((ret = virNodeGetFreeMemory(conn->conn)) != 0) { + LONGLONG_RETURN_AS_STRING(ret); + } else { + set_error("Cannot get the free memory for the node" TSRMLS_CC); + RETURN_FALSE; + } +} + //virsh capabilities | xpath '//capabilities/guest/arch[@name="x86_64"]/machine[@maxCpus=1]' /* diff --git a/src/libvirt-php.h b/src/libvirt-php.h index a8cccbe..561ccff 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -366,6 +366,7 @@ PHP_FUNCTION(libvirt_node_get_info); PHP_FUNCTION(libvirt_node_get_cpu_stats); PHP_FUNCTION(libvirt_node_get_cpu_stats_for_each_cpu); PHP_FUNCTION(libvirt_node_get_mem_stats); +PHP_FUNCTION(libvirt_node_get_free_memory); /* Stream functions */ PHP_FUNCTION(libvirt_stream_create); PHP_FUNCTION(libvirt_stream_close); -- 2.7.4

On 29.06.2016 18:29, Dawid Zamirski wrote:
From: Dawid Zamirski <dzamirski@dattobackup.com>
This patch adds support for virNodeGetFreeMemory which is available in libvirt since v0.3.3. While the php bindings alredy provide libvirt_node_get_mem_stats from which such info could be obtained, not all hypervisors support it, e.g. vbox and esx driver don't have it but they do implement virNodeGetFreeMemory.
Since virNodeGetFreeMemory returns free bytes as unsigned long long which PHP cannot handle, I've added LONGLONG_RETURN_AS_STRING macro which returns the amount bytes as a string - similarly to how bcmath handles such numbers. --- src/libvirt-php.c | 30 ++++++++++++++++++++++++++++++ src/libvirt-php.h | 1 + 2 files changed, 31 insertions(+)
diff --git a/src/libvirt-php.c b/src/libvirt-php.c index 6f6e137..0e9e6b5 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -664,6 +664,7 @@ static zend_function_entry libvirt_functions[] = { PHP_FE(libvirt_node_get_cpu_stats, arginfo_libvirt_conn_optcpunr) PHP_FE(libvirt_node_get_cpu_stats_for_each_cpu, arginfo_libvirt_conn_opttime) PHP_FE(libvirt_node_get_mem_stats, arginfo_libvirt_conn) + PHP_FE(libvirt_node_get_free_memory, arginfo_libvirt_conn) /* Nodedev functions */ PHP_FE(libvirt_nodedev_get, arginfo_libvirt_conn) PHP_FE(libvirt_nodedev_capabilities, arginfo_libvirt_conn) @@ -2089,6 +2090,11 @@ else \ add_index_long(out, key,in); \ }
+#define LONGLONG_RETURN_AS_STRING(in) \ + snprintf(tmpnumber, 63, "%llu", in); \ + VIRT_RETURN_STRING(tmpnumber); + + /* Authentication callback function. Should receive list of credentials via cbdata and pass the requested one to libvirt */ static int libvirt_virConnectAuthCallback(virConnectCredentialPtr cred, unsigned int ncred, void *cbdata) { @@ -2578,6 +2584,30 @@ PHP_FUNCTION(libvirt_node_get_mem_stats) params = NULL; }
+/* + * Function name: libvirt_node_get_free_memory + * Since version: 0.5.3 + * Description: Function is used to get free memory available on the node. + * Arguments: @conn [resource]: resource for connection. + * Returns: The available free memery in bytes as string or FALSE for error. + */ +PHP_FUNCTION(libvirt_node_get_free_memory) +{ + php_libvirt_connection *conn = NULL; + zval *zconn; + unsigned long long ret; + LONGLONG_INIT; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + if ((ret = virNodeGetFreeMemory(conn->conn)) != 0) { + LONGLONG_RETURN_AS_STRING(ret);
Interesting approach.
+ } else { + set_error("Cannot get the free memory for the node" TSRMLS_CC); + RETURN_FALSE; + } +} + //virsh capabilities | xpath '//capabilities/guest/arch[@name="x86_64"]/machine[@maxCpus=1]'
/* diff --git a/src/libvirt-php.h b/src/libvirt-php.h index a8cccbe..561ccff 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -366,6 +366,7 @@ PHP_FUNCTION(libvirt_node_get_info); PHP_FUNCTION(libvirt_node_get_cpu_stats); PHP_FUNCTION(libvirt_node_get_cpu_stats_for_each_cpu); PHP_FUNCTION(libvirt_node_get_mem_stats); +PHP_FUNCTION(libvirt_node_get_free_memory); /* Stream functions */ PHP_FUNCTION(libvirt_stream_create); PHP_FUNCTION(libvirt_stream_close);
ACK Michal

From: Dawid Zamirski <dzamirski@dattobackup.com> This patch implements virConnectDomainXMLFromNative and virConnectDomainXMLToNative in libvirt PHP bindings. The PHP functions are called libvirt_domain_xml_from_native and libvirt_domain_xml_to_native respectively. --- src/libvirt-php.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/libvirt-php.h | 2 ++ 2 files changed, 82 insertions(+) diff --git a/src/libvirt-php.c b/src/libvirt-php.c index 0e9e6b5..8602b93 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -252,6 +252,18 @@ ZEND_ARG_INFO(0, conn) ZEND_ARG_INFO(0, flags) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO_EX(arginfo_libvirt_domain_xml_from_native, 0, 0, 3) +ZEND_ARG_INFO(0, conn) +ZEND_ARG_INFO(0, format) +ZEND_ARG_INFO(0, config_data) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_libvirt_domain_xml_to_native, 0, 0, 3) +ZEND_ARG_INFO(0, conn) +ZEND_ARG_INFO(0, format) +ZEND_ARG_INFO(0, xml_data) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_libvirt_domain_memory_peek, 0, 0, 4) ZEND_ARG_INFO(0, conn) ZEND_ARG_INFO(0, start) @@ -580,6 +592,8 @@ static zend_function_entry libvirt_functions[] = { PHP_FE(libvirt_domain_reboot, arginfo_libvirt_conn_flags) PHP_FE(libvirt_domain_define_xml, arginfo_libvirt_conn_xml) PHP_FE(libvirt_domain_create_xml, arginfo_libvirt_conn_xml) + PHP_FE(libvirt_domain_xml_from_native, arginfo_libvirt_domain_xml_from_native) + PHP_FE(libvirt_domain_xml_to_native, arginfo_libvirt_domain_xml_to_native) PHP_FE(libvirt_domain_memory_peek, arginfo_libvirt_domain_memory_peek) PHP_FE(libvirt_domain_memory_stats, arginfo_libvirt_conn_flags) PHP_FE(libvirt_domain_set_memory, arginfo_libvirt_domain_set_memory) @@ -6695,6 +6709,72 @@ PHP_FUNCTION(libvirt_domain_create_xml) } /* + * Function name: libvirt_domain_xml_from_native + * Since version: 0.5.3 + * Description: Function is used to convert native configuration data to libvirt domain XML + * Arguments: @conn [resource]: libvirt connection resource + * @format [string]: configuration format converting from + * @config_data [string]: content of the native config file + * Returns: libvirt domain XML, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_xml_from_native) +{ + php_libvirt_connection *conn = NULL; + zval *zconn; + char *config_data = NULL; + char *format = NULL; + char *xml = NULL; + strsize_t config_data_len; + strsize_t format_len; + unsigned int flags = 0; + + GET_CONNECTION_FROM_ARGS("rss", &zconn, &format, &format_len, &config_data, &config_data_len); + + xml = virConnectDomainXMLFromNative(conn->conn, format, config_data, flags); + + if (xml == NULL) { + set_error_if_unset("Cannot convert native format to XML" TSRMLS_CC); + RETURN_FALSE; + } + + VIRT_RETVAL_STRING(xml); + free(xml); +} + +/* + * Function name: libvirt_domain_xml_to_native + * Since version: 0.5.3 + * Description: Function is used to convert libvirt domain XML to native configuration + * Arguments: @conn [resource]: libvirt connection resource + * @format [string]: configuration format converting to + * @xml_data [string]: content of the libvirt domain xml file + * Returns: contents of the native data file, FALSE on error +*/ +PHP_FUNCTION(libvirt_domain_xml_to_native) +{ + php_libvirt_connection *conn = NULL; + zval *zconn; + char *xml_data = NULL; + char *format = NULL; + char *config_data = NULL; + strsize_t xml_data_len; + strsize_t format_len; + unsigned int flags = 0; + + GET_CONNECTION_FROM_ARGS("rss", &zconn, &format, &format_len, &xml_data, &xml_data_len); + + config_data = virConnectDomainXMLToNative(conn->conn, format, xml_data, flags); + + if (config_data == NULL) { + set_error_if_unset("Cannot convert to native format from XML" TSRMLS_CC); + RETURN_FALSE; + } + + VIRT_RETVAL_STRING(config_data); + free(config_data); +} + +/* * Function name: libvirt_domain_memory_peek * Since version: 0.4.1(-1) * Description: Function is used to get the domain's memory peek value diff --git a/src/libvirt-php.h b/src/libvirt-php.h index 561ccff..9f89ddd 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -412,6 +412,8 @@ PHP_FUNCTION(libvirt_domain_undefine); PHP_FUNCTION(libvirt_domain_reboot); PHP_FUNCTION(libvirt_domain_define_xml); PHP_FUNCTION(libvirt_domain_create_xml); +PHP_FUNCTION(libvirt_domain_xml_from_native); +PHP_FUNCTION(libvirt_domain_xml_to_native); PHP_FUNCTION(libvirt_domain_set_max_memory); PHP_FUNCTION(libvirt_domain_set_memory); PHP_FUNCTION(libvirt_domain_set_memory_flags); -- 2.7.4

This patch adds libvirt_domain_send_key_api which calls to virDomainSendKey libvirt API function to send key press events to a domain. The PHP bindings already have libvirt_domain_send_keys function which uses VNC to provide similar functionality but that has limited use cases and depends on a command line VNC client as well as VNC server being present which is not always the case. --- src/libvirt-php.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/libvirt-php.h | 1 + 2 files changed, 77 insertions(+) diff --git a/src/libvirt-php.c b/src/libvirt-php.c index 8602b93..bf73f43 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -405,6 +405,14 @@ ZEND_ARG_INFO(0, server) ZEND_ARG_INFO(0, scancode) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO_EX(arginfo_libvirt_domain_send_key_api, 0, 0, 4) +ZEND_ARG_INFO(0, conn) +ZEND_ARG_INFO(0, codeset) +ZEND_ARG_INFO(0, holdime) +ZEND_ARG_INFO(0, keycodes) +ZEND_ARG_INFO(0, flags) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_libvirt_domain_send_pointer_event, 0, 0, 5) ZEND_ARG_INFO(0, conn) ZEND_ARG_INFO(0, server) @@ -624,6 +632,7 @@ static zend_function_entry libvirt_functions[] = { PHP_FE(libvirt_domain_get_screenshot_api, arginfo_libvirt_domain_get_screenshot_api) PHP_FE(libvirt_domain_get_screen_dimensions, arginfo_libvirt_domain_get_screen_dimensions) PHP_FE(libvirt_domain_send_keys, arginfo_libvirt_domain_send_keys) + PHP_FE(libvirt_domain_send_key_api, arginfo_libvirt_domain_send_key_api) PHP_FE(libvirt_domain_send_pointer_event, arginfo_libvirt_domain_send_pointer_event) PHP_FE(libvirt_domain_update_device, arginfo_libvirt_domain_update_device) /* Domain snapshot functions */ @@ -1988,6 +1997,18 @@ PHP_MINIT_FUNCTION(libvirt) /* Connect flags */ REGISTER_LONG_CONSTANT("VIR_CONNECT_FLAG_SOUNDHW_GET_NAMES", CONNECT_FLAG_SOUNDHW_GET_NAMES, CONST_CS | CONST_PERSISTENT); + /* Keycodeset constants */ + REGISTER_LONG_CONSTANT("VIR_KEYCODE_SET_LINUX", VIR_KEYCODE_SET_LINUX, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_KEYCODE_SET_XT", VIR_KEYCODE_SET_XT, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_KEYCODE_SET_ATSET1", VIR_KEYCODE_SET_ATSET1, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_KEYCODE_SET_ATSET2", VIR_KEYCODE_SET_ATSET2, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_KEYCODE_SET_ATSET3", VIR_KEYCODE_SET_ATSET3, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_KEYCODE_SET_OSX", VIR_KEYCODE_SET_OSX, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_KEYCODE_SET_XT_KBD", VIR_KEYCODE_SET_XT_KBD, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_KEYCODE_SET_USB", VIR_KEYCODE_SET_USB, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_KEYCODE_SET_WIN32", VIR_KEYCODE_SET_WIN32, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_KEYCODE_SET_RFB", VIR_KEYCODE_SET_RFB, CONST_CS | CONST_PERSISTENT); + REGISTER_INI_ENTRIES(); /* Initialize libvirt and set up error callback */ @@ -5061,6 +5082,61 @@ PHP_FUNCTION(libvirt_domain_send_keys) } /* + * Function name: libvirt_domain_send_key_api + * Since version: 0.5.3 + * Description: Function sends keys to domain via libvirt API + * Arguments: @res[resource]: libvirt domain resource, e.g. from libvirt_domaing_lookup_by_*() + * @codeset [int]: the codeset of keycodes, from virKeycodeSet + * @holdtime [int]: the duration (in miliseconds) that the keys will be held + * @keycodes [array]: array of keycodes + * @flags [int]: extra flags; not used yet so callers should alway pass 0 + * Returns: TRUE for success, FALSE for failure + */ +PHP_FUNCTION(libvirt_domain_send_key_api) +{ + php_libvirt_domain *domain = NULL; + zval *zdomain; + zend_long codeset; + zend_long holdtime = 0; + zend_long flags = 0; + zval *zkeycodes, *data = NULL; + HashTable *arr_hash = NULL; + HashPosition pointer; + int count, i; + uint *keycodes = NULL; + + GET_DOMAIN_FROM_ARGS("rlla|l", &zdomain, &codeset, &holdtime, &zkeycodes, + &flags); + + arr_hash = Z_ARRVAL_P(zkeycodes); + count = zend_hash_num_elements(arr_hash); + + keycodes = (uint *) emalloc(count * sizeof(uint)); + + i = 0; + for (zend_hash_internal_pointer_reset_ex(arr_hash, &pointer); +#if PHP_MAJOR_VERSION >= 7 + (data = zend_hash_get_current_data_ex(arr_hash, &pointer)) != NULL; +#else + zend_hash_get_current_data_ex(arr_hash, (void **) &data, &pointer) == SUCCESS; +#endif + zend_hash_move_forward_ex(arr_hash, &pointer)) { + if (Z_TYPE_P(data) == IS_LONG) { + keycodes[i++] = (uint) Z_LVAL_P(data); + } + } + + if (virDomainSendKey(domain->domain, codeset, holdtime, keycodes, count, + flags) != 0) { + efree(keycodes); + RETURN_FALSE; + } + + efree(keycodes); + RETURN_TRUE; +} + +/* * Function name: libvirt_domain_send_pointer_event * Since version: 0.4.2 * Description: Function sends keys to the domain's VNC window diff --git a/src/libvirt-php.h b/src/libvirt-php.h index 9f89ddd..eac3ed5 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -440,6 +440,7 @@ PHP_FUNCTION(libvirt_domain_set_autostart); PHP_FUNCTION(libvirt_domain_is_active); PHP_FUNCTION(libvirt_domain_get_next_dev_ids); PHP_FUNCTION(libvirt_domain_send_keys); +PHP_FUNCTION(libvirt_domain_send_key_api); PHP_FUNCTION(libvirt_domain_send_pointer_event); PHP_FUNCTION(libvirt_domain_get_metadata); PHP_FUNCTION(libvirt_domain_set_metadata); -- 2.7.4

Provides PHP bindings to libvirt's virDomainAttachDeviceFlags and virDomainDetachDeviceFlags APIs. --- src/libvirt-php.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- src/libvirt-php.h | 2 ++ 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/src/libvirt-php.c b/src/libvirt-php.c index bf73f43..85bfcc2 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -227,6 +227,18 @@ ZEND_ARG_INFO(0, dev) ZEND_ARG_INFO(0, flags) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO_EX(arginfo_libvirt_domain_attach_device, 0, 0, 2) +ZEND_ARG_INFO(0, conn) +ZEND_ARG_INFO(0, xml) +ZEND_ARG_INFO(0, flags) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_libvirt_domain_detach_device, 0, 0, 2) +ZEND_ARG_INFO(0, conn) +ZEND_ARG_INFO(0, xml) +ZEND_ARG_INFO(0, flags) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_libvirt_domain_lookup_by_id, 0, 0, 2) ZEND_ARG_INFO(0, conn) ZEND_ARG_INFO(0, id) @@ -581,6 +593,8 @@ static zend_function_entry libvirt_functions[] = { PHP_FE(libvirt_domain_disk_remove, arginfo_libvirt_domain_disk_remove) PHP_FE(libvirt_domain_nic_add, arginfo_libvirt_domain_nic_add) PHP_FE(libvirt_domain_nic_remove, arginfo_libvirt_domain_nic_remove) + PHP_FE(libvirt_domain_attach_device, arginfo_libvirt_domain_attach_device) + PHP_FE(libvirt_domain_detach_device, arginfo_libvirt_domain_detach_device) PHP_FE(libvirt_domain_get_info, arginfo_libvirt_conn) PHP_FE(libvirt_domain_get_name, arginfo_libvirt_conn) PHP_FE(libvirt_domain_get_uuid, arginfo_libvirt_conn) @@ -1968,9 +1982,9 @@ PHP_MINIT_FUNCTION(libvirt) REGISTER_LONG_CONSTANT("VIR_DOMAIN_METADATA_DESCRIPTION", 0, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("VIR_DOMAIN_METADATA_TITLE", 1, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("VIR_DOMAIN_METADATA_ELEMENT", 2, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("VIR_DOMAIN_AFFECT_CURRENT", 0, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("VIR_DOMAIN_AFFECT_LIVE", 1, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("VIR_DOMAIN_AFFECT_CONFIG", 2, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_DOMAIN_AFFECT_CURRENT", VIR_DOMAIN_AFFECT_CURRENT, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_DOMAIN_AFFECT_LIVE", VIR_DOMAIN_AFFECT_LIVE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_DOMAIN_AFFECT_CONFIG", VIR_DOMAIN_AFFECT_CONFIG, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("VIR_DOMAIN_STATS_STATE", VIR_DOMAIN_STATS_STATE, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("VIR_DOMAIN_STATS_CPU_TOTAL", VIR_DOMAIN_STATS_CPU_TOTAL, CONST_CS | CONST_PERSISTENT); @@ -6492,6 +6506,56 @@ PHP_FUNCTION(libvirt_domain_nic_remove) } /* + * Function name: libvirt_domain_attach_device + * Since version: 0.5.3 + * Description: Function is used to attach a virtual device to a domain. + * Arguments: @res [resource]: libvirt domain resource, e.g. from libvirt_domain_lookup_by_*() + * @xml [string]: XML description of one device. + * @flags [int]: optional flags to control how the device is attached. Defaults to VIR_DOMAIN_AFFECT_LIVE + * Returns: TRUE for success, FALSE on error. + */ +PHP_FUNCTION(libvirt_domain_attach_device) +{ + php_libvirt_domain *domain = NULL; + zval *zdomain = NULL; + char *xml = NULL; + strsize_t xml_len = 0; + zend_long flags = VIR_DOMAIN_AFFECT_LIVE; + + GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &xml, &xml_len, &flags); + + if (virDomainAttachDeviceFlags(domain->domain, xml, flags) < 0) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_detach_device + * Since version: 0.5.3 + * Description: Function is used to detach a virtual device from a domain. + * Arguments: @res [resource]: libvirt domain resource, e.g. from libvirt_domain_lookup_by_*() + * @xml [string]: XML description of one device. + * @flags [int]: optional flags to control how the device is attached. Defaults to VIR_DOMAIN_AFFECT_LIVE + * Returns: TRUE for success, FALSE on error. + */ +PHP_FUNCTION(libvirt_domain_detach_device) +{ + php_libvirt_domain *domain = NULL; + zval *zdomain = NULL; + char *xml = NULL; + strsize_t xml_len = 0; + zend_long flags = VIR_DOMAIN_AFFECT_LIVE; + + GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &xml, &xml_len, &flags); + + if (virDomainDetachDeviceFlags(domain->domain, xml, flags) < 0) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* * Function name: libvirt_domain_get_info * Since version: 0.4.1(-1) * Description: Function is used to get the domain's information diff --git a/src/libvirt-php.h b/src/libvirt-php.h index eac3ed5..f1ba9c9 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -393,6 +393,8 @@ PHP_FUNCTION(libvirt_domain_disk_add); PHP_FUNCTION(libvirt_domain_disk_remove); PHP_FUNCTION(libvirt_domain_nic_add); PHP_FUNCTION(libvirt_domain_nic_remove); +PHP_FUNCTION(libvirt_domain_attach_device); +PHP_FUNCTION(libvirt_domain_detach_device); PHP_FUNCTION(libvirt_domain_get_info); PHP_FUNCTION(libvirt_domain_get_uuid); PHP_FUNCTION(libvirt_domain_get_uuid_string); -- 2.7.4

On 29.06.2016 18:29, Dawid Zamirski wrote:
This patch series adds more libvirt API bindings to libvirt-php. The first patch in the series is a repost of:
https://www.redhat.com/archives/libvir-list/2014-May/msg00021.html
Dawid Zamirski (4): implement libvirt_node_get_free_memory implement xml_to_native and xml_from_native implement libvirt_domain_send_key_api implement bindings for attaching/detaching devices
src/libvirt-php.c | 256 +++++++++++++++++++++++++++++++++++++++++++++++++++++- src/libvirt-php.h | 6 ++ 2 files changed, 259 insertions(+), 3 deletions(-)
ACKed and pushed. Thank you. Michal
participants (2)
-
Dawid Zamirski
-
Michal Privoznik