Difference between revisions of "How-to compile the Scull examples"

From Klaus' wiki
Jump to: navigation, search
(A helper script)
 
(47 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
This is a small how-to for preparing and compiling the Scull examples from the book Linux Device Drivers. The examples can be found on bren in /home/emb .
 +
 +
==Install and update system==
 +
 
As '''root''' on your virtual server execute the following commands:
 
As '''root''' on your virtual server execute the following commands:
  
 
<source lang="bash">
 
<source lang="bash">
$ yum install gcc kernel-xen-devel ncurses-devel
+
$ yum install gcc kernel-devel ncurses-devel
 
$ yum update
 
$ yum update
 
#
 
#
Line 13: Line 17:
 
</source>
 
</source>
  
Next create the ordinary users for the system with
+
==Create an ordinary user==
 +
Next create the ordinary users, if you do not have any yet, for the system by, as root, issuing:
 +
 
 
<source lang="bash">
 
<source lang="bash">
 
$ adduser <username>
 
$ adduser <username>
 
$ passwd <username>
 
$ passwd <username>
 
</source>
 
</source>
 +
 
As root edit the /etc/group file and add a new group at the end. Copy the line above (one of the ordinary useres) and change the group number to bee different from any other numbers assigned in the file. Add the usernames after the last : having the file looking something like this:
 
As root edit the /etc/group file and add a new group at the end. Copy the line above (one of the ordinary useres) and change the group number to bee different from any other numbers assigned in the file. Add the usernames after the last : having the file looking something like this:
 +
 
<source lang="bash">
 
<source lang="bash">
 
klausk:x:500:
 
klausk:x:500:
Line 24: Line 32:
 
</source>
 
</source>
  
Now as '''an ordinary user''' unpack the example sources if you haven't done that already.
+
Exchange klausk with your name - separate usernames with comma (,).
  
<source lang="bash">
+
Fianlly create a directory for the examples.
$ mkdir -p /home/eml1
+
 
$ chgrp moduledev /home/eml1
+
<source lang="C">
$ cd /home/eml1
+
$ mkdir -p /home/emb2
#
+
$ chgrp moduledev /home/emb2
# Copy the examples to this location from where you've stored them i.e. cp /path/exampes.tar.gz .
+
$ cd /home/emb2
 +
$ chmod g+rwx /home/emb2
 
#
 
#
 +
</source>
 +
 +
After editing in the groups file users, that may be included in a specific group must log-out and re-login in order to get the nes group membership working.
 +
 +
==Unpack the examples==
 +
 +
Logout as root and logout as the ordinary user. Re-login as the ordinary user (which now will become a member of the moduledev group).
 +
 +
Retrieve the examples from bren.
 +
 +
<source lang="bash">
 +
$ cd /home/emb2
 +
$ scp <yourBrenUsername>@bren.hih.au.dk:/home/emb/examples.tar.gz .
 +
</source>
 +
 +
Now unpack the example sources:
 +
 +
<source lang="bash">
 
$ tar zxvf examples.tar.gz
 
$ tar zxvf examples.tar.gz
 
$ chgrp -R moduledev examples
 
$ chgrp -R moduledev examples
 
$ cd examples
 
$ cd examples
 
</source>
 
</source>
 +
 +
==A helper script==
  
 
Create a helper script as described below.
 
Create a helper script as described below.
Line 47: Line 76:
 
# Preparation for the compilation of the scull example code
 
# Preparation for the compilation of the scull example code
 
#
 
#
KERNELDIR=/usr/src/kernels/2.6.18-194.11.4.el5-xen-i686/
+
KERNELDIR=/usr/src/linux-headers-`uname -r`/
 
export KERNELDIR
 
export KERNELDIR
 
</source>
 
</source>
  
When entering the example directory execute this
+
After entering the example directory execute this
  
 
<source lang="bash">
 
<source lang="bash">
Line 62: Line 91:
 
</source>
 
</source>
  
 +
'''NOTE''': Remember to run the . settings.sh everytime you enter the examples directory!
 +
 +
==Adjust the sudoers file==
  
 
Remember that only root can insmod a module in the kernel.
 
Remember that only root can insmod a module in the kernel.
  
If you add a line like this into /etc/sudoers using '''visudo'''
+
If you add a line like this into /etc/sudoers using '''visudo''' (only accessible as '''root''')
 
<source lang="bash">
 
<source lang="bash">
klausk  ALL=(ALL)      NOPASSWD: ALL
+
<klausk> ALL=(ALL)      NOPASSWD: ALL
 
</source>
 
</source>
 +
 +
Substitute <klausk> with your username.
 +
 +
NOTE: You have to be careful with your password strength for the ordinary user logins otherwise you may risk somebody else compromises your server.
 +
 +
==Load a kernel module==
  
 
You can now issue this to insert the module in the kernel:
 
You can now issue this to insert the module in the kernel:
  
 
<source lang="bash">
 
<source lang="bash">
 +
$ sudo chmod +x scull_load
 
$ sudo ./scull_load
 
$ sudo ./scull_load
 
#
 
#
Line 79: Line 118:
 
$ sudo /sbin/lsmod
 
$ sudo /sbin/lsmod
 
</source>
 
</source>
 +
 +
==Fix the code to fit the current kernel==
 +
 +
In the program misc-progs/setlevel.c you have to change a line of code:
 +
 +
At line 30 insert this code and put in comments on the line defining _syscall3
 +
<source lang="C">
 +
#include <sys/klog.h>
 +
//_syscall3(int, syslog, int, type, char *, bufp, int, len);
 +
</source>
 +
 +
In the driver sbull/sbull.c comment the code in function sbull_full_request out - just leave the function empty. It's a block device, that we will not cover in the course.
 +
 +
In the driver usb/usb-skeleton.c at line 224 comment out the .mode field - it is depricated.
 +
 +
In the same file at line 328 comment out the .owner field.
 +
 +
In file tty/tiny_tty.c at line 79 comment out the
 +
 +
<source lang="C">
 +
                if (tty->flip.count >= TTY_FLIPBUF_SIZE)
 +
                        tty_flip_buffer_push(tty);
 +
</source>
 +
 +
and inset these two lines instead:
 +
 +
<source lang="C">
 +
              if (tty_insert_flip_string(tty, data, data_size))
 +
                        tty_flip_buffer_push(tty);
 +
</source>
 +
 +
Put in comments on line 534 and 538 on lines looking like this:
 +
<source lang="C">
 +
//tiny_tty_driver->devfs_name = "tts/ttty%d"
 +
and
 +
//tiny_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
 +
</source>
 +
 +
 +
Finally in file lddbus/lddbus.c at line 73 comment out the line (.hotplug...)
 +
 +
The code should now be ready for making against the current kernel.
 +
 +
NOTE: If you are going to use the above patched code please check that the corrections are not conflicting with any required functionality.

Latest revision as of 16:01, 13 March 2016

This is a small how-to for preparing and compiling the Scull examples from the book Linux Device Drivers. The examples can be found on bren in /home/emb .

Install and update system

As root on your virtual server execute the following commands:

$ yum install gcc kernel-devel ncurses-devel
$ yum update
#
# If a new kernel came in restart the server
#
$ init 6
#
# Wait a few minutes and login once again
#

Create an ordinary user

Next create the ordinary users, if you do not have any yet, for the system by, as root, issuing:

$ adduser <username>
$ passwd <username>

As root edit the /etc/group file and add a new group at the end. Copy the line above (one of the ordinary useres) and change the group number to bee different from any other numbers assigned in the file. Add the usernames after the last : having the file looking something like this:

klausk:x:500:
moduledev:x:501:klausk

Exchange klausk with your name - separate usernames with comma (,).

Fianlly create a directory for the examples.

$ mkdir -p /home/emb2
$ chgrp moduledev /home/emb2
$ cd /home/emb2
$ chmod g+rwx /home/emb2
#

After editing in the groups file users, that may be included in a specific group must log-out and re-login in order to get the nes group membership working.

Unpack the examples

Logout as root and logout as the ordinary user. Re-login as the ordinary user (which now will become a member of the moduledev group).

Retrieve the examples from bren.

$ cd /home/emb2
$ scp <yourBrenUsername>@bren.hih.au.dk:/home/emb/examples.tar.gz .

Now unpack the example sources:

$ tar zxvf examples.tar.gz
$ chgrp -R moduledev examples
$ cd examples

A helper script

Create a helper script as described below.

Helper script called settings.sh - to be run every time you enter this directory. It sets up the environment. To be placed in the examples directory.

#!/bin/bash
#
# Preparation for the compilation of the scull example code
#
KERNELDIR=/usr/src/linux-headers-`uname -r`/
export KERNELDIR

After entering the example directory execute this

$ . settings.sh
#
# To make the scull module perform:
#
$ cd scull
$ make modules

NOTE: Remember to run the . settings.sh everytime you enter the examples directory!

Adjust the sudoers file

Remember that only root can insmod a module in the kernel.

If you add a line like this into /etc/sudoers using visudo (only accessible as root)

<klausk>  ALL=(ALL)       NOPASSWD: ALL

Substitute <klausk> with your username.

NOTE: You have to be careful with your password strength for the ordinary user logins otherwise you may risk somebody else compromises your server.

Load a kernel module

You can now issue this to insert the module in the kernel:

$ sudo chmod +x scull_load
$ sudo ./scull_load
#
# check if the module is loaded in the kernel
#
$ sudo /sbin/lsmod

Fix the code to fit the current kernel

In the program misc-progs/setlevel.c you have to change a line of code:

At line 30 insert this code and put in comments on the line defining _syscall3

#include <sys/klog.h>
//_syscall3(int, syslog, int, type, char *, bufp, int, len);

In the driver sbull/sbull.c comment the code in function sbull_full_request out - just leave the function empty. It's a block device, that we will not cover in the course.

In the driver usb/usb-skeleton.c at line 224 comment out the .mode field - it is depricated.

In the same file at line 328 comment out the .owner field.

In file tty/tiny_tty.c at line 79 comment out the

                if (tty->flip.count >= TTY_FLIPBUF_SIZE)
                        tty_flip_buffer_push(tty);

and inset these two lines instead:

               if (tty_insert_flip_string(tty, data, data_size))
                        tty_flip_buffer_push(tty);

Put in comments on line 534 and 538 on lines looking like this:

 //tiny_tty_driver->devfs_name = "tts/ttty%d"
and
//tiny_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,


Finally in file lddbus/lddbus.c at line 73 comment out the line (.hotplug...)

The code should now be ready for making against the current kernel.

NOTE: If you are going to use the above patched code please check that the corrections are not conflicting with any required functionality.