[libvirt] [libvirt-php][PATCH 00/14] Couple of fixes

This is for examples/ reformat mostly. But there are some other fixes too. BTW: This is pushed, so if you have any objections - patches are welcome :) Michal Privoznik (14): gitignore: Ignore tags file configure: Provide default for php-confdir libvirt-php.spec.in: Honour php-{extensiondir,confdir} examples: Reformat libvirt.php examples: Fix 'if-else' formatting in libvirt.php examples: Fix curly braces indent in libvirt.php examples: Reformat header.php examples: Reformat index.php examples: Properly detect if we're connected examples: Don't report errors when looking up a domain examples: Show screenshot only for active domains examples: Better translate volume perms examples: Don't fetch volume count for inactive pools examples: Switch to ImageMagick for scaling pictures .gitignore | 1 + configure.ac | 2 + examples/header.php | 2 +- examples/index.php | 1335 +++++++++++++------------- examples/libvirt.php | 2317 +++++++++++++++++++++++----------------------- libvirt-php.spec.in | 7 +- m4/virt-php-confdir.m4 | 4 + m4/virt-php-extension.m4 | 35 + 8 files changed, 1865 insertions(+), 1838 deletions(-) create mode 100644 m4/virt-php-extension.m4 -- 2.8.4

Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 927a4db..8c52e3f 100644 --- a/.gitignore +++ b/.gitignore @@ -33,6 +33,7 @@ autom4te.cache/ m4/* src/*.so stamp-h1 +tags tools/generate-api-docs libvirt-php-*.tar.gz *~ -- 2.8.4

We use some really weird logic to get the directory where libvirt-php.ini should go. We try to parse 'php-config --configure' output. However, it may happen, that we learn nothing from that. If that's the case, we should provide some sensible default, /etc/php.d/ for instance. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- m4/virt-php-confdir.m4 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/m4/virt-php-confdir.m4 b/m4/virt-php-confdir.m4 index 4820de0..904d0c8 100644 --- a/m4/virt-php-confdir.m4 +++ b/m4/virt-php-confdir.m4 @@ -27,6 +27,10 @@ AC_DEFUN([LIBVIRT_CHECK_PHP_CONFDIR],[ if test "x$with_php_confdir" != "xno" ; then if test "x$with_php_confdir" = "xcheck" ; then confdir="$($PHPCONFIG --configure-options | sed -n 's/.*--with-config-file-scan-dir=\(\S*\).*/\1/p')" + if test "x$confdir" == "x" ; then + dnl There's no usable result from phpconfig. Use some default. + confdir="/etc/php.d/" + fi elif test "x$with_php_confdir" = "xno" || test "x$with_php_confdir" = "xyes"; then AC_MSG_ERROR([php-confdir must be used only with valid path]) else -- 2.8.4

So while we have the ability to specify where the module should be installed, we are not doing so in the spec file. It's not very wise. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- libvirt-php.spec.in | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libvirt-php.spec.in b/libvirt-php.spec.in index 5a11581..2aae1d5 100644 --- a/libvirt-php.spec.in +++ b/libvirt-php.spec.in @@ -58,7 +58,10 @@ This package contain the document for libvirt-php. %setup -q -n libvirt-php-%{version} %build -%configure --with-html-dir=%{_datadir}/doc --with-html-subdir=%{name}-%{version}/html --libdir=%{php_extdir} +%configure --with-html-dir=%{_datadir}/doc \ + --with-html-subdir=%{name}-%{version}/html \ + --with-php-extensiondir=%{php_extdir} \ + --with-php-confdir=%{php_confdir} make %{?_smp_mflags} %install @@ -72,7 +75,6 @@ rm -rf %{buildroot} %files %defattr(-,root,root) %{php_extdir}/libvirt-php.so -%{php_extdir}/libvirt-php.so.* %config(noreplace) %{php_confdir}/libvirt-php.ini %files -n libvirt-php-doc -- 2.8.4

This file is just one class wrapping some libvirt APIs. However, its formatting is just messy. Resolve this. Best viewed with: git show --ignore-space-change Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- examples/libvirt.php | 2308 +++++++++++++++++++++++++------------------------- 1 file changed, 1154 insertions(+), 1154 deletions(-) diff --git a/examples/libvirt.php b/examples/libvirt.php index cb1a0ad..89aeb9f 100644 --- a/examples/libvirt.php +++ b/examples/libvirt.php @@ -1,1177 +1,1177 @@ <?php - class Libvirt { - private $conn; - private $last_error; - private $allow_cached = true; - private $dominfos = array(); +class Libvirt { + private $conn; + private $last_error; + private $allow_cached = true; + private $dominfos = array(); - function Libvirt($uri = false, $debug=false) { - if ($debug) - $this->set_logfile($debug); - if ($uri != false) - $this->connect($uri); - } + function Libvirt($uri = false, $debug=false) { + if ($debug) + $this->set_logfile($debug); + if ($uri != false) + $this->connect($uri); + } - function _set_last_error() - { - $this->last_error = libvirt_get_last_error(); - return false; - } + function _set_last_error() + { + $this->last_error = libvirt_get_last_error(); + return false; + } - function set_logfile($filename) - { - if (!libvirt_logfile_set($filename)) - return $this->_set_last_error(); + function set_logfile($filename) + { + if (!libvirt_logfile_set($filename)) + return $this->_set_last_error(); - return true; - } + return true; + } - function print_resources() { - return libvirt_print_binding_resources(); - } + function print_resources() { + return libvirt_print_binding_resources(); + } - function connect($uri = 'null') { - $this->conn=libvirt_connect($uri, false); - if ($this->conn==false) - return $this->_set_last_error(); - } + function connect($uri = 'null') { + $this->conn=libvirt_connect($uri, false); + if ($this->conn==false) + return $this->_set_last_error(); + } - function domain_disk_add($domain, $img, $dev, $type='scsi', $driver='raw') { - $dom = $this->get_domain_object($domain); + function domain_disk_add($domain, $img, $dev, $type='scsi', $driver='raw') { + $dom = $this->get_domain_object($domain); - $tmp = libvirt_domain_disk_add($dom, $img, $dev, $type, $driver); - return ($tmp) ? $tmp : $this->_set_last_error(); + $tmp = libvirt_domain_disk_add($dom, $img, $dev, $type, $driver); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function domain_change_numVCpus($domain, $num) { + $dom = $this->get_domain_object($domain); + + $tmp = libvirt_domain_change_vcpus($dom, $num); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function domain_change_memory_allocation($domain, $memory, $maxmem) { + $dom = $this->get_domain_object($domain); + + $tmp = libvirt_domain_change_memory($dom, $memory, $maxmem); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function domain_change_boot_devices($domain, $first, $second) { + $dom = $this->get_domain_object($domain); + + $tmp = libvirt_domain_change_boot_devices($dom, $first, $second); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function domain_get_screenshot($domain) { + $dom = $this->get_domain_object($domain); + + $tmp = libvirt_domain_get_screenshot($dom, $this->get_hostname() ); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function domain_get_screenshot_thumbnail($domain, $w=120) { + $screen = $this->domain_get_screenshot($domain); + $imgFile = tempnam("/tmp", "libvirt-php-tmp-resize-XXXXXX");; + + if ($screen) { + $fp = fopen($imgFile, "wb"); + fwrite($fp, $screen); + fclose($fp); + } + + if (file_exists($imgFile) && $screen) { + list($width, $height) = getimagesize($imgFile); + $h = ($height / $width) * $w; + } else { + $w = $h = 1; + //$h = $w * (3 / 4.5); + } + + $new = imagecreatetruecolor($w, $h); + if ($screen) { + $img = imagecreatefrompng($imgFile); + imagecopyresampled($new,$img,0,0,0,0, $w,$h,$width,$height); + imagedestroy($img); + } + else { + $c = imagecolorallocate($new, 255, 255, 255); + imagefill($new, 0, 0, $c); + } + + imagepng($new, $imgFile); + imagedestroy($new); + + $fp = fopen($imgFile, "rb"); + $data = fread($fp, filesize($imgFile)); + fclose($fp); + + unlink($imgFile); + return $data; + } + + function domain_get_screen_dimensions($domain) { + $screen = $this->domain_get_screenshot($domain); + $imgFile = tempnam("/tmp", "libvirt-php-tmp-resize-XXXXXX");; + + $width = false; + $height = false; + + if ($screen) { + $fp = fopen($imgFile, "wb"); + fwrite($fp, $screen); + fclose($fp); + } + if (file_exists($imgFile) && $screen) + list($width, $height) = getimagesize($imgFile); + + unlink($imgFile); + + return array('height' => $height, 'width' => $width); + } + + function domain_send_keys($domain, $keys) { + $dom = $this->get_domain_object($domain); + + $tmp = libvirt_domain_send_keys($dom, $this->get_hostname(), $keys); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function domain_send_pointer_event($domain, $x, $y, $clicked = 1) { + $dom = $this->get_domain_object($domain); + + $tmp = libvirt_domain_send_pointer_event($dom, $this->get_hostname(), $x, $y, $clicked, true); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function domain_disk_remove($domain, $dev) { + $dom = $this->get_domain_object($domain); + + $tmp = libvirt_domain_disk_remove($dom, $dev); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function supports($name) { + return libvirt_has_feature($name); + } + + function macbyte($val) { + if ($val < 16) + return '0'.dechex($val); + + return dechex($val); + } + + function generate_random_mac_addr($seed=false) { + if (!$seed) + $seed = 1; + + if ($this->get_hypervisor_name() == 'qemu') + $prefix = '52:54:00'; + else + if ($this->get_hypervisor_name() == 'xen') + $prefix = '00:16:3e'; + else + $prefix = $this->macbyte(($seed * rand()) % 256).':'. + $this->macbyte(($seed * rand()) % 256).':'. + $this->macbyte(($seed * rand()) % 256); + + return $prefix.':'. + $this->macbyte(($seed * rand()) % 256).':'. + $this->macbyte(($seed * rand()) % 256).':'. + $this->macbyte(($seed * rand()) % 256); + } + + function domain_nic_add($domain, $mac, $network, $model=false) { + $dom = $this->get_domain_object($domain); + + if ($model == 'default') + $model = false; + + $tmp = libvirt_domain_nic_add($dom, $mac, $network, $model); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function domain_nic_remove($domain, $mac) { + $dom = $this->get_domain_object($domain); + + $tmp = libvirt_domain_nic_remove($dom, $mac); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function get_connection() { + return $this->conn; + } + + function get_hostname() { + return libvirt_connect_get_hostname($this->conn); + } + + function get_domain_object($nameRes) { + if (is_resource($nameRes)) + return $nameRes; + + $dom=libvirt_domain_lookup_by_name($this->conn, $nameRes); + if (!$dom) { + $dom=libvirt_domain_lookup_by_uuid_string($this->conn, $nameRes); + if (!$dom) + return $this->_set_last_error(); + } + + return $dom; + } + + function get_xpath($domain, $xpath, $inactive = false) { + $dom = $this->get_domain_object($domain); + $flags = 0; + if ($inactive) + $flags = VIR_DOMAIN_XML_INACTIVE; + + $tmp = libvirt_domain_xml_xpath($dom, $xpath, $flags); + if (!$tmp) + return $this->_set_last_error(); + + return $tmp; + } + + function get_cdrom_stats($domain, $sort=true) { + $dom = $this->get_domain_object($domain); + + $buses = $this->get_xpath($dom, '//domain/devices/disk[@device="cdrom"]/target/@bus', false); + $disks = $this->get_xpath($dom, '//domain/devices/disk[@device="cdrom"]/target/@dev', false); + + $ret = array(); + for ($i = 0; $i < $disks['num']; $i++) { + $tmp = libvirt_domain_get_block_info($dom, $disks[$i]); + if ($tmp) { + $tmp['bus'] = $buses[$i]; + $ret[] = $tmp; + } + else + $this->_set_last_error(); + } + + if ($sort) { + for ($i = 0; $i < sizeof($ret); $i++) { + for ($ii = 0; $ii < sizeof($ret); $ii++) { + if (strcmp($ret[$i]['device'], $ret[$ii]['device']) < 0) { + $tmp = $ret[$i]; + $ret[$i] = $ret[$ii]; + $ret[$ii] = $tmp; + } } + } + } - function domain_change_numVCpus($domain, $num) { - $dom = $this->get_domain_object($domain); + unset($buses); + unset($disks); - $tmp = libvirt_domain_change_vcpus($dom, $num); - return ($tmp) ? $tmp : $this->_set_last_error(); - } + return $ret; + } - function domain_change_memory_allocation($domain, $memory, $maxmem) { - $dom = $this->get_domain_object($domain); + function get_disk_stats($domain, $sort=true) { + $dom = $this->get_domain_object($domain); - $tmp = libvirt_domain_change_memory($dom, $memory, $maxmem); - return ($tmp) ? $tmp : $this->_set_last_error(); - } + $buses = $this->get_xpath($dom, '//domain/devices/disk[@device="disk"]/target/@bus', false); + $disks = $this->get_xpath($dom, '//domain/devices/disk[@device="disk"]/target/@dev', false); + // create image as: qemu-img create -f qcow2 -o backing_file=RAW_IMG OUT_QCOW_IMG SIZE[K,M,G suffixed] - function domain_change_boot_devices($domain, $first, $second) { - $dom = $this->get_domain_object($domain); + $ret = array(); + for ($i = 0; $i < $disks['num']; $i++) { + $tmp = libvirt_domain_get_block_info($dom, $disks[$i]); + if ($tmp) { + $tmp['bus'] = $buses[$i]; + $ret[] = $tmp; + } + else + $this->_set_last_error(); + } - $tmp = libvirt_domain_change_boot_devices($dom, $first, $second); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function domain_get_screenshot($domain) { - $dom = $this->get_domain_object($domain); - - $tmp = libvirt_domain_get_screenshot($dom, $this->get_hostname() ); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function domain_get_screenshot_thumbnail($domain, $w=120) { - $screen = $this->domain_get_screenshot($domain); - $imgFile = tempnam("/tmp", "libvirt-php-tmp-resize-XXXXXX");; - - if ($screen) { - $fp = fopen($imgFile, "wb"); - fwrite($fp, $screen); - fclose($fp); - } - - if (file_exists($imgFile) && $screen) { - list($width, $height) = getimagesize($imgFile); - $h = ($height / $width) * $w; - } else { - $w = $h = 1; - //$h = $w * (3 / 4.5); - } - - $new = imagecreatetruecolor($w, $h); - if ($screen) { - $img = imagecreatefrompng($imgFile); - imagecopyresampled($new,$img,0,0,0,0, $w,$h,$width,$height); - imagedestroy($img); - } - else { - $c = imagecolorallocate($new, 255, 255, 255); - imagefill($new, 0, 0, $c); - } - - imagepng($new, $imgFile); - imagedestroy($new); - - $fp = fopen($imgFile, "rb"); - $data = fread($fp, filesize($imgFile)); - fclose($fp); - - unlink($imgFile); - return $data; - } - - function domain_get_screen_dimensions($domain) { - $screen = $this->domain_get_screenshot($domain); - $imgFile = tempnam("/tmp", "libvirt-php-tmp-resize-XXXXXX");; - - $width = false; - $height = false; - - if ($screen) { - $fp = fopen($imgFile, "wb"); - fwrite($fp, $screen); - fclose($fp); - } - if (file_exists($imgFile) && $screen) - list($width, $height) = getimagesize($imgFile); - - unlink($imgFile); - - return array('height' => $height, 'width' => $width); - } - - function domain_send_keys($domain, $keys) { - $dom = $this->get_domain_object($domain); - - $tmp = libvirt_domain_send_keys($dom, $this->get_hostname(), $keys); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function domain_send_pointer_event($domain, $x, $y, $clicked = 1) { - $dom = $this->get_domain_object($domain); - - $tmp = libvirt_domain_send_pointer_event($dom, $this->get_hostname(), $x, $y, $clicked, true); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function domain_disk_remove($domain, $dev) { - $dom = $this->get_domain_object($domain); - - $tmp = libvirt_domain_disk_remove($dom, $dev); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function supports($name) { - return libvirt_has_feature($name); - } - - function macbyte($val) { - if ($val < 16) - return '0'.dechex($val); - - return dechex($val); - } - - function generate_random_mac_addr($seed=false) { - if (!$seed) - $seed = 1; - - if ($this->get_hypervisor_name() == 'qemu') - $prefix = '52:54:00'; - else - if ($this->get_hypervisor_name() == 'xen') - $prefix = '00:16:3e'; - else - $prefix = $this->macbyte(($seed * rand()) % 256).':'. - $this->macbyte(($seed * rand()) % 256).':'. - $this->macbyte(($seed * rand()) % 256); - - return $prefix.':'. - $this->macbyte(($seed * rand()) % 256).':'. - $this->macbyte(($seed * rand()) % 256).':'. - $this->macbyte(($seed * rand()) % 256); - } - - function domain_nic_add($domain, $mac, $network, $model=false) { - $dom = $this->get_domain_object($domain); - - if ($model == 'default') - $model = false; - - $tmp = libvirt_domain_nic_add($dom, $mac, $network, $model); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function domain_nic_remove($domain, $mac) { - $dom = $this->get_domain_object($domain); - - $tmp = libvirt_domain_nic_remove($dom, $mac); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function get_connection() { - return $this->conn; - } - - function get_hostname() { - return libvirt_connect_get_hostname($this->conn); - } - - function get_domain_object($nameRes) { - if (is_resource($nameRes)) - return $nameRes; - - $dom=libvirt_domain_lookup_by_name($this->conn, $nameRes); - if (!$dom) { - $dom=libvirt_domain_lookup_by_uuid_string($this->conn, $nameRes); - if (!$dom) - return $this->_set_last_error(); - } - - return $dom; - } - - function get_xpath($domain, $xpath, $inactive = false) { - $dom = $this->get_domain_object($domain); - $flags = 0; - if ($inactive) - $flags = VIR_DOMAIN_XML_INACTIVE; - - $tmp = libvirt_domain_xml_xpath($dom, $xpath, $flags); - if (!$tmp) - return $this->_set_last_error(); - - return $tmp; - } - - function get_cdrom_stats($domain, $sort=true) { - $dom = $this->get_domain_object($domain); - - $buses = $this->get_xpath($dom, '//domain/devices/disk[@device="cdrom"]/target/@bus', false); - $disks = $this->get_xpath($dom, '//domain/devices/disk[@device="cdrom"]/target/@dev', false); - - $ret = array(); - for ($i = 0; $i < $disks['num']; $i++) { - $tmp = libvirt_domain_get_block_info($dom, $disks[$i]); - if ($tmp) { - $tmp['bus'] = $buses[$i]; - $ret[] = $tmp; - } - else - $this->_set_last_error(); - } - - if ($sort) { - for ($i = 0; $i < sizeof($ret); $i++) { - for ($ii = 0; $ii < sizeof($ret); $ii++) { - if (strcmp($ret[$i]['device'], $ret[$ii]['device']) < 0) { - $tmp = $ret[$i]; - $ret[$i] = $ret[$ii]; - $ret[$ii] = $tmp; - } - } - } - } - - unset($buses); - unset($disks); - - return $ret; - } - - function get_disk_stats($domain, $sort=true) { - $dom = $this->get_domain_object($domain); - - $buses = $this->get_xpath($dom, '//domain/devices/disk[@device="disk"]/target/@bus', false); - $disks = $this->get_xpath($dom, '//domain/devices/disk[@device="disk"]/target/@dev', false); - // create image as: qemu-img create -f qcow2 -o backing_file=RAW_IMG OUT_QCOW_IMG SIZE[K,M,G suffixed] - - $ret = array(); - for ($i = 0; $i < $disks['num']; $i++) { - $tmp = libvirt_domain_get_block_info($dom, $disks[$i]); - if ($tmp) { - $tmp['bus'] = $buses[$i]; - $ret[] = $tmp; - } - else - $this->_set_last_error(); - } - - if ($sort) { - for ($i = 0; $i < sizeof($ret); $i++) { - for ($ii = 0; $ii < sizeof($ret); $ii++) { - if (strcmp($ret[$i]['device'], $ret[$ii]['device']) < 0) { - $tmp = $ret[$i]; - $ret[$i] = $ret[$ii]; - $ret[$ii] = $tmp; - } - } - } - } - - unset($buses); - unset($disks); - - return $ret; - } - - function get_nic_info($domain) { - $dom = $this->get_domain_object($domain); - - $macs = $this->get_xpath($dom, '//domain/devices/interface[@type="network"]/mac/@address', false); - if (!$macs) - return $this->_set_last_error(); - - $ret = array(); - for ($i = 0; $i < $macs['num']; $i++) { - $tmp = libvirt_domain_get_network_info($dom, $macs[$i]); - if ($tmp) - $ret[] = $tmp; - else - $this->_set_last_error(); - } - - return $ret; + if ($sort) { + for ($i = 0; $i < sizeof($ret); $i++) { + for ($ii = 0; $ii < sizeof($ret); $ii++) { + if (strcmp($ret[$i]['device'], $ret[$ii]['device']) < 0) { + $tmp = $ret[$i]; + $ret[$i] = $ret[$ii]; + $ret[$ii] = $tmp; + } } + } + } - function get_domain_type($domain) { - $dom = $this->get_domain_object($domain); + unset($buses); + unset($disks); - $tmp = $this->get_xpath($dom, '//domain/@type', false); - if ($tmp['num'] == 0) - return $this->_set_last_error(); + return $ret; + } - $ret = $tmp[0]; - unset($tmp); + function get_nic_info($domain) { + $dom = $this->get_domain_object($domain); - return $ret; + $macs = $this->get_xpath($dom, '//domain/devices/interface[@type="network"]/mac/@address', false); + if (!$macs) + return $this->_set_last_error(); + + $ret = array(); + for ($i = 0; $i < $macs['num']; $i++) { + $tmp = libvirt_domain_get_network_info($dom, $macs[$i]); + if ($tmp) + $ret[] = $tmp; + else + $this->_set_last_error(); + } + + return $ret; + } + + function get_domain_type($domain) { + $dom = $this->get_domain_object($domain); + + $tmp = $this->get_xpath($dom, '//domain/@type', false); + if ($tmp['num'] == 0) + return $this->_set_last_error(); + + $ret = $tmp[0]; + unset($tmp); + + return $ret; + } + + function get_domain_emulator($domain) { + $dom = $this->get_domain_object($domain); + + $tmp = $this->get_xpath($dom, '//domain/devices/emulator', false); + if ($tmp['num'] == 0) + return $this->_set_last_error(); + + $ret = $tmp[0]; + unset($tmp); + + return $ret; + } + + function get_network_cards($domain) { + $dom = $this->get_domain_object($domain); + + $nics = $this->get_xpath($dom, '//domain/devices/interface[@type="network"]', false); + if (!is_array($nics)) + return $this->_set_last_error(); + + return $nics['num']; + } + + function get_disk_capacity($domain, $physical=false, $disk='*', $unit='?') { + $dom = $this->get_domain_object($domain); + $tmp = $this->get_disk_stats($dom); + + $ret = 0; + for ($i = 0; $i < sizeof($tmp); $i++) { + if (($disk == '*') || ($tmp[$i]['device'] == $disk)) + if ($physical) + $ret += $tmp[$i]['physical']; + else + $ret += $tmp[$i]['capacity']; + } + unset($tmp); + + return $this->format_size($ret, 2, $unit); + } + + function get_disk_count($domain) { + $dom = $this->get_domain_object($domain); + $tmp = $this->get_disk_stats($dom); + $ret = sizeof($tmp); + unset($tmp); + + return $ret; + } + + function format_size($value, $decimals, $unit='?') { + /* Autodetect unit that's appropriate */ + if ($unit == '?') { + /* (1 << 40) is not working correctly on i386 systems */ + if ($value > 1099511627776) + $unit = 'T'; + else + if ($value > (1 << 30)) + $unit = 'G'; + else + if ($value > (1 << 20)) + $unit = 'M'; + else + if ($value > (1 << 10)) + $unit = 'K'; + else + $unit = 'B'; + } + + $unit = strtoupper($unit); + + switch ($unit) { + case 'T': return number_format($value / (float)1099511627776, $decimals, '.', ' ').' TB'; + case 'G': return number_format($value / (float)(1 << 30), $decimals, '.', ' ').' GB'; + case 'M': return number_format($value / (float)(1 << 20), $decimals, '.', ' ').' MB'; + case 'K': return number_format($value / (float)(1 << 10), $decimals, '.', ' ').' kB'; + case 'B': return $value.' B'; + } + + return false; + } + + function get_uri() { + $tmp = libvirt_connect_get_uri($this->conn); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function get_domain_count() { + $tmp = libvirt_domain_get_counts($this->conn); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function get_storagepools() { + $tmp = libvirt_list_storagepools($this->conn); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function get_storagepool_res($res) { + if ($res == false) + return false; + if (is_resource($res)) + return $res; + + $tmp = libvirt_storagepool_lookup_by_name($this->conn, $res); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function get_storagepool_info($name) { + if (!($res = $this->get_storagepool_res($name))) + return false; + + $path = libvirt_storagepool_get_xml_desc($res, '/pool/target/path'); + if (!$path) + return $this->_set_last_error(); + $perms = libvirt_storagepool_get_xml_desc($res, '/pool/target/permissions/mode'); + if (!$perms) + return $this->_set_last_error(); + $otmp1 = libvirt_storagepool_get_xml_desc($res, '/pool/target/permissions/owner'); + if (!is_string($otmp1)) + return $this->_set_last_error(); + $otmp2 = libvirt_storagepool_get_xml_desc($res, '/pool/target/permissions/group'); + if (!is_string($otmp2)) + return $this->_set_last_error(); + $tmp = libvirt_storagepool_get_info($res); + $tmp['volume_count'] = sizeof( libvirt_storagepool_list_volumes($res) ); + $tmp['active'] = libvirt_storagepool_is_active($res); + $tmp['path'] = $path; + $tmp['permissions'] = $perms; + $tmp['id_user'] = $otmp1; + $tmp['id_group'] = $otmp2; + + return $tmp; + } + + function storagepool_get_volume_information($pool, $name=false) { + if (!is_resource($pool)) + $pool = $this->get_storagepool_res($pool); + if (!$pool) + return false; + + $out = array(); + $tmp = libvirt_storagepool_list_volumes($pool); + for ($i = 0; $i < sizeof($tmp); $i++) { + if (($tmp[$i] == $name) || ($name == false)) { + $r = libvirt_storagevolume_lookup_by_name($pool, $tmp[$i]); + $out[$tmp[$i]] = libvirt_storagevolume_get_info($r); + $out[$tmp[$i]]['path'] = libvirt_storagevolume_get_path($r); + unset($r); + } + } + + return $out; + } + + function storagevolume_delete($path) { + $vol = libvirt_storagevolume_lookup_by_path($this->conn, $path); + if (!libvirt_storagevolume_delete($vol)) + return $this->_set_last_error(); + + return true; + } + + function translate_volume_type($type) { + if ($type == 1) + return 'Block device'; + + return 'File image'; + } + + function translate_perms($mode) { + $mode = (string)((int)$mode); + + $tmp = '---------'; + + for ($i = 0; $i < 3; $i++) { + $bits = (int)$mode[$i]; + if ($bits & 4) + $tmp[ ($i * 3) ] = 'r'; + if ($bits & 2) + $tmp[ ($i * 3) + 1 ] = 'w'; + if ($bits & 1) + $tmp[ ($i * 3) + 2 ] = 'x'; + } + + + return $tmp; + } + + function parse_size($size) { + $unit = $size[ strlen($size) - 1 ]; + + $size = (int)$size; + switch (strtoupper($unit)) { + case 'T': $size *= 1099511627776; + break; + case 'G': $size *= 1073741824; + break; + case 'M': $size *= 1048576; + break; + case 'K': $size *= 1024; + break; + } + + return $size; + } + + function storagevolume_create($pool, $name, $capacity, $allocation) { + $pool = $this->get_storagepool_res($pool); + + $capacity = $this->parse_size($capacity); + $allocation = $this->parse_size($allocation); + + $xml = "<volume>\n". + " <name>$name</name>\n". + " <capacity>$capacity</capacity>\n". + " <allocation>$allocation</allocation>\n". + "</volume>"; + + $tmp = libvirt_storagevolume_create_xml($pool, $xml); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function get_hypervisor_name() { + $tmp = libvirt_connect_get_information($this->conn); + $hv = $tmp['hypervisor']; + unset($tmp); + + switch (strtoupper($hv)) { + case 'QEMU': $type = 'qemu'; + break; + case 'XEN': $type = 'xen'; + break; + + default: + $type = $hv; + } + + return $type; + } + + function get_connect_information() { + $tmp = libvirt_connect_get_information($this->conn); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function domain_change_xml($domain, $xml) { + $dom = $this->get_domain_object($domain); + + if (!($old_xml = libvirt_domain_get_xml_desc($dom, NULL))) + return $this->_set_last_error(); + if (!libvirt_domain_undefine($dom)) + return $this->_set_last_error(); + if (!libvirt_domain_define_xml($this->conn, $xml)) { + $this->last_error = libvirt_get_last_error(); + libvirt_domain_define_xml($this->conn, $old_xml); + return false; + } + + return true; + } + + function network_change_xml($network, $xml) { + $net = $this->get_network_res($network); + + if (!($old_xml = libvirt_network_get_xml_desc($net, NULL))) { + return $this->_set_last_error(); + } + if (!libvirt_network_undefine($net)) { + return $this->_set_last_error(); + } + if (!libvirt_network_define_xml($this->conn, $xml)) { + $this->last_error = libvirt_get_last_error(); + libvirt_network_define_xml($this->conn, $old_xml); + return false; + } + + return true; + } + + function translate_storagepool_state($state) { + switch ($state) { + case 0: return 'Not running'; + break; + case 1: return 'Building pool'; + break; + case 2: return 'Running'; + break; + case 3: return 'Running degraded'; + break; + case 4: return 'Running but inaccessible'; + break; + } + + return 'Unknown'; + } + + function get_domains() { + $tmp = libvirt_list_domains($this->conn); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function get_domain_by_name($name) { + $tmp = libvirt_domain_lookup_by_name($this->conn, $name); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function get_networks($type = VIR_NETWORKS_ALL) { + $tmp = libvirt_list_networks($this->conn, $type); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function get_nic_models() { + return array('default', 'rtl8139', 'e1000', 'pcnet', 'ne2k_pci', 'virtio'); + } + + 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(); + } + + function get_network_bridge($network) { + $res = $this->get_network_res($network); + if ($res == false) + return false; + + $tmp = libvirt_network_get_bridge($res); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function get_network_active($network) { + $res = $this->get_network_res($network); + if ($res == false) + return false; + + $tmp = libvirt_network_get_active($res); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function set_network_active($network, $active = true) { + $res = $this->get_network_res($network); + if ($res == false) + return false; + + if (!libvirt_network_set_active($res, $active ? 1 : 0)) + return $this->_set_last_error(); + + return true; + } + + function get_network_information($network) { + $res = $this->get_network_res($network); + if ($res == false) + return false; + + $tmp = libvirt_network_get_information($res); + if (!$tmp) + return $this->_set_last_error(); + $tmp['active'] = $this->get_network_active($res); + return $tmp; + } + + function get_network_xml($network) { + $res = $this->get_network_res($network); + if ($res == false) + return false; + + $tmp = libvirt_network_get_xml_desc($res, NULL); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function get_node_devices($dev = false) { + $tmp = ($dev == false) ? libvirt_list_nodedevs($this->conn) : libvirt_list_nodedevs($this->conn, $dev); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function get_node_device_res($res) { + if ($res == false) + return false; + if (is_resource($res)) + return $res; + + $tmp = libvirt_nodedev_get($this->conn, $res); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function get_node_device_caps($dev) { + $dev = $this->get_node_device_res($dev); + + $tmp = libvirt_nodedev_capabilities($dev); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function get_node_device_cap_options() { + $all = $this->get_node_devices(); + + $ret = array(); + for ($i = 0; $i < sizeof($all); $i++) { + $tmp = $this->get_node_device_caps($all[$i]); + + for ($ii = 0; $ii < sizeof($tmp); $ii++) + if (!in_array($tmp[$ii], $ret)) + $ret[] = $tmp[$ii]; + } + + return $ret; + } + + function get_node_device_xml($dev) { + $dev = $this->get_node_device_res($dev); + + $tmp = libvirt_nodedev_get_xml_desc($dev, NULL); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function get_node_device_information($dev) { + $dev = $this->get_node_device_res($dev); + + $tmp = libvirt_nodedev_get_information($dev); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function domain_get_name($res) { + return libvirt_domain_get_name($res); + } + + function domain_get_info($dom) { + if (!$this->allow_cached) + return libvirt_domain_get_info($dom); + + $domname = libvirt_domain_get_name($dom); + if (!array_key_exists($domname, $this->dominfos)) { + $info = libvirt_domain_get_info($dom); + $this->dominfos[$domname] = $info; + } + + return $this->dominfos[$domname]; + } + + function get_last_error() { + return $this->last_error; + } + + function domain_get_xml($domain, $get_inactive = false) { + $dom = $this->get_domain_object($domain); + if (!$dom) + return false; + + $tmp = libvirt_domain_get_xml_desc($dom, $get_inactive ? VIR_DOMAIN_XML_INACTIVE : 0); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function network_get_xml($network) { + $net = $this->get_network_res($network); + if (!$net) + return false; + + $tmp = libvirt_network_get_xml_desc($net, NULL); + return ($tmp) ? $tmp : $this->_set_last_error();; + } + + function domain_get_id($domain, $name = false) { + $dom = $this->get_domain_object($domain); + if ((!$dom) || (!$this->domain_is_running($dom, $name))) + return false; + + $tmp = libvirt_domain_get_id($dom); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function domain_get_interface_stats($nameRes, $iface) { + $dom = $this->get_domain_object($domain); + if (!$dom) + return false; + + $tmp = libvirt_domain_interface_stats($dom, $iface); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function domain_get_memory_stats($domain) { + $dom = $this->get_domain_object($domain); + if (!$dom) + return false; + + $tmp = libvirt_domain_memory_stats($dom); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function domain_start($dom) { + $dom=$this->get_domain_object($dom); + if ($dom) { + $ret = libvirt_domain_create($dom); + $this->last_error = libvirt_get_last_error(); + return $ret; + } + + $ret = libvirt_domain_create_xml($this->conn, $dom); + $this->last_error = libvirt_get_last_error(); + return $ret; + } + + function domain_define($xml) { + $tmp = libvirt_domain_define_xml($this->conn, $xml); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function domain_destroy($domain) { + $dom = $this->get_domain_object($domain); + if (!$dom) + return false; + + $tmp = libvirt_domain_destroy($dom); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function domain_reboot($domain) { + $dom = $this->get_domain_object($domain); + if (!$dom) + return false; + + $tmp = libvirt_domain_reboot($dom); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function domain_suspend($domain) { + $dom = $this->get_domain_object($domain); + if (!$dom) + return false; + + $tmp = libvirt_domain_suspend($dom); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function domain_resume($domain) { + $dom = $this->get_domain_object($domain); + if (!$dom) + return false; + + $tmp = libvirt_domain_resume($dom); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function domain_get_name_by_uuid($uuid) { + $dom = libvirt_domain_lookup_by_uuid_string($this->conn, $uuid); + if (!$dom) + return false; + $tmp = libvirt_domain_get_name($dom); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function domain_shutdown($domain) { + $dom = $this->get_domain_object($domain); + if (!$dom) + return false; + + $tmp = libvirt_domain_shutdown($dom); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function domain_undefine($domain) { + $dom = $this->get_domain_object($domain); + if (!$dom) + return false; + + $tmp = libvirt_domain_undefine($dom); + return ($tmp) ? $tmp : $this->_set_last_error(); + } + + function domain_is_running($domain, $name = false) { + $dom = $this->get_domain_object($domain); + if (!$dom) + return false; + $tmp = $this->domain_get_info($dom); + if (!$tmp) + return $this->_set_last_error(); + $ret = ( ($tmp['state'] == VIR_DOMAIN_RUNNING) || ($tmp['state'] == VIR_DOMAIN_BLOCKED) ); + unset($tmp); + return $ret; + } + + function domain_state_translate($state) { + switch ($state) { + case VIR_DOMAIN_RUNNING: return 'running'; + case VIR_DOMAIN_NOSTATE: return 'nostate'; + case VIR_DOMAIN_BLOCKED: return 'blocked'; + case VIR_DOMAIN_PAUSED: return 'paused'; + case VIR_DOMAIN_SHUTDOWN: return 'shutdown'; + case VIR_DOMAIN_SHUTOFF: return 'shutoff'; + case VIR_DOMAIN_CRASHED: return 'crashed'; + } + + return 'unknown'; + } + + function domain_get_vnc_port($domain) { + $tmp = $this->get_xpath($domain, '//domain/devices/graphics/@port', false); + $var = (int)$tmp[0]; + unset($tmp); + + return $var; + } + + function domain_get_arch($domain) { + $domain = $this->get_domain_object($domain); + + $tmp = $this->get_xpath($domain, '//domain/os/type/@arch', false); + $var = $tmp[0]; + unset($tmp); + + return $var; + } + + function domain_get_description($domain) { + $tmp = $this->get_xpath($domain, '//domain/description', false); + $var = $tmp[0]; + unset($tmp); + + return $var; + } + + function domain_get_clock_offset($domain) { + $tmp = $this->get_xpath($domain, '//domain/clock/@offset', false); + $var = $tmp[0]; + unset($tmp); + + return $var; + } + + function domain_get_feature($domain, $feature) { + $tmp = $this->get_xpath($domain, '//domain/features/'.$feature.'/..', false); + $ret = ($tmp != false); + unset($tmp); + + return $ret; + } + + function domain_get_boot_devices($domain) { + $tmp = $this->get_xpath($domain, '//domain/os/boot/@dev', false); + if (!$tmp) + return false; + + $devs = array(); + for ($i = 0; $i < $tmp['num']; $i++) + $devs[] = $tmp[$i]; + + return $devs; + } + + function _get_single_xpath_result($domain, $xpath) { + $tmp = $this->get_xpath($domain, $xpath, false); + if (!$tmp) + return false; + + if ($tmp['num'] == 0) + return false; + + return $tmp[0]; + } + + function domain_get_multimedia_device($domain, $type, $display=false) { + $domain = $this->get_domain_object($domain); + + if ($type == 'console') { + $type = $this->_get_single_xpath_result($domain, '//domain/devices/console/@type'); + $targetType = $this->_get_single_xpath_result($domain, '//domain/devices/console/target/@type'); + $targetPort = $this->_get_single_xpath_result($domain, '//domain/devices/console/target/@port'); + + if ($display) + return $type.' ('.$targetType.' on port '.$targetPort.')'; + else + return array('type' => $type, 'targetType' => $targetType, 'targetPort' => $targetPort); + } + else + if ($type == 'input') { + $type = $this->_get_single_xpath_result($domain, '//domain/devices/input/@type'); + $bus = $this->_get_single_xpath_result($domain, '//domain/devices/input/@bus'); + + if ($display) + return $type.' on '.$bus; + else + return array('type' => $type, 'bus' => $bus); + } + else + if ($type == 'graphics') { + $type = $this->_get_single_xpath_result($domain, '//domain/devices/graphics/@type'); + $port = $this->_get_single_xpath_result($domain, '//domain/devices/graphics/@port'); + $autoport = $this->_get_single_xpath_result($domain, '//domain/devices/graphics/@autoport'); + + if ($display) + return $type.' on port '.$port.' with'.($autoport ? '' : 'out').' autoport enabled'; + else + return array('type' => $type, 'port' => $port, 'autoport' => $autoport); } - - function get_domain_emulator($domain) { - $dom = $this->get_domain_object($domain); - - $tmp = $this->get_xpath($dom, '//domain/devices/emulator', false); - if ($tmp['num'] == 0) - return $this->_set_last_error(); - - $ret = $tmp[0]; - unset($tmp); - - return $ret; - } - - function get_network_cards($domain) { - $dom = $this->get_domain_object($domain); - - $nics = $this->get_xpath($dom, '//domain/devices/interface[@type="network"]', false); - if (!is_array($nics)) - return $this->_set_last_error(); - - return $nics['num']; - } - - function get_disk_capacity($domain, $physical=false, $disk='*', $unit='?') { - $dom = $this->get_domain_object($domain); - $tmp = $this->get_disk_stats($dom); - - $ret = 0; - for ($i = 0; $i < sizeof($tmp); $i++) { - if (($disk == '*') || ($tmp[$i]['device'] == $disk)) - if ($physical) - $ret += $tmp[$i]['physical']; - else - $ret += $tmp[$i]['capacity']; - } - unset($tmp); - - return $this->format_size($ret, 2, $unit); - } - - function get_disk_count($domain) { - $dom = $this->get_domain_object($domain); - $tmp = $this->get_disk_stats($dom); - $ret = sizeof($tmp); - unset($tmp); - - return $ret; - } - - function format_size($value, $decimals, $unit='?') { - /* Autodetect unit that's appropriate */ - if ($unit == '?') { - /* (1 << 40) is not working correctly on i386 systems */ - if ($value > 1099511627776) - $unit = 'T'; - else - if ($value > (1 << 30)) - $unit = 'G'; - else - if ($value > (1 << 20)) - $unit = 'M'; - else - if ($value > (1 << 10)) - $unit = 'K'; - else - $unit = 'B'; - } - - $unit = strtoupper($unit); - - switch ($unit) { - case 'T': return number_format($value / (float)1099511627776, $decimals, '.', ' ').' TB'; - case 'G': return number_format($value / (float)(1 << 30), $decimals, '.', ' ').' GB'; - case 'M': return number_format($value / (float)(1 << 20), $decimals, '.', ' ').' MB'; - case 'K': return number_format($value / (float)(1 << 10), $decimals, '.', ' ').' kB'; - case 'B': return $value.' B'; - } - - return false; - } - - function get_uri() { - $tmp = libvirt_connect_get_uri($this->conn); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function get_domain_count() { - $tmp = libvirt_domain_get_counts($this->conn); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function get_storagepools() { - $tmp = libvirt_list_storagepools($this->conn); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function get_storagepool_res($res) { - if ($res == false) - return false; - if (is_resource($res)) - return $res; - - $tmp = libvirt_storagepool_lookup_by_name($this->conn, $res); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function get_storagepool_info($name) { - if (!($res = $this->get_storagepool_res($name))) - return false; - - $path = libvirt_storagepool_get_xml_desc($res, '/pool/target/path'); - if (!$path) - return $this->_set_last_error(); - $perms = libvirt_storagepool_get_xml_desc($res, '/pool/target/permissions/mode'); - if (!$perms) - return $this->_set_last_error(); - $otmp1 = libvirt_storagepool_get_xml_desc($res, '/pool/target/permissions/owner'); - if (!is_string($otmp1)) - return $this->_set_last_error(); - $otmp2 = libvirt_storagepool_get_xml_desc($res, '/pool/target/permissions/group'); - if (!is_string($otmp2)) - return $this->_set_last_error(); - $tmp = libvirt_storagepool_get_info($res); - $tmp['volume_count'] = sizeof( libvirt_storagepool_list_volumes($res) ); - $tmp['active'] = libvirt_storagepool_is_active($res); - $tmp['path'] = $path; - $tmp['permissions'] = $perms; - $tmp['id_user'] = $otmp1; - $tmp['id_group'] = $otmp2; - - return $tmp; - } - - function storagepool_get_volume_information($pool, $name=false) { - if (!is_resource($pool)) - $pool = $this->get_storagepool_res($pool); - if (!$pool) - return false; - - $out = array(); - $tmp = libvirt_storagepool_list_volumes($pool); - for ($i = 0; $i < sizeof($tmp); $i++) { - if (($tmp[$i] == $name) || ($name == false)) { - $r = libvirt_storagevolume_lookup_by_name($pool, $tmp[$i]); - $out[$tmp[$i]] = libvirt_storagevolume_get_info($r); - $out[$tmp[$i]]['path'] = libvirt_storagevolume_get_path($r); - unset($r); - } - } - - return $out; - } - - function storagevolume_delete($path) { - $vol = libvirt_storagevolume_lookup_by_path($this->conn, $path); - if (!libvirt_storagevolume_delete($vol)) - return $this->_set_last_error(); - - return true; - } - - function translate_volume_type($type) { - if ($type == 1) - return 'Block device'; - - return 'File image'; - } - - function translate_perms($mode) { - $mode = (string)((int)$mode); - - $tmp = '---------'; - - for ($i = 0; $i < 3; $i++) { - $bits = (int)$mode[$i]; - if ($bits & 4) - $tmp[ ($i * 3) ] = 'r'; - if ($bits & 2) - $tmp[ ($i * 3) + 1 ] = 'w'; - if ($bits & 1) - $tmp[ ($i * 3) + 2 ] = 'x'; - } - - - return $tmp; - } - - function parse_size($size) { - $unit = $size[ strlen($size) - 1 ]; - - $size = (int)$size; - switch (strtoupper($unit)) { - case 'T': $size *= 1099511627776; - break; - case 'G': $size *= 1073741824; - break; - case 'M': $size *= 1048576; - break; - case 'K': $size *= 1024; - break; - } - - return $size; - } - - function storagevolume_create($pool, $name, $capacity, $allocation) { - $pool = $this->get_storagepool_res($pool); - - $capacity = $this->parse_size($capacity); - $allocation = $this->parse_size($allocation); - - $xml = "<volume>\n". - " <name>$name</name>\n". - " <capacity>$capacity</capacity>\n". - " <allocation>$allocation</allocation>\n". - "</volume>"; - - $tmp = libvirt_storagevolume_create_xml($pool, $xml); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function get_hypervisor_name() { - $tmp = libvirt_connect_get_information($this->conn); - $hv = $tmp['hypervisor']; - unset($tmp); - - switch (strtoupper($hv)) { - case 'QEMU': $type = 'qemu'; - break; - case 'XEN': $type = 'xen'; - break; - - default: - $type = $hv; - } - - return $type; - } - - function get_connect_information() { - $tmp = libvirt_connect_get_information($this->conn); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function domain_change_xml($domain, $xml) { - $dom = $this->get_domain_object($domain); - - if (!($old_xml = libvirt_domain_get_xml_desc($dom, NULL))) - return $this->_set_last_error(); - if (!libvirt_domain_undefine($dom)) - return $this->_set_last_error(); - if (!libvirt_domain_define_xml($this->conn, $xml)) { - $this->last_error = libvirt_get_last_error(); - libvirt_domain_define_xml($this->conn, $old_xml); - return false; - } - - return true; - } - - function network_change_xml($network, $xml) { - $net = $this->get_network_res($network); - - if (!($old_xml = libvirt_network_get_xml_desc($net, NULL))) { - return $this->_set_last_error(); - } - if (!libvirt_network_undefine($net)) { - return $this->_set_last_error(); - } - if (!libvirt_network_define_xml($this->conn, $xml)) { - $this->last_error = libvirt_get_last_error(); - libvirt_network_define_xml($this->conn, $old_xml); - return false; - } - - return true; - } - - function translate_storagepool_state($state) { - switch ($state) { - case 0: return 'Not running'; - break; - case 1: return 'Building pool'; - break; - case 2: return 'Running'; - break; - case 3: return 'Running degraded'; - break; - case 4: return 'Running but inaccessible'; - break; - } - - return 'Unknown'; - } - - function get_domains() { - $tmp = libvirt_list_domains($this->conn); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function get_domain_by_name($name) { - $tmp = libvirt_domain_lookup_by_name($this->conn, $name); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function get_networks($type = VIR_NETWORKS_ALL) { - $tmp = libvirt_list_networks($this->conn, $type); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function get_nic_models() { - return array('default', 'rtl8139', 'e1000', 'pcnet', 'ne2k_pci', 'virtio'); - } - - 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(); - } - - function get_network_bridge($network) { - $res = $this->get_network_res($network); - if ($res == false) - return false; - - $tmp = libvirt_network_get_bridge($res); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function get_network_active($network) { - $res = $this->get_network_res($network); - if ($res == false) - return false; - - $tmp = libvirt_network_get_active($res); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function set_network_active($network, $active = true) { - $res = $this->get_network_res($network); - if ($res == false) - return false; - - if (!libvirt_network_set_active($res, $active ? 1 : 0)) - return $this->_set_last_error(); - - return true; - } - - function get_network_information($network) { - $res = $this->get_network_res($network); - if ($res == false) - return false; - - $tmp = libvirt_network_get_information($res); - if (!$tmp) - return $this->_set_last_error(); - $tmp['active'] = $this->get_network_active($res); - return $tmp; - } - - function get_network_xml($network) { - $res = $this->get_network_res($network); - if ($res == false) - return false; - - $tmp = libvirt_network_get_xml_desc($res, NULL); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function get_node_devices($dev = false) { - $tmp = ($dev == false) ? libvirt_list_nodedevs($this->conn) : libvirt_list_nodedevs($this->conn, $dev); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function get_node_device_res($res) { - if ($res == false) - return false; - if (is_resource($res)) - return $res; - - $tmp = libvirt_nodedev_get($this->conn, $res); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function get_node_device_caps($dev) { - $dev = $this->get_node_device_res($dev); - - $tmp = libvirt_nodedev_capabilities($dev); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function get_node_device_cap_options() { - $all = $this->get_node_devices(); - - $ret = array(); - for ($i = 0; $i < sizeof($all); $i++) { - $tmp = $this->get_node_device_caps($all[$i]); - - for ($ii = 0; $ii < sizeof($tmp); $ii++) - if (!in_array($tmp[$ii], $ret)) - $ret[] = $tmp[$ii]; - } - - return $ret; - } - - function get_node_device_xml($dev) { - $dev = $this->get_node_device_res($dev); - - $tmp = libvirt_nodedev_get_xml_desc($dev, NULL); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function get_node_device_information($dev) { - $dev = $this->get_node_device_res($dev); - - $tmp = libvirt_nodedev_get_information($dev); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function domain_get_name($res) { - return libvirt_domain_get_name($res); - } - - function domain_get_info($dom) { - if (!$this->allow_cached) - return libvirt_domain_get_info($dom); - - $domname = libvirt_domain_get_name($dom); - if (!array_key_exists($domname, $this->dominfos)) { - $info = libvirt_domain_get_info($dom); - $this->dominfos[$domname] = $info; - } - - return $this->dominfos[$domname]; - } - - function get_last_error() { - return $this->last_error; - } - - function domain_get_xml($domain, $get_inactive = false) { - $dom = $this->get_domain_object($domain); - if (!$dom) - return false; - - $tmp = libvirt_domain_get_xml_desc($dom, $get_inactive ? VIR_DOMAIN_XML_INACTIVE : 0); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function network_get_xml($network) { - $net = $this->get_network_res($network); - if (!$net) - return false; - - $tmp = libvirt_network_get_xml_desc($net, NULL); - return ($tmp) ? $tmp : $this->_set_last_error();; - } - - function domain_get_id($domain, $name = false) { - $dom = $this->get_domain_object($domain); - if ((!$dom) || (!$this->domain_is_running($dom, $name))) - return false; - - $tmp = libvirt_domain_get_id($dom); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function domain_get_interface_stats($nameRes, $iface) { - $dom = $this->get_domain_object($domain); - if (!$dom) - return false; - - $tmp = libvirt_domain_interface_stats($dom, $iface); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function domain_get_memory_stats($domain) { - $dom = $this->get_domain_object($domain); - if (!$dom) - return false; - - $tmp = libvirt_domain_memory_stats($dom); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function domain_start($dom) { - $dom=$this->get_domain_object($dom); - if ($dom) { - $ret = libvirt_domain_create($dom); - $this->last_error = libvirt_get_last_error(); - return $ret; - } - - $ret = libvirt_domain_create_xml($this->conn, $dom); - $this->last_error = libvirt_get_last_error(); - return $ret; - } - - function domain_define($xml) { - $tmp = libvirt_domain_define_xml($this->conn, $xml); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function domain_destroy($domain) { - $dom = $this->get_domain_object($domain); - if (!$dom) - return false; - - $tmp = libvirt_domain_destroy($dom); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function domain_reboot($domain) { - $dom = $this->get_domain_object($domain); - if (!$dom) - return false; - - $tmp = libvirt_domain_reboot($dom); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function domain_suspend($domain) { - $dom = $this->get_domain_object($domain); - if (!$dom) - return false; - - $tmp = libvirt_domain_suspend($dom); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function domain_resume($domain) { - $dom = $this->get_domain_object($domain); - if (!$dom) - return false; - - $tmp = libvirt_domain_resume($dom); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function domain_get_name_by_uuid($uuid) { - $dom = libvirt_domain_lookup_by_uuid_string($this->conn, $uuid); - if (!$dom) - return false; - $tmp = libvirt_domain_get_name($dom); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function domain_shutdown($domain) { - $dom = $this->get_domain_object($domain); - if (!$dom) - return false; - - $tmp = libvirt_domain_shutdown($dom); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function domain_undefine($domain) { - $dom = $this->get_domain_object($domain); - if (!$dom) - return false; - - $tmp = libvirt_domain_undefine($dom); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - - function domain_is_running($domain, $name = false) { - $dom = $this->get_domain_object($domain); - if (!$dom) - return false; - $tmp = $this->domain_get_info($dom); - if (!$tmp) - return $this->_set_last_error(); - $ret = ( ($tmp['state'] == VIR_DOMAIN_RUNNING) || ($tmp['state'] == VIR_DOMAIN_BLOCKED) ); - unset($tmp); - return $ret; - } - - function domain_state_translate($state) { - switch ($state) { - case VIR_DOMAIN_RUNNING: return 'running'; - case VIR_DOMAIN_NOSTATE: return 'nostate'; - case VIR_DOMAIN_BLOCKED: return 'blocked'; - case VIR_DOMAIN_PAUSED: return 'paused'; - case VIR_DOMAIN_SHUTDOWN: return 'shutdown'; - case VIR_DOMAIN_SHUTOFF: return 'shutoff'; - case VIR_DOMAIN_CRASHED: return 'crashed'; - } - - return 'unknown'; - } - - function domain_get_vnc_port($domain) { - $tmp = $this->get_xpath($domain, '//domain/devices/graphics/@port', false); - $var = (int)$tmp[0]; - unset($tmp); - - return $var; - } - - function domain_get_arch($domain) { - $domain = $this->get_domain_object($domain); - - $tmp = $this->get_xpath($domain, '//domain/os/type/@arch', false); - $var = $tmp[0]; - unset($tmp); - - return $var; - } - - function domain_get_description($domain) { - $tmp = $this->get_xpath($domain, '//domain/description', false); - $var = $tmp[0]; - unset($tmp); - - return $var; - } - - function domain_get_clock_offset($domain) { - $tmp = $this->get_xpath($domain, '//domain/clock/@offset', false); - $var = $tmp[0]; - unset($tmp); - - return $var; - } - - function domain_get_feature($domain, $feature) { - $tmp = $this->get_xpath($domain, '//domain/features/'.$feature.'/..', false); - $ret = ($tmp != false); - unset($tmp); - - return $ret; - } - - function domain_get_boot_devices($domain) { - $tmp = $this->get_xpath($domain, '//domain/os/boot/@dev', false); - if (!$tmp) - return false; - - $devs = array(); - for ($i = 0; $i < $tmp['num']; $i++) - $devs[] = $tmp[$i]; - - return $devs; - } - - function _get_single_xpath_result($domain, $xpath) { - $tmp = $this->get_xpath($domain, $xpath, false); - if (!$tmp) - return false; - - if ($tmp['num'] == 0) - return false; - - return $tmp[0]; - } - - function domain_get_multimedia_device($domain, $type, $display=false) { - $domain = $this->get_domain_object($domain); - - if ($type == 'console') { - $type = $this->_get_single_xpath_result($domain, '//domain/devices/console/@type'); - $targetType = $this->_get_single_xpath_result($domain, '//domain/devices/console/target/@type'); - $targetPort = $this->_get_single_xpath_result($domain, '//domain/devices/console/target/@port'); - - if ($display) - return $type.' ('.$targetType.' on port '.$targetPort.')'; - else - return array('type' => $type, 'targetType' => $targetType, 'targetPort' => $targetPort); - } - else - if ($type == 'input') { - $type = $this->_get_single_xpath_result($domain, '//domain/devices/input/@type'); - $bus = $this->_get_single_xpath_result($domain, '//domain/devices/input/@bus'); - - if ($display) - return $type.' on '.$bus; - else - return array('type' => $type, 'bus' => $bus); - } - else - if ($type == 'graphics') { - $type = $this->_get_single_xpath_result($domain, '//domain/devices/graphics/@type'); - $port = $this->_get_single_xpath_result($domain, '//domain/devices/graphics/@port'); - $autoport = $this->_get_single_xpath_result($domain, '//domain/devices/graphics/@autoport'); - - if ($display) - return $type.' on port '.$port.' with'.($autoport ? '' : 'out').' autoport enabled'; - else - return array('type' => $type, 'port' => $port, 'autoport' => $autoport); - } - else - if ($type == 'video') { - $type = $this->_get_single_xpath_result($domain, '//domain/devices/video/model/@type'); - $vram = $this->_get_single_xpath_result($domain, '//domain/devices/video/model/@vram'); - $heads = $this->_get_single_xpath_result($domain, '//domain/devices/video/model/@heads'); - - if ($display) - return $type.' with '.($vram / 1024).' MB VRAM, '.$heads.' head(s)'; - else - return array('type' => $type, 'vram' => $vram, 'heads' => $heads); - } - else - return false; - } - - function domain_get_host_devices_pci($domain) { - $xpath = '//domain/devices/hostdev[@type="pci"]/source/address/@'; - - $dom = $this->get_xpath($domain, $xpath.'domain', false); - $bus = $this->get_xpath($domain, $xpath.'bus', false); - $slot = $this->get_xpath($domain, $xpath.'slot', false); - $func = $this->get_xpath($domain, $xpath.'function', false); - - $devs = array(); - for ($i = 0; $i < $bus['num']; $i++) { - $d = str_replace('0x', '', $dom[$i]); - $b = str_replace('0x', '', $bus[$i]); - $s = str_replace('0x', '', $slot[$i]); - $f = str_replace('0x', '', $func[$i]); - $devid = 'pci_'.$d.'_'.$b.'_'.$s.'_'.$f; - $tmp2 = $this->get_node_device_information($devid); - $devs[] = array('domain' => $dom[$i], 'bus' => $bus[$i], - 'slot' => $slot[$i], 'func' => $func[$i], - 'vendor' => $tmp2['vendor_name'], - 'vendor_id' => $tmp2['vendor_id'], - 'product' => $tmp2['product_name'], - 'product_id' => $tmp2['product_id']); - } - - return $devs; - } - - function _lookup_device_usb($vendor_id, $product_id) { - $tmp = $this->get_node_devices(false); - for ($i = 0; $i < sizeof($tmp); $i++) { - $tmp2 = $this->get_node_device_information($tmp[$i]); - if (array_key_exists('product_id', $tmp2)) { - if (($tmp2['product_id'] == $product_id) - && ($tmp2['vendor_id'] == $vendor_id)) - return $tmp2; - } - } - - return false; - } - - function domain_get_host_devices_usb($domain) { - $xpath = '//domain/devices/hostdev[@type="usb"]/source/'; - - $vid = $this->get_xpath($domain, $xpath.'vendor/@id', false); - $pid = $this->get_xpath($domain, $xpath.'product/@id', false); - - $devs = array(); - for ($i = 0; $i < $vid['num']; $i++) { - $dev = $this->_lookup_device_usb($vid[$i], $pid[$i]); - $devs[] = array('vendor_id' => $vid[$i], 'product_id' => $pid[$i], - 'product' => $dev['product_name'], - 'vendor' => $dev['vendor_name']); - } - - return $devs; - } - - function domain_get_host_devices($domain) { - $domain = $this->get_domain_object($domain); - - $devs_pci = $this->domain_get_host_devices_pci($domain); - $devs_usb = $this->domain_get_host_devices_usb($domain); - - return array('pci' => $devs_pci, 'usb' => $devs_usb); - } - - function domain_set_feature($domain, $feature, $val) { - $domain = $this->get_domain_object($domain); - - if ($this->domain_get_feature($domain, $feature) == $val) - return true; - - $xml = $this->domain_get_xml($domain, true); - if ($val) { - if (strpos('features', $xml)) - $xml = str_replace('<features>', "<features>\n<$feature/>", $xml); - else - $xml = str_replace('</os>', "</os><features>\n<$feature/></features>", $xml); - } - else - $xml = str_replace("<$feature/>\n", '', $xml); - - return $this->domain_change_xml($domain, $xml); - } - - function domain_set_clock_offset($domain, $offset) { - $domain = $this->get_domain_object($domain); - - if (($old_offset = $this->domain_get_clock_offset($domain)) == $offset) - return true; - - $xml = $this->domain_get_xml($domain, true); - $xml = str_replace("<clock offset='$old_offset'/>", "<clock offset='$offset'/>", $xml); - - return $this->domain_change_xml($domain, $xml); - } - - function domain_set_description($domain, $desc) { - $domain = $this->get_domain_object($domain); - - $description = $this->domain_get_description($domain); - if ($description == $desc) - return true; - - $xml = $this->domain_get_xml($domain, true); - if (!$description) - $xml = str_replace("</uuid>", "</uuid><description>$desc</description>", $xml); - else { - $tmp = explode("\n", $xml); - for ($i = 0; $i < sizeof($tmp); $i++) - if (strpos('.'.$tmp[$i], '<description')) - $tmp[$i] = "<description>$desc</description>"; - - $xml = join("\n", $tmp); - } - - return $this->domain_change_xml($domain, $xml); - } - - function host_get_node_info() { - $tmp = libvirt_node_get_info($this->conn); - return ($tmp) ? $tmp : $this->_set_last_error(); - } - } + else + if ($type == 'video') { + $type = $this->_get_single_xpath_result($domain, '//domain/devices/video/model/@type'); + $vram = $this->_get_single_xpath_result($domain, '//domain/devices/video/model/@vram'); + $heads = $this->_get_single_xpath_result($domain, '//domain/devices/video/model/@heads'); + + if ($display) + return $type.' with '.($vram / 1024).' MB VRAM, '.$heads.' head(s)'; + else + return array('type' => $type, 'vram' => $vram, 'heads' => $heads); + } + else + return false; + } + + function domain_get_host_devices_pci($domain) { + $xpath = '//domain/devices/hostdev[@type="pci"]/source/address/@'; + + $dom = $this->get_xpath($domain, $xpath.'domain', false); + $bus = $this->get_xpath($domain, $xpath.'bus', false); + $slot = $this->get_xpath($domain, $xpath.'slot', false); + $func = $this->get_xpath($domain, $xpath.'function', false); + + $devs = array(); + for ($i = 0; $i < $bus['num']; $i++) { + $d = str_replace('0x', '', $dom[$i]); + $b = str_replace('0x', '', $bus[$i]); + $s = str_replace('0x', '', $slot[$i]); + $f = str_replace('0x', '', $func[$i]); + $devid = 'pci_'.$d.'_'.$b.'_'.$s.'_'.$f; + $tmp2 = $this->get_node_device_information($devid); + $devs[] = array('domain' => $dom[$i], 'bus' => $bus[$i], + 'slot' => $slot[$i], 'func' => $func[$i], + 'vendor' => $tmp2['vendor_name'], + 'vendor_id' => $tmp2['vendor_id'], + 'product' => $tmp2['product_name'], + 'product_id' => $tmp2['product_id']); + } + + return $devs; + } + + function _lookup_device_usb($vendor_id, $product_id) { + $tmp = $this->get_node_devices(false); + for ($i = 0; $i < sizeof($tmp); $i++) { + $tmp2 = $this->get_node_device_information($tmp[$i]); + if (array_key_exists('product_id', $tmp2)) { + if (($tmp2['product_id'] == $product_id) + && ($tmp2['vendor_id'] == $vendor_id)) + return $tmp2; + } + } + + return false; + } + + function domain_get_host_devices_usb($domain) { + $xpath = '//domain/devices/hostdev[@type="usb"]/source/'; + + $vid = $this->get_xpath($domain, $xpath.'vendor/@id', false); + $pid = $this->get_xpath($domain, $xpath.'product/@id', false); + + $devs = array(); + for ($i = 0; $i < $vid['num']; $i++) { + $dev = $this->_lookup_device_usb($vid[$i], $pid[$i]); + $devs[] = array('vendor_id' => $vid[$i], 'product_id' => $pid[$i], + 'product' => $dev['product_name'], + 'vendor' => $dev['vendor_name']); + } + + return $devs; + } + + function domain_get_host_devices($domain) { + $domain = $this->get_domain_object($domain); + + $devs_pci = $this->domain_get_host_devices_pci($domain); + $devs_usb = $this->domain_get_host_devices_usb($domain); + + return array('pci' => $devs_pci, 'usb' => $devs_usb); + } + + function domain_set_feature($domain, $feature, $val) { + $domain = $this->get_domain_object($domain); + + if ($this->domain_get_feature($domain, $feature) == $val) + return true; + + $xml = $this->domain_get_xml($domain, true); + if ($val) { + if (strpos('features', $xml)) + $xml = str_replace('<features>', "<features>\n<$feature/>", $xml); + else + $xml = str_replace('</os>', "</os><features>\n<$feature/></features>", $xml); + } + else + $xml = str_replace("<$feature/>\n", '', $xml); + + return $this->domain_change_xml($domain, $xml); + } + + function domain_set_clock_offset($domain, $offset) { + $domain = $this->get_domain_object($domain); + + if (($old_offset = $this->domain_get_clock_offset($domain)) == $offset) + return true; + + $xml = $this->domain_get_xml($domain, true); + $xml = str_replace("<clock offset='$old_offset'/>", "<clock offset='$offset'/>", $xml); + + return $this->domain_change_xml($domain, $xml); + } + + function domain_set_description($domain, $desc) { + $domain = $this->get_domain_object($domain); + + $description = $this->domain_get_description($domain); + if ($description == $desc) + return true; + + $xml = $this->domain_get_xml($domain, true); + if (!$description) + $xml = str_replace("</uuid>", "</uuid><description>$desc</description>", $xml); + else { + $tmp = explode("\n", $xml); + for ($i = 0; $i < sizeof($tmp); $i++) + if (strpos('.'.$tmp[$i], '<description')) + $tmp[$i] = "<description>$desc</description>"; + + $xml = join("\n", $tmp); + } + + return $this->domain_change_xml($domain, $xml); + } + + function host_get_node_info() { + $tmp = libvirt_node_get_info($this->conn); + return ($tmp) ? $tmp : $this->_set_last_error(); + } +} ?> -- 2.8.4

In the file, there are couple of 'if-else' occurrences that just hurt my eyes. For instance, multiline expression should be wrapped in curly braces, if one side of 'else' has curly braces the other one should have them too, and so on. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- examples/libvirt.php | 106 ++++++++++++++++++++++++--------------------------- 1 file changed, 49 insertions(+), 57 deletions(-) diff --git a/examples/libvirt.php b/examples/libvirt.php index 89aeb9f..9107885 100644 --- a/examples/libvirt.php +++ b/examples/libvirt.php @@ -94,8 +94,7 @@ class Libvirt { $img = imagecreatefrompng($imgFile); imagecopyresampled($new,$img,0,0,0,0, $w,$h,$width,$height); imagedestroy($img); - } - else { + } else { $c = imagecolorallocate($new, 255, 255, 255); imagefill($new, 0, 0, $c); } @@ -167,15 +166,17 @@ class Libvirt { if (!$seed) $seed = 1; - if ($this->get_hypervisor_name() == 'qemu') + if ($this->get_hypervisor_name() == 'qemu') { $prefix = '52:54:00'; - else - if ($this->get_hypervisor_name() == 'xen') + } else { + if ($this->get_hypervisor_name() == 'xen') { $prefix = '00:16:3e'; - else + } else { $prefix = $this->macbyte(($seed * rand()) % 256).':'. - $this->macbyte(($seed * rand()) % 256).':'. - $this->macbyte(($seed * rand()) % 256); + $this->macbyte(($seed * rand()) % 256).':'. + $this->macbyte(($seed * rand()) % 256); + } + } return $prefix.':'. $this->macbyte(($seed * rand()) % 256).':'. @@ -247,9 +248,9 @@ class Libvirt { if ($tmp) { $tmp['bus'] = $buses[$i]; $ret[] = $tmp; - } - else + } else { $this->_set_last_error(); + } } if ($sort) { @@ -283,9 +284,9 @@ class Libvirt { if ($tmp) { $tmp['bus'] = $buses[$i]; $ret[] = $tmp; - } - else + } else { $this->_set_last_error(); + } } if ($sort) { @@ -393,17 +394,14 @@ class Libvirt { /* (1 << 40) is not working correctly on i386 systems */ if ($value > 1099511627776) $unit = 'T'; + else if ($value > (1 << 30)) + $unit = 'G'; + else if ($value > (1 << 20)) + $unit = 'M'; + else if ($value > (1 << 10)) + $unit = 'K'; else - if ($value > (1 << 30)) - $unit = 'G'; - else - if ($value > (1 << 20)) - $unit = 'M'; - else - if ($value > (1 << 10)) - $unit = 'K'; - else - $unit = 'B'; + $unit = 'B'; } $unit = strtoupper($unit); @@ -1012,41 +1010,35 @@ class Libvirt { return $type.' ('.$targetType.' on port '.$targetPort.')'; else return array('type' => $type, 'targetType' => $targetType, 'targetPort' => $targetPort); - } - else - if ($type == 'input') { - $type = $this->_get_single_xpath_result($domain, '//domain/devices/input/@type'); - $bus = $this->_get_single_xpath_result($domain, '//domain/devices/input/@bus'); + } else if ($type == 'input') { + $type = $this->_get_single_xpath_result($domain, '//domain/devices/input/@type'); + $bus = $this->_get_single_xpath_result($domain, '//domain/devices/input/@bus'); - if ($display) - return $type.' on '.$bus; - else - return array('type' => $type, 'bus' => $bus); - } + if ($display) + return $type.' on '.$bus; else - if ($type == 'graphics') { - $type = $this->_get_single_xpath_result($domain, '//domain/devices/graphics/@type'); - $port = $this->_get_single_xpath_result($domain, '//domain/devices/graphics/@port'); - $autoport = $this->_get_single_xpath_result($domain, '//domain/devices/graphics/@autoport'); + return array('type' => $type, 'bus' => $bus); + } else if ($type == 'graphics') { + $type = $this->_get_single_xpath_result($domain, '//domain/devices/graphics/@type'); + $port = $this->_get_single_xpath_result($domain, '//domain/devices/graphics/@port'); + $autoport = $this->_get_single_xpath_result($domain, '//domain/devices/graphics/@autoport'); - if ($display) - return $type.' on port '.$port.' with'.($autoport ? '' : 'out').' autoport enabled'; - else - return array('type' => $type, 'port' => $port, 'autoport' => $autoport); - } - else - if ($type == 'video') { - $type = $this->_get_single_xpath_result($domain, '//domain/devices/video/model/@type'); - $vram = $this->_get_single_xpath_result($domain, '//domain/devices/video/model/@vram'); - $heads = $this->_get_single_xpath_result($domain, '//domain/devices/video/model/@heads'); + if ($display) + return $type.' on port '.$port.' with'.($autoport ? '' : 'out').' autoport enabled'; + else + return array('type' => $type, 'port' => $port, 'autoport' => $autoport); + } else if ($type == 'video') { + $type = $this->_get_single_xpath_result($domain, '//domain/devices/video/model/@type'); + $vram = $this->_get_single_xpath_result($domain, '//domain/devices/video/model/@vram'); + $heads = $this->_get_single_xpath_result($domain, '//domain/devices/video/model/@heads'); - if ($display) - return $type.' with '.($vram / 1024).' MB VRAM, '.$heads.' head(s)'; - else - return array('type' => $type, 'vram' => $vram, 'heads' => $heads); - } - else - return false; + if ($display) + return $type.' with '.($vram / 1024).' MB VRAM, '.$heads.' head(s)'; + else + return array('type' => $type, 'vram' => $vram, 'heads' => $heads); + } else { + return false; + } } function domain_get_host_devices_pci($domain) { @@ -1128,9 +1120,9 @@ class Libvirt { $xml = str_replace('<features>', "<features>\n<$feature/>", $xml); else $xml = str_replace('</os>', "</os><features>\n<$feature/></features>", $xml); - } - else + } else { $xml = str_replace("<$feature/>\n", '', $xml); + } return $this->domain_change_xml($domain, $xml); } @@ -1155,9 +1147,9 @@ class Libvirt { return true; $xml = $this->domain_get_xml($domain, true); - if (!$description) + if (!$description) { $xml = str_replace("</uuid>", "</uuid><description>$desc</description>", $xml); - else { + } else { $tmp = explode("\n", $xml); for ($i = 0; $i < sizeof($tmp); $i++) if (strpos('.'.$tmp[$i], '<description')) -- 2.8.4

The majority of the code has opening curly brace at the very same line as the function declaration. Except for the two: _set_last_error() and set_logfile(). Fix them. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- examples/libvirt.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/examples/libvirt.php b/examples/libvirt.php index 9107885..c60889a 100644 --- a/examples/libvirt.php +++ b/examples/libvirt.php @@ -12,14 +12,12 @@ class Libvirt { $this->connect($uri); } - function _set_last_error() - { + function _set_last_error() { $this->last_error = libvirt_get_last_error(); return false; } - function set_logfile($filename) - { + function set_logfile($filename) { if (!libvirt_logfile_set($filename)) return $this->_set_last_error(); -- 2.8.4

It's <br/> not <br />. Hawk. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- examples/header.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/header.php b/examples/header.php index c944da0..14d45d9 100644 --- a/examples/header.php +++ b/examples/header.php @@ -4,4 +4,4 @@ | <a href="?action=virtual-networks">Virtual networks</a> | <a href="?action=node-devices">Node devices</a> | <a href="?action=storage-pools">Storage pools</a> -<br /><br /> +<br/><br/> -- 2.8.4

Le sigh. There's too much in this patch, I know. But hey, have you seen the code? It was terrible. Now it's at least slightly better readable. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- examples/index.php | 1333 ++++++++++++++++++++++++++-------------------------- 1 file changed, 657 insertions(+), 676 deletions(-) diff --git a/examples/index.php b/examples/index.php index b2f5b69..687f408 100644 --- a/examples/index.php +++ b/examples/index.php @@ -1,28 +1,28 @@ <?php - require('libvirt.php'); - $lv = new Libvirt('qemu:///system'); - if ($lv == false) - die('<html><body>Cannot open connection to hypervisor</body></html>'); - $hn = $lv->get_hostname(); - if ($hn == false) - die('<html><body>Cannot get hostname</body></html>'); + require('libvirt.php'); + $lv = new Libvirt('qemu:///system'); + if ($lv == false) + die('<html><body>Cannot open connection to hypervisor</body></html>'); + $hn = $lv->get_hostname(); + if ($hn == false) + die('<html><body>Cannot get hostname</body></html>'); - $action = array_key_exists('action', $_GET) ? $_GET['action'] : false; - $subaction = array_key_exists('subaction', $_GET) ? $_GET['subaction'] : false; + $action = array_key_exists('action', $_GET) ? $_GET['action'] : false; + $subaction = array_key_exists('subaction', $_GET) ? $_GET['subaction'] : false; - if (($action == 'get-screenshot') && (array_key_exists('uuid', $_GET))) { - if (array_key_exists('width', $_GET) && $_GET['width']) - $tmp = $lv->domain_get_screenshot_thumbnail($_GET['uuid'], $_GET['width']); - else - $tmp = $lv->domain_get_screenshot($_GET['uuid']); + if (($action == 'get-screenshot') && (array_key_exists('uuid', $_GET))) { + if (array_key_exists('width', $_GET) && $_GET['width']) + $tmp = $lv->domain_get_screenshot_thumbnail($_GET['uuid'], $_GET['width']); + else + $tmp = $lv->domain_get_screenshot($_GET['uuid']); - if (!$tmp) - echo $lv->get_last_error().'<br />'; - else { - Header('Content-Type: image/png'); - die($tmp); - } - } + if (!$tmp) { + echo $lv->get_last_error().'<br/>'; + } else { + Header('Content-Type: image/png'); + die($tmp); + } + } ?> <html> <head> @@ -37,490 +37,487 @@ $uri = $lv->get_uri(); $tmp = $lv->get_domain_count(); - if ($action == 'storage-pools') { - $msg = ''; - if ($subaction == 'volume-delete') { - if ((array_key_exists('confirm', $_GET)) && ($_GET['confirm'] == 'yes')) - $msg = $lv->storagevolume_delete( base64_decode($_GET['path']) ) ? 'Volume has been deleted successfully' : 'Cannot delete volume'; - else - $msg = '<table> - <tr> - <td colspan="2"> - <b>Do you really want to delete volume <i>'.base64_decode($_GET['path']).'</i> ?</b><br /> - </td> - </tr> - <tr align="center"> - <td> - <a href="'.$_SERVER['REQUEST_URI'].'&confirm=yes">Yes, delete it</a> - </td> - <td> - <a href="?action='.$action.'">No, go back</a> - </td> - </tr>'; - } - else if ($subaction == 'volume-create') { - if (array_key_exists('sent', $_POST)) { - $msg = $lv->storagevolume_create($_GET['pool'], $_POST['name'], $_POST['capacity'], $_POST['allocation']) ? - 'Volume has been created successfully' : 'Cannot create volume'; - } - else - $msg = '<h3>Create a new volume</h3><form method="POST"> - <table> - <tr> - <td>Volume name: </td> - <td><input type="text" name="name"></td> - </tr> - <tr> - <td>Capacity (e.g. 10M or 1G): </td> - <td><input type="text" name="capacity"></td> - </tr> - <tr> - <td>Allocation (e.g. 10M or 1G): </td> - <td><input type="text" name="allocation"></td> - </tr> - <tr align="center"> - <td colspan="2"><input type="submit" value=" Add storage volume "></td> - </tr> - <input type="hidden" name="sent" value="1" /> - </table> - </form>'; - } + if ($action == 'storage-pools') { + $msg = ''; + if ($subaction == 'volume-delete') { + if ((array_key_exists('confirm', $_GET)) && ($_GET['confirm'] == 'yes')) { + $msg = $lv->storagevolume_delete( base64_decode($_GET['path']) ) ? 'Volume has been deleted successfully' : 'Cannot delete volume'; + } else { + $msg = "<table>" . + "<tr>" . + "<td colspan=\"2\">" . + "<b>Do you really want to delete volume <i>".base64_decode($_GET['path'])."</i> ?</b><br/>" . + "</td>" . + "</tr>" . + "<tr align=\"center\">" . + "<td>" . + "<a href=\"".$_SERVER['REQUEST_URI']."\"&confirm=\"yes\">Yes, delete it</a>" . + "</td>" . + "<td>" . + "<a href=\"?action=".$action."\">No, go back</a>" . + "</td>" . + "</tr>" . + "</table>" ; + } + } else if ($subaction == 'volume-create') { + if (array_key_exists('sent', $_POST)) { + $msg = $lv->storagevolume_create($_GET['pool'], $_POST['name'], $_POST['capacity'], $_POST['allocation']) ? + 'Volume has been created successfully' : 'Cannot create volume'; + } else { + $msg = "<h3>Create a new volume</h3><form method=\"POST\">" . + "<table>" . + "<tr>" . + "<td>Volume name: </td>" . + "<td><input type=\"text\" name=\"name\"></td>" . + "</tr>" . + "<tr>" . + "<td>Capacity (e.g. 10M or 1G): </td>" . + "<td><input type=\"text\" name=\"capacity\"></td>" . + "</tr>" . + "<tr>" . + "<td>Allocation (e.g. 10M or 1G): </td>" . + "<td><input type=\"text\" name=\"allocation\"></td>" . + "</tr>" . + "<tr align=\"center\">" . + "<td colspan=\"2\"><input type=\"submit\" value=\" Add storage volume \"></td>" . + "</tr>" . + "<input type=\"hidden\" name=\"sent\" value=\"1\" />" . + "</table>" . + "</form>"; + } + } - echo "<h2>Storage pools</h2>"; - echo "Here's the list of the storage pools available on the connection.<br /><br />"; + echo "<h2>Storage pools</h2>"; + echo "Here's the list of the storage pools available on the connection.<br/><br/>"; - echo "<table>"; - echo "<tr> - <th>Name<th> - <th>Activity</th> - <th>Volume count</th> - <th>State</th> - <th>Capacity</th> - <th>Allocation</th> - <th>Available</th> - <th>Path</th> - <th>Permissions</th> - <th>Ownership</th> - <th>Actions</th> - </tr>"; + echo "<table>"; + echo "<tr>" . + "<th>Name<th>" . + "<th>Activity</th>" . + "<th>Volume count</th>" . + "<th>State</th>" . + "<th>Capacity</th>" . + "<th>Allocation</th>" . + "<th>Available</th>" . + "<th>Path</th>" . + "<th>Permissions</th>" . + "<th>Ownership</th>" . + "<th>Actions</th>" . + "</tr>"; - $pools = $lv->get_storagepools(); - for ($i = 0; $i < sizeof($pools); $i++) { - $info = $lv->get_storagepool_info($pools[$i]); - $act = $info['active'] ? 'Active' : 'Inactive'; + $pools = $lv->get_storagepools(); + for ($i = 0; $i < sizeof($pools); $i++) { + $info = $lv->get_storagepool_info($pools[$i]); + $act = $info['active'] ? 'Active' : 'Inactive'; - echo "<tr align=\"center\"> - <td>$spaces{$pools[$i]}$spaces<td> - <td>$spaces$act$spaces</td> - <td>$spaces{$info['volume_count']}$spaces</td> - <td>$spaces{$lv->translate_storagepool_state($info['state'])}$spaces</td> - <td>$spaces{$lv->format_size($info['capacity'], 2)}$spaces</td> - <td>$spaces{$lv->format_size($info['allocation'], 2)}$spaces</td> - <td>$spaces{$lv->format_size($info['available'], 2)}$spaces</td> - <td>$spaces{$info['path']}$spaces</td> - <td>$spaces{$lv->translate_perms($info['permissions'])}$spaces</td> - <td>$spaces{$info['id_user']} / {$info['id_group']}$spaces</td> - <td>$spaces<a href=\"?action=storage-pools&pool={$pools[$i]}&subaction=volume-create\">Create volume</a>$spaces</td> - </tr>"; + echo "<tr align=\"center\">" . + "<td>$spaces{$pools[$i]}$spaces<td>" . + "<td>$spaces$act$spaces</td>" . + "<td>$spaces{$info['volume_count']}$spaces</td>" . + "<td>$spaces{$lv->translate_storagepool_state($info['state'])}$spaces</td>" . + "<td>$spaces{$lv->format_size($info['capacity'], 2)}$spaces</td>" . + "<td>$spaces{$lv->format_size($info['allocation'], 2)}$spaces</td>" . + "<td>$spaces{$lv->format_size($info['available'], 2)}$spaces</td>" . + "<td>$spaces{$info['path']}$spaces</td>" . + "<td>$spaces{$lv->translate_perms($info['permissions'])}$spaces</td>" . + "<td>$spaces{$info['id_user']} / {$info['id_group']}$spaces</td>" . + "<td>$spaces<a href=\"?action=storage-pools&pool={$pools[$i]}&subaction=volume-create\">Create volume</a>$spaces</td>" . + "</tr>"; - if ($info['volume_count'] > 0) { - echo "<tr> - <td colspan=\"10\" style='padding-left: 40px'><table> - <tr> - <th>Name</th> - <th>Type</th> - <th>Capacity</th> - <th>Allocation</th> - <th>Path</th> - <th>Actions</th> - </tr>"; - $tmp = $lv->storagepool_get_volume_information($pools[$i]); - $tmp_keys = array_keys($tmp); - for ($ii = 0; $ii < sizeof($tmp); $ii++) { - $path = base64_encode($tmp[$tmp_keys[$ii]]['path']); - echo "<tr> - <td>$spaces{$tmp_keys[$ii]}$spaces</td> - <td>$spaces{$lv->translate_volume_type($tmp[$tmp_keys[$ii]]['type'])}$spaces</td> - <td>$spaces{$lv->format_size($tmp[$tmp_keys[$ii]]['capacity'], 2)}$spaces</td> - <td>$spaces{$lv->format_size($tmp[$tmp_keys[$ii]]['allocation'], 2)}$spaces</td> - <td>$spaces{$tmp[$tmp_keys[$ii]]['path']}$spaces</td> - <td>$spaces<a href=\"?action=storage-pools&path=$path&subaction=volume-delete\">Delete volume</a>$spaces</td> - </tr>"; - } + if ($info['volume_count'] > 0) { + echo "<tr>" . + "<td colspan=\"10\" style='padding-left: 40px'><table>" . + "<tr>" . + "<th>Name</th>" . + "<th>Type</th>" . + "<th>Capacity</th>" . + "<th>Allocation</th>" . + "<th>Path</th>" . + "<th>Actions</th>" . + "</tr>"; + $tmp = $lv->storagepool_get_volume_information($pools[$i]); + $tmp_keys = array_keys($tmp); + for ($ii = 0; $ii < sizeof($tmp); $ii++) { + $path = base64_encode($tmp[$tmp_keys[$ii]]['path']); + echo "<tr>" . + "<td>$spaces{$tmp_keys[$ii]}$spaces</td>" . + "<td>$spaces{$lv->translate_volume_type($tmp[$tmp_keys[$ii]]['type'])}$spaces</td>" . + "<td>$spaces{$lv->format_size($tmp[$tmp_keys[$ii]]['capacity'], 2)}$spaces</td>" . + "<td>$spaces{$lv->format_size($tmp[$tmp_keys[$ii]]['allocation'], 2)}$spaces</td>" . + "<td>$spaces{$tmp[$tmp_keys[$ii]]['path']}$spaces</td>" . + "<td>$spaces<a href=\"?action=storage-pools&path=$path&subaction=volume-delete\">Delete volume</a>$spaces</td>" . + "</tr>"; + } - echo "</table></td> - </tr>"; - } - } - echo "</table>"; + echo "</table></td>" . + "</tr>"; + } + } + echo "</table>"; - echo (strpos($msg, '<form')) ? $msg : '<pre>'.$msg.'</pre>'; - } - else - if ($action == 'node-devices') { - echo "<h2>Node devices</h2>"; - echo "Here's the list of node devices available on the host machine. You can dump the information about the node devices there so you have simple way ". - "to check presence of the devices using this page.<br /><br />"; + echo (strpos($msg, '<form')) ? $msg : '<pre>'.$msg.'</pre>'; + } else if ($action == 'node-devices') { + echo "<h2>Node devices</h2>"; + echo "Here's the list of node devices available on the host machine." . + "You can dump the information about the node devices there so you" . + "have simple way to check presence of the devices using this" . + "page.<br/><br/>"; - $ret = false; - if (array_key_exists('subaction', $_GET)) { - $name = $_GET['name']; - if ($_GET['subaction'] == 'dumpxml') - $ret = 'XML dump of node device <i>'.$name.'</i>:<br /><br />'.htmlentities($lv->get_node_device_xml($name, false)); - } + $ret = false; + if (array_key_exists('subaction', $_GET)) { + $name = $_GET['name']; + if ($_GET['subaction'] == 'dumpxml') + $ret = 'XML dump of node device <i>'.$name.'</i>:<br/><br/>'.htmlentities($lv->get_node_device_xml($name, false)); + } - echo "Host node devices capability list: "; - $tmp = $lv->get_node_device_cap_options(); - for ($i = 0; $i < sizeof($tmp); $i++) { - echo "<a href=\"?action={$_GET['action']}&cap={$tmp[$i]}\">{$tmp[$i]}</a>"; - if ($i < (sizeof($tmp) - 1)) - echo ', '; - } - echo "<br /><br />"; + echo "Host node devices capability list: "; + $tmp = $lv->get_node_device_cap_options(); + for ($i = 0; $i < sizeof($tmp); $i++) { + echo "<a href=\"?action={$_GET['action']}&cap={$tmp[$i]}\">{$tmp[$i]}</a>"; + if ($i < (sizeof($tmp) - 1)) + echo ', '; + } + echo "<br/><br/>"; - $tmp = $lv->get_node_devices( array_key_exists('cap', $_GET) ? $_GET['cap'] : false ); - echo "<table> - <tr> - <th>Device name $spaces</th> - <th>$spaces Identification $spaces</th> - <th>$spaces Driver name $spaces</th> - <th>$spaces Vendor $spaces</th> - <th>$spaces Product $spaces</th> - <th>$spaces Action $spaces</th> - </tr>"; + $tmp = $lv->get_node_devices( array_key_exists('cap', $_GET) ? $_GET['cap'] : false ); + echo "<table>" . + "<tr>" . + "<th>Device name $spaces</th>" . + "<th>$spaces Identification $spaces</th>" . + "<th>$spaces Driver name $spaces</th>" . + "<th>$spaces Vendor $spaces</th>" . + "<th>$spaces Product $spaces</th>" . + "<th>$spaces Action $spaces</th>" . + "</tr>"; - for ($i = 0; $i < sizeof($tmp); $i++) { - $tmp2 = $lv->get_node_device_information($tmp[$i]); + for ($i = 0; $i < sizeof($tmp); $i++) { + $tmp2 = $lv->get_node_device_information($tmp[$i]); - $act = !array_key_exists('cap', $_GET) ? "<a href=\"?action={$_GET['action']}&subaction=dumpxml&name={$tmp2['name']}\">Dump configuration</a>" : - "<a href=\"?action={$_GET['action']}&subaction=dumpxml&cap={$_GET['cap']}&name={$tmp2['name']}\">Dump configuration</a>"; + $act = !array_key_exists('cap', $_GET) ? "<a href=\"?action={$_GET['action']}&subaction=dumpxml&name={$tmp2['name']}\">Dump configuration</a>" : + "<a href=\"?action={$_GET['action']}&subaction=dumpxml&cap={$_GET['cap']}&name={$tmp2['name']}\">Dump configuration</a>"; - if ($tmp2['capability'] == 'system') { - $driver = '-'; - $vendor = array_key_exists('hardware_vendor', $tmp2) ? $tmp2['hardware_vendor'] : ''; - $serial = array_key_exists('hardware_version', $tmp2) ? $tmp2['hardware_version'] : ''; - $ident = $vendor.' '.$serial; - $product = array_key_exists('hardware_serial', $tmp2) ? $tmp2['hardware_serial'] : 'Unknown'; - } - else - if ($tmp2['capability'] == 'net') { - $ident = array_key_exists('interface_name', $tmp2) ? $tmp2['interface_name'] : '-'; - $driver = array_key_exists('capabilities', $tmp2) ? $tmp2['capabilities'] : '-'; - $vendor = 'Unknown'; - $product = 'Unknown'; - } - else { - $driver = array_key_exists('driver_name', $tmp2) ? $tmp2['driver_name'] : 'None'; - $vendor = array_key_exists('vendor_name', $tmp2) ? $tmp2['vendor_name'] : 'Unknown'; - $product = array_key_exists('product_name', $tmp2) ? $tmp2['product_name'] : 'Unknown'; - if (array_key_exists('vendor_id', $tmp2) && array_key_exists('product_id', $tmp2)) - $ident = $tmp2['vendor_id'].':'.$tmp2['product_id']; - else - $ident = '-'; - } + if ($tmp2['capability'] == 'system') { + $driver = '-'; + $vendor = array_key_exists('hardware_vendor', $tmp2) ? $tmp2['hardware_vendor'] : ''; + $serial = array_key_exists('hardware_version', $tmp2) ? $tmp2['hardware_version'] : ''; + $ident = $vendor.' '.$serial; + $product = array_key_exists('hardware_serial', $tmp2) ? $tmp2['hardware_serial'] : 'Unknown'; + } else if ($tmp2['capability'] == 'net') { + $ident = array_key_exists('interface_name', $tmp2) ? $tmp2['interface_name'] : '-'; + $driver = array_key_exists('capabilities', $tmp2) ? $tmp2['capabilities'] : '-'; + $vendor = 'Unknown'; + $product = 'Unknown'; + } else { + $driver = array_key_exists('driver_name', $tmp2) ? $tmp2['driver_name'] : 'None'; + $vendor = array_key_exists('vendor_name', $tmp2) ? $tmp2['vendor_name'] : 'Unknown'; + $product = array_key_exists('product_name', $tmp2) ? $tmp2['product_name'] : 'Unknown'; + if (array_key_exists('vendor_id', $tmp2) && array_key_exists('product_id', $tmp2)) + $ident = $tmp2['vendor_id'].':'.$tmp2['product_id']; + else + $ident = '-'; + } - echo "<tr> - <td>$spaces{$tmp2['name']}$spaces</td> - <td align=\"center\">$spaces$ident$spaces</td> - <td align=\"center\">$spaces$driver$spaces</td> - <td align=\"center\">$spaces$vendor$spaces</td> - <td align=\"center\">$spaces$product$spaces</td> - <td align=\"center\">$act</td> - </tr>"; - } - echo "</table>"; + echo "<tr>" . + "<td>$spaces{$tmp2['name']}$spaces</td>" . + "<td align=\"center\">$spaces$ident$spaces</td>" . + "<td align=\"center\">$spaces$driver$spaces</td>" . + "<td align=\"center\">$spaces$vendor$spaces</td>" . + "<td align=\"center\">$spaces$product$spaces</td>" . + "<td align=\"center\">$act</td>" . + "</tr>"; + } + echo "</table>"; - if ($ret) - echo "<pre>$ret</pre>"; - } - else - if ($action == 'virtual-networks') { - echo "<h2>Networks</h2>"; - echo "This is the administration of virtual networks. You can see all the virtual network being available with their settings. Please make sure you're using the right network for the purpose you need to since using the isolated network between two or multiple guests is providing the sharing option but internet connectivity will be disabled. Please enable internet services only on the guests that are really requiring internet access for operation like e.g. HTTP server or FTP server but you don't need to put the internet access to the guest with e.g. MySQL instance or anything that might be managed from the web-site. For the scenario described you could setup 2 network, internet and isolated, where isolated network should be setup on both machine with Apache and MySQL but internet access should be set up just on the machine with Apache webserver with scripts to remotely connect to MySQL instance and manage it (using e.g. phpMyAdmin). Isolated network is the one that's havin! g forwarding column set to None."; + if ($ret) + echo "<pre>$ret</pre>"; + } else if ($action == 'virtual-networks') { + echo "<h2>Networks</h2>"; + echo "This is the administration of virtual networks. You can see all" . + "the virtual network being available with their settings. Please" . + "make sure you're using the right network for the purpose you need" . + "to since using the isolated network between two or multiple guests" . + "is providing the sharing option but internet connectivity will be" . + "disabled. Please enable internet services only on the guests that" . + "are really requiring internet access for operation like e.g. HTTP" . + "server or FTP server but you don't need to put the internet access" . + "to the guest with e.g. MySQL instance or anything that might be" . + "managed from the web-site. For the scenario described you could" . + "setup 2 network, internet and isolated, where isolated network" . + "should be setup on both machine with Apache and MySQL but internet" . + "access should be set up just on the machine with Apache webserver" . + "with scripts to remotely connect to MySQL instance and manage it" . + "(using e.g. phpMyAdmin). Isolated network is the one that's having" . + "forwarding column set to None."; - $ret = false; - if ($subaction) { - $name = $_GET['name']; - if ($subaction == 'start') - $ret = $lv->set_network_active($name, true) ? "Network has been started successfully" : 'Error while starting network: '.$lv->get_last_error(); - else - if ($subaction == 'stop') - $ret = $lv->set_network_active($name, false) ? "Network has been stopped successfully" : 'Error while stopping network: '.$lv->get_last_error(); - else - if (($subaction == 'dumpxml') || ($subaction == 'edit')) { - $xml = $lv->network_get_xml($name, false); + $ret = false; + if ($subaction) { + $name = $_GET['name']; + if ($subaction == 'start') { + $ret = $lv->set_network_active($name, true) ? "Network has been started successfully" : 'Error while starting network: '.$lv->get_last_error(); + } else if ($subaction == 'stop') { + $ret = $lv->set_network_active($name, false) ? "Network has been stopped successfully" : 'Error while stopping network: '.$lv->get_last_error(); + } else if (($subaction == 'dumpxml') || ($subaction == 'edit')) { + $xml = $lv->network_get_xml($name, false); - if ($subaction == 'edit') { - if (@$_POST['xmldesc']) { - $ret = $lv->network_change_xml($name, $_POST['xmldesc']) ? "Network definition has been changed" : - 'Error changing network definition: '.$lv->get_last_error(); - } - else - $ret = 'Editing network XML description: <br /><br /><form method="POST"><table><tr><td>Network XML description: </td>'. - '<td><textarea name="xmldesc" rows="25" cols="90%">'.$xml.'</textarea></td></tr><tr align="center"><td colspan="2">'. - '<input type="submit" value=" Edit domain XML description "></tr></form>'; - } - else - $ret = 'XML dump of network <i>'.$name.'</i>:<br /><br />'.htmlentities($lv->get_network_xml($name, false)); - } - } + if ($subaction == 'edit') { + if (@$_POST['xmldesc']) { + $ret = $lv->network_change_xml($name, $_POST['xmldesc']) ? "Network definition has been changed" : + 'Error changing network definition: '.$lv->get_last_error(); + } else { + $ret = 'Editing network XML description: <br/><br/><form method="POST"><table><tr><td>Network XML description: </td>'. + '<td><textarea name="xmldesc" rows="25" cols="90%">'.$xml.'</textarea></td></tr><tr align="center"><td colspan="2">'. + '<input type="submit" value=" Edit domain XML description "></tr></form>'; + } + } else { + $ret = 'XML dump of network <i>'.$name.'</i>:<br/><br/>'.htmlentities($lv->get_network_xml($name, false)); + } + } + } - echo "<h3>List of networks</h3>"; - $tmp = $lv->get_networks(VIR_NETWORKS_ALL); + echo "<h3>List of networks</h3>"; + $tmp = $lv->get_networks(VIR_NETWORKS_ALL); - echo "<table> - <tr> - <th>Network name $spaces</th> - <th>$spaces Network state $spaces</th> - <th>$spaces Gateway IP Address $spaces</th> - <th>$spaces IP Address Range $spaces</th> - <th>$spaces Forwarding $spaces</th> - <th>$spaces DHCP Range $spaces</th> - <th>$spaces Actions $spaces</th> - </tr>"; + echo "<table>" . + "<tr>" . + "<th>Network name $spaces</th>" . + "<th>$spaces Network state $spaces</th>" . + "<th>$spaces Gateway IP Address $spaces</th>" . + "<th>$spaces IP Address Range $spaces</th>" . + "<th>$spaces Forwarding $spaces</th>" . + "<th>$spaces DHCP Range $spaces</th>" . + "<th>$spaces Actions $spaces</th>" . + "</tr>"; - for ($i = 0; $i < sizeof($tmp); $i++) { - $tmp2 = $lv->get_network_information($tmp[$i]); - $ip = ''; - $ip_range = ''; - $activity = $tmp2['active'] ? 'Active' : 'Inactive'; - $dhcp = 'Disabled'; - $forward = 'None'; - if (array_key_exists('forwarding', $tmp2) && $tmp2['forwarding'] != 'None') { - if (array_key_exists('forward_dev', $tmp2)) - $forward = $tmp2['forwarding'].' to '.$tmp2['forward_dev']; - else - $forward = $tmp2['forwarding']; - } + for ($i = 0; $i < sizeof($tmp); $i++) { + $tmp2 = $lv->get_network_information($tmp[$i]); + $ip = ''; + $ip_range = ''; + $activity = $tmp2['active'] ? 'Active' : 'Inactive'; + $dhcp = 'Disabled'; + $forward = 'None'; + if (array_key_exists('forwarding', $tmp2) && $tmp2['forwarding'] != 'None') { + if (array_key_exists('forward_dev', $tmp2)) + $forward = $tmp2['forwarding'].' to '.$tmp2['forward_dev']; + else + $forward = $tmp2['forwarding']; + } - if (array_key_exists('dhcp_start', $tmp2) && array_key_exists('dhcp_end', $tmp2)) - $dhcp = $tmp2['dhcp_start'].' - '.$tmp2['dhcp_end']; + if (array_key_exists('dhcp_start', $tmp2) && array_key_exists('dhcp_end', $tmp2)) + $dhcp = $tmp2['dhcp_start'].' - '.$tmp2['dhcp_end']; - if (array_key_exists('ip', $tmp2)) - $ip = $tmp2['ip']; + if (array_key_exists('ip', $tmp2)) + $ip = $tmp2['ip']; - if (array_key_exists('ip_range', $tmp2)) - $ip_range = $tmp2['ip_range']; + if (array_key_exists('ip_range', $tmp2)) + $ip_range = $tmp2['ip_range']; - $act = "<a href=\"?action={$_GET['action']}&subaction=" . ($tmp2['active'] ? "stop" : "start"); - $act .= "&name=" . urlencode($tmp2['name']) . "\">"; - $act .= ($tmp2['active'] ? "Stop" : "Start") . " network</a>"; - $act .= " | <a href=\"?action={$_GET['action']}&subaction=dumpxml&name=" . urlencode($tmp2['name']) . "\">Dump network XML</a>"; - if (!$tmp2['active']) - $act .= ' | <a href="?action='.$_GET['action'].'&subaction=edit&name='. urlencode($tmp2['name']) . '">Edit network</a>'; + $act = "<a href=\"?action={$_GET['action']}&subaction=" . ($tmp2['active'] ? "stop" : "start"); + $act .= "&name=" . urlencode($tmp2['name']) . "\">"; + $act .= ($tmp2['active'] ? "Stop" : "Start") . " network</a>"; + $act .= " | <a href=\"?action={$_GET['action']}&subaction=dumpxml&name=" . urlencode($tmp2['name']) . "\">Dump network XML</a>"; + if (!$tmp2['active']) + $act .= ' | <a href="?action='.$_GET['action'].'&subaction=edit&name='. urlencode($tmp2['name']) . '">Edit network</a>'; - echo "<tr> - <td>$spaces{$tmp2['name']}$spaces</td> - <td align=\"center\">$spaces$activity$spaces</td> - <td align=\"center\">$spaces$ip$spaces</td> - <td align=\"center\">$spaces$ip_range$spaces</td> - <td align=\"center\">$spaces$forward$spaces</td> - <td align=\"center\">$spaces$dhcp$spaces</td> - <td align=\"center\">$spaces$act$spaces</td> - </tr>"; - } - echo "</table>"; + echo "<tr>" . + "<td>$spaces{$tmp2['name']}$spaces</td>" . + "<td align=\"center\">$spaces$activity$spaces</td>" . + "<td align=\"center\">$spaces$ip$spaces</td>" . + "<td align=\"center\">$spaces$ip_range$spaces</td>" . + "<td align=\"center\">$spaces$forward$spaces</td>" . + "<td align=\"center\">$spaces$dhcp$spaces</td>" . + "<td align=\"center\">$spaces$act$spaces</td>" . + "</tr>"; + } + echo "</table>"; - if ($ret) - echo "<pre>$ret</pre>"; - } - else - if ($action == 'host-information') { - $tmp = $lv->host_get_node_info(); - $ci = $lv->get_connect_information(); + if ($ret) + echo "<pre>$ret</pre>"; + } else if ($action == 'host-information') { + $tmp = $lv->host_get_node_info(); + $ci = $lv->get_connect_information(); - $info = ''; - if ($ci['uri']) - $info .= 'connected to <i>'.$ci['uri'].'</i> on <i>'.$ci['hostname'].'</i>, '; - if ($ci['encrypted'] == 'Yes') - $info .= 'encrypted, '; - if ($ci['secure'] == 'Yes') - $info .= 'secure, '; - if ($ci['hypervisor_maxvcpus']) - $info .= 'maximum '.$ci['hypervisor_maxvcpus'].' vcpus per guest, '; + $info = ''; + if ($ci['uri']) + $info .= 'connected to <i>'.$ci['uri'].'</i> on <i>'.$ci['hostname'].'</i>, '; + if ($ci['encrypted'] == 'Yes') + $info .= 'encrypted, '; + if ($ci['secure'] == 'Yes') + $info .= 'secure, '; + if ($ci['hypervisor_maxvcpus']) + $info .= 'maximum '.$ci['hypervisor_maxvcpus'].' vcpus per guest, '; - if (strlen($info) > 2) - $info[ strlen($info) - 2 ] = ' '; + if (strlen($info) > 2) + $info[ strlen($info) - 2 ] = ' '; - echo "<h2>Host information</h2> - <table> - <tr> - <td>Hypervisor: </td> - <td>{$ci['hypervisor_string']}</td> - </tr> - <tr> - <td>Connection information: </td> - <td>$info</td> - </tr> - <tr> - <td>Architecture: </td> - <td>{$tmp['model']}</td> - </tr> - <tr> - <td>Total memory installed: </td> - <td>".number_format(($tmp['memory'] / 1048576), 2, '.', ' ')."GB </td> - </tr> - <tr> - <td>Total processor count: </td> - <td>{$tmp['cpus']}</td> - </tr> - <tr> - <td>Processor speed: </td> - <td>{$tmp['mhz']} MHz</td> - </tr> - <tr> - <td>Processor nodes: </td> - <td>{$tmp['nodes']}</td> - </tr> - <tr> - <td>Processor sockets: </td> - <td>{$tmp['sockets']}</td> - </tr> - <tr> - <td>Processor cores: </td> - <td>{$tmp['cores']}</td> - </tr> - <tr> - <td>Processor threads: </td> - <td>{$tmp['threads']}</td> - </tr> - </table> - "; + echo "<h2>Host information</h2>" . + "<table>" . + "<tr>" . + "<td>Hypervisor: </td>" . + "<td>{$ci['hypervisor_string']}</td>" . + "</tr>" . + "<tr>" . + "<td>Connection information: </td>" . + "<td>$info</td>" . + "</tr>" . + "<tr>" . + "<td>Architecture: </td>" . + "<td>{$tmp['model']}</td>" . + "</tr>" . + "<tr>" . + "<td>Total memory installed: </td>" . + "<td>".number_format(($tmp['memory'] / 1048576), 2, '.', ' ')."GB </td>" . + "</tr>" . + "<tr>" . + "<td>Total processor count: </td>" . + "<td>{$tmp['cpus']}</td>" . + "</tr>" . + "<tr>" . + "<td>Processor speed: </td>" . + "<td>{$tmp['mhz']} MHz</td>" . + "</tr>" . + "<tr>" . + "<td>Processor nodes: </td>" . + "<td>{$tmp['nodes']}</td>" . + "</tr>" . + "<tr>" . + "<td>Processor sockets: </td>" . + "<td>{$tmp['sockets']}</td>" . + "</tr>" . + "<tr>" . + "<td>Processor cores: </td>" . + "<td>{$tmp['cores']}</td>" . + "</tr>" . + "<tr>" . + "<td>Processor threads: </td>" . + "<td>{$tmp['threads']}</td>" . + "</tr>" . + "</table>"; - unset($tmp); - } - else - if ($action == 'domain-information') { - $subaction = array_key_exists('subaction', $_GET) ? $_GET['subaction'] : false; - $ret = false; - $die = false; - $domName = $lv->domain_get_name_by_uuid($_GET['uuid']); + unset($tmp); + } else if ($action == 'domain-information') { + $subaction = array_key_exists('subaction', $_GET) ? $_GET['subaction'] : false; + $ret = false; + $die = false; + $domName = $lv->domain_get_name_by_uuid($_GET['uuid']); - if ($subaction == 'disk-remove') { - if ((array_key_exists('confirm', $_GET)) && ($_GET['confirm'] == 'yes')) - $ret = $lv->domain_disk_remove($domName, $_GET['dev']) ? 'Disk has been removed successfully' : 'Cannot remove disk: '.$lv->get_last_error(); - else { - $ret = '<table> - <tr> - <td colspan="2"> - <b>Do you really want to delete disk <i>'.$_GET['dev'].' from the guest</i> ?</b><br /> - </td> - </tr> - <tr align="center"> - <td> - <a href="'.$_SERVER['REQUEST_URI'].'&confirm=yes">Yes, delete it</a> - </td> - <td> - <a href="?action='.$action.'&uuid='.$_GET['uuid'].'">No, go back</a> - </td> - </tr>'; - $die = true; - } - } - if ($subaction == 'disk-add') { - $img = array_key_exists('img', $_POST) ? $_POST['img'] : false; + if ($subaction == 'disk-remove') { + if ((array_key_exists('confirm', $_GET)) && ($_GET['confirm'] == 'yes')) { + $ret = $lv->domain_disk_remove($domName, $_GET['dev']) ? 'Disk has been removed successfully' : 'Cannot remove disk: '.$lv->get_last_error(); + } else { + $ret = "<table>" . + "<tr>" . + "<td colspan=\"2\">" . + "<b>Do you really want to delete disk <i>".$_GET['dev']." from the guest</i> ?</b><br/>" . + "</td>" . + "</tr>" . + "<tr align=\"center\">" . + "<td>" . + "<a href=\"".$_SERVER['REQUEST_URI']."&confirm=yes\">Yes, delete it</a>" . + "</td>" . + "<td>" . + "<a href=\"?action='.$action.'&uuid=".$_GET['uuid']."\">No, go back</a>" . + "</td>" . + "</tr>"; + $die = true; + } + } else if ($subaction == 'disk-add') { + $img = array_key_exists('img', $_POST) ? $_POST['img'] : false; - if ($img) - $ret = $lv->domain_disk_add($domName, $_POST['img'], $_POST['dev']) ? 'Disk has been successfully added to the guest' : - 'Cannot add disk to the guest: '.$lv->get_last_error(); - else - $ret = '<b>Add a new disk device</b> - <form method="POST"> - <table> - <tr> - <td>Disk image: </td> - <td><input type="text" name="img" /></td> - </tr> - <tr> - <td>Disk device in the guest: </td> - <td><input type="text" name="dev" value="hdb" /></td> - </tr> - <tr align="center"> - <td colspan="2"><input type="submit" value=" Add new disk " /></td> - </tr> - </table> - </form>'; - } + if ($img) { + $ret = $lv->domain_disk_add($domName, $_POST['img'], $_POST['dev']) ? 'Disk has been successfully added to the guest' : + 'Cannot add disk to the guest: '.$lv->get_last_error(); + } else { + $ret = "<b>Add a new disk device</b>" . + "<form method=\"POST\">" . + "<table>" . + "<tr>" . + "<td>Disk image: </td>" . + "<td><input type=\"text\" name=\"img\" /></td>" . + "</tr>" . + "<tr>" . + "<td>Disk device in the guest: </td>" . + "<td><input type=\"text\" name=\"dev\" value=\"hdb\" /></td>" . + "</tr>" . + "<tr align=\"center\">" . + "<td colspan=\"2\"><input type=\"submit\" value=\" Add new disk \" /></td>" . + "</tr>" . + "</table>" . + "</form>"; + } + } else if ($subaction == 'nic-remove') { + if ((array_key_exists('confirm', $_GET)) && ($_GET['confirm'] == 'yes')) { + $ret = $lv->domain_nic_remove($domName, $_GET['mac']) ? 'Network card has been removed successfully' : 'Cannot remove network card: '.$lv->get_last_error(); + } else { + $ret = "<table>" . + "<tr>" . + "<td colspan=\"2\">" . + "<b>Do you really want to delete NIC with MAC address <i>".$_GET['mac']." from the guest</i> ?</b><br/>" . + "</td>" . + "</tr>" . + "<tr align=\"center\">" . + "<td>" . + "<a href=\"".$_SERVER['REQUEST_URI']."&confirm=yes\">Yes, delete it</a>" . + "</td>" . + "<td>" . + "<a href=\"?action='.$action.'&uuid=".$_GET['uuid']."\">No, go back</a>" . + "</td>" . + "</tr>"; + $die = true; + } + } else if ($subaction == 'nic-add') { + $mac = array_key_exists('mac', $_POST) ? $_POST['mac'] : false; - if ($subaction == 'nic-remove') { - if ((array_key_exists('confirm', $_GET)) && ($_GET['confirm'] == 'yes')) - $ret = $lv->domain_nic_remove($domName, $_GET['mac']) ? 'Network card has been removed successfully' : 'Cannot remove network card: '.$lv->get_last_error(); - else { - $ret = '<table> - <tr> - <td colspan="2"> - <b>Do you really want to delete NIC with MAC address <i>'.$_GET['mac'].' from the guest</i> ?</b><br /> - </td> - </tr> - <tr align="center"> - <td> - <a href="'.$_SERVER['REQUEST_URI'].'&confirm=yes">Yes, delete it</a> - </td> - <td> - <a href="?action='.$action.'&uuid='.$_GET['uuid'].'">No, go back</a> - </td> - </tr>'; - $die = true; - } - } - if ($subaction == 'nic-add') { - $mac = array_key_exists('mac', $_POST) ? $_POST['mac'] : false; + if ($mac) { + $ret = $lv->domain_nic_add($domName, $_POST['mac'], $_POST['network'], $_POST['model']) ? 'Network card has been successfully added to the guest' : + 'Cannot add NIC to the guest: '.$lv->get_last_error(); + } else { + $ret = "<b>Add a new NIC device</b>" . + "<form method=\"POST\">" . + "<table>" . + "<tr>" . + "<td>Network card MAC address: </td>" . + "<td><input type=\"text\" name=\"mac\" value=\"".$lv->generate_random_mac_addr()."\" /></td>" . + "</tr>" . + "<tr>" . + "<td>Network: </td>" . + "<td>" . + "<select name=\"network\">"; - if ($mac) - $ret = $lv->domain_nic_add($domName, $_POST['mac'], $_POST['network'], $_POST['model']) ? 'Network card has been successfully added to the guest' : - 'Cannot add NIC to the guest: '.$lv->get_last_error(); - else { - $ret = '<b>Add a new NIC device</b> - <form method="POST"> - <table> - <tr> - <td>Network card MAC address: </td> - <td><input type="text" name="mac" value="'.$lv->generate_random_mac_addr().'" /></td> - </tr> - <tr> - <td>Network: </td> - <td> - <select name="network"> - '; - - $nets = $lv->get_networks(); - for ($i = 0; $i < sizeof($nets); $i++) - $ret .= '<option value="'.$nets[$i].'">'.$nets[$i].'</option>'; - - $ret .= ' - </select> - </td> - </tr> - <tr> - <td>Card model: </td> - <td> - <select name="model"> - '; - - $models = $lv->get_nic_models(); - for ($i = 0; $i < sizeof($models); $i++) - $ret .= '<option value="'.$models[$i].'">'.$models[$i].'</option>'; - - $ret .= ' - </select> - </td> - </tr> - <tr align="center"> - <td colspan="2"><input type="submit" value=" Add new network card " /></td> - </tr> - </table> - </form>'; - } - } + $nets = $lv->get_networks(); + for ($i = 0; $i < sizeof($nets); $i++) + $ret .= '<option value="'.$nets[$i].'">'.$nets[$i].'</option>'; - $dom = $lv->get_domain_object($domName); - $info = $lv->domain_get_info($dom); - $mem = number_format($info['memory'] / 1024, 2, '.', ' ').' MB'; - $cpu = $info['nrVirtCpu']; - $state = $lv->domain_state_translate($info['state']); - $id = $lv->domain_get_id($dom); - $arch = $lv->domain_get_arch($dom); - $vnc = $lv->domain_get_vnc_port($dom); + $ret .= "</select>" . + "</td>" . + "</tr>" . + "<tr>" . + "<td>Card model: </td>" . + "<td>" . + "<select name=\"model\">"; + + $models = $lv->get_nic_models(); + for ($i = 0; $i < sizeof($models); $i++) + $ret .= '<option value="'.$models[$i].'">'.$models[$i].'</option>'; + + $ret .= "</select>" . + "</td>" . + "</tr>" . + "<tr align=\"center\">" . + "<td colspan=\"2\"><input type=\"submit\" value=\" Add new network card \" /></td>" . + "</tr>" . + "</table>" . + "</form>"; + } + } + + $dom = $lv->get_domain_object($domName); + $info = $lv->domain_get_info($dom); + $mem = number_format($info['memory'] / 1024, 2, '.', ' ').' MB'; + $cpu = $info['nrVirtCpu']; + $state = $lv->domain_state_translate($info['state']); + $id = $lv->domain_get_id($dom); + $arch = $lv->domain_get_arch($dom); + $vnc = $lv->domain_get_vnc_port($dom); if (!$id) $id = 'N/A'; @@ -528,238 +525,222 @@ $vnc = 'N/A'; echo "<h2>$domName - domain information</h2>"; - echo "<b>Domain type: </b>".$lv->get_domain_type($domName).'<br />'; - echo "<b>Domain emulator: </b>".$lv->get_domain_emulator($domName).'<br />'; - echo "<b>Domain memory: </b>$mem<br />"; - echo "<b>Number of vCPUs: </b>$cpu<br />"; - echo "<b>Domain state: </b>$state<br />"; - echo "<b>Domain architecture: </b>$arch<br />"; - echo "<b>Domain ID: </b>$id<br />"; - echo "<b>VNC Port: </b>$vnc<br />"; - echo '<br />'; + echo "<b>Domain type: </b>".$lv->get_domain_type($domName).'<br/>'; + echo "<b>Domain emulator: </b>".$lv->get_domain_emulator($domName).'<br/>'; + echo "<b>Domain memory: </b>$mem<br/>"; + echo "<b>Number of vCPUs: </b>$cpu<br/>"; + echo "<b>Domain state: </b>$state<br/>"; + echo "<b>Domain architecture: </b>$arch<br/>"; + echo "<b>Domain ID: </b>$id<br/>"; + echo "<b>VNC Port: </b>$vnc<br/>"; + echo '<br/>'; - echo $ret; - if ($die) - die('</body></html'); + echo $ret; + if ($die) + die('</body></html'); /* Disk information */ echo "<h3>Disk devices</h3>"; $tmp = $lv->get_disk_stats($domName); - if (!empty($tmp)) { - echo "<table> - <tr> - <th>Disk storage</th> - <th>$spaces Storage driver type $spaces</th> - <th>$spaces Domain device $spaces</th> - <th>$spaces Disk capacity $spaces</th> - <th>$spaces Disk allocation $spaces</th> - <th>$spaces Physical disk size $spaces</th> - <th>$spaces Actions $spaces</th> - </tr>"; + if (!empty($tmp)) { + echo "<table>" . + "<tr>" . + "<th>Disk storage</th>" . + "<th>$spaces Storage driver type $spaces</th>" . + "<th>$spaces Domain device $spaces</th>" . + "<th>$spaces Disk capacity $spaces</th>" . + "<th>$spaces Disk allocation $spaces</th>" . + "<th>$spaces Physical disk size $spaces</th>" . + "<th>$spaces Actions $spaces</th>" . + "</tr>"; - for ($i = 0; $i < sizeof($tmp); $i++) { - $capacity = $lv->format_size($tmp[$i]['capacity'], 2); - $allocation = $lv->format_size($tmp[$i]['allocation'], 2); - $physical = $lv->format_size($tmp[$i]['physical'], 2); - $dev = (array_key_exists('file', $tmp[$i])) ? $tmp[$i]['file'] : $tmp[$i]['partition']; + for ($i = 0; $i < sizeof($tmp); $i++) { + $capacity = $lv->format_size($tmp[$i]['capacity'], 2); + $allocation = $lv->format_size($tmp[$i]['allocation'], 2); + $physical = $lv->format_size($tmp[$i]['physical'], 2); + $dev = (array_key_exists('file', $tmp[$i])) ? $tmp[$i]['file'] : $tmp[$i]['partition']; - echo "<tr> - <td>$spaces".basename($dev)."$spaces</td> - <td align=\"center\">$spaces{$tmp[$i]['type']}$spaces</td> - <td align=\"center\">$spaces{$tmp[$i]['device']}$spaces</td> - <td align=\"center\">$spaces$capacity$spaces</td> - <td align=\"center\">$spaces$allocation$spaces</td> - <td align=\"center\">$spaces$physical$spaces</td> - <td align=\"center\">$spaces - <a href=\"?action=$action&uuid={$_GET['uuid']}&subaction=disk-remove&dev={$tmp[$i]['device']}\"> - Remove disk device</a> - $spaces</td> - </tr>"; - } - echo "</table>"; - } - else - echo "Domain doesn't have any disk devices"; - - echo "<br />$spaces<a href=\"?action=$action&uuid={$_GET['uuid']}&subaction=disk-add\">Add new disk</a>"; - - /* Network interface information */ - echo "<h3>Network devices</h3>"; - $tmp = $lv->get_nic_info($domName); - if (!empty($tmp)) { - $anets = $lv->get_networks(VIR_NETWORKS_ACTIVE); - - echo "<table> - <tr> - <th>MAC Address</th> - <th>$spaces NIC Type$spaces</th> - <th>$spaces Network$spaces</th> - <th>$spaces Network active$spaces</th> - <th>$spaces Actions $spaces</th> - </tr>"; - - for ($i = 0; $i < sizeof($tmp); $i++) { - if (in_array($tmp[$i]['network'], $anets)) - $netUp = 'Yes'; - else - $netUp = 'No <a href="?action=virtual-networks&subaction=start&name='.$tmp[$i]['network'].'">[Start]</a>'; - - echo "<tr> - <td>$spaces{$tmp[$i]['mac']}$spaces</td> - <td align=\"center\">$spaces{$tmp[$i]['nic_type']}$spaces</td> - <td align=\"center\">$spaces{$tmp[$i]['network']}$spaces</td> - <td align=\"center\">$spaces$netUp$spaces</td> - <td align=\"center\">$spaces - <a href=\"?action=$action&uuid={$_GET['uuid']}&subaction=nic-remove&mac={$tmp[$i]['mac']}\"> - Remove network card</a> - $spaces</td> - </tr>"; - } - echo "</table>"; - - echo "<br />$spaces<a href=\"?action=$action&uuid={$_GET['uuid']}&subaction=nic-add\">Add new network card</a>"; - } - else - echo 'Domain doesn\'t have any network devices'; - - if ( $dom['state'] == 1 ) { - echo "<h3>Screenshot</h3><img src=\"?action=get-screenshot&uuid={$_GET['uuid']}&width=640\">"; - } + echo "<tr>" . + "<td>$spaces".basename($dev)."$spaces</td>" . + "<td align=\"center\">$spaces{$tmp[$i]['type']}$spaces</td>" . + "<td align=\"center\">$spaces{$tmp[$i]['device']}$spaces</td>" . + "<td align=\"center\">$spaces$capacity$spaces</td>" . + "<td align=\"center\">$spaces$allocation$spaces</td>" . + "<td align=\"center\">$spaces$physical$spaces</td>" . + "<td align=\"center\">$spaces" . + "<a href=\"?action=$action&uuid={$_GET['uuid']}&subaction=disk-remove&dev={$tmp[$i]['device']}\">" . + "Remove disk device</a>" . + "$spaces</td>" . + "</tr>"; + } + echo "</table>"; + } else { + echo "Domain doesn't have any disk devices"; } - else { - echo "Hypervisor URI: <i>$uri</i>, hostname: <i>$hn</i><br /> - Statistics: {$tmp['total']} domains, {$tmp['active']} active, {$tmp['inactive']} inactive<br /><br />"; + + echo "<br/>$spaces<a href=\"?action=$action&uuid={$_GET['uuid']}&subaction=disk-add\">Add new disk</a>"; + + /* Network interface information */ + echo "<h3>Network devices</h3>"; + $tmp = $lv->get_nic_info($domName); + if (!empty($tmp)) { + $anets = $lv->get_networks(VIR_NETWORKS_ACTIVE); + + echo "<table>" . + "<tr>" . + "<th>MAC Address</th>" . + "<th>$spaces NIC Type$spaces</th>" . + "<th>$spaces Network$spaces</th>" . + "<th>$spaces Network active$spaces</th>" . + "<th>$spaces Actions $spaces</th>" . + "</tr>"; + + for ($i = 0; $i < sizeof($tmp); $i++) { + if (in_array($tmp[$i]['network'], $anets)) + $netUp = 'Yes'; + else + $netUp = 'No <a href="?action=virtual-networks&subaction=start&name='.$tmp[$i]['network'].'">[Start]</a>'; + + echo "<tr>" . + "<td>$spaces{$tmp[$i]['mac']}$spaces</td>" . + "<td align=\"center\">$spaces{$tmp[$i]['nic_type']}$spaces</td>" . + "<td align=\"center\">$spaces{$tmp[$i]['network']}$spaces</td>" . + "<td align=\"center\">$spaces$netUp$spaces</td>" . + "<td align=\"center\">$spaces" . + "<a href=\"?action=$action&uuid={$_GET['uuid']}&subaction=nic-remove&mac={$tmp[$i]['mac']}\">" . + "Remove network card</a>" . + "$spaces</td>" . + "</tr>"; + } + echo "</table>"; + + echo "<br/>$spaces<a href=\"?action=$action&uuid={$_GET['uuid']}&subaction=nic-add\">Add new network card</a>"; + } else { + echo 'Domain doesn\'t have any network devices'; + } + + if ( $dom['state'] == 1 ) { + echo "<h3>Screenshot</h3><img src=\"?action=get-screenshot&uuid={$_GET['uuid']}&width=640\">"; + } + } else { + echo "Hypervisor URI: <i>$uri</i>, hostname: <i>$hn</i><br/>" . + "Statistics: {$tmp['total']} domains, {$tmp['active']} active, {$tmp['inactive']} inactive<br/><br/>"; $ret = false; - if ($action) { - $domName = $lv->domain_get_name_by_uuid($_GET['uuid']); + if ($action) { + $domName = $lv->domain_get_name_by_uuid($_GET['uuid']); - if ($action == 'domain-start') { - $ret = $lv->domain_start($domName) ? "Domain has been started successfully" : 'Error while starting domain: '.$lv->get_last_error(); - } - else if ($action == 'domain-stop') { - $ret = $lv->domain_shutdown($domName) ? "Domain has been stopped successfully" : 'Error while stopping domain: '.$lv->get_last_error(); - } - else if ($action == 'domain-destroy') { - $ret = $lv->domain_destroy($domName) ? "Domain has been destroyed successfully" : 'Error while destroying domain: '.$lv->get_last_error(); - } - else if (($action == 'domain-get-xml') || ($action == 'domain-edit')) { - $inactive = (!$lv->domain_is_running($domName)) ? true : false; - $xml = $lv->domain_get_xml($domName, $inactive); + if ($action == 'domain-start') { + $ret = $lv->domain_start($domName) ? "Domain has been started successfully" : 'Error while starting domain: '.$lv->get_last_error(); + } else if ($action == 'domain-stop') { + $ret = $lv->domain_shutdown($domName) ? "Domain has been stopped successfully" : 'Error while stopping domain: '.$lv->get_last_error(); + } else if ($action == 'domain-destroy') { + $ret = $lv->domain_destroy($domName) ? "Domain has been destroyed successfully" : 'Error while destroying domain: '.$lv->get_last_error(); + } else if (($action == 'domain-get-xml') || ($action == 'domain-edit')) { + $inactive = (!$lv->domain_is_running($domName)) ? true : false; + $xml = $lv->domain_get_xml($domName, $inactive); - if ($action == 'domain-edit') { - if (@$_POST['xmldesc']) { - $ret = $lv->domain_change_xml($domName, $_POST['xmldesc']) ? "Domain definition has been changed" : - 'Error changing domain definition: '.$lv->get_last_error(); - } - else - $ret = 'Editing domain XML description: <br /><br /><form method="POST"><table><tr><td>Domain XML description: </td>'. - '<td><textarea name="xmldesc" rows="25" cols="90%">'.$xml.'</textarea></td></tr><tr align="center"><td colspan="2">'. - '<input type="submit" value=" Edit domain XML description "></tr></form>'; - } - else - $ret = "Domain XML for domain <i>$domName</i>:<br /><br />".htmlentities($xml); - } - } + if ($action == 'domain-edit') { + if (@$_POST['xmldesc']) { + $ret = $lv->domain_change_xml($domName, $_POST['xmldesc']) ? "Domain definition has been changed" : + 'Error changing domain definition: '.$lv->get_last_error(); + } else { + $ret = "Editing domain XML description: <br/><br/><form method=\"POST\"><table><tr><td>Domain XML description: </td>". + "<td><textarea name=\"xmldesc\" rows=\"25\" cols=\"90%\">".$xml."</textarea></t\></tr><tr align=\"center\"><td colspan=\"2\">". + "<input type=\"submit\" value=\" Edit domain XML description \"></tr></form>"; + } + } else { + $ret = "Domain XML for domain <i>$domName</i>:<br/><br/>".htmlentities($xml); + } + } + } - $doms = $lv->get_domains(); - echo "<table> - <tr> - <th>Name</th> - <th>CPU#</th> - <th>Memory</th> - <th>Disk(s)</th> - <th>NICs</th> - <th>Arch</th> - <th>State</th> - <th>ID / VNC port</th>"; + $doms = $lv->get_domains(); + echo "<table>" . + "<tr>" . + "<th>Name</th>" . + "<th>CPU#</th>" . + "<th>Memory</th>" . + "<th>Disk(s)</th>" . + "<th>NICs</th>" . + "<th>Arch</th>" . + "<th>State</th>" . + "<th>ID / VNC port</th>"; - if (($tmp['active'] > 0) && ($lv->supports('screenshot'))) - echo " - <th>Domain screenshot</th> - "; + if (($tmp['active'] > 0) && ($lv->supports('screenshot'))) + echo "<th>Domain screenshot</th>"; - echo " - <th>Action</th> - </tr>"; - $active = $tmp['active']; - foreach ($doms as $name) { - $dom = $lv->get_domain_object($name); - $uuid = libvirt_domain_get_uuid_string($dom); - $info = $lv->domain_get_info($dom); - $mem = number_format($info['memory'] / 1024, 2, '.', ' ').' MB'; - $cpu = $info['nrVirtCpu']; - $state = $lv->domain_state_translate($info['state']); - $id = $lv->domain_get_id($dom); - $arch = $lv->domain_get_arch($dom); - $vnc = $lv->domain_get_vnc_port($dom); - $nics = $lv->get_network_cards($dom); - if (($diskcnt = $lv->get_disk_count($dom)) > 0) { - $disks = $diskcnt.' / '.$lv->get_disk_capacity($dom); - $diskdesc = 'Current physical size: '.$lv->get_disk_capacity($dom, true); - } - else { - $disks = '-'; - $diskdesc = ''; - } + echo "<th>Action</th>" . + "</tr>"; - if ($vnc < 0) - $vnc = '-'; - else - $vnc = $_SERVER['HTTP_HOST'].':'.$vnc; + $active = $tmp['active']; + foreach ($doms as $name) { + $dom = $lv->get_domain_object($name); + $uuid = libvirt_domain_get_uuid_string($dom); + $info = $lv->domain_get_info($dom); + $mem = number_format($info['memory'] / 1024, 2, '.', ' ').' MB'; + $cpu = $info['nrVirtCpu']; + $state = $lv->domain_state_translate($info['state']); + $id = $lv->domain_get_id($dom); + $arch = $lv->domain_get_arch($dom); + $vnc = $lv->domain_get_vnc_port($dom); + $nics = $lv->get_network_cards($dom); + if (($diskcnt = $lv->get_disk_count($dom)) > 0) { + $disks = $diskcnt.' / '.$lv->get_disk_capacity($dom); + $diskdesc = 'Current physical size: '.$lv->get_disk_capacity($dom, true); + } else { + $disks = '-'; + $diskdesc = ''; + } - unset($tmp); - if (!$id) - $id = '-'; - unset($dom); + if ($vnc < 0) + $vnc = '-'; + else + $vnc = $_SERVER['HTTP_HOST'].':'.$vnc; - echo "<tr> - <td>$spaces - <a href=\"?action=domain-information&uuid=$uuid\">$name</a> - $spaces</td> - <td>$spaces$cpu$spaces</td> - <td>$spaces$mem$spaces</td> - <td align=\"center\" title='$diskdesc'>$spaces$disks$spaces</td> - <td align=\"center\">$spaces$nics$spaces</td> - <td>$spaces$arch$spaces</td> - <td>$spaces$state$spaces</td> - <td align=\"center\">$spaces$id / $vnc$spaces</td> - "; + unset($tmp); + if (!$id) + $id = '-'; + unset($dom); - if (($active > 0) && ($lv->supports('screenshot'))) - echo " - <td align=\"center\"><img src=\"?action=get-screenshot&uuid=$uuid&width=120\" id=\"screenshot$i\"></td> - "; + echo "<tr>" . + "<td>$spaces" . + "<a href=\"?action=domain-information&uuid=$uuid\">$name</a>" . + "$spaces</td>" . + "<td>$spaces$cpu$spaces</td>" . + "<td>$spaces$mem$spaces</td>" . + "<td align=\"center\" title='$diskdesc'>$spaces$disks$spaces</td>" . + "<td align=\"center\">$spaces$nics$spaces</td>" . + "<td>$spaces$arch$spaces</td>" . + "<td>$spaces$state$spaces</td>" . + "<td align=\"center\">$spaces$id / $vnc$spaces</td>"; - echo " - <td align=\"center\">$spaces - "; + if (($active > 0) && ($lv->supports('screenshot'))) + echo "<td align=\"center\"><img src=\"?action=get-screenshot&uuid=$uuid&width=120\" id=\"screenshot$i\"></td>"; - if ($lv->domain_is_running($name)) - echo "<a href=\"?action=domain-stop&uuid=$uuid\">Stop domain</a> | <a href=\"?action=domain-destroy&uuid=$uuid\">Destroy domain</a> |"; - else - echo "<a href=\"?action=domain-start&uuid=$uuid\">Start domain</a> |"; + echo "<td align=\"center\">$spaces"; - echo " - <a href=\"?action=domain-get-xml&uuid=$uuid\">Dump domain</a> - "; + if ($lv->domain_is_running($name)) + echo "<a href=\"?action=domain-stop&uuid=$uuid\">Stop domain</a> | <a href=\"?action=domain-destroy&uuid=$uuid\">Destroy domain</a> |"; + else + echo "<a href=\"?action=domain-start&uuid=$uuid\">Start domain</a> |"; - if (!$lv->domain_is_running($name)) - echo "| <a href=\"?action=domain-edit&uuid=$uuid\">Edit domain XML</a>"; - else - if ($active > 0) - echo "| <a href=\"?action=get-screenshot&uuid=$uuid\">Get screenshot</a>"; + echo "<a href=\"?action=domain-get-xml&uuid=$uuid\">Dump domain</a>"; - echo " - $spaces - </td> - </tr>"; - } - echo "</table>"; + if (!$lv->domain_is_running($name)) + echo "| <a href=\"?action=domain-edit&uuid=$uuid\">Edit domain XML</a>"; + else if ($active > 0) + echo "| <a href=\"?action=get-screenshot&uuid=$uuid\">Get screenshot</a>"; - echo "<br /><pre>$ret</pre>"; - } + echo "$spaces" . + "</td>" . + "</tr>"; + } + echo "</table>"; + + echo "<br/><pre>$ret</pre>"; + } ?> </body> </html> -- 2.8.4

So we have this special class Libvirt implemented in examples/libvirt.php. It's aim is to wrap some low-level libvirt APIs into slightly more advanced methods. Now, when instantiating the class, connection URI can be passed in which case the class will connect to it right in the constructor. However, later in the code, the returned value (which is an object reference) is compared against false, which will never ever be true. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- examples/index.php | 4 ++-- examples/libvirt.php | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/examples/index.php b/examples/index.php index 687f408..dbaa1bf 100644 --- a/examples/index.php +++ b/examples/index.php @@ -1,7 +1,7 @@ <?php require('libvirt.php'); - $lv = new Libvirt('qemu:///system'); - if ($lv == false) + $lv = new Libvirt(); + if ($lv->connect("qemu:///system") == false) die('<html><body>Cannot open connection to hypervisor</body></html>'); $hn = $lv->get_hostname(); if ($hn == false) diff --git a/examples/libvirt.php b/examples/libvirt.php index c60889a..14436d8 100644 --- a/examples/libvirt.php +++ b/examples/libvirt.php @@ -5,11 +5,9 @@ class Libvirt { private $allow_cached = true; private $dominfos = array(); - function Libvirt($uri = false, $debug=false) { + function Libvirt($debug = false) { if ($debug) $this->set_logfile($debug); - if ($uri != false) - $this->connect($uri); } function _set_last_error() { @@ -32,6 +30,7 @@ class Libvirt { $this->conn=libvirt_connect($uri, false); if ($this->conn==false) return $this->_set_last_error(); + return true; } function domain_disk_add($domain, $img, $dev, $type='scsi', $driver='raw') { -- 2.8.4

Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- examples/libvirt.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/libvirt.php b/examples/libvirt.php index 14436d8..2cab7a8 100644 --- a/examples/libvirt.php +++ b/examples/libvirt.php @@ -210,9 +210,9 @@ class Libvirt { if (is_resource($nameRes)) return $nameRes; - $dom=libvirt_domain_lookup_by_name($this->conn, $nameRes); + $dom=@libvirt_domain_lookup_by_name($this->conn, $nameRes); if (!$dom) { - $dom=libvirt_domain_lookup_by_uuid_string($this->conn, $nameRes); + $dom=@libvirt_domain_lookup_by_uuid_string($this->conn, $nameRes); if (!$dom) return $this->_set_last_error(); } -- 2.8.4

Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- examples/index.php | 12 +++++++----- examples/libvirt.php | 5 +++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/examples/index.php b/examples/index.php index dbaa1bf..bdeb506 100644 --- a/examples/index.php +++ b/examples/index.php @@ -668,16 +668,16 @@ "<th>State</th>" . "<th>ID / VNC port</th>"; - if (($tmp['active'] > 0) && ($lv->supports('screenshot'))) + if ($lv->supports('screenshot')) echo "<th>Domain screenshot</th>"; echo "<th>Action</th>" . "</tr>"; - $active = $tmp['active']; foreach ($doms as $name) { $dom = $lv->get_domain_object($name); $uuid = libvirt_domain_get_uuid_string($dom); + $active = $lv->domain_is_active($dom); $info = $lv->domain_get_info($dom); $mem = number_format($info['memory'] / 1024, 2, '.', ' ').' MB'; $cpu = $info['nrVirtCpu']; @@ -716,8 +716,10 @@ "<td>$spaces$state$spaces</td>" . "<td align=\"center\">$spaces$id / $vnc$spaces</td>"; - if (($active > 0) && ($lv->supports('screenshot'))) - echo "<td align=\"center\"><img src=\"?action=get-screenshot&uuid=$uuid&width=120\" id=\"screenshot$i\"></td>"; + if ($lv->supports('screenshot') && $active) + echo "<td align=\"center\"><img src=\"?action=get-screenshot&uuid=$uuid&width=120\" id=\"screenshot$id\"></td>"; + else + echo "<td>$spaces</td>"; echo "<td align=\"center\">$spaces"; @@ -735,7 +737,7 @@ echo "$spaces" . "</td>" . - "</tr>"; + "</tr>\n"; } echo "</table>"; diff --git a/examples/libvirt.php b/examples/libvirt.php index 2cab7a8..c30f06a 100644 --- a/examples/libvirt.php +++ b/examples/libvirt.php @@ -1162,5 +1162,10 @@ class Libvirt { $tmp = libvirt_node_get_info($this->conn); return ($tmp) ? $tmp : $this->_set_last_error(); } + + function domain_is_active($domain) { + $dom = $this->get_domain_object($domain); + return libvirt_domain_is_active($dom); + } } ?> -- 2.8.4

Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- examples/libvirt.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/libvirt.php b/examples/libvirt.php index c30f06a..96aac6e 100644 --- a/examples/libvirt.php +++ b/examples/libvirt.php @@ -502,12 +502,13 @@ class Libvirt { } function translate_perms($mode) { - $mode = (string)((int)$mode); + $mode = (int)$mode; $tmp = '---------'; - for ($i = 0; $i < 3; $i++) { - $bits = (int)$mode[$i]; + for ($i = 2; $i >=0 ; $i--) { + $bits = $mode % 10; + $mode /= 10; if ($bits & 4) $tmp[ ($i * 3) ] = 'r'; if ($bits & 2) -- 2.8.4

Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- examples/libvirt.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/examples/libvirt.php b/examples/libvirt.php index 96aac6e..32d8314 100644 --- a/examples/libvirt.php +++ b/examples/libvirt.php @@ -456,12 +456,16 @@ class Libvirt { if (!is_string($otmp2)) return $this->_set_last_error(); $tmp = libvirt_storagepool_get_info($res); - $tmp['volume_count'] = sizeof( libvirt_storagepool_list_volumes($res) ); $tmp['active'] = libvirt_storagepool_is_active($res); $tmp['path'] = $path; $tmp['permissions'] = $perms; $tmp['id_user'] = $otmp1; $tmp['id_group'] = $otmp2; + if ($tmp['active']) { + $tmp['volume_count'] = sizeof( libvirt_storagepool_list_volumes($res) ); + } else { + $tmp['volume_count'] = 0; + } return $tmp; } -- 2.8.4

Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- configure.ac | 2 ++ examples/index.php | 6 ++--- examples/libvirt.php | 68 ++++++++++++++++++++++++------------------------ libvirt-php.spec.in | 1 + m4/virt-php-extension.m4 | 35 +++++++++++++++++++++++++ 5 files changed, 75 insertions(+), 37 deletions(-) create mode 100644 m4/virt-php-extension.m4 diff --git a/configure.ac b/configure.ac index ed0d555..a82f7e6 100644 --- a/configure.ac +++ b/configure.ac @@ -164,6 +164,8 @@ AC_SUBST(PHP_LDFLAGS) LIBVIRT_CHECK_PHP_EXTENSIONDIR LIBVIRT_CHECK_PHP_CONFDIR +LIBVIRT_CHECK_PHP_EXTENSION([imagick]) + OS=`uname` if test "$OS" = "Darwin"; then WL=-Wl, diff --git a/examples/index.php b/examples/index.php index bdeb506..1ba8193 100644 --- a/examples/index.php +++ b/examples/index.php @@ -14,13 +14,13 @@ if (array_key_exists('width', $_GET) && $_GET['width']) $tmp = $lv->domain_get_screenshot_thumbnail($_GET['uuid'], $_GET['width']); else - $tmp = $lv->domain_get_screenshot($_GET['uuid']); + $tmp = $lv->domain_get_screenshot($_GET['uuid'], 0); if (!$tmp) { echo $lv->get_last_error().'<br/>'; } else { - Header('Content-Type: image/png'); - die($tmp); + Header('Content-Type: ' . $tmp['mime']); + die($tmp['data']); } } ?> diff --git a/examples/libvirt.php b/examples/libvirt.php index 32d8314..f65c50e 100644 --- a/examples/libvirt.php +++ b/examples/libvirt.php @@ -61,50 +61,50 @@ class Libvirt { return ($tmp) ? $tmp : $this->_set_last_error(); } - function domain_get_screenshot($domain) { + function domain_get_screenshot($domain, $convert = 1) { $dom = $this->get_domain_object($domain); - $tmp = libvirt_domain_get_screenshot($dom, $this->get_hostname() ); - return ($tmp) ? $tmp : $this->_set_last_error(); + $tmp = libvirt_domain_get_screenshot_api($dom); + if ($tmp == false) + return $this->_set_last_error(); + + $mime = $tmp['mime']; + + if ($convert && $tmp['mime'] != "image/png") { + $image = new Imagick(); + $image->readImage($tmp['file']); + $image->setImageFormat("png"); + $data = $image->getImageBlob(); + $mime = "image/png"; + } else { + $fp = fopen($tmp['file'], "rb"); + $data = fread($fp, filesize($tmp['file'])); + fclose($fp); + } + unlink($tmp['file']); + unset($tmp['file']); + $tmp['data'] = $data; + $tmp['mime'] = $mime; + + return $tmp; } function domain_get_screenshot_thumbnail($domain, $w=120) { $screen = $this->domain_get_screenshot($domain); - $imgFile = tempnam("/tmp", "libvirt-php-tmp-resize-XXXXXX");; - if ($screen) { - $fp = fopen($imgFile, "wb"); - fwrite($fp, $screen); - fclose($fp); - } + if (!$screen) + return false; - if (file_exists($imgFile) && $screen) { - list($width, $height) = getimagesize($imgFile); - $h = ($height / $width) * $w; - } else { - $w = $h = 1; - //$h = $w * (3 / 4.5); - } + $image = new Imagick(); + $image->readImageBlob($screen['data']); + $origW = $image->getImageWidth(); + $origH = $image->getImageHeight(); + $h = ($w / $origW) * $origH; + $image->resizeImage($w, $h, 0, 0); - $new = imagecreatetruecolor($w, $h); - if ($screen) { - $img = imagecreatefrompng($imgFile); - imagecopyresampled($new,$img,0,0,0,0, $w,$h,$width,$height); - imagedestroy($img); - } else { - $c = imagecolorallocate($new, 255, 255, 255); - imagefill($new, 0, 0, $c); - } + $screen['data'] = $image->getImageBlob(); - imagepng($new, $imgFile); - imagedestroy($new); - - $fp = fopen($imgFile, "rb"); - $data = fread($fp, filesize($imgFile)); - fclose($fp); - - unlink($imgFile); - return $data; + return $screen; } function domain_get_screen_dimensions($domain) { diff --git a/libvirt-php.spec.in b/libvirt-php.spec.in index 2aae1d5..8cf6144 100644 --- a/libvirt-php.spec.in +++ b/libvirt-php.spec.in @@ -27,6 +27,7 @@ BuildRequires: php-devel BuildRequires: libvirt-devel >= %{req_libvirt_version} BuildRequires: libxml2-devel BuildRequires: libxslt +BuildRequires: php-pecl-imagick %if 0%{?suse_version} BuildRequires: xhtml-dtd %else diff --git a/m4/virt-php-extension.m4 b/m4/virt-php-extension.m4 new file mode 100644 index 0000000..9040eb1 --- /dev/null +++ b/m4/virt-php-extension.m4 @@ -0,0 +1,35 @@ +dnl The libvirt-php.so config +dnl +dnl Copyright (C) 2016 Red Hat, Inc. +dnl +dnl This library is free software; you can redistribute it and/or +dnl modify it under the terms of the GNU Lesser General Public +dnl License as published by the Free Software Foundation; either +dnl version 2.1 of the License, or (at your option) any later version. +dnl +dnl This library is distributed in the hope that it will be useful, +dnl but WITHOUT ANY WARRANTY; without even the implied warranty of +dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +dnl Lesser General Public License for more details. +dnl +dnl You should have received a copy of the GNU Lesser General Public +dnl License along with this library. If not, see +dnl <http://www.gnu.org/licenses/>. +dnl + +dnl +dnl Check whether php module exists +dnl +dnl LIBVIRT_CHECK_PHP_EXTENSION([EXTENSION]) +dnl +AC_DEFUN([LIBVIRT_CHECK_PHP_EXTENSION],[ + AC_MSG_CHECKING([for php module $1]) + + module="$(php -m | grep $1)" + + if test "x$module" = "x"; then + AC_MSG_ERROR([php module $1 not found]) + else + AC_MSG_RESULT([found]) + fi +]) -- 2.8.4
participants (1)
-
Michal Privoznik