Mounting a fresh data disk on a Linux VM

By Ward Pieters on

Intro

Every now and then I attach an extra virtual disk to a VM. This post documents the full flow I used on a Debian-based VM so future me (and you) can mount a new drive without googling the steps again.

Prerequisites

  • You already attached the block device to the VM (in my case it shows up as /dev/sdb).
  • You have root privileges.

Partition the disk

Run fdisk against the new device and create a single primary partition that spans the entire disk.

Make sure to replace /dev/sdb with the actual device name of your new disk. You can confirm the device name with lsblk if you're unsure.

fdisk /dev/sdb

Inside the interactive prompt, press the keys in this order:

  1. n to create a new partition.
  2. p to make it a primary partition.
  3. 1 to accept partition number 1.
  4. Press Enter twice to accept the default first/last sector (fills the disk).
  5. w to write the changes and exit.

If you rerun fdisk -l /dev/sdb you should now see /dev/sdb1 listed.

Create an ext4 filesystem

Format the new partition with ext4. This wipes any existing data on /dev/sdb1, so double-check the device path before continuing.

mkfs.ext4 /dev/sdb1

Create the mount point

Pick a descriptive directory name for the mount point so you remember what lives there. I used /mnt/big_data.

mkdir -p /mnt/big_data

Add it to /etc/fstab

To make the mount persistent across reboots, add a line to fstab. I still use nano for quick edits, but use whatever editor you prefer.

nano /etc/fstab

Append the following line to the bottom of the file:

/dev/sdb1  /mnt/big_data  ext4  defaults  0  2

Save and exit the editor. This tells mount to treat /dev/sdb1 as an ext4 filesystem, mounted at /mnt/big_data, and to perform a non-root filesystem check at boot.

Reload systemd units and mount everything

Whenever fstab changes, I run a daemon reload so systemd notices the new mount unit. Then mount all entries to verify the syntax.

systemctl daemon-reload
mount -a

If mount -a returns silently, the entry is good. To be extra sure, run lsblk and confirm the partition shows the mount point you expect.

lsblk

At this point the VM automatically mounts /dev/sdb1 on /mnt/big_data during boot.

Problems?

If you find mistakes or have suggestions, let me know. If you encounter problems, first check that the disk is properly attached and recognized by the OS with lsblk. Then review the fstab entry for typos. You can also check system logs with journalctl -xe for any mount-related errors.