forked from mogilefs/MogileFS-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mogautomount
executable file
·134 lines (95 loc) · 3.34 KB
/
mogautomount
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/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