Adding new hard drive in linux

 In Linux

 

In this article we’ll have a look on how to add a new HDD in linux, how to format it with one of the file systems and mount the new drive using the command line. This operation can be done from a GUI too either if it’s a virtual drive (if you’re using virtualization) or a physical drive but as I mentioned, today we’ll be doing it through the linux command line.

First we make sure we add the new hard drive (either virtual or physical) and restart the machine. After that we use the following command to see the list of all the existing partitions on the machine:

fdisk -l

The output should be something similar with this one (you should be able to see your new device as /dev/sdb at the bottom of the list):

Device             Boot                Start                End                Blocks                Id            System
/dev/sda1              *                2048         1026047               512000                83              Linux
/dev/sda2                          1026048         7737343             3355648                82              Linux          swap / Solaris
/dev/sda3                          7737344       67108863           29685760                83              Linux

Disk /dev/sdb: 644.2 GB, 644245094400 bytes, 1258291200 sectors

As you can see, in this example we’ve got 3 partitions for this device – sda1, sda2, sda3 and the new device as /dev/sdb unpartitioned and unformatted.

The next step is to create a partition for the new device. To do this you need to use the following command:

fdisk /dev/sdb

This means that we’re now using the new device. After this command you are basically in configuration mode:

Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x8ea0e668.

Command (m for help): n

To create a new partition just type n and press enter.

Next you need to chose the type of the partition. Usually default (primary) is fine so either type p and enter or just press enter.

Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):

Also, you need to chose a partition number (for exaple 1 if it’s the first partition of the device but your free to chose any number):

Partition number (1-4, default 1): 1
First sector (2048-1258291199, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-1258291199, default 1258291199):
Using default value 1258291199
Partition 1 of type Linux and of size 600 GiB is set

After the partition has been created, you can type w to write table to disk and exit the configuration mode. At this point, if you type again the command fdisk -l you should be able to see your new partition numbered along with the other ones:

Device        Boot            Start                End                     Blocks        Id        System
/dev/sdb1                       2048   1258291199              629144576       83          Linux

Now it’s time to format the new partition with a file system. When creating a new partition, you need to format it with one of the existing file systems (a common file system used by linux is ext3 or ext4). When I created my new partition in CentOS 7 for example, the default file system happened to be xfs which actually suited me fine because I used the new partition for a mysql database.

So for formatting the partition use the following command:

mkfs.xfs -b size=4096 -L new_drive /dev/sdb1 (new_drive is the label I gave to my new drive and it is optional)

Now you need to mount the new drive in order to be able to use it. To do this, you first create a location (directory) where you want to mount the drive e.g. /media/mynewdrive and user the mount command:

mount /dev/sdb1 /media/mynewdrive

If you need this drive to be automatically mounted at startup you need to specify this in /etc/fstab. Use the blkid command to reveal your new drive UUID and add this as a new line in /etc/fstab:

UUID=72481790-2921-452e-9799-1e10feb5e4b9 /media/mynewdrive       xfs     defaults        0 0

At this point, you should be able to use your new device. Don’t forget to restart your machine and make sure the drive is mounted correctly.

Leave a Comment

Contact Us

Call us on: 01332 890460 or Send us an email and we'll get back to you, asap.