3

Here is how automatically mount a USB drive on boot. This way only works for one USB unless others are specified.

2 Answers 2

1

First create a new folder in /mnt . It needs to be called usb. Next, make a new file with leafpad and type inside it:

sudo mount /dev/sda1 /mnt/usb -o uid=pi,gid=pi

Save it as Mount.sh on desktop. Then open terminal and type:

sudo chmod +x /home/pi/Desktop/mount.sh

Then type:

sudo nano /etc/rc.local

At the bottom of the file before exit 0, type:

/home/pi/Desktop/Mount.sh

Reboot and the USB drive inserted will be mounted at /mnt/usb.

This only works for one USB drive inserted. It does not work if you have not rebooted with your drive plugged in. To see if your drive has been mounted or not, goto /mnt/usb. Any data on the USB will be there.

There are many ways to mount a USB. Other ways require the USB to be in the raspberry pi at all times. For my method, if a USB is not found, a line before boot will say Failed.

1

Here's a general script I provide that you can just tweak a bit for your own use-case to quickly configure automounting as a SystemD service (on a Pi or any other SystemD Linux host).

I specified the "exfat" for the "Type" directive in the .mount file, but obviously tweak this value to one that's appropriate for your USB storage format. The default mount period I've set is 300 seconds (5 min) in the "TimeoutIdleSec" directive in the .automount file's directive. This is arbitrary- set it for as long or short as you wish. Just cut-n-paste this snippet into a file and chmod 700 it and you're set.

Mount Naming: Be aware if you change my directory naming convention in the script that the SystemD mount file created should be named same as its mountpoint as specified in "Where" directive in the .mount file or the automount will fail.

Service Enabling: You'll note int he script that although there is a .mount file, I only enable the automount service. The automount file references the .mount file created by the script when raising the mount, but that's it. Only the automount service need be enabled.

Extending the script to ALL 4 USB Ports: I've given you (2) automounts so can see the pattern for creating additional mounts. Be sure and change the "What" directive in the .mount file to "/dev/sdc1" and "/dev/sdd1" for the 3rd & 4th mounts when copying and pasting.

HOW SystemD AUTOMOUNTING WORKS: When an access is attempted on the automounted drive, SystemD kicks-in and mounts it for the period specified the automount file's directive "TimeoutIdleSec". If no further accesses are made within that period the drive is unmounted. No hard-coded fstab entries are required. Also note that until something attempts an access, the automounted drives will not appear in the output of the mount command.

HTH- Terrence

#!/bin/bash

mkdir /media/automount1

chmod 775 /media/automount1
chown pi:pi /media/automount1

# SystemD mount file created below should be named same as its mountpoint as specified in *Where* directive below:
cat <<EOF> /etc/systemd/system/media-automount1.mount
[Unit]
Description=Create automount1 mount for USB storage for videos and images
Requires=local-fs.target
After=local-fs.target

[Mount]
What=/dev/sda1
Where=/media/automount1
Type=exfat
Options=defaults

[Install]
WantedBy=multi-user.target

EOF

chmod 644 /etc/systemd/system/media-automount1.mount


# NOTE: SystemD automount file created below should be named same as its mountpoint as specified in *Where* directive below:
cat <<EOF> /etc/systemd/system/media-automount1.automount
[Unit]
Description=Automount USB storage mount for videos and images on /media/automount1
Requires=local-fs.target
After=local-fs.target

[Automount]
Where=/media/automount1
DirectoryMode=0755
TimeoutIdleSec=300

[Install]
WantedBy=multi-user.target

EOF

chmod 644 /etc/systemd/system/media-automount1.automount

systemctl disable media-automount1.mount
systemctl enable media-automount1.automount

systemctl daemon-reload

systemctl start media-automount1.automount



mkdir /media/automount2

chmod 775 /media/automount2
chown pi:pi /media/automount2

# SystemD mount file created below should be named same as its mountpoint as specified in *Where* directive below:
cat <<EOF> /etc/systemd/system/media-automount2.mount
[Unit]
Description=Create automount2 mount for USB storage for videos and images
Requires=local-fs.target
After=local-fs.target

[Mount]
What=/dev/sdb1
Where=/media/automount2
Type=exfat
Options=defaults

[Install]
WantedBy=multi-user.target

EOF

chmod 644 /etc/systemd/system/media-automount2.mount


# NOTE: SystemD automount file created below should be named same as its mountpoint as specified in *Where* directive below:
cat <<EOF> /etc/systemd/system/media-automount2.automount
[Unit]
Description=Automount USB storage mount for videos and images on /media/automount2
Requires=local-fs.target
After=local-fs.target

[Automount]
Where=/media/automount2
DirectoryMode=0755
TimeoutIdleSec=300

[Install]
WantedBy=multi-user.target

EOF

chmod 644 /etc/systemd/system/media-automount2.automount

systemctl disable media-automount2.mount
systemctl enable media-automount2.automount

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.