FreeBSD Desktop – Part 17 – Automount Removable Media

In this article in the FreeBSD Desktop series I will introduce various methods to automatically (or not) mount external/removable devices such as USB or eSATA disks/pendrives or SD/microSD flash cards.

You may also check earlier articles of the FreeBSD Desktop series:

One of the FreeBSD daemons is devd(8) – the device state change daemon that provides a way to have userland programs run when certain kernel events happen. Such events are when new block device appears/disappears from the /dev directory.

For this problem about 2013 I created a devd(8) based solution – the automount daemon. Its workflow is very simple. When new ada/da/mmcsd* device appears then it tries to detect the filesystems on these partitions/slices and mount them. It you remove such device (device disappears) then it forcefully unmounts it and cleans used mountpoint from the /media directory.

It is available on GitHub - https://github.com/vermaden/automount - and in the FreeBSD Ports as sysutils/automount port. You can also install it as pkg(8) package. Its 2018 and various other solutions appeared in the meantime. One is the included in the FreeBSD base system autofs(8)/automount(8)
subsystem.

The other one is dsbmd/dsbmc – a media mounting daemon/automounter for FreeBSD with optional graphical GTK+ frontend. There is also FreeBSD base system solution called autofs(8) which I was not able to make work.

Below I will try to compare all their features, strengths and weaknesses.

  • sysutils/automount
    + can customize mount options for each filesystem type
    + written in POSIX /bin/sh script – easy to modify
    + can open selected file manager when device is inserted
    + provides log file
    - does not support MTP devices (Android phones for example)
    - does not support BTRFS or XFS (can be added)
  • sysutils/dsbmd
    + can customize mount options for each filesystem type
    + supports MTP devices (Android phones for example)
    + provides graphical fronted in GTK+ toolkit
    + can open selected file manager when device is inserted
    + does support BTRFS or XFS
    + provides log file
    - sometimes hangs at 100% cpu usage
  • autofs(8)
    + supports MTP devices (Android phones for example)
    + is available in the base system
    - does not allow custom mount options per filesystem
    - does not provide log file
    - does not support BTRFS or XFS (can be added)
    - does not work πŸ™‚

Up to this day I used mine sysutils/automount exclusively as removable devices automount solution. For Android phones I used simple-mtpfs command within xterm(1) terminal. I tried sysutils/dsbmd along with sysutils/dsbmc-cli and GTK+ graphical sysutils/dsbmd frontend and I really liked it but it somtimes fails me with 100% cpu usage and requires restarting. That does not happen withΒ sysutils/automount which just works so I will stick to it but I will provide an UPDATE later with results after longer period of testing the sysutils/dsbmd daemon.

sysutils/automount

First I will describe daemon that I used since 2013. The sysutils/automount solution. Its installation and setup is very easy, just add the automount package using pkg(8). Nothing more is needed as the pkg(8) will restart devd(8) after adding new configuration files.

# pkg install automount

One of the nice features of sysutils/automount is /var/log/automount.log log file which has all details about mounted filesystems.

% tail /var/log/automount.log 
2018-10-08 12:18:45 /dev/da0s1: mount (fat)
2018-10-08 12:19:23 /dev/da0: detach
2018-10-08 12:19:23 /dev/da0: mount point '/media/da0' removed
2018-10-08 12:19:23 /dev/da0s1: detach
2018-10-08 12:19:23 /dev/da0s1: mount point '/media/da0s1' removed
2018-10-09 11:38:14 /dev/da0: random wait for '0.1' seconds before 'attach' action
2018-10-09 11:38:14 /dev/da0: attach
2018-10-09 11:38:14 /dev/da0: mount (exfat)
2018-10-09 11:44:02 /dev/da0: detach
2018-10-09 11:44:02 /dev/da0: mount point '/media/da0' removed

The sysutils/automount comes with /usr/local/etc/automount.conf configuration file which has these options on my box.

% cat /usr/local/etc/automount.conf
USERUMOUNT=YES
ATIME=NO
REMOVEDIRS=YES
FM="caja --browser --no-desktop"
USER=vermaden
ENCODING=pl_PL.UTF-8
CODEPAGE=cp852

Besides not supporting (yet) MTP devices it just works. It is also very simple solution and being written in POSIX /bin/sh script it is very easy to modify it to one’s needs.

sysutils/dsbmd

The second option is the sysutils/dsbmd solution. Check the links below for more detailed description of this automounting daemon.

To install the dsbmd/dsbmc/dsbmc-cli trio just use pkg(8).

# pkg install dsbmc-cli dsbmc dsbmd

To configure dsbmd/dsbmc/dsbmc-cli trio these steps are needed.

1. Add dsbmd_enable=YES to your /etc/rc.conf file.

2. Install additional filesystems support depending on your needs:

  • fusefs-exfat
  • fusefs-gphotofs
  • fusefs-ntfs
  • fusefs-simple-mtpfs (MTP)
  • fusefs-ext2
  • fusefs-hfsfuse
  • fusefs-lkl

3. Add these lines below to your ~/.xinitrc file (or ~/.xsession).

# DSB AUTOMOUNTER
  dsbmc-cli -a &
  dsbmc &

You will then see the dsbmc icon in the system tray area.

dsbmc-tray

By default dsbmd will mount storage devices as regular user but You need to make sure that vfs.usermount=1 is in your /etc/sysctl.conf file and applied.

After I put the FAT32 or exFAT USB device it was automatically mounted.

/dev/da0 on /media/da0 (msdosfs, local, nosuid, mounted by vermaden)

The configuration file is available at /usr/local/etc/dsbmd.conf location. The dsbmd also comes with /var/log/dsbmd.log log file.

% tail /var/log/dsbmd.log
dsbmd: Killing blocking process 85421 ... on Thu Oct 11 16:48:10 2018
dsbmd: Sending SIGTERM to 85421 ... on Thu Oct 11 16:48:10 2018
dsbmd: Command /usr/local/sbin/mount.exfat ${DSBMD_DEVICE} "${DSBMD_MNTPT}" executed by UID 1000 failed with code 15: No error: 0 on Thu Oct 11 16:48:11 2018
dsbmd: Device /dev/da0 mounted on /media/GKPGE by UID 1000 on Thu Oct 11 16:48:12 2018
dsbmd: Device /dev/da0 unmounted from /media/GKPGE by UID 1000 on Thu Oct 11 16:49:09 2018
dsbmd: Device /dev/da0 mounted on /media/GKPGE by UID 1000 on Thu Oct 11 16:49:15 2018
dsbmd: Device /dev/da0 unmounted from /media/GKPGE by UID 1000 on Thu Oct 11 16:49:16 2018
dsbmd: Client with UID 1000 disconnected on Thu Oct 11 16:52:53 2018
dsbmd: Client with UID 1000 connected on Thu Oct 11 16:52:55 2018
dsbmd: Device /dev/da0 mounted on /media/GKPGE by UID 1000 on Thu Oct 11 16:58:22 2018

The dsbmd can also be configured with many useful options like mount(8) options for each filesystem type separately.

% grep _opts /usr/local/etc/dsbmd.conf | sed 's|\#\ ||g' | awk '{print $1}' | sort -u
cd9660_opts
exfat_opts
ext_opts
ext4_opts
msdosfs_opts
ntfs_opts
ufs_opts

The main GTK+ dsbmc window looks as follows.

dsbmc-prefs.jpg

You can also disable automatic mounting and can mount devices by hand with dsbmc tool.

dsbmc-mount.png

… or unmount them.

dsbmc-unmount.png

It is also a solution used in the NomadBSD portable distribution.

It sometimes fails with 100% cpu usage or with error message as this one:

dsbmc-error

Β 

autofs(8)

Last one is the FreeBSD base system solution.

To enable it one should do these steps:

  1. Add autofs_load=YES to your /boot/loader.conf file.
  2. Add autofs_enable=YES to your /etc/rc.conf file.
  3. Uncomment the /media -media -nosuid line in /etc/auto_master file.
  4. To have MTP support you will also have to install sysutils/fusefs-simple-mtpfs and add the /mtp -simple-mtpfs -allow_other line to the /etc/auto_master file. Along with fuse_load=YES in the /boot/loader.conf file.

After doing all these I attached USB device and Android phone but nothing happened.

I tried restarting the autofs(8) daemons but that did not helped, still zero reaction after inserting removable media.

# grep -r autofs_enable /etc
/etc/rc.d/automountd:rcvar="autofs_enable"
/etc/rc.d/autounmountd:rcvar="autofs_enable"
/etc/rc.d/automount:rcvar="autofs_enable"
/etc/defaults/rc.conf:autofs_enable="NO" # Run autofs daemons.
# /etc/rc.d/automountd onestop
# /etc/rc.d/autounmountd onestop
# /etc/rc.d/automount onestop
# /etc/rc.d/automountd onestart
# /etc/rc.d/autounmountd onestart
# /etc/rc.d/automount onestart

History

Historically was also sysutils/am-utils – the Berkeley Automounter and Suite of Utilities – and sysutils/automounter which provides scripts to dynamically configure amd(8) daemon.

EOF
Advertisements

One thought on “FreeBSD Desktop – Part 17 – Automount Removable Media

  1. Pingback: In Other BSDs for 2018/10/13 – DragonFly BSD Digest

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s