The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl

use strict;
use Getopt::Long;

# Rename binary in process list to make init scripts saner
$0 = $_ = $0;

my ($help, $verbose, $chmod_mountpoints);
usage(0) unless GetOptions(
                           'help' => \$help,
                           'verbose' => \$verbose,
                           'chmod-mountpoints' => \$chmod_mountpoints,
                           );
usage(0) if @ARGV;
usage(2) if $help;

sub usage {
    my $verbosity = shift;
    require Pod::Usage;
    Pod::Usage::pod2usage({
        -exitval => 1,
        -verbose => $verbosity,
    });
}

my $base = "/var/mogdata";
my @bdevs = `/sbin/blkid -c /dev/null`;
die "Failed to run /sbin/blkid to get available block devices." if $?;

my %mounted;  # dev -> 1
open (M, "/proc/mounts") or die "Failed to open /proc/mounts for reading: $!\n";
while (<M>) {
    m!^(\S+) /var/mogdata/dev(\d+)! or next;
    my $devid = $2;
    $mounted{$1} = 1;
    if ($verbose) {
        warn "Mogile device $devid, $1, is already mounted.\n";
    }
}

my $bad_count = 0;
my $good_count = 0;

foreach my $bdev (@bdevs) {
    next unless $bdev =~ /^(.+?):.*LABEL="MogileDev(\d+)"/;
    my ($dev, $devid) = ($1, $2);
    unless (-d "$base") { mkdir $base or die "Failed to mkdir $base: $!"; }
    my $mnt = "$base/dev$devid";
    unless (-d $mnt) { mkdir $mnt or die "Failed to mkdir $mnt: $!"; }
    next if $mounted{$dev};

    if ($chmod_mountpoints and ((stat($mnt))[2] & 0777) != 0) {
        warn "Mountpoint on parent filesystem is writable, fixing.\n" if $verbose;
        chmod 0, $mnt
            or die "Unable to set mogile device mountpoint '$mnt' mode to 0 (no access)";
    }

    if (system("mount", '-o', 'noatime', $dev, $mnt)) {
        warn "Failed to mount $dev at $mnt.\n";
        $bad_count++;
    } else {
        warn "Mounted device $devid at $mnt.\n" if $verbose;
        $good_count++;
    }
}

exit 0 if ! $bad_count;
exit 1 if $good_count;
exit 2;

__END__

=head1 NAME

mogautomount - automatically discover and mount MogileFS disks

=head1 SYNOPSIS

  mogautomount [--verbose | -v]
  mogautomount [--help | -h]

=head1 DESCRIPTION

Mounts all unmounted filesystems with labels of form "MogileDev<n>" at
/var/mogdata/dev<n>, creating the needed directories as well.

You can do this at runtime without restarting mogstored, assuming you
can add new block devices at runtime via your SCSI/SATA/etc controller.

=head1 OPTIONS

=over

=item --help | -h

this help

=item --verbose | -verbose

be verbose

=item --chmod-mountpoints

If a mogile device isn't mounted yet, check to make sure the underlying filesystem has the directory set
to be not readable or writable at all (chmod 0). This could help prevent mogstored from accidentally writing
to the underlying filesystem.

=back

=head1 RETURN CODE

0 on success or inaction because no action needed to happen.

1 on partial failure (some mounts succeed).

2 on total failure (things had to be done, but nothing was).

=head1 AUTHOR

Brad Fitzpatrick, E<lt>brad@danga.comE<gt>

=head1 WARRANTY, BUGS, DISCLAIMER

This tool mounts disks, and disks hold data, so naturally you should
be afraid.  Real the source code to see what it does.  This tool comes
with no warranty of any kind.  You're response for its use or misuse.

=head1 COPYRIGHT & LICENSE

This tool is Copyright 2006, Six Apart, Ltd.
You're free to redistribute it under the same terms as perl itself.

=end