Mounting a new disk under Linux: partitioning, formatting and fstab

Linux 4 min read

If a new disk is added to the server (for example, a second disk for a dedicated server, or a separate data disk assigned to a VPS), the system will recognise it, but will only be able to use it once it has been partitioned, a file system created on it, and it has been mounted to a directory. This article explains these steps.

If you have not been issued with a new disk but your existing disk has been upgraded to a larger capacity, the article ‘How can I resize the file system after expanding my VPS disk?’ applies to you.

Please note: partitioning and formatting will delete all data on the disk. Before each step, check that you are working on the correct disk.

1. Identifying the new disk

lsblk -o NAME,FSTYPE,SIZE,MOUNTPOINTS

On an AlmaLinux 10 test machine, with a new 6 GB hard drive:

NAME   FSTYPE   SIZE MOUNTPOINTS
vda              10G 
├─vda1 vfat     200M /boot/efi
├─vda2 xfs        1G /boot
└─vda3 xfs      8.8G /
vdb    iso9660  900K 
vdc               6G

The new disk is the one that has no partitions, file system or mount point; in the example, this is vdc. (vdb here is a small, read-only configuration disk used by the installer; not all servers have one.) Do not tamper with the partitions on the system disk (vda or sda). We tested these steps on this machine; the commands use the more general name vdb – please replace this with the name of your own disk.

2. Partitioning

Create a GPT partition table and a partition that spans the entire disk:

sudo parted /dev/vdb --script mklabel gpt mkpart primary 0% 100%

Check: the lsblk command displays the vdb1 partition in its output.

3. Creating a file system

XFS is the default on AlmaLinux and Rocky Linux, whilst ext4 is the default on Ubuntu and Debian; either is a good choice.

# XFS
sudo mkfs.xfs /dev/vdb1

# vagy ext4
sudo mkfs.ext4 /dev/vdb1

4. Uploading

Create the directory where the disk will be accessible, and mount it:

sudo mkdir -p /mnt/data
sudo mount /dev/vdb1 /mnt/data
df -h /mnt/data

5. Permanent mounting in the /etc/fstab file

A disk mounted using the mount command will not be remounted after a reboot. To ensure this happens, you must add it to the /etc/fstab file. The device name (/dev/vdb1) may change upon reboot, so use the partition’s unique identifier (UUID):

sudo blkid /dev/vdb1
/dev/vdb1: UUID="3f1c9a2e-7b4d-4e0a-9c55-1d2e3f4a5b6c" TYPE="xfs" ...

Make a copy of the fstab file, then add the following new line:

sudo cp /etc/fstab /etc/fstab.bak
sudo nano /etc/fstab
UUID=3f1c9a2e-7b4d-4e0a-9c55-1d2e3f4a5b6c  /mnt/data  xfs  defaults,nofail  0 0

For the ext4 file system, the type is ext4 and the last number is 2 (the file system is checked at start-up). The nofail setting ensures that the server starts up even if the disk is unavailable for some reason.

Check the configuration before restarting. Due to an incorrect fstabe entry, the server will not start and can only be repaired via the console.

sudo umount /mnt/data
sudo systemctl daemon-reload
sudo mount -a
sudo findmnt --verify
df -h /mnt/data

When the settings are correct, findmnt --verify displays the following: Success, no errors or warnings detected. For a line with a misspelt UUID, the test returned the following:

/mnt/rossz
   [E] unreachable on boot required target: No such file or directory
   [E] unreachable on boot required source: UUID=00000000-1111-2222-3333-444444444444

As for mount -a: mount: /mnt/rossz: can't find UUID=…. If mount -a returns an error, or if df does not show the drive, correct the line before restarting the server. In the test, the drive added with the correct line mounted automatically after the restart.

6. Permissions

The root directory of the mounted drive is owned by the root user. If a user or a service (such as the web server) also wishes to write to it, set the owner as follows:

sudo chown -R janos:janos /mnt/data

For more information on permissions: File permissions (chmod, chown) on Linux. On AlmaLinux, if the disk will contain web content or a database, SELinux labels must also be configured (see: SELinux: why am I getting a ‘Permission denied’ error?).

Using LVM

If you wish to expand your storage space using multiple disks at a later date, instead of partitioning and formatting the disk, you can also set it up as an LVM volume (see: What is LVM?):

sudo pvcreate /dev/vdb
sudo vgcreate data /dev/vdb
sudo lvcreate -l 100%FREE -n adat data
sudo mkfs.xfs /dev/data/adat

Next, mount the /dev/data/adat volume as described in steps 4–5 above (the name /dev/data/adat can also be specified in the fstab_line; the name of the LVM volume remains unchanged). On AlmaLinux 9 and 10, the first pvcreate command creates the /etc/lvm/devices/system.devices file (“Creating devices file”): LVM will then only manage the disks listed here; pvcreate and vgextend will automatically add new ones.

Back: Linux articles (62)