Linux System Administration

0 of 83 lessons complete (0%)

Managing Unix File Systems

Formatting a Disk Partition

You don’t have access to this lesson

Please register or sign in to access the course content.

Formatting a disk in Linux means preparing a storage device (such as a hard drive, SSD, or USB drive) for use by creating a new file system on it. This process wipes out all existing data on the disk and creates a structure to store new files. It involves two main steps:

  1. Partitioning the disk: Dividing the disk into logical sections called partitions.
  2. Creating a file system: Formatting each partition with a specific file system (e.g., ext4, xfs, FAT32).

Steps to Format a Disk in Linux

Step 1: Identify the Disk

Before you can format a disk, you need to identify it. You can use the lsblk or fdisk commands to list available disks and their partitions.

lsblk

This will show a list of all block devices (disks and partitions). For example:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   500G  0 disk
├─sda1   8:1    0   100G  0 part /
└─sda2   8:2    0   400G  0 part /home
sdb      8:16   0   100G  0 disk

In this example, /dev/sda and /dev/sdb are disks, and sda1 and sda2 are partitions on /dev/sda.

Step 2: Unmount the Disk (If Necessary)

Before formatting, ensure that the disk is not mounted. If it is mounted, unmount it using the umount command.

For example, if the disk is mounted at /mnt:

sudo umount /mnt

Step 3: Partition the Disk (If Needed)

If your disk has no partitions or you want to change the partition structure, you can use tools like fdisk, parted, or gparted to create partitions.

  1. Using fdisk:
    • Open the disk for partitioning:
sudo fdisk /dev/sdb
  1. In fdisk:
    • Press n to create a new partition.
    • Press p to create a primary partition.
    • Set the partition size and press w to write the changes.
  2. Using parted:
sudo parted /dev/sdb

In parted:

  1. Use mklabel gpt to create a GPT partition table (for large disks).
  2. Use mkpart to create partitions.

Example:

mklabel gpt
mkpart primary ext4 0% 100%

Step 4: Format the Partition

After creating partitions, format them with the desired file system.

  1. Format with ext4 (a popular Linux file system):
sudo mkfs.ext4 /dev/sdb1
  1. Format with xfs:

sudo mkfs.xfs /dev/sdb1

  1. Format with exFAT (for cross-platform use, such as USB drives shared with Windows):
sudo mkfs.exfat /dev/sdb1
  1. Format with NTFS (for use with Windows):
sudo mkfs.ntfs /dev/sdb1

Step 5: Mount the Formatted Partition

After formatting the partition, you need to mount it to access the files. Create a mount point (a directory) and mount the partition.

  1. Create a Mount Directory:

sudo mkdir /mnt/mydisk

  1. Mount the Partition:
sudo mount /dev/sdb1 /mnt/mydisk
  1. Verify the Mount:
df -h

This will display a list of mounted file systems and their disk usage.

Step 6: (Optional) Add the Disk to /etc/fstab

To ensure that the partition is automatically mounted at boot, add it to the /etc/fstab file.

  1. Open /etc/fstab for editing:
sudo nano /etc/fstab
  1. Add the following line:
/dev/sdb1   /mnt/mydisk   ext4   defaults   0 2

Adjust the file system type and mount point as needed.

Example Workflow:

  1. List all disks:
lsblk
  1. Partition the disk:
sudo fdisk /dev/sdb
  1. Format the partition as ext4:
sudo mkfs.ext4 /dev/sdb1
  1. Mount the partition:
sudo mount /dev/sdb1 /mnt/mydisk
  1. Verify the mount:
df -h

Important Notes:

  • Backup your data: Formatting a disk will erase all data. Make sure to back up important files.
  • File system choice: Choose the file system based on your needs. ext4 is generally the default for Linux systems, while exFAT or NTFS is preferable for cross-platform (Linux and Windows) usage.