Archive for May, 2009

Compile Linux 2.6 Kernel Modules

The following Captain howto shows how to compile (your own) custom linux 2.6 kernel modules in general.

If you want to compile the sum-module (source mirrored below), follow these steps:

Create the Makefile in your directory with the sum-module.c

obj-m := sum-module.o

KDIR := /lib/modules/$(shell uname -r)/build

PWD := $(shell pwd)

default:

$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

Now do a

make

… and the sum-module.ko is built.

If you get something like this

# make

make: Nothing to be done for `default’.

you need to install the kernel source and compile the kernel first (run “make” at least to the point until all “HOSTCC scripts/” stuff is done – this will configure your kernel and allows external module compilation). Make sure /lib/modules/$(shell uname -r)/build points to your build directory (most likely /usr/src/linux…).

Another reason for the above error can be, that your browser converted the TAB before $(MAKE) to spaces. Make sure there is a TAB before $(MAKE).

Install it with install.sh:

#!/bin/sh

install -m 644 sum-module.ko /lib/modules/`uname -r`/kernel/drivers/sum-module.ko

/sbin/depmod -a

(adjust the /lib/modules path according to your needs)

Now make a

# modprobe sum-module

Or if you don’t want to install the module, do this:

# insmod ./sum-module.ko

..and if your system doesn’t freeze you’ve done it right ๐Ÿ˜‰

For kernel 2.4, the Makefile would look like this:

TARGET := modulename

INCLUDE := -I/lib/modules/`uname -r`/build/include

CFLAGS := -O2 -Wall -DMODULE -D__KERNEL__ -DLINUX

CC := gcc

${TARGET}.o: ${TARGET}.c

$(CC) $(CFLAGS) ${INCLUDE} -c ${TARGET}.c

(not yet tested)

sum-module source from: http://www.win.tue.nl/~aeb/linux/lk/lk-9.html

/*

* sum-module.c

# modprobe sum-module.o

# ls -l /proc/arith

total 0

dr-xr-xr-x 2 root root 0 Sep 30 12:40 .

dr-xr-xr-x 89 root root 0 Sep 30 12:39 ..

-r–r–r– 1 root root 0 Sep 30 12:40 sum

# cat /proc/arith/sum

0

# echo 7 > /proc/arith/sum

# echo 5 > /proc/arith/sum

# echo 13 > /proc/arith/sum

# cat /proc/arith/sum

25

# rmmod sum-module

# ls -l /proc/arith

ls: /proc/arith: No such file or directory

#

*/

#include

#include

#include

#include

static unsigned long long sum;

static int show_sum(char *buffer, char **start, off_t offset, int length) {

int size;

size = sprintf(buffer, “%lld\n”, sum);

*start = buffer + offset;

size -= offset;

return (size > length) ? length : (size > 0) ? size : 0;

}

/* Expect decimal number of at most 9 digits followed by ‘\n’ */

static int add_to_sum(struct file *file, const char *buffer,

unsigned long count, void *data) {

unsigned long val = 0;

char buf[10];

char *endp;

if (count > sizeof(buf))

return -EINVAL;

if (copy_from_user(buf, buffer, count))

return -EFAULT;

val = simple_strtoul(buf, &endp, 10);

if (*endp != ‘\n’)

return -EINVAL;

sum += val; /* mod 2^64 */

return count;

}

static int __init sum_init(void) {

struct proc_dir_entry *proc_arith;

struct proc_dir_entry *proc_arith_sum;

proc_arith = proc_mkdir(“arith”, 0);

if (!proc_arith) {

printk (KERN_ERR “cannot create /proc/arith\n”);

return -ENOMEM;

}

proc_arith_sum = create_proc_info_entry(“arith/sum”, 0, 0, show_sum);

if (!proc_arith_sum) {

printk (KERN_ERR “cannot create /proc/arith/sum\n”);

remove_proc_entry(“arith”, 0);

return -ENOMEM;

}

proc_arith_sum->write_proc = add_to_sum;

return 0;

}

static void __exit sum_exit(void) {

remove_proc_entry(“arith/sum”, 0);

remove_proc_entry(“arith”, 0);

}

module_init(sum_init);

module_exit(sum_exit);

MODULE_LICENSE(“GPL”);

via Linux Kernel 2.6 – Compile Modules.

Saturday, May 9th, 2009 Uncategorized No Comments

Compiling Debian-supported kernel modules using module-assistant

The howto below indicates how to use Debian module-assistant to compile kernel modules available via sources.list (possibly after patching them) in a relatively fussless way.

To get my webcam gspca compatible working under Linux Mint 6 this is what I ended up having to do:

1: install build-essential, gspca-source and module-assistant:

Code: Select all

sudo apt-get install build-essential module-assistant gspca-source

2: get gspca.patch.gz attached

3: untar the gspca module tarball in /usr/src:

Code: Select all

sudo tar zxvf gspca.tar.gz

4: copy gspca.patch.gz to /usr/src/modules/gspca:

Code: Select all

sudo cp ~/Desktop/gspca.patch.gz /usr/src/modules/gspca

change command to match where you downloaded the file to

5: change to module directory:

Code: Select all

cd /usr/src/modules/gspca

6: apply the patch:

Code: Select all

sudo zcat gspca.patch.gz | patch -p0

7: run module assistant as such:

Code: Select all

m-a –not-unpack a-i gspca

8: plug in camera.

9: do:

Code: Select all

dmesg

and look for lines resembling:

Code: Select all

[1382659.289831] gspca: USB GSPCA camera found.ZC3XX

[1382659.289841] gspca: [spca5xx_probe:4289] Camera type JPEG

[1382659.289846] gspca: [zc3xx_config:588] Sensor MC501CB

[1382659.300995] gspca: [spca5xx_getcapability:1252] maxw 640 maxh 480 minw 160 minh 120

[1382659.302026] usbcore: registered new interface driver gspca

[1382659.302269] gspca: gspca driver 01.00.20 registered

If it’s there, then the module loaded correctly. If not, you may need to do:

Code: Select all

sudo modprobe gspca

My webcam is a Logitech Quickcam Connect E2500 usb ID from lsusb: 046d:089d Logitech, Inc.. This is the only way I could get it to work.

via Linux Mint Forums โ€ข View topic – Howto: install gspca webcam module Mint 6, kernel 2.6.27-11.

Saturday, May 9th, 2009 Uncategorized No Comments

Exiting vim

When trying to exit vim, but instead a menu comes up, chances are that you hit a “q” key of the quit command before hitting the semicolon, for example:

wq:

instead of

:wq

When this happens often, you can consider using

ZZ

to save and exit or

ZQ

to discard and exit.

Saturday, May 9th, 2009 Uncategorized No Comments

Piping stderr to another program

How to Pipe standard error STDERR to another program

Everybody who has a little knowledge in linux shell know about piping and redirection. Piping is done by โ€˜|โ€™ sign and for redirection โ€˜>โ€™, โ€˜<โ€™, โ€˜<<โ€™, โ€˜>>โ€™, โ€˜<<<โ€™ signs are used.To redirect standard output of a program to a file we use

% foo > bar.txt

or

% foo 1> bar.txt

To redirect standard error of a program to a file we use

% foo 2> bar.txt

The pipe command is used normally to pass one commands output to another programs input. To pass output of `foo` to `bar` we use

% foo | bar

The problem is `bar` always gets the standard output of `foo`. Not standard error.

I was trying like redirection.

% foo 2| bar

But this didnโ€™t work. After some trial and error I found a way.

See the command bellow.

% foo 2>&1 1>/dev/null | bar

The extra part here is โ€œ2>&1 1>/dev/nullโ€œ. What does it do? How does it work?

Well, `bar` takes output of `foo` not error. Keeping this in mind if I could redirect the standard error to standard output of `foo`, `bar` will get what it wants. But if I just redirect stderr to stdout, stdout will contain both original stdout content and new stderr content. To overcome this, I removed the original content of stdout at first then I do redirection.

1>/dev/null cleans the original content of standard output, then

2>&1 redirects standard error to standard output.

When `bar` is executed it has the input.

via Thoughtful Thoughts ยป How to Pipe standard error STDERR to another program.

Thursday, May 7th, 2009 Uncategorized No Comments

Gimp: add colors and alpha to indexed image

If you have an image in indexed mode, simply select Image –> Mode –> RGB to be able to do all kinds of fancy color things.

Monday, May 4th, 2009 Uncategorized No Comments