How to use an external HDD that's not permanently attached (eg PLEX movies drive)

Roberto24

New Member
Jul 23, 2024
17
1
3
Hi, I want to install a PLEX LXC for accessing movies and shows thtat are currently stored on a USB HDD.
I know how to passthrough a USB HDD, but I don't want to keep the USB drive attached to my homeserver all the time to watch a movie every now and then due to the energy consumption. Do I need to mount, unmount the USB drive each time or is there a simpler way to realize this?
What would happen if I keep the USB HDD mounted but simply detach it or power it off after exiting the Plex LXC and power it on again before opening Plex next time? Would this work fine or will it cause problems? The drive could be mounted read-only if that helps..

I read somewhere that AutoFS might be a good option, but I know nothing about it / how this needs to be set up.. Can anyone give me some advice?

Thank you !
 
Containers don't support hotplug (or unplug or replug) and your data might be written on the mountpoint instead of the drive by the container. Maybe write some (generic Linux) udev rules (on the Proxmox host) to detect your drive being added or removed and restart the container automatically? Or do some additional commands to it to inform the software inside?

EDIT: Here is an example rule that switches my display when a USB keyboard is unplugged: ACTION=="remove", ENV{ID_BUS}=="usb", ENV{ID_INPUT_KEYBOARD}=="1", RUN+="/usr/bin/ddcutil setvcp 60 x03"
 
Last edited:
I'm trying to create an automatic USB drive mount using udev rules.
I found the script and changed it a bit:
nano /usr/local/bin/auto-mount-usb.sh
Code:
#!/bin/bash

DEVICE="$1"

PARTITION_LABEL=$(lsblk -no LABEL "$DEVICE")
PARTITION_TYPE=$(lsblk -no FSTYPE "$DEVICE")

MOUNT_POINT="/mnt/usb/$PARTITION_LABEL"

ALLOWED_FS=("ext4" "ntfs" "xfs" "btrfs" "vfat")
SYSTEM_PARTITIONS=("boot" "root" "swap" "" "efi")
DISK_ID=("/dev/sda" "/dev/sda1" "/dev/sda2" "/dev/sda3" "/dev/sda4")

echo "$DEVICE $MOUNT_POINT $PARTITION_LABEL $PARTITION_TYPE"

if [[ " ${SYSTEM_PARTITIONS[@]} " =~ " ${PARTITION_LABEL,,} " ]] || \
   [[ " ${DISK_ID[@]} " =~ "$DEVICE" ]] || \
   [[ ! " ${ALLOWED_FS[@]} " =~ " ${PARTITION_TYPE} " ]]; then
    echo "Urządzenie $DEVICE jest prawdopodobnie partycją systemową lub ma nieobsługiwany system plików. Pominięto."
else
    mkdir -p "$MOUNT_POINT"
    mount "$DEVICE" "$MOUNT_POINT"
    chmod 777 "$MOUNT_POINT"

    echo "Urządzenie $DEVICE zamontowane w $MOUNT_POINT"
fi
chmod +x /usr/local/bin/auto-mount-usb.sh


udev rules: nano /etc/udev/rules.d/99-auto-mount-usb.rules

Code:
ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd[a-z][0-9]", RUN+="/usr/local/bin/auto-mount-usb.sh %k"
udevadm control --reload-rule
udevadm trgged



I don't know why it doesn't work - it creates a directory but doesn't mount.
If I manage /usr/local/bin/auto-mount-usb.sh /dev/sdb3,
the disk is installed.
When replug USB, there is only a directory.
I don't know anything about this - can anyone help me?

________
EDIT

I've created a new custom udev rule
Code:
ACTION=="add",SUBSYSTEM=="usb",RUN+="/usr/local/bin/mount_usb.sh"

and script to automatically mount all unmounted partitions (except system partitions) when a USB device is connected.
Code:
#!/bin/bash

LOGFILE="/home/log/udev-script.log"

# List of filesystem types that we want to mount
ALLOWED_FS=("ext4" "ntfs" "xfs" "btrfs" "vfat")
# List of names that may suggest system partitions
SYSTEM_PARTITIONS=("boot" "root" "swap" "" "efi")
# List of system disks (which we do not want to mount)
DISK_ID=("/dev/sda" "/dev/sda1" "/dev/sda2" "/dev/sda3" "/dev/sda4")

# Function to mount the device
mount_device() {
    DEVICE="$1"
    PARTITION_LABEL=$(lsblk -no LABEL "$DEVICE")
    PARTITION_TYPE=$(lsblk -no FSTYPE "$DEVICE")
    MOUNT_POINT="/mnt/usb/${PARTITION_LABEL:-unknown}"

    # Check if the device is not a system partition
    if [[ " ${SYSTEM_PARTITIONS[@]} " =~ " ${PARTITION_LABEL,,} " ]] || [[ ! " ${ALLOWED_FS[@]} " =~ " ${PARTITION_TYPE} " ]]; then
        echo "$(date) - partition $DEVICE - ignored" >> "$LOGFILE"
    else
        # Create the mount point
        mkdir -p "$MOUNT_POINT"
        echo "$(date) - mount $DEVICE in $MOUNT_POINT" >> "$LOGFILE"
        # Mount the device
        mount "$DEVICE" "$MOUNT_POINT"
    fi
}

# Get the list of partitions
devices=($(lsblk -lnpo NAME | grep 'sd'))

# Iterate over each partition
for device1 in "${devices[@]}"; do
  if [[ ! " ${DISK_ID[@]} " =~ " $device1 " ]]; then
    echo "Checking partition: $device1"
    # Check if the disk is already mounted
    if ! mount | grep -q "$device1"; then
      mount_device "$device1"
    else
      echo "$(date) - Device $device1 is already mounted." >> "$LOGFILE"
    fi
  fi
done

The script works as expected when run manually from the terminal. /usr/local/bin/mount_usb.sh
However, the automatic mounting doesn't trigger when a USB device is physically plugged in or unplugged - only create a directory.
 
Last edited:
Finally, it worked! Here's the final script.

Code:
#!/bin/bash

LOGFILE="/home/log/udev-script.log"

# Create mount directory
DEVICE="/dev/$1"

PARTITION_LABEL=$(lsblk -no LABEL "$DEVICE")
PARTITION_TYPE=$(lsblk -no FSTYPE "$DEVICE")
MOUNT_POINT="/mnt/usb/$PARTITION_LABEL"
# List of filesystem types that we want to mount
ALLOWED_FS=("ext4" "ntfs" "xfs" "btrfs" "vfat")
# List of names that may suggest system partitions
SYSTEM_PARTITIONS=("boot" "root" "swap" "" "efi")

DISK_ID=("/dev/sda" "/dev/sda1" "/dev/sda2" "/dev/sda3" "/dev/sda4")

# Checking if the device is not a system partition
if [[ " ${SYSTEM_PARTITIONS[@]} " =~ " ${PARTITION_LABEL,,} " ]] || [[ " ${DISK_ID[@]} " =~ "$DEVICE" ]] || [[ ! " ${ALLOWED_FS[@]} " =~ " ${PARTITION_TYPE} " ]]; then
    echo "$(date) - Device $DEVICE is likely a system partition or has an unsupported filesystem. Skipped." >> "$LOGFILE"
else
    # Create the mount point
    mkdir -p "$MOUNT_POINT"
    # Mount the device
    systemd-run mount "$DEVICE" "$MOUNT_POINT"
    echo "$(date) - Device $DEVICE mounted at $MOUNT_POINT" >> "$LOGFILE"
fi
 
Last edited:
  • Like
Reactions: Roberto24

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!