How to add a new disk to Linux?

A brief summary of the commands:

lsblk- list block devices;
fdisk /dev/sdb - create a new partition in the unallocated area /dev/sdb,
n - create partition, p - create main partition, w - write changes to disk; pvs - list physical volumes;
vgs - list volume groups;
lvs - list logical volumes;
df -hT - list partitions and file systems;
pvcreate /dev/sdb1 - create a new physical volume /dev/sdb1;
vgextend centos /dev/sdb1 - add volume /dev/sdb1 to the centos group;
lvextend -l +100%FREE /dev/centos/root - expand logical volume /dev/centos/root;
xfs_growfs /dev/mapper/centos-root - increase the size of the XFS file system on a logical volume /dev/mapper/centos-root (for Fedora, CentOS, RHEL, Oracle Linux, Alma Linux, Rocky Linux);
resize2fs /dev/mapper/ubuntu-root - increase the size of an EXT4 file system on a logical volume (for Ubuntu, Debian).

Disk Adding

This example will describe how to expand the disk space of the / (root) partition by 10Gb by adding a new device (disk) using the capabilities of LVM. The extension will be performed on the example of the CentOS distribution. This manual is suitable for most Linux distributions, gives detailed information on how to correctly expand disk space even with a non-standard partition layout, and takes into account the features of commands of various distributions.

Note: All commands must be run as root or superuser (sudo).
When following this instruction, there is a risk of data loss, so it is recommended to make a backup of the virtual machine first.

Step 1. Analysis of disk space configuration and search for a new device (disk)

First you need to determine the name of the newly added device (disk) in the system using the command lsblk.

add1

The structure of disk space can be displayed as a diagram:

In this example, we see that there are 3 devices in the system:

  • /dev/sda
  • /dev/sdb
  • /dev/vda

On device /dev/sda (30Gb) 2 partitions are created:

  • The /dev/sda1 (1Gb) partition is the primary partition labeled /boot, on which the Linux kernel loader is installed.
  • The /dev/sda2 (29Gb) partition is an extended LVM partition on which the /dev/sda2 physical volume is created.
    Based on the physical volume, a centos volume group was created, which includes 2 logical volumes root and swap.

The devices /dev/sdb (10Gb) and /dev/vda (20Gb) are added to the system but are not partitioned.

Let’s select a device /dev/sdb with a size of 10Gb for expansion. Devices (disks) in Linux can have the following names: sda, sdb, sdc, etc.

Note: the vda device was created during the initial order of the virtual machine with the additional disk option. In this example, we are using the new device /dev/sdb. (Please note your device name may be different)

Step 2. Creating a new partition on a new device (disk)

You need to create a new partition with the command: fdisk /dev/sdb - where /dev/sdb is the name of the new partition.

Next, enter the following keys in sequence:
n - create partition
p - create main partition
select the partition number, its first and last sectors (Enter by default)
w - write changes to disk

The created partition can be seen by typing the command again lsblk.

2.1 Using the fdisk utility, create a new partition on a new device, in our case /dev/sdb.

Note: your device name may be different.

Enter n to create a new partition:

[root@centos ~]# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.32.1).
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.
Created a new DOS disklabel with disk identifier 0xba91dc06.
Command (m for help): n

2.2 Then choose p to create a new primary partition:

Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1

Note: Your system can only have 4 primary partitions on a single drive. If you have already reached this limit, create an extended partition.

2.3 Select the partition number and its first and last sectors, if you press Enter, then, by default, the new partition will use all available disk space:

First sector (2048-20971519, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519):
Created a new partition 1 of type 'Linux' and of size 10 GiB.

2.4 Finally, you need to write the partition to disk using the w command.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

2.5 Check if the partition has been created using the lsblk command.

add 2.5

The /dev/sdb (10Gb) device has a /dev/sdb1 (10Gb) partition.

Step 3. Analysis of the existing LVM configuration and file system

To check an existing LVM configuration, you can enter the commands: pvs - display the name and number of available physical volumes (PV) vgs - list volume groups (VG) lvs - list logical volumes (LV)

You need to determine the system partition and the type of file system on it, which you want to expand using the command df -hT.

add 3.1

In this example, we see that the system has a physical volume /dev/sda2, which is part of the created centos volume group. This volume group has 2 logical volumes: root and swap.

add 3.2

In this example, we want to expand a 28Gb partition called /dev/mapper/centos-root with an xfs file system that has a mount point /.

The structure of disk space before the LVM expansion is shown in the diagram:

Step 4. LVM extension

4.1 On the new disk, you need to create a new physical volume using the command:
pvcreate /dev/sdb1 - where /dev/sdb1 is the name of the new partition.

4.2 Extend a volume group by adding a new physical volume to it using the command: vgextend centos /dev/sdb1 - where centos is the name of the volume group (can be found using the vgs command).

4.3 Extend the logical volume by running the command:
lvextend -l +100%FREE /dev/centos/root - where
/dev/centos/root is the path of the expandable file system,
centos is the name of the group (can be found with the vgs command),
root is the name of the logical volume (can be found with the lvs command).

In the command above, instead of /dev/centos/root, you can specify another path /dev/mapper/centos-root (can be found with the
df -hT command), and the result will be the same.

4.1 Create a physical volume on the newly created partition using the pvcreate command. (in our example, the created partition is /dev/sdb1. Please note that your partition name may differ, and also check the created physical volume with the pvs command:

add 4.1

In this example, the physical volume has 10Gb free space and no volume group.

4.2 In step 3.1, we learned the name of the volume group. Extend this volume group by adding a new physical volume to it using the vgextend command.
Check the changes with the vgs command. (in our example, the volume group was called centos, the new physical volume is /dev/sdb1. Note that you will have your own group name and physical volume.
The VFree column shows the amount of free space.

add 4.2

In this example, the centos volume group has 2 physical volumes and 10Gb of free space.

4.3 In step 3.1, we learned the names of the volume group and the expandable logical volume. Extend this logical volume by running the lvextend command. (in our example, the volume group was called centos, and the logical volume is root.
Note that you will have your own name for the group and logical volume.
Make sure to expand the logical volume with the lvs command.

add 4.3

In this example, the total size of the logical volume from the centos volume group is expanded to 37.99Gb.

Step 5. Expanding the file system

Note: This operation carries the risk of data loss. It is recommended that you make a backup of the virtual machine beforehand.

To increase the file system on a logical volume, you must enter the command, depending on the distribution:

  • Fedora, CentOS, RHEL, Oracle Linux, Alma Linux, Rocky Linux (XFS file system):
    xfs_growfs /dev/mapper/centos-root - where
    /dev/mapper/centos-root - the name of the file system being expanded (the name and type can be found with the df -hT command)

  • Ubuntu, Debian (ext4 file system): resize2fs /dev/mapper/ubuntu-root - where
    /dev/mapper/ubuntu-root is the name of the expandable file system (the name and type can be found with the df -hT command)

5.1 Let’s determine the mount path of the file system of the root partition, the mount point, and the type of the file system. In our example, the path is /dev/mapper/centos-root (or /dev/centos/root),
the mount point is /,
the file system type is xfs.
Note: you will have your own path and file system type.

5.2 Next, you need to increase the size of the file system on the logical volume. Depending on the type of file system and distribution, select the type that suits you below.

5.2.1 For the XFS file system (Fedora, CentOS, RHEL, Oracle Linux, Alma Linux, Rocky Linux distributions), the xfs_growfs utility is used and the path specified in the output of the df -hT command for the expandable partition (in the example below for Centos it is /dev/mapper /centos-root).

add 5.2.1

5.2.2 For the ext4 file system (Ubuntu, Debian distributions), use the resize2fs utility and the path specified in the output of the df -hTcommand for the expandable partition (in the example below, for Ubuntu this is /dev/mapper/ubuntu-root).

add 5.2.2

Step 6. Verification.

To check the new disk size and file system, repeat the commands df -hT and lsblk.

add 6

The final configuration of disk space and the file system is shown in the diagram: