Attaching an additional disk to a Linux server

Description

This section describes the process of adding and connecting a new, ‘raw’ disk to an existing Linux server such as RHEL / CentOS or Debian / Ubuntu.

In this example an additional disk with a volume of 90 GB is added, to be connected as the ‘/data’ partition.

The example describes the process of connecting the new disk on the basis of the CentOS Linux 7 distribution. The processes are discussed of creating a partition, formatting the partition, creating a mount point and then mounting the created partition, both manually and automatically.

The partition is created using the fdisk command. This is a command line utility for viewing and managing hard disks and partitions in Linux systems.

To obtain information on the connected disks and the partitions before the new disk is connected:

fdisk -l

Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000a25ba

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048    10485759     4729856   8e  Linux LVM

Disk /dev/mapper/vg0-rootfs: 4840 MB, 4840226816 bytes, 9453568 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Once the new 90 GB disk is connected, fdisk -l will give the following output:

fdisk -l

Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000a25ba

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048    10485759     4729856   8e  Linux LVM

Disk /dev/mapper/vg0-rootfs: 4840 MB, 4840226816 bytes, 9453568 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/vdb: 96.6 GB, 96636764160 bytes, 188743680 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

It can be seen that a new device has appeared in the system: a virtual disk: ‘Disk /dev/vdb: 96.6 GB’.

Creating a partition

In this example, a single partition is created that occupies all the free space on the disk. In other words, the size of the partition is the same as the size of the disk.

Note

Two commands are available on the command line for partitioning a new disk: fdisk and parted. Of these, fdisk is the older program and its main shortcoming is that it can only create MBR partitions. Newer is parted, which can create both MBR and GPT partitions.

The older MBR (Master Boot Record) system has two major limitations: it cannot accommodate partitions larger than 2 TB and there cannot be more than 4 primary partitions. The GPT (GUID Partition Table), on the other hand, can satisfy both these requirements, yet is a part of the EFI standard. This therefore requires that your kernel supports EFI. The latest version of the kernel supports EFI and almost all of the latest distributions do so as well.

Since the maximum disk size permitted is 2 TB but the disk in the example is only 90 GB, the fdisk utility is used.

Creating the partition.

  1. Run fdisk using this command:
fdisk /dev/vdb
  1. The fdisk utility displays the following menu:
fdisk /dev/vdb

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 0x6561828e.
Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)
  1. To create a new partition, enter ‘n’ (‘Add a new partition’) and press ‘Enter’:
Command (m for help): n

Partition type:
 p   primary (0 primary, 0 extended, 4 free)
 e   extended
Select (default p):
  1. Create a primary partition. Enter ‘p’ and press ‘Enter’.
Select (default p): p
Partition number (1-4, default 1):
  1. This will be the only partition on the disk, number 1. Enter ‘1’ and press ‘Enter’.
Partition number (1-4, default 1): 1
First sector (2048-188743679, default 2048):
  1. Select the default value ‘default 2048’. Press ‘Enter’.
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-188743679, default 188743679):
  1. Select the default value ‘default 188743679’. Press ‘Enter’.
Using default value 188743679
Partition 1 of type Linux and of size 90 GiB is set
  1. Now that the partition is entered, select option ‘w’ to write the partition table to the disk. Enter ‘w’ and press ‘Enter’.
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

9. If all is well, the disk was correctly defined and is now ready for formatting. Since this is the first partition, Linux recognises it as /dev/vdb1, while the disk on which the partition is situated is still recognised as /dev/vdb.

Formatting the partition

  1. Format the new partition /dev/vdb1 as the file system ‘ext4’:
mkfs -t ext4 /dev/vdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
5898240 inodes, 23592704 blocks
1179635 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2172649472
720 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
    4096000, 7962624, 11239424, 20480000

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

Note

When performing the format, substitute ‘/dev/vdb1’ for the path of your own partition.

  1. Perform a check using the blkid command:
blkid
/dev/mapper/vg0-rootfs: UUID="1e4b1167-6fa3-4d44-bdb7-282841e0b31e" TYPE="ext4"
/dev/vda2: UUID="9stkBB-ZiYX-S7sb-rGxJ-idrH-m0ov-OTEuNs" TYPE="LVM2_member"
/dev/vda1: UUID="ab1dd6e8-62a4-4752-befe-cda7f0e8383a" TYPE="ext4"
/dev/vdb1: UUID="10396c2f-9335-4966-8e94-377e413045b4" TYPE="ext4"

Creating a mount point

12. Now that the disk is divided into partitions and has been formatted, a mount point should be selected. This will be the place from which you will have access to the disk in future. For the present example the path ‘/data’ is used.

mkdir /data

Now all is ready for mounting the partition on the mount point.

Mounting the partition

  1. To mount the partition manually, use the following command:
mount /dev/vdb1 /data

Automatic partition mounting

14. To provide automatic mounting of the partition at the mount point, changes are required at ‘/etc/fstab’. Add the following autoconnect line to this configuration file:

UUID=10396c2f-9335-4966-8e94-377e413045b4 /data ext4 defaults 0 0

Once the changes have been made, the /etc/fstab file will look analogous to this:

# /etc/fstab
# Created by anaconda on Thu Sep 14 14:20:31 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg0-rootfs  /                       ext4    defaults        1 1
UUID=ab1dd6e8-62a4-4752-befe-cda7f0e8383a /boot                   ext4    defaults        1 2
UUID=10396c2f-9335-4966-8e94-377e413045b4 /data ext4 defaults 0 0

If the mounting process described in point 13 was not performed before the change was made, it must be carried out now.

15. To complete the operation and to demonstrate that the task of connecting the disk has been successful, use the df -h command:

df -h
Filesystem              Size  Used Avail Use% Mounted on
/dev/mapper/vg0-rootfs  4,4G  1,6G  2,5G  40% /
devtmpfs                3,9G     0  3,9G   0% /dev
tmpfs                   3,9G     0  3,9G   0% /dev/shm
tmpfs                   3,9G  8,6M  3,9G   1% /run
tmpfs                   3,9G     0  3,9G   0% /sys/fs/cgroup
/dev/vdb1                89G   57M   84G   1% /data
/dev/vda1               477M  121M  327M  28% /boot
tmpfs                   799M     0  799M   0% /run/user/0

As can be seen from the output, the partition /dev/vdb1 has been successfully mounted to /data. The total volume of the partition is less than 90 GB because part of the disk space is reserved for administrative purposes.

The disk is now generally ready for use.