If you're using a Raspberry Pi or other ARM board for your server, you will want an external hard drive for larger data storage. Here's how you mount it to a specific directory at every boot.
First, make sure you have all the necessary parts.
- If your hard drive comes straight out of a desktop or laptop, you will need to buy a USB enclosure. These USB 3.0 enclosures are backwards compatible with USB 2.0: 2.5-inch, 3.5-inch
- If your board supports SATA, obviously you can just connect it directly. You need a $10 external power source though.
- If the external hard drive has it's own power supply, just plug it in, you're good to go.
- If not, you will need to use a Powered USB hub, since the Raspberry Pi probably can't supply enough power to keep a hard drive running.
Before setting up the hard drive with the server, make sure to format it to EXT4 filesystem format, using GParted on Linux. If you really need to, you could try NTFS, but EXT4 is native to Linux and has significantly higher performance.
Find the Drive via UUID
Next, plug in the drive and make sure it is detected by the server. Check the
dmesg command for any new drives:
dmesg | tail
After that, look for the drive's UUID number. Usually, the hard drive will be named
/dev/sda (drive letter will vary).-
This output will appear. In this example, the first entry is
sda1, so grab the UUID from that.
/dev/sda1: UUID="13d368bf-6dbf-4751-8ba1-88bed06bef77" TYPE="ext4" /dev/sda2: SEC_TYPE="msdos" LABEL="boot" UUID="15CD-3B79" TYPE="vfat" /dev/sdb1: LABEL="Sandisk Cruzer" UUID="8B22-130D" TYPE="vfat"
Now, as root, create a folder (we'll call it
/media/usbdisk) to automatically mount the hard drive into at boot:
sudo mkdir /media/usbdisk
Next, edit the
/etc/fstab file as root.
sudo nano /etc/fstab
Add this entry to the bottom of the
fstab to mount the drive at boot every time at
/media/usbdisk. (Replace the UUID with your drive's UUID):
UUID=13d368bf-6dbf-4751-8ba1-88bed06bef77 /media/usbdisk ext4 noatime,relatime 0 0
Note: If your drive uses a different filesystem from ext4, you can replace it with
vfatfor FAT16/FAT32, or others.
Notice that you will need permissions to write to the usb disk. If you're not security conscious, you can give the
pi user ownership of the entire disk. If you are, skip this step.
sudo chown pi:pi /media/usbdisk sudo chmod 777 /media/usbdisk
But for forward-facing internet servers, it's much safer to create bind-mounted folders, where ownership is explicitly given to the user that runs a program (such as a torrent server or FTP user). See the following section for instructions.
- Source: K. Wilson - Force your Raspberry Pi to mount an external USB drive every time it starts up
- Source: TechJawab - How to Setup Mount/Auto-Mount USB Hard Drives at Boot
Mounting External Drives
Fstab mount is the most straightforward method to automount devices at boot. Unfortunately, a major downside is that if even a single listed storage device is offline or disconnected, the computer simply won't boot. This is a major problem for external drives on a server that you may be unable to service while away from home.
A new safer cross-platform mounting method for external drives is to use the
nofail option and systemd's new unit mount system. If the drive is disconnected or has failed, systemd will log the error, but let the server continue running.
The following fstab line adds the
nofail option and the
x-systemd.device-timeout=1 option (which is necessary because as of Systemd 219 the default timeout if unavailable is 90 seconds).
/dev/sdg1 /media/backup jfs defaults,nofail,x-systemd.device-timeout=1 0 2