# simple automount script



## sk8harddiefast (Dec 7, 2010)

Everytime I want to mount my usb on fluxbox, I run:

```
sudo mount_msdosfs /dev/da0s1 /mnt/usb
```
I want to create a simple script that automatically run this command everytime *da0s1* is detected on /dev
Any idea?


----------



## graudeejs (Dec 7, 2010)

For my GF I wrote these scripts (Integrated in fvwm)

Check if media is mounted/unmounted

```
#!/bin/sh
# check if there is device mounted to ~/mnt

if [ "`mount | grep -e "$HOME/mnt"`" ]; then
	which gxmessage > /dev/null 2> /dev/null && gxmessage -name 'MEDIA' -title 'INFO: Media mounted' -center -bg '#9ADF9C' -buttons '_Ok:0' -default 'Ok' 'Media mounted'
else
	which gxmessage > /dev/null 2> /dev/null && gxmessage -name 'MEDIA' -title 'INFO: Media not mounted' -center -bg '#DF9A9C' -buttons '_Ok:.' -default 'Ok' 'Media not mounted'
fi

# vim: set ts=4 sw=4:
```

And this mounts/umounts media

```
#!/bin/sh

# mount/umount media to ~/mnt and start file manager
# arg1 commant to launch after mount

if [ "`mount | grep -e "$HOME/mnt"`" ]; then
	while [ 1 -eq 1 ]; do
		umount "$HOME/mnt" \
			&& gxmessage -name 'MEDIA' -title 'INFO: Media unmounted' -center -bg '#9ADF9C' -buttons '_Ok:0' -default 'Ok' 'It is now safe to remove media' && exit \
			|| gxmessage -name 'MEDIA' -title 'ERROR: Media still mounted' -center -bg '#DF9A9C' -buttons '_Cancel:0,_Retry:1' -default 'Retry' 'Failed to umount media!!!' && exit
	done
else
	while [ 1 -eq 1 ]; do
		{	   mount -t msdosfs /dev/da0   "$HOME/mnt" \
			|| mount -t msdosfs /dev/da0s1 "$HOME/mnt" \
			|| mount -t msdosfs /dev/da0p1 "$HOME/mnt" \
			|| mount -t cd9660  /dev/acd0  "$HOME/mnt" \
			|| mount -t udf     /dev/acd0  "$HOME/mnt" \
			|| mount -t ufs     /dev/da0   "$HOME/mnt"
		} && { [ "$1" ] && $1; exit; }
		which gxmessage > /dev/null 2> /dev/null || exit
		which gxmessage > /dev/null 2> /dev/null && gxmessage -name 'MEDIA' -title 'ERROR: Media mount failed' -center -bg '#DF9A9C' -buttons '_Cancel:0,_Retry:1' -default 'Retry' 'Failed to mount media!!!' && exit
	done

fi

# vim: set ts=4 sw=4:
```

You can setup your PC, to avoid using sudo (I think it was in Handbook and it was mentioned in few threads on forum)


----------



## sk8harddiefast (Dec 7, 2010)

I think is /etc/devfs.rules.

```
[localrules=5] mode 0660 group operator
add path 'da*' mode 0660 group operator
add path 'cuse' mode 0660 group operator
add path 'video0' mode 0660 group operator
```
For cuse & video0 work. I open my webcam without sudo anymore. But didn't work for da


----------



## graudeejs (Dec 7, 2010)

Do you have

```
vfs.usermount=1
```
in /etc/sysctl.conf


----------



## sk8harddiefast (Dec 8, 2010)

Yeap


----------



## gordon@ (Dec 8, 2010)

Check out devd(8) and the corresponding devd.conf(5). It does exactly what you want: It watches the devfs tree for changes and let's you execute tasks as a result.


----------



## sk8harddiefast (Dec 8, 2010)

I will read them and then I will run killasmurf86's script (doing some changes like the path of mountpoint) and I will post the results


----------



## graudeejs (Dec 8, 2010)

Don't change mountpoint... user who want to mount FS without rising privileges must own mountpoint. That's why I mount under ~/mnt/
Maybe you're trying yo mount under /mnt/? That could explain your problem


----------



## sk8harddiefast (Dec 8, 2010)

You have right  Mount of my usb on */home/emberdaemon* run without sudo 
If I do:

```
chown -R emberdaemon /mnt
```
is not solving the "problem" from the time that /mnt will own to me?


----------



## graudeejs (Dec 8, 2010)

Ye... because / is not owned by emberdaemon

I guess, I formulated my sentence badly... you need to own mount-point and parent directory


----------



## sk8harddiefast (Dec 8, 2010)

Ok. Then I will mount it */home/emberdaemon/usb*.


----------



## sk8harddiefast (Dec 8, 2010)

From the first script change 
	
	



```
if [ "`mount | grep -e "$HOME/mnt"`" ]; then
```
 to 
	
	



```
if [ "`mount | grep -e "$HOME/emberdaedmon/usb"`" ]; then
```
After I run:

```
sudo chown -R emberdaemon script
sudo chmod +x script
./script
```
Return none results :/


----------



## graudeejs (Dec 8, 2010)

why *$HOME/emberdaedmon/usb* and not just *$HOME/usb*?
$HOME resolves to full path of homedir of current user
If your username is emberdaedmon, then $HOME=/home/emberdaedmon


----------



## sk8harddiefast (Dec 8, 2010)

I thought that and I tried this too. But also returned none result


----------



## graudeejs (Dec 8, 2010)

run it under the microscope and show me output
`$ sh -x mounter.sh`


----------



## sk8harddiefast (Dec 8, 2010)

sh -x script         

```
+ mount
+ grep -e /home/emberdaemon/usb
+ [ '' ]
+ which gxmessage
```


----------



## graudeejs (Dec 8, 2010)

Install x11/gmessage
and show `$ ls /dev/da*`


----------



## sk8harddiefast (Dec 8, 2010)

Ok. gmessame installed.
*ls /dev/da**

```
/dev/da0   /dev/da0s1
```


----------



## graudeejs (Dec 8, 2010)

on /dev/da0s1 script only checks msdosfs, because it's common that way 

WHen I format my drives for UFS (For example) I make sure ufs is on entire device (*newfs -U /dev/da0* for example)

So either adjust script, or format your drive, like I do....
Otherwise it should work


----------



## vermaden (Dec 8, 2010)

killasmurf86 said:
			
		

> For my GF I wrote these scripts (Integrated in fvwm)



Check ZENITY mate, it gives a lot more possibilities in 'hiding' scripting under the GUI with very little work.


----------



## lme@ (Dec 9, 2010)

You should also add this:

```
[localrules=5]
add path 'msdosfs/*' mode 0660 group operator
```

So you can use [CMD=">"]mount -t msdosfs /dev/msdosfs/foo /media/bar[/CMD]


----------



## sk8harddiefast (Dec 12, 2010)

How to adjust my script? I have none idea about bash but seems to be the commands of console with some variables and I guess should be easy to learn it


----------



## graudeejs (Dec 12, 2010)

Where did you see me mentioning bash? Or where in script did you see it's a bash?
It's not BASH. it's *sh*, *sh*, [red]*SH*[/red]!

Here's resource to learn it:
http://www.grymoire.com/Unix/Sh.html


----------



## jalla (Dec 12, 2010)

Have you considered using amd(8)()?
It's mostly more flexible/extensible than the other methods discussed here, with the added bonus of automatically unmount'ing inactive drives.

To set up amd, create a suitable map in /etc/amd.removable


```
/defaults    opts:=intr,nosuid  
cd0          opts:=ro;type:=cdfs;dev:=/dev/cd0
cd1          opts:=ro;type:=cdfs;dev:=/dev/cd1
# usb devs with fatXX partitions are usually whole disk
da0          opts:=rw;type:=pcfs;dev:=/dev/da0
da1          opts:=rw;type:=pcfs;dev:=/dev/da1
# but sometimes just the first slice (extend as necessary)
da0s1        opts:=rw;type:=pcfs;dev:=/dev/da0s1
da1s1        opts:=rw;type:=pcfs;dev:=/dev/da1s1
```

create/edit /etc/amd.conf with this


```
[global]
auto_dir = /a
cache_duration = 60   
log_file = /var/log/amd
 
[/r]
map_type = file
map_name = amd.removable
```
 

edit /etc/rc.conf like this


```
amd_enable="YES" 
amd_flags="-F /etc/amd.removable"
```
 

Various devices can then be accessed like /r/da0/, etc, and automatically unmounted after 60 seconds of inactivity


----------



## graudeejs (Dec 12, 2010)

I you ask me, then No....
Personally I don't want auto-anything


----------



## jalla (Dec 12, 2010)

killasmurf86 said:
			
		

> I you ask me, then No....
> Personally I don't want auto-anything



amd does nothing until you deliberately access the device. It's not like it does anything behind your back.

And unlike a specialized script, amd is easily configured to handle nfs exported filesystems, ufs snapshots, etc,


----------



## lme@ (Dec 13, 2010)

You might also give sysutils/automounter a try.


----------



## sk8harddiefast (Dec 13, 2010)

Guess that automounter have relation with jalla's first post?
I installed sysutils/automounter and made this:
My /etc/amd.removable

```
/defaults    opts:=intr,nosuid  
cd0          opts:=ro;type:=cdfs;dev:=/dev/cd0
# usb devs with fatXX partitions are usually whole disk
da0          opts:=rw;type:=pcfs;dev:=/dev/da0
da1          opts:=rw;type:=pcfs;dev:=/dev/da1
# but sometimes just the first slice (extend as necessary)
da0s1        opts:=rw;type:=pcfs;dev:=/dev/da0s1
da1s1        opts:=rw;type:=pcfs;dev:=/dev/da1s1
```

My /etc/amd.conf

```
[global]
auto_dir = /a
cache_duration = 60   
log_file = /var/log/amd
 
[/r]
map_type = file
map_name = amd.removable
```

My /etc/rc.conf

```
amd_enable="YES" 
amd_flags="-F /etc/amd.removable"
```

What is wrong? My usb shouldn't now be seen on /media? Is da0s1


----------



## jalla (Dec 13, 2010)

Forget about sysutils/automounter for now.
With amd running see if you can access the device with, say, `ls /r/da0s1/`
(the trailing '/' is significant)

I'm not familiar with automounter, but if you want to use it you must at least disable amd and set automounter_enable="YES" in /etc/rc.conf. See the man page for details


----------



## sk8harddiefast (Dec 13, 2010)

ls /dev/da0*

```
/dev/da0   /dev/da0s1
```

but there is none folder /r on / or on /media


----------



## jalla (Dec 13, 2010)

Is amd running? Check /var/log/amd for any problems.

Also the output from amq(8)() should show something like this

```
snapper:/h/tl# amq
/       root    "root"              
/net    toplvl  /etc/amd.net        /net
/snap   toplvl  /etc/amd.snap       /snap
/r      toplvl  /etc/amd.removable  /r
```


----------



## sk8harddiefast (Dec 13, 2010)

Thereis no /var/log/amd
How to check if amd is running?


----------



## lme@ (Dec 13, 2010)

sk8harddiefast said:
			
		

> Guess that automounter have relation with jalla's first post?
> I installed sysutils/automounter and made this:
> My /etc/amd.removable
> 
> ...



With automounter you don't need to change any config file.
Use a stock amd.map, don't start amd(8) manually, just add 
	
	



```
automounter_enable="YES"
```
 to /etc/rc.conf.
Then insert the USB device, go to /media/msdosfs/$label and the device is mounted automatically. Some seconds after the last access to a file in /media/msdosfs/$label is made, the device gets unmounted.


----------



## jalla (Dec 13, 2010)

Check with

`pgrep amd`

amq will also tell you if amd is running or not

```
snapper:/h/tl# amq
amq: localhost: RPC: Program not registered
```

To start it

`/etc/rc.d/amd start`


----------



## sk8harddiefast (Dec 13, 2010)

Ok. Solved with automounter! Now fluxbox automount usb


----------



## jalla (Dec 13, 2010)

lme@ said:
			
		

> You might also give sysutils/automounter a try.



I read the manpages and it's not clear to me how (or even if it's possible) to incorporate different types of mounts with automounter. Say I'm running amd like this

`amd /r /etc/amd.r /snap /etc/amd.snap /net /etc/amd.net`

to automount local removable drives, any local ufs snapshots, and any available nfs filesystem respectively.

Can automounter handle this?
If so, what should the amd.map look like?


----------

