Skip to content

[Procedure] Automount Ext4 Usb Stick

Éric Massé edited this page Mar 15, 2015 · 4 revisions

References:

Required:


Step 0 – Prepare The Device

MiniTool partition wizard is a free (for home use) partition manager that can format a partition as EXT2/3/4 from windows - http://www.partitionwizard.com

Step 1 – Plug In The Device

The first step is to plug in your USB stick. If you are using a mouse and keyboard you will need a decent USB hub at this point. (e.g. the PiHub by Pimoroni).

Step 2 – Identify The Devices Unique ID

In order to find the unique reference (UUID) for your drive run the following command in the terminal :

ls -l /dev/disk/by-uuid/
total 0
lrwxrwxrwx 1 root root 15 Mar  9 10:51 05A5-E36A -> ../../mmcblk0p6
lrwxrwxrwx 1 root root 15 Dec 31  1969 6a3a8b61-e44d-4837-9fbb-7443dc7fc551 -> ../../mmcblk0p5
lrwxrwxrwx 1 root root 15 Dec 31  1969 72DD-3612 -> ../../mmcblk0p1
lrwxrwxrwx 1 root root 15 Dec 31  1969 acd4dfcc-1e96-4e6d-8e54-5af702858602 -> ../../mmcblk0p3
lrwxrwxrwx 1 root root 10 Mar  9 10:51 7fbdf3ad-995b-d001-7039-f0ad995bd001 -> ../../sda1
lrwxrwxrwx 1 root root 15 Mar  9 10:51 eda5e8c5-7810-4140-9530-214987633fea -> ../../mmcblk0p7

The line will usually refer to “/sda” and in this example it is “sda1″. My ID is “7fbdf3ad-995b-d001-7039-f0ad995bd001″. Note down yours.

You would need to repeat this step if you wanted to use a different device as the UUID would be different.

Step 3 – Create a Mount Point

A mount point is a directory that will point to the contents of your flash drive. Create a suitable folder :

sudo mkdir /external

I’m using “usb” but you can give it whatever name you like. Keep it short as it saves typing later on. Now we need to make sure the Pi user owns this folder :

sudo chown -R pi:pi /external

You will only need to do this step once.

Step 4 – Manually Mount The Drive

To manually mount the drive use the following command :

sudo mount /dev/sda1 /external

This will mount the drive so that the ordinary Pi user can write to it. Omitting the “-o uid=pi,gid=pi” would mean you could only write to it using “sudo”.

Now you can read, write and delete files using “/external” as a destination or source without needing to use sudo.

Step 5 – Auto Mount

When you restart your Pi your mounts will be lost and you will need to repeat Step 4. If you want your USB drive to be mounted when the system starts you can edit the fstab file :

sudoedit /etc/fstab

Then add the following line at the end :

UUID=7fbdf3ad-995b-d001-7039-f0ad995bd001       /external       ext4    defaults          0       0

My fstab file looks like this :

proc            /proc           proc    defaults          0       0
/dev/mmcblk0p6  /boot           vfat    defaults          0       2
/dev/mmcblk0p7  /               ext4    defaults,noatime  0       1
# a swapfile is not a swap partition, so no using swapon|off from here on, use  dphys-swapfile swap[on|off]  for t$

# Mounting external usb 16gb EXT4 as /external
UUID=7fbdf3ad-995b-d001-7039-f0ad995bd001       /external       ext4    defaults          0       0

Make sure you set the correct UUID. Use CTRL-X followed by Y to save and exit the nano editor.

Now reboot :

sudo reboot

Your USB drive should be auto-mounted and available as “/external”.

If the drive doesn't mount on reboot

According to the man page, mount -a mounts the entries in fstab sequentially, while adding the -F ('fork') option will mount them in parallell. It appears that the standard boot is using 'fork', presumably to reduce boot time, so I guess mounting (semi-)manually afterwards is the simplest way to regain control of the mount order.

One option is to postpone mounting until after /etc/fstab has completed, this gives you full control over the mount order.

Just add noauto to the mount options in fstab, and mount in rc.local.

Try adding the "noauto" option to the troublesome fstab entry:

# Mounting external usb 16gb EXT4 as /external
UUID=7fbdf3ad-995b-d001-7039-f0ad995bd001       /external       ext4    defaults,noauto    0       0

This should prevent it from attempting to mount too soon.

Then add "mount -a" to the /etc/rc.local file just before the existing "exit 0" line, to remount all drives as the very last action of the boot process.

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

# Mount all /etc/fstab
mount -a

exit 0