format_disk.php 5.43 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
<?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;
28 29 30 31 32 33 34 35 36 37
	if (isset($_GET["noraw"]) || isset($_GET["all"]))
	    $noraw = ' -a ';
	else
	    $noraw = ' ';
    if (isset($_GET["reformat"]))
        $reformat = ' -r ';
    else
        $reformat = ' ';
	            
        exec($parted_script . " --list". $noraw . $reformat, $output, $ret_val);
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
	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)
{
58 59
	global $parted_script;
	
60 61 62 63 64 65 66 67
	$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) {
68 69
		exec($parted_script . " --partitions", $output, $ret);
		if ($ret == 0) {
70
			$msg = "Disk is already partitioned: <ul>";
71 72 73
			foreach ($output as $line) {
				$plist = explode(':', $line);
				foreach ($plist as $p) {
74
					$msg = $msg . "<li>" . $p . "</li>";
75 76
				}
			}
77
			$msg = $msg . "</ul>";
78 79 80 81 82 83 84
			$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);
		}
85 86 87 88 89 90 91 92 93 94 95 96 97
	} else {
		table_row_err($disks["disks"][0]);
	}
}

function btn_class($inactive)
{
	$class = "btn btn-danger";
	if ($inactive)
		echo $class . " disabled";
	else
		echo $class;
}
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
function isForce()
{
    if (isset($_GET["force"]))
        echo ' checked ';
    else
        echo ' ';
}
function isNoRaw()
{
    if (isset($_GET["noraw"]) || isset($_GET["all"]))
        echo ' checked ';
    else
        echo ' ';
}
function isReformat()
{
    if (isset($_GET["reformat"]))
        echo ' checked ';
        else
            echo ' ';
}
119 120 121 122 123 124 125 126 127 128 129 130

/* 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 = ' ';
131 132 133 134 135 136 137 138 139 140
		if (isset($_GET["noraw"]) || isset($_GET["all"]))
		    $noraw = ' -a ';
	    else
	        $noraw = ' ';
        if (isset($_GET["reformat"]))
            $reformat = ' -r ';
        else
            $reformat = ' ';
        $disk_path = $_GET["disk_path"];
        exec($parted_script . $force . $noraw . $reformat .$disk_path, $output, $ret_val);
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
		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;">
187 188 189 190 191
		<span class="checkbox"><label><input id="chk_force"    type="checkbox" <?php isForce(); ?>>Force 'mkfs' to create a file system</label></span>
		<span class="checkbox"><label><input id="chk_noraw"    type="checkbox" <?php isNoRaw(); ?>>Create a system partition only, no raw</label></span>
		<span class="checkbox"><label><input id="chk_reformat" type="checkbox" <?php isReformat(); ?>>Reformat disk (delete existing partitions)</label></span>
        <button id="btn_format" type="button" class="<?php btn_class($no_disk); ?>"><b>Format</b></button>		
<!-- 	<button id="btn_format" type="button" class="<?php btn_class(0); ?>"><b>Format</b></button>  -->
192 193 194 195
	</div>
</body>

</html>