Adding a New Storage Device



This guide covers how to add a new storage device (such as a platter or a solid state drive) to your Linux machine. All work is done from the command-line.

Selecting the right storage device

This guide does not cover selecting the right model or kind of storage device.

Plugging the device into the computer

This guide does not cover how to plug the device into the computer.

What's the device name?

Now that the device is plugged into the computer we should figure out what name it got. It usually follows the pattern /dev/sdX where X is a letter (a, b, c, d...).

Step 1 is to find the names of all devices.

ls /dev/sd*

Is a somewhat crude method that is often used and often works.

cat /proc/partitions

/proc/partitions will list all the block devices and partitions that the system recognizes.

fdisk -l

This will list the partition tables for the devices mentioned in /proc/partitions. I personally dislike fdisk since it can't handle GPT partition tables. We will look into that subject later.

Step 2 is to figure out which of these devices is the new one.

A new storage-device almost never has a partition table setup. Perhaps you already know from the output of the previous two commands.

mount
df

Those two commands will give you info on what devices are mounted where and if they are filled with anything yet. Your device is probably not mounted already.

cat /etc/fstab

A properly setup storage-device should have an entry in /etc/fstab. Your device is probably not in there.

You should now have figured out the device name and for the rest of the guide we will call the device /dev/sdb.

Partitioning and file system

You may skip this section if your storage device already has partitions and you want to keep them.

We use the application parted to setup a partition table. I personally prefer the partition table type gpt since it can handle disks larger than 2TB:

parted
select /dev/sdb
mktable gpt
quit

We now have a partition table but no partitions. You can view info on the storage-device at any time using parted print command:

parted
select /dev/sdb
print
quit

Lets create one large partition only:

parted
select /dev/sdb
mkpart primary 0% 100%
quit

We now have a partition covering all of the device. Note how we used %-notation and parted will then align the partition for best performance. If you try entering detailed values manually you may get the message Warning: The resulting partition is not properly aligned for best performance.. Now lets place an ext4 file system on that partition:

mkfs.ext4 /dev/sdb1

And that is pretty much it. The partitions and file systems are set up. This would be all the commands in a batch:

parted
select /dev/sdb
print
mktable gpt
print
mkpart primary 0% 100%
print
quit
mkfs.ext4 /dev/sdb1

Parted has interactive and non-interactive mode. So far we have been making use of the interactive mode. This is what the above would look like in non-interactive mode:

parted /dev/sdb mktable gpt mkpart primary 0% 100% print mkfs.ext4 /dev/sdb1

Creating a mount-point

A mount point is simply a directory. We should create one using the mkdir command, but where should we put it? I do not know the best practice here but the folders /media and /mnt both seem to be used for temporary mount points. As our storage device is a permanent one I suggest creating a brand new top-level folder for it. For example:

mkdir /hdd

What file permissions should this new directory have? It does not matter. They will be changed as we mount something onto the directory.

Temporarily mounting the device partition

To temporarily mount the device partition we can run the command:

mount -t ext4 /dev/sdb1 /hdd

This will however only get it mounted once. We should create an entry in /etc/fstab to make it mount automatically on reboot.

Permanently mounting the device partition using fstab

Step 1 is to find the partition UUID. To add the filesystem by UUID is a good idea since you will be able to switch around with SATA hardware contacts and the system will still work. Find the UUID using any of these commands:

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

It may also be fun to know the model number of the storage device. You may get this info using:

hdparm -I /dev/sdb

Step 2 is to add the info to /etc/fstab. Please read up more on fstab elsewhere since my info here is not complete.

Are you running on an SSD? In such case you should add the discard option to /etc/fstab. Doing that will enable automatic TRIM and works well on ext4 filesystems. You can also stop writing last access time for files and folders by adding the option noatime.

What you add could look something like either of these two:

#
# A "WDC WD1002FAEX-00Y9A0" for extra storage.
# The device was on /dev/sdb1 when added on 2012-07-16 (YYYY-MM-DD)
UUID=10e27590-16c0-44cd-9a61-07e03b97c46d /hdd ext4 defaults 0 0
#
# A Crucial M4 SSD for main storage.
# / was on /dev/sda1 during installation
UUID=6fbfc91d-e514-4bfe-a785-f3c207763dbe / ext4 noatime,discard,errors=remount-ro 0 1

Make sure you leave a blank line at the end of the file. If you do not you will get a warning message in the next step:

Step 3 is to test that the fstab entry is correct. First unmount the /hdd using:

umount /hdd

In case you get the warning message umount: /hdd: device is busy. you are probably standing in the device. Use cd / to get out of there and try again.

Now ensure everything in /etc/fstab is correct by mounting all of it:

mount -a