mkdosfs Command in Linux

Introduction

In this tutorial, we will delve into the Linux mkdosfs command, a crucial tool for systemadmin tasks. Specifically, we'll explore its use in creating a DOS filesystem on a partition or formatting a USB drive. The mkdosfs command is an integral part of the dosfstools package, a suite of utilities designed for creating and verifying MS-DOS FAT filesystems. Our focus will be on practical applications, demonstrating how to create a DOS filesystem on a partition and how to format a USB drive with a DOS filesystem. This guide will cover real-world examples and a breakdown of the most common options available for the mkdosfs command.

Introduction to mkdosfs Command

This section provides an in-depth look at the mkdosfs command. As mentioned, it's used to create DOS filesystems on partitions or format USB drives. The mkdosfs command relies on the dosfstools package, a valuable set of utilities for any systemadmin managing MS-DOS FAT filesystems.

Before we begin, let's confirm that the dosfstools package is installed on your Linux system:

sudo apt-get update
sudo apt-get install -y dosfstools

Example output:

Reading package lists... Done
Building dependency tree... Done
The following additional packages will be installed:
  libfuse2
The following NEW packages will be installed:
  dosfstools libfuse2
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.

The fundamental syntax of the mkdosfs command is as follows:

sudo mkdosfs [options] <device>

Here, <device> refers to the specific partition or device that you intend to format.

Here are some commonly used options for the mkdosfs command:

  • -F 12|16|32: This option specifies the FAT type, allowing you to choose between 12, 16, or 32 bits.
  • -n <volume-name>: Use this to assign a specific volume name.
  • -S <sector-size>: This allows you to customize the sector size. The default is 512 bytes.
  • -c: Before formatting, this option instructs the command to check the device for any bad blocks.

Let's put this into practice by creating a DOS filesystem on a partition:

sudo mkdosfs -F 32 /dev/sdb1

Example output:

mkdosfs 4.2 (2021-01-31)
/dev/sdb1 has 20971520 sectors of 512 bytes.

Creating a FAT32 filesystem in the volume with 20971520 available sectors.
Creating boot sector...
Creating FAT table...
Reserving space for root directory...
Writing directory entries...
Writing FAT tables...
Writing root directory...

In this particular example, we are creating a FAT32 filesystem on the /dev/sdb1 partition.

Creating a DOS Filesystem on a Partition

This section will provide step-by-step instructions on creating a DOS filesystem on a partition using the mkdosfs command.

First, we'll need to create a new partition on our Linux system. We can accomplish this using the fdisk command:

sudo fdisk /dev/sdb

Example output:

Welcome to fdisk (util-linux 2.38).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x1d7d1d7d.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-20971519, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-20971519, default 20971519):

Created a new partition 1 of type 'Linux' and of size 10 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

This example demonstrates the creation of a new primary partition on the /dev/sdb device.

With the new partition created, let's proceed to create a DOS filesystem on it:

sudo mkdosfs -F 32 /dev/sdb1

Example output:

mkdosfs 4.2 (2021-01-31)
/dev/sdb1 has 20971520 sectors of 512 bytes.

Creating a FAT32 filesystem in the volume with 20971520 available sectors.
Creating boot sector...
Creating FAT table...
Reserving space for root directory...
Writing directory entries...
Writing FAT tables...
Writing root directory...

As you can see, we've successfully created a FAT32 filesystem on the /dev/sdb1 partition.

Formatting a USB Drive with DOS Filesystem

This section focuses on formatting a USB drive with a DOS filesystem, another common systemadmin task. We'll again use the mkdosfs command.

The first step is to insert the USB drive into your Linux system. Use the lsblk command to identify the correct device:

lsblk

Example output:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 465.8G  0 disk
├─sda1   8:1    0   512M  0 part /boot/efi
└─sda2   8:2    0 465.3G  0 part /
sdb      8:16   1   7.5G  0 disk

In this example, the USB drive is recognized as /dev/sdb.

Now, format the USB drive using the mkdosfs command to create a DOS filesystem:

sudo mkdosfs -F 32 /dev/sdb

Example output:

mkdosfs 4.2 (2021-01-31)
/dev/sdb has 15523840 sectors of 512 bytes.

Creating a FAT32 filesystem in the volume with 15523840 available sectors.
Creating boot sector...
Creating FAT table...
Reserving space for root directory...
Writing directory entries...
Writing FAT tables...
Writing root directory...

We have successfully formatted the /dev/sdb USB drive with a FAT32 filesystem.

Summary

In this lab, we explored the mkdosfs command, a vital tool for any Linux systemadmin needing to create DOS filesystems. We covered installing the necessary dosfstools package, understanding the command's syntax and options, creating a FAT32 filesystem on a partition, and formatting a USB drive with a DOS filesystem. This knowledge is crucial for managing storage devices and ensuring compatibility across different operating systems.

400+ Linux Commands