reinis
Posts: 3
Joined: Fri Mar 11, 2016 12:34 pm

USB Auto mount - Raspbian Stretch Lite [Solved]

Fri Feb 09, 2018 6:40 pm

Hi all,

I have a Raspberry PI 2 running the newest version of Raspbian Stretch Lite in Command Line environment. Out of the box my external USB hard/flash drives are not auto-mounted when plugged into the Raspberry Pi.

The ultimate goal is to have any USB drive to auto-mounting when plugged into the Raspberry PI 2. If it is not possible to achieve this for all drives, then as a worst case scenario I would be ok to auto-mount specific HDDs by specifying their UUIDs.

I have been looking into this for hours, but I could not find a solid and simple solution online. I would truly appreciate if someone could please post a working solution in here. Moreover, below you can see the output of my sudo blkid. In this case I want the USB hdd assigned to /dev/sda1 to be auto-mounted when I plug it into my Raspberry pi 2.

Code: Select all

[email protected]:~ $ sudo blkid
/dev/mmcblk0p1: LABEL="boot" UUID="CDD4-B453" TYPE="vfat" PARTUUID="896c5641-01"
/dev/mmcblk0p2: LABEL="rootfs" UUID="72bfc10d-73ec-4d9e-a54a-1cc507ee7ed2" TYPE="ext4" PARTUUID="896c5641-02"
/dev/mmcblk0: PTUUID="896c5641" PTTYPE="dos"
/dev/sda1: UUID="93ddc2e0-48e1-457c-9d59-f6a34596e6e1" TYPE="ext4" PARTLABEL="SSD-120GB" PARTUUID="9eea6391-2399-4983-889c-d5a651c73952"
PS I find it very inconvenient that such functionality is not built into Raspbian out of the box for the Command Line environment (I know that auto-mount works when in desktop environment).

Thank you!

Best regards,
Reinis
Last edited by reinis on Sat Feb 10, 2018 6:46 am, edited 1 time in total.

User avatar
thagrol
Posts: 698
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: USB HDD Auto Mount - Raspbian Stretch

Sat Feb 10, 2018 12:11 am

reinis wrote:
Fri Feb 09, 2018 6:40 pm

PS I find it very inconvenient that such functionality is not built into Raspbian out of the box for the Command Line environment (I know that auto-mount works when in desktop environment).
Functionality is there it just takes a little setting up. First thing to do is read up on fstab and mount. Try:

Code: Select all

man fstab
man mount
then

Code: Select all

cat /etc/fstab

Taking your current /dev/sda1 as an example, you'd do something like this:
  1. Create a mount point:

    Code: Select all

    sudo mkdir /media/SSD-120GB
  2. Backup your existing /etc/fstab e.g.:

    Code: Select all

    sudoi cp /etc/fstab /etc/fstab.bak
  3. Open /etc/fstab in your favourite text editor e.g.:

    Code: Select all

    sudo nano /etc/fstab
  4. Add a line similar to this, tweaking mount options and mount point as required:

    Code: Select all

    UUID=93ddc2e0-48e1-457c-9d59-f6a34596e6e1 /media/SSD-120GB ext4 defaults,nofail 0 0
    Use UUID rather than the device node (/dev/sda1) as the device node can change across reboots especially if drives are added or removed.
    Include "nofail" in the mount options to prevent the Pi hanging if it is booted without the drive attached.
  5. Save and close /etc/fstab
  6. Connect the HDD and reboot
Repeat for as many drives/partitions as you need but be aware that they each need their own mount point.

The above method won't automount if you hot plug a drive but the easy way around that is either

Code: Select all

sudo mount /path/to/mount-point
or

Code: Select all

sudo mount -a
It might be possible to mount a hot plugged drive with systemd and/or udev but I' don't know enough to help you with that. Disconnecting a USB drive without first unmounting it is definitely not a good idea.

Edit:
A quick google for "udev mount" returned this: https://www.axllent.org/docs/view/auto- ... b-storage/ No idea if it actually wokrs though.
Note to self: don't feed the trolls
If you believe "L'enfer, c'est les autres" (Hell is other people) have you considered that it may be of your own making?

reinis
Posts: 3
Joined: Fri Mar 11, 2016 12:34 pm

Re: USB HDD Auto Mount - Raspbian Stretch

Sat Feb 10, 2018 6:45 am

Thanks a lot for the input. The problem is that I tried fstab 10x times but it did not work for me. I tried it with UUID and LABEL - still would not mount the drive on boot. It returns exit code error 32. A quick google search show why - it seems that raspberry pi 2 simply does not manage to detect the usb drive before fstab is executed. You can read about it [ur=viewtopic.php?f=28&t=99491l]here[/url] and here.

The good news is that I found a very simple alternative (at least in terms of installation, not sure about how "native" the solution is) - a package called usbmount. I can confirm that with usbmount I can auto-mount on boot and hot-plug any USB device in Raspbian Stretch Lite on my Raspberry Pi 2!

Installation steps of usbmount:

1. Install the package:

Code: Select all

sudo apt-get install usbmount
2. Make sure it works in Stretch by changing MountFlags=slave to MountFlags=shared here:

Code: Select all

sudo nano /lib/systemd/system/systemd-udevd.service
3. Reboot and it works!

Best,
Reinis

spectre-nz
Posts: 2
Joined: Sat Nov 25, 2017 9:43 am

Re: USB Auto mount - Raspbian Stretch Lite [Solved]

Sat Apr 07, 2018 4:22 am

Great thanks!
Worked for me on a Pi 3 Model B+ after it stopped recognising USB drives.

yuvarajoo
Posts: 2
Joined: Sat Apr 28, 2018 12:35 pm

Re: USB Auto mount - Raspbian Stretch Lite [Solved]

Sun Apr 29, 2018 6:22 am

Thanks. It solved for me as well... :D
Anyway maybe anyone have native solution can advise as well...

donbrew
Posts: 45
Joined: Sun Sep 04, 2016 2:32 pm

Re: USB Auto mount - Raspbian Stretch Lite [Solved]

Sun Jun 17, 2018 8:56 pm

The problem with using fstab is it won't boot if the drive is not plugged in first. The system gets stuck looking for it.

My bet is you forgot the 0 0 at the end of the fstab line.

usbmount seems to be the native fix on Lite. A lot of packages need to be installed, like samba, nfs, ntfs-3g etc.

User avatar
thagrol
Posts: 698
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: USB Auto mount - Raspbian Stretch Lite [Solved]

Sun Jun 17, 2018 10:22 pm

donbrew wrote:
Sun Jun 17, 2018 8:56 pm
The problem with using fstab is it won't boot if the drive is not plugged in first. The system gets stuck looking for it.
There's an easy fix for that: add "nofail" to the mount options in your fstab entry.
Note to self: don't feed the trolls
If you believe "L'enfer, c'est les autres" (Hell is other people) have you considered that it may be of your own making?

Return to “Troubleshooting”

Who is online

Users browsing this forum: No registered users and 48 guests