Created
November 2, 2015 11:41
-
-
Save spiculator/75ba5dc0ee768989c975 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/perl -w | |
use strict; | |
use Cwd 'abs_path'; | |
sub usage() { | |
die "Usage: $0 [-f] [-l] [-v] <path> | |
-f unmount subdirectories even if <path> is not mounted | |
-l use lazy umount | |
-v verbose mode\n";} | |
my ($verbose, $lazy, $force); | |
while( @ARGV and $ARGV[0] =~ /^-(\w)(.*)$/ ) { | |
my ($opt, $rest) = ($1, $2); | |
length($rest) ? $ARGV[0] = "-$rest" : shift @ARGV; | |
"v" eq $opt ? ($verbose=1) : "l" eq $opt ? ($lazy=1) : "f" eq $opt ? ($force=1) : die "unknown option: $opt\n"; | |
} | |
1 == @ARGV or usage(); | |
my $arg = shift @ARGV; | |
die "$arg does not exist\n" unless -l $arg or -e $arg; | |
die "$arg is not a dir\n" unless -d $arg; | |
my $path = abs_path($arg); | |
die "abs_path failed for $arg\n" unless defined $path; | |
my ($umounted_smth, $path_is_mp); | |
for(;;) { | |
my $last; | |
open MOUNTS, "<", "/proc/mounts" or die $!; | |
while(<MOUNTS>) { | |
my $cur = (split)[1]; | |
$cur =~ s/\\([0-7]{1,3})/chr oct $1/ge; | |
#warn "checking $cur\n"; ############## | |
if( $cur eq $path ) { | |
$path_is_mp = 1; | |
$last = $cur; | |
#warn "got mp\n"; ############## | |
} elsif( $cur =~ /^$path\// ) { | |
$last = $cur; | |
#warn "got $cur\n"; ################ | |
} | |
} | |
close MOUNTS or die $!; | |
die "$path is not a mount point\n" unless $path_is_mp or $force; | |
last if not defined $last; | |
print "umounting $last\n" if $verbose; | |
my @args = ($last); | |
unshift @args, "-l" if $lazy; | |
0 == system("umount", @args) or die "could not umount $last\n"; | |
$umounted_smth = 1; | |
} | |
print "nothing is actually unmounted\n" if $verbose and not $umounted_smth; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment