In this article, we will take you through the steps to add a new raw hard disk to an existing Linux server such as RHEL/CentOS or Debian/Ubuntu.

Important: Please note that the purpose of this article is to show only how to create a new partition and doesn’t include partition extension or any other switches.

We are using the fdisk utility to do this configuration. Additionally, as an example we have added a hard disk of 20GB capacity to be mounted as a /data partition.

Please note, the /data directory must be created before mapping the new partition. Also, sudo privileges or root user access is required to execute the necessary commands.

 

Creating the /data directory

Note that you can name this directory to any identifier that meets your needs.

# mkdir /data 

 

List existing partitions

fdisk is a command line utility to view and manage hard disks and partitions on Linux systems.

# fdisk -l

This will list the current partitions and configurations.

 

Find Linux Partition Details

After attaching the hard disk of 20GB capacity, the fdisk -l command will show the new disk as /dev/xvdc. If we are adding physical disk it will show as /dev/sda based of the disk type. Here I used a virtual disk.

To partition a hard disk, for example /dev/xvdc

# fdisk /dev/xvdc

 

Commonly used fdisk commands:

n – Create partition

p – print partition table

d – delete a partition

q – exit without saving the changes

w – write the changes and exit.

 

In this case, since we are creating a partition use the n option.

 

Create New Partition in Linux

Create either primary/extended partitions. By default, we can have up to 4 primary partitions.

 

Create Primary Partition

Give the partition number as desired. Recommended to go for the default value 1.

 

Assign a Partition Number

Give the value of the first sector. If it is a new disk, always select default value. If you are creating a second partition on the same disk, we need to add 1 to the last sector of the previous partition.

 

Assign Sector to Partition

Give the value of the last sector or the partition size. Always recommended to give the size of the partition. Always prefix + to avoid a value out of range error. For instance: +18G

Save the changes and exit by entering the W letter.

# Command (m for help): W

 

Assign Partition Size

Save the changes and exit.

 

Format New Partition

Now format the disk with mkfs command.

# mkfs.ext4 /dev/xvdc1

 

Once formatting has been completed, mount the partition as shown below.

# mount /dev/xvdc1 /data

 

Make an entry in /etc/fstab file for permanent mount at boot time.

/dev/xvdc1    /data    ext4    defaults     0   0

 

Conclusion

Now you know how to partition a raw disk using fdisk command and mount it.

 

 

Was this answer helpful? 2 Users Found This Useful (3 Votes)