Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2008/12/09

Useful Test

I'm mostly going to test how to put code samples here, but if you're going to test, might as well make it useful, right?

I run Ubuntu, and I have sshfs installed. sshfs is part of FUSE, Filesystems in Userspace. What SSH allows you to do is mount the file systems on other machines via SSH.

The issue I ran into was that I wanted certain continual mounts all the time. I created a ~/Mount directory and put the mountpoints in there, and had mounting and unmounting shell scripts. If I wanted everything, I got everything. If I just wanted to unmount and remount one, well, I had to grep for the commands I want.

I have two example machines to mount: Home and Work. mount.pl mounts both. mount.pl -q unmounts both. mount.pl -m Home mounts just the Home filesystem. mount.pl -u Home unmounts just the Home filesystem.

It could easily be set up to mount and unmount the NTFS filesystem of a dual-boot computer using ntfsmount, or interact with Windows systems via smbmount, or other things. Right now, it seems way too small to try to put together as a project, but if you find it useful, feel free to comment.

#!/usr/bin/perl

use strict ;
use warnings ;
use Getopt::Long ;
use 5.010 ;
use subs qw( mount unmount ) ;

# Definition
# mount.pl                   --   mounts all in the config
# mount.pl -Q                -- unmounts all in the config
# mount.pl -m foo            --   mounts foo, if foo is in config
# mount.pl -u foo            -- unmounts foo, if foo is in config
# mount.pl -m foo -u bar     --   mounts and unmounts can be mixed

my @mount ;
my @unmount ;
my $unmount ;
my %local ;
my %remote ;

my $mountprog   = '/usr/bin/sshfs' ;
my $unmountprog = '/bin/fusermount' ;

GetOptions( 'mount=s'   => \@mount,
            'unmount=s' => \@unmount,
            'quit'      => \$unmount, ) ;

while (  ) {
    chomp ;
    my $line = $_ ;
    next if $line !~ m{\w}mx ;
    my $result = ( split m{\#}mx, $line )[ 0 ] ;
    $line = $result ;
    next if $line !~ m{\w}mx ;
    my ( $name, $remote, $local ) = split m{\s*\|\s*}mx, $line ;
    $local{ $name }  = $local ;
    $remote{ $name } = $remote ;
    }

# UNMOUNT EVERYTHING
if ( $unmount ) {
    for my $mount ( sort keys %local ) {
        unmount $mount , $local{ $mount } ;
        }
    }

# MOUNT EVERYTHING
elsif ( ( $#mount == -1 ) && ( $#unmount == -1 ) ) {
    for my $mount ( sort keys %local ) {
        mount $mount , $remote{ $mount }, $local{ $mount } ;
        }
    }

# MIXED MOUNTS AND UNMOUNTS
else {

    # mounts first
    for my $mount ( @mount ) {
        mount $mount , $remote{ $mount }, $local{ $mount } ;
        }

    # then unmounts
    for my $mount ( @unmount ) {
        unmount $mount , $local{ $mount } ;
        }
    }

exit 0 ;

sub mount {
    my $name   = shift ;
    my $remote = shift ;
    my $local  = shift ;
    return 0 if $name !~ /\w/mx ;
    return 0 if $remote !~ /\w/mx ;
    return 0 if $local !~ /\w/mx ;
    return 0 if !-d $local ;
    say 'Mounting ' . $name ;
    say qx( /usr/bin/sshfs $remote $local ) ;
    return 1 ;
    }

sub unmount {
    my $name  = shift ;
    my $local = shift ;
    return 0 if $name !~ /\w/mx ;
    return 0 if $local !~ /\w/mx ;
    return 0 if !-d $local ;
    say 'Unmounting ' . $name ;
    say qx( /bin/fusermount -u $local ) ;
    return 1 ;
    }

__DATA__
# NAME | REMOTE MOUNT POINT | LOCAL MOUNT POINT
Work     | xxx.xxxx.xxxx.edu: | /home/rant/Mount/work
Home     | xxx.xxxx.xxxx.net: | /home/rant/Mount/home


(I plan to beautify the means to dump code in the near future.)

No comments:

Post a Comment