Post navigation

Installing a new drive in Ubuntu Server 14.04

This last weekend I built a new home network server on which I run Ubuntu Server 14.04. There is a long post coming on that (as soon as I’ve cleaned it up and added the pictures), but I’ll start small with this one since I need to link to it from the other post.

Power down the server, install the drive and connect the cables, power the server up again and log on.

Finding the device name of the drive

Ubuntu will detect all the installed drives, and will give each one a device name (or “logical name”). Hard drives (HDDs and SSDs alike) are named with the prefix letters “sd” (which I believe stands for SCSI Device – not that they generally are SCSI devices, but what do I care) followed by a third letter that represents the order in which the device was (originally) detected.

So on my server, the system drive, which was the first hard drive ever detected by the system, has the name sda. Now that I’ve have installed a second hard drive (the data drive as I’ll call it), it will have been named sdb. If I ever install a third hard disk, it will become sdc, and so on.

Guessing the name would of course be silly, so we use the lshw command to make sure:

1

sudolshw-Cdisk

The output will be a section like the one below for each drive (including any optical drive) on your system. Not that I forgot to keep the output for my new drive before I partitioned and mounted it, so this is the description of the new drive after I had already done that.

1

2

3

4

5

6

7

8

9

10

11

12

*-disk

description:ATA Disk

product:ST1000DM003-1ER1

vendor:Seagate

physical id:0.0.0

bus info:scsi@1:0.0.0

logical name:/dev/<strong>sdb</strong>

version:CC43

serial:Z4Y1T0L5

size:931GiB(1TB)

capabilities:partitioned partitioned:dos

configuration:ansiversion=5sectorsize=4096signature=bfbeaa65

Use the output from lshw to find the “logical name” for your new drive (sdb in my case).

Partitioning and formatting the drive

Before you can store any data on a hard drive, it must be partitioned and formatted. Partitioning a drive means splitting it up into one ore more sections (partitions). Each partition must then be formatted, meaning that the partition is prepared for use as a specific file system type (a few example file systems that you may recognize are ext3 or ext4 if you are only going to use it from Linux, or FAT32 or NTFS if you also need to access it from Windows).

If your data drive has already been partitioned and formatted, and you are satisfied with that, then you can just skip to Mounting the drive now. In my case, though, the disk is brand new, and needs to be partitioned before I can use it.

IMPORTANT NOTE: The commands below assume that the new drive has the device name sdb. Make sure to replace that name with the actual device name of your own new drive, since these commands will destroy any existing data on the drive!

I will just make one single, large partition of the whole 1TB drive. If you want something else, you’ll just have to find the information elsewhere. Use the fdisk utility for partitioning the drive, which you access through the device file that represents the drive in the /dev directory:

1

sudo fdisk/dev/sdb

The drive is now partitioned, and has a single partition on it. This means that the /dev directory now should contain two entries for the drive; /dev/sdb which is the device file representing the physical drive, and /dev/sdb1 which represents the first (and only in this case) partition on that drive.

So when we now format the drive, it is not the drive itself (/dev/sdb) that we want to format, but rather the only partition on the drive, /dev/sdb1.

So what file system should we use for this partition? There are loads to choose from, but with the latest Ubuntu Server version installed, all new hardware, no older software installed to check compatibility for, and the knowledge that this drive will be for internal use only (not shared with any Windows computers over the network and no dual boot with Windows involved), I chose the ext4 file system.

Use the mkfs command to format partition 1 on the drive for the ext4 file system:

1

sudo mkfs-text4/dev/sdb1

The drive is now ready for use, but there is one final thing that I want to do here. By default, the system reserves 5% of the total size of the partition for the super user. This is to ensure that critical operations will not fail due to the disk getting full, and to make sure that there won’t be a problem defragmenting the disk when it’s (close to) full.

For a data disk such as this, that is an unnecessary waste of space (5% of 1TB is 50GB of space on the disk that you will not be able to use). You can use the tune2fs command to adjust the size of the reserved data. The -m option sets the percentage of disk space to reserve (the default is 5% as mentioned above). Below, I set it to 0%, since I don’t want to waste any space at all. You could set it to (for example) 1% if it makes you feel safer, but 5% is much too much for a data drive.

1

sudo tune2fs-m0/dev/sdb1

Mounting the drive

So. The drive is now ready for use, but we need to mount it (the formatted partition) before we can access it.

We could mount it using the same handle as above (/dev/sdb1), and would probably never see any problems with it. Not related to that anyway However, it is recommended to mount drives using their UUIDs rather than their device names, since the device name (at least in theory) can change between boots – so we’ll do that.

The UUID (Universally Unique Identifier) is guarantied to always point to the same physical device. You can determine the UUID for a device in different ways. I’ll use the blkid command (remember that the device file for the only partition on our new drive is /dev/sdb1):

1

sudo blkid/dev/sdb1

The output will be similar to this:

1

/dev/sdb1:UUID="2115bee4-c800-41f5-bcbd-50c64cf843ac"TYPE="ext4"

As you can see, the UUID is a rather long string of characters. Not something you would like to try to remember, but guaranteed to be unique over time.

I must admit that I still have a bit of a hard time understanding the best place to put stuff in the Ubuntu file hierarchy. There are lots of opinions on the web, but I haven’t (yet) found a good source, though “man hier” gives a lot of good clues. I would, for example, have said that the best place to mount this new drive would be under /mnt, but apparently, the Ubuntu “standard” choice for these things is to mount it under /media. Well, it doesn’t really matter to me, so let’s go with the latter.

To mount a device, we must first have an empty directory to use as mount point. I’ll call the new drive “data1“, so the mount point will be /media/data1:

1

sudo mkdir/media/data1

We don’t want to have to mount this drive manually whenever we need it, so we will add a line for it in the fstab file to have it mounted automatically when the system boots. So, open the fstab file for editing:

1

sudo nano-Bw/etc/fstab

Add a line like this in the file (read below it here before saving):

1

UUID=2115bee4-c800-41f5-bcbd-50c64cf843ac/media/data1 ext4 defaults02

Note that you must replace the UUID value above with what you got from the blkid command earlier, even if your device name is the same as mine (universally unique id – remember?). And the mount point must of course correspond with the directory you just created for it. Also, if you used a different file system than I did (ext4), you need to change that too (check the output from the blkid command above if you are unsure about that).

Save the file and run this command to mount it:

1

sudo mount-a

You can now check the drive through /media/data1.

Finally, you need to decide who will have access to the drive and its contents. The command below gives all users full access to the drive, which is the way I want it.