Forum: Cool Things
Topic: Auto mounter script.
started by: Zucca
Posted by Zucca on July 28 2006,05:27
This script will be included to my server.
Any suggestios? Bug fixes?
Code Sample |
#!/bin/bash
# amusrpart.sh By: Zucca@dive.to (ICQ: 66768853 - MSN: ki77bi77@hotmail.com - IRC: Zucca@IRCnet) # Full copyright owned by Zucca. First non-copyrighted relase will be version 0.1.xxx.;) # Why? Because I'd like to finish this to... 'stable' version before anyone tries to use this. Any help is still very welcome.;) # NOTE: This script is mainly designed for DSL LiveCD. I don't know how this would act if used with other distributions/installs. # v 0.0.2b
# This simple script will mount all the partitions in all devices available on current computer for users if .mount.config is found from the root of partition.
for PARTITION in `ls /mnt/ | grep "[hs]d.[1-9]*"` # Let's list all the hd* and sd* devices... do
mount /mnt/$PARTITION 2> /dev/null
if [ -e /mnt/$PARTITION/.mount.conf ] # Check if .mount.conf exists.
then OWNER=`awk 'NR == 1' /mnt/$PARTITION/.mount.conf` echo "$PARTITION owner: $OWNER" MOUNTPOINT=`awk 'NR == 2' /mnt/$PARTITION/.mount.conf` echo "Mount point: $MOUNTPOINT" if [ -e $MOUNTPOINT ] # If mount point specified in .mount.conf already exist then abort. then echo "Aborting mount: $MOUNTPOINT already exists." umount /mnt/$PARTITION else umount /mnt/$PARTITION # Next lines are commented because... Think what would happen if filetool.sh is executed.;) # mkdir $MOUNTPOINT # chmod 750 $MOUNTPOINT # chown $OWNER $MOUNTPOINT # chgrp staff $MOUNTPOINT
mount -o owner=$OWNER,mode=0750 /mnt/$PARTITION # So instead mounting partition straight to $MOUNTPOINT we do regular mount and create a sym link. ln -s /mnt/$PARTITION $MOUNTPOINT fi else # Just simply unmount partition if .mount.conf cannot be found. umount /mnt/$PARTITION fi echo "" done
echo "User dedicated partitions mounted."
|
Thanks for all the tips I've received alredy from you guys.
Posted by Zucca on July 31 2006,10:54
I've noticed few security hazard bugs.
I'll try to solve those until next friday...
Posted by Zucca on Aug. 21 2006,13:04
Project halted.
All my work from past three months are propably gone because of some nasty bug...
Posted by Zucca on Sep. 28 2006,23:47
Well now I'm back wiith this project. I have improves my script a bit but now I don't get what's wrong...
Here's the source:Code Sample |
#!/bin/bash
# amusrpart.sh By: [EMAIL=Zucca@dive.to]Zucca@dive.to[/EMAIL] (ICQ: 66768853 - MSN: [EMAIL=ki77bi77@hotmail.com]ki77bi77@hotmail.com[/EMAIL] - IRC: Zucca@IRCnet) # Full copyright owned by Zucca. First non-copyrighted relase will be version 0.1.xxx.;) # Why? Because I'd like to finish this to... 'stable' version before anyone tries to use this. Any help is still very welcome.;) # NOTE: This script is mainly designed for DSL LiveCD. I don't know how this would act if used with other distributions/installs. # v 0.0.5b
# This simple script will mount all the partitions in all devices available on current computer for users if .mount.config is found from the root of partition.
if [ ! -e /var/mount_confs ] then mkdir /var/mount_confs fi
if [ -z $1 ] then echo 'USAGE: amusrpart.sh <mount|umount>' exit fi
TELLUSAGE="no"
for PARTITION in `ls /mnt/ | grep "[hs]d.[1-9]*"` # Let's list all the hd* and sd* devices... do
if [ $1 = "mount" ] then if [ -e /tmp/.amusrpart.mark ] then echo "Partitions already mounted." echo "Or if you are sure that it is otherwise then delete /tmp/.amusrpart.mark" exit fi
mount /mnt/$PARTITION
if [ -f /mnt/$PARTITION/.mount.conf ] # Check if .mount.conf exists. then OWNER=`awk 'NR == 1' /mnt/$PARTITION/.mount.conf` echo "$PARTITION owner: $OWNER" MOUNTPOINT=`awk 'NR == 2' /mnt/$PARTITION/.mount.conf` echo "Mount point: $MOUNTPOINT" if [ -e $MOUNTPOINT ] # If mount point specified in .mount.conf already exist then abort. then echo "Aborting mount: $MOUNTPOINT already exists." umount /mnt/$PARTITION else mv /mnt/$PARTITION/.mount.conf /var/mount_confs/mount.$PARTITION
# Next lines are commented because... Think what would happen if filetool.sh is executed.;) # mkdir $MOUNTPOINT # chmod 750 $MOUNTPOINT # chown $OWNER $MOUNTPOINT # chgrp staff $MOUNTPOINT
mount -o remount,owner=$OWNER,mode=0750 /mnt/$PARTITION
# So instead mounting partition straight to $MOUNTPOINT we do regular mount and create a sym link. ln -s /mnt/$PARTITION $MOUNTPOINT fi echo "" else # Just simply unmount partition if .mount.conf cannot be found. umount /mnt/$PARTITION fi elif [ $1 = "umount" ] # move mount.conf back to partition and unmount. if [ -f /var/mount_confs/mount.$PARTITION ] then mv /var/mount_confs/mount.$PARTITION /mnt/$PARTITION/.mount.conf if [ -L $MOUNTPOINT ] then rm -fr $MOUNTPOINT fi umount /mnt/$PARTITION fi elif [ -n $1 ] TELLUSAGE="yes" fi done |
When I try to run it with mount option I get an error message:Quote |
./amusrpart.sh: line 83: syntax error near unexpected token `elif' ./amusrpart.sh: line 83: ` elif [ -n $1 ]' |
There are the lines from 82 to 84:Code Sample |
fi elif [ -n $1 ] TELLUSAGE="yes" |
Can you figure out what's wrong?
Posted by ^thehatsrule^ on Sep. 29 2006,02:25
I think you could place some "then"s after your elifs.
Posted by Zucca on Sep. 29 2006,05:09
Jeah. I thought that too, but manuals never use then after elif.
But well. Let's try it anyway. =)
Posted by Zucca on Sep. 29 2006,06:08
Well it worked anyway.
Thanks.
Posted by mikshaw on Sep. 29 2006,12:28
In addition to that, you might consider cutting down on your "if" statements, simply to make maintenance easier and keep it more readable.
If you're testing multiple (more than 2) values of a single variable I'd say "case" is much more appropriate. This way all of your results of the $1 check can be put in an orderly manner, you won't have a huge amount of overlapping ifs and fis and thens to confuse, and then *I'll* be able to read it
Anyway, that's just opinion, but here's what I'd do....
Code Sample |
if [ -n "$1" ]; then case "$1" in mount) stuff for mount ;; umount) stuff for umount ;; *) stuff for every other $1 result ;; esac fi
|
Also, you don't have to test both -z $1 and -n $1. If -z $1 is false and doesn't exit, you know -n $1 will be true. If a case with -n $1 is the only code, including -z $1 is just redundant.
Posted by ^thehatsrule^ on Sep. 29 2006,21:58
Heh, nice review mikshaw.
I'll just add that you probably won't need to use 'then' after 'else' if you're just doing 1 (self-enclosed) command.
Posted by humpty on Sep. 29 2006,22:04
I dunno what the fuss is all about,
I just have a list of mount cmds in my script, don't care about any error msgs,
just ignore them.
Posted by Zucca on Sep. 30 2006,00:21
True. I plan it 'optimeze' my script when I can get jEdit. ;)
Posted by Zucca on Nov. 06 2006,09:51
I made a new feature to my script.
It searches for .mount.exec file from $PARTITION. If found then sources it.
This is handy when you have some programs on your hd that you want to run automatically on startup.
I run my eggdrop that way. =) Yes. There's then a big security hole, but I managed to avoid it by then moving .mount.exec and .mount.conf files to another directory and make it accesible only by root. Of course script will move those files back on shutdown/reboot.
Anyway this script still needs optimizing and features. For example (un)mounting only one partition.
I'll be posting more later...
Posted by mikshaw on Nov. 06 2006,15:11
The autorun is, or at least can be, a useful idea.
You might be able to avoid moving and replacing files by mounting the drive without executable permission and running a copy of the file instead of the original. This way you won't need to bother moving the files back....it will allow the file to continue working after a crash, or on a cdrom.
Just some thought food.
Posted by Zucca on April 16 2007,12:08
I think too that moving and/or copying those files is unnecessary.
I think script should look if modes of the file are 500 and owner is root, then if both are true then source it.
Any suggestios how to read permissions and owner of a file? I can't (at least at the moment) figure out anything expect `ls -l` and it would need some "dirty" parsing.
Posted by Zucca on April 19 2007,15:45
Correction to previous post: mode should be 600.
Anyway I managed to do a neat function to check root priviliges:
Code Sample |
function crootp () {
# crootp: short for check root priviliges # user and group must be root and permissions must be 600 [ -f $1 ] && [ -G $1 ] && [ -O $1 ] && [ `stat -c %a $1` -eq "600" ]
}
|
It's usage is quite simple:
Code Sample |
if crootp $FILE then echo FIle is only accessible by root else echo File does not exist or permissions are incorrect fi |