Creating a file under /sys/devices in Linux - linux

I want to create a file under /sys/devices directory in Linux.
What is the best way to do this?

These answer and explanation came up after a quick google-search:
Why cant I create a directory in /sys - Link removed because of limitation
Wikipedia: Sysfs - Link removed because of limitation
If you absolutely have to modify/create anything there, you should first understand how /sys works. And why you want to change it.
EDIT: Petesh pointed out that you where indeed referring to drivers.
As I understand it, /sys/devices is simply a place for devices to dump information about themselves. You don't insert drivers here.
The drivers, or modules, are either implemented into the kernel before compiling it.
Or you can add the module to /usr/lib/modules/uname -r/extramodules/, or overwrite it in /usr/lib/modules/uname -r/kernel/fs/btrfs/
You may also want to look at these:
Arch: Manual module handling
The Linux Kernel Module Programming Guide

Related

How to get config data from a Linux kernel module?

I'm working on a module in the Linux kernel that needs access to a configuration file containing some basic text values.
Problem is, I read that it's a no-no to read files from within the kernel. This article says to "use sysfs files within a kernel module" - problem is I'm not sure what that means.
I need something dirt simple. Does anyone know how I can create a custom config file for my custom module and read it in at runtime?
I'm working with Linux kernel 3.12.0.
If you truly need dirt simple, then pass parameters to the module on the insmod/modprobe command line.
To see how to declare parameters, look at other people's modules.
(To do anything in a module that other people's modules already do, look at other people's modules, including how to register into sysfs.)
If the configuration can change at runtime, while the module stays inserted, then you can't use module parameters for it.

Make changes to embedded linux kernel code

I'd like to understand & edit(adding printk, etc.) the linux kernel for a craneboard, which I obtained from github. I'm a newbie. I have gone through certain questions related to this in stackoverflow, but they all concentrate on linux in PC (not in embedded!). I don't know where to start. Any kind of guidance is accepted. Thanks in advance.
Dear did you got chance to look at README located at
https://github.com/craneboard/craneboard-kernel
if you need to add any information or some logging i.e. adding printk in kernel itslef, you need to recompile it, it also shows how to do it. But i am interested to know why you need to put logging information in kernel? Are you going to fix some bug in kernel?
Printk is same like a C Printf, but with few differences http://www.makelinux.net/books/lkd2/ch18lev1sec3
Also have a look at http://processors.wiki.ti.com/index.php/CraneBoard
It might also be a good start to look at https://linuxlink.timesys.com/files/pdf/Timesys-EmbeddedLinuxTutorial.pdf
You'll need an ARM toolchain before you can compile the kernel. Here is a good tutorial.

Linux driver access through sysfs

I'm making a small kernel module to provide user-space access to some kernel-mode only features of an ARMv7 chip (specifically, cache control). I'm reading through Linux Device Drivers by Corbet, Rubini, and Hartman. In it they describe how to make a full driver+device+bus. I don't want to create a bus driver at all. In fact the 'driver' that I'm making doesn't really need to match against a device definition at all - it is implicitly matched to the platform's CPU. Can anyone explain to me:
Where in sysfs should my attributes go? Should it be in my module entry under /sysfs/modules/mymodule? /sys/devices/platform seems promising too, and so does /sys/devices/system/cpu.
If there is an existing place where I should put my kobject/attributes, how do I plug it into it? How do I get the necessary kset? All the examples I've seen create a kset and then link to it from the kobject - I haven't seen an API for requesting an existing named kset?
Sorry if this is just impossibly obvious, or if there's some really straightforward and easily discovered example somewhere that I haven't discovered for some reason. Can anyone shed any light on this?
I haven't worked with sysfs much, but I found a simple-looking example that's pretty similar to what you are doing (naturally, it's also under ARM). Take a look at arch/arm/mach-omap1/pm.c, specifically the idle_show/idle_store sysfs file. It gets registered (using sysfs_create_file()) as /sys/power/sleep_while_idle and uses the global /sys/power kobj (defined in include/linux/kobject.h). There are a few other global kobj's defined there that you could use, although I'm don't think any are a good fit for your driver.
Is this going to be a platform driver? As a driver that doesn't fit under any bus, it seems like a good fit. Platform drivers get their own directory under /sys/devices/platform, and can have attributes there. Take a look at drivers/hwmon/coretemp.c, which has temp1_crit, temp1_crit_alarm, temp1_input, etc. as attributes. It looks fairly simple: create the attributes (maybe with __ATTR()?), list them all in an array, define an attribute_group, register it with sysfs_create_group() in the probe() function, and unregister it with sysfs_remove_group() in the remove() function.
There are probably other platform drivers that define attributes (search for sysfs_create_group) if you need other examples. Hope this helps!

where can I find a good enough default config to compile the Linux kernel?

i want to compile a custom linux kernel for myself. because i dont know every option of kernel therefore i am looking for a good default config for start.
You should check out KernelCheck. It's basically an app that will help you figure out what kernel options are worth tweaking for your particular system. It will also automatically download, compile and install the selected kernel for you.
If you have a good running kernel that has builtin support for stored configuration (i.e. /proc/config.gz exists), then you could copy that kernel configuration and use it as a starting point:
zcat /proc/config.gz > /path/to/kernel/source/.config
Look in the repository of the Linux distribution you are using, they often have a generic or a huge kernel .config, those are the "best choices".
From the Linux kernel source tree, type make help and look for the lines that include defconfig. Default configurations for various platforms are typically included here. Once you find the one that matches your platform, type, for example make defconfig to get your default .config file. For example, to build on a Nexus S, I use make herring_defconfig. (herring is, I guess, the code name for the Nexus S hardware, which can incidentally be found if you look at /proc/cpuinfo on the Nexus S)
If you are not building for x86 (for example, like in my case, if you are building for Android) be sure to remember to export ARCH (for example, export ARCH=arm before running make). Otherwise, make help will not show the correct defconfig targets for the architecture you want to build.
I haven't done this in a while but aren't there sane defaults in the config dialogs?
This question is old, but I feel compelled to add http://kernel-seeds.org/ as a very viable option for a starting point to compile a fitting custom kernel -- as there are seeds for the vanilla sources, even if you are not on gentoo.

Pseudo filesystems on *nix

I need some opinions pointers on creating pseudo-filesystems for linux/*nix systems.
Firstly when I say pseudo-filesystem I mean something like /proc where the structure within does not represent actual files on disks or such but the state of the kernel. I would like to try something similar as an interface to an application.
As an example you could say, mount a ftp url to your filesystem and your browser app could then allow you to interact with the remote system doing ls et al on it and translating the standard filesystem requests into ftp ones.
So the first question is: how does one go about doing that? I have read a bit about it and it looks like you need to implement a new kernel module. If possible I would like to avoid that - my thinking being that someone may have already provided a tool for doing this sort of thing and provided the module to assist already.
My second question is: does anyone have a good list of examples of applications/services/whatever using this sort of technique to provide a filesystem based interface.
Lastly if anyone has any opinions on why this might be a good/bad idea to do such a thing on a generic level I would like to hear it.
A userspace filesystem via fuse would probably be your best way to go.
Regarding the next part of your question (which applications use this method), there is the window manager wmii, it uses the 9p filesystem via v9fs, which is a port of 9p to Linux. There are many examples on plan9, most notably acme. I suggested fuse because it seems more actively developed and mainstream in the Linux world, but plan9 is pretty much the reference for this approach as far as I know.

Resources