LFCS - Creating File Systems

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
372
Reaction score
433
Credits
13,798
After you have partitions made, you will want to use those partitions to store files on it. Until a partition has a file system, it cannot store files.

The File System (FS) provides an organized structure for storing files and information about the files. There are many types of File Systems that a Linux system can use, but we will go over three types:
  1. EXT4
  2. XFS
  3. NTFS
Current System

I am using VBox as we covered in the previous article, 'LFCS - Partitioning Disks'. You should have a second disk that is set up from the script in the previous article, as shown in Figure 1.

Figure 1.JPG

FIGURE 1

Here, there are six basic partitions that I can format with a File System: 'sdb5', 'sdb6', 'sdb7', 'sdb8', 'sdb9', 'sdb15'. We will format 'sdb5', 'sdb6' and 'sdb7'.

EXT4 File System

The EXT file systems are the default file system used for many Linux systems. There is EXT2, EXT3 and EXT4.

Each version of the File System has some unique abilities than the previous versions. This is the same as the various files systems as well, but that is beyond the scope of this article.

So, we need to get 'sdb5' formatted to EXT4.

Start by opening a terminal and performing a 'sudo su' to get to root privileges. Type in 'mkfs.' and hit TAB twice. You should see something like in Figure 2.

Figure 2.JPG

FIGURE 2

You can use the standard 'mkfs' command to format '/dev/sdb5' as EXT4:

Code:
mkfs -t ext4 /dev/sdb5

Or, you can use 'mkfs.ext4' with the command:

Code:
mkfs.ext4 /dev/sdb5

The output shown in Figure 3 lets you can see the UUID of the partition, superblock positions and the journal creation.

Figure 3.JPG

FIGURE 3

Now, if you perform the command 'fdisk -l /dev/sdb' you should see no difference in the listing even though the one partition has a file format. To see the file system of the partition, use the command:

Code:
lsblk --fs /dev/sdb5

You can also not specify the individual partition and just get the information on the entire drive and all partitions (sdb).

There is also the command 'dump2fs' to get information on a specific partition:

Code:
dumpe2fs /dev/sdb5

Let's give the EXT4 partition a name, or label:

Code:
tune2fs -L p-ext4 /dev/sdb5

To see the change, check the 'lsblk --fs' command.

For parameter information, use the command 'mkfs.ext4' to see a list.

Now, we can look at XFS.

XFS File System

From before, typing in 'mkfs.' and pressing TAB twice, you'll see the command 'mkfs.xfs'.

You can use the standard 'mkfs' command to format '/dev/sdb6' as XFS:

Code:
mkfs -t xfs /dev/sdb6

With no extra parameters, the output will be like Figure 4.

Figure 4.JPG

FIGURE 4

Or, you can use 'mkfs.xfs' with the command:

Code:
mkfs.xfs /dev/sdb6

NOTE: Using this command may require you to install 'xfsprogs'.

Possible parameters to use on these commands are:
  • -b - sets block size (-b size=2k)
  • -f - force an overwrite and deletes all data on the partition
  • -L - sets a label on the partition (-L Data)
You can use the program 'xfs_db' to access a mode to change items about the partition or list them. To enter the mode, use the parameter '-x' and also specify the partition:

Code:
xfs_db -x /dev/sdb6

At the prompt, you can enter 'help' to get a list of commands. When you want to exit the command mode, enter 'quit'.

You can also use the parameter '-c' with a command to perform the corresponding parameter and not enter the command mode. For example, to use the parameter 'uuid' to see the partition's UUID, the command is:

Code:
xfs_db -c uuid /dev/sdb6

For parameter information, use the command 'mkfs.xfs' to see a list.

NTFS

The NTFS file system is the default file system for Microsoft Windows. If you are going to dual boot a system and need a partition to share information between the two Operating Systems, use NTFS.

NOTE: For CentOS 7, you'll need to install 'epel-release' and then install 'ntfs-3g ntfsprogs'. If you type 'mkfs.' and press TAB twice, but see no 'mkfs-ntfs' command, run 'sudo ln -s /sbin/mkntfs /sbin/mkfs.ntfs' to create the shortcut link to the command.

Use one of the following commands to format a partition as NTFS. In the example, we'll use '/dev/sdb7'.

Code:
mkfs -t ntfs /dev/sdb7

Or, you can use 'mkfs.ntfs' with the command:

Code:
mkfs.ntfs /dev/sdb7

The output will cause the partition to be formatted and then the sectors zeroed out.

Some of the main parameters that can be useful are:
  • -Q - quick format
  • -L - label the partition (-L <name>)
  • -C - compress the partition
  • -I - no indexing of files
For parameter information, use the command 'mkfs.ntfs' to see a list.

The methods are all similar, but just provide a different file system. Now that the file system is in place, we need to mount the partition so we can use the partition.

Mount a Partition

We cannot access the file system until we mount the partition for use.

There are two ways to mount a partition: temporary or permanent.

Let's look at the temporary way first. In this method, the partition is available until the partition is unmounted or we restart the system. We need a mount point to hold the partition, so perform the following to create a folder, 'drives', with three sub-folders called, 'ext4', xfs' and 'ntfs'.

Code:
mkdir -p drives/{ext4, xfs, ntfs}

So, now we can mount each drive using the 'mount' command:

Code:
mount /dev/sdb5 drives/ext4
mount /dev/sdb6 drives/xfs
mount /dev/sdb7 drives/ntfs

You can use the following command to list all the mounted drives under the specified folder and see what default parameters it is using one of the two commands:

Code:
mount | grep drives
mount | grep sdb

Since the partitions are all under the folder named 'drives', it can be done with the first command. If they are all in their own folder, then you can do this for each individual drive or as the drive 'sdb'.

To make the mounting of a partition permanent, you need to add the drive to the '/etc/fstab' file. You need the UUID of the partitions being added. For example, the UUID for each of my partitions is:
  • /dev/sdb5: UUID="7883d0d1-b58b-45ec-9ebe-17c38133b4d3" TYPE="ext4"
  • /dev/sdb6: UUID="95e76b81-4c6e-4605-ab84-5b02dad4f3ac" TYPE="xfs"
  • /dev/sdb7: UUID="7A4A698B0D62F596" TYPE="ntfs" PTTYPE="dos"
The list also shows the path and file system type. To get the output, I use the command:

Code:
blkid /dev/sdb*

So, we need to edit the '/etc/fstab' file and add the following to it:

Code:
UUID=7883d0d1-b58b-45ec-9ebe-17c38133b4d3 /home/jarret/drives/ext4 ext4 defaults 0 0
UUID=95e76b81-4c6e-4605-ab84-5b02dad4f3ac /home/jarret/drives/xfs xfs defaults 0 0
UUID=7A4A698B0D62F596 /home/jarret/drives/ntfs ntfs defaults 0 0

The format is the UUID, path to the mount point, file system type, parameters, dump file and file system check.

The parameters most times can be 'defaults' to just get the general use of a partition. Some parameters are:
  • rw - set permissions to read-write
  • exec - allow executing binaries and scripts on the partition
  • noexec - disallows executing binaries and scripts on the partition
  • nouser - make the filesystem not mountable by a standard user
  • atime - keeps access time on files
  • noatime - does not keep access time on files
NOTE: Some parameters only work on specific file systems, while others work on more than one or all of them.

We used 'defaults', but the actual default parameters vary depending on the file system. To see the default values, you can run the mount command and pipe it through grep for the 'sdb*' partitions. Figure 5 shows an example.

Figure 5.JPG

FIGURE 5

After changing the '/etc/fstab' file, you can reboot to make the changes take effect, but if there are errors in the entries, then this can take up time. The fastest way is to just mount the information in the 'fstab' file by using the command:

Code:
mount -a

Conclusion

Setting up file systems from the command line can give you little more control than using the GParted Graphical User Interface (GUI). Specific mount options can cause the partition to behave as you need. For example, you may want the partition to just be storage, but allow no files to be executed (noexec).

You can also set specific file system parameters, such as block size, to configure the layout better for your files. If you have a lot of small files, then you want a smaller block size so you do not waste space. If you set a block size too large, a small file will take up the full block size. For example, if you set your block size to 4096 bytes, then any files smaller than this will use up 4096 bytes of space.
 

Members online


Top