-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from mariux64/add-mxmount
Add mxmount
- Loading branch information
Showing
4 changed files
with
309 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,284 @@ | ||
#! /usr/local/system/perl/bin/perl | ||
|
||
use strict; | ||
use warnings; | ||
use Sys::Hostname; | ||
use Getopt::Long; | ||
|
||
my $configfile = "/etc/mxmounts"; | ||
my $exports = "/etc/exports"; | ||
|
||
my $exportstag = "# DO NOT EDIT BELOW THIS LINE # created by /usr/bin/mxmount #"; | ||
|
||
my $fullhostname; | ||
my $hostname; | ||
my @lines; | ||
my @exports; | ||
my %V; | ||
my %D; | ||
|
||
our $USAGE=<<"_EOF_"; | ||
usage: $0 | ||
$0 --reexport-only | ||
_EOF_ | ||
|
||
our ($opt_reexport_only); | ||
|
||
$fullhostname = hostname(); | ||
($hostname) = $fullhostname =~ /^(.*?)\./; | ||
|
||
%D = (); | ||
%V = ( | ||
DEFAULT_MOUNT_OPTIONS => '', | ||
DEFAULT_EXPORT_OPTIONS => '', | ||
DEFAULT_MOUNT_PREFIX => '/', | ||
|
||
SHORTHOST => $hostname, | ||
); | ||
|
||
@lines = read_file($configfile); | ||
@exports = read_file_raw($exports); | ||
|
||
@lines = parse_variables(@lines); | ||
|
||
@lines = parse_data(@lines); | ||
|
||
foreach(@lines) { | ||
print STDERR "skipping: '$_'\n"; | ||
} | ||
|
||
add_data0_if_not_present(); | ||
|
||
my %options; | ||
GetOptions ( | ||
'reexport-only' => \$opt_reexport_only, | ||
) or die $USAGE; | ||
@ARGV and die $USAGE; | ||
|
||
mount_all() unless $opt_reexport_only; | ||
create_exports(); | ||
system("exportfs -ra"); | ||
|
||
sub safe_qx { open my $pipe,'-|',@_; return join('',<$pipe>) } | ||
|
||
sub add_data0_if_not_present { | ||
my $allmp = $D{$hostname}; | ||
|
||
$allmp = [] unless (defined $allmp); | ||
|
||
foreach my $mp (sort { $a->{mountpoint} cmp $b->{mountpoint} } @$allmp ) { | ||
if ($mp->{label} eq "data0") { | ||
return; | ||
} | ||
if ($mp->{mountpoint} eq "$V{DEFAULT_MOUNT_PREFIX}/$hostname/0") { | ||
print STDERR "$mp->{mountpoint} already blocked by $mp->{label}\n"; | ||
return; | ||
} | ||
} | ||
parse_data("$hostname !data0"); | ||
} | ||
|
||
sub create_exports { | ||
my $allmp = $D{$hostname}; | ||
my $newexport=system('hostconfig newexport')==0; | ||
|
||
open(EXPORTS, '>', $exports) or die "can't open $exports: $!"; | ||
|
||
foreach my $exp (@exports) { | ||
chomp($exp); | ||
next if($exp =~ /^\s*$/); | ||
last if($exp eq $exportstag); | ||
print EXPORTS "$exp\n"; | ||
} | ||
print EXPORTS "\n$exportstag\n\n"; | ||
|
||
foreach my $mp (@$allmp) { | ||
next if($mp->{noexport}); | ||
unless ($newexport) { | ||
my @CMD = ($mp->{mountpoint}, $mp->{exportopts}); | ||
print join " ", "$exports: ", @CMD, "\n"; | ||
print EXPORTS join " ", @CMD, "\n"; | ||
} else { | ||
my ($mountpoint,$exportopts)=($mp->{mountpoint}, $mp->{exportopts}); # '/amd/theinternet/1','@amd(sync,rw,...)' | ||
my ($hostspec,$optspec)=$exportopts=~/^([^(]+)(.*)/; # '@amd','(sync,rw,...)' | ||
my ($opts)=$optspec=~/\((.*)\)/; # 'sync,rw,...' | ||
my $hosts=''; | ||
warn "export $mountpoint to $hostspec opts $opts\n"; | ||
if (my ($group) = $hostspec=~/^@(.+)/) { | ||
$hosts=safe_qx('/usr/sbin/hostconfig','--list',$group); # expanded group | ||
$hosts or warn "group $group is empty\n"; | ||
} else { | ||
$hosts=$hostspec; # single host | ||
} | ||
$hosts and printf EXPORTS "%s -%s %s\n",$mountpoint,$opts,$hosts; | ||
} | ||
} | ||
close EXPORTS; | ||
} | ||
|
||
sub mount_all { | ||
my $allmp = $D{$hostname}; | ||
my @CMD; | ||
foreach my $mp (sort { $a->{mountpoint} cmp $b->{mountpoint} } @$allmp) { | ||
|
||
@CMD = (); | ||
|
||
push @CMD, 'mount', "LABEL=$mp->{label}", $mp->{mountpoint}; | ||
|
||
if($mp->{fs}) { | ||
push @CMD, '-t', $mp->{fs}; | ||
} | ||
if($mp->{mountopts}) { | ||
push @CMD, '-o', $mp->{mountopts}; | ||
} | ||
system('mkdir','-p',$mp->{mountpoint}); | ||
print STDERR join " ", @CMD, "\n"; | ||
system(@CMD); | ||
} | ||
} | ||
|
||
sub parse_data { | ||
my @lines = @_; | ||
my @invalid = (); | ||
my @data; | ||
|
||
my $rest; | ||
|
||
foreach(@lines) { | ||
@data = split /\s+/, $_; | ||
|
||
unless($data[1]) { | ||
push @invalid, $_; | ||
next; | ||
} | ||
|
||
my $D = {}; | ||
|
||
$D->{line} = $_; | ||
|
||
$D->{host} = $data[0]; | ||
$D->{label} = $data[1]; | ||
if($D->{label} =~ /^!(.*)/) { | ||
$D->{noexport} = 1; | ||
$D->{label} = $1; | ||
} | ||
|
||
if($D->{label} =~ /(.*):(.*)/) { | ||
$D->{label} = $1; | ||
$D->{mountpoint} = $2; | ||
} else { | ||
if($D->{label} =~ /^X/) { # X: decent controller based raid | ||
$D->{mountpoint} = 'X/' . $D->{label}; | ||
$D->{label} = lc($D->{label}); | ||
} elsif($D->{label} =~ /^C/) { # C: controller based raid, confidential | ||
$D->{mountpoint} = 'C/' . $D->{label}; | ||
$D->{label} = lc($D->{label}); | ||
if (!$data[3]) { | ||
$D->{noexport} = 1; | ||
} | ||
} elsif($D->{label} =~ /^D/) { # D: scratch software raid (mdadm) | ||
$D->{mountpoint} = 'D/' . $D->{label}; | ||
$D->{label} = lc($D->{label}); | ||
} elsif($D->{label} =~ /^M/) { # M: decent software raid (mdadm) | ||
$D->{mountpoint} = 'M/' . $D->{label}; | ||
$D->{label} = lc($D->{label}); | ||
} elsif($D->{label} =~ /^data(.*)/) { | ||
$D->{mountpoint} = $1; | ||
} else { | ||
warn "mxmount: unknown shortlabel '$D->{label}'.. skipping.."; | ||
next; | ||
} | ||
} | ||
if($D->{mountpoint} !~ m(^\/)) { | ||
$D->{mountpoint} = "$V{DEFAULT_MOUNT_PREFIX}/" . $D->{mountpoint}; | ||
} | ||
|
||
$D->{fs} = 'auto'; | ||
$D->{mountopts} = $data[2] ? $data[2] : $V{DEFAULT_MOUNT_OPTIONS}; | ||
if($D->{mountopts} =~ /\[(.*)\](.*)/) { | ||
$D->{fs} = $1; | ||
$D->{mountopts} = $2; | ||
} | ||
|
||
$D->{exportopts} = $data[3] ? $data[3] : $V{DEFAULT_EXPORT_OPTIONS}; | ||
|
||
foreach(qw(host label mountpoint fs mountopts exportopts )) { | ||
$D->{$_} = expand_variables($D->{$_}); | ||
} | ||
push @{$D{$D->{host}}}, $D; | ||
} | ||
return @invalid; | ||
} | ||
|
||
sub expand_variables { | ||
my $s = shift; | ||
|
||
foreach my $k (keys %V) { | ||
$s =~ s/$k/$V{$k}/g; | ||
} | ||
return $s; | ||
} | ||
|
||
sub parse_variables { | ||
my @lines = @_; | ||
my @invalid = (); | ||
|
||
my ($key, $value); | ||
|
||
foreach(@lines) { | ||
if(($key, $value) = /^(\S+)=\s*(.*)$/) { | ||
$V{$key} = $value; | ||
} else { | ||
push @invalid,expand_variables($_); | ||
} | ||
} | ||
return @invalid; | ||
} | ||
sub read_file { | ||
my $file = shift; | ||
open F, "$file" or die "can't open $file: $!\n"; | ||
my @lines=(); | ||
my $line=""; | ||
my $cont=0; | ||
while(<F>) { | ||
chomp; | ||
next if(/^\s*#/ or /^\s*$/); | ||
$cont=0; | ||
s/#.*$//; # remove comments.. | ||
$_ = $line . $_; | ||
|
||
if(s/\\\s*$//) { | ||
# line continous in next line.. | ||
$cont=1; | ||
} | ||
|
||
$line = $_; | ||
|
||
unless($cont) { | ||
$line =~ s/\s+/ /g; | ||
$line =~ s/^\s+//; | ||
$line =~ s/\s+$//; | ||
push @lines, $line; | ||
$line=""; | ||
} | ||
} | ||
close F; | ||
return @lines; | ||
} | ||
|
||
sub read_file_raw { | ||
my $file = shift; | ||
my @lines; | ||
|
||
open F, "$file" or return; | ||
@lines = <F>; | ||
close F; | ||
|
||
return @lines; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[Unit] | ||
Description=MX mount local data filessystems | ||
After=mxraid.startup.service | ||
ConditionPathExists=/etc/mxmounts | ||
|
||
[Service] | ||
Type=oneshot | ||
ExecStart=/usr/bin/mxmount | ||
RemainAfterExit=yes | ||
|
||
[Install] | ||
WantedBy=local-fs.target |