# USB automount and hal daemon



## melgo (Dec 13, 2008)

Hello,

I'm new to FreeBSD and on a fresh 7.1 install.
I'm using Openbox as a WM and Thunar-volman to automount USB drives. My problem : when I connect an USB drive, nothing happens.

If I "lshal" after my drive is inserted, hal daemon isn't running anymore.

Relevant dmesg :

```
da0 at umass-sim0 bus 0 target 0 lun 0
da0: <Verbatim Store 'n' Go PB 1.00> Removable Direct Access SCSI-2 device 
da0: 40.000MB/s transfers
da0: 1927MB (3948543 512 byte sectors: 255H 63S/T 245C)
GEOM_LABEL: Label for provider da0s1 is msdosfs/\^_\M^K\^H.
pid 4477 (hald-addon-mouse-sy), uid 0: exited on signal 11 (core dumped)
umass0: at uhub4 port 1 (addr 2) disconnected
```

hald verbose :

```
17:55:27.603 [I] hf-usb.c:686: received USB_EVENT_DEVICE_ATTACH event, device 4.2
17:55:27.605 [I] hald.c:108: Added device to GDL; udi=/org/freedesktop/Hal/devices/usb_device_a16_1200_2470a7457bf79e
17:55:27.607 [I] hald.c:108: Added device to GDL; udi=/org/freedesktop/Hal/devices/usb_device_a16_1200_2470a7457bf79e_if0
17:55:27.677 [I] device.c:1894: Removing locks from ':1.35'
17:55:27.745 [I] device.c:1894: Removing locks from ':1.36'

** (process:4535): CRITICAL **: hf_devd_process_event: assertion `event != NULL' failed
hald-addon-storage info: Checking whether device /dev/acd0 is locked by HAL
hald-addon-storage info: ... device /dev/acd0 is locked by HAL
pid 4540: rc=0 signaled=1: /usr/local/libexec/hald-addon-mouse-sysmouse
```

/etc/fstab :

```
Device		Mountpoint	FStype	Options		Dump	Pass#
/dev/ad0s1b		none		swap	sw		0	0
/dev/ad0s1a		/		ufs	rw		1	1
/dev/ad0s1e		/tmp		ufs	rw		2	2
/dev/ad0s1f		/usr		ufs	rw		2	2
/dev/ad0s1d		/var		ufs	rw		2	2
#/dev/acd0		/cdrom		cd9660	ro,noauto	0	0
proc			/proc		procfs	rw		0	0
```

/etc/rc.conf : dbus_enable="YES" and hald_enable="YES"

Any idea ?
Thanks


----------



## CdK1 (Dec 16, 2008)

JI, look this http://desktopbsd.net/wiki/doku.php?id=doc:usb4bsd

and configure [root@Reina-Tonia /]# nano usr/local/etc/PolicyKit/PolicyKit.conf

similar to:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE pkconfig PUBLIC "-//freedesktop//DTD PolicyKit Configuration 
1.0//EN"
"http://hal.freedesktop.org/releases/PolicyKit/1.0/config.dtd">

<!-- See the manual page PolicyKit.conf(5) for file format -->

<config version="0.1">
<match user="root">
<return result="yes"/>
</match>
<define_admin_auth group="wheel"/>
<match action="org.freedesktop.hal.storage.mount-removable">
<match user="CdK1">
<return result="yes"/>
</match>
</match>
</config>


----------



## SirDice (Dec 16, 2008)

IIRC you can also just add the user to the operator group. You can follow a few of the hints and tips from the gnome/hal faq. Especially the user mount bits and perhaps the cd-burner tips.

Do remember that it isn't hal that's doing the (auto) mounting. In GNOME the mounting is taken care of by nautilus, not sure what openbox uses.


----------



## anemos (Dec 17, 2008)

Why don't you use the way described in Lucas' book?

Add the following

```
attach 10 {
    match "device-name" "umass0";
    action "/home/automount.sh";
};
```

to /etc/devd.conf where /home/automount.sh is:

```
#!/bin/sh
sleep 2
usr/bin/true > /dev/da0
/sbin/mount -t msdosfs /dev/da0 /media
```

Of course /dev/daX may be different to your system and, hey, don't forget to 
	
	



```
chmod 755 automount.sh
```
This way, USB drives are mounted automatically. Note that you have to unmount the drive by your self. _Most of the times_ it works fine for me.
Cheers


----------



## hedwards (Dec 17, 2008)

anemos said:
			
		

> Of course /dev/daX may be different to your system and, hey, don't forget to
> 
> 
> 
> ...


In practice, that only works under certain circumstances and definitely not if you care about where it gets mounted. The problem I had quite a while back was getting the device mounted to the correct directory.


```
camcontrol devlist | awk '$1 ~ /\<SanDisk/ {print $11}' \
| sed -Ee 's/(\(|\)|\,)/ /g' | awk '{print $2 "s1"}'
```
Something like that's going to help a lot in nailing down the device to the correct name and ultimately to a consistent directory. But as far as I know that can't really be done in devd.conf.

I know this is rather more complicated than what the OP was looking for, but it seems like something to consider. And I suppose that one could inject that into the automount.sh as well.


----------



## anemos (Dec 17, 2008)

> In practice, that only works under certain circumstances and definitely not if you care about where it gets mounted. The problem I had quite a while back was getting the device mounted to the correct directory.


Actually, sometimes it doesn't work at all. Do not know why though (yet). It's just a tip and it can be much improved. 
What do you mean by saying the correct directory?
The thing is that you know the exact directory but sometimes you don't know the device.


----------



## kamikaze (Dec 19, 2008)

Well there's still the option to do it without HAL. The port *sysutils/automounter* installs a script that can be hooked into devd. It dynamically configures the automounter daemon (amd), so that you're going to be able to access your stick under /media/msdosfs/<label>.

The only requirement for it to work is that the partitions on the media are labeled.


----------



## skepsi (Dec 19, 2008)

Not sure if this will be useful to the original poster, but I recently had trouble with HAL/Xfce4/Thunar refusing to mount 2 USB drives, which had previously worked fine. I discovered that a different drive (all were Memorex TravelDrives) mounted just fine. I decided to reformat them in Windows XP, changing their names. That seems to have fixed the issue.

It may simply be a coincidence, but the drives that stopped working were the only two drives I have ever used on a Vista machine. I plan to test soon whether Vista is doing something to the drives (I kinda doubt it, but it's the only thing I can think of).


----------



## hedwards (Dec 22, 2008)

anemos said:
			
		

> Actually, sometimes it doesn't work at all. Do not know why though (yet). It's just a tip and it can be much improved.
> What do you mean by saying the correct directory?
> The thing is that you know the exact directory but sometimes you don't know the device.


I'm not sure correct is necessarily the right word, but I like to have my backup drive mounted to /backup and for pretty much everything else to be mounted to the /mnt /mnt2 etc.

And if you put logic similar to that into the mount script you can do that sort of thing. Of course it is a personal preference.

I'll often times just create a script out of that for the backup and then use that to call the backup program as well. But it's not really ideal and there's probably a better way. But it does work and that's good enough.


----------



## bigboss (Jan 2, 2009)

*auth_admin directive always return false*

Hi there guys.
I was trying to configure policykit to work with the following PolicyKit.conf file:



```
<config version="0.1">
    <match user="root">
        <return result="yes"/>
    </match>
     <define_admin_auth group="operator"/>
     <match action="org.freedesktop.hal.storage.mount-removable">
             <return result="auth_admin"/>
     </match>

</config>
```

However the auth_admin directive doesn't work. I am in the operator and wheel group.

Here is the output from hald:


```
using action org.freedesktop.hal.storage.mount-removable for uid 1001, system_bus_connection :1.6
pid 3553: rc=1 signaled=0: /usr/local/libexec/hal-storage-mount
17:50:29.494 [I] hald_dbus.c:4042: No more methods in queue
17:50:29.494 [I] hald_dbus.c:4105: failed with 'org.freedesktop.Hal.Device.PermissionDeniedByPolicy' 'org.freedesktop.hal.storage.mount-removable auth_admin <-- (action, result)'
17:50:29.496 [I] device.c:1894: Removing locks from ':1.34'
```


I want to associate it with a group because I am not the only user of my computer.

Right now I am using the following PolicyKit.conf and it works fine:

```
<config version="0.1">
    <match user="root">
        <return result="yes"/>
    </match>
     <define_admin_auth group="operator"/>
     <match action="org.freedesktop.hal.storage.mount-removable">
        <match user="israel">
             <return result="yes"/>
        </match>
     </match>

</config>
```


Anyone know why the auth_admin option doesn't work ?


----------



## lme@ (Jan 6, 2009)

You can try this (taken from PC-BSD)


```
<config version="0.1"> 
        <define_admin_auth group="operator"/> 
        <match action="org.freedesktop.hal.storage.mount-removable"> 
                <return result="yes"/> 
        </match> 
        <match action="org.freedesktop.hal.storage.mount-fixed"> 
                <return result="yes"/> 
        </match> 
        <match action="org.freedesktop.hal.storage.eject"> 
                <return result="yes"/> 
        </match> 
</config>
```


----------



## bigboss (Feb 24, 2009)

*Thanks lme@*

That did the job.
Maybe this should be the default file in the package, but with the wheel group instead, for a tighter security ?
No worry use the operator group, but on a multi-user server enterprise machine I don't know if this can pose as a security risk.


----------



## Mel_Flynn (Feb 25, 2009)

Policykit adds an extra administration layer upon existing, tested and tried unix permissions, but has no concept of the "groups" construct.
If you have multiple users on a machine wanting to mount removable media, you will need to OR these:

```
<match user="husband|wife">
    <return result="yes" />
</match>
```

The PC-BSD way gets around this, but note that any user on the system can now mount devices. This might not be acceptable, when we add 'kid' to the above example.


----------



## bigboss (Jun 13, 2009)

*Secure config*

But according to the man page, this should work I think...


```
<config version="0.1">
        <define_admin_auth group="wheel"/>
        <match action="org.freedesktop.hal.storage.mount-removable">
                <return result="auth_admin_keep_always"/>
        </match>
        <match action="org.freedesktop.hal.storage.mount-fixed">
                <return result="auth_admin_keep_always"/>
        </match>
        <match action="org.freedesktop.hal.storage.eject">
                <return result="auth_admin_keep_always"/>
        </match>
</config>
```

Right now I am using

```
<config version="0.1">
        <define_admin_auth group="wheel"/>
        <match action="org.freedesktop.hal.storage.mount-removable">
          <match user="husband|wife">
            <return result="yes"/>
          </match>

        </match>
        <match action="org.freedesktop.hal.storage.mount-fixed">
          <match user="husband|wife">
                 <return result="yes"/>
          </match>
        </match>
        <match action="org.freedesktop.hal.storage.eject">
          <match user="husband|wife">
                 <return result="yes"/>
          </match>
        </match>
</config>
```

This was not acceptable in my case, since I use an external USB drive for backups and my Kid does use the computer once in a while.

Mel_Flynn, in this case, I am confused. I saw that in practice what you said is true, it has no concept of the "groups" construct, but in the manual(FreeBSD 7.2) it says that define_admin_auth directive supports both user or group definition.

Is the man page wrong ? Or didn't I understand it correctly.

Thanks a lot Mel_Flynn


----------



## alphazo (Sep 12, 2009)

Hello,

On a stock FreeBSD 8.0 beta 4 I installed Xfce and thunar-volman however I have some trouble automounting devices:
* USB drives formated with FAT do not mount automatically. However I can mount them manually with the mount -t msdosfs command
* I had a spare UFS formatted USB drive and it did mount automatically on the desktop.

Any reason why UFS drives would mount and not FAT ones?

I tried the troubleshooting found at http://thunar.xfce.org/documentation/C/using-removable-media.html but it didn't help.

Thank you
Alphazo


----------



## JohnLeclerc (Sep 16, 2009)

I actually got *automounting* configured and working correctly on FreeBSD pcbsd 7.2-PRERELEASE. I did not need to install the "automounter" tools port. *amd* and *amq* where already there. It works by treating mountable devices (properly configured in *fstab*) as *NFS* volumes, so it relies on *portmapper*, and is limited to configurations supported by fstab (which is usually a static file). It will automatically mount AND DISMOUNT devices (when not in use). The mount is still a local, hard mount though, so removing it will probably still cause a nasty system reset (I will not test that . An NFS virtual mount disappearing will not cause a crash, but it is still mounted normally for use by the pipe and will cause a crash there, so you still have to check what's mounted from the shell before pulling anything (file browsers are often the culprit keeping things mounted- Thunar is bad- try dolphin. *hald* can also make problems), but it definitely saves steps and time. I had some unexplained hiccups getting it to work where nothing I did had any effect. A few hours later it worked perfectly and continues. Cannot say why. 

*amd* dynamically creates mount points in */home*, but only while devices are mounted. Unfortunately you must attempt to access an unmounted device in order to auto-mount it, though the mount point will not exist. The work around is to create symlinks (ex:*ln -s /home/flash /media/flash*) to the non-existant mount points, which will suddenly work if auto-mount finds a device and is able to mount it via *fstab*. You are limited to connecting USB (or anything dynamically allocated in */dev*) devices with differing formats to the order supported by whatever *fstab* file you can come up with (it is possible to give the same device several entries in *fstab* to allow different device numbers in */dev*. For simple configurations this is fine, but probably not for complex, changing ones.


I had to add the following to */etc/rc.conf*:

```
#automounting stuff
portmap_enable=YES
portmap_flags="-h 127.0.0.1"
amd_enable=YES
amd_flags="-a /.amd_mnt -c 10 -w 2 -l syslog /host /etc/amd.map"
```

create */etc/amd.map*:

```
# to reload this file: amq -f
# $FreeBSD: src/etc/amd.map,v 1.10 2006/11/06 01:42:11 obrien Exp $
#
/defaults       type:=host;fs:=${autodir}/${rhost}/host;rhost:=${key}
*               opts:=rw,grpid,resvport,vers=3,proto=tcp,nosuid,nodev


localhost               type:=auto;fs:=${map};pref:=${key}/

localhost/flash 	type:=program;fs:=/media/flash;\
			mount:="/sbin/mount mount /media/flash";\
			unmount:="/sbin/umount umount /media/flash"

localhost/cdrom         type:=program;fs:=/media/cdrom;\
                        mount:="/sbin/mount mount /media/cdrom";\
                        unmount:="/sbin/umount umount /media/cdrom"

localhost/reader 	type:=program;fs:=/media/reader;\
			mount:="/sbin/mount mount /media/reader";\
			unmount:="/sbin/umount umount /media/reader"
			
			
localhost/floppy        type:=program;fs:=/media/floppy;\
                        mount:="/sbin/mount mount /media/floppy";\
                        unmount:="/sbin/umount umount /media/floppy"
```


and */etc/amd.conf*:

```
[ global ]
restart_mounts = yes
unmount_on_exit= yes
```

add a little protection to /etc/hosts.allow:

```
portmap: localhost : allow
portmap: ALL : severity auth.warning : deny
```



ref:
http://renaud.waldura.com/doc/freebsd/automounting.txt

http://shellshore.com/content/freebsd-setting-usb-and-cddvd-auto-mount


----------



## JohnLeclerc (Sep 16, 2009)

*correction*

*amd* mounts appear in */host*, not /home


----------



## JohnLeclerc (Sep 16, 2009)

*automount details...*

There are some tricky things about the mount points I forgot I had configured. 

The symlinks should NOT replace your normal mounts as I described. Your mounts (ex: /media/cdrom) must be left as they are to allow normal mounting and fstab to work normally. amd will hang and cause all kinds of nasty directory errors if local mount points are *symlinks back to /host*. I instead created the symlinks in */media/auto* directory.

The virtual auto-mounts appear in */host/localhost/*, but only for 2 seconds after an access to allow quick removals without crashing (the "2" can be changed in the amd_flags line in rc.conf). The virtual mounts created in */host/localhost* are themselves symlinks back to the regular fstab mount points. Since the non-existant symlinks in /host/localhost must be accessed to trigger auto-mounting, I created symlinks to those folders in */media/auto*. Paradoxically, these are symlinks to non-existant locations until I access the location. Ultimately you have: media/auto/flash --> /host/localhost/flash --> /media/flash. I then only access a flash device through */media/auto/flash*. 

All this does is pipe file access through */host* to keep things temporarily mounted. Your regular fstab entries are mounting and umounting very quickly, many times. Serious problems occur if mount points used by amd are symlinks back to /host. MANY directory functions will hang across the whole system. 

So long as no shell commands, hald or silly filebrowsers like Thunar are keeping files open, things are automatically dismounted and can be disconnected very quickly. Unfortunately (except for cdrom's which lock the door when mounted) you *must* still check from the shell that something is dismounted before disconnecting it or you risk system failure.


----------



## lme@ (Sep 17, 2009)

@JohnLeclerc: sysutils/automounter does exactly the same.


----------



## alphazo (Sep 22, 2009)

Hi lme@,

Following your recommendation I installed automounter, enabled it rc.conf and added the few required lines in devd.conf (as mentioned in the man page). Guess what? My FAT formated USB drive now shows up in XFCE4 (maybe with the help of thunar-volman plugin) and under 8.0RC1.

Thanks again for this tip. I was about to try using AMD based on the long post found above and this HOWTO http://networking.ringofsaturn.com/Unix/freebsdautomount.php but to be honest I much prefer the automounter way.

Cheers
Alphazo


----------



## rnejdl (Dec 4, 2009)

*Updating online docs*

I'm the author of that page.   It was the best I could get working at the time but I do agree it was not elegant. I'm testing out the automounter port now and based on my results, I'll update the page with the newer info.

If ever you guys have feedback on those pages, please let me know.

Thanks!
Rusty Nejdl
http://networking.ringofsaturn.com


----------

