CodeGym /Courses /Docker SELF /Formatting Partitions: `mkfs`

Formatting Partitions: `mkfs`

Docker SELF
Level 6 , Lesson 1
Available

Formatting Partitions: mkfs

1. What Happens During Formatting?

When we format a partition, it's like creating a "clean slate" for data. Formatting wipes out all the content of the partition and sets up a new file system, which defines how files will be organized and accessed.

Think of a library. Formatting is like the process of removing all old books, setting up new shelves, and labeling each shelf with a genre. Now everything is organized—you know exactly where each new book should go.

Why Do You Need to Format Devices?

  1. Preparing a New Device (Disk or Partition): You can't use any device without formatting it first. It's like blank pages in a notebook—useless until you define their purpose.
  2. Changing the File System: For example, if you need to switch from FAT32 to ext4, you'll need to format it.
  3. Clearing Old Data: When you want to completely wipe a device and start fresh, formatting is the best choice.

Important: After formatting, all data on the partition will be destroyed. Always double-check that there's no important info on the device.


2. Tool mkfs: Syntax and Usage

The mkfs program is your main tool for formatting partitions. It supports a wide range of file systems, so you can use it in almost any situation.

General Command Syntax

mkfs.<file_system_type> <device>

For example, to format the /dev/sdb1 partition into the ext4 file system:

mkfs.ext4 /dev/sdb1

Supported File Systems

mkfs works with most of the commonly used file system types. Here's just a few of them:

File System Command Usage
ext4 mkfs.ext4 The main Linux file system, supporting large files and disks.
xfs mkfs.xfs High performance, suitable for large files and server workloads.
vfat (FAT32) mkfs.vfat Good for data sharing between Linux, Windows, and MacOS.
ntfs mkfs.ntfs For compatibility with Windows (though editing from Linux can sometimes be tricky).

To get a full list of supported file systems, use the command:

mkfs -t help

3. Example: Formatting a Partition to ext4

Let’s break down the step-by-step process of formatting a partition.

1. Check the Device

First off, find the name of the device you want to format. Use the lsblk command:

lsblk

The output will show a list of all connected devices and their partitions. For example:

NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  100G  0 disk 
├─sda1   8:1    0   50G  0 part /
├─sda2   8:2    0   45G  0 part /home
└─sda3   8:3    0    5G  0 part [SWAP]
sdb      8:16   0  200G  0 disk 
└─sdb1   8:17   0  200G  0 part 

Let’s say we want to format the /dev/sdb1 partition.

2. Make Sure the Partition is Not Mounted

Before formatting, the device should not be mounted. To check and unmount it, use:

sudo umount /dev/sdb1

3. Start Formatting

Now, create an ext4 file system on the chosen partition:

sudo mkfs.ext4 /dev/sdb1

The command will output something like this:

mke2fs 1.45.7 (28-Jan-2021)
Creating filesystem with 52428800 4k blocks and 13107200 inodes
Filesystem UUID: 5634f623-7b2d-4d6b-b8f5-abcdef123456
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, ...
Note:

UUID is the unique identifier of the file system. We highly recommend using it to specify the mount point in /etc/fstab.

4. Verify the Result

Check that the file system has been created:

sudo blkid /dev/sdb1

The output will show that the ext4 file system is now installed on the device:

/dev/sdb1: UUID="5634f623-7b2d-4d6b-b8f5-abcdef123456" TYPE="ext4"

4. Choosing a File System

Which file system should you pick? It all depends on your needs. Here are a few recommendations:

  1. ext4: A universal choice for Linux. Works for most scenarios.
  2. XFS: If you need high performance for large files or servers.
  3. FAT32 (vfat): If you need to share the device with Windows and MacOS. Keep in mind, the max file size here is limited to 4 GB.
  4. NTFS: For compatibility with Windows, but performance may be lower compared to ext4 in Linux.
Tip for students:

If you're unsure, just go with ext4. It rarely lets you down.


5. Common Formatting Mistakes

Formatting is a task that requires special care. Here are a few possible mistakes and how to avoid them:

  • Formatting the wrong device: To prevent this, always double-check which device you're working on (lsblk and blkid are your friends here).

  • Leaving the device mounted: If the device is mounted, the mkfs command will throw an error. Make sure the device is unmounted.

  • Accidental data loss: Never run mkfs on a partition without understanding that everything will be wiped. If you're not sure, it's better to make a backup before formatting.

Practical Application

Knowing how to format is important not only for system administrators but also for developers. For example, if you work with cloud platforms like AWS or Azure, you'll frequently create and format partitions for your applications. Also, if you want to set up an external hard drive or USB stick for data storage, these skills will come in handy.

Don't forget, a properly set up file system is the foundation for stable app performance. I mean, who wants to see a "file system not found" error 5 minutes before the deadline?

Now that you know how to format a device and choose a file system, you're ready to move on to the next step: checking and fixing file systems using the fsck command.

1
Task
Docker SELF, level 6, lesson 1
Locked
Determining connected devices
Determining connected devices
1
Task
Docker SELF, level 6, lesson 1
Locked
Checking the filesystem of a device
Checking the filesystem of a device
1
Task
Docker SELF, level 6, lesson 1
Locked
Formatting and Choosing a File System
Formatting and Choosing a File System
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION