CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Disk & Storage / mkfs

mkfs

Create a filesystem on a disk or partition

Intermediatediskfilesystemadmin

mkfs writes a new, empty filesystem (ext4, xfs, vfat...) onto a disk or partition so it can be mounted and hold files. It is how a fresh cloud volume becomes usable, and it destroys whatever was on the device before.

Syntaxmkfs.TYPE [options] DEVICE (mkfs -t TYPE DEVICE is the same)

Watch it run

A new, empty 100G volume (nvme2n1) needs a filesystem before you can mount it on /archive.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
sudo blkid /dev/nvme2n1; echo "exit=$?"

No output and exit code 2: blkid found no filesystem signature. That's the check that the volume is really empty before you format it.

Anatomy of a command

$
commandsudo

Writing to a raw block device needs root.

How it works

mkfs is a small front end: mkfs -t ext4 runs mkfs.ext4, which is mke2fs from e2fsprogs; mkfs.xfs comes from xfsprogs. mke2fs opens the device, asks the kernel for its size and sector size, and on SSDs and EBS first sends discard (TRIM) for the whole device. It then divides the space into block groups (32768 blocks of 4 KiB = 128 MiB each) and writes a superblock with a new random UUID, backup superblocks in some groups (block 32768, 98304...), group descriptors, block and inode bitmaps, inode tables and a journal. The number of inodes is fixed now: one per 16 KiB by default, which is why millions of tiny files can exhaust an ext4 filesystem. XFS instead creates allocation groups and allocates inodes on demand. Data blocks themselves are not zeroed, which is why mkfs on a large disk takes seconds, not hours.

Options you'll actually use

-t TYPEWith plain mkfs: which filesystem to build (mkfs -t xfs = mkfs.xfs)
-L LABELVolume label (ext4 up to 16 chars, xfs up to 12)
-m PCT (ext4)Percentage reserved for root; default 5, 0-1 for pure data disks
-i BYTES (ext4)One inode per BYTES of space; smaller = more inodes for small files
-N NUM (ext4)Exact number of inodes to create
-T TYPE (ext4)Usage profile from /etc/mke2fs.conf: small, news, largefile, huge
-n (ext4)Dry run: print what would be done, including backup superblock locations
-F (ext4) / -f (xfs)Force: overwrite an existing filesystem without asking
-b SIZEBlock size, normally 4096
-E nodiscard / -KSkip the initial TRIM (ext4 / xfs), useful on slow thin-provisioned storage

Common mistakes

Running mkfs on a volume restored from a snapshot

A snapshot volume already contains a filesystem with data. mkfs replaces it with an empty one.

sudo blkid /dev/nvme3n1 first; if it shows a TYPE, mount it instead
Using -F / -f by habit to get past the prompt

The prompt is the only thing standing between you and wiping the wrong device.

lsblk -f and blkid to identify the device, then mkfs without force
mkfs.ext4 /dev/nvme2n1 after creating partition nvme2n1p1

Formatting the whole disk overwrites the partition table you just created.

sudo mkfs.ext4 /dev/nvme2n1p1
Keeping the 5% root reserve on a 4 TB data volume

That's 200 GB nobody can use; the reserve only protects system daemons on the root filesystem.

sudo mkfs.ext4 -m 1 ... or later: sudo tune2fs -m 1 /dev/nvme2n1

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create a 256 MB image file and put an ext4 filesystem with the label 'lab' on it.

  2. 2

    Confirm the filesystem type, label and UUID of lab.img.

  3. 3

    Without writing anything, find out where the backup superblocks of lab.img would be.

  4. 4

    Reformat lab.img with four times the default number of inodes and compare with tune2fs.

Related commands

lsblkList disks, partitions and where they are mountedblkidShow filesystem type, UUID and label of devicesmountAttach a filesystem to the directory treefsckCheck and repair a filesystemfdiskView and edit a disk's partition table
PreviousumountNextfsck
Linux / Disk & Storage / mkfs

mkfs

Create a filesystem on a disk or partition

Intermediatediskfilesystemadmin

mkfs writes a new, empty filesystem (ext4, xfs, vfat...) onto a disk or partition so it can be mounted and hold files. It is how a fresh cloud volume becomes usable, and it destroys whatever was on the device before.

Syntaxmkfs.TYPE [options] DEVICE (mkfs -t TYPE DEVICE is the same)

Watch it run

A new, empty 100G volume (nvme2n1) needs a filesystem before you can mount it on /archive.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
sudo blkid /dev/nvme2n1; echo "exit=$?"

No output and exit code 2: blkid found no filesystem signature. That's the check that the volume is really empty before you format it.

Anatomy of a command

$
commandsudo

Writing to a raw block device needs root.

How it works

mkfs is a small front end: mkfs -t ext4 runs mkfs.ext4, which is mke2fs from e2fsprogs; mkfs.xfs comes from xfsprogs. mke2fs opens the device, asks the kernel for its size and sector size, and on SSDs and EBS first sends discard (TRIM) for the whole device. It then divides the space into block groups (32768 blocks of 4 KiB = 128 MiB each) and writes a superblock with a new random UUID, backup superblocks in some groups (block 32768, 98304...), group descriptors, block and inode bitmaps, inode tables and a journal. The number of inodes is fixed now: one per 16 KiB by default, which is why millions of tiny files can exhaust an ext4 filesystem. XFS instead creates allocation groups and allocates inodes on demand. Data blocks themselves are not zeroed, which is why mkfs on a large disk takes seconds, not hours.

Options you'll actually use

-t TYPEWith plain mkfs: which filesystem to build (mkfs -t xfs = mkfs.xfs)
-L LABELVolume label (ext4 up to 16 chars, xfs up to 12)
-m PCT (ext4)Percentage reserved for root; default 5, 0-1 for pure data disks
-i BYTES (ext4)One inode per BYTES of space; smaller = more inodes for small files
-N NUM (ext4)Exact number of inodes to create
-T TYPE (ext4)Usage profile from /etc/mke2fs.conf: small, news, largefile, huge
-n (ext4)Dry run: print what would be done, including backup superblock locations
-F (ext4) / -f (xfs)Force: overwrite an existing filesystem without asking
-b SIZEBlock size, normally 4096
-E nodiscard / -KSkip the initial TRIM (ext4 / xfs), useful on slow thin-provisioned storage

Common mistakes

Running mkfs on a volume restored from a snapshot

A snapshot volume already contains a filesystem with data. mkfs replaces it with an empty one.

sudo blkid /dev/nvme3n1 first; if it shows a TYPE, mount it instead
Using -F / -f by habit to get past the prompt

The prompt is the only thing standing between you and wiping the wrong device.

lsblk -f and blkid to identify the device, then mkfs without force
mkfs.ext4 /dev/nvme2n1 after creating partition nvme2n1p1

Formatting the whole disk overwrites the partition table you just created.

sudo mkfs.ext4 /dev/nvme2n1p1
Keeping the 5% root reserve on a 4 TB data volume

That's 200 GB nobody can use; the reserve only protects system daemons on the root filesystem.

sudo mkfs.ext4 -m 1 ... or later: sudo tune2fs -m 1 /dev/nvme2n1

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create a 256 MB image file and put an ext4 filesystem with the label 'lab' on it.

  2. 2

    Confirm the filesystem type, label and UUID of lab.img.

  3. 3

    Without writing anything, find out where the backup superblocks of lab.img would be.

  4. 4

    Reformat lab.img with four times the default number of inodes and compare with tune2fs.

Related commands

lsblkList disks, partitions and where they are mountedblkidShow filesystem type, UUID and label of devicesmountAttach a filesystem to the directory treefsckCheck and repair a filesystemfdiskView and edit a disk's partition table
PreviousumountNextfsck