format_disk.php 4.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
<?php
/**
 * @file format_disk.php
 * @brief Disk formatting back end for Elphel393 series camera
 * @copyright Copyright (C) 2017 Elphel Inc.
 * @author Mikhail Karpenko <mikhail@elphel.com>
 *
 * @par <b>License</b>:
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

$parted_script = "format_disk.py";

function get_disks_list()
{
	global $parted_script;
	
	exec($parted_script . " --list", $output, $ret_val);
	return array("ret_val" => $ret_val, "disks" => $output);
}

function table_row($index, $disk_path, $disk_size, $sys_size, $status = "")
{
	echo "<tr id='disk_row_" . $index . "'>" .
			"<td id='path_cell_" . $index . "'>" . $disk_path . "</td>" .
			"<td>" . $disk_size . "</td>" .
			"<td>" . $sys_size . "</td>" .
			"<td id='status_cell_". $index . "'>" . $status . "</td>" .
			"</tr>";
}

function table_row_err($msg)
{
	echo "<tr>" . "<td colspan='4'>" . $msg . "</td>" . "</tr>";
}

function table_body($disks)
{
50 51
	global $parted_script;
	
52 53 54 55 56 57 58 59
	$ret_val = $disks["ret_val"];
	$num = count($disks["disks"]);
	if ($ret_val == 0 && $num > 0) {
		for ($i = 0; $i < $num; $i++) {
			$data = explode(":", $disks["disks"][$i]);
			table_row($i, $data[0], $data[1], $data[2]);
		}
	} else if ($ret_val == 0 && $num == 0) {
60 61
		exec($parted_script . " --partitions", $output, $ret);
		if ($ret == 0) {
62
			$msg = "Disk is already partitioned: <ul>";
63 64 65
			foreach ($output as $line) {
				$plist = explode(':', $line);
				foreach ($plist as $p) {
66
					$msg = $msg . "<li>" . $p . "</li>";
67 68
				}
			}
69
			$msg = $msg . "</ul>";
70 71 72 73 74 75 76
			$partition = substr($plist[0], 0, strpos($plist[0], '(') - 1);
			$disk = substr($partition, 0, -1);
			table_row(0, $disk, "", "", $msg);
		} else {
			$msg = "No disks suitable for partitioning";
			table_row_err($msg);
		}
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	} else {
		table_row_err($disks["disks"][0]);
	}
}

function btn_class($inactive)
{
	$class = "btn btn-danger";
	if ($inactive)
		echo $class . " disabled";
	else
		echo $class;
}

/* process commands */
if (isset($_GET["cmd"]))
	$cmd = $_GET["cmd"];
else
	$cmd = "no_command";
if ($cmd == "format") {
	if (isset($_GET["disk_path"])) {
		if (isset($_GET["force"]))
			$force = ' -f ';
		else
			$force = ' ';
		$disk_path = $_GET["disk_path"];
		exec($parted_script . $force . $disk_path, $output, $ret_val);
		if ($ret_val == 0) {
			print("OK");
		} else {
			foreach ($output as $key => $val)
				if ($val == '')
					unset($output[$key]);
			print(implode(', ', $output));
		}
		exit();
	}
} else {
	// just create the page
	$disks_list = get_disks_list();
	if ($disks_list["ret_val"] != 0 || count($disks_list["disks"]) == 0)
		$no_disk = true;
	else
		$no_disk = false;
}
	
?>

<!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8"/>
	<meta name="author" content="Elphel"/>
	<link rel="stylesheet" href="js/bootstrap/css/bootstrap.css">
	<script src="js/jquery-2.2.3.js"></script>
	<script src="js/jquery-ui/jquery-ui.js"></script>
	<script src="js/bootstrap/js/bootstrap.js"></script>
	<script src="format_disk.js"></script>	
</head>

<body onload="init_actions()" style="padding-top:0px;">
	<h2 id="title" style="padding-left:10px;">Format disk</h2>
	<div style="padding-left:10px;">
		<table class="table">
			<thead>
				<tr><th>Disk</th><th>Total size</th><th>System partition</th><th>Status</th></tr>
			</thead>
			<tbody id="disks_list">
				<?php table_body($disks_list);	?>
			</tbody>
		</table>
	</div>
	<div style="padding-left:10px;">
		<span class="checkbox"><label><input id="chk_force" type="checkbox">Forse 'mkfs' to create a file system</label></span>
		<button id="btn_format" type="button" class="<?php btn_class($no_disk); ?>"><b>Format</b></button>
	</div>
</body>

</html>