setting up a development environment for Linux device driver - linux-kernel

I am trying to read the LDD book by Jonathan Corbet, Greg Kroah-Hartman, Alessandro Rubini and implement the sample modules. So to begin with, I tried setting up a development system. Installed Ubuntu 16.04 Xenial. Now, I just created a directory and wrote the hello_world module with a Makefile. Got it built and run it, verified the dmesg logs.
Is that all the development setup? I searched online and found articles where they are asking to download and compile the kernel, use a VM to boot the kernel. What is the reason? Or what am I missing?
Is there any better article which clarifies this?
Thanks
hago

You can try one more way:
If you have native windows, install virtual machine software such as
Virtual box. Get your favourite Linux distribution (no bias, just
an example - Ubuntu) and install it through Virtual box.
Get the latest kernel (or of your choice) from kernel.org.
Choose the platform you want to build this kernel for. E.g arm64 or x86.
In case you do not have real boards (e.g RPi for arm variant), you can use qemu-arm64 or qemu-x86 to run your compiled kernel. This is also a good option when users do not have the boards.
Another good use case for using qemu for the newbie kernel developers is even they write some modules which crashes, then the qemu instance is crashed so no harm.
I think using qemu is a good option for people who starts to learn kernel programming and also want to try writing some of their modules and do not intend to purchase hardware at this point of time.

It depends on your target. For your case, you have made a kernel driver for your computer (it run Linux kernel).
But if you want to develop a Kernel driver for another target like Rasberry Pi, ARM board, X86-X64 board, ... you must learn to compile, edit Kernel config, boot Kernel image, ... because each target has different kernel versions.
You can refer to this training for more detail: https://bootlin.com/training/embedded-linux/

Related

Buiding kernel for an ARM processor

I working on Odroid XU3 with the ubuntu platform. For the DS5 software to crosscompile for profiling , I need to build Linux kernel with specific configuration. I am new to this stuff, but I have created the UImage of the kernel on the host machine for the Arm processor. I need to ask how one can get that kernel copy in the target platform i.e. Odroid.
Because for the profiling I need to have gatord and kernel with specific configuration installed on the target machine. I am done with gatord and build the kernel on host. Just need to copy it on target. But it is not happening using the sdcard of the odroid. Please let me know.
So if you have created uImage that seems like you have U-Boot as a bootloader on your target board. U-Boot in its turns can download kernel uImages via TFTP. I haven't worked with such devices as yours, but if it has Ethernet port, you could use it.
Also you have to know the U-Boot commands (fortunatelly there are a lot of information can be found over Internet, just ask google.)

How to install a bare Linux kernel without any distribution to study it?

I want to study the kernel of Linux without any distribution.
I found the LoadLin boatloader of Ms-dos, but i think it works only in older version of windows (windows 95,98, ME).
So i need to install the kernel only in my PC if Possible.
How I can install it?
The kernel only is not that much useful to you; you'll probably need some shell and a working compiler if you want to test things first-hand, and these are not part of the kernel.
There's a distribution called Linux From Scratch which basically allows you to install the kernel and then whatever other stuff you want, literally from scratch (as in, by compiling stuff yourself and only adding what YOU want)
I am wondering though, what is it exactly you want to study and how does having a distribution affect your studying of the kernel? (Yes, some distributions ship custom kernels but the major features are almost always the same)
Minimal Linux Live is a small script that:
downloads the source for the kernel and busybox
compiles them
generates a bootable 8Mb ISO with them
The ISO then leaves you in a minimal shell with busybox.
With QEMU you can then easily boot into the system, which might be a more convenient way to study the kernel.
Or you can just use the Live ISO as a regular distribution and install it on metal.
Usage:
git clone https://github.com/ivandavidov/minimal
cd minimal/src
./build_minimal_linux_live.sh
# Wait.
# Install QEMU.
# minimal_linux_live.iso was generated
./qemu64.sh
and you will be left inside a QEMU Window with you new minimal system. Awesome.
See also:
https://unix.stackexchange.com/questions/17122/is-it-possible-to-install-the-linux-kernel-alone
https://superuser.com/questions/307087/linux-distro-with-just-busybox-and-bash
Why not use a distribution? Just get some free VM (eg. virtualbox) and install an arbitrary Linux distribution. You have all the build tools there you need to compile the kernel, without actually touching your system.

Linux kernel on virtual machine

I am studying Linux driver programming and in it, it is recommended that I work on self-compiled Linux kernels and not any distributions. I have tried compiling Linux 2.6.9 in ubuntu but the process returns errors in 'make menuconfig' stage.
I would prefer to work with Linux in a virtual environment so that I can fearlessly experiment with the kernel. So, is there any way I can compile and run Linux in a virtual machine (say VMware installed on Windows)? I can use live CDs for the purpose of compiling the kernel.
So in short, please suggest, how can I compile, install and run Linux kernel in a virtual machine in an error-free way?
I searched and read this. But after following these steps when I restarted my computer there was no separate Linux 3.2.17 OS. But my ubuntu 12.04 was now showing 3.2.17 kernel. Although this is the first time I could compile a whole kernel on ubuntu without any error, I want to load that kernel on other partition and use it as an independent OS. So, if anyone can tell, what to do in addition to the steps in the tutorial so that I can achieve this?
The simplest thing to do is probably to install some Linux distribution on a VM, such as VMWare or VirtualBox, and continue from there. You could try using a live-cd, but I'm guessing that the lack of persistent storage might get irritating. There are, of course, ways around that, but installing some distribution is probably simpler, and you don't really need that much disk space for it if all you want to do is compile a kernel.
If all you want to do is compile a kernel module, and if you already have some pre-installed Linux environment, you should also note that modern Linux installations allow you to compile modules without the need to re-compile the entire kernel. You will need the kernel source and headers, though. See, for example, this document.
And BTW, speaking of modern kernels, why did you choose to use 2.6.9? It's almost 8 years old by now. Newer kernels might actually be easier to develop for. Also, there's no guarantee that
modules developed with such an old kernel would still work with current ones.
I suggest you to read this page. This document shows you how to boot your personal kernel on qemu and how to use the debugger on it.
Kernelnewbies is the right place to start kernel hacking. This website contains a set of rich tutorials about kernel hacking and tweaking just for newbie Linux developers. Also, you can join the community and start contributing to some tiny Linux projects.
For a quick start, follow the instruction from the "kernel first patch" tutorial. Since you're cloning the "origin" remote repository in this tutorial, you'll work on the latest branches of Linux kernel. So, there's no need to worry about working on an old version of Linux. Meanwhile, if you're not comfortable working with git trees, you can always download the latest version of Linux from front page of "kernel.org".

Playing/Learning -- QEMU (for ARM), Angstrom Linux (or Debian)

My ultimate goal is to do some programming for the Angstrom Linux (or Debian or other Linux distros), on QEMU emulating ARM processor board s.a. Versatile board. I am happy to experiement, but if someone has attempted something similar, and can give little guidance, it might hasten progress.
My understanding of the steps needed are:-
1. Build QEMU from source (although I am not sure if a prebuilt binary won't do). I found QEMuManager on Windows (XP being my Desktop OS on which I intend to run QEMU).
2. Install ARM tool chain (e.g. Yagarto / GNU-ARM for Cygwin?)
3. Download an Angstrom Linux tarball and build it
4. Create a QEMU image with Angstrom Linux.
However I am missing on the details, as I believe there are choices to be made at each of those steps.
IMHO you should use a linux distribution as host machine for your QEmu instead of trying to compile/install all the QEmu stuff in a cygwin based system, it will remove some futur headaches. You can use a VMWare player with an ubuntu image.
I used to play with this tutorial for Debian on QEMU.
The beagleboard, hawkboard, open-rd sites all tend to lead to their distros being built on qemu (arm), and from there there is no reason why you cannot just continue to keep running on the simulation instead of heading for hardware.
This is an example of how to do it with ubuntu.
https://wiki.edubuntu.org/ARM/RootfsFromScratch
Yes it is also possible to cross compile everything as well, I would start with wiki pages that hand hold you through all of the steps. Or as with the hawkboard or beagleboard get a pre-built binary (kernel and root file system) and just boot it and run on that environment and not mess with building everything.

Low level qemu based debugging

I've to test some low level code on an ARM architecture. Typically experimentation is quite complicated on the real board, so I was thinking about QEMU.
What I'd like to get is some kind of debugging information like printfs or gdb. I know that this is simple with linux since it implements both the device driver for the QEMU Integrator and the gdb feature, but I'm not working with Linux. Also I suspect that extracting this kind of functionality from the Linux kernel source code would be complicated.
I'm searching from some simple operating system that already implements one of those features. Do you have some advice?
You don't need a target OS to debug code that's running inside QEMU -- QEMU already does that for you.
Specifically, QEMU supports remote debugging from GDB -- you can run QEMU with the appropriate command-line options and it will export an interface that a copy of GDB (running on the host machine) can connect to. At that point, you can debug the program in GDB pretty much just as if you were running it on the host machine.
http://wiki.osdev.org/GDB appears to have a bit more basic information; possibly not enough to completely get you started, but at least give you the basic idea and some terms to look for in the QEMU and GDB documentation. Skip over the bit about "Implementing GDB Stubs", which doesn't apply here since QEMU has one already, and start at the section on "Using Emulator Stubs". The short form is simply that you start QEMU with the -s option (export a GDB connection on localhost:1234) and the -S option (wait for a GDB "continue" command before starting execution), and then in GDB on your host you say target remote :1234 instead of run. Also, of course, you need to be using an ARM version of GDB rather than a native-x86 one.
(In addition, if you're willing to pay for a commercial solution, CodeSourcery's ARM toolchain has the IDE integration to set all of this up automatically, including support for "printf" to print into the debugger console. That works on a physical board, too, if you've got a hardware debugger. Usual disclaimer about me being a CodeSourcery employee applies -- but I do find it very easy to use.)
Update, 2012: CodeSourcery's toolchain is now called Mentor Graphics Sourcery CodeBench, but all the above still applies.
I realise that I am addressing your original problem here rather than your proposed solution (perhaps that's better?), but to use GDB (or Insight/GDB) directly on the target, use a low-cost JTAG tool and OpenOCD. An example of such a set-up and how to implement it can be found here.
If you have a larger budget, a more fully featured JTAG debugger may be useful, such as the Abatron BDI3000 with bdiGDB firmware which allows remote debugging and device programming over Ethernet with GDB and no special drivers or target debug agent.
Maybe a microkernel like OKL4 would suit your needs?

Resources