Detailed Host-Side Script Approach
The host-side script approach separates privileged operations from the container application, maintaining security while providing the needed functionality.
Implementation Overview
This approach uses bind-mount volumes to share communication files between the container and host (1). The container writes unmount requests to a shared location, and a host-side service monitors for these requests and executes the privileged unmount operations.
Step 1: Create Shared Communication Directory
On the host, create a directory for communication:
# mkdir -p /home/torizon/usb-manager
# chown torizon:torizon /home/torizon/usb-manager
Step 2: Host-Side Monitoring Script
Create a script on the host that monitors for unmount requests:
#!/bin/bash
# /home/torizon/usb-manager/monitor-unmount.sh
WATCH_DIR="/home/torizon/usb-manager"
REQUEST_FILE="$WATCH_DIR/unmount-request"
RESPONSE_FILE="$WATCH_DIR/unmount-response"
while true; do
if [ -f "$REQUEST_FILE" ]; then
# Read the device to unmount
DEVICE=$(cat "$REQUEST_FILE")
# Perform the unmount operation
if sudo umount "$DEVICE" 2>/dev/null; then
echo "SUCCESS: $DEVICE unmounted" > "$RESPONSE_FILE"
else
echo "ERROR: Failed to unmount $DEVICE" > "$RESPONSE_FILE"
fi
# Remove the request file
rm "$REQUEST_FILE"
fi
sleep 1
done
Step 3: Docker Compose Configuration
Configure your container to access the shared directory using bind-mount (1):
services:
your-app:
image: your-image
volumes:
- "/home/torizon/usb-manager:/usb-manager"
- "/media:/media:ro" # Read-only access to mount points
user: "1000:1000" # Run as torizon user
(1)
Step 4: Container Application Code
In your container application, implement the unmount request:
import time
import os
def request_unmount(device_path):
request_file = "/usb-manager/unmount-request"
response_file = "/usb-manager/unmount-response"
# Write unmount request
with open(request_file, 'w') as f:
f.write(device_path)
# Wait for response
timeout = 10 # seconds
start_time = time.time()
while time.time() - start_time < timeout:
if os.path.exists(response_file):
with open(response_file, 'r') as f:
response = f.read().strip()
os.remove(response_file)
return response
time.sleep(0.1)
return "ERROR: Timeout waiting for unmount response"
# Usage example
result = request_unmount("/media/usb-drive")
print(result)
Step 5: Systemd Service for Monitoring
Create a systemd service to run the monitoring script automatically:
# /etc/systemd/system/usb-unmount-monitor.service
[Unit]
Description=USB Unmount Monitor Service
After=network.target
[Service]
Type=simple
User=torizon
ExecStart=/home/torizon/usb-manager/monitor-unmount.sh
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
Enable and start the service:
# sudo systemctl enable usb-unmount-monitor.service
# sudo systemctl start usb-unmount-monitor.service
Advantages of This Approach
- Security: Container runs with minimal privileges (2)
- Separation of Concerns: Privileged operations isolated to host
- Flexibility: Can extend to handle multiple USB management operations
- Monitoring: Easy to log and audit unmount operations
Alternative Communication Methods
Instead of file-based communication, you could also use:
- Named pipes (FIFOs): More efficient for real-time communication
- Unix domain sockets: Better for bidirectional communication
- Network sockets: If container and host need network-based communication
The file-based approach is simpler to implement and debug, making it ideal for USB unmount operations where real-time performance isn’t critical.
Best regards, Toradex AI Assistant.
Did you like my answer?

If I answered your question, please tick the “Solution” checkbox.
For follow-up questions to me, please use @ToradexAI in your post.