How can I get the label of a filesystem using /sys
? I know I can get much of the info about a block device by going to /sys/class/block/<device>
, e.g. /sys/class/block/sr1
for a cd that I know has the filesystem label config
. I hunted through each item, found everything but the label.
I did dig through the lsblk
source code, which, in turn, depends on calling udev_device_new_from_subsystem_sysname
in libudev
, so I went through that. It does appear to populate the property ID_FS_LABEL_ENC
, but I cannot figure out where it takes it from in the tree, unless it is tracking it elsewhere?
I would just use libudev
, but need to access outside of a C program.
I think that the problem here is that you seem to think that the label of a volume is a kernel thing, as is the size or the free space.
But AFAIK it is not, the kernel doesn't care at all about volume labels, it is just a thing that goes from the in-disk format to user-land: there is no kernel API to get that information. If you need it, you just open the raw binary volume and read the data from there.
But then, there is the big issue that every filesystem is different, so you need special code to manage every single partition type there is. Fortunately, somebody has done the hard work, and you have blkid
, part of util-linux
available in most Linux distributions. If you need it, you can call the program directly, or link to the library libblkid
that does the hard work.
Naturally, to use blkid
/libblkid
you need read access to the block device, that is, root access. If you think that root access should not be needed to read a label, the people from udev
think the same, and that is why there is a udev rule that copies the label when the filesystem is first dectected (running blkid
of course). This is the ID_FS_LABEL_ENC
you already know about.