Automatically mount USB devices on a Raspberry PI

This article has been published by hey in our forum

In Raspberry, if you don't use a graphical interface, it becomes annoying to be mounting our USB memory over and over again. Also, in the process of automating this action (which is what I show below) you can learn a little about how Linux handles devices.

Install autofs and udev

The first thing we are going to do is install autofs y udev

sudo apt-get install autofs udev

udev is a linux kernel tool in charge of managing the / dev directory which is where all the devices are located. And autofs allows us, once configured, all mount and unmount automatically as soon as the usb is connected or disconnected.

The first thing we will do is connect our usb memory (I will use a Kingston brand) in our computer / Raspberry Pi. Then we execute:

sudo fdisk-l

will have an output similar to this:

Disk / dev / mmcblk0: 15.7 GB ... Device Boot Start End Blocks Id System / dev / mmcblk0p1 2048 1607421 802687 e W95 FAT16 (LBA) / dev / mmcblk0p2 1613824 30613503 14499840 85 Linux extended / dev / mmcblk0p3 30613504 30679039 32768 Linux … .. Disk / dev / sda: 83 GB ... Device Boot Start End Blocks Id System / dev / sda30.9 1 2048 60436479 c W30217216 FAT95 (LBA)

My external usb memory has 30.9 GB (ie it is / dev / sda1) while the SD memory that is where I have Linux installed has 15.7 GB.

Custom rules in udev

Knowing that sda1 is our device, we will use udev to extract information from memory, so we execute the following command:

udevadm info -a -p $ (udevadm info -q path -n / dev / sda1)

there will be blocks separated by the phrase “looking at parent device '/ devices /… ..”

to make the search a bit easier we can use grep, so I do the following:

udevadm info -a -p $ (udevadm info -q path -n / dev / sda1) | grep manufacturer

in my case as my memory is a Kingston the output is:

    ATTRS {manufacturer} == "Kingston" # 1 ATTRS {manufacturer} == "Linux 3.12.28+ dwc_otg_hcd"

or we can also search:

udevadm info -a -p $ (udevadm info -q path -n / dev / sda1) | grep model udevadm info -a -p $ (udevadm info -q path -n / dev / sdd1) | grep vendor

I'm interested:

ATTRS {manufacturer} == "Kingston"

As was the first coincidence. In the command output udevadm I look for the block where it first appears "ATTRS {manufacturer}"

I take some data that I consider unique from the device of a block, in my case:

   ATTRS {product} == "xxx" ATTRS {serial} == "xxxx" DRIVERS == "usb"

you just need to create the rules. We make a file finished in .rules in udev:

sudo nano /etc/udev/rules.d/personal.rules

inside the file we put

ATTRS {product} == "xxx", ATTRS {serial} == "xxx", DRIVERS == "usb", SYMLINK + = "miusb"

Now when I connect my usb there will be a file / dev / miusb. This was the hardest part.

Setting up autofs

We execute:

sudo nano / etc / default / autofs

where it says "TIMEOUT =" they put "TIMEOUT = 1"

Let's go to /etc/auto.master

nano /etc/auto.master

and inside the file we put in the last line:

/ media /etc/auto.misc

now we go to /etc/auto.misc

nano /etc/auto.master

and in the last line we write:

mymemory -fstype = vfat, users, rw, umask = 000: / dev / miusb

finally we make it load the autofs module at the beginning:

sudo nano / etc / modules

and in the last line we write:

autofs4

and voila, we restart the Raspberry. Keep in mind that the folder does not exist in / media but when we do a

cd / media / memory

we are already inside. And if we stop using it, the folder disappears and we can remove the device without manually unmounting it.


5 comments, leave yours

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   hey said

    thanks for posting it 🙂

  2.   Pablo said

    I think there is an error, in the step of editing auto.misc, in the code is written auto.master, in case some sucker does it and does not realize 🙂

  3.   Fernando Díaz said

    Thanks, I was just looking to do it on Raspbian, I used Arch before and it was easier.

  4.   Azureus said

    Very good, that I got to occupy when I put my Pi as a torrent client with directories shared by samba.
    The fstab manual says that something a little more robust is to check the label of the unit with # blkid, in my case I have an automounted windows partition at each startup as follows:

    / Dev / sda2
    UUID = 24A0729FA07276E0 / home / azureus / Windows ntfs auto, defaults 0 2

    on the raspberry I have an LVM mounted, I don't remember the configuration well.
    According to the manual, you can use # fdisk -l to get the drive number and letter and # blkid to find out which label corresponds to which drive.

    Greetings.

    1.    Azureus said

      [Update]
      This label arises from the fact that there are people (like me) who feel lazy to disconnect a volume every time the computer is turned off, the problem is when you have several volumes since it is not always mounted in the same order after several reboots . By the way, this has a drawback, when the device corresponding to the assigned label for automount is removed it throws an error and does not allow the / home to be loaded in case the partition is mounted on home. This can be solved by commenting out the device causing conflicts in fstab or by connecting the device again