To automatically detect the UUID of the USB drive and mount it on insertion, you can modify your script and udev
rules to use the UUID instead of relying on device names like /dev/sda1
. Using the UUID ensures consistency, as UUIDs are unique and won't change, even if the device path (e.g., /dev/sda1
, /dev/sdb1
) changes.
Steps to Automatically Detect UUID and Mount:
1. Find the UUID of the USB Drive
First, identify the UUID of the USB drive manually to ensure the process works. Run the following command:
sudo blkid
This will list all available block devices and their UUIDs. Look for your USB drive (e.g., /dev/sda1
) and note its UUID.
Example output:
/dev/sda1: UUID="1234-5678" TYPE="vfat"
In this case, 1234-5678
is the UUID of the USB device.
2. Modify the Mount Script to Automatically Detect UUID
You can modify the script to automatically detect the USB device's UUID and mount it.
- Edit the script:
sudo nano /usr/local/bin/usb-mount.sh
  2. Replace the content with the following:
#!/bin/bash
LOG_FILE="/var/log/usb-mount.log"
MOUNT_POINT="/media/myusb"
echo "[$(date)] USB mount script started" >> $LOG_FILE
# Create the mount point if it doesn't exist
if [ ! -d "$MOUNT_POINT" ]; then
sudo mkdir -p "$MOUNT_POINT"
echo "[$(date)] Created mount point: $MOUNT_POINT" >> $LOG_FILE
fi
# Find the UUID of the USB device (you can also filter by filesystem type if needed)
UUID=$(blkid -t TYPE=vfat -o value -s UUID)
if [ -n "$UUID" ]; then
DEVICE=$(blkid -t UUID="$UUID" -o device)
echo "[$(date)] Detected device with UUID: $UUID, device: $DEVICE" >> $LOG_FILE
# Mount the device if it's not already mounted
if ! mountpoint -q "$MOUNT_POINT"; then
sudo mount UUID="$UUID" "$MOUNT_POINT"
echo "[$(date)] Mounted UUID=$UUID at $MOUNT_POINT" >> $LOG_FILE
else
echo "[$(date)] $MOUNT_POINT is already mounted" >> $LOG_FILE
fi
else
echo "[$(date)] No USB device found" >> $LOG_FILE
fi
#!/bin/bash
LOG_FILE="/var/log/usb-mount.log"
MOUNT_BASE="/home/pi"
echo "[$(date)] USB mount script started" >> $LOG_FILE
# Find all block devices with a valid UUID (supports multiple devices)
for UUID in $(blkid -t TYPE=vfat -o value -s UUID) $(blkid -t TYPE=ext4 -o value -s UUID) $(blkid -t TYPE=ntfs -o value -s UUID)
do
# Get the corresponding device for this UUID
DEVICE=$(blkid -t UUID="$UUID" -o device)
MOUNT_POINT="$MOUNT_BASE/pendrive"
# Create the mount point if it doesn't exist
if [ ! -d "$MOUNT_POINT" ]; then
sudo mkdir -p "$MOUNT_POINT"
echo "[$(date)] Created mount point: $MOUNT_POINT" >> $LOG_FILE
fi
# Mount the device if it's not already mounted
if ! mountpoint -q "$MOUNT_POINT"; then
sudo mount UUID="$UUID" "$MOUNT_POINT"
echo "[$(date)] Mounted UUID=$UUID at $MOUNT_POINT" >> $LOG_FILE
else
echo "[$(date)] $MOUNT_POINT is already mounted" >> $LOG_FILE
fi
done
Using LABEL
#!/bin/bash
LOG_FILE="/var/log/usb-mount.log"
MOUNT_BASE="/home/kali/PycharmProjects"
echo "[$(date)] USB mount script started" >> $LOG_FILE
# Find all block devices with a valid LABEL (supports multiple devices)
for LABEL in $(blkid -t TYPE=vfat -o value -s LABEL) $(blkid -t TYPE=ext4 -o value -s LABEL) $(blkid -t TYPE=ntfs -o value -s LABEL)
do
# Get the corresponding device for this LABEL
DEVICE=$(blkid -t LABEL="$LABEL" -o device)
MOUNT_POINT="$MOUNT_BASE/pendrive"
# Create the mount point if it doesn't exist
if [ ! -d "$MOUNT_POINT" ]; then
sudo mkdir -p "$MOUNT_POINT"
echo "[$(date)] Created mount point: $MOUNT_POINT" >> $LOG_FILE
fi
# Mount the device if it's not already mounted
if ! mountpoint -q "$MOUNT_POINT"; then
sudo mount "$DEVICE" "$MOUNT_POINT"
echo "[$(date)] Mounted LABEL=$LABEL at $MOUNT_POINT" >> $LOG_FILE
else
echo "[$(date)] $MOUNT_POINT is already mounted" >> $LOG_FILE
fi
done
Explanation:
- The script automatically detects the UUID of any attached USB drive using
blkid
. - It attempts to mount the device by UUID to
/media/myusb
if it is not already mounted. - Logs are written to
/var/log/usb-mount.log
for troubleshooting.
 3. Make the Script Executable:
  Â
sudo chmod +x /usr/local/bin/usb-mount.sh
Â
Fixing the udev
Rule
The current rule may be too aggressive and gets triggered during every block device initialization (including system partitions, swap devices, etc.). Here’s how to refine the rule to prevent issues:
3. Set Up the udev Rule for Automatic Mounting
 Now, set up the udev
rule so that the system automatically triggers the mount script when a USB     drive  is inserted.
-
Edit/Create the
udev
rule:
sudo nano /etc/udev/rules.d/99-usb-mount.rules
4. Use a Safer Service Trigger
If you prefer using a systemd service instead of running the script directly from udev
, use a more specific trigger, like adding a delay to avoid conflicts during boot:
-
Updated udev Rule for Delayed Service Start:
ACTION=="add", SUBSYSTEM=="block", ENV{ID_FS_UUID}=="*", RUN+="/bin/sleep 5; /bin/systemctl start usb-mount.service"
Reload udev
rules:
After saving the new rule, reload udev
rules to apply changes:
sudo udevadm control --reload-rules
sudo udevadm trigger
5. Enable the Service at Boot
Let’s ensure the systemd service is properly configured. Open the service file:
sudo nano /etc/systemd/system/usb-mount.service
Ensure it has the following structure:
[Unit]
Description=Auto-mount USB drives
After=multi-user.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/usb-mount.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
Notice the change from simple
to oneshot
. Since the mount script doesn’t need to run continuously, this might work better for your use case.
After editing the service file, reload systemd:
sudo systemctl daemon-reload
Manually Test the Service
Run the service manually to check if the script works via systemd:
Make sure the service is enabled to run at boot
sudo systemctl enable usb-mount.service
Check the service status again to see if it worked:
sudo systemctl status usb-mount.service
Check Permissions on the Mount Point
Sometimes, the files may be mounted, but they are not visible due to permission issues.
-
Check the ownership and permissions of the mount point:
    Â
ls -ld /media/myusb
Ensure that your user has read and write permissions. If permissions are not set correctly, you won’t be able to see or access the files.
2 Fix the permissions if necessary:
sudo chmod 777 /media/myusb
sudo chmod 777 /media
This will give full read, write, and execute permissions to everyone for the /media/myusb
folder.
Check if files are actually present by forcing a list with root access:
  Â
sudo ls -la /media/myusb
-
This will list all files, even hidden ones, and will bypass any permission issues you might be facing with your normal user account.
4. Verify Filesystem Type and Mount Options
If the USB drive uses a special filesystem (like NTFS or exFAT), you need to make sure the proper filesystem drivers are installed and used for mounting.
-
Check the filesystem type:
sudo blkid /dev/sda1
If the drive is using exFAT
, you’ll need to install exfat-fuse
and exfat-utils
:
sudo apt install exfat-fuse exfat-utils
For NTFS
drives, you may need the ntfs-3g
package:
sudo apt install ntfs-3g
Conclusion
#!/bin/bash
LOG_FILE="/var/log/usb-mount.log"
MOUNT_BASE="/home/pi"
echo "[$(date)] USB unmount script started" >> $LOG_FILE
# Unmount all currently mounted devices in the MOUNT_BASE directory
for MOUNT_POINT in $MOUNT_BASE/*; do
# Check if it's a mount point
if mountpoint -q "$MOUNT_POINT"; then
# Get the LABEL of the mounted device
LABEL=$(blkid -o value -s LABEL "$MOUNT_POINT")
# Unmount the device
sudo umount "$MOUNT_POINT"
echo "[$(date)] Unmounted LABEL=$LABEL from $MOUNT_POINT" >> $LOG_FILE
# Remove the mount point directory if it exists and is empty
if [ -d "$MOUNT_POINT" ] && [ -z "$(ls -A "$MOUNT_POINT")" ]; then
sudo rmdir "$MOUNT_POINT"
echo "[$(date)] Removed empty mount point: $MOUNT_POINT" >> $LOG_FILE
fi
else
echo "[$(date)] $MOUNT_POINT is not a mount point" >> $LOG_FILE
fi
done
echo "[$(date)] USB mount script started" >> $LOG_FILE
# Find all block devices with a valid LABEL (supports multiple devices)
for LABEL in $(blkid -t TYPE=vfat -o value -s LABEL) $(blkid -t TYPE=ext4 -o value -s LABEL) $(blkid -t TYPE=ntfs -o value -s LABEL)
do
# Skip rootfs and boot
if [[ "$LABEL" == "rootfs" || "$LABEL" == "boot" ]]; then
echo "[$(date)] Skipped mounting system partition LABEL=$LABEL" >> $LOG_FILE
continue
fi
# Get the corresponding device for this LABEL
DEVICE=$(blkid -t LABEL="$LABEL" -o device)
MOUNT_POINT="$MOUNT_BASE/$LABEL"
# Create the mount point if it doesn't exist
if [ ! -d "$MOUNT_POINT" ]; then
sudo mkdir -p "$MOUNT_POINT"
sudo chmod 777 "$MOUNT_POINT"
echo "[$(date)] Created mount point: $MOUNT_POINT" >> $LOG_FILE
fi
# Mount the device if it's not already mounted
if ! mountpoint -q "$MOUNT_POINT"; then
sudo mount LABEL="$LABEL" "$MOUNT_POINT"
echo "[$(date)] Mounted LABEL=$LABEL at $MOUNT_POINT" >> $LOG_FILE
else
echo "[$(date)] $MOUNT_POINT is already mounted" >> $LOG_FILE
fi
done
This approach automatically detects the USB device's UUID when plugged in and mounts it to a fixed mount point. The use of UUIDs ensures the device is reliably mounted even if the device path changes.
Comments