Commit 2997e18c authored by Mikhail Karpenko's avatar Mikhail Karpenko
Browse files

Add raw device detection

parent 1a586b78
Loading
Loading
Loading
Loading
+63 −1
Original line number Diff line number Diff line
@@ -400,7 +400,12 @@ else
				}
				echo "</item>";
			}

			break;
		case "list_raw_devices":
			$devices = get_raw_dev();
			foreach ($devices as $device) {
				echo "<raw_device>" . $device . "</raw_device>";
			}
			break;
		case "mkdir":
			$dir_name = $_GET['name'];
@@ -601,6 +606,63 @@ function xml_footer() {
	echo "</camogm_interface>\n";
}

/** Get a list of disk devices which have file system and can be mounted. This function
 *  uses 'blkid' command from busybox.
*/ 
function get_mnt_dev()
{
	exec("blkid", $ids);
	$i = 0;
	foreach ($ids as $id) {
		$devices[$i] = preg_replace('/: +.*/', "", $id);
		if (preg_match('/(?<=TYPE=")[a-z0-9]+(?=")/', $id, $fs) == 1)
			$fs_types[$i] = $fs[0];
		else
			$fs_types[$i] = "none";
		$i++;
	}
	return array("devices" => $devices, "types" => $fs_types);
}

/** Get a list of devices whithout file system which can be used for raw disk storage from camogm. */
function get_raw_dev()
{
	$j = 0;
	$regexp = '/sd[a-z0-9]+$/';
	$names = array();
	$ret = get_mnt_dev();
	$devices = $ret["devices"]; 
	$types = $ret["types"];
	exec("cat /proc/partitions", $partitions);

	// get a list of all suitable partitions	
	// the first two elements of an array are table header and empty line delimiter, skip them
	for ($i = 2; $i < count($partitions); $i++) {
		// select SATA devices only
		if (preg_match($regexp, $partitions[$i], $name) == 1) {
			$names[$j] = $name[0];
			$j++;
		}
	}
	
	// filter out partitions with file system 
	$i = 0;
	$raw_devices = array();
	foreach ($names as $name) {
		$found = false;
		foreach ($devices as $device) {
			if (strpos($device, $name) !== false)
				$found = true;
		}
		if ($found === false) {
			// current partition is not found in the blkid list, add it to raw devices
			$raw_devices[$i] = "/dev/" . $name;
			$i++;
		}
	}
	
	return $raw_devices;
}
?>