How to make a sysfs entry

Okay, this was a long time pending task. I wanted to learn how to make the sysfs entry. I was always deferring this task as it was a bit boring. The alternative I had was of using procfs. Finally from a nice article and some r&d in kernel code lead me to write this sample code.

Source Link :

The code provided in the link doesn’t work for kernel version I have modified it a bit to make it work with latest kernel, but it makes the top level entry under /sys – but it’s okay for learning purpose.

Here is the code,

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/slab.h>

struct my_attr {
    struct attribute attr;
    int value;

static struct my_attr my_first = {"first",
    .attr.mode = 0644,
    .value = 1,

static struct my_attr my_second = {"second",
    .attr.mode = 0644,
    .value = 2,

static struct attribute * myattr[] = {

static ssize_t default_show(struct kobject *kobj, struct attribute *attr,
        char *buf)
    struct my_attr *a = container_of(attr, struct my_attr, attr);
    return scnprintf(buf, PAGE_SIZE, "%d\n", a->value);

static ssize_t default_store(struct kobject *kobj, struct attribute *attr,
        const char *buf, size_t len)
    struct my_attr *a = container_of(attr, struct my_attr, attr);
    sscanf(buf, "%d", &a->value);
    return sizeof(int);

static struct sysfs_ops myops = {
    .show = default_show,
    .store = default_store,

static struct kobj_type mytype = {
    .sysfs_ops = &myops,
    .default_attrs = myattr,

struct kobject *mykobj;
static int __init sysfsexample_module_init(void)
    int err = -1;
    mykobj = kzalloc(sizeof(*mykobj), GFP_KERNEL);
    if (mykobj) {
        kobject_init(mykobj, &mytype);
        if (kobject_add(mykobj, NULL, "%s", "sysfs_sample")) {
             err = -1;
             printk("Sysfs creation failed\n");
             mykobj = NULL;
        err = 0;
    return err;

static void __exit sysfsexample_module_exit(void)
    if (mykobj) {


Have fun!!


4 thoughts on “How to make a sysfs entry

  1. I was really having a hard time learning sysfs…and in the book LDD 3rd edition the code is outdated…Thanks for the modification for latest kernel and sharing

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s