Commit f09457c0 authored by Andrey Filippov's avatar Andrey Filippov

added options to format_disk - single partition, reformat

parent 2f06ddbb
......@@ -38,9 +38,21 @@ function init_actions() {
force = '&force';
else
force = '';
noraw = '';
if ($("#chk_noraw").is(':checked'))
noraw = '&noraw';
else
noraw = '';
reformat = '';
if ($("#chk_reformat").is(':checked'))
reformat = '&reformat';
else
reformat = '';
$("#status_cell_" + index).text("Formatting");
$.ajax({
url:"format_disk.php?cmd=format&disk_path=" + disk_path + force,
url:"format_disk.php?cmd=format&disk_path=" + disk_path + force + noraw + reformat,
success: function(result) {
if (result == "OK") {
$("#status_cell_" + index).text("Done");
......
......@@ -25,8 +25,16 @@ $parted_script = "format_disk.py";
function get_disks_list()
{
global $parted_script;
if (isset($_GET["noraw"]) || isset($_GET["all"]))
$noraw = ' -a ';
else
$noraw = ' ';
if (isset($_GET["reformat"]))
$reformat = ' -r ';
else
$reformat = ' ';
exec($parted_script . " --list", $output, $ret_val);
exec($parted_script . " --list". $noraw . $reformat, $output, $ret_val);
return array("ret_val" => $ret_val, "disks" => $output);
}
......@@ -87,6 +95,27 @@ function btn_class($inactive)
else
echo $class;
}
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 ' ';
}
/* process commands */
if (isset($_GET["cmd"]))
......@@ -99,8 +128,16 @@ if ($cmd == "format") {
$force = ' -f ';
else
$force = ' ';
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 . $disk_path, $output, $ret_val);
exec($parted_script . $force . $noraw . $reformat .$disk_path, $output, $ret_val);
if ($ret_val == 0) {
print("OK");
} else {
......@@ -147,8 +184,11 @@ if ($cmd == "format") {
</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>
<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> -->
</div>
</body>
......
......@@ -43,6 +43,7 @@ import sys
import stat
import argparse
import subprocess
import time
# use this % of total disk space for system partition
......@@ -56,6 +57,7 @@ class ErrCodes(object):
NO_TOOLS = 4
NO_PERMISSIONS = 5
PART_FAILURE = 6
PART_MOUINTED = 7
def __init__(self, code = OK):
"""
Prepare strings with error code description.
......@@ -67,7 +69,8 @@ class ErrCodes(object):
"The path provided is a partition, not a disk",
"One of the command-line utilities required for this script is not found",
"This scrip requires root permissions",
"Partitioning finished unsuccessfully"]
"Partitioning finished unsuccessfully",
"Partition mounted, umount failed",]
self._err_code = code
def err2str(self):
......@@ -174,19 +177,35 @@ def get_disk_size(dev_path):
Return: disk size in GB
"""
#TODO: test error
label=""
try:
parted_print = subprocess.check_output(['parted', '-m', dev_path, 'unit', 'GB', 'print'])
parted_print = subprocess.check_output(['parted', '-m', dev_path, 'unit', 'GB', 'print']) #, 'mklabel', 'msdos'])
fields = parted_print.split(':')
sz = fields[1]
disk_size = int(sz[:-2])
label = fields[5] #unknown label - not an error just a message
except:
print ("FIXME: (with fresh disk only) add ''mklabel', 'msdos'' in case of 'Error: /dev/sda: unrecognised disk label'")
'''
needs a separate command (before print):
parted -s /dev/sda mklabel msdos
'''
disk_size = 0
return 0
if (label == 'unknown'):
#print("unknown label, running parted -s /dev/sda mklabel msdos!")
try:
parted_print = subprocess.check_output(['parted', '-s', dev_path, 'mklabel', 'msdos'])
except:
print ("Failed 'parted -s ", dev_path, " mklabel msdos")
return 0
try:
parted_print = subprocess.check_output(['parted', '-m', dev_path, 'unit', 'GB', 'print'])
fields = parted_print.split(':')
sz = fields[1]
disk_size = int(sz[:-2])
except:
print ("Failed 'parted -m ", dev_path, " unit GB print'")
return 0
return disk_size
def partition_disk(dev_path, sys_size, disk_size, dry_run = True, force = False):
......@@ -206,6 +225,7 @@ def partition_disk(dev_path, sys_size, disk_size, dry_run = True, force = False)
'mklabel', 'msdos',
'mkpart', 'primary', str(start), str(end)], stderr = subprocess.STDOUT)
# create raw partition
if (sys_size < disk_size):
start = sys_size
end = disk_size
subprocess.check_output(['parted', '-s', dev_path, 'unit', 'GB',
......@@ -251,7 +271,56 @@ if __name__ == "__main__":
parser.add_argument('-d', '--dry_run', action = 'store_true', help = "execute the script but do not actually create partitions")
parser.add_argument('-f', '--force', action = 'store_true', help = "force 'mkfs' to create a file system and re-format existing partitions")
parser.add_argument('-p', '--partitions', action = 'store_true', help = "list partitions and their sizes separated by colon")
parser.add_argument('-a', '--all', action = 'store_true', help = "Format single partition (no raw)")
parser.add_argument('-r', '--reformat', action = 'store_true', help = "Delete existing partitions, reformat (has to be unmounted)")
args = parser.parse_args()
if args.all:
SYS_PARTITION_RATIO = 100
# print ("SYS_PARTITION_RATIO=",SYS_PARTITION_RATIO)
if args.reformat:
args.force = True #
"""
Unmount if needed and delete MBR (delete partitions before trying to re-format)
"""
disk_path = ""
if args.disk_path:
ret_code = ErrCodes()
if os.path.exists(args.disk_path):
mode = os.stat(args.disk_path).st_mode
if stat.S_ISBLK(mode):
disk_path = args.disk_path
else:
# find existing disks, take first
disks = find_disks(True)
if disks:
disk_path = disks[0]
"""
if (disk_path != ""):
print (disk_path)
else:
print ("disk_path empty")
"""
if (disk_path != ""):
with open("/proc/mounts", "r") as file:
content = file.read()
if (content.find(disk_path) > 0): # <0 - not founed
# print (disk_path+" is mounted")
mount_point = content[content.find(disk_path):].split()[1]
# try to unmount
try:
subprocess.check_output(['umount', mount_point])
except:
ret_code = ErrCodes()
ret_code.err_code = ErrCodes.PART_MOUINTED
print(ret_code.err2str())
sys.exit(ret_code.err_code)
disk_name = disk_path[:8]# /dev/sdx
#delete MBR on this device : dd if=/dev/zero of=/dev/sda bs=512 count=1
try:
subprocess.check_output(['dd', 'if=/dev/zero', 'of='+disk_name,'bs=512', 'count=1'])
except:
ret_code = ErrCodes()
if args.list:
disks = find_disks(args.force)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment