Skip to content

Conversation

@iliaross
Copy link
Collaborator

Hello Jamie,

As described in this comment, a user suggests that the changes made are tested and working correctly.

I have reviewed the changes and fixed a large number of broken indents, along with some logic that, from my perspective, was incorrect.

I will add comments along the way, inviting the original author, @karmantyu, to comment on them.

# Iterate over disk devices
foreach my $dev (glob("/dev/ada[0-9]"), glob("/dev/ada[0-9][0-9]"),
glob("/dev/ad[0-9]"), glob("/dev/ad[0-9][0-9]"),
glob("/dev/nvd[0-9]"), glob("/dev/nvd[0-9][0-9]"),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@karmantyu, I have modified it to nvd[0-9][0-9] for all three regexes because nvd[0-9]* is greedy and would even match nvd alone. Will this work for your case? If you are unsure, could you provide the list of devices?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

99 disk is plenty enough

# Get size and slices
my $out;
my $sdev = 's';
if (&has_command('gpart')) {
Copy link
Collaborator Author

@iliaross iliaross Feb 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@karmantyu, I changed this part because we want to support older systems that still use fdisk.

Copy link

@karmantyu karmantyu Feb 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've got the suggestion to implement:

# Add at the beginning of each CGI script
sub get_disk_manager {
    my $has_gpart = &has_command("gpart");
    return $has_gpart ? "gpart" : "fdisk";
}

# Example implementation for disk operations
sub handle_disk_operation {
    my ($disk, $operation) = @_;
    my $manager = &get_disk_manager();
    
    if ($manager eq "gpart") {
        # Modern gpart commands
        return &execute_gpart_command($disk, $operation);
    } else {
        # Legacy fdisk commands
        return &execute_fdisk_command($disk, $operation);
    }
}

$slice = { 'number' => $1,
'device' => $dev."$sdev".$1,
'index' => scalar(@{$disk->{'slices'}}) };
if ($slice->{'device'} =~ /^\/dev\/([a-z]+)(\d+)[ps](\d+)/) {
Copy link
Collaborator Author

@iliaross iliaross Feb 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@karmantyu And, the same here, the regex will match both, old and new format.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A possible example for backward compat.
fdisk_compat_example.zip

use WebminCore;
&init_config();
&foreign_require("mount");
&foreign_require("fdisk");
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@karmantyu, I reverted back this change to use fdisk module, because using mount::device_status seems incorrect, simply because device_status sub isn't available in mount module.

# Validate inputs, starting with slice number
my $part = { };
$in{'letter'} =~ /^[a-d]$/i || &error($text{'npart_eletter'});
$in{'letter'} =~ /^[a-z]$/i || &error($text{'npart_eletter'});
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@karmantyu, can you explain this change?

# Slice type
$part->{'type'} = $in{'type'};
# Set partition properties
$part->{'label'} = $in{'label'} if ($in{'label'} =~ /^[a-zA-Z0-9._-]+$/);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@karmantyu, I have also changed the regex from /^[a-zA-Z0-9._-]*$/ to /^[a-zA-Z0-9._-]+$/ because we expect the label not to be empty.


# Slice type
$slice->{'type'} = $in{'type'};
$slice->{'label'} = $in{'label'} if ($in{'label'} =~ /^[a-zA-Z0-9._-]+$/);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@karmantyu, and the same here.

if ($disk->{'model'}) {
push(@info, &text('disk_model', $disk->{'model'}));
}
push(@info, &text('disk_cylinders', $disk->{'cylinders'}));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@karmantyu, why deleting this line? Please remember we need to provide support for older versions!

&ui_select("type", $part->{'type'},
[ &list_partition_types() ], 1, 0, 1));
print &ui_table_row($text{'part_label'},
&ui_textbox("label", $part->{'label'}, 20));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@karmantyu, remember to maintain indent correctly! We use tabs with tab display size set to 8.

[ sort { $a->[1] cmp $b->[1] }
map { [ $_, &fdisk::tag_name($_) ] }
&fdisk::list_tags() ]));
[ &list_partition_types() ]));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@karmantyu, we need to main backward compatibility with older versions of BSD that don't have gpart available.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I folder than FreeBSD 10.0. needed of course.

$slice->{'active'} ? $text{'yes'} :
&ui_yesno_radio("active", $slice->{'active'}));
# Label field
print &ui_table_row($text{'slice_label'},
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@karmantyu, where is the updated lang/en file? I couldn't find it in the initial archive, and all these new language strings are missing.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry missed that. Maybe tomorrow.

Copy link

@karmantyu karmantyu Feb 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

en.txt

I've let Sourcegraph cross-reference the language file with the changes. Need further testing.

# Partition type selection
print &ui_table_row($text{'npart_type'},
&ui_select("type", '4.2BSD',
&ui_select("type", 'freebsd-ufs',
Copy link
Collaborator Author

@iliaross iliaross Feb 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@karmantyu, it has to be backward compatible with older versions of BSD.

# Start and end blocks (defaults to last slice+1)
my ($start, $end) = (63, $disk->{'blocks'});
foreach my $s (sort { $a->{'startblock'} cmp $b->{'startblock'} }
my ($start, $end) = (2048, $disk->{'blocks'});
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@karmantyu, can you explain this change? Again, it has to be compatible with older versions.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using newer disk drives, GPT is recommended which uses 2048 as start sector by default. For partitioning newer "Advanced Format 4K" disks.

my ($start, $end) = (63, $disk->{'blocks'});
foreach my $s (sort { $a->{'startblock'} cmp $b->{'startblock'} }
my ($start, $end) = (2048, $disk->{'blocks'});
foreach my $s (sort { $a->{'startblock'} <=> $b->{'startblock'} }
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@karmantyu, and why this change?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 MiB (2048 sector) alignment was introduced to Linux fdisk in util-linux-ng-2.17.1/fdisk/fdisk.c, function update_sector_offset(void), released on 2010-02-22. Windows Vista was released in 2006-11.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And more: In other words, the LBA sector number 63 corresponds to cylinder 0, head 1, sector 1 in the CHS format, which is the first sector you can use in the MBR format. However, the number 63 is not divisible by 8, which causes a problem with 4K drives, so some modern tools starts the first partition at 2048 which also provides future GPT compatibility.

@karmantyu
Copy link

Appreciate Your hard work. Sorry for the mistakes.

@karmantyu
Copy link

working_example.zip

New example bsdfdiksk-lib.pl now checking if the BSD is newer than ver. 10 and using gpart in case of new systems. Showing the partitions and slices nicely on FreeBSD 14.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants