Tech: Add a hard disk to your (headless) Raspberry Pi
The RasPi usually boots from an SDCard which has limited storage capacity. The following describes how to add a USB hard disk to a Pi and prepare it to store data, using the command line. If you are running a GUI then the system should recognise and mount the drive automatically.
As we are connecting the drive to a Pi with no keyboard, mouse or monitor, you will need to have already installed an operating system (I recommend Raspian) and obtained SSH access to the Raspberry Pi.
For this exercise I will be adding a 2.5" Samsung 500GB SATA disk which I recovered from a broken and discarded multimedia recorder (best 10 bucks I ever spent at Andy's Salvage). With the Pi up and running, plug in the hard disk. I'm using a USB3 lead which is backwardly compatible with the Pi's USB2 ports.
Always back up the data on the external drive. This is particularly true before attempting to put it into a new system. I will be reformatting mine with the native Linux EXT4 file system for use. If you have existing partition(s) on the drive, you can mount NTFS and other partition types to access existing data.
Plugging the drive into the USB port should cause the operating system to silently recognise the drive and add an entry into the /dev (devices) directory. To see the entry for the new hard disk, run the following command..
The new disk is usually listed at the bottom. Here's an example of my own results..
Note the Samsung drive (previously labelled by me and NTFS formatted) is listed as /dev/sda5 which is a typical notation. The other entries refer to the SDCard. These notations are needed to mount drives for use.
At this stage, if it has an existing partition, you can mount the drive to make it accessible. This involves creating a new directory and running a command to make the data accessible through this directory. By convention drives are mounted to directories inside the /mnt directory, but this is not compulsory. I'm going to make my drive's NTFS partition accessible through a new directory called "samsung" (note Unix is case sensitive)..
sudo mkdir /mnt/samsung
Finally run the mount command to link the drive identifier to the new directory..
sudo mount /dev/sda5 /mnt/samsung
This assumes the operating system running on your Pi has support for reading NTFS partitions, which recent versions of Raspbian do have at least read support. Access the data on the drive by changing to its mounted partition..
Formatting drives with a file system
If there is no existing filesystem or you wish to change to another, the mkfs command can be invoked. Note this must be done before mounting the drive (sudo umount /dev/sda5 - using your own device name - will unmount a drive).
* FORMATTING WILL DELETE ALL THE DATA ON THE DRIVE *
Best performance is through using the native EXT4 filesystem, which is not natively supported by Windows, but I do not plan to remove the drive to plug into any other systems..
sudo mkfs.ext4 /dev/sda5 -L somename
- where somename can be some name of your choice.
Note that formatting a drive with the EXT file system can take some time, using a Raspberry Pi. It's not fast like Windows and there's no real 'quick format' option. My 500GB took the best part of an hour to format (although it turned out the drive was a bit fucked after all, so that may have made a difference).
This took 1 minute on a Toshiba 1GB external USB3 drive, on a Rasperry Pi III running Raspbian.
To format the drive as NTFS, use..
sudo mkfs.ntfs /dev/sda1 -f -v -I -L somename
Other filesystems can be used but are not recommended.
Automatically mount a drive
At the moment the drive will need to be mounted manually if the system reboots. Editing the file system table fstab will mount the drive on startup. Take care with editing this file as errors may cause the system not to boot. Using the editor of choice..
sudo vi /etc/fstab
Add a new line to the end of the file, modifying the details to suit your system..
/dev/sda5 /mnt/samsung auto defaults,user,nofail 0 1
/dev/sda5 is the location of the drive to mount..
/mnt/samsung is the mount point, the directory to access the content of the drive..
auto detects the file system type..
The entries defaults,user,nofail are mount options:
user enables write permission for all users; replacing this with ro would make the drive read-only
nofail tells Raspbian to ignore this entry if the USB drive is not plugged in.
Note the required use of non-spaced commas to separate multiple options
0 is a binary value used for debugging. It is best to keep this set at zero.
1 - pass number for a file system check at boot. 0 (zero) to disable or 2 to enable.
Save the changes to fstab and exit the editor.