An operating system uses a file system on a storage device to manage its files. A file system allows us to create and manage files in a hierarchical manner.
It’s very common to see a single Linux server with multiple storage devices. Often these storage devices are configured to use different types of file systems. One of the common tasks performed by the administrator is to find the file system type used by the particular storage device.
In this short tutorial, we’ll discuss various ways to find the file system type of the particular storage device.
2. Setting up an Example
In Linux, we can create a file system only on a block device. A block device can be any physical storage device, for example – Hard Disk Drive, Solid State Drive, USB Drive, etc. In a virtualized environment, it can be a virtual disk as well.
In this tutorial, primarily, we’ll use a loop device to represent a block device. This will allow us to create multiple disk drives with different file systems. However, the commands discussed in the tutorials will work with real block devices as well.
A loop device is a pseudo-device that doesn’t correspond to any physical block device. It allows us to use a regular file as a block device. Let’s understand this with an example.
2.1. Creating the Files
First, we’ll create five files of 1 GB size each. We can use the dd command for this:
$ mkdir /tmp/fs-demo $ cd /tmp/fs-demo $ dd if=/dev/zero of=file-1.img bs=1M count=1024 $ dd if=/dev/zero of=file-2.img bs=1M count=1024 $ dd if=/dev/zero of=file-3.img bs=1M count=1024 $ dd if=/dev/zero of=file-4.img bs=1M count=1024 $ dd if=/dev/zero of=file-5.img bs=1M count=1024
The bs option represents the block size, whereas the count option represents the number of blocks to be copied. In our case, these values are 1 M and 1024, respectively.
In a nutshell, this command copies 1024 blocks of 1 M size each to create a file of a 1 GB size.
Let’s verify that the files have been created:
$ ls -1sh total 5.1G 1.1G file-1.img 1.1G file-2.img 1.1G file-3.img 1.1G file-4.img 1.1G file-5.img
2.2. Creating the Loop Devices
Now, the next step is to create loop devices. We can use the losetup command to create and manage the loop devices.
Let’s associate the loop devices with the files that we created in the previous section:
$ sudo losetup -fP file-1.img $ sudo losetup -fP file-2.img $ sudo losetup -fP file-3.img $ sudo losetup -fP file-4.img $ sudo losetup -fP file-5.img
In this example, the -f option finds the first unused device, and the -P option forces the kernel to scan the partition table on a newly created loop device.
Let’s check that loop devices have been associated with the files correctly. We can achieve this using the -l option:
$ losetup -l NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE DIO LOG-SEC /dev/loop1 0 0 0 0 /tmp/fs-demo/file-2.img 0 512 /dev/loop4 0 0 0 0 /tmp/fs-demo/file-5.img 0 512 /dev/loop2 0 0 0 0 /tmp/fs-demo/file-3.img 0 512 /dev/loop0 0 0 0 0 /tmp/fs-demo/file-1.img 0 512 /dev/loop3 0 0 0 0 /tmp/fs-demo/file-4.img 0 512
Here, the NAME column represents the loop device, and the BACK-FILE column represents the file associated with it.
We should note that, the available loop devices vary from system to system. Hence it’s important to find the correct loop devices before using them.
In our case, we can use the loop device from /dev/loop0 to /dev/loop4.
2.3. Creating the File System
Now, let’s create a few different types of file systems on the loop devices using the mkfs command:
$ sudo mkfs -t ext2 /dev/loop0 $ sudo mkfs -t ext3 /dev/loop1 $ sudo mkfs -t ext4 /dev/loop2 $ sudo mkfs -t xfs /dev/loop3 $ sudo mkfs -t vfat /dev/loop4
In this example, we have used the -t option with mkfs to specify the file system type.
2.4. Mounting the Loop-back File System
Finally, we can mount the first three loop devices onto directories using the mount command:
$ sudo mkdir -p /mnt/fs-1 /mnt/fs-2 /mnt/fs-3 $ sudo mount -o loop file-1.img /mnt/fs-1 $ sudo mount -o loop file-2.img /mnt/fs-2 $ sudo mount -o loop file-3.img /mnt/fs-3
We should note that, we have used the -o loop option with the command to mount the loop devices.
In the next section, we’ll see how to verify that the loop devices have been mounted correctly.
3. Finding the File System Type of the Mounted Devices
3.1. Using the mount Command
In the previous section, we used the mount command to mount a file system. We can use the same command to find the file system’s type.
Let’s see this in action:
$ mount | grep -iE "loop0|loop1|loop2" /dev/loop0 on /mnt/fs-1 type ext2 (rw,relatime) /dev/loop1 on /mnt/fs-2 type ext3 (rw,relatime) /dev/loop2 on /mnt/fs-3 type ext4 (rw,relatime)
Here, the second to last column shows the file system type.
Additionally, this command also shows the other file system details like – the loop device, its mount point, its permissions, etc.
3.2. Cleaning Up
In this tutorial, we created loop devices and files for demo purposes. Hence, we should remove them to reclaim the storage space.
Let’s see the steps to remove the first loop device and its associated file.
First, unmount and delete the directory:
$ sudo umount /mnt/fs-1 $ sudo rm -rf /mnt/fs-1/
Next, we detach the loop device with the help of the -d option:
$ sudo losetup -d /dev/loop0
Finally, we’ll remove the file associate with the loop device:
$ rm -f /tmp/fs-demo/file-1.img
3.3. Using the df Command
The df command shows the details about the file system’s disk space. We can use the -T option with it to show file system type:
$ df -T | grep -iE "loop1|loop2" /dev/loop1 ext3 998060 60 945572 1% /mnt/fs-2 /dev/loop2 ext4 996780 24 927944 1% /mnt/fs-3
In this example, the second column represents the file system type.
Apart from this, we can also use the -t option with the command to filters the output based on the file system type.
Let’s list the device that is using the ext3 file system:
$ df -t ext3 Filesystem 1K-blocks Used Available Use% Mounted on /dev/loop1 998060 60 945572 1% /mnt/fs-2
3.4. Cleaning Up the Other Devices
Now, let’s remove the /dev/loop1 and /dev/loop2 devices and their associated files:
$ sudo umount /mnt/fs-2 /mnt/fs-3 $ sudo rm -rf /mnt/fs-2/ /mnt/fs-3/ $ sudo losetup -d /dev/loop1 /dev/loop2 $ rm -f /tmp/fs-demo/file-2.img /tmp/fs-demo/file-3.img
4. Finding the File System Type of the Unmounted Devices
4.1. Using the file Command
The file command is used to determine a file type. However, to use it with the block devices, we have to use the -s option:
$ sudo file -s /dev/loop3 /dev/loop3: SGI XFS filesystem data (blksz 4096, inosz 512, v2 dirs)
The -s option instructs the command to treat the file as a special type of file.
Here, we can see that the /dev/loop3 device is configured to use the XFS file system.
4.2. Cleaning Up
Let’s clean up the /dev/loop3 device and its associated file:
$ sudo losetup -d /dev/loop3 $ rm -f /tmp/fs-demo/file-4.img
We should note that, the /dev/loop3 device wasn’t mounted while setting up our example. Hence steps related to unmounting and deleting the directory are not required.
4.3. Using the blkid Command
The blkid command is useful when we want to print the attributes of the block devices. The file system type is one such attribute.
Let’s use the blkid command to find the file system type:
$ blkid /dev/loop4 /dev/loop4: UUID="0700-9673" BLOCK_SIZE="512" TYPE="vfat"
In this example, the last column represents the file system type which is vfat.
Similar to the df command, we can use the -t option of this command to filter the output.
Let’s list the device that is formatted using the vfat file system:
$ blkid -t TYPE=vfat /dev/loop4: UUID="0700-9673" BLOCK_SIZE="512" TYPE="vfat"
4.4. Using the lsblk Command
The lsblk command list all block devices. We can use the -f option of this command to display details about the file system:
$ lsblk -f /dev/loop4 NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS loop4 vfat FAT32 0700-9673
Here, we can determine the file system type by referring to the second column of the output.
4.5. Cleaning Up the Last Device
Now, let’s remove the /dev/loop4 device and the file associated with it:
$ sudo losetup -d /dev/loop4 $ rm -f /tmp/fs-demo/file-5.img
5. Finding the File System Type of the Real Devices
In the previous sections, we worked with pseudo-devices only. However, we can use all of these commands with real block devices as well.
Let’s use the df command to find the file system type:
$ df -T | grep -iE "sda|sdb" /dev/sda3 ext4 238735004 111667452 114867616 50% / /dev/sda2 vfat 524252 5364 518888 2% /boot/efi /dev/sdb vfat 15000952 4025864 10975088 27% /media/jarvis/NARENDRA
In this example, the /dev/sda is a Hard Disk Drive, and /dev/sdb is a USB drive.
Similarly, we can use the lsblk command to achieve the same result:
$ lsblk -f /dev/sda2 /dev/sda3 /dev/sdb NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS sda2 vfat FAT32 76C6-9FD9 506.7M 1% /boot/efi sda3 ext4 1.0 82cfd4da-604d-4127-a05c-a6eb240f87b8 109.5G 47% / sdb vfat FAT32 NARENDRA 5E75-A2F2 10.5G 27% /media/jarvis/NARENDRA
Here, /dev/sda2 and /dev/sda3 represent the partitions of the Hard Disk Drive.
In this article, we saw how to identify the file system type of the block devices.
First, we used the mount and df commands to find the file system type of the mounted block devices.
Then we used the file, blkid, and lsblk commands to find the file system type of the unmounted block devices.
Finally, we used the df and lsblk commands with the real block devices.