*

Welcome, Guest. Please login or register.

Get your own OLPC - Buy an XO laptop on eBay!
Pages: [1]
Print
Author Topic: Inserting a kernel module - the "easy way" HowTo ...  (Read 7745 times)

Inserting a kernel module - the "easy way" HowTo ...

mavrothal
Administrator
OLPC News Forum Expert
*****
Posts: 1289


October 11, 2009, 07:03:52 AM

Here is a not so complicated Shocked/risky Roll Eyes way to insert a kernel module in your running kernel and use it when you need it. It is a 12-step program... Grin
You do not need to be a linux guru but some linux knowledge and navigation through the terminal is needed. Knowledge of a simple linux text editor like "nano" is also handy. If you have none of that have a linux book handy or proficiency in searching linux forums (not always easy if you do not know what exactly to search for). In any case take your time!  

The advantage of this approach  is you can not mess up your system easily (worse case scenario your module will not work). The disadvantage that you do not really have a rebuild kernel running on your XO at the end of the process. You can do it though with some additional steps. Another point is that if this module is required for early startup function will not work since it will load after the start up is finished.
The whole process can be done on the XO-1 with an additional 2GB (or larger) USB stick for the files  or much faster, in another linux machine (with the exception of step 3, 11 and 12). Your USB stick must be ext2/3 formated NOT vfat.

You will need to first install the gcc, make, ncurses, ncurses-devel, glibc, glibc-devel, glibc-headers packages if not installeda already.
Depending on your installation additional packages might be needed. If this is the case during installation or the "make" commands (steps 8 and 9) the program will complain and tell you what is missing. Just install that too.  
Finding Fedora 9 packages is a problem. yum may nor find them sine F9 is an EOL release, so doing everything on the XO-1 under os767/802 can be tricky. However, if yum install filename does not find any file to download or complains that the version is inappropriate for your OS, you can look for the Fedora 9 rpm packages here and install them manually with the rpm -ivvh filename command. Make sure you get the dependencies each one may require.
Alternatively do steps 4-10 it in another fully supported linux machine. If you do, still do it on a USB stick so you can easier transfer between the two machines.

Conventions:
Code fields you just copy/paste to the terminal as is, except the second one in step 12 where you must define the exact path and module name
Monospace words are commands, filenames or paths but the exact form depend on which one you are actually using. Is like code fields but you can not copy/past the commands exactly as they are.
Quoted words are typed without the quotes. Indicate  file/folder names or commands. A bit like monospace I'm afraid... Embarrassed

1) So now on the XO-1 terminal type
Code:
uname -r
 The output is the kernel you are running. Keep this name is important. From now on we 'll call it "kernel_ver"

2) If you are running os767/802 on your XO-1, go to http://dev.laptop.org/~dilinger/testing/. If you run a newer build or hardware, go to http://xs-dev.laptop.org/~dsd/repos/ and look for appropriate subdirectory for your XO-  and Fedora-version.  Download the "kernel-headers.kernel_ver.rpm" and "kernel-devel.kernel_ver.rpm" (os767/802) plus "kernel-firmware.kernel_ver.rpm" ( for F11+). Do not get the "qemu" versions

Note: Downloading on the XO in a place that you can find the file can be tricky. If you have Firefox, Opera properly configured under os767/802 or the new F11-XO1 builds, is not  a problem. A general approach though is this:  Open the pages with Browse.activity, right-click on the file you want to download and copy the link, then open the terminal, type wget <space> and paste the link. The file will be downloaded in the directory that you currently are.  

3) Type su to become the "master of the machine" (cd to the download directory if you did not use wget to download the files) and install them by rpm -ivvh filename

4) In another machine or the XO download the "kernel-kernel_ver.src.rpm" file. is about 50MB. Careful to get the src file. Also make sure that you do not get the "qemu" versions.

5) Expand it as a normal user by rpm -ivvh filename
If you do this in the XO-1 will generate the 50BM rpmbuild folder in your home directory. Move the "rpmbuild" directory in the USB stick/SD card. Use the "mv" command mv path/of/original/rpmbuild /media/media_name

6) cd to the "rpmbuild/SOURCES/" directory and type
Code:
tar jxvf olpc-2.6.tar.bz2
this will generate the ~300MB "linux-2.6.XX" directory where your expanded kernel sources reside.

7)
Code:
cd linux-2.6.*
make clean distclean
At this point you need the config-kernel_ver file from the /boot directory of your XO-1 to place it in the linux-2.6.XX folder as .config (there is a dot before config). At the XO-1as "su" type cp /boot/config-kernel_ver .config (see the dot?). In another machine have to copy the file to a stick and transfer it to the other machine.
Alternatively you can just copy the "somename.config" file from the, just above,    "rpmbuild/SOURCES" directory or the one included with the kernel source by
Code:
cp ../kernel*custom.config .config # or
make olpc_defconfig # for os767/802. Or
make olpc_xo1_defconfig # for F11+ builds of XO-1. Or
make olpc_xo1.5_defconfig # for F11+ builds of XO-1.5
However I prefer to use the config file of whatever is running at the moment on the XO.

8 ) while in the "linux-2.6.XX" directory as a normal user type in order
Code:
make menuconfig
you'll get a blue screen that is the kernel configuration. Activate the module(s) you want. Pressing </> (slash) will give you a search field where you can type the name. It will tell you in which section the module is and what  it is doing. Activate the module by typing <M> (for modular use) Exit and save (will prompt for saving at exit)

9) Make sure that you have at least 1GB of space! Type "df" to see what is left on your USB/SDcard/disk where the "rpmbuild" folder resides
If ok type
Code:
make
and walk away
An overnight is a good timeframe for the XO-1 (2-3 hours) but on a fast machine will take ~30min. Inactivate any power management so your XO will not "sleep" in the middle of the process.
At the end you have a new kernel and modules.

Note: you can only build your module instead of the whole kernel and all the modules, by typing make modules instead of make in the command above, but you never know what you may want to do afterwards if you have everything Grin

10) Now find your newly build module. Lets say you build a squasfs module, it will be in the "fs/squahfs" sub-directory in the "linux-2.6.XX" folder.
Check that this module does not currently run on your XO. Type lsmod Sould not be there.  While still working from the "linux-2.6.XX" directory on the XO as an "su" now type insmod path/module name in our example
Code:
su
insmod fs/squashfs/squashfs.ko
Typing now lsmod again you should see your module in the list.
At this point check if your module is functional and supports what you need to do. Keep in mind that it may need additional software or drivers to work. squashfs-tools in our example or drivers for your bluetooth adapter etc.

11) The module at this point in not inserted permanently in the kernel. After you reboot will not be there any more. What you can do is to insert it every-time you start up. If you are more sophisticated you can write a script to load only when you need it but if you are, you should not be reading this... Grin.
So to make it permanent you first need to copy your module from the stick (or the other computer) in the XO NAND. I like to keep it in its place eg in
/lib/modules/kernel_ver/kernel/section/module/module.ko
In our example /lib/modules/kernel_ver/kernel/fs/squashfs/squashfs.ko
 I actually copy the whole folder that has the kernel module in our case the "squashfs" folder (just in case...). You need to be a "su" to write in this directory. You can have it anywhere but it will be easier to delete it by mistake if is not in a protected directory.

12) And the final step is to load it every time you start up your XO. As a "su" type
Code:
nano /etc/rc.local
In the file that opens immediately under the comments (the lines that start with "#") add
Code:
/sbin/insmod /full/path/to/the/module/module.ko &
Exit and save. Make sure that you have the path of the module correct. Usually I "cd" to where my module.ko is, then I "pwd" and copy/paste that to the command above.

You are done!
Every time you boot your module will be there to use and your original kernel, initrd etc untouched.

If this does not work all you lost is your time. Your XO should be unaffected. If it does you can now have that bluetooth installed Grin.

PS: Knowledgeable people please correct any mistakes.
PPS: As always in linux there are "better" ways to do things. This is just one way.
PPPS: The kernel and the modules that you made can be used as your XO kernel. However additional steps are needed to include system map, initrd, vmlinuz and config files in the boot directory. There are plenty of post in the web about these... Cool

PPPPS Grin If you find that everything works as described (or after corrections), pls let me know so this can be posted in the OLPC Wiki

Edit: Install the kernel and initrd
OK. here is a very simple way to install and actually boot your newly build kernel (tested ONLY with the F11-XO1 builds and kernel 2.6.31).
After you finish the 9th step above as "su" type
Code:
make modules_install
make install
the fist command will complete smoothly and the second will complain about Lilo. It's OK.
Now in you /boot folder of your XO you should find a new "vmlinuz" (not a symlink) "System.map-2.6.xxxxx" and "config-2.6.xxxxx" files and you'll see that your old vmlinuz symbolic link has been moved to "vmlinuz.old".

Warning! Do not reboot your XO yet, will hang. If this happens make sure that you can boot from an SDcard/USB and access your internal NAND from there (like teapot's Ubuntu) so you can correct the vmlinuz back to the original symlink and recover your XO-1, or be prepared to reflash your XO and erase everything from the NAND....

Now you have to "fix" your initrd.img file, so while  "su" type the following
Code:
cd /boot
mkdir initramfs
cd initramfs
gunzip -c ../initrd.img | cpio -i
cd lib/modules
ls # should see only one folder named 2.6.31_xo1-xxxxxxx
mv 2.6.31_xo1-xxxxxxx 2.6.xxxxx # DO NOT copy/paste this. See note below about this
cd /boot/initramfs
find . -print | cpio -H newc -o | gzip -9 >../initrd-modified.img
cd ..
ln -sf initrd-modified.img initrd.img
rm -rf initramfs
What you are actually doing is to "fool" initrd to think that is build from your new kernel instead of the original one. To the extend that your new kernel is just adding modules to the original and does not modify things included in the initrd.img, is OK. However, if your new modules must be in the initrd for early startup functions, or you have modified any of the initrd included modules, is not going to work.

Note: Be very careful renaming (the mv sep above)  the 2.6.31_xo1-xxxxxx folder  with the correct name that is showing in your /lib/modules folder. Should also be the same with the name of the new config- and System.map- files that were generated by the "make install" command in you /boot folder.

Edit 2 You can also "fool" your machine to use the new kernel modules instead of the old ones so you do not need to have your extra modules permanently inserted but only when needed.
After step 9 above type
Code:
su
make modules_install # will take some time
cd /lib/modules
ls
You should see a new folder with the new kernel modules. Note this name. Now type
Code:
mv `uname -r` `uname -r`.ORIGINAL
ln -sf <the name of your new kernel modules> `uname -r`
modprobe
ls -l
You should now see that there is an new folder ending in .ORIGINAL and that a link with your old kernel name is pointing to your new kernel folder.
You are done. Now the new modules will be loaded as needed.
Note If the `uname -r` trick does not work for some reason the idea is to rename your old modules and make a symbolic link to the new modules folder with the old modules name. If thinks go bad you can always delete (rm) the symbolic link and rename (mv) your .ORIGINAL folder back to its name (without the ".ORIGINAL" part)

« Last Edit: November 06, 2011, 11:50:14 PM by mavrothal » Logged

XO-1: Is never going to run Flash, but is certainly flashy!
(If you want Flash, get an XO-1.5 running OLPC 11.2.0 or XOpup Grin )

#1 Re: Inserting a kernel module - the "easy way" HowTo ...

anthill
Contributor
*
Posts: 55


November 23, 2009, 10:15:24 AM

Thanks for the how-to post, mavrothal.  I'm trying your 12-step program to try and get an sisusbvga kernel module for a StarTech USB2VGA2 display adapter.  I'm using Teapot's Ubuntu 8.10 so this may go a bit off track.

Edit-heavy comments as I go:
1) Are steps 1-3 really needed for machines running build 8.2.0?  I'm not sure I understand what they do and Teapot's ubuntu uses that kernel version.
2) Is there a typo in your "make menuconfig"?
3) make menuconfig fails if your USB stick filesystem doesn't support symlinks
4) A "better way" for step 10/11 is described on the wiki: put the newly built modules (sisusbvga.ko in my case) into the XO's /lib/modules/drivers/...etc... directory, then run depmod to make sure that modprobe knows about your new driver.

my system:

Code:
olpc@OLPC:/$ uname -r ; X -version
2.6.25-20080925.1.olpc.f10b654367d7065

X.Org X Server 1.5.2
Release Date: 10 October 2008
X Protocol Version 11, Revision 0
Build Operating System: Linux 2.6.24-19-server i686 Ubuntu
Current Operating System: Linux OLPC 2.6.25-20080925.1.olpc.f10b654367d7065 #1 PREEMPT Thu Sep 25 18:07:48 EDT 2008 i586
Build Date: 09 March 2009  10:48:54AM
xorg-server 2:1.5.2-2ubuntu3.1 ([email protected])
« Last Edit: November 23, 2009, 11:15:02 AM by anthill » Logged

#2 Re: Inserting a kernel module - the "easy way" HowTo ...

mavrothal
Administrator
OLPC News Forum Expert
*****
Posts: 1289


November 23, 2009, 11:22:57 AM

Thanks for the how-to post, mavrothal.  I'm trying your 12-step program to try and get an sisusbvga kernel module for a StarTech USB2VGA2 display adapter.  I'm using Teapot's Ubuntu 8.10 so this may go a bit off track.

Edit-heavy comments as I go:
1) Are steps 1-3 really needed for machines running build 8.2.0?  I'm not sure I understand what they do and Teapot's ubuntu uses that kernel version.
Yes, teapots Ubuntu uses the 767 kernel and you can compile the new one in this machine with the sources from the laptop.org site
I think 2-3 is mostly for the newer (2.6.30-based) kernels and if you want to do the job on the XO, and since I'm trying different things I always want to check what kernel I'm running  at the time Grin, hence the 1. 

Quote
2) Is there a typo in your "make menuconfig"?
Yes. Corrected. Thanks

Quote
3) make menuconfig fails if your USB stick filesystem doesn't support symlinks
correct must be an ext2/3 formated USB stick not VFAT. corrected. Thx

Quote
4) A "better way" for step 10/11 is to put the newly built modules (like the sisusbvga.ko attached) into the XO's /lib/modules/drivers/...etc... directory, then run depmod to make sure that modprobe knows about your new driver.
There is always a better way in linux Grin

So did it work for you?
« Last Edit: November 23, 2009, 02:37:04 PM by mavrothal » Logged

XO-1: Is never going to run Flash, but is certainly flashy!
(If you want Flash, get an XO-1.5 running OLPC 11.2.0 or XOpup Grin )

#3 Re: Inserting a kernel module - the "easy way" HowTo ...

anthill
Contributor
*
Posts: 55


November 23, 2009, 11:43:18 AM

Yes, it worked for me!  Of course, now I have to get the X server set up, but that's no fault of your excellent instructions.  Smiley

I skipped steps 1-3, went straight to 4, compiled the modules on an Ubuntu desktop, returned the one I wanted to the XO on a USB stick, and set up modprobe.
Logged

#4 Re: Inserting a kernel module - the "easy way" HowTo ...

mavrothal
Administrator
OLPC News Forum Expert
*****
Posts: 1289


November 23, 2009, 02:38:52 PM

Of course, now I have to get the X server set up,
Let us know how did it go (with a howto Grin)
Logged

XO-1: Is never going to run Flash, but is certainly flashy!
(If you want Flash, get an XO-1.5 running OLPC 11.2.0 or XOpup Grin )

#5 Re: Inserting a kernel module - the "easy way" HowTo ...

anthill
Contributor
*
Posts: 55


November 23, 2009, 04:09:40 PM

Huge USB2VGA dongle HOWTO here. Now I know how Teapot feels.
Logged
Pages: [1]
Print
Jump to:  

Members
Total Members: 2405
Latest: sembik
Stats
Total Posts: 31943
Total Topics: 3843
Online Today: 19
Online Ever: 238
(April 18, 2011, 09:48:50 PM)
Users Online
Users: 0
Guests: 21
Total: 21