Archive for the ‘Linux’ Category

kernel – hello world

Monday, March 1st, 2010

To compile up this module you will need to have the linux header files, which should be installed but if not you just need to either use apt-get, rpm, pacman etc to install the linux header files.

With kubuntu I use the aptitude command to install applications and updates etc, but to get the linux-headers I used

aptitude install linux-headers

that is normally linked to the latest version of the linux kernel that you are using.

The main part of the program is outputting a potential parameter being passed (passing parameters to a nvidia kernel module here) and also saying hello world, kinder two for the price of one as such tutorial.

As in c language there is the printf, but in the kernel there is a printk which is the same similar thing but more for outputting errors/messages to the user.

So for example

printk(KERN_INFO "hi there\n");

the KERN_INFO is a kernel information marco that sends the details to a information level of output, e.g. /var/log/messages or you could use dmesg

here is the code on how to pull in parameters from a kernel module being loaded, the parameter_int is my parameter to be checked against and the S_IRUSR is the access rights (IR = read permission, IW = write permission, USR = user, GRP = group, OTH = others)

The module_param takes 3 parameters, the first is the variable to place the value into, second is the variable type and the third is the access rights, with the MODULE_PARM_DESC is the description of the parameter to pass, in this case it is just a integer value.

static int parameter_int = 0;
module_param(parameter_int, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
MODULE_PARM_DESC(parameter_int, "An integer value");

Within a normal c/c++ program there is a main function where the program is run from, but in the kernel space since the main program is already running you can define what functions to call when the module is inserted and also removed,

module_init(hello_init);
module_exit(hello_exit);

the module_init parameter is the function to call when the module is loaded and the module_exit parameter is the function to call when module is removed from the kernel space.

Here is the full code, if you save this as helloworld_parameter.c

/* hello world with passing some parameters in the kernel module */
 
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/stat.h>
 
MODULE_LICENSE("GPL");
MODULE_AUTHOR("genux");
 
/* need to setup a setup a static variable to hold the parameter value and
   set it to a default value is none is passed */
static int parameter_int = 0;
 
/* the linux/stat.h has the S_IRUSR definitions etc.. */
/* S_IRUSR = read permission, owner
   S_IWUSR = write permission, owner
   S_IRGRP = read permission, group
   S_IROTH = read permission, others
   */
 
module_param(parameter_int, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
MODULE_PARM_DESC(parameter_int, "An integer value");
 
static int __init hello_init(void)
{
	printk(KERN_INFO "Hello world\n\n");
	printk(KERN_INFO "my parameter int value is : %d\n", parameter_int);
	return 0;
}
 
static void __exit hello_exit(void)
{
	printk(KERN_INFO "Goodbye from hello world parameter\n");
}
 
module_init(hello_init);
module_exit(hello_exit);

since we are compiling a kernel module we need to link to the loaded kernel modules, here is a Makefile to compile up the program, so save this as “Makefile”

obj-m += helloworld_parameter.o
 
all:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
 
clean:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

the first is the obj-m += which means compile a object module and you could have more than one file to compile up so use the += to add more files to it, the -C means change directory for the build environment for the kernel space, the M is a parameter passed to the build environment to use this current directory for where the source files are, and the modules means to create kernel modules e.g. filename.ko.

once you have run the

make

there should be a file called helloworld_parameter.ko, to find out details about your new module you can use the modinfo

modinfo helloworld_parameter.ko
filename:       helloworld_parameter.ko
author:         genux
license:        GPL
srcversion:     A81F18D40DA3C5FAB1C71FF
depends:
vermagic:       2.6.31-17-generic SMP mod_unload modversions
parm:           parameter_int:An interger value (int)

and the parm: is the important part here, it is what the parameter is called to pass a value to for example to watch the module being inserted if you open up two consoles and on one put

tail -f /var/log/messages

in the second console do

insmod helloworld_parameter.ko
rmmod helloworld_parameter.ko
insmod helloworld_parameter.ko parameter_int=3
rmmod helloworld_parameter.ko

and the output should be in the first console

Hello world
 
my parameter int value is : 0
Goodbye from hello world parameter
 
Hello world
 
my parameter int value is : 3
Goodbye from hello world parameter

hope that helps with kernel modules, I am planning on doing a kernel module for a custom built USB device.

kernel – passing module parameters

Saturday, February 27th, 2010

With normal programs that allow you pass parameters on there command line, for example

printoutmyname genux

where the program is called printoutmyname and the first parameter is genux (it is normal that the first parameter [0] is actually the program name and the second parameter [1] is the parameter that is passed)

Well in Linux kernel (where here is a example on how to compile the kernel for a ubuntu based setup) you can pass parameters to the modules that are getting loaded. One module would be your graphics card, in my case a nvidia graphics card.

To find out what parameters can be passed to the graphics card you will have to find the kernel module file (filename.ko, where the ko is the kernel object file), so to search for the nvidia.ko in my case I did

locate nvidia.ko

and then changed to that directory and did a module information on it

cd /lib/modules/2.6.31-17-generic/updates/dkms/
ls
nvidia.ko  vboxdrv.ko  vboxnetadp.ko  vboxnetflt.ko

and doing a module information on it you call the modinfo on the kernel object file as

modinfo nvidia.ko
</pre
 
my output was
 
<pre lang="bash">
filename:       nvidia.ko
license:        NVIDIA
alias:          char-major-195-*
alias:          pci:v000010DEd*sv*sd*bc03sc02i00*
alias:          pci:v000010DEd*sv*sd*bc03sc00i00*
depends:
vermagic:       2.6.31-17-generic SMP mod_unload modversions
parm:           NVreg_EnableVia4x:int
parm:           NVreg_EnableALiAGP:int
parm:           NVreg_ReqAGPRate:int
parm:           NVreg_EnableAGPSBA:int
parm:           NVreg_EnableAGPFW:int
parm:           NVreg_Mobile:int
parm:           NVreg_ResmanDebugLevel:int
parm:           NVreg_RmLogonRC:int
parm:           NVreg_ModifyDeviceFiles:int
parm:           NVreg_DeviceFileUID:int
parm:           NVreg_DeviceFileGID:int
parm:           NVreg_DeviceFileMode:int
parm:           NVreg_RemapLimit:int
parm:           NVreg_UpdateMemoryTypes:int
parm:           NVreg_UseVBios:int
parm:           NVreg_RMEdgeIntrCheck:int
parm:           NVreg_UsePageAttributeTable:int
parm:           NVreg_EnableMSI:int
parm:           NVreg_MapRegistersEarly:int
parm:           NVreg_RmNvclkIdleGraphics:int
parm:           NVreg_RegistryDwords:charp
parm:           NVreg_NvAGP:int

where the parm: is a parameter to pass to the module on load (insmod, or loaded via the kernel at boot time which you can force to load via the /etc/modules file and the parameters can be placed in the /etc/modprobe.d directory).

for example to load the nvidia module with a parameter NVreg_NvAGP you would do something like

insmod nvidia.ko NVreg_NvAGP=1

and the passing value is 1 to the NVreg_NvAGP parameter

root password reset

Wednesday, February 17th, 2010

Within the standard ubuntu or the derviertives (kubuntu/edubuntu etc) the root password is not set and the way to gain access to the root commands is via the command line

sudo command here

I personally prefer to have root password set because then I can use the root admin rights on the desktop without having to constantly type in sudo all of the time, so to set the root password to something that I will use you can use this

sudo set password root

this will set the password to something that you can remember and thus any time that you want to have a console open as root just type in

su

and then the password and you are in the root console :)

changing the hostname

Tuesday, February 9th, 2010

To change the hostname of a linux distribution you can either change the hostname on-the-fly, but it does not restore it after a reboot. To change the hostname on-the-fly you can do

echo "your new hostname" > /proc/sys/kernel/hostname

on a ubuntu/debian system to alter the hostname after a reboot you can alter the

/etc/hostname

file, if you cannot find the hostname file there, you can search for the file within the /etc directory by

grep -R "your hostname" /etc

it will display a few files, but you should find out where your hostname is set, normally either within /etc/sysconfig or /etc/network directories.

ubuntu – kubuntu – version

Tuesday, February 9th, 2010

Sometimes when you just cannot remember which version of ubuntu/kubuntu etc that you have on your server/desktop I use this command which displays the version that you have installed and running at present.

lsb_release -c

and the output at present is

Codename:       karmic

Linux file structure – comparsion with Windows

Sunday, February 7th, 2010

Here is the Linux file structure from the root, which some explanations. The main explanation for the /bin, /boot /lib are the basic files that are required on boot up that the kernel needs to “run” as such. The files within the /usr are the files that user programs use e.g. games.

Anyway here is a list of directories on my linux setup.

bin binary files for boot up e.g. mount e.g.
boot boot files – kernel images etc.
dev devices on the computer.
etc configuration files for the programs
home home users, e.g. your name /home/ian
lib libraries
lib32 libraries for the 32bit programs
lib64 /lib (link to the libraries since I am using the 64 linux version)
lost+found
media media that is going to be mounted (cd-rom’s)
mnt media that is going to be mounted (Hard drives etc)
opt opitional programs e.g. things like google chrome, they are normally place in here is not distro specific.
proc processes that are happening on the computer, all process you can “talk” to
root root home files.
sbin sbin, booted up at initial stage of the boot process, things like modprobe for setting up systems items.
sys system image of devices attached and also file systems that are loadable.
tmp tempoary files.
usr user files, e.g. games, libraries, binary files
bin games include lib lib32 lib64 local sbin share src
it has its own includes, libraries, sbin and bin directories for all of the files within that user directory.
var variable files, e.g. logs, apache www hosting files.

The Windows equivalent would be that most of the / (root) directory is within the c:/windows directory, apart from the /home which is the c:/Users or c:/Documents depending on your Windows version.

bin /Windows /Windows/System32 /Windows/System
boot boot.ini file that points to what to do.
dev Does not appear to have something similar on the file system
etc /Program Data (depending on Windows versions)
home /Users
lib /Windows /Windows/System32 /Windows/System /Windows/.Net (for .Net stuff) etc.
lib32
media Does not appear to have a link to the different mount points but display them on the windows explorer
mnt Does not appear to have a link to the different mount points but display them on the windows explorer
opt /Program files/
proc Does not appear to have a list of running process on the file system, but you can view them with pslist pskill
root /Documents/Admin user account
sbin /Windows
sys Does not appear to have a list of devices attached
tmp /tmp
usr /Program files/
var /Program files/ or where ever you want them.

That kinder helps me to understand that there is more details on the command line, directory structure to actual processes and devices attached than Windows, well of course there is the regviewer that can display options like the /etc in the linux but nothing as structured, things just across like a mess (to me anyway).

WebGL – open GL on the web

Thursday, January 28th, 2010

Flash, Silver light and now OpenGL on the web, http://www.khronos.org/webgl/ but this one is open standards and based on OpenGL engine. More information can be found the link above.. cannot wait for it to come to light and see what is the major player in the market place.

Blank screen in grub

Monday, January 25th, 2010

After doing a update to the nvidia kernel for the 4GB problem, the grub screen was using a graphical interface which was not allowing me to view the options on the grub screen. It was black / blank, so to alter it so that it uses a terminal (console) version instead of a graphical interface you have to alter the /etc/default/grub file

vim /etc/default/grub

And just uncomment the GRUB_TERMINAL option, or alter it to say console as below.

# Uncomment to disable graphical terminal (grub-pc only)
GRUB_TERMINAL=console

You will need to update the grub /boot/grub/grub.cfg file which is done via the

update-grub

command, you will have to be root to do this, or sudo if you do not have a root access as such.

now the screen will be viewable in a console mode which is all that I needed anyway, I was not really that bothered about having a graphical image in the backdrop just to select a OS or kernel version.

Mounting – Linux

Monday, January 25th, 2010

Sometimes a USB external drive may not mount in Linux, could be because the the harddrive is getting to that point of I am going to give up soon so you better copy off all of the data!!!!..

Anyway, here are some basic guides to mount a external harddrive if it has not been auto mounted by udev

If you do a dmesg on the command line

dmesg 
 
[ 4454.972580] scsi 6:0:0:0: Direct-Access     Maxtor 4 D040H2           0811 PQ: 0 ANSI: 0                                                            
[ 4454.973682] sd 6:0:0:0: Attached scsi generic sg3 type 0                                                                                            
[ 4454.980992] sd 6:0:0:0: [sdc] 80043264 512-byte logical blocks: (40.9 GB/38.1 GiB)                                                                  
[ 4454.983014] sd 6:0:0:0: [sdc] Test WP failed, assume Write Enabled                                                                                  
[ 4454.983021] sd 6:0:0:0: [sdc] Assuming drive cache: write through                                                                                   
[ 4454.985201] sd 6:0:0:0: [sdc] Test WP failed, assume Write Enabled                                                                                  
[ 4454.985205] sd 6:0:0:0: [sdc] Assuming drive cache: write through                                                                                   
[ 4454.985209]  sdc: sdc1                                                                                                                              
[ 4455.022214] sd 6:0:0:0: [sdc] Test WP failed, assume Write Enabled                                                                                  
[ 4455.022218] sd 6:0:0:0: [sdc] Assuming drive cache: write through                                                                                   
[ 4455.022224] sd 6:0:0:0: [sdc] Attached SCSI disk

and get something like the above, the most interesting part is the sdc:sdc1, because this tells you where the device has been placed onto the /dev directory structure. You could also use the

lsusb

to find out where it was placed in the USB structure as well and try to mount from that device entry, but I just use the /dev/sdc ones normally, both will work through.

To mount a external drive, you need to find somewhere to place it, e.g. /mnt and then create a directory (that is not there already)

cd /mnt
mkdir USBEXT

and then mount the USB external drive, most are the type of V/FAT types so you just specify that in the type (-t) parameter

mount -t vfat /dev/sdc1 /mnt/USBEXT

the /dev/sdc1 is where it was placed and the /mnt/USBEXT was where I want it mounted and the -t was VFAT.

My desktop – what more can I say!!

Wednesday, January 13th, 2010

Here is my linux desktop!!.. what more can I say.. apart from I do not use Windows at all and more than happy with that decision about 10 years ago..