Newsgroups: comp.sys.hp

Here's what I do with MO's to make them mount, and them link themselves
into my file system ... this way, whole (seldom used) subsystems can be
stuck in their favorite spots without taxing my memory ... and quickly
I might add ...

The other neat use is  to move filesystems between home
and work, i.e. cad tools and designs, etc.

-mike

  Mike Ferrara M/S 2LRR
  HP Signal Analysis Div R&D
  1212 Valley House Drive
  Rohnert Park, CA 94928
  (707) 794-4479
  mikef%hpsadle@relay.hp.com
  mikef@hpsadle.sad.hp.com


----------------cut here------------------------------
#!/usr/local/bin/suidperl
#!/usr/local/bin/perl -d

require "getopts.pl";

$ENV{'PATH'}='/bin:/usr/bin:/usr/local/bin';
$ENV{'SHELL'}= '/bin/sh';
$ENV{'IFS'}='';

#Setup signal handler
$SIG{'INT'}='cleanup';
$SIG{'HUP'}='cleanup';
$SIG{'QUIT'}='cleanup';
$SIG{'TERM'}='cleanup';

$tmpfile="/tmp/mnt$$";
$debug=0;
$verbose=0;
$nolink=0;
$fsck=0;
$machine=`/bin/hostname`;
chop($machine);

%mdev = ('mo1' , '/dev/dsk/mo1',
'mo2' , '/dev/dsk/mo2',
'fd'  , '/dev/dsk/md9133',
);

%mpoint = ('mo1', '/mo1',
'mo2', '/mo2',
'fd', '/fd',
);


$ret=&Getopts('vndhf');
$debug=1 if (defined($opt_d));
$verbose=1 if (defined($opt_d) || defined($opt_v));
$nolink=1 if (defined($opt_n));
$fsck=1 if (defined($opt_f));

if (defined($opt_h) || ($ret != 1)) {
   print<<EOF;

am/um mounts/unmounts a removable filesystem, and if a file
.amrc.<machine-name> or .amrc exists at the head of the mounted
filesystem, it is read and used as follows:

>>> The amrc must be owned by root and not world writable.

1st section:
   Delimited by the words "begin links" on a line by itself
   and "end" on a line by itself defines links to be done
   after the mount and undone after the umount.
   the lines are of the form <file1> <newfile> [<owner> <group> <mode>]
   this results in the following commands to be executed:
        ln -s file1 newfile
        chown owner file1
        chgrp group file1
        chmod mode  file1
   owner group mode defaults to "bin bin 555"

2nd section:
   Delimited by the words "begin mountscript" on a line by itself and
   "end" on a line by itself defines a script to be executed
   by /bin/sh (may contain a #! construct) upon mounting the filesystem
   after the link section has been processed.

3rd section:
   Delimited by the words "begin umountscript" on a line by itself and
   "end" on a line by itself defines a script to be executed
   by /bin/sh (may contain a #! construct) before umounting the filesystem
   but after the link section has been un-processed.

You may reverse the script and links sections, and their execution order
will be reversed. Umounting always happens last, and mounting always happens
first.

Usage:
    am [-v] [-h] [-n] [-f] name
    um [-v] [-h] [-n] name

Options: 
	-v   Be verbose
	-h   This help message
	-n   Don't execute the .amrc file on the disk
	-f   fsck -y the disk first if necessary

EOF
print "Currently, I know about the following devices:\n";
    print "	name	device\n";
    print "	------- --------\n";
foreach $dev (keys(%mdev)) {
    print "	$dev	$mdev{$dev}\n";
}

exit(0);
}

$request=shift(@ARGV);

$myname=$0;
$mount=1 if ($myname=~/am/);
$mount=0 if ($myname=~/um/);

sub cleanup {
   print STDERR "Hey I've been interrupted!";
   unlink($tmpfile) if (-e $tmpfile);
   exit(1);
   }

sub Error {
   local($errmsg)=shift(@_);
   print STDERR "$myname: $errmsg \n";
   unlink($tmpfile) if (-e $tmpfile);
   exit(1);
   }
   
$dev=$mdev{$request};
$point=$mpoint{$request};


if (!defined($dev)) {
   &Error("Sorry, I don't know about device $request.");
   }

if ($mount) { 
print STDERR "mount $dev on $point\n" if ($verbose);
$ret=system "/etc/fsclean",$dev;
if ($ret != 0) {
   print STDERR "fsck -y $dev" if ($verbose || $debug);
   system "/etc/fsck","-y",$dev if ($fsck == 1);
   die "Disk $dev needs to be fsck'd before mounting!\n" if ($fsck != 1);
   }
$ret=system "/etc/mount",$dev,$point;
if ($ret != 0) {
   $ret=system "/etc/mount",$dev,$point,"-r";
   print "OK. mounted read-only \n" if ($ret == 0);
}   
exit(0) if ($nolink);
}

print "$machine" if ($debug);
$amrc="$point/.amrc" if (-f "$point/.amrc");
$amrc="$point/.amrc.$machine" if (-f "$point/.amrc.$machine");

($st_dev,$st_ino,$st_mode,$st_nlink,$st_uid,$st_gid,$st_rdev,$st_size,
	$st_atime,$st_mtime,$st_ctime,$st_blksize,$st_blocks) = stat($amrc);

if (($st_uid != 0) || ($st_mode & 0022)) {
        printf STDERR "owner=%s, mode=%o\n",$st_uid,$st_mode;
	&Error("$amrc is INSECURE!, I'm not executing it.");
}

if (defined($amrc)) {
   print STDERR "Begin processing $amrc\n" if ($debug);
   open(AMRC,$amrc);
   while (<AMRC>) {
        chop;
        s/^\s//; #get rid of leading whitespace
        s/\s$//; #get rid of trailing whitespace
        s/#.*//; #get rid of comments
        if (/^begin\slinks$/) {
           while(<AMRC>) {
               chop;
               s/^\s//;
               s/\s$//;
               s/#.*//; #get rid of comments
               print STDERR "$_\n" if ($debug);
               next if (/^$/);
               last if (/^end$/);
  	       ($file,$link,$own,$grp,$mode)=split;
    	       unlink($link) if (-l $link);
               if (!$mount) {
                  next;
                  }
               $file="$point/$file";
	       symlink($file,$link);
               print STDERR "link $file $link\n" if ($verbose);
               $own="bin" if (!defined($own));
               $grp="bin" if (!defined($grp));
               $mode=0555 if (!defined($mode));
               chown((getpwnam($own))[2],(getgrnam($grp))[2],$link);
               chmod $mode,$link;
               printf STDERR ("chmodes $own $grp %o $link\n",$mode) if ($verbose);
               }
           }
        if (/^begin\smountscript$/&& $mount) {
           open(TMP,">$tmpfile");
           while(<AMRC>) {
               chop;
               s/^\s//;
               s/\s$//;
               last if (/^end$/);
               print TMP "$_\n";
               }
           close(TMP);
           print STDERR "execute mountscript\n" if ($verbose);
           chmod(0744,$tmpfile);
           system ("$tmpfile $dev $point");
           unlink($tmpfile);
           }
                
        if (/^begin\sumountscript$/&& !($mount)) {
           open(TMP,">$tmpfile");
           while(<AMRC>) {
               chop;
               s/^\s//;
               s/\s$//;
               last if (/^end$/);
               print TMP "$_\n";
               }
           close(TMP);
           print STDERR "execute umountscript\n" if ($verbose);
           chmod(0744,$tmpfile);
           system ("$tmpfile $dev $point");
           unlink($tmpfile);
           }
} #done processing the .amrc file
close(AMRC);
} #if -f .amrc
if (!$mount) {
	$ret=system "/etc/umount",$dev;
        print STDERR "umount $dev\n" if ($verbose);
}
exit(0);
Newsgroups: comp.sys.hp,comp.sys.sun.misc,comp.sys.sun.hardware