#!/usr/bin/perl -w
# 
# Script to compare the de-normalized /etc/mtab files with
# the live kernel view of what's mounted (/proc/mount).
# Will report any entries in /etc/mtab which are missing or
# redundant. Any redundant entries must be removed, otherwise
# automounter will refuse to mount them again and log a message:
#
# automount[4349]: BUG: /sbcimp/shared already mounted

my %mtab;
my %mounts;

open MOUNTS, "/proc/mounts"
    or die "cannot read /proc/mounts: $!";

while (<MOUNTS>) {
    next if /^#/;
    next if /^\s*$/;

    chomp;

    my ($dev, $path, $type, $opts, $c1, $c2) =
	split / /;
    
    $mounts{$path} = { dev => $dev,
		       type => $type,
		       opts => $opts,
		       c1 => $c1,
		       c2 => $c2 };
}

close MOUNTS;

open MTAB, "/etc/mtab"
    or die "cannot read /etc/mtab: $!";

while (<MTAB>) {
    next if /^#/;
    next if /^\s*$/;

    chomp;

    my ($dev, $path, $type, $opts, $c1, $c2) =
	split / /;

    $mtab{$path} = { dev => $dev,
		     type => $type,
		     opts => $opts,
		     c1 => $c1,
		     c2 => $c2 };
}

close MTAB;

foreach my $path (keys %mtab) {
    if (!exists $mounts{$path}) {
	warn "  Should not be in /etc/mtab: $path\n";
    }
}

foreach my $path (keys %mounts) {
    if (!exists $mtab{$path}) {
	warn "  Missing in /etc/mtab: $path\n";
    }
}
