RESOLVED: auto-mount a USB drive: works in terminal, vars not populating on start

I made a file inspired by balena-storage (thanks for that!). It works when I login to the container via the dashboard and run the script manually. It hangs with unpopulated vars when the container runs it when starting (a script that runs the script). I think it’s a permissions issue or script running script thing.

# Automatically mount a USB drive by specified volume name.
# Note: make sure to have USB_VOLUME_NAME set in env vars.
# Thanks:

echo "Checking for USB_VOLUME_NAME..."

echo "A"

if [[ -z $USB_VOLUME_NAME ]]; then
  echo "Make sure to set environment variable USB_VOLUME_NAME in order to find a connected USB drive by that label and connect to it. Exiting..." >> /usr/src/app/mount.log
  exit 1

echo "B"

# Get device by label env var set in dashboard device env vars
if [[ -z $USB_DEVICE ]]; then
  echo "Invalid USB_DEVICE name: $USB_DEVICE" >> /usr/src/app/mount.log
  exit 1


echo "C"

# Get extra device info
ID_FS_TYPE=${ID_FS_TYPE:=$(/bin/udevadm info -n $USB_DEVICE | /usr/bin/awk -F "=" '/ID_FS_TYPE/{ print $2 }')}
ID_FS_UUID_ENC=${ID_FS_UUID_ENC:=$(/bin/udevadm info -n $USB_DEVICE | /usr/bin/awk -F "=" '/ID_FS_UUID_ENC/{ print $2 }')}
ID_FS_LABEL_ENC=${ID_FS_LABEL_ENC:=$(/bin/udevadm info -n $USB_DEVICE | /usr/bin/awk -F "=" '/ID_FS_LABEL_ENC/{ print $2 }')}


echo $ID_FS_TYPE

echo "D"

# Bail if file system is not supported by the kernel
if ! /bin/grep -qw $ID_FS_TYPE /proc/filesystems; then
  echo "File system not supported: $ID_FS_TYPE" >> /usr/src/app/mount.log
  exit 1

echo "E"

# Mount device
if /bin/findmnt -rno SOURCE,TARGET $USB_DEVICE >/dev/null; then
    echo "Device $USB_DEVICE is already mounted!" >> /usr/src/mount.log
    echo "Mounting - Source: $USB_DEVICE - Destination: $MOUNT_POINT" >> /usr/src/app/mount.log
    /bin/mkdir -p $MOUNT_POINT
    /bin/mount -t $ID_FS_TYPE -o rw $USB_DEVICE $MOUNT_POINT

echo "F"

When the container runs the script, it gets stuck after “D”, with ID_FS_TYPE, ID_FS_UUID_ENC and ID_FS_LABEL_ENC being empty (a good reason to hang).


22.07.20 10:36:27 (-0600)  server  Checking for USB_VOLUME_NAME...
22.07.20 10:36:27 (-0600)  server  A
22.07.20 10:36:27 (-0600)  server  B
22.07.20 10:36:27 (-0600)  server  /dev/sda1
22.07.20 10:36:27 (-0600)  server  C
22.07.20 10:36:27 (-0600)  server  
22.07.20 10:36:27 (-0600)  server  
22.07.20 10:36:27 (-0600)  server  
22.07.20 10:36:27 (-0600)  server  /mnt/MYDRIVE
22.07.20 10:36:27 (-0600)  server  D

My dockerfile.template:

FROM balenalib/%%BALENA_MACHINE_NAME%%-node

# Enable udev for detection of dynamically plugged devices
COPY udev/usb.rules /etc/udev/rules.d/usb.rules

# Install dependencies
RUN install_packages util-linux

WORKDIR /usr/src/app

# Move scripts used for mounting USB
COPY scripts scripts
RUN chmod +x scripts/*


# server.js will run when container starts up on the device
CMD ["/bin/bash", "/usr/src/app/scripts/"]

echo "Mounting USB drive..."
cd /usr/src/app/scripts

echo "Starting server..."
cd /usr/src/app
/usr/local/bin/yarn run serve

Any ideas on what’s up? I’m so close and could use a professional opinion. :smiley:

As this wasn’t really related specifically, I also posted on StackOverflow. Here’s the answer that resolved issues (need to quote the variables):

Thanks for sharing !