Skip to content

Connecting and Disconnecting a Disk in Linux

In this article

Note

For VPS servers use the upgrade plan option. Below is the instruction only for dedicated Linux servers.

Connecting a Disk

After attaching the disk, you need to prepare it within the OS. The process consists of four stages:

  1. Create a partition;
  2. Format (create a filesystem);
  3. Mount;
  4. Add to /etc/fstab for autostart.

Before preparing the disk, you need to identify the disk names present in the system.

You can list disks with the following command:

lsblk
Example output:

NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda      8:0    0   15G  0 disk
└─sda1   8:1    0   15G  0 part /
sdb      8:16   0   10G  0 disk   # new disk (no partitions)
vda    252:0    0    1M  1 disk

In this example, the new disk is /dev/sdb. All commands below use it as an example. Replace sdb with the actual name in your system.

1. Creating a Partition (fdisk)

Start the interactive mode for the disk:

sudo fdisk /dev/sdb

Sequence of actions:

  • Press n > create a new partition;
  • Choose p > primary;
  • Press Enter three times (keep default values: whole disk, first sector, etc.);
  • Press w > write changes and exit.

Note

The MBR partition table (the fdisk default for an empty disk) does not support disks larger than 2 TB. For such disks, press g before creating the partition - a GPT partition table will be created (you will not need to choose p in this case).

Check the result:

lsblk

A new partition should now appear, e.g., /dev/sdb1.

2. Creating a Filesystem

For example, use ext4 — a reliable and widely supported filesystem:

sudo mkfs.ext4 /dev/sdb1

Note

Alternatives: xfs, btrfs, ntfs (via mkfs.ntfs) — depending on your needs.

3. Mounting the Disk

Create a mount point (directory):

sudo mkdir -p /mnt/disk2

Mount the partition:

sudo mount /dev/sdb1 /mnt/disk2

Verify:

df -Th

The disk is now ready for use: all files in /mnt/disk2 will be stored on the new disk.

4. Automatic Mounting at Boot (/etc/fstab)

Using the UUID (instead of /dev/sdb1) ensures stability when disk order changes.

Retrieve the partition's UUID:

lsblk -o NAME,UUID

Example output:

NAME   UUID
sda
└─sda1 073e596c-0101-4b87-8f4e-40f96b90baa9
sdb
└─sdb1 1a138d62-46e2-4c2c-9d41-f488d6e340a4

Open /etc/fstab:

sudo nano /etc/fstab

Add the following line (replace the UUID and path as needed):

UUID=1a138d62-46e2-4c2c-9d41-f488d6e340a4  /mnt/disk2  ext4  defaults  0  2

Note

defaults — standard mount options (rw, suid, dev, exec, auto, nouser, async); 0 — not included in dumps; 2 — filesystem check at boot (after root).

Check the entry without rebooting - the command mounts everything listed in /etc/fstab and reports errors:

sudo systemctl daemon-reload
sudo mount -a

Attention

An error in /etc/fstab can prevent the server from booting. Do not reboot the server until mount -a completes without errors. For disks that may be absent at boot, add the nofail option (for example, defaults,nofail).

Disconnecting the Disk

A local disk cannot be removed if the server has a recovery point. First delete it in the control panel.

Steps:

  1. Edit /etc/fstab and comment out or remove the line with the disk:

    sudo nano /etc/fstab
    # UUID=... /mnt/disk2 ext4 defaults 0 2
    
  2. Unmount the disk:

    sudo umount /mnt/disk2
    

    Note

    If you get the target is busy error, find out which processes are using the disk with sudo lsof +f -- /mnt/disk2 or sudo fuser -vm /mnt/disk2 and stop them. The -l (lazy, umount -l) flag only hides the mount point, and the actual unmounting happens after all files are closed - use it only as a last resort.

  3. Remove the mount point (optional):

    sudo rmdir /mnt/disk2
    
question_mark
Is there anything I can help you with?
question_mark
AI Assistant ×