# Using USB Drives for Storage

An external USB Drive can be used to supplement internal storage and to reduce the number of writes to the internal Micro SD Card. The low-profile of slimline style USB Flash Drives makes them perfect for use with ARM-based SBCs.

On some operating systems, like standard Raspian, drives are mounted automatically when they are inserted. These instructions are for operating systems like Raspian Lite where this is not the case.

## Identify Device

Without the USB drive inserted run `ls /dev` to list all the currently known devices. Also run `ls /dev/disk/by-uuid` to list all attached disk devices by their UUID. Now insert the USB flash drive and run the same commands again. The device will likely be named something like sda1, sdb1, sdc1, etc. depending on how many other devices are known to the operating system. Note: do not use sda, sdb, sdc, etc. because these are the raw devices. 

You should now know the device name and UUID for your USB drive. UUIDs are supported to provide a stable identifier for your device regardless of how many other devices were inserted before. 

You can check you have the correct device by running `sudo fdisk -l | grep /dev/{device-name}` and substituting the correct device name for your USB flash drive. This might be a good time to check the device size is close to what you expect.

If you are unsure whether you have identified the correct device, remove the USB drive and repeat these steps.

## Reformat

Caution: this is a destructive operation that will erase all data from a storage device for practical purposes. Ensure you have the correct device ID before proceeeding!

USB Flash Drives typically ship with the exFAT filesystem, and often some preinstalled tools.  However, for Lollipop services to best use your device, we recommend you reformat your USB Flash Device with a Linux-native filesystem like EXT4. 

To format your device using the EXT4 filesystem:

``` bash
sudo mkfs.ext4 /dev/{device-id}
``` 

## Mount Device

To be usable, you must *mount* your device into the filesystem of your computer.

Create a *mount-point* in your filesystem as a subdirectory from `/media`, perhaps something like this: `sudo mdkir -p /media/usb/device-uuid`. This is the directory you will use to access your storage device. You will need to change the owner of this directory to match the user and group that will own the mount (on Raspian this will usually be *pi*): 

``` bash
sudo chown -R pi:pi {mount-point}
```

Next we will need the user ID and group ID for the user that will own the mount. Run the following commands, replacing *user-name* with the name of the user you wish to own the mount (again, on Raspian this will usually be *pi*):

``` bash
id -u {user-name}
id -g {user-name}
```

Next edit `/etc/fstab` with your preferred text editor (remember to run with sudo if necessary) and append the following line, subsituting values for device-uuid, mount-point, user-uid and user-gid from the previous steps:

``` bash
UUID={device-uuid} {mount-point} auto nofail,uid={user-uid},gid={user-gid},noatime 0 0
```

You can now test whether the drive can be mounted by running `sudo mount -a` to mount all currently unmounted devices. If successful, you should be able to list the contents at the *mount-point* (which should be empty) by running `ls {mount-point}`.

If successful, now would be a good time to reboot your computer to ensure the device is mounted on boot: `sudo reboot`

