In this process, you will be introduced to the standard CentOS filesystems XFS, Ext4, and Btrfs. Filesystems form one of the most fundamental parts of any operating system and nearly everything depends on them. Here, you will learn how to create different types of standard filesystems available in CentOS 7, and how to link them to your system so that we can access them afterward for reading and writing. These two techniques are called formatting and mounting filesystems; while you do not do this very often, it remains one of the most fundamental Linux system administrator tasks.
To Start With: What Do You Need?
To complete this process, you will require a minimal installation of the CentOS 7 operating system with root access. We will also use virtual block devices instead of real disk devices because it’s better to demonstrate the usage of creating filesystems and formatting disks using “dummy” devices, instead of erasing your real hard disk contents. Therefore, you should have applied the Creating a virtual block device process and created a 1 Gigabyte virtual block device, which will be named /dev/loop0 in this example.
If you want to apply this process for real disk devices, all you have to do is replace /dev/loop0 with your correct partition—for logical volumes (lv) for example, /dev/mapper/myServer/data, for a SATA device /dev/sdX, or for an IDE-based hard disk name /dev/hdX (where X is a character a-z).
The Process
In our example, this block device is labeled at /dev/loop0. Please note that, if you have created more than one block device, your number could be different, so please change the name accordingly:
- First, let’s log in as root and show information about all currently available block devices:
lsblk -io NAME,TYPE,SIZE,MOUNTPOINT,FSTYPE,MODEL
- Now, recheck that we have a valid partition table installed on the device:
parted /dev/loop0 print
- The preceding line should print out the following content: Partition Table: gpt. If this is not the case, let’s create a new partition table (confirm the deletion of any data):
parted /dev/loop0 mklabel gpt
- Now, we will create a new partition spanning the complete disk space with an ext4 filesystem label (no filesystem will be installed yet; it’s just a label):
parted -a optimal /dev/loop0 mkpart primary ext4 2048KiB 100%
- Print the partition table again to show the new partition we just created:
parted /dev/loop0 print
- Now, let’s remove the partition:
parted /dev/loop0 rm 1
- We can also create a btrfs-labeled partition:
parted -a optimal /dev/loop0 unit MB mkpart primary btrfs 2048KiB 100%
- Afterward, let’s create an XFS-labeled partition spanning the whole disk:
parted /dev/loop0 rm 1
parted -a optimal /dev/loop0 mkpart primary xfs 2048KiB 100% - Now, show the block table again to see what we have changed:
lsblk -io NAME,TYPE,SIZE,MOUNTPOINT,FSTYPE,MODEL
- As we have only defined the partition type label, we still don’t have a valid filesystem on our partition; so, in the next step, we format our disk using the correct type. We use XFS in our example. Please change mkfs -t <type> if you use ext4 or btrfs instead:
mkfs -t xfs /dev/loop0p1
- Next, let’s mount our virtual block device partition on the system, into the directory /media/vbd-1, and please change -t <type> if you use ext4 or btrfs instead:
mkdir /media/vbd-1
mount -t xfs /dev/loop0p1 /media/vbd-1 - Finally, test if we can read and write to the new filesystem:
echo "this is a test" > /media/vbd-1/testfile.txt
cat /media/vbd-1/testfile.txt
How Does It Work?
Here, in this process, we showed the user how to create CentOS 7 standard partitions spanning the whole disk, and then we created some filesystems on them, which is called formatting, using different filesystem types. The standard filesystem available in CentOS 7 is XFS, but as we have learned in this process, there are lots of other ones available as well, including the popular ext4 and btrfs. XFS is a very robust and high-performing file system for large storage configurations; it is considered very mature and stable. Before CentOS 7, the standard file system was ext4, but it had some limitations and not the best performance when working with millions of files and is considered barely suitable for today’s very large filesystems. btrfs is a relatively new filesystem and is included in CentOS 7, but at the time of writing it is still under development and should not be used for production systems. It is considered to be fully supported in later CentOS 7 minor releases and is likely to replace XFS as the standard CentOS filesystem type in the future, as it has a list of very promising features and enhancements, such as copy-on-write, which copies files each time you write to them, and which makes it possible to go back to former file versions.
So, what have we learned from this experience?
We started this process by using the lsblk command to print a list of all available block devices currently attached to the system. We used this command to check if our target block device that we want to use for installing partitions and filesystems on is available. In our example we will use the /dev/loop0 device, please change this name if it’s different on your system (as said before, you could also use a “real” disk block device, such as /dev/sda, but always be careful!). After confirming that we have our device ready, we used the parted command to check the partition table of the disk. A partition table is mandatory for any hard disk to keep track of the partition information on it. As you have seen, our primary tool for creating partition tables and partitions is parted, as it is the officially recommended CentOS 7 tool for these tasks, but there are other programs that do the same as well, such as fdisk or gdisk. If there is no partition table available, we must create one of type gpt using parted’s mklabel gpt parameter.
Next, after we created the partition table, we put some partitions on it. Therefore, we issued parted’s mkpart command with the -a optimal primary ext4 2048KiB 100% options.
Note
Be careful with the parted command all the time and recheck everything before executing, as most of its commands will completely destroy all the data currently stored on the disk.
This will create a new partition starting at 2,048 kilobytes (kb) until the end of the disk. We did not start at the very beginning of the disk (0%) as 2,048 kb is the start of the first sector on the disk to leave some space left to store some additional data. -a optimal aligns the partition to a multiple of the physical block size that will guarantee optimal performance. Next, we removed the partition again using the rm option and number 1, which refers to the first partition we just created. We recreated new partitions of type btrfs and finally xfs. After the disk is partitioned, we need an actual filesystem on it, as parted only labels the partition to a specific type, but does not do the actual formatting. To make the filesystem, we use the mkfs utility. You can either run it with the -t flag, as we did, or use a dot notation, such as mkfs.xfs, to specify the type you want to format it to. The mkfs command gives us a detailed output of what it has done, such as how many blocks have been written and so on.
Finally, after we have created the filesystem on our disk partition, we can use the mount command to make it available and work with it in our current system. mount either attaches or detaches a device’s filesystem to our system’s root filesystem. Therefore, we need to first create a directory to define where we want to attach it to. We use the directory, /media/vbd-1, as a parameter for the actual mount command with the syntax, mount -t <file system type> <device> <dir>. For almost all standard filesystems, you can skip the -t parameter as it will automatically detect the right type. To detach a filesystem from your system, you can use the umount command with the argument of the device you want to remove (you can also use the folder it’s mounted to; both do work!). In our example, to unmount our loop device’s first partition, type umount /dev/loop0p1.
After mounting our formatted partition device, we can access it like any other component beneath the root folder.
There's more…
In this process, we always use one partition spanning the complete available disk space. Often, you have more than one partition on a disk, so let’s create this kind of layout instead. In this example, we create three 100 MB partitions on /dev/loop0:
- First, let’s delete our partition once again using the rm parameter so that we can add new ones:
parted /dev/loop0 rm 1
- Now, let’s create three equal partitions:
parted -a optimal /dev/loop0 unit MiB mkpart primary ext4 2048KiB 100
parted -a optimal /dev/loop0 unit MiB mkpart primary ext4 100 200
parted -a optimal /dev/loop0 unit MiB mkpart primary ext4 300 400 - Let’s review our layout:
parted /dev/loop0 print
Note
Using the gpt partition table, we can create up to 128 primary partitions on any disk; when using the older msdos partition type, there is a maximum of four primary partitions. If you need more, you have to create extended partitions out of primary ones.