Attach an External HDD / USB Thumb Drive

Plug in the external storage device and check it’s been recognised:

lsusb
lsusb - External Storage
lsusb – External Storage

Delete any unwanted partitions

First, list the drive and partitions:

sudo fdisk -l

sudo fdisk -l
sudo fdisk -l

In this example, you can see that the usb drive i’ve added is /dev/sda and has two microsoft partitions as it was previously used with an XBox game console: /dev/sda1 and /dev/sda2. I’ll need to delete both of them.

sudo fdisk /dev/sda

Then from the interactive menu we can select d for delete. If you have more than one partition, you’ll be asked which partition you wish to delete. Once you’ve finished, enter w for write, this makes the changes and exits fdisk.

Create new partition

sudo fdisk /dev/sda

and enter the following command at the menu:

  • n – Create a new partition
  • 1 – Partition number
  • Use the default First sector, probably starting at 2048
  • Use the default Last sector
  • w – write the changes to the disk

Format Partition

sudo mkfs.ext4 /dev/sda1

This formats the drive using ext4. Other file systems include ext3 or NTFS. ext4 has performance improvements over ext3 so if the drive is to only be used on unix systems then ext4 is recommended. NTFS would allow the drive to be read in windows machines as well – but as NTFS is an inferior filesystem it should only be used if you need to be able to share the drive between unix and Windows.

Mounting the drive at boot-up

Each drive is allocated a unique ‘UUID’. If you wish to automatically remout this drive at each boot-up, I recommend doing it by UUID. You can find the UUID by:

ls -l /dev/disk/by-uuid/

ls -l /dev/disk/by-uuid/
ls -l /dev/disk/by-uuid/

In my example, the partition I created on the drive is: 5ac87ba1-0de0-4251-9a77-a0fa4e88faf8

Edit the fstab file

sudo nano /etc/fstab

and add the following line to the end, replacing the UUID in my example with he UUID from your external storage:
UUID=5ac87ba1-0de0-4251-9a77-a0fa4e88faf8 /mnt/hdd1 ext4 defaults 0 2

Reboot the Pi:

sudo shutdown -r now

You’ll find that a directory /mnt/hdd1 has been created (you can change the location by editing the fstab file). You can change the permissions of the device by using chmod. By default, the owner will have full (r,w,x) privileges, group members and all other users will have just read and execute privileges. For example, to give group members full access, but guest members no access then you’d use the following command:

sudo chmod 770 /mnt/hdd1