0

I've been having trouble with my external hard drive unmounting from my pi 3 torrentbox and then mounting as read-only with a different /dev/sd[letter]. I saw this blog post about how a user set up udev rules to automatically mount the new sd[letter] should it unmounts ( link to his blog entry).

1. Identify filesystem UUID and destination directory

Use some trick (gnome-disks on another system, listing /dev/disks/by-uuid as seen earlier) to find your filesystem's UUID. Let's say it's aaDREAMa-aaaa-aaaa-aaaa-aFILESYSaaaa Also prepare a directory to mount to. It should be empty. Let's say it's /media/dest

2. Create fstab entry - do not use automount

Edit /etc/fstab to contain all configuration related to your filesystem. It's not mandatory (you can specify the destination mount in the udev rule), but I think it's better to keep that configuration in a standard location. You can use something like this:

echo -e 'UUID=aaDREAMa-aaaa-aaaa-aaaa-aFILESYSaaaa /media/dest auto >defaults,noauto 0 0' | sudo tee -a /etc/fstab

3. Create udev rules to mount / unmount whenever the device is connected / disconnected

This is the fun part. Create a file in /lib/udev/rules.d. I chose /lib/udev/rules.d/99-automount-usb.rules to make sure it runs last (after the by-uuid symlink has been created), and put two actions in it - one will mount the device when connected, and another dismounts it when it's disconnected:

echo 'ACTION=="add", ENV{ID_FS_UUID_ENC}=="aaDREAMa-aaaa-aaaa-aaaa->aFILESYSaaaa", RUN+="/bin/mount /dev/%k"' | sudo tee '/lib/udev/rules.d/99->automount-usb.rules'
echo 'ACTION=="remove", ENV{ID_FS_UUID_ENC}=="aaDREAMa-aaaa-aaaa-aaaa->aFILESYSaaaa", RUN+="/bin/umount /dev/%k"' | sudo tee -a '/lib/udev/rules.d/99->automount-usb.rules'

4. Test

Apply the settings using

sudo udevadm control --reload-rules

Now stop any processes interacting with the device (like Deluge), disconnect and reconnect it, and note the output of:

mount | grep /media/dest;ls /media/dest

If everything works, it should either return nothing when the drive is unplugged, and something like this when it is:

/dev/sdb1 on /media/dest type ext4 (rw,relatime,data=ordered)
<Files inside the mobile drive's filesystem>

Will this still work on rpi 3? My current entry's format in #2's fstab goes something like:

UUID=aaDREAMa-aaaa-aaaa-aaaa-aFILESYSaaaa /mnt/torrents ext4 defaults,nobarrier 0 0

do i still include echo -e and | sudo tee -a /etc/fstab in the line?

How should #3's code look like when typing into a created rules file?

  • 2
    Isn't it better to try to find out why the external hard drive is unmounting? Kind of searching for the root cause, instead of solving the secondary cause? – MatsK Jan 15 '18 at 11:49
  • 2
    Agreed @MatsK - How is the drive being powered? If just by the RPi USB port, try powering the drive separately. A drive that is constantly being disconnected is likely to be damaged at some point. Re-mounting RO usually indicates you need to run fsck on it. – bobstro Jan 15 '18 at 14:31
  • The Hard drive is connected to a powered usb hub then from hub to RPi 3. It switches between sda1 and sdb1. I try to keep my active DLs to a minimum (2 or 3 only). The error includes cannot read block bitmap and journal being aborted and then remounting as read-only – Kurover Jan 15 '18 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.