Author |
Message |
shankargopal
Joined: 03 Dec 2005 Posts: 295
|
Posted: 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 ) 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
|
|
 |
Pizzasgood

Joined: 04 May 2005 Posts: 6266 Location: Knoxville, TN, USA
|
Posted: 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
|
|
 |
shankargopal
Joined: 03 Dec 2005 Posts: 295
|
Posted: 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
|
|
 |
nooby
Joined: 29 Jun 2008 Posts: 10548 Location: SwedenEurope
|
Posted: 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
|
|
 |
big_bass
Joined: 13 Aug 2007 Posts: 1742
|
Posted: 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 $@`
|
|
Back to top
|
|
 |
sunburnt

Joined: 08 Jun 2005 Posts: 5087 Location: Arizona, U.S.A.
|
Posted: 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
|
|
 |
big_bass
Joined: 13 Aug 2007 Posts: 1742
|
Posted: 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
Description |
|
Filesize |
14.64 KB |
Viewed |
743 Time(s) |

|
Last edited by big_bass on Wed 14 Apr 2010, 12:32; edited 2 times in total
|
Back to top
|
|
 |
sunburnt

Joined: 08 Jun 2005 Posts: 5087 Location: Arizona, U.S.A.
|
Posted: 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.
Description |
No buttons now... But getting the popup menu right is a trick. |
Filesize |
5.54 KB |
Viewed |
758 Time(s) |

|
|
Back to top
|
|
 |
big_bass
Joined: 13 Aug 2007 Posts: 1742
|
Posted: 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
|
Back to top
|
|
 |
sunburnt

Joined: 08 Jun 2005 Posts: 5087 Location: Arizona, U.S.A.
|
Posted: 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.
|
Back to top
|
|
 |
big_bass
Joined: 13 Aug 2007 Posts: 1742
|
Posted: 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
|
Back to top
|
|
 |
|