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.

  1. #!/usr/bin/perl  
  2.   
  3. use strict ;  
  4. use warnings ;  
  5. use Getopt::Long ;  
  6. use 5.010 ;  
  7. use subs qw( mount unmount ) ;  
  8.   
  9. # Definition  
  10. # mount.pl                   --   mounts all in the config  
  11. # mount.pl -Q                -- unmounts all in the config  
  12. # mount.pl -m foo            --   mounts foo, if foo is in config  
  13. # mount.pl -u foo            -- unmounts foo, if foo is in config  
  14. # mount.pl -m foo -u bar     --   mounts and unmounts can be mixed  
  15.   
  16. my @mount ;  
  17. my @unmount ;  
  18. my $unmount ;  
  19. my %local ;  
  20. my %remote ;  
  21.   
  22. my $mountprog   = '/usr/bin/sshfs' ;  
  23. my $unmountprog = '/bin/fusermount' ;  
  24.   
  25. GetOptions( 'mount=s'   => \@mount,  
  26.             'unmount=s' => \@unmount,  
  27.             'quit'      => \$unmount, ) ;  
  28.   
  29. while ( <data> ) {  
  30.     chomp ;  
  31.     my $line = $_ ;  
  32.     next if $line !~ m{\w}mx ;  
  33.     my $result = ( split m{\#}mx, $line )[ 0 ] ;  
  34.     $line = $result ;  
  35.     next if $line !~ m{\w}mx ;  
  36.     my ( $name$remote$local ) = split m{\s*\|\s*}mx, $line ;  
  37.     $local$name }  = $local ;  
  38.     $remote$name } = $remote ;  
  39.     }  
  40.   
  41. # UNMOUNT EVERYTHING  
  42. if ( $unmount ) {  
  43.     for my $mount ( sort keys %local ) {  
  44.         unmount $mount , $local$mount } ;  
  45.         }  
  46.     }  
  47.   
  48. # MOUNT EVERYTHING  
  49. elsif ( ( $#mount == -1 ) && ( $#unmount == -1 ) ) {  
  50.     for my $mount ( sort keys %local ) {  
  51.         mount $mount , $remote$mount }, $local$mount } ;  
  52.         }  
  53.     }  
  54.   
  55. # MIXED MOUNTS AND UNMOUNTS  
  56. else {  
  57.   
  58.     # mounts first  
  59.     for my $mount ( @mount ) {  
  60.         mount $mount , $remote$mount }, $local$mount } ;  
  61.         }  
  62.   
  63.     # then unmounts  
  64.     for my $mount ( @unmount ) {  
  65.         unmount $mount , $local$mount } ;  
  66.         }  
  67.     }  
  68.   
  69. exit 0 ;  
  70.   
  71. sub mount {  
  72.     my $name   = shift ;  
  73.     my $remote = shift ;  
  74.     my $local  = shift ;  
  75.     return 0 if $name !~ /\w/mx ;  
  76.     return 0 if $remote !~ /\w/mx ;  
  77.     return 0 if $local !~ /\w/mx ;  
  78.     return 0 if !-d $local ;  
  79.     say 'Mounting ' . $name ;  
  80.     say qx( /usr/bin/sshfs $remote $local ) ;  
  81.     return 1 ;  
  82.     }  
  83.   
  84. sub unmount {  
  85.     my $name  = shift ;  
  86.     my $local = shift ;  
  87.     return 0 if $name !~ /\w/mx ;  
  88.     return 0 if $local !~ /\w/mx ;  
  89.     return 0 if !-d $local ;  
  90.     say 'Unmounting ' . $name ;  
  91.     say qx( /bin/fusermount -u $local ) ;  
  92.     return 1 ;  
  93.     }  
  94.   
  95. __DATA__  
  96. # NAME | REMOTE MOUNT POINT | LOCAL MOUNT POINT  
  97. Work     | xxx.xxxx.xxxx.edu: | /home/rant/Mount/work  
  98. Home     | xxx.xxxx.xxxx.net: | /home/rant/Mount/home</data>  


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

No comments:

Post a Comment