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

From Klaus' wiki
Jump to: navigation, search
(A helper script)
 
(62 intermediate revisions by the same user not shown)
Line 1: Line 1:
As root on your virtual server execute the following commands:
+
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:
  
 
<source lang="bash">
 
<source lang="bash">
Line 13: Line 17:
 
</source>
 
</source>
  
Now we need a local copy of the kernel sources. Follow the instructions on [http://wiki.centos.org/HowTos/I_need_the_Kernel_Source#head-a8dae925eec15786df9f6f8c918eff16bf67be0d Centos Wiki]
+
==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">
 +
$ adduser <username>
 +
$ passwd <username>
 +
</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:
  
Next create the ordinary users for the system with
 
<source lang="bash"> $ adduser <username></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 at least one higher. 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 23: Line 32:
 
</source>
 
</source>
  
Next follow the instructions at [http://wiki.centos.org/HowTos/BuildingKernelModules#head-d2e4c05886f94c701e4ae74387d41d8c40c25d01 Centos wiki on building kernel modules] '''REMEMBER''' do this as an ordinary user as recommended.
+
Exchange klausk with your name - separate usernames with comma (,).
 +
 
 +
Fianlly create a directory for the examples.
 +
 
 +
<source lang="C">
 +
$ mkdir -p /home/emb2
 +
$ chgrp moduledev /home/emb2
 +
$ 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">
 
<source lang="bash">
$ cd ~/rpmbuild/BUILD/kernel-2.6.18/linux-2.6.18.i686
+
$ cd /home/emb2
$ make oldconfig
+
$ scp <yourBrenUsername>@bren.hih.au.dk:/home/emb/examples.tar.gz .
$ make menuconfig
+
$ make prepare
+
$ make modules_prepare
+
 
</source>
 
</source>
  
As '''root''' issue this command:
+
Now unpack the example sources:
 +
 
 
<source lang="bash">
 
<source lang="bash">
$ depmod -a
+
$ tar zxvf examples.tar.gz
 +
$ chgrp -R moduledev examples
 +
$ cd examples
 
</source>
 
</source>
  
As '''normal user''' issue this:
+
==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.
 +
 
 
<source lang="bash">
 
<source lang="bash">
$ cd /home/eml1/examples
+
#!/bin/bash
$ make -C ~/rmpbuild/BUILD/kernel-2.6.18/linux-2.6.18.i686
+
$ make
+
 
#
 
#
# Takes a while to compile the kernel
+
# Preparation for the compilation of the scull example code
 
#
 
#
 +
KERNELDIR=/usr/src/linux-headers-`uname -r`/
 +
export KERNELDIR
 
</source>
 
</source>
 +
 +
After entering the example directory execute this
  
 
<source lang="bash">
 
<source lang="bash">
#
 
# Provided that the examples are placed in /home/eml1 issue these commands:
 
#
 
$ cd /home/eml1
 
$ tar zxvf examples.tar.gz
 
$ chown -R root:root examples
 
$ cd examples
 
#
 
# Create a link to the current kernel - remember to change this, when kernel changes
 
#
 
# The version number below may vary
 
#
 
$ ln -s /usr/src/kernels/2.6.18-194.11.4.el5-i686/ current-kernel
 
#
 
# Prepare the helper script as described below
 
#
 
# When entering the example directory execute this
 
#
 
 
$ . settings.sh
 
$ . settings.sh
 
#
 
#
Line 72: Line 88:
 
#
 
#
 
$ cd scull
 
$ cd scull
$ make
+
$ make modules
 
</source>
 
</source>
  
Helper script called settings.sh - to be run every time you enter this directory. It sets up the environment.
+
'''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''')
 
<source lang="bash">
 
<source lang="bash">
 +
<klausk>  ALL=(ALL)      NOPASSWD: ALL
 +
</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:
 +
 +
<source lang="bash">
 +
$ sudo chmod +x scull_load
 +
$ sudo ./scull_load
 
#
 
#
#!/bin/bash
+
# check if the module is loaded in the kernel
 
#
 
#
# Preparation for the compilation of the scull example code
+
$ sudo /sbin/lsmod
#
+
KERNELDIR=`pwd`/current-kernel
+
export KERNELDIR
+
 
</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.