diff app/controllers/storage_controller.php @ 1:eb4fee3d9bb8

added StoragePools management functions (storage_controller)
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Wed, 27 Jul 2011 00:00:25 +0900
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/controllers/storage_controller.php	Wed Jul 27 00:00:25 2011 +0900
@@ -0,0 +1,204 @@
+<?php
+
+class StorageController extends AppController
+{
+	public $name = "Storage";
+	public $uses = null;
+
+	public function index()
+	{
+		$this->redirect("exec");
+	}
+
+	public function exec()
+	{
+		$con = WebVirtUtil::GetConnection();
+
+		$pools = array();
+		foreach(libvirt_list_storagepools($con) as $name){
+			$pool = libvirt_storagepool_lookup_by_name($con,$name);
+			$uuid = libvirt_storagepool_get_uuid_string($pool);
+			$info = libvirt_storagepool_get_info($pool);
+			$state = "NOT RUNNING";
+			switch($info["state"]){
+				case 0:
+					break;
+				case 1:
+					$state = "INITIALIZING";
+					break;
+				case 2:
+					$state = "RUNNING";
+					break;
+				case 3:
+					$state = "DEGRADED";
+					break;
+				case 4:
+					$state = "INACCESSIBLE";
+					break;
+			}
+			$capacity = $info["capacity"];
+			$allocation = $info["allocation"];
+			$available = $info["available"];
+
+			$volumes = array();
+			foreach(libvirt_storagepool_list_volumes($pool) as $vname){
+				$volume = libvirt_storagevolume_lookup_by_name($pool,$vname);
+				$vinfo = libvirt_storagevolume_get_info($volume);
+				$vpath = libvirt_storagevolume_get_path($volume);
+				$vtype = ($vinfo["type"] == 0) ? "FILE" : "BLOCK";
+				$vcapa = $vinfo["capacity"];
+				$valloc = $vinfo["allocation"];
+				
+				array_push($volumes,array("name" => $vname,"path" => $vpath,"type" => $vtype,
+							"capacity" => $vcapa,"allocation" => $valloc));
+			}
+			
+			array_push($pools,array("name" => $name,"state" => $state,"capacity" => $capacity,
+						"allocation" => $allocation,"available" => $available,
+						"volumes" => $volumes,"uuid" => $uuid));
+		}
+		$this->set("storagepools",$pools);
+
+	}
+
+	public function EditPool()
+	{
+		if(!isset($this->params["url"]["id"])){
+			$this->set("content","StoragePool ID is required.");
+			$this->render("storage/Error");
+			exit();
+		}
+
+		$con = WebVirtUtil::GetConnection();
+		$id = $this->params["url"]["id"];
+		$res = libvirt_storagepool_lookup_by_uuid_string($con,$id);
+
+		$pool = array();
+		$pool["id"] = $id;
+		$pool["name"] = libvirt_storagepool_get_name($res);
+		$info = libvirt_storagepool_get_info($res);
+		$state = "NOT RUNNING";
+		switch($info["state"]){
+			case 0:
+				break;
+			case 1:
+				$state = "INITIALIZING";
+				break;
+			case 2:
+				$state = "RUNNING";
+				break;
+			case 3:
+				$state = "DEGRADED";
+				break;
+			case 4:
+				$state = "INACCESSIBLE";
+				break;
+		}
+		$pool["state"] = $state;
+		$pool["capacity"] = $info["capacity"];
+		$pool["allocation"] = $info["allocation"];
+		$pool["available"] = $info["available"];
+
+		$volumes = array();
+		foreach(libvirt_storagepool_list_volumes($res) as $vname){
+			$volume = libvirt_storagevolume_lookup_by_name($res,$vname);
+			$vinfo = libvirt_storagevolume_get_info($volume);
+			$vpath = libvirt_storagevolume_get_path($volume);
+			$vtype = ($vinfo["type"] == 0) ? "FILE" : "BLOCK";
+			$vcapa = $vinfo["capacity"];
+			$valloc = $vinfo["allocation"];
+			
+			array_push($volumes,array("name" => $vname,"path" => $vpath,"type" => $vtype,
+						"capacity" => $vcapa,"allocation" => $valloc));
+		}
+		$pool["volumes"] = $volumes;
+		$this->set("pool",$pool);
+	}
+
+	public function DeleteVolume()
+	{
+		if(!isset($this->params["form"])){
+			$this->set("content","invalid volume information");
+			$this->render("error");
+			return;
+		}
+
+		$con = WebVirtUtil::GetConnection();
+		if($con === FALSE){
+			$this->set("content","libvirt Error :".libvirt_get_last_error());
+			$this->render("error");
+			return;
+		}
+
+		$pid = $this->params["form"]["pid"];
+		$pool = @libvirt_storagepool_lookup_by_uuid_string($con,$pid);
+		$vname = $this->params["form"]["vname"];
+		$volume = @libvirt_storagevolume_lookup_by_name($pool,$vname);
+		if($volume === FALSE){
+			$this->set("content","libvirt Error :".libvirt_get_last_error());
+			$this->render("error");
+			return;
+		}
+
+		// 0 => VIR_STORAGE_VOL_DELETE_NORMAL : Delete metadata only (fast operation)
+		$ret = @libvirt_storagevolume_delete($volume,0);
+		if($volume === FALSE){
+			$this->set("content","libvirt Error :".libvirt_get_last_error());
+			$this->render("error");
+			return;
+		}
+
+		$this->set("content",$vname." was successfully deleted");
+	}
+
+	public function AddVolume()
+	{
+		if(!isset($this->params["form"])){
+			$this->set("content","invalid volume definition");
+			$this->render("error");
+			return;
+		}
+
+		$id = htmlspecialchars($this->params["form"]["id"]);
+		$name = htmlspecialchars($this->params["form"]["name"]);
+		$capacity = htmlspecialchars($this->params["form"]["capacity"]);
+		$allocate = htmlspecialchars($this->params["form"]["allocate"]);
+
+		//validate
+
+		$xml = "<volume>";
+		$xml .= "<name>".$name."</name>";
+		$xml .= "<capacity>".$capacity."</capacity>";
+		$xml .= "<allocation>".$allocate."</allocation>";
+		$xml .= "</volume>";
+
+		$con = WebVirtUtil::GetConnection();
+		if($con === FALSE){
+			$this->set("content","libvirt Error : ".libvirt_get_last_error());
+			$this->render("error");
+			return;
+		}
+
+		$res = @libvirt_storagepool_lookup_by_uuid_string($con,$id);
+		if($res === FALSE){
+			$this->set("content","libvirt Error : ".libvirt_get_last_error());
+			$this->render("error");
+			return;
+		}
+
+		$ret = @libvirt_storagevolume_create_xml($res,$xml);
+		if($ret === FALSE){
+			$this->set("content","libvirt Error : ".libvirt_get_last_error());
+			$this->render("error");
+			return;
+		}
+
+		$info = @libvirt_storagevolume_get_info($ret);
+		$info["name"] = $name;
+		$info["path"] = @libvirt_storagevolume_get_path($ret);
+		$this->set("content",$name." was successfully created");
+		$this->set("volumeinfo",$info);
+	}
+}
+
+?>