First we need to find out where our system locates the device. With the USB device not plugged in, type tail -f /var/log/messages Then plug in your USB device and, looking in the new lines which appear, make a note of the ID – it will be something like sda1, sdb1, sda2, sdb2 etc. Then CTRL-C To.
You can connect your external hard disk, SSD, or USB stick to any of the USB ports on the Raspberry Pi, and mount the file system to access the data stored on it.
By default, your Raspberry Pi automatically mounts some of the popular file systems such as FAT, NTFS, and HFS+ at the
/media/pi/<HARD-DRIVE-LABEL>
location.To set up your storage device so that it always mounts to a specific location of your choice, you must mount it manually.
Mounting a storage device
You can mount your storage device at a specific folder location. It is conventional to do this within the /mnt folder, for example /mnt/mydisk. Note that the folder must be empty.
- Plug the storage device into a USB port on the Raspberry Pi.
- List all the disk partitions on the Pi using the following command:The Raspberry Pi uses mount points
/
and/boot
. Your storage device will show up in this list, along with any other connected storage. - Use the SIZE, LABEL, and MODEL columns to identify the name of the disk partition that points to your storage device. For example,
sda1
. - The FSTYPE column contains the filesystem type. If your storage device uses an exFAT file system, install the exFAT driver:
- If your storage device uses an NTFS file system, you will have read-only access to it. If you want to write to the device, you can install the ntfs-3g driver:
- Run the following command to get the location of the disk partition:For example,
/dev/sda1
. - Create a target folder to be the mount point of the storage device. The mount point name used in this case is
mydisk
. You can specify a name of your choice: - Mount the storage device at the mount point you created:
- Verify that the storage device is mounted successfully by listing the contents:
Setting up automatic mounting
You can modify the
fstab
file to define the location where the storage device will be automatically mounted when the Raspberry Pi starts up. In the fstab
file, the disk partition is identified by the universally unique identifier (UUID).- Get the UUID of the disk partition:
- Find the disk partition from the list and note the UUID. For example,
5C24-1453
. - Open the fstab file using a command line editor such as nano:
- Add the following line in the
fstab
file:Replace FSTYPE with the type of your file system, which you found in step 2 of 'Mounting a storage device' above.
Now that you have set an entry in
fstab
, you can start up your Raspberry Pi with or without the storage device attached. Before you unplug the device you must either shut down the Pi, or manually unmount it using the steps in 'Unmounting a storage device' below.Note: if you do not have the storage device attached when the Pi starts, the Pi will take an extra 90 seconds to start up. You can shorten this by adding
,x-systemd.device-timeout=30
immediately after nofail
in step 4. This will change the timeout to 30 seconds, meaning the system will only wait 30 seconds before giving up trying to mount the disk.For more information on each Linux command, refer to the specific manual page using the
man
command. For example, man fstab
.Unmounting a storage device
When the Raspberry Pi shuts down, the system takes care of unmounting the storage device so that it is safe to unplug it. If you want to manually unmount a device, you can use the following command:
If you receive an error that the 'target is busy', this means that the storage device was not unmounted. If no error was displayed, you can now safely unplug the device.
Dealing with 'target is busy'
The 'target is busy' message means there are files on the storage device that are in use by a program. To close the files, use the following procedure.
- Close any program which has open files on the storage device.
- If you have a terminal open, make sure that you are not in the folder where the storage device is mounted, or in a sub-folder of it.
- If you are still unable to unmount the storage device, you can use the
lsof
tool to check which program has files open on the device. You need to first installlsof
usingapt-get
:To use lsof:
Whether you call them USB flash disks, memory sticks, keys or drives they are a useful accessory in the world of computers. By now many people will have a collection of various devices of varying capacities. Using them with the Pi is a great way of getting some use out of them rather than letting them gather dust in a drawer.
I tend to transfer files from my Pi using FTP but occasionally it is useful to get stuff onto a USB drive. There are lots of guides already out there but I had a few issues using most of them, mainly due to permission problems. My main goal was to clarify the process by which I could mount standard FAT32 drives and allow the default Pi user read/write permissions.
So here is my procedure for using USB flash drives with the Raspberry Pi where the Pi user has permissions to use it without needing “sudo”. It works great with the shiney new Kingston Digital 32GB DataTraveler Micro I’ve just bought myself.
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 :
This will give you an output that should list your drive :
The line will usually refer to “/sda” and in this example it is “sda1”. My ID is “18A9-9943”. 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 :
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 :
You will only need to do this step once.
Step 4 – Manually Mount The Drive
To manually mount the drive use the following command :
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 “/media/usb” as a destination or source without needing to use sudo.
Step 5 – Un-mounting The Drive
You don’t need to manually un-mount if you shutdown your Pi but if you need to remove the drive at any other time you should un-mount it first. Only the user that mounted the drive can un-mount it.
If you used the fstab file to auto-mount it you will need to use :
If you are paying attention you will notice the command is “umount” NOT “unmount”!
Step 6 – 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 :
Then add the following line at the end :
The “nofail” option allows the boot process to proceed if the drive is not plugged in. The “noatime” option stops the file access time being updated every time a file is read from the USB stick. This helps improve performance.
My fstab file looks like this :
Make sure you set the correct UUID. Use CTRL-X followed by Y to save and exit the nano editor.
Now reboot :
Your USB drive should be auto-mounted and available as “/media/usb”.
An Extra Note About File Systems
In the examples above I specified “vfat” as the file system of the device as it was formatted as FAT32. If you need to change the file system replace references of “vfat” with “ntfs-3g”, “ext3” or “ext4”.
If you are using NTFS you will also need to install the following package :
Final Thoughts
This technique suits my applications but the main disadvantage is that it is specific to a known USB device given we are using the device ID. However if you created a few mount points in advance you could manually mount a new device to a spare mount point without worrying about updating the fstab file.