Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 27 Aug 2014, 13:23
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
multiuser environment: mounting devices from script
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [11 Posts]  
Author Message
shankargopal

Joined: 03 Dec 2005
Posts: 295

PostPosted: Sun 04 Apr 2010, 06:35    Post subject:  multiuser environment: mounting devices from script  

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 Smile ) that one can use setuid but apparently most distros now disallow setuid for scripts. So what does one do?

Thanks in advance!
Back to top
View user's profile Send private message 
Pizzasgood


Joined: 04 May 2005
Posts: 6270
Location: Knoxville, TN, USA

PostPosted: Wed 07 Apr 2010, 02:05    Post subject:  

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).
_________________
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

Back to top
View user's profile Send private message Visit poster's website 
shankargopal

Joined: 03 Dec 2005
Posts: 295

PostPosted: Wed 07 Apr 2010, 11:47    Post subject:  

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_hide_passwords_in_a_shell_script.html
Back to top
View user's profile Send private message 
nooby

Joined: 29 Jun 2008
Posts: 10557
Location: SwedenEurope

PostPosted: Wed 07 Apr 2010, 14:31    Post subject:  

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
Back to top
View user's profile Send private message 
big_bass

Joined: 13 Aug 2007
Posts: 1747

PostPosted: Wed 07 Apr 2010, 22:51    Post subject:  

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:

#!/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:


#!/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 $@`




_________________
debian wheezy ,linux mint, slackware I use them all and they all have good points
Mint would be best for general users though
Back to top
View user's profile Send private message 
sunburnt


Joined: 08 Jun 2005
Posts: 5016
Location: Arizona, U.S.A.

PostPosted: Thu 08 Apr 2010, 08:10    Post subject:  

Simple and nice Big Bass, but specifying the partition type isn`t necessary.
Making your script much simplier, this does the mounting part:
Code:
[ ! `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
Back to top
View user's profile Send private message 
big_bass

Joined: 13 Aug 2007
Posts: 1747

PostPosted: Fri 09 Apr 2010, 12:53    Post subject:  

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


Quote:
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:
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:

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:
#!/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
image_mounter.png
 Description   
 Filesize   14.64 KB
 Viewed   712 Time(s)

image_mounter.png


_________________
debian wheezy ,linux mint, slackware I use them all and they all have good points
Mint would be best for general users though

Last edited by big_bass on Wed 14 Apr 2010, 12:32; edited 2 times in total
Back to top
View user's profile Send private message 
sunburnt


Joined: 08 Jun 2005
Posts: 5016
Location: Arizona, U.S.A.

PostPosted: Fri 09 Apr 2010, 14:51    Post subject:  

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:
BN="`cat /tmp/list_of_drives`"      "
BN=`cat /tmp/list_of_drives`      

AND... Better than using "cat":
Code:
BN=$(</tmp/list_of_drives)

Here`s a pix of my DriveMan app. I still need to fix a few items to finish it.
000_DM_newest.png
 Description   No buttons now... But getting the popup menu right is a trick.
 Filesize   5.54 KB
 Viewed   729 Time(s)

000_DM_newest.png

Back to top
View user's profile Send private message 
big_bass

Joined: 13 Aug 2007
Posts: 1747

PostPosted: Fri 09 Apr 2010, 19:43    Post subject:  

Hey sunburnt
Code:

BN=$(</tmp/list_of_drives)

thanks again


Quote:
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/viewtopic.php?t=51197&start=30
Joe

_________________
debian wheezy ,linux mint, slackware I use them all and they all have good points
Mint would be best for general users though
Back to top
View user's profile Send private message 
sunburnt


Joined: 08 Jun 2005
Posts: 5016
Location: Arizona, U.S.A.

PostPosted: Fri 09 Apr 2010, 19:56    Post subject:  

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. Cool
Back to top
View user's profile Send private message 
big_bass

Joined: 13 Aug 2007
Posts: 1747

PostPosted: Sat 10 Apr 2010, 16:25    Post subject:  

sunburnt

Quote:
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:

#!/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

_________________
debian wheezy ,linux mint, slackware I use them all and they all have good points
Mint would be best for general users though
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [11 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0942s ][ Queries: 13 (0.0043s) ][ GZIP on ]