#!/opt/bin/perl

my @devlist;

# find currently mounted devices from df -k and metastat.
open(DF,"/bin/df -k|") || die "Unable to run /bin/df -k: $!\n";
while(<DF>) {
    my ($dev,$size,$used,$avail,$capacity,$mounted)=split /\s+/;
    next if ($dev =~ /^(swap|\/proc|fd|Filesystem)/);
    
    my ($metadev)=($dev=~m:/dev/md/dsk/(d\d+):);
    if ($metadev) {
	# solstice disksuite metadevice
	print "* Getting metadevice config for $metadev\n";
	open(MD,"/usr/opt/SUNWmd/sbin/metastat $metadev|") || warn "Unable to run metastat: $!\n";
	while (<MD>) {
	    ($sd)=/(c\dt\dd\ds\d)/;
	    push @devlist,$sd if $sd;
	}
	close(MD);
    } else {
	($sd)=($dev=~/(c\dt\dd\ds\d)/);
	push @devlist,$sd if $sd;
    }
}
close(DF);

# get mapping
my %path_to_inst;
open(PI,"</etc/path_to_inst") ||
    die "Can't open /etc/path_to_inst: $!\n";
while (<PI>) {
    ($path,$inst)=/\"(.*)\" (\d+)/;
    $path_to_inst{$path}=$inst;
}
close(PI);

# connect em.
foreach (@devlist) {
    my $lp=readlink("/dev/dsk/$_");
    $lp=~s@^.*/devices@@g; $lp=~s@:.$@@g;
    $device{"sd$path_to_inst{$lp}"}=$_;
}

# print the results
if (@ARGV) {
    foreach (@ARGV) {
	print "$_ => $device{$_}\n";
    }
} else {
    foreach (sort keys %device) {
	next unless /sd/;
	print "$_ => $device{$_}\n";
    }
}

