Merge pull request #1697 from webmin/dev/fdisk-improvements

Add `fdisk` GPT improvements
This commit is contained in:
Jamie Cameron
2022-07-24 13:04:42 -07:00
committed by GitHub
5 changed files with 106 additions and 64 deletions

View File

@@ -4,7 +4,6 @@
require './fdisk-lib.pl';
&ReadParse();
&can_edit_disk($in{'device'}) || &error($text{'disk_ecannot'});
$extwidth = 300;
# Get the disk
@disks = &list_disks_partitions();
@@ -13,6 +12,10 @@ $d || &error($text{'disk_egone'});
@parts = @{$d->{'parts'}};
&ui_print_header($d->{'desc'}, $text{'disk_title'}, "", undef,
@disks == 1 ? 1 : 0, @disks == 1 ? 1 : 0);
$caneditpart =
$d->{'table'} ne 'gpt' ||
($d->{'table'} eq 'gpt' &&
&has_command('parted') && $config{'mode'} ne 'fdisk');
# Work out links to add partitions
foreach $p (@parts) {
@@ -29,17 +32,22 @@ foreach $p (@parts) {
$anyfree++;
}
}
if ($regular < 4 || $disk->{'table'} ne 'msdos') {
push(@edlinks, "<a href=\"edit_part.cgi?disk=$d->{'index'}&new=1\">".
$text{'index_addpri'}."</a>");
if ($caneditpart) {
if ($regular < 4 || $disk->{'table'} ne 'msdos') {
push(@edlinks, "<a href=\"edit_part.cgi?disk=$d->{'index'}&new=1\">".
$text{'index_addpri'}."</a>");
}
if ($extended) {
push(@edlinks, "<a href=\"edit_part.cgi?disk=$d->{'index'}&new=2\">".
$text{'index_addlog'}."</a>");
}
elsif ($regular < 4 && &supports_extended()) {
push(@edlinks, "<a href=\"edit_part.cgi?disk=$d->{'index'}&new=3\">".
$text{'index_addext'}."</a>");
}
}
if ($extended) {
push(@edlinks, "<a href=\"edit_part.cgi?disk=$d->{'index'}&new=2\">".
$text{'index_addlog'}."</a>");
}
elsif ($regular < 4 && &supports_extended()) {
push(@edlinks, "<a href=\"edit_part.cgi?disk=$d->{'index'}&new=3\">".
$text{'index_addext'}."</a>");
else {
$wantsparted = 1;
}
if ($d->{'table'} eq 'unknown') {
# Must create a partition table first
@@ -65,6 +73,12 @@ if ($d->{'table'}) {
}
print &ui_links_row(\@info),"<p>\n";
if ($wantsparted) {
my $label = $config{'mode'} eq 'fdisk' ?
'edit_edisk2' : 'edit_edisk';
print "<p>$text{$label}</p>\n";
}
# Show table of partitions, if any
if (@parts) {
print &ui_links_row(\@edlinks);
@@ -84,16 +98,12 @@ if (@parts) {
# Create extent images
$ext = "";
$ext .= sprintf "<img src=images/gap.gif height=10 width=%d>",
$extwidth*($p->{'start'} - 1) /
$d->{'cylinders'};
$ext .= sprintf "<img src=images/%s.gif height=10 width=%d>",
$p->{'extended'} ? "ext" : "use",
$extwidth*($p->{'end'} - $p->{'start'}) /
$d->{'cylinders'};
$ext .= sprintf "<img src=images/gap.gif height=10 width=%d>",
$extwidth*($d->{'cylinders'} - ($p->{'end'} - 1)) /
$d->{'cylinders'};
$ext1 = int((($p->{'start'} - 1) / $d->{'cylinders'}) * 100) . "%";
$ext2 = int((($p->{'end'} - $p->{'start'}) / $d->{'cylinders'}) * 100) . "%";
$ext3 = int((($d->{'cylinders'} - ($p->{'end'} - 1)) / $d->{'cylinders'}) * 100) . "%";
$ext .= "<img src=images/gap.gif height=10 width='$ext1'>";
$ext .= sprintf "<img src=images/%s.gif height=10 width='$ext2'>", $p->{'extended'} ? "ext" : "use";
$ext .= "<img src=images/gap.gif height=10 width='$ext3'>";
# Work out usage
@stat = &device_status($p->{'device'});
@@ -116,38 +126,43 @@ if (@parts) {
print &ui_columns_end();
}
else {
print "<b>$text{'disk_none'}</b><p>\n";
print "<p>$text{'disk_none'}</p>\n"
if (!$wantsparted);
}
print &ui_links_row(\@edlinks);
# Buttons for IDE params and SMART
print &ui_hr();
print &ui_buttons_start();
my $ui_buttons_content;
if (&supports_hdparm($d)) {
print &ui_buttons_row("edit_hdparm.cgi", $text{'index_hdparm'},
$ui_buttons_content = &ui_buttons_row("edit_hdparm.cgi", $text{'index_hdparm'},
$text{'index_hdparmdesc'},
&ui_hidden("disk", $d->{'index'}));
}
if (&supports_smart($d)) {
print &ui_buttons_row("../smart-status/index.cgi", $text{'index_smart'},
$ui_buttons_content = &ui_buttons_row("../smart-status/index.cgi", $text{'index_smart'},
$text{'index_smartdesc'},
&ui_hidden("drive", $d->{'device'}));
}
if (&supports_relabel($d)) {
if ($d->{'table'} eq 'unknown') {
print &ui_buttons_row(
$ui_buttons_content = &ui_buttons_row(
"edit_relabel.cgi", $text{'index_relabel2'},
$text{'index_relabeldesc2'},
&ui_hidden("device", $d->{'device'}));
}
else {
print &ui_buttons_row(
$ui_buttons_content = &ui_buttons_row(
"edit_relabel.cgi", $text{'index_relabel'},
$text{'index_relabeldesc'},
&ui_hidden("device", $d->{'device'}));
}
}
print &ui_buttons_end();
if ($ui_buttons_content) {
print &ui_hr();
print &ui_buttons_start();
print $ui_buttons_content;
print &ui_buttons_end();
}
&ui_print_footer("", $text{'index_return'});

View File

@@ -111,19 +111,24 @@ $dev = $dinfo->{'prefix'} =~ /^\/dev\/mmcblk.*/ ?
$dinfo->{'prefix'}.$np;
print &ui_table_row($text{'edit_device'}, $dev);
if (!$in{'new'}) {
@stat = &device_status($dev);
$mounted = $stat[2];
}
# Partition type
if ($pinfo->{'extended'} || $in{'new'} == 3) {
# Extended, cannot change
print &ui_table_row($text{'edit_type'}, $text{'extended'});
}
elsif ($pinfo->{'edittype'} || $in{'new'}) {
elsif (($pinfo->{'edittype'} == 1 || $in{'new'}) && !$mounted) {
# Can change
print &ui_table_row($text{'edit_type'},
&ui_select("type",
$in{'new'} ? &default_tag() : $pinfo->{'type'},
[ map { [ $_, &tag_name($_) ] }
(sort { &tag_name($a) cmp &tag_name($b) }
&list_tags()) ]));
&list_tags($pinfo->{'dtable'})) ]));
}
else {
# Tool doesn't allow change
@@ -160,9 +165,8 @@ if ($pinfo->{'extended'}) {
}
}
elsif (!$in{'new'}) {
@stat = &device_status($dev);
if (@stat) {
$msg = $stat[2] ? 'edit_mount' : 'edit_umount';
$msg = $mounted ? 'edit_mount' : 'edit_umount';
$msg .= 'vm' if ($stat[1] eq 'swap');
$msg .= 'raid' if ($stat[1] eq 'raid');
$msg .= 'lvm' if ($stat[1] eq 'lvm');
@@ -199,7 +203,7 @@ if (($has_e2label || $has_xfs_db) && &supports_label($pinfo) && !$in{'new'}) {
}
# Show field for partition name
if (&supports_name($dinfo)) {
if (&supports_name($dinfo) && !$mounted && $pinfo->{'edittype'} != 2) {
print &ui_table_row($text{'edit_name'},
&ui_textbox("name", $pinfo->{'name'}, 20));
}
@@ -214,24 +218,24 @@ print &ui_table_end();
if ($in{'new'}) {
print &ui_form_end([ [ undef, $text{'create'} ] ]);
}
elsif (@stat && $stat[2]) {
elsif (@stat && $stat[2] &&
$pinfo->{'edittype'} != 2) {
print &ui_form_end();
print "<b>$text{'edit_inuse'}</b><p>\n";
print "$text{'edit_inuse'}\n";
}
else {
elsif ($pinfo->{'edittype'} != 2) {
print &ui_form_end([ $pinfo->{'extended'} ? ( ) :
( [ undef, $text{'save'} ] ),
[ 'delete', $text{'delete'} ] ]);
}
if (!$in{'new'} && !$pinfo->{'extended'}) {
print &ui_hr();
print &ui_buttons_start();
if (!$in{'new'} && !$pinfo->{'extended'} && $pinfo->{'edittype'} != 2) {
my $ui_buttons_content;
if (!@stat || $stat[2] == 0) {
# Show form for creating filesystem
local $rt = @stat ? $stat[1] : &conv_type($pinfo->{'type'});
print &ui_buttons_row("mkfs_form.cgi",
$ui_buttons_content = &ui_buttons_row("mkfs_form.cgi",
$text{'edit_mkfs2'}, $text{'edit_mkfsmsg2'},
&ui_hidden("dev", $dev),
&ui_select("type", $rt,
@@ -241,7 +245,7 @@ if (!$in{'new'} && !$pinfo->{'extended'}) {
if (!$in{'new'} && @stat && $stat[2] == 0 && &can_fsck($stat[1])) {
# Show form to fsck filesystem
print &ui_buttons_row("fsck_form.cgi",
$ui_buttons_content = &ui_buttons_row("fsck_form.cgi",
$text{'edit_fsck'},&text('edit_fsckmsg', "<tt>fsck</tt>"),
&ui_hidden("dev", $dev)." ".
&ui_hidden("type", $stat[1]));
@@ -249,7 +253,7 @@ if (!$in{'new'} && !$pinfo->{'extended'}) {
if (!$in{'new'} && @stat && $stat[2] == 0 && &can_tune($stat[1])) {
# Show form to tune filesystem
print &ui_buttons_row("tunefs_form.cgi",
$ui_buttons_content = &ui_buttons_row("tunefs_form.cgi",
$text{'edit_tune'}, $text{'edit_tunemsg'},
&ui_hidden("dev", $dev)." ".
&ui_hidden("type", $stat[1]));
@@ -260,7 +264,7 @@ if (!$in{'new'} && !$pinfo->{'extended'}) {
# Show form to mount filesystem
if ($types[0] eq "swap") {
# Swap partition
print &ui_buttons_row("../mount/edit_mount.cgi",
$ui_buttons_content = &ui_buttons_row("../mount/edit_mount.cgi",
$text{'edit_newmount2'},$text{'edit_mountmsg2'},
&ui_hidden("type", $types[0]).
&ui_hidden("newdev", $dev));
@@ -275,14 +279,24 @@ if (!$in{'new'} && !$pinfo->{'extended'}) {
else {
$dirsel .= &ui_hidden("type", $types[0]);
}
print &ui_buttons_row("../mount/edit_mount.cgi",
$ui_buttons_content = &ui_buttons_row("../mount/edit_mount.cgi",
$text{'edit_newmount'}, $text{'edit_mountmsg'},
&ui_hidden("newdev", $dev),
$dirsel);
}
}
print &ui_buttons_end();
if ($ui_buttons_content) {
print &ui_hr();
print &ui_buttons_start();
print $ui_buttons_content;
print &ui_buttons_end();
}
} elsif (!$mounted &&
$pinfo->{'edittype'} == 2) {
my $label = $config{'mode'} eq 'fdisk' ?
'edit_eparted2' : 'edit_eparted';
print "$text{$label}\n";
}
&ui_print_footer("edit_disk.cgi?device=$dinfo->{'device'}",

View File

@@ -245,8 +245,7 @@ local $devs = join(" ", @devs);
local ($disk, $m2);
if ($has_parted) {
open(FDISK, join(" ; ",
map { "parted $_ unit cyl print 2>/dev/null || ".
"fdisk -l $_ 2>/dev/null" } @devs)." |");
map { "parted $_ unit cyl print 2>/dev/null" } @devs)." |");
}
else {
open(FDISK, "fdisk -l -u=cylinders $devs 2>/dev/null || fdisk -l $devs 2>/dev/null |");
@@ -506,16 +505,19 @@ while(<FDISK>) {
$disk->{'cylsize'};
push(@{$disk->{'parts'}}, $part);
}
elsif (/(\/dev\/\S+?(\d+))\s+(\d+)\s+(\d+)\s+(\d+)\s+([0-9\.]+[kMGTP])\s+(\S.*)/) {
elsif (/(?<dev>\/dev\/\S+?(?<num>\d+))\s+(?<start>\d+)\s+(?<end>\d+)\s+(?<blocks>\d+)\s+(?<size>[0-9\.]+[kMGTP])\s+(?<type>\S.*)/ ||
/(?<dev>\/dev\/\S+?(?<num>\d+))\s+(?<start>\d+)\s+(?<end>\d+)\s+(?<size>[0-9\.]+[kMGTP])\s+(?<type>\S.*)/) {
# Partition within the current disk from fdisk (gpt format)
local $part = { 'number' => $2,
'device' => $1,
'type' => $7,
'start' => $3,
'end' => $4,
'blocks' => $5,
local $part = {
'number' => "$+{num}",
'device' => "$+{dev}",
'type' => "$+{type}",
'start' => "$+{start}",
'end' => "$+{end}",
'blocks' => "$+{blocks}",
'index' => scalar(@{$disk->{'parts'}}),
'edittype' => 1, };
'dtable' => $disk->{'table'},
'edittype' => 2, };
$part->{'desc'} = &partition_description($part->{'device'});
$part->{'size'} = ($part->{'end'} - $part->{'start'} + 1) *
$disk->{'cylsize'};
@@ -861,7 +863,12 @@ undef(@list_disks_partitions_cache);
# Returns a list of known partition tag numbers
sub list_tags
{
if ($has_parted) {
my ($fdisk_gpt) = @_;
if ($fdisk_gpt eq 'gpt') {
# fdisk gpt types
return sort { $a cmp $b } (keys %fdisk_gpt_tags);
}
elsif ($has_parted) {
# Parted types
return sort { $a cmp $b } (keys %parted_tags);
}
@@ -875,7 +882,7 @@ else {
# Returns a human-readable version of a tag
sub tag_name
{
return $tags{$_[0]} || $parted_tags{$_[0]} || $hidden_tags{$_[0]};
return $tags{$_[0]} || $parted_tags{$_[0]} || $fdisk_gpt_tags{$_[0]} || $hidden_tags{$_[0]};
}
sub default_tag
@@ -1563,6 +1570,14 @@ else { return " $_[2] $in{$_[0]}"; }
'f', 'Windows extended LBA',
);
%fdisk_gpt_tags = (
'', 'None',
'EFI System', 'EFI system partition',
'BIOS boot', 'BIOS boot partition',
'Linux filesystem', 'Linux filesystem',
'Linux LVM', 'Linux LVM',
);
%parted_tags = (
'', 'None',
'fat16', 'Windows FAT16',

View File

@@ -9,12 +9,6 @@ require './fdisk-lib.pl';
# Work out which disks are accessible
@disks = &list_disks_partitions();
@disks = grep { $access{'view'} || &can_edit_disk($_->{'device'}) } @disks;
if (@disks == 1 && &can_edit_disk($disks[0]->{'device'})) {
# Just one .. go direct to it's page
&redirect("edit_disk.cgi?device=$disks[0]->{'device'}");
edit;
}
$pdesc = $has_parted ? $text{'index_parted'} : $text{'index_fdisk'};
&ui_print_header($pdesc, $module_info{'desc'}, "", undef, 1, 1, 0,
&help_search_link("fdisk", "man", "doc", "howto"));

View File

@@ -98,6 +98,10 @@ edit_name=Partition name
edit_volid=Volume ID
edit_blocks=$1 blocks
edit_inuse=This partition cannot be changed as it is currently in use or configured for use.
edit_eparted=This partition cannot be changed unless <tt>parted</tt> command is installed.
edit_eparted2=This partition cannot be changed unless <tt>parted</tt> is set as management command in module config.
edit_edisk=This disk cannot be edited unless <tt>parted</tt> command is installed.
edit_edisk2=This disk cannot be edited unless <tt>parted</tt> is set as management command in module config.
edit_mkfs=Create New Filesystem
edit_mkfs2=Create Filesystem:
edit_mkfsmsg=Builds a new $1 filesystem on this partition, permanently erasing any existing files. You must do this after creating a new partition or changing an existing one.