Wednesday, January 13, 2016

Raspbian (Or Most Linux): Always Mount My External Hard Drive

This is mainly documenting how I set up my Raspberry Pi2 to always mount an external hard disk connected via USB to a specific mount point. It should also work on most recent Linux distros but I don't assume that.

WARNING: playing with hard drive partitions and formatting can destroy data. Don't blame me if you lose information because of a typo.

First, where is the drive located? Usually you'll get the output from dmesg when the drive is connected. In my case, it's /dev/sda.
dmesg |grep sd

Be careful that you've found the correct drive. Linux + root privileges lets you do a lot of damage.

If the drive is completely fresh you'll want to partition and format it.

  • sudo fdisk /dev/sda
  • n - new partition
  • p - primary
  • 1
  • <enter> - default start sector
  • <enter> - default end sector
  • w - write changes to the drive

If the drive had a partition already on it, you can delete it with "d". Using "m" will give you a little guidance on help commands.

Format the drive:
sudo mkfs.ext4 /dev/sda1

Find the UUID of the drive.
sudo blkid

Copy the drive's UUID to the clipboard. Create a mount point (directory) where you want the drive mounted.
sudo mkdir /mnt/mydrive

Alter the fstab file; first make a backup.
cd /etc
sudo cp ./fstab ./fstab.orig

Open the fstab file in your favorite text editor of choice. Add your modification line at the end.
UUID=<uuid> /mnt/mydrive ext4 defaults,noatime 0 2

...where <uuid> is the copied UUID from blkid output.

Reboot. Your drive should be mounted when you log back in.

No comments:

Post a Comment