Skip to content

Instantly share code, notes, and snippets.

@eklex
Last active December 12, 2024 04:34
Show Gist options
  • Save eklex/c5fac345de5be9d9bc420510617c86b5 to your computer and use it in GitHub Desktop.
Save eklex/c5fac345de5be9d9bc420510617c86b5 to your computer and use it in GitHub Desktop.
udev rule for Home Assistant OS (hassio) to mount USB drives into the Supervisor Media directory
#
# udev rule
# Mount USB drive to the media directory using the partition name as mount point
#
# Description:
# Created for Home Assistant OS, this rule mounts any USB drives
# into the Hassio media directory (/mnt/data/supervisor/media).
# When a USB drive is connected to the board, the rule creates one directory
# per partition under the media directory. The newly created partition is named
# as the partition name. If the partition does not have a name, then the following
# name format is used: "usb-{block-name}" where the block name is sd[a-z][0-9].
#
# Note 1:
# The rule name is always prefixed with a number. In this case, the rule uses 80.
# This represents the order of the rule when multiple rules exists in udev.
# Low numbers run first, high numbers run last. However, low numbers do not have all
# the facilities than high numbers may have.
# For this rule to run properly, use numbers equal or greater than 80.
#
# Note 2:
# This rule will skip mounting the 'CONFIG' USB key.
# https://github.com/home-assistant/operating-system/blob/dev/Documentation/configuration.md
#
# Note 3:
# This rule will mount the OS partitions if the OS is sorted on a USB drive (i.e. USB booting).
# To prevent this issue from happening, update the rule to skip the booting USB drive.
# See the CAUTION message below.
#
# Source of inspiration:
# https://www.axllent.org/docs/auto-mounting-usb-storage/
#
# Useful links:
# https://wiki.archlinux.org/index.php/Udev
#
# udev commands:
# - Restart udev to reload new rules:
# udevadm control --reload-rules
# - List device attributes of sdb1:
# udevadm info --attribute-walk --name=/dev/sdb1
# - List environment variables of sdb1:
# udevadm info /dev/sdb1
# - Trigger add/remove event for sdb1:
# udevadm trigger --verbose --action=add --sysname-match=sdb1
# udevadm trigger --verbose --action=remove --sysname-match=sdb1
#
# Filter on block devices, exit otherwise
# CAUTION: Change to 'sd[b-z][0-9]' if booting from a USB drive (e.g.: sda)
KERNEL!="sd[a-z][0-9]", GOTO="abort_rule"
# Skip none USB devices (e.g.: internal SATA drive)
ENV{ID_PATH}!="*-usb-*", GOTO="abort_rule"
# Import the partition info into the environment variables
IMPORT{program}="/usr/sbin/blkid -o udev -p %N"
# Exit if partition is not a filesystem
ENV{ID_FS_USAGE}!="filesystem", GOTO="abort_rule"
# Exit if this is the 'CONFIG' USB key
ENV{ID_FS_LABEL}=="CONFIG", GOTO="abort_rule"
# Get the partition name if present, otherwise create one
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
ENV{ID_FS_LABEL}=="", ENV{dir_name}="usb-%k"
# Determine the mount point
ENV{mount_point}="/mnt/data/supervisor/media/%E{dir_name}"
# Mount the device on 'add' action (a.k.a. plug the USB drive)
ACTION=="add", RUN{program}+="/usr/bin/mkdir -p %E{mount_point}", RUN{program}+="/usr/bin/systemd-mount --no-block --automount=no --collect $devnode %E{mount_point}"
# Umount the device on 'remove' action (a.k.a unplug or eject the USB drive)
ACTION=="remove", ENV{dir_name}!="", RUN{program}+="/usr/bin/systemd-umount %E{mount_point}", RUN{program}+="/usr/bin/rmdir %E{mount_point}"
# Exit
LABEL="abort_rule"
@hulkhaugen
Copy link

I've been using this with great success for a couple of years, but now I've messed it up somehow. The folder /media/usb-sdb1 is still there, but is now a folder on the internal hdd instead of the usb hdd and it's pretty much empty. I guess it's been re-generated by one of my add-ons that uses that folder. How can i troubleshoot this? I can see that the drive is still attached in my HA hw info, but it's apparently not mounted. I want to confirm if the rule is still present, and if something is preventing it from mounting, or if i just need to re-apply it...

Check this tutorial out, on how to access your root of Home Assistant to see if the drive is mounted. https://www.youtube.com/watch?v=CHvBzX41-Ag&t=507s

Sometimes HA is really strange when it comes to the media file system. Sometimes it places files in (root) /media, and sometimes in config/media.

Sometimes a reboot of the host helps. Also - if you are using samba, check if you can see your files through samba share.

Yes I have samba, and yes I can find that folder, but it's empty apart from some empty folders. I also have a storage sensor that used to say 4TB free space, now it says 68 GB free space, suggesting that the usb-sdb1 is mounted on the internal SSD instead of the external usb hdd. I have tried a reboot of the host, but no cigar. My plan next is to disable all add-ons that might be using the usb-sdb1 destination, and then delete that empty folder. Reboot the host and see if it mounts correctly. If that doesn't work, i'll try to apply the udev rule again I guess. I'm not at home and thus not able to access the server atm, but will be this weekend...

@hulkhaugen
Copy link

hulkhaugen commented Mar 3, 2023

Ok, so since my drive was no longer available, I've tried some things:

  1. Disabled all apps using the usb, and deleted the new empty usb-sdb1 folder on the internal drive. Rebooted. No usb-sdb1 or similar folders to be found after reboot.
  2. Setup SSH access to Home Assistant OS (Not Core) on port 22222. Found the 80-mount-usb-to-media-by-label.rules rule file at /etc/udev/rules.d and opened it up in vi to be sure it wasn't empty. Didn't read it end-to-end, but it looked familiar so I'm assuming it's working. Dunno if it has to be enabled or activated somehow?
  3. Disconnected the drive, and connected it to my Windows PC. It did show up in Diskmanager, but it's making some rather high clicking sounds every now and then. Assuming it's dead.
  4. To be sure, I installed Raspbian on a RPi i had lying around. Booted it up and viola, still the clicking sounds and a few errormeassages at boot related to sda1. However, my 5TB usb hdd was right there on the desktop. I tried playing some video files, no problems what-so-ever.

Sooo...what might be causing this drive to not mount on my HA setup?

EDIT: I connected it back, and sure, usb-sdb1 re-appeard in my media directory. I had to reboot the host to get the content back. But I think my HDD is dying, even if it's only 1 year old, and not very much i/o.

@sumnerma
Copy link

@jane-t Did you ever get your issue sorted? I'm having the same problem

@IngoEF
Copy link

IngoEF commented Jan 7, 2024

Hi,
this might have been a well usable solution, but for me it's not working (Raspberry3, HASSIO version 11.3 docker edition)
After creating the rule in /lib/udev/rules.d and trying the udevadm commands in CLI I was informed this way: zsh: command not found: udevadm. Additionally, the added file disappeared after the next reboot
What is going wrong here?

In the meantime here is some more which might be a solution but I didn't get it working.

@krzakx
Copy link

krzakx commented Oct 15, 2024

@jane-t Did you ever get your issue sorted? I'm having the same problem

so there are three of us....
I did check and when root into HAOS by ssh -p 2222 -> ls /mnt/data/supervisor/media/USBSTICK gaves me content however I do not see that content from Media dashboard in HomeAssistant.

Anybody would have any idea for that?

@microraptor
Copy link

I made an updated fork of this gist and the gist from zeehio:
https://gist.github.com/microraptor/be170ea642abeb937fc030175ae89c0c

It adds:

  • An option to mount only partitions with a specific label
  • An option to mount NTFS read/write as implemented by zeehio (uncomment that line only, if modinfo ntfs3 does not return an error)
  • 'hassos-data' partition won't be mounted
  • Some minor documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment