Kernel compile notes

NOTE: The upstream (git) kernel *sometimes* does not compile with gcc-4.x. Install gcc-3.4 and change /usr/bin/gcc (which links to gcc-4.x) to point to gcc-3.4 instead.

Compile using make-kpkg on Debian. Kernel compiles as a .deb (initrd image + modules) and can be directly installed using dpkg -i. (Steps, UbuntuWiki). The problem with make-kpkg is that every time you make a change to the kernel code it recompiles everything from scratch, which is a pain in the neck! To avoid that, it is better to compile the kernel from scratch and not use jazzy distro scripts


The old fashioned way

1. Go to the kernel directory (say, kernel_dir) and edit the Makefile to specify the EXTRAVERSION. (This is what --append-to-version does in make-kpkg). It is good to do this as it helps to identify your kernel from the rest.

2. Configure the kernel
make menuconfig

If you do not know where to start for, use
make defconfig

This creates a default configuration for i386. You can then confirm the configuration with
make menuconfig

[OLD]It might be useful to run (not required)
make oldconfig

3. Compile the kernel and modules
make bzImage
make modules


In the 2.6+ kernel, you can just do the following instead

make -j N, where N is the number of parallel compilation tasks you want to kick in for a faster compile. (N = 2,4,...)

TIP: If you have already compiled and installed the kernel once and later made changes ONLY to the kernel, doing a make would build the modules too. To avoid that do

make bzImage

TIP: To reduce the compilation noise you can forward the output of make to /dev/null.

4. Install the modules in /lib/modules

sudo make modules_install

Confirm that /lib/modules/ has the modules corresponding to your kernel version.

5. Copy the kernel image binaries to /boot

Lets say that the kernel version (along with the EXTRAVERSION) is 2.6.24-custom.010209

cd kernel_dir
sudo mv arch/i386/boot/bzImage /boot/vmlinuz-2.6.24-custom.010209
sudo mv .config /boot/config-2.6.24-custom.010209
sudo mv System.map /boot/System.map-2.6.24-custom.010209


6. Create the RAM Disk image (initrd).

In Ubuntu the mkinitrd is no longer supported. Instead mkinitramfs is used.

update-initramfs -c -k 2.6.24-custom.010209 # your kernel version

Note that update-initramfs looks at /lib/module for the kernel version. Make sure that you have installed the modules in step 4.

The initrd file initrd-2.6.24-custom.010209 would be created in /boot

NOTE: Usually the following are selected by default at the time of kernel configuration. If these are not set, you might get a kernel panic while booting.
CONFIG_BLK_DEV_RAM=y
CONFIG_BLK_DEV_INITRD=y
CONFIG_CRAMFS=y


7. Last step is to update grub

sudo update-grub

Check in /boot/grub/menu.lst and confirm.

8. Reboot, and select your kernel

Getting the front-mic to work

Weird problem. By default the front-mic does not work on my laptop. In order to get it to work, there is some magic that needs to be done with the ALSA Mixer.

1. Open Alsa mixer, select Input source as "Front Mic". Close Alsa mixer
2. Now open Alsa mixer again and now select the Input source as "Mic". Close Alsa mixer.
3. The front mic starts working again! (Need to find the real fucking reason behind this)

(confirmed: link)

Linux system calls

1. How to implement my own system call in kernel 2.6 -> here
2. IBM developerWorks "Kernel command using Linux system calls" -> here
3. Linux system call table -> here
4. How system call works on Linux/i386 -> here
5. syscalls() man page -> here

Pandora as an application on Gnome using firefox

Firefox supports multiple profiles. You can create a dedicated profile for Pandora.

1. Close firefox and start it from the command prompt using

firefox -no-remote -ProfileManager

2. Create a new profile and call it Pandora

3. Customize the profile. Install the add-on "Compact Menu 2" and remove all the toolbars.

4. Make pandora.com the home page.

5. ProfileManager makes the last profile as default. Start profile manager again and select the "default" profile so that it opens your actual profile.

6. You can create a GNOME launcher now and give the following command

firefox -no-remote -P Pandora

7. Go to menu edit and add this under (say) "Sound & Video"

8. Search google images and get a PNG or SVG image for Pandora.

9. If you are using Avante-window-manager (AWM) you can add this to your launcher.

10. Enjoy the music!

Convert Firefox into Chrome in 4 simple steps

1. Install the following Add-ons : Compact Menu 2, Download Satusbar, Stylish
2. Install the following theme : Chromifox
3. Install the following styles using Stylish: Back/Forward drop-down, Tab bar on top
4. Right click on the menubar, hide it and click on customize. Add compact menu on the right side along with the the new tab button.

Restart Firefox. Bingo!

Getting Bamboo to work on Ubuntu

Wacom is very well supported on Linux but to make it to work with all the features you need to build the driver.

Ubuntu's Wacom wiki page has all the details .. here

Just to reiterate the steps .. here

Jarnal is a good application that you can use for taking notes. Apart from that inkscape and GIMP can be used for drawing\sketching.

Using Latex for formulas

NOTE: This is only if you are using it within OpenOffice

All of this translates to the formulas below

P_total = AC{V^2}f + {%tau}AVI_shortf + VI_leak newline
P_o = C_o {{V_o}^2}f newline
P_n = 0.5 P_o newline
I_leak = K_2 W(V over {T_ox})^2 func e^({{%alpha}T_ox over V}) newline
sum from 0 to 100 (a^2)(b^3)d newline
where, left lbrace {stack{a, contanst# b, variable # d, dynamic}} right rbrace


Using GNUPlot

GNU plot is one of the most useful applications on Linux for creating plots. GNU plot takes in data in the following format

10 324.12 12312 56
100 34231.3 2312123 67

There are three columns here, there can be any number, make sure the data is separated by a space. You can make your program write the data directly in a file, call it whatever you want to, e.g. "simout.dat"

# Fire gnuplot
gnuplot>

# draw using the first two columns from the data file
gnuplot> plot "simout.dat" using 1:2

# draw using the first and second col AND first and third col.
gnuplot> plot "simout.dat" using 1:2, "simout.dat" using 1:3

#draw using lines
gnuplot> plot "simout.dat" using 1:2 with lines, "simout.dat" using 1:3 with lines

# draw using points on the lines
gnuplot> plot "simout.dat" using 1:2 with linespoint, "simout.dat" using 1:3 with linespoint

# draw with a legend for the lines
gnuplot> plot "simout.dat" using 1:2 with linespoint ti "Marks in CA", "simout.dat" using 1:3 with linespoint ti "Marks in WUC"

# set title
set title "The title of the plot"

# set X label
set xlabel "Blocks (N)"

# Set Y label
set ylabel "Performance [MFlops]"

# Set x and y Range
set xrange [100:10000]
set yrange [0:10e6]

# Set log scale on X
set logscale x

There are so many other things that you can do with GNUPlot, which I even don't know as I am still learning how to use it.

Excellent tutorial here (that is how I got started).

Using commands recursively with find

I copied my home folder to Windows for a backup and when I copied them back for some weird reason all the directories had executables privileges.

This is what I did to get rid of it recursively

$ find . -type d -exec chmod 755 {} \;

you can pass any command as an argument to -exec. Some useful things that you can do with find are here

using wget to download websites

There are many tools available that help you download the entire website for ofline browsing but nothing can beat wget. Although it has a command line interface but it works like a song.

$ wget --wait=5 --limit-rate=20K -r -p -U Mozilla --no-parent http://the.site.url

For more information do man wget

--wait If you try to download everything some websites might blacklist you. It is recommended that you specify a wait of 20 secs before any new fetch and --wait helps you do that.

--limit-rate You can limit the download speed by specifying an amount which can be expressed in bytes/sec or Kbytes/sec (with the K suffix)

-r Turns on recursive retrieval

-p Ensures that wget downloads all files that are required to display the HTML page correctly like pictures, PDF files etc

-U Some website do not allow downloading HTML if it is not a browser. Wget gives is -U option to fake the browser.

and most important
--no-parent You need to be sure that you are not downloading any other folder above the one which you want to.

Video Editing with ffMPEG

Using commmand line editing tool ffMPEG for editing videos.

Watch.

Finding SLOC using wc

$ find . -name "*.cpp" -o -name "*.c" -o -name "*.h" | xargs wc -l 

Packing structures \ enums

There are various ways in which this can be done.

1. using #pragma pack()
#pragma pack(2)
typedef struct
{
char c;
int i;
} DataType;
#pragma pack()

This would pack the structure on a 2 byte boundary. If a tight packing is required use #pragma pack(1) instead. Compile it normally using gcc.

2. Using -fpack-struct
Instead of using the #pragma, we can directly use the compiler flags instead.
$gcc -Wall -fpack-struct -fshort-enums test.c -o test
This would pack all the structs to the 1 byte boundary and consider shorts for enums instead of integers

2. Using __attribute__ ((__packed__))

typedef struct
{
char c;
int i;
} __attribute__ ((__packed__))DataType;
.
Compile the code normally.

We can also do it this way

typedef struct
{
char c __attribute__ ((__packed__));
int i1 __attribute__ ((__packed__));
int i2;
} DataType;

APR error

If you are using APR (Apache portable runtime) and on compilation of your code you get the following error

apr expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘apr_off_t’

Do this,

$ apr-config --cppflags --cflags

which returns the following on my system

-DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE -pipe -Wall -g -O2 -pthread

Use these while compiling your code.

gcc file.c -o file -I/usr/include/apr-1.0 -lapr-1 `apr-config --cppflags --cflags`

Using POSIX Message Queues

Code

/* Includes */
#include < pthread.h >
#include < stdio.h >
#include < stdlib.h >
#include < unistd.h >
#include < mqueue.h >
#include < sys/stat.h >
#include < errno.h >
#include < string.h >


/* Threads */
static void *MainThread (void *);
static void *AnotherThread (void *);

/* Defines */
#define MAIN_QNAME "/MainQueue"
pthread_mutex_t wait_mutex;

int mq_test(void)
{
pthread_t mainThread, anotherThread;


printf ("Creating threads .. \n");
pthread_create (&mainThread, NULL, &MainThread, NULL);

pthread_create (&anotherThread, NULL, &AnotherThread, NULL);

pthread_mutex_init (&wait_mutex, NULL);


pthread_join (mainThread, NULL);
pthread_join (anotherThread, NULL);


return 1;
}

#define BUFFER_SIZE 10000

/* Main thread .. Waiting for messages */
static void *MainThread (void *args)
{

mqd_t queue_handle;
char buffer[BUFFER_SIZE];
int bytes_read;

struct mq_attr msgq_attr;
unsigned int sender;

printf ("[MainThread] Inside main thread \n");


// Let the other thread wait till I am ready!
pthread_mutex_lock (&wait_mutex);

// Clear the buffer
memset (buffer, 0, BUFFER_SIZE);


// Detach the thread
pthread_detach (pthread_self());

// unlink the queue if it exisits - debug
mq_unlink (MAIN_QNAME);


printf ("[MainThread]Opening MQ \n");
queue_handle= mq_open(MAIN_QNAME, O_RDWR | O_CREAT, S_IRWXU | S_IRWXG, NULL);

if (queue_handle == -1)
{
perror ("[MainThread] Error Opening MQ: ");
return 0;
}

printf ("[MainThread] Waiting for messages ... \n");
pthread_mutex_unlock (&wait_mutex);


for (; ;)
{
bytes_read = mq_receive(queue_handle, buffer, BUFFER_SIZE, &sender);

if (bytes_read == -1)
{
perror("[MainThread] Failed to recieve:");
return 0;
}

else
{
printf ("[MainThread] Data: %s \n", buffer);
// Get the MQ attributes
mq_getattr(queue_handle, &msgq_attr);
printf("[MainThread] Queue \"%s\":\n"
"\t- stores at most %ld messages\n"
"\t- large at most %ld bytes each\n"
"\t- currently holds %ld messages\n",
MAIN_QNAME,
msgq_attr.mq_maxmsg,
msgq_attr.mq_msgsize,
msgq_attr.mq_curmsgs);

// Clear buffer and sleep to block for some time t and see
// if you get all the messages!

memset(buffer, 0, BUFFER_SIZE);
sleep(5);
}
}

mq_close (queue_handle);
}

#define MAX_SEND_BUFFER 70

static void *AnotherThread (void *args)
{

mqd_t queue_handle;
char buffer[MAX_SEND_BUFFER];
unsigned int msgprio = 1;

int count;

printf ("[AnotherThread] Inside Another thread \n");

pthread_mutex_lock (&wait_mutex);


queue_handle= mq_open(MAIN_QNAME, O_RDWR);
if (queue_handle == -1)
{

perror ("[AnotherThread] Error Opening MQ:");
return 0;
}

for (count = 0; count < 100; count++)
{

snprintf (buffer, MAX_SEND_BUFFER, "Message %d from Another thread", count);

if (0 != mq_send (queue_handle, buffer, strlen(buffer)+1, msgprio))
{

perror ("[AnotherThread] Sending:");
mq_close (queue_handle);
pthread_mutex_unlock (&wait_mutex);

return 0;
}
}

pthread_mutex_unlock (&wait_mutex);
mq_close (queue_handle);


return 0;
}

#ifdef _DEBUG_
int main (void)
{
return mq_test(void);
}

#endif

grep - argument list too long

If you are doing

$ grep "text" `find . -type f` this would result in a long argument list that grep can't handle and it would give an error saying

Argument list too long

Instead use

find . -type f | xargs egrep "text"

Using smbclient from command prompt

If you are using *ubuntu, change to the root bash shell

$sudo bash
Password: [Your kubuntu password]
$smbclient \\\\169.12.10.184\\SHARE_NAME -U DOMAIN\\USERNAME
Password: [Your windows password]
smb: \> help


You can use ? or help to get the commands which are similar to ftp like cd, lcd, mget, mput and so on

Virtualization to a next level

coLinux is a port of the Linux Kernel to Windows which executes as a single process. Using Xming server one can easily run X apps on windows.

IBM Developer works has an article on the same topic:
Virtualization with coLinux .. read

One of the best applications of coLinux is andLinux which allows you to run Ubuntu Linux seamlessly on Windows 2000/XP/Vista machines .. read.

LifeHacker has an article on andLinux .. read

RAID on Linux

Some information about RAID

RAID Wikipedia reference .. read
Linux SATA RAID FAQ .. read
The Software-RAID HowTo .. read
Linux Logical Volume Manager .. read

PHP

A good article about the good and bad things of PHP .. read

Apache Links

Some useful links on Apache and its internals

The Apache Modelling Project .. read
Building Apache the way you want it .. read
How to install Apache, MySQL and PHP on Linux .. read
Apache 2 Modules Tutorial .. read

Linux Clustering Links

Some useful links on Linux clustering

Linux Clustering Information Centre .. read
IBM Developer Works Clustering Links .. read
Open Source Cluster Application Resource (OSCAR) .. read
Cluster Monkey .. read
Linux Virtual Server (LVS) .. read
Linux Clusters Vs Grids .. read
UltraMonkey, Load Balancing and High Availability Solution .. read
Linux Clustering Cornucopia .. read
High Performance Linux Clustering Part 1 and Part 2

Signals

Linux Journal has couple of good articles on Signals on Linux.

Linux Signal Handling Model .. read
Linux Signals for the Application Programmer .. read

Memory Leak Detection

Couple of articles by Cal Erickson in Linux Journal on Memory Leak detection.

1. Memory Leak Detection in Embedded Systems .. read
[mtrace, memwatch, dmalloc]

2. Memory Leak Detection in C++ .. read
[dmalloc, ccmalloc, NJAMD(not just another malloc debugger), YAMD (Yet another malloc debugger), Valgrind, mpatrol, Insure++]

Another article on Using Valgrind.

Sudo and Upstart

Linux.com features two articles.

1. About Sudo
2. Upstart - Ubuntu's event based init daemon

Virtualization on Linux

Techtrob.com reviews four virtualization software choices on Linux

1. VMWare Server
2. Innotech's VirtualBox
3. Parallels Workstation
4. Open source x86 emulator - Qemu
5. XenServer - Express Edition (Now Citrix)

Read

gSOAP and examples

SOAP is a protocol for exchanging XML based messages between computers using HTTP\HTTPS. SOAP is part of the web services protocol. Alex Nghiem has written an excellent article on the web service stack that comprises of XML, SOAP, WSDL and UDDI. You can read the article here.

gSOAP is the open source implementation of SOAP and it is based on C/C++.

There are some excellent examples on how to use gSOAP, can be found here.

working with core dump

Core dumps are very important when debugging a problem. But sometime you would notice that even after the program crashes with a Segmentation Fault, the size of the core dump file is still zero or in some cases the core dump is not created.

The size of the core dump file is governed by "ulimit" in bash. To get more information about ulimit, do a info on bash and search for ulimit

$info bash

To get the status of the current limits set you can do

ulimit -a

Notice the core file size (-c). If it reads 0, it means that bash would not create the core dump file. You need to change the size.

ulimit -c 1024

You can even set it to unlimited

ulimit -c unlimited


Generating the core

/* coredump.c */
#include <stdio.h>

int main (void)
{
  int *point = NULL;
  *point = 0;

  return 0;
}


Compile the code

gcc -g coredump.c -o coredump

When you try to run, it would generate a segmentation fault

./coredump
Segmentation fault (core dumped)


Using the core
To use the core, start gdb and pass the core dump generated

gdb coredump -c core
GNU gdb 6.6-debian
Copyright (C) 2006 Free Software Foundation, Inc.

Reading symbols...
Core was generated by ./coredump
Program terminated with signal 11, Segmentation fault.
#0 0x08048381 in main () at coredump.c:6
(gdb)


You can now use the gdb commands to view the call stack, registers, memory etc.