Merge remote-tracking branch 'bogomips/resurrect-device' into testing
[MogileFS-Server.git] / mogautomount
bloba9e7dc5424c71d245754d43b4c2ecda4103db9f1
1 #!/usr/bin/perl
3 use strict;
4 use Getopt::Long;
6 # Rename binary in process list to make init scripts saner
7 $0 = $_ = $0;
9 my ($help, $verbose, $chmod_mountpoints);
10 usage(0) unless GetOptions(
11 'help' => \$help,
12 'verbose' => \$verbose,
13 'chmod-mountpoints' => \$chmod_mountpoints,
15 usage(0) if @ARGV;
16 usage(2) if $help;
18 sub usage {
19 my $verbosity = shift;
20 require Pod::Usage;
21 Pod::Usage::pod2usage({
22 -exitval => 1,
23 -verbose => $verbosity,
24 });
27 my $base = "/var/mogdata";
28 my @bdevs = `/sbin/blkid -c /dev/null`;
29 die "Failed to run /sbin/blkid to get available block devices." if $?;
31 my %mounted; # dev -> 1
32 open (M, "/proc/mounts") or die "Failed to open /proc/mounts for reading: $!\n";
33 while (<M>) {
34 m!^(\S+) /var/mogdata/dev(\d+)! or next;
35 my $devid = $2;
36 $mounted{$1} = 1;
37 if ($verbose) {
38 warn "Mogile device $devid, $1, is already mounted.\n";
42 my $bad_count = 0;
43 my $good_count = 0;
45 foreach my $bdev (@bdevs) {
46 next unless $bdev =~ /^(.+?):.*LABEL="MogileDev(\d+)"/;
47 my ($dev, $devid) = ($1, $2);
48 unless (-d "$base") { mkdir $base or die "Failed to mkdir $base: $!"; }
49 my $mnt = "$base/dev$devid";
50 unless (-d $mnt) { mkdir $mnt or die "Failed to mkdir $mnt: $!"; }
51 next if $mounted{$dev};
53 if ($chmod_mountpoints and ((stat($mnt))[2] & 0777) != 0) {
54 warn "Mountpoint on parent filesystem is writable, fixing.\n" if $verbose;
55 chmod 0, $mnt
56 or die "Unable to set mogile device mountpoint '$mnt' mode to 0 (no access)";
59 if (system("mount", '-o', 'noatime', $dev, $mnt)) {
60 warn "Failed to mount $dev at $mnt.\n";
61 $bad_count++;
62 } else {
63 warn "Mounted device $devid at $mnt.\n" if $verbose;
64 $good_count++;
68 exit 0 if ! $bad_count;
69 exit 1 if $good_count;
70 exit 2;
72 __END__
74 =head1 NAME
76 mogautomount - automatically discover and mount MogileFS disks
78 =head1 SYNOPSIS
80 mogautomount [--verbose | -v]
81 mogautomount [--help | -h]
83 =head1 DESCRIPTION
85 Mounts all unmounted filesystems with labels of form "MogileDev<n>" at
86 /var/mogdata/dev<n>, creating the needed directories as well.
88 You can do this at runtime without restarting mogstored, assuming you
89 can add new block devices at runtime via your SCSI/SATA/etc controller.
91 =head1 OPTIONS
93 =over
95 =item --help | -h
97 this help
99 =item --verbose | -verbose
101 be verbose
103 =item --chmod-mountpoints
105 If a mogile device isn't mounted yet, check to make sure the underlying filesystem has the directory set
106 to be not readable or writable at all (chmod 0). This could help prevent mogstored from accidentally writing
107 to the underlying filesystem.
109 =back
111 =head1 RETURN CODE
113 0 on success or inaction because no action needed to happen.
115 1 on partial failure (some mounts succeed).
117 2 on total failure (things had to be done, but nothing was).
119 =head1 AUTHOR
121 Brad Fitzpatrick, E<lt>brad@danga.comE<gt>
123 =head1 WARRANTY, BUGS, DISCLAIMER
125 This tool mounts disks, and disks hold data, so naturally you should
126 be afraid. Real the source code to see what it does. This tool comes
127 with no warranty of any kind. You're response for its use or misuse.
129 =head1 COPYRIGHT & LICENSE
131 This tool is Copyright 2006, Six Apart, Ltd.
132 You're free to redistribute it under the same terms as perl itself.
134 =end