public function work() { $t = new Timer('libvirt extension'); if (!extension_loaded('libvirt')) { Errors::add('libvirt extension', 'Libvirt PHP extension not installed'); $this->res = false; return; } if (!$this->connect()) { Errors::add('libvirt extension', 'Failed connecting'); return; } if (!($doms = libvirt_list_domains($this->connection))) { Errors::add('libvirt extension', 'Failed getting domain list'); $this->res = false; return; } foreach ($doms as $name) { if (!($domain = libvirt_domain_lookup_by_name($this->connection, $name))) { continue; } if (!($info = libvirt_domain_get_info($domain)) || !is_array($info)) { continue; } $info['autostart'] = libvirt_domain_get_autostart($domain); if ($info['autostart'] == 1) { $info['autostart'] = 'Yes'; } elseif ($info['autostart'] == 0) { $info['autostart'] = 'No'; } else { $info['autostart'] = 'N/A'; } $info['nets'] = array(); $nets = @libvirt_domain_get_interface_devices($domain); foreach ($nets as $key => $net) { if (!is_numeric($key)) { continue; } $info['nets'][] = $net; } $info['storage'] = array(); foreach ((array) @libvirt_domain_get_disk_devices($domain) as $blockName) { if (!is_string($blockName)) { continue; } // Sometime device exists but libvirt fails to get more docs. just settle for device name if (!($blockInfo = @libvirt_domain_get_block_info($domain, $blockName)) || !is_array($blockInfo)) { $info['storage'][] = array('device' => $blockName); continue; } if (isset($blockInfo['partition']) && !isset($blockInfo['file'])) { $blockInfo['file'] = $blockInfo['partition']; } $info['storage'][] = $blockInfo; } $this->VMs[$name] = $info; } $this->res = true; }
/** * Domain Resource * * @param $domain * @return resource * @throws \Exception */ public function getDomainResource($domain) { $domain = libvirt_domain_lookup_by_name($this->resource, $domain); if (is_resource($domain)) { return $domain; } $domain = libvirt_domain_lookup_by_uuid_string($this->resource, $domain); if (is_resource($domain)) { return $domain; } throw new \Exception("Dominio informado não existe: " . libvirt_get_last_error()); }
public function index() { $conn = libvirt_connect('qemu+tcp://localhost/system', false); $doms = libvirt_list_domains($conn); $r_doms = []; foreach ($doms as $dom) { $r_dom = new \stdClass(); $res = libvirt_domain_lookup_by_name($conn, $dom); $dd = libvirt_domain_get_info($res); $r_dom->name = $dom; $r_dom->cpu = $dd['nrVirtCpu']; $r_dom->memory = $dd['maxMem'] / 1024 / 1024; $r_dom->state = $dd['state']; $r_doms[] = $r_dom; } return $r_doms; return Vm::orderBy('created_at', 'asc')->get(); }
public function work() { $t = new LinfoTimerStart('libvirt extension'); if (!extension_loaded('libvirt')) { $this->LinfoError->add('libvirt extension', 'Libvirt PHP extension not installed'); $this->res = false; return false; } if (!$this->connect()) { return false; } if (!($doms = libvirt_list_domains($this->connection))) { $this->LinfoError->add('libvirt extension', 'Failed getting domain list'); $this->res = false; return false; } foreach ($doms as $name) { if (!($domain = libvirt_domain_lookup_by_name($this->connection, $name))) { continue; } if (!($info = libvirt_domain_get_info($domain)) || !is_array($info)) { continue; } $info['storage'] = array(); foreach ((array) @libvirt_domain_get_disk_devices($domain) as $blockName) { if (!is_string($blockName)) { continue; } // Sometime device exists but libvirt fails to get more docs. just settle for device name if (!($blockInfo = libvirt_domain_get_block_info($domain, $blockName)) || !is_array($blockInfo)) { $info['storage'][] = array('device' => $blockName); continue; } if (isset($blockInfo['partition']) && !isset($blockInfo['file'])) { $blockInfo['file'] = $blockInfo['partition']; } $info['storage'][] = $blockInfo; } $this->VMs[$name] = $info; } $this->res = true; }
$courseid = $_POST['courseid']; $list = libvirt_list_domains($libvirt->conn); $size = sizeof($list); for ($i = 0; $i < $size; $i++) { if (strlen(strstr($list[$i], $courseid)) > 0) { echo "<div id=\"uppervm\">"; echo "<div id=\"vmname\">" . $list[$i] . "</div>"; echo "<div id=\"vmscreen\">"; if ($libvirt->is_active($list[$i])) { echo "<img src=\"images/running.jpg\" height=95% style=\"border:2px solid gray;border-radius:10px;\">"; } else { echo "<img src=\"images/vm.png\" height=95% style=\"border:2px solid gray;border-radius:10px;\">"; } echo "</div>"; echo "<div id=\"vminfo\">"; $res = libvirt_domain_lookup_by_name($libvirt->conn, $list[$i]) or die(libvirt_get_last_error()); $info = libvirt_domain_get_info($res) or die(libvirt_get_last_error()); $result = mysql_query("select template_info.os from template_info,course_info where template_info.templatename=course_info.template && course_info.courseid in \t\t\t\t\t\t\t(SELECT courseid from vm_info where vmname='{$list[$i]}')"); $row = mysql_fetch_array($result); echo "Max memory:\t" . $info['maxMem'] . " KB<br>"; echo "Used Memory:\t" . $info['memory'] . " KB<br>"; if (!$libvirt->is_active($list[$i])) { echo "State:\t Shutdown" . "<br>"; } else { echo "State:\t Running" . "<br>"; } echo "CPU(s):\t" . $info['nrVirtCpu'] . "<br>"; echo "</div><div id=\"vmop\">"; echo "<button type=\"submit\" name=\"ON\" value=\"{$list[$i]}\"><img src=\"images/poweron.png\" alt=\"\" style=\"width:11%;height:10%;\"></button>"; echo "<button type=\"submit\" name=\"OFF\" value=\"{$list[$i]}\"><img src=\"images/poweroff.png\" alt=\"\" style=\"width:11%;height:21%\";></button>"; echo "</div></div>";
function get_domain_by_name($name) { $tmp = libvirt_domain_lookup_by_name($this->conn, $name); return $tmp ? $tmp : $this->_set_last_error(); }
echo "<button type=\"submit\" name=\"create\" value=\"{$vmname}\"><img src=\"images/createvm.png\" alt=\"CreateVM\" style=\"width:55%;height:100%;\"></\t\t\n\t\t\t\tbutton>"; echo "</div>"; echo "<div id=\"vminfo\">"; $libvirt->student_domain_info($list['vmname']); echo "</div><div id=\"vmop\">"; echo "<button type=\"submit\" name=\"start\" value=\"{$vmname}\"><img src=\"images/poweron.png\" alt=\"\" style=\"width:11%;height:10%;\"></button>"; echo "<button type=\"submit\" name=\"stop\" value=\"{$vmname}\"><img src=\"images/poweroff.png\" alt=\"\" style=\"width:11%;height:21%\";></button>"; echo "</div></div>"; } } } } if (isset($_POST['type']) && $_POST['type'] == 'INACTIVE') { $result = mysql_query("Select * from vm_info where vmname LIKE '{$user}%'"); while ($list = mysql_fetch_array($result)) { if (libvirt_domain_lookup_by_name($libvirt->conn, $list['vmname']) != 0) { if (!$libvirt->is_active($list['vmname'])) { echo "<div id=\"uppervm\">"; echo "<div id=\"vmname\">" . $list['vmname'] . "</div>"; echo "<div id=\"vmscreen\">"; $vmname = $list['vmname']; echo "<button type=\"submit\" name=\"create\" value=\"{$vmname}\"><img src=\"images/createvm.png\" alt=\"CreateVM\" style=\"width:55%;height:100%;\"></\t\t\n\t\t\t\tbutton>"; echo "</div>"; echo "<div id=\"vminfo\">"; $libvirt->student_domain_info($list['vmname']); echo "</div><div id=\"vmop\">"; echo "<button type=\"submit\" name=\"start\" value=\"{$vmname}\"><img src=\"images/poweron.png\" alt=\"\" style=\"width:11%;height:10%;\"></button>"; echo "<button type=\"submit\" name=\"stop\" value=\"{$vmname}\"><img src=\"images/poweroff.png\" alt=\"\" style=\"width:11%;height:21%\";></button>"; echo "</div></div>"; } }
function ace_kvm_vm_delete_snapshots($vm_virt_id) { global $host_conn; $result = FALSE; $vm_res = libvirt_domain_lookup_by_name($host_conn, $vm_virt_id); ace_kvm_log_last_error(); $vm_snapshot_list = libvirt_list_domain_snapshots($vm_res); ace_kvm_log_last_error(); foreach ($vm_snapshot_list as $vm_snapshot) { $vm_snapshot_res = libvirt_domain_snapshot_lookup_by_name($vm_res, $vm_snapshot); ace_kvm_log_last_error(); # there's a problem with the flag VIR_SNAPSHOT_DELETE_CHILDREN here $result = libvirt_domain_snapshot_delete($vm_snapshot_res); ace_kvm_log_last_error(); } $return = $result; return $return; }