multiuser environment: mounting devices from script

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
shankargopal
Posts: 295
Joined: Sat 03 Dec 2005, 11:30

multiuser environment: mounting devices from script

#1 Post by shankargopal »

Hi

I have a couple of backup scripts that I wrote in Puppy - very basic, clunky things, but they work for me and have saved me many times from pen drive crashes, lost pen drives etc. Once in a while I experiment with other distros in persistent mode on my flash drive (current experimental candidate is Mint, running alongside Dpup 484 beta 4, which is the main workhorse right now). I'd like to port these scripts over but I have a very basic problem, born out of the fact taht most of my scripting experience has been in Puppy. The problem is this: how do you mount a device in a script in a multiuser environment (where mounting is superuser only)?

The reason is that the backup scripts check all attached storage devices for a certain file name that consists of an image, which the script then uses to store incremental backups (done using rsync and hard links). The image file is required because the backups are almost always on VFAT / NTFS file systems (as these are mostly not computers that I own).

But to check for the file, the script has to mount each device in turn, and this is impossible because it is not running as root. I can't use /etc/fstab either as the devices will be different on each computer that I boot from the flash drive on. I had a vague memory from my last time using a multiuser distro (about five years ago - that was before my Puppy epiphany :) ) that one can use setuid but apparently most distros now disallow setuid for scripts. So what does one do?

Thanks in advance!

User avatar
Pizzasgood
Posts: 6183
Joined: Wed 04 May 2005, 20:28
Location: Knoxville, TN, USA

#2 Post by Pizzasgood »

You could have it use 'su' to run them as root. Or install sudo and do the same thing, the difference being that sudo would let you limit which programs could be run with root permissions, and would use the user's password instead of root's password (because while having passwords in scripts is generally a bad idea, and having root's password much worse - though severity really depends on the application).
[size=75]Between depriving a man of one hour from his life and depriving him of his life there exists only a difference of degree. --Muad'Dib[/size]
[img]http://www.browserloadofcoolness.com/sig.png[/img]

shankargopal
Posts: 295
Joined: Sat 03 Dec 2005, 11:30

#3 Post by shankargopal »

Thanks a lot, Pizzasgood. For anyone else reading this thread who, like me, was initially confused about how to use sudo in a script when it will prompt for a password, you can use the -S command line option to make it read the password from standard input. Another and more secure option is here:

http://www.askdavetaylor.com/how_can_i_ ... cript.html

nooby
Posts: 10369
Joined: Sun 29 Jun 2008, 19:05
Location: SwedenEurope

#4 Post by nooby »

So does it work for you now?

Some of us have asked similar question and maybe they would be happy to know how to do it too?
I use Google Search on Puppy Forum
not an ideal solution though

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#5 Post by big_bass »

shankargopal


if you install ROX you'll have drag N drop power to make it very easy
I have used these many times on my slackware box

I even made a xfilemout package for slackware using ROX that works the same way puppy does by clicking on the files to mount them
if you are interested I 'll post it too



Code: Select all

#!/bin/sh
#call this mount_tool make it executable
#simple drag and drop mounting  tool for
#sfs iso or ext3 /ext2 files  (2fs 3fs in puppy) 
#just drag and drop  on this script
#they will get mounted with the correct name
#at /mnt
#script by Joe Arose ...big_bass



#SFS
if echo `basename "$1" ` | grep -q '.sfs$'; then
                  mkdir -p /mnt/`basename $1`
mount -t squashfs -o loop,ro  $1 /mnt/`basename $1`
cd /mnt/`basename $1`
 rox -d /mnt/`basename $1`   
				  
				  
#EXT2				  
elif echo `basename "$1" ` | grep -q '.2fs$'; then
                  mkdir -p /mnt/`basename $1`
mount -t ext2 -o loop,ro  $1 /mnt/`basename $1`
cd /mnt/`basename $1`
 rox -d /mnt/`basename $1`   
					  
#EXT3				  
elif echo `basename "$1" ` | grep -q '.3fs$'; then
                  mkdir -p /mnt/`basename $1`
mount -t ext3 -o loop,ro  $1 /mnt/`basename $1`
cd /mnt/`basename $1`
 rox -d /mnt/`basename $1`   
				 	  		  

#ISO				  
elif echo `basename "$1" ` | grep -q '.iso$'; then
                  mkdir -p /mnt/`basename $1`
mount -t iso9600 -o loop,ro  $1 /mnt/`basename $1`
cd /mnt/`basename $1`
 rox -d /mnt/`basename $1`   
				  fi	  		  				  


Code: Select all


#!/bin/sh

#call this unmount_tool make it executeable
#simple drag and drop unmounting  tool for puppy files
#just drag and drop sfs files on this script
#the will get mounted with the correct name then unmounted
#at /mnt
#script by Joe Arose ...big_bass


#to be used together with  the "mount_tool" script 
#simple drag and drop mounting  tool for
#sfs iso or ext3 /ext2 files  (2fs 3fs in puppy) 



mkdir -p /mnt/`basename $@`
umount /mnt/`basename $@` 
rmdir /mnt/`basename $@`




User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#6 Post by sunburnt »

Simple and nice Big Bass, but specifying the partition type isn`t necessary.
Making your script much simplier, this does the mounting part:

Code: Select all

[ ! `echo $1 |sed 's/^.*\.//' |egrep '(sfs|ext2|ext3|iso)'`]&& exit
BN=`basename $1`
mkdir -p /mnt/$BN
mount -o loop /dev/$BN /mnt/$BN
[ $? -eq 0 ]&& rox /mnt/$BN

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#7 Post by big_bass »

sunburnt


thanks I am always looking for some snippet to save a few steps
and I was able to play a bit and make an "emergency mount tool"
nothing fancy just works with rox and Xdialog
when you just set up a box

BN=`basename $1`
mkdir -p /mnt/$BN
mount -o loop /dev/$BN /mnt/$BN
[ $? -eq 0 ]&& rox /mnt/$BN
that partial snippet above works well for drives




I went about making a list another way this works

Code: Select all

fdisk -l  |egrep '(hd|sd)'| cut -f 1 -d ' ' | egrep -v 'Disk'| sed 's/\/dev\///'>/tmp/list_of_drives


this part isnt a stand alone working code it is an example of
what will change

with edit
going right to the devices listed instead of a drag N drop
when using drives

Code: Select all

BN="`cat /tmp/list_of_drives`"
mkdir -p /mnt/$BN
mount -o loop /dev/$BN /mnt/$BN
[ $? -eq 0 ]&& rox /mnt/$BN





I played with it using Xdialog with rox this is a working final script

Code: Select all

#!/bin/bash

rm -f /tmp/list_of_drives_viewer   
rm -f /tmp/list_of_drives 
rm -f /tmp/body_list_of_drives_viewer   
rm -f /tmp/selected


#----------------------------------------
# generate  /tmp/list_of_drives 
#----------------------------------------

fdisk -l  |egrep '(hd|sd)'| cut -f 1 -d ' ' | egrep -v 'Disk'| sed 's/\/dev\///'>/tmp/list_of_drives
#cat /tmp/list_of_drives 



#----------------------------------------
#generate /tmp/body_list_of_drives_viewer   
#----------------------------------------      
for i in  `cat /tmp/list_of_drives` ; do
        echo \"$i\" \" \" OFF "\\">> /tmp/body_list_of_drives_viewer   
done  



#----------------------------------------
#   generate  list_of_drives_viewer 
#----------------------------------------

#head of the viewer  (static)

cat << 'EOF' > /tmp/list_of_drives_viewer 
#!/bin/sh
Xdialog --title " Drive you want to mount " \
--backtitle "Mount drives   "     \
--checklist "Select a drive to mount on /mnt       \
                                             \n"  0 0 0  \
EOF


#body of the viewer (dynamic)
                                             
#build the list                                            
echo "`cat /tmp/body_list_of_drives_viewer`">> /tmp/list_of_drives_viewer 
echo '2> /tmp/selected'>> /tmp/list_of_drives_viewer



chmod a+x /tmp/list_of_drives_viewer
    
 
#----------------------------------------
# run the new "mounter" 
#/tmp/list_of_drives_viewer
#----------------------------------------
   
. /tmp/list_of_drives_viewer


MOUNT_THIS="`cat /tmp/selected`"
mkdir -p /mnt/$MOUNT_THIS
mount -o loop /dev/$MOUNT_THIS /mnt/$MOUNT_THIS
[ $? -eq 0 ]&& rox /mnt/$MOUNT_THIS






Joe
Attachments
image_mounter.png
(14.64 KiB) Downloaded 760 times
Last edited by big_bass on Wed 14 Apr 2010, 16:32, edited 2 times in total.

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#8 Post by sunburnt »

The " isn`t needed when assigning a string to a variable, only when you use it.
This preserves the "new lines" when it`s used with echo. echo "$BN" > (file)

Code: Select all

BN="`cat /tmp/list_of_drives`"		"
BN=`cat /tmp/list_of_drives`		
AND... Better than using "cat":

Code: Select all

BN=$(</tmp/list_of_drives)
Here`s a pix of my DriveMan app. I still need to fix a few items to finish it.
Attachments
000_DM_newest.png
No buttons now... But getting the popup menu right is a trick.
(5.54 KiB) Downloaded 774 times

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#9 Post by big_bass »

Hey sunburnt

Code: Select all

BN=$(</tmp/list_of_drives)
thanks again

No buttons now... But getting the popup menu right is a trick.
I didn't see your code but anyway a tip

I discovered using Xdialog on different operating systems with different gtk themes the sizes go crazy when you preset values

it looks perfect on one system and borked on another

one trick is assign 0 0 0 when three values are needed and 0 0 when only two values are needed I noticed that on slackware everything I wrote in Xdialog works fine but on slax everything was cut
but I fixed it by changing to zeros for sizes


I recompiled xdialog with a patch I posted it in the src2pkg thread
a couple of days ago give it a try
http://www.murga-linux.com/puppy/viewto ... 7&start=30
Joe

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#10 Post by sunburnt »

big_bass; I have a couple of text files of shell code scraps and some gtkDialog code.
If you`d like them I`ll PM them to you... T. 8)

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#11 Post by big_bass »

sunburnt
big_bass; I have a couple of text files of shell code scraps and some gtkDialog code.
If you`d like them I`ll PM them to you... T.
yeah, I can check it out
thanks




this script is what the guy asked for just a simple command line script
that you could run on any distro to auto mount all drives
if you were in the console you could call it and auto load all the available drives on /mnt


Code: Select all

#!/bin/sh

#call it  mount_all
# auto_mount all drives 
# now if you  have to mount stuff from the command line only  here's  one 
# unmount in /mnt     manually 
# don't unmount a drive that has your save file !
# you have to "refresh"  to see the updated mounts in ROX
# this doesnt depend on xdialog or rox 

fdisk -l  |egrep '(hd|sd)'| cut -f 1 -d ' ' | egrep -v 'Disk'| sed 's/\/dev\///'>/tmp/list_of_drives

for drive in  `cat /tmp/list_of_drives` ; do
 
mkdir -p /mnt/$drive
mount -o loop /dev/$drive /mnt/$drive
[ $? -eq 0 ] 
            
done  



Joe

Post Reply