Linux format file system

Using mkfs command in Linux to Format the Filesystem on a Disk or Partition

mkfs is the command line tool in Linux to format a disk or partition in a certain filesystem of your choice.

The letters in mkfs stand for «make file system». The command is commonly used for managing storage devices in Linux. You can think of mkfs as a command line tool for formatting a disk into a specific filesystem.

mkfs -t [fs type] [target device]

You can also use the mkfs command in the following fashion.

mkfs.[fs type] [target device]

In this tutorial, I will discuss generally what a file system is. I’ll also provide examples for using the mkfs command effectively.

Since the mkfs command deals with disks and partitions, it requires you to be either root or sudo user with access to run admin commands.

mkfs is a powerful tool. It is important to understand the potential consequences of altering the filesystem. Selecting the wrong device node will erase all data on that device. Use this command at your own risk. You have been warned

What is a file system, again?

A file system (fs) refers to the structure and logic that manage data on a device. The file system controls how data is stored and retrieved.

There are many types of file systems and each has its own advantages and disadvantages. Here are the most common type of filesystems:

You have probably come across one or more of these filesystem types before. You may even associate the types with their respective operating systems.

Читайте также:  Linux изменить основную группу пользователя

Generally speaking, FAT/NTFS are designed for Windows, Ext is used with Linux systems, and APFS/HFS are macOS file systems. Each of these address the logic of file structure differently which can result in issues.

This is why it is crucial to think about this before declaring a filesystem type, or «formatting» your device. Each use case is different, and it is up to you to decide what filesystem works best for your needs.

mkfs command examples

Let me show you some of the most common usage of the mkfs command.

Please don’t try any of these commands on your actual system blindly. It will format the target device/disk partition erasing all the data.

mkfs without any option creates ext2 filesystem

If you don’t specify the filesystem type for the target device, it will use the default ext2 filesystem.

Specify filetype with mkfs

Using ext2 is not very convenient or advisable. Most Linux systems these days recommend using ext4. You can specify the file system in the following manner:

You can also use it like this:

Check for bad blocks on a device

You can also check for bad blocks on a device but keep in mind that the check often takes a long time.

Bonus tip: Check the filesystem type of a device

You may want to check the type of filesystem of a device that you just formatted using mkfs command. You can use the file command for this purpose.

Here’s an example that shows what kind of output it could show:

[email protected]:~$ sudo file -s /dev/nvme0n1p2 /dev/nvme0n1p2: Linux rev 1.0 ext4 filesystem data, UUID=34daa4b7-a2c4-4983-b5cc-df9baa6a6543 (extents) (64bit) (large files) (huge files)

Real life use case: Using mkfs command to create a File System on a USB device

Now that you have some background information, you can start using mkfs.

The most practical demonstration I can think of is formatting a USB flash storage drive. These same principles will apply to any type of storage you choose.

Читайте также:  Linux действие при закрытии крышки ноутбука

Find Your Device

First you will need to find your device. One method you can use is sudo fdisk -l . This will list all disk nodes that are currently mounted.

[email protected]:~$ sudo fdisk -l Disk /dev/sda: 25 GiB, 26843545600 bytes, 52428800 sectors Disk model: VBOX HARDDISK Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0x3c62c49c Device Boot Start End Sectors Size Id Type /dev/sda1 * 4096 1023998 1019903 498M 83 Linux /dev/sda2 1024000 44036094 43012095 20.5G 83 Linux /dev/sda3 44036096 52424702 8388607 4G 82 Linux swap / Solaris Disk /dev/sdb: 28.93 GiB, 31040995328 bytes, 60626944 sectors Disk model: Patriot Memory Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: gpt Disk identifier: 91A34D6F-E67B-E54D-B19C-5CC828DAAB9A Device Start End Sectors Size Type /dev/sdb1 2048 60626910 60624863 28.9G Linux filesystem 

Your output will obviously vary. Please be very careful when identifying your desired drive. If you are unsure, remove the disk and run the fdisk -l command again. If you have the correct device, it won’t be listed while disconnected.

Verify the Partition

The device I’m using is a Patriot Memory USB and it is located at /dev/sdb . In addition to identifying the correct disk, you will need to make sure that you are changing the fs of the desired partition.

I used fdisk tools to delete existing data and write a new partition table. While I was doing that, I created a new partition to write to. That partition will be our target: /dev/sdb1 .

Unmount

Before you attempt to change the file system, you will need to unmount it using the umount command.

Create the File System

Now that you have verified your target and unmounted the drive, you can proceed to create the file system.

I have added the -v verbose option here to display more information when running.

[email protected]:~$ sudo mkfs.ext4 /dev/sdb1 -v mke2fs 1.45.5 (07-Jan-2020) fs_types for mke2fs.conf resolution: 'ext4' Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 1896832 inodes, 7578107 blocks 378905 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2155872256 232 block groups 32768 blocks per group, 32768 fragments per group 8176 inodes per group Filesystem UUID: 73882769-7599-4c79-a00b-ef317ccd921d Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done 

This process can take some time, but should finish in under 20 minutes unless the target is larger than 2 TB.

Читайте также:  Linux disk quota exceeded

I had several issues with the program hanging on the last item. Unfortunately, there is no progress indicator and I had no errors thrown.

Verify the newly created filesystem

First, check the filesystem of the device you just used. Is it the one you wanted?

It is important to make sure that the device is recognized on the systems you will use it with. I created a folder called test and a file within it called test.txt.

To save time, you can copy and paste my commands here.

mkdir test && cd test touch test.txt echo "THIS IS ONLY A TEST" > test.txt cat test.txt 

If everything worked, you should be able to mount the drive to your desired systems and access the files. If you cannot access the files on your system, there is probably a compatibility issue.

Conclusion

I hope you find this quick guide to mkfs command useful. If you like this tutorial, please share it on social media.

If you have any comments or questions, please leave them below. If you have any suggestions for topics you’d like to see covered, feel free to leave those as well.

Источник

Оцените статью
Adblock
detector