How to Partition, Format, and Mount Secondary Drives in Linux - Manual Drive Configuration (MDC)

How to Partition, Format, and Mount Secondary Drives in Linux - Manual Drive Configuration (MDC)

Introduction

This guide provides detailed instructions on how to manually configure secondary drives in a Linux system, including partitioning, formatting, and mounting the drives.

Warning

Performing these steps on an existing primary drive or a drive with important data will result in data loss. These instructions should be applied only to new, unpartitioned, or secondary drives. Ensure you have verified the correct drive identifiers before proceeding. Always back up any important data before making changes to disk partitions or filesystems.

Prerequisites

  1. Administrative (root) access to the Linux system.
  2. Basic understanding of Linux commands and file systems.
  3. The parted, mkfs, and blkid tools installed on your system.
To install these tools, use the following commands:
sudo apt-get install parted e2fsprogs util-linux  # For Debian/Ubuntu-based systems
sudo yum install parted e2fsprogs util-linux      # For RHEL/CentOS-based systems
sudo dnf install parted e2fsprogs util-linux      # For Fedora-based systems

Steps

1. Identify Disk Devices

First, identify the available disk devices on your system:
sudo fdisk -l | grep Disk
Command Breakdown
sudo fdisk -l
sudo: Runs the command with root privileges. Many disk operations require root access to ensure you have the necessary permissions to view and manipulate disk partitions.
fdisk: A command-line utility for disk partitioning. It is used to create, delete, modify, and manage disk partitions.
-l: Lists the partition tables for all the disks that fdisk can find. This shows detailed information about each disk and its partitions.
| grep Disk
|: A pipe operator that takes the output of the command on the left (fdisk -l) and passes it as input to the command on the right (grep Disk).
grep Disk: Filters the input and returns only the lines that contain the word "Disk". This helps in quickly identifying the available disks and their sizes without showing detailed partition information.
In the below example output. sda is the primary drive. We should not perform any configuration on this drive since OS is installed on this drive. Need to partition, format and mount only the secondary drives sdb, sdc, sdd and sde.
Example Output
Disk /dev/sda: 512 GB, 512110190592 bytes, 1000215216 sectors
Disk /dev/sdb: 2000.4 GB, 2000398934016 bytes, 3907029168 sectors
Disk /dev/sdc: 4000.8 GB, 4000787030016 bytes, 7814037168 sectors
Disk /dev/sdd: 4000.8 GB, 4000787030016 bytes, 7814037168 sectors
Disk /dev/sde: 4000.8 GB, 4000787030016 bytes, 7814037168 sectors
Disk /dev/mapper/ubuntu--vg-root: 512 GB, 512110190592 bytes, 100
In this example, the disks /dev/sda, /dev/sdb, /dev/sdc, /dev/sdd, and /dev/sde are listed with their respective sizes. 

2. Check Disk Partitions and Mount Points

Execute the following command to list all block devices and their partitions:
sudo lsblk
Command Breakdown
lsblk: Lists information about all available or specified block devices. This utility provides a tree-like overview of the devices, their partitions, and their mount points.
Example Output
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda           8:0    0  512G  0 disk 
├─sda1        8:1    0   512M  0 part /boot/efi
├─sda2        8:2    0 476.3G  0 part /
└─sda3        8:3    0   328K  0 part [SWAP]
sdb           8:16   0   1.8T  0 disk 
sdc           8:32   0   3.6T  0 disk 
sdd           8:48   0   3.6T  0 disk 
sde           8:64   0   3.6T  0 disk 

3. Partition Drives

3.1 Less than 2TB:

3.1.1 Create an MSDOS partition table:

sudo parted /dev/sdb mklabel msdos
If there are multiple 2TB drives. Create a 'msdos' partition by changing the disk specification (sdc/sdd/sde) in the command.
Command Breakdown
parted: The command-line utility for partitioning disks.
/dev/sdb: Specifies the disk to partition.
mklabel msdos: Creates an MSDOS partition table on the specified disk.

3.1.2 Create a primary partition:

sudo parted -s /dev/sdb mkpart primary 1 -- -1
To create a primary partition on /dev/sdb that starts at 1MB and extends to the end of the disk.
Command Breakdown
parted: A command-line utility for managing hard disk partitions. It can create, resize, and delete partitions.
-s: Runs parted in script mode. This option suppresses all interactive prompts, which is useful for scripting and automation.
/dev/sdb: Specifies the target disk device to be partitioned. Replace /dev/sdb with the appropriate device name if different.
mkpart: The command within parted to create a new partition.
primary: Specifies the type of partition to create. Options include primary, logical, and extended. Here, a primary partition is being created.
1: The starting point of the partition in megabytes. This can also be specified in other units like 1MiB, 1GB, etc.
-- -1: The ending point of the partition. -1 specifies the end of the disk, creating a partition that spans from 1MB to the end of the disk.

3.2 Greater than 2TB:

3.2.1 Create a GPT partition table:

sudo parted /dev/sdb mklabel gpt
To create a GPT partition on the specified disk.

3.2.2 Create a primary partition:

Kindly refer section 3.1.2.

4. Formatting Drives

File System Size Limits
ext3: up to 8TB (only if specifically requested by the customer)
ext4: up to 16TB
xfs: from 16TB to 8EB
ufs: all sizes
Format the drives with the appropriate filesystem.
sudo mkfs.ext4 -L disk1 /dev/sdb1
To create an ext4 filesystem on /dev/sdb1 and label it as disk1.
Command Breakdown
mkfs.ext4: The command to create an ext4 filesystem on a specified partition or disk.
-L disk1: Assigns a label (disk1) to the filesystem. This label can be used to identify the filesystem when mounting.
/dev/sdb1: Specifies the partition on which to create the ext4 filesystem. Replace /dev/sdb1 with the appropriate partition if different.
Example Output
mke2fs 1.45.5 (07-Jan-2020)
Creating filesystem with 488378368 4k blocks and 122101760 inodes
Filesystem UUID: e1a9b6d5-6d67-4c8c-9f21-0d8a2b58d1c2
Superblock backups stored on blocks: 
32768, 98304, 163840, 229376, 294912, 819200, 884736, 
1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 
23887872, 71663616

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done
Explanation of Example Output:
mke2fs 1.45.5: The version of the mke2fs utility being used.
Creating filesystem with ... blocks and ... inodes: Indicates the size of the filesystem being created.
Filesystem UUID: A unique identifier assigned to the filesystem.
Superblock backups stored on blocks: Lists blocks where superblock backups are stored.
Allocating group tables, Writing inode tables, Creating journal: Progress messages showing the creation of various filesystem structures.
Writing superblocks and filesystem accounting information: done: Indicates that the filesystem creation process has completed successfully.

5. Mounting Drives

5.1 Identify the Disk UUIDs

A command-line utility to locate/print block device attributes. The blkid command prints information about available block devices, including their UUIDs, labels, and filesystem types.
blkid
Example Output
/dev/sda1: UUID="2E24-96A5" TYPE="vfat" PARTLABEL="EFI System Partition" PARTUUID="1d3a9e74-0c4d-4aee-8248-2d0a6f9b23b1"
/dev/sda2: UUID="1b09ae30-7e74-4d6c-88b6-e59f7d8b26a6" TYPE="ext4" PARTUUID="3e5dc77f-5f7d-4d6e-9c2f-1e4a99b5ed42"
/dev/sda3: UUID="a71e2e84-18f4-4c5f-8989-f7356f48a1a0" TYPE="swap" PARTUUID="c2b1b88d-3e49-45f2-a920-1c5b88e32b93"
/dev/sdb1: LABEL="disk1" UUID="e1a9b6d5-6d67-4c8c-9f21-0d8a2b58d1c2" TYPE="ext4" PARTUUID="be839f16-019e-4a83-9ff6-7f0a6c88c5b1"
Document the secondary partition UUID.

Explanation of Example Output

/dev/sda1, /dev/sda2, /dev/sda3, /dev/sdb1: Device names for partitions on the disks.

UUID: The Universally Unique Identifier for the filesystem on the partition. For example, UUID="e1a9b6d5-6d67-4c8c-9f21-0d8a2b58d1c2" uniquely identifies the filesystem on /dev/sdb1.

TYPE: The type of filesystem on the partition (e.g., ext4, vfat, swap).

LABEL: The label assigned to the filesystem, such as disk1 for /dev/sdb1.

PARTLABEL: The label for the partition, if any (e.g., EFI System Partition for /dev/sda1).

PARTUUID: The unique identifier for the partition itself, distinct from the filesystem UUID.

5.2 Create Mount Points

Create directories where the disks will be mounted:
mkdir /disk1
mkdir /disk2
mkdir /disk3
mkdir /disk4

5.3 Backup and Edit /etc/fstab

5.3.1 Create a backup of the fstab file:

cp /etc/fstab /root/fstab.bak

Command Breakdown
cp: The command used to copy files or directories.
/etc/fstab: The source file, which is the filesystem table file that defines how disk partitions and other devices are mounted.
/root/fstab.bak: The destination file, a backup copy of the fstab file, stored in the /root directory with the name fstab.bak.

5.3.2 Edit the fstab file to include the new partitions:

sudo nano/etc/fstab
Add the following lines, replacing the UUIDs with the ones from your blkid output in the following format:
UUID=7bfe4cc6-c79a-434f-9028-7e03908466a9  /disk1  ext4  defaults  1 2
UUID=9e56b27c-de41-4a2a-be3d-9cc5b3da932d  /disk2  ext4  defaults  1 2
UUID=some-uuid-for-disk3  /disk3  ext4  defaults  1 2
UUID=some-uuid-for-disk4  /disk4  ext4  defaults  1 2
Explanation of Fields
UUID: The universally unique identifier for the filesystem. This ensures that the correct filesystem is mounted even if the device name changes.
<mount point>: The directory where the filesystem will be mounted.
<type>: The type of filesystem, such as ext4, xfs, etc.
<options>: Mount options, such as defaults, noatime, nodiratime, etc.
<dump>: Controls the backup utility dump; 0 indicates no dump, and 1 indicates the filesystem should be dumped.
<pass>: Controls the order in which filesystems are checked at boot time by fsck. 0 means no check, 1 is the first filesystem to be checked, and 2 is for other filesystems
Save and exit the editor (i to insert, Esc to exit insert mode, :wq! to write and quit).

5.4 Mount All Drives

5.4.1 Mount the new filesystems:

mount -a
The mount -a command is particularly useful after making changes to /etc/fstab. It allows you to mount all the filesystems specified in the file without having to reboot the system.

5.4.2 Verify the mounts:

df -h
To display the disk space usage of all mounted filesystems in a human-readable format
Example Output
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       476G   20G  432G   5% /
udev            7.9G     0  7.9G   0% /dev
tmpfs           1.6G  1.9M  1.6G   1% /run
tmpfs           7.9G  252M  7.7G   4% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           7.9G     0  7.9G   0% /sys/fs/cgroup
/dev/sdb1       1.8T  100M  1.7T   1% /disk1
/dev/sdc1       1.8T  100M  1.7T   1% /disk2
/dev/sdd1       1.8T  100M  1.7T   1% /disk3
/dev/sde1       1.8T  100M  1.7T   1% /disk4
Explanation of Output
Filesystem: The name of each mounted filesystem.
Size: The total size of each filesystem.
Used: The amount of space used on each filesystem.
Avail: The amount of space available on each filesystem.
Use%: The percentage of the filesystem that is used.
Mounted on: The directory where the filesystem is mounted.

Conclusion

Following these steps, you can successfully partition, format, and mount secondary drives in a Linux system using manual drive configuration (MDC). This ensures that the drives are correctly set up and accessible upon system startup. Always verify each step to avoid data loss and ensure system stability.
    • Related Articles

    • Backup, Snapshot, and Restore Linux installations using Timeshift

      If you mess up (or are going to mess up) some config files or drivers, it is always good to have a fallback point. The de-facto standard for doing this on Linux is Timeshift. Timeshift for Linux is an application that provides functionality similar ...
    • What is Boot Process in Linux OS.

      Boot Process in Linux OS. Have you ever wondered what happens behind the scenes from the time you press the power button until the Linux login prompt appears? Press the power button on your system, and after few moments you see the Linux login ...
    • Lexar JumpDrive Fingerprint F35 Flash Drive Manual

      Contents Before You Start ................................................................................. 1 For Users ............................................................................................................... 1 Specifications ...
    • Troubleshooting storage drive not detected

      A storage drive not being detected can be due to a variety of factors, and the solution depends on the underlying cause. Here are some of the major parameters or reasons why a storage drive may not be detected and the corresponding solutions: Cable ...
    • Mount NTFS File System Flash Drive in Centos7.

      To Mount NTFS File System USB Drive/Pen Drive/Flash Drive execute following Commands: - Install NTFS Package. sudo yum install -y epel-release ntfs-3g Now mount Pen Drive. mkdir /mnt/pendrive sudo mount -t ntfs-3g /dev/sdd1 /mnt/pendrive Note: ...