11.12.2024

Mounting a disk and creating partitions in Linux

The hard disk installed in the system may not be enough and sometimes the question arises of adding additional media. Let's look at how to mount a disk on a Virtual Private Server (VPS), Virtual Dedicated Server (VDS), and Standalone Server.

Preparation

Before you start working, make sure that the disk exists in the system. Sometimes the presence of a device in the BIOS may not be sufficient. Check the available drives with the command:

sudo fdisk -l

Screenshot - 1. Checking available discs by command.

In our case, there are 2 disks in the system - vda and vdb, both 10 GB each, the first system disk. We will work with the second disk (vda).

It is important to remember that Linux systems can be damaged by incorrectly specifying a drive, such as a system one. Carefully check and double-check media selection. The fact that the new disk (out of the box) does not have a file system or partitions helps.

Disk marking

To mark the disk, run the fdisk utility with the path to the disk:

fdisk /dev/vdb

Screenshot 2. - Partitioning the drive using command.

When you press “m” and confirm the entry with Enter, the program will provide a page of available commands:

Reference:

DOS (MBR)

a toggle a bootable flag

b edit nested BSD disklabel

c switch the DOS compatibility flag

General

d delete a section

F list free unpartitioned space

l list of known partition types

n add a new section

p show partition table

t change partition type

v check the partition table

i print information about the partition

Miscellaneous

m show this menu

u change display/input units

x additional features (only for experts)

Script

I load disk layout from sfdisk script file

O dump disk layout to sfdisk script file

Save and exit

w save the table to disk and exit

q exit without saving changes

Create a new tag

g create a new empty GPT partition table

G create a new empty SGI partition table (IRIX)

o create a new empty DOS partition table

s create a new empty Sun partition table

Since we will create a simple partition (not bootable), we will use the entire disk space, press the “n” key and Enter.
The system will ask about the type of partition - select p - primary.

The partition number — 1

We answer the question about the first and last sector by pressing the Enter key.

Important! If you want to create several logical drives, then in response to the last sector you should specify the size in kilo-, mega-, giga-, tera-, petabytes. Accordingly, if the partition needs a size of 2 gigabytes, then specify 2G.

Save the changes by pressing “w” and confirm the selection with the Enter key.

Screenshot - 3. Setting result.

Formatting a disk

After the operation described above, the device /dev/vdb1 will be created in the system - in essence, this is a partition on the disk. Now formatting.

Modern Linux offers a choice of several file system options. The file system is created by executing the mkfs command with the keys specified, or by running one of the programs:

mkfs.bfs
mkfs.btrfs
mkfs.cramfs
mkfs.ext2
mkfs.ext3
mkfs.ext4
mkfs.ext4dev
mkfs.fat
mkfs.minix
mkfs.msdos
mkfs.ntfs
mkfs.vfat
mkfs.xfs

Formatting is performed by the command:

sudo mkfs.ext4 /dev/vdb1

Screenshot - 4. Formatting the drive.

The disk is ready to work. It remains only to mount it.

Mounting a disk

The peculiarity of the Linux OS is that it is possible to mount a disk (or another block device) in any of the directories, the main thing is that the directory is empty.

Create a directory in the /mnt directory:

sudo mkdir /mnt/1

We change the access rights to the directory. Only root and only read and write.

sudo chmod -R 660 /mnt/1

We mount:

sudo mount /dev/vdb1 /mnt/1

To mount the disk automatically when the system boots, edit the /etc/fstab file. Open with any text editor, for example, nano:

sudo nano /etc/fstab

At the very end of the file, insert the line:

/dev/vdb1 /mnt/1 ext4 defaults 0 0

Save the file.