I am reading a book on creating drivers on Linux, I do not have too much experience with Linux, I have installed a quite a few distros before but I have always used windows as my main operating system.

I know some C, but I am mainly a C# programmer.

I wish to learn (for fun) driver programming, and hope it will teach me some things about Linux and "get into it".

My first hurdle is to find a distro with a standard 2.6.x kernel. I tried to find one but they are all past 3.x now. Any one know of a suitable one for this kind of task?

Also the book says I should get a standard kernel from kernel.org and build a source tree. I have no idea what that means.

This is the book: http://oreilly.com/openbook/linuxdrive3/book/

  • You can download older kernels from kernel.org – mkc Feb 17 '14 at 17:02
  • @Ketan yes fortunately I know that part. However I do not know what building a source tree means. – sprocket12 Feb 17 '14 at 17:03
  • AFAIK building means compiling in this context. Once you have your kernel unpacked look into README, INSTALL etc. docs. – mkc Feb 17 '14 at 17:10
  • 1
    Questions like this one come up regularly and we should have a canonical version, so I wrote one: unix.stackexchange.com/questions/115620/… – goldilocks Feb 17 '14 at 21:59

You know of kernel.org, and this is where you can get vanilla kernels from old or new. For the newest kernels you can also use git.

Once you have a kernel, to create a "source tree", you just unpack it. For example, if you downloaded a .bz2 archive, you could unpack it with the command tar -jxvf <kernel version>.bz2. Traditionally this tree is located in /usr/src and the symlink /usr/src/linux is made to point at the specific kernel directory you are using.

Once you have unpacked the source tree, to build it you need to first configure it e.g. make menuconfig (ncurses based) or make xconfig (X based). There are a few other config make targets if you are migrating a .config from a different kernel version. Finally, to build the kernel and its modules, use make. To install and boot the kernel use make modules_install for the modules and copy arch/<your arch>/boot/bzImage to wherever your bootloader expects kernels to be and configure it to boot that kernel. You will need to boot into the kernel if you expect to load any modules you build against it.

With that said, if you just want to experiment with building modules, just grab the source from your current distro kernel (there will be a package) and build modules against that kernel rather than going through the above process. It's not hard, but if you are new to the kernel it may be a bit overwhelming.

  • I have downloaded an ISO for CentOS 6.5 which uses a 2.6 kernel. Would that be sufficient (in a VM) or would I have to download another to get a source tree? – sprocket12 Feb 18 '14 at 11:47

It has been a while since I played with kernel drivers, but the following is what I had set up:

  • A multiboot machine, with at least 2 partitions for two distributions
  • One partition with an up-to-date Linux that stays stable, from which the MBR is boots.
  • A virtual machine with the target development kernel, running under the up-to-date Linux.
  • second partition with the target development kernel, which is booted through the grub configuration on the stable up-to-date Linux.

Initial development and testing is done on the virtual machine (VM) with only working drivers copied to the target development machine. You take regular snapshots of the VM, in particular before you inject a possible broken driver in the kernel of the VM. That way you can easily roll-back to a working development system state. If you don't need to test your driver on real hardware you do not need the second partition at all.

In your case I would install something like Ubuntu 10.04 on the VM (and the second partition). It comes with a 2.6 kernel and everything is there and fits together (any other distribution with a 2.6 kernel version). You just have to download the kernel sources and the development tools.

I think that will get you up and running faster than starting from scratch with a new kernel checked out from kernel.org, at least it did back in my SuSE 6 days.

  • Anthon, are you saying if I get Ubuntu 10.04 it already has its source tree of a 2.6 kernal, so I would not need to download/compile etc. for the book? – sprocket12 Feb 18 '14 at 11:46
  • @Muhammad you would have to install the source for the kernel, but you could do that with the normal apt-get install command, and you would get any dependencies as well -> less searching to get something that is compilable. – Anthon Feb 18 '14 at 11:48
  • Anthon, could I do the same with the current CentOS 6.5 which comes with a 2.6.x kernel? I know that would use yum but the same downloading of source should be possible right? – sprocket12 Feb 18 '14 at 11:50
  • I have not used CentOS, but I guess that would amount to the same, I suggested Ubuntu because that is what I am familiar with (now). You would have to search for kernel and src/source and get the package. There is a good chance that also pulls in all of the necessary tools to build the kernel (assuming the dependencies are correct). – Anthon Feb 18 '14 at 11:52

As per my commment, having seen questions similar to this before, I decided to write a canonical version which I think should get you started with configuring, building, and installing a kernel. But here I wanted to address some points specific to your case:

I wish to learn (for fun) driver programming, and hope it will teach me some things about Linux and help me "get into it".

I don't think this is a good reason. 99.9%+ of even long term power users never get involved with driver programming, including those who use the platform for development in C. By analogy, if someone who had never used MS Windows before said to you, "Well, I've got it installed now, and I thought a good way to learn about Windows would be to start writing a device driver for it...", what would you think?

I'd start fooling around with C doing platform specific things in userspace first, e.g., filesystem and networking stuff.

My first hurdle is to find a distro with a standard 2.6.x kernel. I tried to find one but they are all past 3.x now.

You don't need to do that. Most likely, a 2.6 kernel will still run fine on a current distro -- it's easy enough to try, of course. But beyond that, if the reason is because you are using LDD3 (the O'Reilly book), a 3.x kernel will still be compatible with most or all of that. Much (probably, most) of the driver code in the 3.x kernel exists unchanged from 2.6, and I'm sure the kernel devs consider it a priority to maintain this backward compatibility. The vanilla source is something like 16 million LOC; it can't all be constantly re-written.

So go ahead and try with whatever version you want.

  • Thanks goldilocks you are right it doesn't seem like a natural place to start with drivers, however since I have been programming at a higher level for the last 8 years I wished to try something low level, learning a bit of c and Linux at the same time. – sprocket12 Feb 18 '14 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.