How to Read Kobject Attributes in Linux

Embedded linux






In this article, we show how to read kobject attributes in linux.

Reading kobject attributes is very useful because they give us useful information about kobjects.

First, what is a kobject?

A kobject attribute is a basically a building block in the sys directory of linux, which is the directory which gives us a map or guide of all the devices in a linux system.

The sys directory gives us all the devices that are connected to all of the various buses in a linux system.

We can find every type of hardware device connected to our linux system in the sys directory. This includes the various device drivers, along with the devices themselves.

Kobject represent devices in our linux system.

Everything in linux is represented as a kobject.

A SSD hard drive is a kobjetc.

A microphone device connected to the USB bus is a kobject.

A USB memory card is a kobject.

A temperature sensor connected to the I2C bus is a kobject.

Everything in linux is represented by a kobject.

Kobjects, again, are the basic building blocks that allow the linux system to map out all of the devices in a system and put them in the sys directory. Every device in linux is represented by a kobject.

An important thing to know is that kobjects come with attributes. These attributes tell us specific information about kobjects.

All kobjects will be created with default kobject attributes, such as uevent.

uevent will tell us information such as the device major number, minor number, device name, device type, etc.

Let's see.

Let's go to the /sys/block/sda/sda1 directory.

This represents the directory which contains information about the sda1 device, which is the solid state disk hard drive partition 1.

So we go to this directory and see the following contents below.


Contents of the sda1 directory in linux


Looking into the contents of this directory, the items labeled in white are kobject attributes.

Kobject attributes give us specific information about the kobject.

This can be various things such as the device major and minor number, its size, its location in memory, its partition number, etc.

The information depends on the type of device that it is, but all devices have certain features all in common such as a major and minor number.

So let's now look at the values of some of these kobject attributes.

So let's take the first kobject, uevent, which is a default kobject attribute for all kobjects.

This gives us information such as the device major number, minor number, and device name, and can provide us with other information as well.

We read a kobject attribute in linux the same way we do so with any file, using the cat command.

If we want to read the uevent kobject attribute, we write, cat uevent

This is shown below.


Reading the uevent kobject attribute of sda1 in linux


With this attribute with the sda1 kosbject, we find out the major number, minro number, device name, device type, disk sequence number, and partition number.

Again, every kobject has a uevent object. Though if created by default, it may not have any parameters as if it was intentionally populated.

Now let's read other attributes of this sda1 kobject.

We do this below also with the cat command.


Reading various kobject attributes of sda1 in linux


The value of the start attribute is 2048. This means that the partition starts at 2048 bytes from the beginning of the disk.

The size attribute is 1048576 bytes, which shows the size of this sda partition, which is sda1.

The partition attribute tells us that this is the first sda partition, sda1.

The dev attribute tells us the device number of the device, including the major and minor number.

So this is how we can read the values of kobject attributes in linux.

And this we can do with any other kobject (device) in the sys directory.

Kobject attributes, again, are very useful for obtaining information from a device.

Kobject attributes specify device information which can be very useful for a linux user.



Related Resources





HTML Comment Box is loading comments...