use strict;
use Getopt::Long;

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

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

sub usage {
    my $verbosity = shift;
    require Pod::Usage;
        -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 (system("mount", '-o', 'noatime', $dev, $mnt)) {
        warn "Failed to mount $dev at $mnt.\n";
    } else {
        warn "Mounted device $devid at $mnt.\n" if $verbose;

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


=head1 NAME

mogautomount - automatically discover and mount MogileFS disks


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


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


=item --help | -h

this help

=item --verbose | -verbose

be verbose



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

1 on partial failure (some mounts succeeed).

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

=head1 AUTHOR

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


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.


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