Every new unit needs to have a file system to be used, hence the need to partition and format disks. For both Linux and Unix, tools such as fdisk (from windows) are required to create partitions and mkfs.
This process is necessary because the way to access the files on a disk requires having a structure that allows us to access them. In addition, the entire space of a disk cannot be used by the user in its entirety, since it requires a reserved space where such a structure must be stored to provide access. From there it is required to create partitions, so that the user can access the data on the disk, and these are independent of the reserved space, which cannot be used by the user to store their files.
The purpose of this tutorial is to show users how to create partitions on a disk, as well as their formatting to later use the disks, and we will show other utilities for the file system at the end.
Creating a File System in Linux with Fdisk
One of the easiest ways to create a file system is through fdisk. With this tool we can partition our hard drive or drive easily. First we can find out the name of our disk using the –l option:
fdisk -l
If your disk already has partitions, they will be shown with a number after their name, for example if / dev / sdb is the original disk, if there were partitions these could be / dev / sdb1, / dev / sdb2, etc. If there are no partitions, it is already possible to create them directly (skip the next paragraph)
If the hard drive is already mounted, it must be disassembled with the umount drive command. Once the unit is disassembled, we proceed to partition the disk we want.
To create a partition on the dismounted disk, the command to create a new partition is:
fdisk /dev/sdb
In summary, the simplest way to create a partition is as follows:
Once fdisk is opened, if we want to create the first partition, we write n to create a new partition, then p to indicate that it is primary, 1 to indicate that it is the first partition, and indicate the partition size and number of cylinders (or select the maximum available).
To select the type of file system, we write t, and if we want to see all the available types, option L (from here we select 83 for Linux). At the end we write w and the changes made to the disk partition are saved.
File system and formatting units with mkfs
Before we will explain the mke2fs application. In its simplest form, it is sufficient to create a file system for a unit specified as an argument and whose configuration options are contained in /etc/mke2fs.conf. Before displaying the command, it is necessary to know the location of the unit where we are going to create the file system (mount can be used).
Assuming that our device is located in /dev/sdb1, the simplest command to create a file system would be the following:
mke2fs /dev/sdb1
Here the File System is generated automatically, without giving the user the opportunity to select the desired file system. Therefore, if you want to add a specific file system to the drive, such as ext2, ext3, ext4 or ntfs, use the mkfs command. For example, if you want to add as an ext4 e system, you must execute the following command:
mkfs.ext4 /dev/sdb1
Where when executing this command, the drive is also formatted and ready to be mounted through the command:
mount -t /dev/sdb1
Other useful commands
There are also other commands that allow you to create file systems, compatible with various versions of Linux:
Tune2fs: Shows the creation information of the file system, as well as the count of reserved blocks. (option -l). We can also indicate the percentage of reserved space (option –m percentage_reserved)
#First case tune2fs -l /dev/sdb1 #Second case tune2fs -m 5 /dev/sdb1
Fsck: This tool is used to verify the integrity of a disk. Similar to the Chkdsk command, it analyzes the disk file system for errors, with the option to repair it.
fsck /dev/sdb1
You can specify the type of file system, if it cannot be detected automatically, the –t option is used for this case we execute the following command (before dismounting the disk with umount /dev/sdb1 if it has a mount point)
fsck -t ext3 /dev/sdb1