Mastering the Mkfs Utility: A Beginner's Guide to File System Creation in Linux

Mastering the Mkfs Utility: A Beginner's Guide to File System Creation in Linux

Christopher Lv13

Mastering the Mkfs Utility: A Beginner’s Guide to File System Creation in Linux

You must create a file system before you can use any data storage device connected to a Linux computer. Learn how to use mkfs and other utilities to do just that for all sorts of file systems. We show you how.

mkfs Makes File Systems

The mkfs command makes file systems . On other operating systems, creating a file system is called formatting . Regardless of its name, it is the process that prepares a partition so that it can store data. The partition needs a way to store files, yes. But it also needs a mechanism to store the names and locations of those files, together with their metadata such as the file creation timestamp, the file modified timestamp, the size of the file, and so on. Once mkfs has built the necessary framework for handling and storing file metadata, you can start adding files to the partition.

The syntax is very simple. You just tell mkfs the device partition you want the file system created on, and what type of file system you want. That’s on the face of it. Behind the scenes, it’s a little different. For some time now on most Linux distributions mkfs has been a wrapper for mke2fs. The mkfs command calls the mke2fs command and passes it the options you’ve specified. Poor old mke2fs does all of the work but gets none of the glory.

The syntax of mkfs has been updated, and the old format has been deprecated. Both forms will work, but we’ll use the modern style in this article.

The Choice of File Systems

The modern way of using mkfs is to type “mkfs.” and then the name of the file system you wish to create.

To see the file systems that mkfs can create, type “mkfs” and then hit the Tab key twice. There’s no space after “mkfs”, just hit Tab twice.

List of supported file systems in a terminal window

The list of available file systems is displayed in the terminal window. The screenshot is from Ubuntu 18.04 LTS. Other distributions may offer more or fewer options. We’ll run through these and describe each one briefly. After a quick word about journaling.

Journaling is an important concept in file systems. The file systems records the pending file writes to a journal. As each file is written to, the journal is updated, and the pending write records are updated. This allows the file system to repair broken, partially written files that have occurred due to a catastrophic event such as a power cut. Some of the older file systems do not support journaling. Those that don’t, write to the disk less frequently because they don’t need to update the journal. They may perform faster, but they are more prone to damage due to interrupted file writes.

  • Ext2: The very first file system for Linux was the MINIX file system. It was later replaced by the first file system ever written specifically for Linux, which was Ext . Ext2 was Ext’s successor . Ext2 is not a journaling file system.
  • Ext3: This was the successor to Ext2 , and can be thought of as Ext2 with journaling, which protects your file system from data corruption caused by crashes and sudden power loss.
  • Ext4: Ext4 is the standard file system for may Linux distributions. It is a solid, tried, and trusted file system. It has features that reduce file fragmentation and can be used with larger drives, partitions, and files than Ext3.
  • BFS: This is the Boot File System , which is designed for one job and one only: to handle the files in the boot partition. It’s rare that you’d be creating a boot file system by hand. Your Linux installation process will do this for you.
  • FAT: The File Allocation Table file system was designed for floppy disks by a consortium of computer-industry heavyweights. It was introduced in 1977. The only reason you’d use this non-journaling file system is for compatibility with non-Linux operating systems.
  • NTFS: The New Technology File System is a Microsoft journaling file system introduced with Windows NT. It was the successor to FAT. The only reason you’d use this file system is for compatibility with non-Linux operating systems.
  • MINIX: Originally created by Andrew S. Tanenbaum as an educational aid, MINIX is a “mini-Unix” operating system. Nowadays, it is aimed at providing a self-healing and fault-tolerant operating system. The MINIX file system was designed as a simplified version of the Unix File System . Perhaps if you are cross-developing on a Linux computer and targetting a MINIX platform you may use this file system. Or perhaps you need compatibility with a MINIX computer for other reasons. Use cases for this file system on a Linux computer are not leaping out at me, but it’s available.
  • VFAT: Virtual File Allocation Table , was introduced with Windows 95, and removed the eight-character limit for filenames. File names of up to 255 characters became possible. The only reason you’d use this file system is for compatibility with non-Linux operating systems.
  • CRAMFS: The Compressed ROM File System is a read-only file system designed for embedded systems and specialist read-only uses, such as in the boot processes of Linux computers. It is common to have a small, transient, file system loaded first so that bootstrap processes can be launched to prepare for the “real” boot system to be mounted.
  • MSDOS: The file system of the Microsoft Disk Operating System . Released in 1981, it’s an elementary file system that is as basic as it gets. The first version didn’t even have directories. It holds a place of prominence in computing history but, beyond compatibility with legacy systems, there is little reason to use it today.

A Safe Way to Experiment With File Systems

Creating a file system on a partition is destructive to any data that might already reside on that partition. Using a spare hard drive—or even a spare computer—is the perfect way to experiment with creating and using different file systems. But of course, many people don’t have spare hardware lying around, waiting to be experimented on.

However, we can create an image file and create file systems within that. Once we mount it, we can use it as though it were a regular partition. We can explore and experiment with file systems without a need for spare hardware. We’ll use the dd command to create our image file .

The image file is created by taking source data and putting it into an image. We need to tell dd where to get its source data. We’ll use the if (input file) option to tell dd to use /dev/zero as the input data source. This will be a stream of zeroes.

The of (output file) option allows us to provide a name for the image file. We’ll call it “howtogeek.img”.

The size of the image file is determined by the size and number of blocks we add to it. We’re using the bs (block size) option to request a block size of 1 MB and the count option to request 250 blocks. This will give us a file system of 250 MB. When you issue this command, adjust the number of blocks to suit your needs and the spare capacity you have on your Linux computer.

dd if=/dev/zero of=~/howtogeek.img bs=1M count=250

dd if=/dev/zero of=~/howtogeek.img bs=1M count=250

The file is created for us and dd reports that there were 250 blocks created for us, as requested.

Output from the dd command in a terminal window


We can look at our image file with ls :

ls -hl

ls -hl in a terminal window

It’s 250 MB as expected, which is encouraging.

Creating the File System

Let’s pick a file system to use. We’ll go back in time and use Ext2, which is the earliest version of Ext that this implementation of mkfs can create. This is a non-journaling file system, so don’t store anything precious in it without having backups elsewhere. We use the mkfs.ext2 variant of the mkfs command, and we tell it to use our image file as the target.

mkfs.ext2 ~/howtogeek.img

mkfs.ext2 ~/howtogeek.img in a terminal window

The file system is created, and some details of the file system are displayed.

Output of the mkfs.ext2 command in a terminal window

As you can see from the highlighted text, mke2fs makes an appearance.

Now we have a container for the file system—the image file—which is standing in for a hard drive in this scenario. Inside that container, we’ve created a file system. Now we need to mount the file system so that we can use it.

This is a temporary set-up, so we’ll make a mount point within /mnt called “geek.” We’ll remove it when we’re finished.

sudo mkdir /mnt/geek

mkfs.ext2 ~/howtogeek.img in a terminal window

Now we can mount our image file.

sudo mount ~/howtogeek.img /mnt/geek

mkfs.ext2 ~/howtogeek.img in a terminal window

We need to change the file ownership of the mount point so that we have read and write access to it.

sudo chown dave:users /mnt/geek/

sudo chown dave:users /mnt/geek/  in a terminal window

And now we should be able to use our new file system. Let’s change into the file system, and copy some files to it.

cd /mnt/geek

cp ~/Documents/Code/*.? .

cp ~/Documents/Code/*.? . in a terminal window

This will copy all files with a single-letter extension from the “~/Documents/Code” directory to our new file system. Let’s check that they were copied.

ls

ls in a terminal window

The files have been copied, so our file system has been created, mounted and used. Or so we think. Let’s double-check. From our home directory, we’ll unmount the file system . Note there is only one “n” in umount .

sudo umount /mnt/geek

sudo umount /mnt/geek  in a terminal window

Now, if we pop back to /mnt/geek and check for files, we shouldn’t find any because they are inside our image file, and that has been unmounted.

cd /mnt/geek

ls

cd /mnt/geek in a terminal window

TubeDigger - online video downloader from mostly any site

Further Exploration

Now we’ve got the process worked out, trying another file system should be easy. We’ll use the MINIX file system this time. In our home directory, we can create a new file system inside our existing image file.

Be careful! If there are any important files on the file system inside the image file, mount the image file, and retrieve them before you create a new file system.

mkfs.minix ~/howtogeek.image

mkfs.minix ~/howtogeek.image in a terminal window

Without any hint of asking you “if you’re sure,” the new file system is created over the old one. We can mount our image file with exactly the same command as before:

sudo mount ~/howtogeek.img /mnt/geek

sudo mount ~/howtogeek.img /mnt/geek in a terminal window

Let’s change into the new file system at /mnt/geek and see if we can create a file.

touch geek.txt

touch geek.txt in a terminal window

ls -ahl geek.txt

ls -ahl geek.txt in a terminal window

And, as simply and as quickly as that, we’ve created a new file system, mounted it, and we can use it.

Removing the Mount Point

When you’re all done, we can remove the “geek” mount point. To do that we’ll use rmdir :

cd /mnt

sudo rmdir geek

sudo rmdir geek in a terminal window

Juggling With Fire

With Linux, as with most things, you learn by doing. The problem with some commands is that they are potentially destructive. The dilemma is how to practice using them without putting your system or data at risk?

You now have a simple method of creating and trying out file systems with mkfs that leaves your computer untouched.

| | Linux Commands | | |
| —————– | ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— | |
| Files | tar · pv · cat · tac · chmod · grep · diff · sed · ar · man · pushd · popd · fsck · testdisk · seq · fd · pandoc · cd · $PATH · awk · join · jq · fold · uniq · journalctl · tail · stat · ls · fstab · echo · less · chgrp · chown · rev · look · strings · type · rename · zip · unzip · mount · umount · install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · vi · nano · mkdir · du · ln · patch · convert · rclone · shred · srm · scp · gzip · chattr · cut · find · umask · wc · tr | |
| Processes | alias · screen · top · nice · renice · progress · strace · systemd · tmux · chsh · history · at · batch · free · which · dmesg · chfn · usermod · ps · chroot · xargs · tty · pinky · lsof · vmstat · timeout · wall · yes · kill · sleep · sudo · su · time · groupadd · usermod · groups · lshw · shutdown · reboot · halt · poweroff · passwd · lscpu · crontab · date · bg · fg · pidof · nohup · pmap | |
| Networking | netstat · ping · traceroute · ip · ss · whois · fail2ban · bmon · dig · finger · nmap · ftp · curl · wget · who · whoami · w · iptables · ssh-keygen · ufw · arping · firewalld | |

  • Title: Mastering the Mkfs Utility: A Beginner's Guide to File System Creation in Linux
  • Author: Christopher
  • Created at : 2024-08-30 21:19:13
  • Updated at : 2024-08-31 21:19:13
  • Link: https://some-approaches.techidaily.com/mastering-the-mkfs-utility-a-beginners-guide-to-file-system-creation-in-linux/
  • License: This work is licensed under CC BY-NC-SA 4.0.