Basic FreeBSD disk list

This commit is contained in:
Jamie Cameron
2013-03-19 17:53:37 -07:00
parent d335f36a8a
commit fbd44b6c70
6 changed files with 162 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

View File

@@ -11,4 +11,119 @@ use WebminCore;
&init_config();
&foreign_require("mount");
sub check_fdisk
{
if (!&has_command("fdisk")) {
return &text('index_ecmd', "<tt>fdisk</tt>");
}
return undef;
}
# list_disks_partitions()
# Returns a list of all disks, partitions and slices
sub list_disks_partitions
{
my @rv;
# Iterate over disk devices
foreach my $dev (glob("/dev/ada[0-9]"),
glob("/dev/ad[0-9]"),
glob("/dev/da[0-9]")) {
next if (!-r $dev || -l $dev);
my $disk = { 'device' => $dev,
'prefix' => $dev,
'parts' => [ ] };
if ($dev =~ /^\/dev\/(.*)/) {
$disk->{'short'} = $1;
}
push(@rv, $disk);
# Get size and partitions
my $out = &backquote_command("fdisk $dev");
my @lines = split(/\r?\n/, $out);
my $part;
for(my $i=0; $i<@lines; $i++) {
if ($lines[$i] =~ /cylinders=(\d+)\s+heads=(\d+)\s+sectors\/tracks=(\d+)\s+\((\d+)/) {
# Disk information
# XXX model and size?
$disk->{'cylinders'} = $1;
$disk->{'heads'} = $2;
$disk->{'sectors'} = $3;
$disk->{'blksper'} = $4;
$disk->{'size'} = $disk->{'cylinders'} *
$disk->{'blksper'} * 512;
$disk->{'index'} = scalar(@rv);
}
elsif ($lines[$i] =~ /data\s+for\s+partition\s+(\d+)/ &&
$lines[$i+1] !~ /<UNUSED>/) {
# Start of a partition
$part = { 'number' => $2,
'device' => $dev."p".$2,
'index' => scalar(@{$disk->{'parts'}}) };
push(@{$disk->{'parts'}}, $part);
}
elsif ($lines[$i] =~ /sysid\s+(\d+)/ && $part) {
# Partition type
$part->{'type'} = $2;
}
elsif ($lines[$i] =~ /start\s+(\d+),\s+size\s+(\d+)\s+\((.*)\)/ && $part) {
# Partition start and size
$part->{'blocks'} = $2;
$part->{'size'} = &string_to_size("$3");
}
elsif ($lines[$i] =~ /beg:\s+cyl\s+(\d+)/ && $part) {
# Partition start
$part->{'start'} = $1;
}
elsif ($lines[$i] =~ /end:\s+cyl\s+(\d+)/ && $part) {
# Partition end
$part->{'end'} = $1;
}
}
# Get disk model from dmesg
open(DMESG, "/var/run/dmesg.boot");
while(<DMESG>) {
if (/^(\S+):\s+<(.*)>/ && $1 eq $disk->{'short'}) {
$disk->{'model'} = $2;
}
elsif (/^(\S+):\s+(\d+)(\S+)\s+\((\d+)\s+(\d+)\s+byte\s+sectors/ &&
$1 eq $disk->{'short'}) {
$disk->{'sectorsize'} = $5;
$disk->{'size'} = &string_to_size("$2 $3");
}
}
close(DMESG);
# Get slices within partitions
# XXX
}
return @rv;
}
# string_to_size(str)
# Convert a string like 100 Meg to a number in bytes
sub string_to_size
{
my ($str) = @_;
my ($n, $pfx) = split(/\s+/, $str);
if ($pfx =~ /^b/i) {
return $n;
}
if ($pfx =~ /^k/i) {
return $n * 1024;
}
if ($pfx =~ /^m/i) {
return $n * 1024 * 1024;
}
if ($pfx =~ /^g/i) {
return $n * 1024 * 1024 * 1024;
}
if ($pfx =~ /^t/i) {
return $n * 1024 * 1024 * 1024 * 1024;
}
return undef;
}
1;

38
bsdfdisk/index.cgi Normal file
View File

@@ -0,0 +1,38 @@
#!/usr/local/bin/perl
# Show a list of disks
use strict;
use warnings;
require './bsdfdisk-lib.pl';
our (%in, %text, %config, $module_name);
&ui_print_header(undef, $text{'index_title'}, "", undef, 1, 1, 0);
my $err = &check_fdisk();
if ($err) {
&ui_print_endpage(&text('index_problem', $err));
}
my @disks = &list_disks_partitions();
@disks = sort { $a->{'device'} cmp $b->{'device'} } @disks;
if (@disks) {
print &ui_columns_start([ $text{'index_dname'},
$text{'index_dsize'},
$text{'index_dmodel'},
$text{'index_dparts'} ]);
foreach my $d (@disks) {
print &ui_columns_row([
"<a href='edit_disk.cgi?dev=".&urlize($d->{'device'}).
"'>".&html_escape($d->{'device'})."</a>",
&nice_size($d->{'size'}),
$d->{'model'},
scalar(@{$d->{'parts'}}),
]);
}
print &ui_columns_end();
}
else {
print "<b>$text{'index_none'}</b> <p>\n";
}
&ui_print_footer("/", $text{'index'});

View File

@@ -1 +1,8 @@
index_title=Partitions on Local Disks
index_ecmd=The required command $1 is missing
index_problem=This module cannot be used : $1
index_none=No disks were found on this system!
index_dname=Disk name
index_dsize=Total size
index_dmodel=Make and model
index_dparts=Partitions

1
gray-theme/bsdfdisk Symbolic link
View File

@@ -0,0 +1 @@
../blue-theme/bsdfdisk

View File

@@ -111,6 +111,7 @@ return $name eq "apache" ? "apache22 ap22-mod_.*" :
$name eq "openssh" ? "openssh-portable" :
$name eq "postgresql" ? "postgresql-server" :
$name eq "openldap" ? "openldap-server openldap-client" :
$name eq "samba" ? "samba36 samba36-nmblookup samba36-smbclient" :
$name;
}