Formatting & Mounting Storage Volumes in Linux

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
Search for a command to run...

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
The init system is the very first process that starts on the system and its job is to schedule and manage all other processes on the system. It is also referred to as PID 1 short for process id 1. When a process such as Apache or Nginx is started on ...
The git log command is used to view the commit history of a Git repository. You can customize the output format to show specific details in a more readable or structured way. Below are some useful variations and formatting options for git log: To sh...

Overview This article will teach us how to: Install Ubuntu Server 24.04 on a laptop from scratch Install Ubuntu server by using Vagrant tool with ISO image Prerequisites: A laptop without any OS installed; A virtualization software Virtualbox t...

The /etc/passwd and /etc/shadow files are the backbone of Linux user management. Together, they store user account information and handle authentication securely. This article provides a hands-on guide to understanding these files, their structure, a...

In Linux, links are powerful tools that allow you to create references to files and directories. There are two main types of links: hard links and soft links (also known as symbolic links or symlinks). Understanding the differences between these two ...

A hostname is a human-friendly name given to a computer. It is a unique identifier that allows us to identify the machine in various network communications, making it easier to locate and manage devices. Type: hostname To see where it is stored, typ...

Physical storage devices (hard disk, SSD, floppy disk, etc.) are the actual hardware components that store data. A single physical storage device can be divided into multiple smaller usable sections, each acting as a separate volume.

Hard Disk 1 and Hard Disk 2 are physical disks;
"/dev/sda1", "/dev/sda2", and "/dev/sdb" are volumes or partitions;
Any of these can be called a "drive".
In Linux, storage devices are typically represented as files in the /dev directory. Typically, files representing storage devices start with sd or hd followed by a letter.
For example, the first physical drive we attach to the system will be /dev/sda. For the subsequent physical storage drive we attach to the system, they’ll get the identifier in the form of sd{alphabet}, where the alphabet is the next letter in the line.
The kernel provides a name to every storage device plugged in or added to the computer or server on Linux.

Partitioning is the process of dividing the storage device into segments. To partition the disk in Linux utilities such as fdisk, gdisk, parted and the partitioning schemes such as MBR (Master Boot Record), GPT (GUID Partition Table) are used.
MBR is over 30 years old. Because of its age, it has some serious limitations. For instance, it cannot be used for disks over 2TB, and can only have a maximum of four primary partitions.
GPT is a more modern partitioning scheme that resolves some of the issues inherent in MBR. Systems running GPT can have many more partitions per disk. This is usually only limited by the restrictions imposed by the operating system itself. Additionally, the disk size limitation does not exist with GPT and the partition table information is available in multiple locations to guard against corruption. GPT can also write a “protective MBR” for compatibility with MBR-only tools. In most cases, GPT is the better choice unless your operating system prevents you from using it.
Each partition can be used for a different purpose, This gives a user more flexibility, allowing them to potentially segment a single disk for multiple operating systems, swap space, or specialized filesystems.
As a next step after partitioning, formatting and mounting of the storage volume should be done to prepare the disk for use.
While the Linux kernel can recognize a raw disk, it must be formatted to be used. Formatting is the process of writing a filesystem to the disk and preparing it for file operations. A filesystem is a system that structures data and controls how information is written to and retrieved from the underlying disk. The storage device can not be used for standard filesystem operations without a filesystem. Common Linux filesystems include ext4, XFS, BTRFS, and NTFS (for Windows compatibility).
The general syntax for formatting disk partitions in Linux is:
sudo mkfs [options] [-t type ] [fs-options] device [size]
mkfs - used to build a Linux filesystem on a device, usually a hard disk partition;
-t, --type - Specify the type of filesystem to be built. If not specified, the default filesystem type (currently ext2) is used.
device argument is either the device name (e.g. /dev/hda1, /dev/sdb2) or a regular file that shall contain the filesystem.
size argument is the number of blocks to be used for the filesystem.
Mounting is the process of attaching a formatted partition or drive to a directory within the Linux filesystem. In Linux and other Unix-like operating systems, the entire system, regardless of how many physical devices are involved, is represented by a single unified file tree. When a filesystem on a drive or partition is to be used, it must be attached to the existing tree. The drive’s contents can then be accessed from that directory.
Drives are almost always mounted on dedicated empty directories – mounting on a non-empty directory means that the directory’s usual contents will be inaccessible until the drive is unmounted. Many different mounting options can be set to alter the behavior of a mounted device. For example, the drive can be mounted in read-only (RO) mode to ensure its contents won’t be changed.
The standard form of the mount command is:
sudo mount -t type device dir
This tells the kernel to attach the filesystem found on device (which is of type type) at the directory dir. The option -t type is optional. The mount command is usually able to detect a filesystem. The root permissions are necessary to mount a filesystem by default.
A disk partition must be formatted and mounted before use. The formatting process can also be done for several other reasons, such as changing the file system, fixing errors, or deleting all data.
This hands-on exercise shows how to format and mount disk partitions in Linux using ext4 file system.
To list all block devices attached to the local computer or server, use:
lsblk
The terminal prints out a list of all block devices which are files that represent devices such as hard drives, RAM disks, USB drives, and CD/ROM drives as well as information about them:
NAME – Device names
MAJ:MIN – Major or minor device numbers. These are the major and minor device numbers used by the Linux kernel to uniquely identify the type of device (major) and the specific instance of that device type (minor).
RM – Whether the device is removable (1 if yes, 0 if no)
SIZE – The size of the device
RO – Whether the device is read-only (1 if yes, 0 if no)
TYPE – The type of the device
MOUNTPOINT – Device’s mount point

loop0 - Loop devices are a special block device that maps a normal file onto a virtual block device. This allows the file to be treated as if it were a physical disk, making it possible to mount a filesystem within a file onto the Linux file system hierarchy. Linux names these devices loop0, loop1, etc., based on their order of creation or mapping.
sda - The name is derived from "SCSI Disk A," where "SCSI" stands for Small Computer System Interface, a set of standards for physically connecting and transferring data between computers and peripheral devices. Despite its origins, the naming convention is used broadly for devices using various interfaces, including SATA, which is very common in HDDs and SSDs. The letter a indicates that this is the first device recognized by the system. Additional partitions on this device would be listed as sda1, sda2, etc., representing different storage segments that were allocated for different purposes, such as system files, user data, or swap space. Subsequent devices would be named sdb, sdc, and so on.
xvda - This naming convention suggests that this block device is a virtual disk, typically seen in environments using Xen or KVM virtualization, or cloud platforms like AWS EC2.
To display a list of all block devices containing file system information, add the -f option:
lsblk -f

This allows us to locate a partition for formatting and mounting. In this case, we can see that sdc partition is empty and not allocated for any specific purpose by the system or cloud provider. Therefore, it can be used for formatting and mounting.
To view what partitions are available on a storage device:
sudo fdisk -l
fdisk - a dialog-driven program for the creation and manipulation of partition tables. It understands GPT and MBR partition tables.
-l, --list - lists the partition tables for the specified devices and then exit.

To format a disk or disk partition with the file system, use the following command:
sudo mkfs -t ext4 /dev/sdc
Make sure the correct disk is targeted. Then, verify the file system change using the command, lsblk -f

Create a new folder under the /mnt to mount the formatted storage device before using the disk:
sudo mkdir /mnt/mountpoint
ls -l /mnt
This command creates a directory that will serve as the mount point for the new filesystem.

/media for temporary storage volumes and /mnt for permanent storage volumesTo mount the disk, use the following command and verify if the disk is mounted:
sudo mount /dev/sdc /mnt/disk3
df -h

To get a list of all mounted storage devices on the system:
mount
mount | grep sdc
To remove a filesystem from the directory hierarchy, unmount it with the umount command:
sudo umount /mnt/mydisk
lsblk -f

Once a disk is successfully formatted and mounted on the Linux system, it's ready for use! This new storage space can serve various purposes depending on your needs such as storing personal or application data, backing up storage, storing Virtual Machine (VM) disks etc;