Linux – Check disk image file system type

disk-imagefilesystemslinux

How could I check the file system format of a disk image?

I know that I can check with the file command but I would like to automate the behaviour.

$ file img.raw
img.raw:   data

$ file img.ext4
img.raw:   Linux rev 1.0 ext4 filesystem data, UUID=346712e7-1a56-442b-a5bb-90ba4c6cc663 (extents) (64bit) (large files) (huge files)

$ file img.vfat
img.vfat:  DOS/MBR boot sector, code offset 0x3c+2, OEM-ID "mkfs.fat", sectors/cluster 16, reserved sectors 16, root entries 512, Media descriptor 0xf8, sectors/FAT 256, sectors/track 32, heads 64, sectors 1024000 (volumes > 32 MB) , serial number 0x4b5e9a12, unlabeled, FAT (16 bit)

I would like to check if the given image disk is formatted with the given format.

For example checkfs <image> <format> returns 0 if the image contains a 'format' file system, another value otherwise.

I thought about doing something like file <image> | grep <format> and check the return code, however for vfat filesystems, 'vfat' is not appearing on file's output.

I could also write a script to do it but I can't find tools which allow me to get the file system format of a disk image.

I've also tried with the following tools: fdisk, parted, and df.

Is there a tool which would allow me to check a disk image file system format and that works with most used file system formats?

Best Answer

Finally found what I needed

  • blkid -o value -s TYPE <image> will return the fs type or nothing if it's raw data.

EDIT:

As mentioned by @psusi, parted has a machine parsable output. I find it less convenient than using blkid but it could also be useful.

  • parted -m <image> print | tail -n +3 | awk -F ":" '{print $(NF-2)}' will print the fs type of each partition.

tail -n +3 is used to skip the two first lines
awk -F ":" '{print $(NF-2)}' is used to get the fs type which is the third last element starting from the end

Related Question