Determining the filesystem for storage media

6kor3dos, 123RF

6kor3dos, 123RF

Guessing Games

Linux has some compact tools for quickly finding out the filesystem of a given storage medium. We show you how to use these effectively.

Linux can handle a wide variety of filesystems. However, in order to achieve problem-free use of a data storage device, it is a good idea to at least know what type of filesystem is on the device. Attempts to restore data on storage medium that have an unknown filesystem can be very difficult. Similar difficulties occur in situations in which you want to read a downloaded image of a DVD or an unknown data storage device and therefore have to incorporate it in the system. This can also be the case for new media, like recently purchased memory cards.

We take a closer look at the relevant out-of-the-box tools available for determining the correct type of filesystem. After that, we look at the partition and then the integration of the directory, which is made available via the network and the storage medium, into the system.

Mounting a filesystem is accomplished by a user with administrative rights on the mount command. Usually, this means as a root user. As part of the mount procedure, you should enter a mount point that tells the system where it should incorporate the filesystem in the local filesystem tree. If you do not enter a mount point, then the command pulls the prepared partitions or storage media into the filesystems table (/etc/fstab ) in order to find a directory that should serve as the mounting point.

There is a list of the filesystems that have already been incorporated in the /proc/mounts file. This file mirrors the kernel's information. The mounted filesystems table, /etc/mtab , was previously responsible for this task. Today, this table usually is a symbolic link to /proc/mounts .

The -t option in the mount command specifies the interpretation that the operating system applies to the type of the filesystem it will incorporate. Below, you can see an example of a call that mounts a partition in the /opt/usb-drive/ directory. This is the first partition of the second hard drive, and it has been formatted with ext3.

# mount -t ext3 /dev/sdb1 /opt/usb-drive

The types of filesystem that your system supports depends on the type of kernel components statically integrated or what has been loaded as a kernel module. In order to determine the correct type of filesystem, you can take the following approaches:

  1. Try things out with mount until something works
  2. Have the operating system recognize what type of filesystem sits on the storage media
  3. Use a suitable tool for finding out by yourself what kind of filesystem it is.

The first approach is frustrating and can take a long time. The second approach is reliable and works well. The third approach is appropriate when you don't want to bother with the first approach, and the second approach hasn't worked for reasons you don't know but would like to uncover.

Auto-Detect

Automatic identification of the filesystem type is accomplished in mount by calling the program either without the -t or explicitly with -t auto . mount will try to identify the filesystem with the help of the libblkid [1] library. If the filesystem is successfully identified, the program will mount the media without acknowledging this directly by using so-called silent mode.

Otherwise, mount evaluates the entries in the /proc/filesystems file. Figure 1 shows the file as it appears on an Ubuntu system. The entries marked with the nodev keyword indicate that no corresponding filesystem is currently allocated to a block device [2].

Figure 1: A standard kernel's integrated filesystems under Ubuntu.

In most cases, mounting works like this, at least for common filesystems. These would include ext2/3/4, VFAT, and MS-DOS. Btrfs, XFS, and ZFS, on the other hand, are more difficult candidates for mounting. They need additional modules and potentially also more software packages. Once these needs are met, the corresponding versions of commands from fsck , mount , and mkfs also work with these systems.

The -v option causes mount to produce an additional status message once the process has been successfully completed. Listing 1 demonstrates this for a Fedora image, which mount recognizes as a CD/DVD (ISO 9660).

Listing 1

Mounting a Fedora disk image

# mount -v Fedora-Xfce-Live-x86_64-24-1.2.iso /tmp/usb-platte/
mount: /dev/loop0 is write-protected, mounting read-only
mount: /dev/loop0 mounted on /tmp/usb-drive.

If the automatic detection function fails to identify the filesystem that is to be mounted, then you will be forced to switch over to the third approach described above and find out for yourself what type of filesystem you are dealing with. Fortunately, there are some on-board tools that work smoothly with this approach.

fdisk

You can use fdisk and its convenient ncurse version, cfdisk, to arrange the partitions on the storage medium including setting up new partitions, deleting old ones, and setting a bootable flag to make a partition boot-ready. Use

fdisk /dev/sda -l

in the terminal to output the first hard drive's entire partition table. In a similar fashion,

cfdisk /dev/sda

puts the information you have searched for up on the screen for you to see.

fdisk and cfdisk usually only display the type of partition, meaning you only see filesystems that are potentially supported. This is common with Linux, Solaris, BSD, Mac OS X and Windows. We are a bit luckier with the mounted ISO image in Figure 2, and we have learned some useful details. The second entry contains a FAT filesystem.

Figure 2: Filesystems in a mounted ISO image, above in fdisk and below in cfdisk.

blkid

The blkid ("Block ID") tool reads the information to the filesystems, relying on the libblkid library previously mentioned. Listing 2 shows the call for the first partition of the first hard drive.

Listing 2

Using blkid

# blkid -ps TYPE /dev/sda1
/dev/sda1: TYPE="ext2"

The -p option makes sure that the tool ignores the information from the drive cache and instead accesses the data storage device directly. Since the output from blkid is typically very large, you should restrict it with the -s TYPE option for information about the filesystem. In Listing 3, you can see the information that blkid has found digging into the ISO image used in Figure 2. The TYPE option also contains the correct parameters that the mount program can use for attaching the filesystem to your directory tree.

Listing 3

Probing a Fedora ISO image

# blkid -s TYPE Fedora-Xfce-Live-x86_64-24-1.2.iso
Fedora-Xfce-Live-x86_64-24-1.2.iso: TYPE="iso9660"

blkid possesses additional useful capabilities. For example, it can check for the existence of particular filesystems and also display numerous pieces of detailed information. The option corresponding to this functionality is described on the program man page.

lsblk

The lsblk command outputs a list of block devices in a tree-like form. This makes mapping easier. The -f , long form --fs , option delivers the same information as:

-o NAME,FSTYPE,LABEL,MOUNTPOINT

In this way, you can see the details of how the individual storage media have been mounted.

The second column of the output contains information about the filesystem. Figure 3 shows sda1 (ext2), as well as sda5 (CryptoFS). Underneath you can see an LVM2 with logical volumes that have been set up and formatted as ext4 or swap. All of the filesystems have already been mounted.

Figure 3: lsblk offers easy to understand output in a tree-like form on all of the block devices.

Figure 4, on the other hand, shows an example with an ISO image that has been identified but not yet mounted. An ISO 9660 filesystem has already been allocated to the /dev/sr0 device. If needed, this can be mounted with the command:

# mount -t iso9660 /dev/sr0 /tmp/iso

In order for this to work, the /tmp/iso/ directory should already exist. If it doesn't, then you should first quickly set it up with:

mkdir /tmp/iso
Figure 4: Underlying the filesystem that has been recognized by lsblk, there is also an ISO image that has not yet been mounted.

parted

As with the graphical tools Kvpm [3] and GParted [4], parted proves to be information friendly. parted delivers detailed information about partitions with the -l (--list ) option. Listing 4 shows the excerpt from the last line of Figure 3. In the fifth column of the output, you will see the filesystem, which in this figure is ext4. GParted delivers similarly large amounts of information but much more elegantly and in graphical format (Figure 5).

Listing 4

parted output

Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/fehmarn--vg-home: 227GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags:
Number  Start  End    Size   File system  Flags
 1      0,00B  227GB  227GB  ext4
Figure 5: If necessary, GParted can deliver extensive details about the mounted filesystem.

file

Occasionally, the file command also comes in handy. This command yields information about a given entry in a filesystem. By default, it ignores unusual entries like "special files." However, file is able to read these entries with the help of the -s (--special-files ) option. Listing 5 shows the call for the fifth partition on the first hard drive. The call correctly finds an encrypted filesystem (LUKS) on the drive.

Listing 5

Using file

# file -s /dev/sda5
/dev/sda5: LUKS encrypted file, ver 1 [aes, xts-plain64, sha1] UUID: 305a1a1b-797a-4634-9acd-49c1f530978b

Conclusion

Specialized tools such as blkid , lsblk , parted , and file deliver extensive amounts of information about a storage medium's filesystem. Using these tools does not require much effort on your part. As a result, there should be nothing keeping you from cleanly mounting a data storage medium.