| 1 | #!/usr/bin/perl |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use Getopt::Long; |
|---|
| 5 | |
|---|
| 6 | my ($help, $verbose); |
|---|
| 7 | usage(0) unless GetOptions( |
|---|
| 8 | 'help' => \$help, |
|---|
| 9 | 'verbose' => \$verbose, |
|---|
| 10 | ); |
|---|
| 11 | usage(0) if @ARGV; |
|---|
| 12 | usage(2) if $help; |
|---|
| 13 | |
|---|
| 14 | sub usage { |
|---|
| 15 | my $verbosity = shift; |
|---|
| 16 | require Pod::Usage; |
|---|
| 17 | Pod::Usage::pod2usage({ |
|---|
| 18 | -exitval => 1, |
|---|
| 19 | -verbose => $verbosity, |
|---|
| 20 | }); |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | my $base = "/var/mogdata"; |
|---|
| 24 | my @bdevs = `/sbin/blkid -c /dev/null`; |
|---|
| 25 | die "Failed to run /sbin/blkid to get available block devices." if $?; |
|---|
| 26 | |
|---|
| 27 | my %mounted; # dev -> 1 |
|---|
| 28 | open (M, "/proc/mounts") or die "Failed to open /proc/mounts for reading: $!\n"; |
|---|
| 29 | while (<M>) { |
|---|
| 30 | m!^(\S+) /var/mogdata/dev(\d+)! or next; |
|---|
| 31 | my $devid = $2; |
|---|
| 32 | $mounted{$1} = 1; |
|---|
| 33 | if ($verbose) { |
|---|
| 34 | warn "Mogile device $devid, $1, is already mounted.\n"; |
|---|
| 35 | } |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | my $bad_count = 0; |
|---|
| 39 | my $good_count = 0; |
|---|
| 40 | |
|---|
| 41 | foreach my $bdev (@bdevs) { |
|---|
| 42 | next unless $bdev =~ /^(.+?):.*LABEL="MogileDev(\d+)"/; |
|---|
| 43 | my ($dev, $devid) = ($1, $2); |
|---|
| 44 | unless (-d "$base") { mkdir $base or die "Failed to mkdir $base: $!"; } |
|---|
| 45 | my $mnt = "$base/dev$devid"; |
|---|
| 46 | unless (-d $mnt) { mkdir $mnt or die "Failed to mkdir $mnt: $!"; } |
|---|
| 47 | next if $mounted{$dev}; |
|---|
| 48 | |
|---|
| 49 | if (system("mount", '-o', 'noatime', $dev, $mnt)) { |
|---|
| 50 | warn "Failed to mount $dev at $mnt.\n"; |
|---|
| 51 | $bad_count++; |
|---|
| 52 | } else { |
|---|
| 53 | warn "Mounted device $devid at $mnt.\n" if $verbose; |
|---|
| 54 | $good_count++; |
|---|
| 55 | } |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | exit 0 if ! $bad_count; |
|---|
| 59 | exit 1 if $good_count; |
|---|
| 60 | exit 2; |
|---|
| 61 | |
|---|
| 62 | __END__ |
|---|
| 63 | |
|---|
| 64 | =head1 NAME |
|---|
| 65 | |
|---|
| 66 | mogautomount - automatically discover and mount MogileFS disks |
|---|
| 67 | |
|---|
| 68 | =head1 SYNOPSIS |
|---|
| 69 | |
|---|
| 70 | mogautomount [--verbose | -v] |
|---|
| 71 | mogautomount [--help | -h] |
|---|
| 72 | |
|---|
| 73 | =head1 DESCRIPTION |
|---|
| 74 | |
|---|
| 75 | Mounts all unmounted filesystems with labels of form "MogileDev<n>" at |
|---|
| 76 | /var/mogdata/dev<n>, creating the needed directories as well. |
|---|
| 77 | |
|---|
| 78 | You can do this at runtime without restarting mogstored, assuming you |
|---|
| 79 | can add new block devices at runtime via your SCSI/SATA/etc controller. |
|---|
| 80 | |
|---|
| 81 | =head1 OPTIONS |
|---|
| 82 | |
|---|
| 83 | =over |
|---|
| 84 | |
|---|
| 85 | =item --help | -h |
|---|
| 86 | |
|---|
| 87 | this help |
|---|
| 88 | |
|---|
| 89 | =item --verbose | -verbose |
|---|
| 90 | |
|---|
| 91 | be verbose |
|---|
| 92 | |
|---|
| 93 | =back |
|---|
| 94 | |
|---|
| 95 | =head1 RETURN CODE |
|---|
| 96 | |
|---|
| 97 | 0 on success or inaction because no action needed to happen. |
|---|
| 98 | |
|---|
| 99 | 1 on partial failure (some mounts succeeed). |
|---|
| 100 | |
|---|
| 101 | 2 on total failure (things had to be done, but nothing was). |
|---|
| 102 | |
|---|
| 103 | =head1 AUTHOR |
|---|
| 104 | |
|---|
| 105 | Brad Fitzpatrick, E<lt>brad@danga.comE<gt> |
|---|
| 106 | |
|---|
| 107 | =head1 WARRANTY, BUGS, DISCLAIMER |
|---|
| 108 | |
|---|
| 109 | This tool mounts disks, and disks hold data, so naturally you should |
|---|
| 110 | be afraid. Real the source code to see what it does. This tool comes |
|---|
| 111 | with no warranty of any kind. You're response for its use or misuse. |
|---|
| 112 | |
|---|
| 113 | =head1 COPYRIGHT & LICENSE |
|---|
| 114 | |
|---|
| 115 | This tool is Copyright 2006, Six Apart, Ltd. |
|---|
| 116 | You're free to redistribute it under the same terms as perl itself. |
|---|
| 117 | |
|---|
| 118 | =end |
|---|