PC Speaker

If you are getting mad by the sound of the PC Speaker while you are working in the console mode, kill it before it drives you mad.

sudo rmmod pcspkr

Update: It is better to do,

sudo modprobe -r pcspkr

Latex tip - how to include graphics

Tools

First convert your PNG file to EPS format. For that the png2eps can be used. To make png2eps work you would need to install the following:
  1. pngcheck (part of netpbm)
  2. pngtopnm (part of netpbm)
  3. pnmtotiff (part of netpbm)
  4. tiff2ps (part of libtiff-tools)
  5. sed basename bc (should come default with your distro)

$ sudo aptitude install pngcheck libtiff4 libtiff-tools netpbm
$ wget http://www.henlich.de/media/png2eps/png2eps
$ chmod a+x png2eps
$ ./png2eps myimage.png > myimage.eps

Using graphics

\documentclass[a4paper,10pt]{article}
\usepackage{graphics}
\usepackage{graphicx}

\begin{figure}
\caption{some figure}
\begin{center}
\includegraphics[bb=0 0 438 476]{myimage.eps}
\end{center}
\end{figure}


NOTE: Although I prefer PNG, if you have a jpj file, the jpg2eps tool can be used.

[Source: A PNG to EPS convertor, Images in LaTeX]

Update:
I have found it much easier to create illustrations in Dia and export them directly in the eps format. Dia is available for Windows, Mac and Linux.

Empathy has a long way to go!

The new IM client on 9.10, Empathy, is not at all impressive or may I say that I am so used to Pidgin that I had to switch back.

What is bad?
  1. Notification mechanism - when someone pings you, the new chat window does not show up but is hidden in the notification area, which is really weird
  2. Does not give too many options under preferences
  3. You can't search for contacts while they are offline (which is required because most of the people are invisible these days)
  4. You can't add a buddy pounce
  5. Empathy would always remember your password. If you tell it not to, it won't log you in and once you give your password, it stays there. (this is really weird)
  6. You can't close a conversation with the ESC key

The GREAT news is that Pidgin now supports voice and video, so can someone please tell me why they hell did Gnome decide to dump Pidgin for Empathy :-o

How to install Pidgin?

sudo aptitude install pidgin pidgin-themes pidgin-facebookchat pidgin-musictracker

Latex tip - don't forget to run bibtex

If you compile your .tex file and end up seeing an error ".bbl could not be located", after you open the .dvi file generated, you will notice that the reference section is missing. This is because in the first run LaTeX created the .aux file which needs to be fed to Bibtex which will generate the .bbl file.

So,

#1 $ latex source.tex
#2 $ bibtex source.aux (generated in Step #1)
#3 $ latex source.tex


Kile does this in one shot, it is able to figure out that bibtex needs to be run in the middle.

Q: Is there any another way to do this in one shot from the command line?

Sqlite

SQLite is a C library that implements an SQL database engine. Programs that link with the SQLite library can have SQL database access without running a separate RDBMS process. (src: aptitude show sqlite)

To install sqlite on Ubuntu

$ sudo aptitude install sqlite3

Help
SQLite documentation,
SQLite in 5 minutes or less,
Using SQLite for simple database storage
C\C++ API reference

Ubuntu 9.10 is here



Ubuntu 9.10 Karmic Koala is here. Download now or upgrade from existing installation.

Check the upgrade notes before going for an upgrade.

[Source: Upgrading to Ubuntu 9.10]

  1. Start System/Administration/Update Manager
  2. Click the Check button to check for new updates
  3. If there are any updates to install, use the Install Updates button to install them, and press Check again after that is complete
  4. A message will appear informing you of the availability of the new release
  5. Click Upgrade
  6. Follow the on-screen instructions
On the blog-sphere

Latex Math symbols


A good online reference to the different Math symbols in LaTeX. The file A.tex has all the symbols for ready reference.

Download the file
$ latex A.tex

Open DVI file in Evince or Okular.

Tools

You would need to install latex(TeX-Live). Also, KBibTex, Kile and Lyx are highly recommended tools

sudo aptitude install texlive-full kile kbibtex lyx

Ubuntu LaTeX help guide

Shared libraries

Remember

gcc -shared -Wl,-soname,your_soname -o library_name file_list library_list

Which means, if you have a.c and b.c

gcc -fPIC -g -c -Wall a.c
gcc -fPIC -g -c -Wall b.c
gcc -shared -Wl,-soname,libmystuff.so.1 \
-o libmystuff.so.1.0.1 a.o b.o -lc


Then create the symbolic links to libmystuff.so and libmystuff.so.1 from libmystuff.so.1.0.1 and don't forget to add the libraries to the standard path /usr/local/lib OR add the path to LD_LIBRARY_PATH before executing.

More details: Creating shared libraries
Very good How-To: Link

pthread

A very good one-shot tutorial to the POSIX thread libraries (pthread). Talks all about threading, synchronization and scheduling with example code.

Deadline scheduling for Linux kernel

A new scheduler class has been proposed for including deadline based scheduling algorithms like EDF
The public git repository is available on gitorious
git clone git://gitorious.org/sched_deadline/linux-deadline.git

Generating patches without cleaning code

Instead of generating a patch from two pristine copies of the code (one with your changes and both not compiled), it is useful to create a exclude-list of all the files that you want to ignore.

 .*
 *.lds
 *.o
 *.o.*
 *.a
 *.s
 *.ko
 *.so
 *.so.dbg
 *.mod.c
 *.i
 *.lst
 *.symtypes
 *.order
 *.elf
 *.bin
 *.gz
 *.lzma
 *.patch
 *.gcno
 tags
 TAGS
 bzImage
 vmlinux
 System.map
 Module.markers
 Module.symvers
 !.gitignore
 !.mailmap
 patches-*
 patches
 series
 exclude-list
 filefortag
 filelist
 cscope.*
 ncscope.*
 GPATH
 GRTAGS
 GSYMS
 GTAGS
 *.orig
 *~


Then create the patch

diff --exclude-from /path/to/exclude-list -urNd linux-2.6.24-hunterwala linux-2.6.24 > patch_file

Each line in exclude-list is a pattern which diff would match and ignore the files. I am having trouble excluding following directories.

include/asm
include/asm-*/asm-offsets.h
include/config
include/linux/autoconf.h
include/linux/compile.h
include/linux/version.h
include/linux/utsrelease.h
include/linux/bounds.h
include/generated


So, the -N passed to diff would treat these as new files and include them in the patch. :(

TODO: Update post once I find a better way to handle directories.

[The exclude list above has been taken from .gitignore]

Threading and parallelism

Another good series of articles found on www.embedded.com on threading and parallelism

Part 1: Parallelism and threading primer
Part 2: The threading development cycle
Part 3: Debuging and tuning multi-threaded code

Linux performance analysis for embedded system

Nice series of articles found on www.embedded.com

Part 1: Available tools
Part 2: Profiling/analysis methods and techniques

fscanf

Did not know this until yesterday.

fscanf (fptr, "%*s");
               ^^^
This reads the line from the file but does not store it in any local variable, as compared to this,

fscanf (fptr, "%s", buffer);

Handy '*' :)

Find executables

To find all the executables files

find . -type f -executable -name "whatever*"

Kernel debugging tools

While working inside the kernel you are prone to crashes, oops and complete kernel freeze. Some of these tools\methods help

1. lockdep
http://www.mjmwired.net/kernel/Documentation/lockdep-design.txt

2. netconsole
http://www.mjmwired.net/kernel/Documentation/networking/netconsole.txt

Using netconsole inside Ubuntu
https://wiki.ubuntu.com/KernelTeam/Netconsole

Another netconsole tutorial
http://www.cyberciti.biz/tips/linux-netconsole-log-management-tutorial.html

3. Debugging kernel Oops
https://wiki.ubuntu.com/DebuggingKernelOops

4. Using the built-in kernel debugger
http://oss.sgi.com/projects/kdb/
IBM tutorial (http://www.ibm.com/developerworks/linux/library/l-kdbug/) .. looks dated .. not sure if this works with 2.6.24+ kernels

5. Nice article on Kernel_Debugging_Tips

6. Another brilliant article on Kernel oops

Update: I recently posted a piece on how to use KGDB over the serial null modem cable

Print the IP of the machine at login

Wanted to know the IP address of the test machine the moment I log in. include the following in your .bashrc file.

echo "Welcome to" `hostname` "("`ifconfig eth0 | grep "inet addr:" | awk -F: '{print($2)}' | sed "s/ /:/g" | awk -F: '{print($1)}' `")"

Not very efficient use of sed and awk :(

Script to install a new kernel

#!/bin/sh
#
# (c) Hunterwala

if [ $(/usr/bin/id -u) -ne 0 ]; then
     echo "run this script with sudo"
     exit 2
fi

if [ -z $1 ]; then
     echo "kernel version not found"
     echo "usage: ./install_kernel [kernel_version]"
     exit 2
else
     VERSION=$1
     echo "using $VERSION"
fi

if [ ! -d /lib/modules/$VERSION ]; then
     echo "kernel modules not installed"
     echo "run make install_modules from the kernel directory"
     exit 2
fi

BOOTDIR=/boot

echo "Installing kernel ........................"
cp $PWD/.config $BOOTDIR/config-$VERSION
cp $PWD/System.map $BOOTDIR/System.map-$VERSION
cp $PWD/arch/i386/boot/bzImage $BOOTDIR/vmlinuz-$VERSION
echo "done"

echo "Creating initrd image ...................."
update-initramfs -c -k $VERSION
echo "done"

echo "Updating grub ............................"
update-grub
echo "done"

echo "Linux kernel $VERSION successfully installed"
echo "Please restart and select $VERSION in grub menu"


Kernel module programming

Found a nice article on TLDP on Linux kernel module programming in the 2.6 kernel. (link)

Using VLC player to play Internet Radio

Agent M sent me a link to a very good online western classical radio site. In order to play the radio inside Firefox on Ubuntu the gstreamer plugins are required. The other option is to play it through VLC player.

Install VLC player
%% sudo aptitude install vlc

Open the link in VLC, Media->Open Network, enter the URL and you are good to go.

To avoid this step every time, create a launcher.

1. Right click on the desktop, "Create launcher"
2. Give it a name, "My Radio"
3. In the command use the following /usr/bin/vlc "URL"
4. You can specify a tool-tip comment as well, "ABCD Radio"
5. Click on the icon and select the icon of your choice form /usr/share/icons/hicolor/scalable

Enjoy the music

Battery monitoring from console

If you prefer to work on a console based machine (without gdm\kdm), the following script is useful if you are running your laptop on battery. It shows the current battery charge and can be used for monitoring the battery discharge.

#!/bin/sh
# While working in console mode (without gdm) this script
# tells you the remaining charge of your laptop battery
#
# Run it along with watch on a separate tty
# $ watch -n 10 ./batmon
#
# (c) Hunterwala, 2009
#
# TODO: check for error levels and buzz using pcspkr mod
# when the charge level decreases beyond that level.

PROC_PATH="/proc/acpi/battery/BAT0"

org_cap=`cat $PROC_PATH/info | grep "last full capacity" | sed "s/ //g" | awk -F: '{print($2)}' | sed "s/mAh//g"`
rem_cap=`cat $PROC_PATH/state | grep "remaining capacity" | sed "s/ //g" | awk -F: '{print($2)}'| sed "s/mAh//g"`
message=`cat $PROC_PATH/state | grep "charging state" | sed "s/ //g" | awk -F: '{print($2)}'| sed "s/mAh//g"`

diff=$(echo "($org_cap-$rem_cap)" | bc)
total_per=$(echo "100-($diff*100/$org_cap)" | bc)

echo "Battery is" $message ":" $total_per"%"


Save this script as batmon.sh

Change the permissions chmod a+x batmon.sh

Run it on a separate tty watch -n 10 ./batmon.sh

64 bit division

If you are trying to do this inside the kernel:
unsigned long long arg1, arg2;
arg1 = arg1/arg2;


and you get this error:
Unknown symbol __udivdi3

You ought to do this instead:
unsigned long long arg1, arg2;
unsigned long temp = arg1;
do_div(temp, arg2);
arg1 = temp;

Installing RPMs on Ubuntu

Method 1

1. Install rpm
$ sudo aptitude install rpm

Along with other things it would install rpm2cpio. (RPM and initramfs use the CPIO archiver)

2. Extract the contents of the RPM
$ rpm2cpio < some-package.rpm | cpio -i --make-directories --verbose

3. Copy over the required files to the different directories.



Method 2

1. Install alien
$ sudo aptitude install alien

2. Covert the .rpm to .deb
$ sudo alien --to-deb --scripts --verbose some-package.rpm

NOTE: Alien internally uses rpm2cpio, so make sure you have installed rpm.

--scripts : If the .rpm has scripts then you need to include those

3. Install the .deb file.
sudo dpkg -i some-package.deb

The advantage of converting it to .deb and installing using dpkg is that you can manage the install\uninstall\reinstall and version using apt\aptitude.

LCD monitor resolution

Got a new Samsung 22" 1920x1080 monitor and the default resolution being shown was 1280x800. Now with 8.04 I used to do dpkg-reconfigure xserver-xorg and it would redo the X11 configuration.

In Jaunty, the above command just configures the keyboard setting. In order to get the monitor to work with the correct resolution do

$ xrandr

This would probe the monitor and display the resolutions, then do the following

$ xrandr --output VGA --auto --above LVDS

emacs - indent entire buffer

c-set-style {chose the mode}
C-x h
C-M \


Line 2: selects the entire buffer
Line 3: changes the indentation

20 great GDM screens for Ubuntu

Awesome GDM screens for Ubuntu, here

Installing is very easy:

1. Click on the theme you like and download the archive.
2. Right click on your name on the upper right-side toolbar and select "Setup login screen"
3. Click on "Local" pane, then on the "Add" button, browse to the folder where you downloaded the archive (tar.gz) and then select the theme.

Bingo! you are done. logout and log back in!!

The best part is that you can select "Random from selected", pick the best ones and every time you login, it would be a different theme!

emacs client

When one instance of emacs is already running and you want to open files from command line into the same instance, use emacs client.

First, start the emacs server. Add the following line in your .emacs file:
(server-start)

Start emacs, and use emacs client from the command line to open the files in the already open emacs instance:
emacsclient -n filename

It helps to create an alias:
alias cemacs='emacsclient -n'
alias emacs='emacs &'

new system

Just installed a new system with a fresh Ubuntu install? Install all the packages in one shot,

~$ sudo aptitude install build-essential ddd insight kdbg kdevelop eclipse eclipse-cdt eclipse-jdt kompare kate bless kile lyx cscope global octave kmplot vim emacs virtualbox-ose pdfedit scribus inkscape checkgmail kompozer manpages manpages-posix easytag gtkpod vlc libncurses5 libncurses5-dev dialog curl sun-java6-jre sun-java6-jdk wireshark qt4-dev-tools qt4-assistant qt4-designer qdevelop monodevelop umbrello git git-core gitk git-gui graphviz doxygen doxygen-gui

Apart from that, install the individual .deb for

1. Google Chrome (dev channel)
2. Skype for 32 bit (64-bit still shaky)
3. Picasa for Linux

Before installing new packages, do
~$ sudo apt-get update; sudo apt-get upgrade

sudo first

Check if you running your script through sudo.

if [[ $(/usr/bin/id -u) -ne 0 ]]; then
    echo "Not running as root"
    exit
fi

Linux Kernel design patterns

Linux kernel design patterns, part 1, part 2 and part 3.

Gnuplot - resources

Very useful gnuplot resources
  1. The official gnuplot documentation and demos (The demos are really useful as they give the scripts that can me modified to suit your plot)
  2. Introduction to gnuplot 
  3. Creating graphs with gnuplot part1, part2 and part3

Git

Started using git.. this page would get more info as I learn more.

1. A very good presentation\video - here
2. Git documentation guide\book - here
3. Understanding Git, another tutorial - here

Tweet from the command line

Do this from the command line

curl -u username:password -d status="the message" http://twitter.com/statuses/update.xml

Add the following function in your .bashrc file.

# function to tweet
tweet() {
  flag=0
  if [[ -z "$1" && -z "$2" ]]; then
    echo "Usage: tweet password \"message\""
    echo "Make sure the message is within quotes"
    flag=1;
  fi
  if [ ${flag} == "0" ]; then
    echo "Sending message...."
    curl -u username:$1 -d status="$2" http://twitter.com/statuses/update.xml
  fi
}


and then do
$tweet password "The message goes here"

[Source]

more vim

Useful stuff to put inside your ~/.vimrc file

" Set tab width
set tabstop=4
set softtabstop=4
set shiftwidth=4
set smarttab
set smartindent
set expandtab
set cursorline

" Show line numbers and cursor position
set ruler
set number

" Disable default splash screen
set shortmess+=I
set guioptions-=T

" Show tabs and trailing white spaces
set list listchars=tab:-»,trail:·,extends:»

" Don't generate backup files
set nobackup

" Set default color scheme to something
colorscheme something


If you don't have it, create a folder ~/.vim/colors and put the color scheme inside.

Color schemes are available here

oprofile

oprofile is very helpful in getting a trace out of the kernel especially to know the %age usage of methods from the kernel and the application. Helps in debugging some weird problems.

First, enable oprofile in the kernel, it is good to build it part of the kernel rather than a kernel module, so make sure in the menuconfig option it is a [*] rather than [m].

You can install the oprofile client once you boot back into your file system.
sudo apt-get install oprofile oprofile-gui

Before starting your application, initialize
$ sudo opcontrol --init

The following two options can be done once in the beginning

Tell oprofile the path to your compiled and unstripped vmlinux file so that it can pick the symbols from there
$ sudo opcontrol --vmlinux=/path/to/kernel/image

Set the call stack depth
# sudo opcontrol --callgraph=#depth

Now check the status,
$ sudo opcontrol --status

Reset the earlier dump,
$ sudo opcontrol --reset

Start the profiler
$ sudo opcontrol --start

Run your application
$ ./mystupidapp

All done, stop the profiler
$ sudo opcontrol --stop

Get the dump
$ sudo opcontrol --dump

Now, get the report
$ sudo opreport -l (and various options if you want)

Clear it,
$ sudo opcontrol --reset

Deinit,
$ sudo opcontrol --deinit

and start again if you want to.

I use alias(es) in the .bashrc file. Really helps!
alias opc='sudo opcontrol'
alias opcdeinit='opc --deinit'
alias opcdump='opc --dump'
alias opcinit='opc --init'
alias opcreset='opc --reset'
alias opcstart='opc --start'
alias opcstatus='opc --status'
alias opcstop='opc --stop'
alias opr='sudo opreport -l'
alias oprcall='opr -c'


Bing !

I am not sure if the search results given by bing are going to be anything to brag about or any different (good\worst) than Google's but there are certain features in bing (the ones that I noticed, there might be a gazillion according to peeps at MS) which might find their way into Google soon, in one way or the other ;)

1. Tiny website preview\summary with the context of the search word\phrase - move to mouse to right of the result, a yellow dot appears which gives a snapshot of the result - handy and avoids opening the page to see if the result makes sense.

2. Video preview - this is the best one, while searching for videos (especially the useful ones :D ), bing plays them right in the search result panel. Isn't that nice!

3. Enhanced pages for Wikipedia results - you can click on the link to "enhanced page" and the wikipedia entry opens right inside the search panel. Do know how useful that is!

emacs - match parenthesis

One good thing I liked about vim is that the parenthesis matching is so damn easy - use %

So, in order to enable the same in emacs, add the following in your .emacs file
(global-set-key "%" 'match-paren)
    (defun match-paren (arg)
    "Go to the matching paren if on a paren; otherwise insert %."
    (interactive "p")
    (cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
          ((looking-at "\\s\)") (forward-char 1) (backward-list 1))
          (t (self-insert-command (or arg 1)))))


[source]

emacs lightweight options and vim

I use emacs all of the time. However, the problem with emacs is that it is very heavy duty and usually not available on embedded targets.

Zile and mg and two light-weight options for emacs (sudo apt-get install mg zile )

Also, it is good to know how to use vim

Quick starter

Open file [Esc first] :e
Save & Exit - [Esc first] :wq
Save - [Esc first]:w
Don't save & Exit - [Esc first]:!q

Up - [Esc first] k or Up arrow
down - [Esc first] j or Down arrow
left - [Esc first] h or Left arrow
right - [Esc first] l or right arrow
start of line - [Esc first] ^
end of line - [Esc first] $
page up - cntrl+b
page down - cntrl+f
Goto line x - xG

undo - [Esc first] u
redo - [Esc First] Shift+r
delete char - [Esc First] x
delete word - [Esc First] dw
delete line - [Esc First] dd

search - [Esc first] /
next - n
prev - Shift+n

help - [Esc first]:help
or start from command line
$ vimtutor

Regular expressions and Sed

Bruce Barnett is God!

1. Regular expression tutorial, here
2. Awesome Sed tutorial, here

Weird

I was just checking the post list on this blog and for 2008 and 2009 I have not made any posts in the months of March and April. Strange!

Using gtags (global)

As a follow up to the last post, I tried emacs+gtags and it is really powerful when compared to emacs+etags.

Important things to get started.

First run gtags on the code.
$ gtags

This would create four files GPATH, GRTAGS, GSYMS, GTAGS

Open emacs and enable gtags
M-x gtags-mode RET

To enable auto completion of function names\symbols etc use,
M-x gtags-make-complete-list
NOTE: This is deprecated and not required now.

To locate a function use,
M-x gtags-find-tag function_name.
You can also use M-.

If more than one is found a stack is returned. Select one and hit RET. In order to get the stack use,
M-x gtags-pop-stack.
You can also use M-*

To find a symbol use,
M-x gtags-find-symbol m_myVar

To find anything use,
M-x gtags-find-with-grep "gtags rules"

Once you modify the code, just update the code
global -u -v

To visit the tags folder use,
M-x gtags-visit-rootdir and point it to the folder that has the TAGS!

global and gtags

I have heard a lot about GNU GLOBAL source code tag system, need to try it out.

GNU global

emacs - system wide copy and paste

Using [M]-W and [C]-Y one can copy and paste inside emacs but it only works for the emacs buffers. In order to enable system wide copy\paste (through the clipboard) with other applications set the following in the .emacs file

(setq x-select-enable-clipboard t)

OR use

META-X set-variable RET x-select-enable-clipboard RET t

Lot more here

[src]

initird

Nice (but a little old) article on initrd internals. Click here.

Bash scripting

1. A very good basic BASH scripting tutorial. Click here
2. A very advanced tutorial at TLDP. Click here

Linux embedded Systems

Nice tutorial that concludes a series of tutorials on embedded Linux system development by noted ARM Linux kernel hackers Vincent Sanders and Daniel Silverstone, here

ASCII art

This is amazing stuff!!
 _   _             _                          _       
| | | |_ _ _ __ | |_ ___ _ ____ ____ _| | __ _
| |_| | | | | '_ \| __/ _ \ '__\ \ /\ / / _` | |/ _` |
| _ | |_| | | | | || __/ | \ V V / (_| | | (_| |
|_| |_|\__,_|_| |_|\__\___|_| \_/\_/ \__,_|_|\__,_|
OR
 _   _  __  __  _  _  ____  ____  ____  _    _    __    __      __   
( )_( )( )( )( \( )(_ _)( ___)( _ \( \/\/ ) /__\ ( ) /__\
) _ ( )(__)( ) ( )( )__) ) / ) ( /(__)\ )(__ /(__)\
(_) (_)(______)(_)\_) (__) (____)(_)\_)(__/\__)(__)(__)(____)(__)(__)
Make you own and embed it in your source code :)
Click here to convert. They have various fonts as you see ;)

ping sweep

You have a machine on the subnet for which you know the MAC address but you don't know what IP address it got after a restart, use nmap and do a ping sweep:

sudo nmap -n -sP 192.168.1.0/24 | grep "00:11:22:33:44"

-n tells nmap not to never do reverse DNS resolution on the active IP address it finds. Makes scanning faster
-sP Only perform a ping scan (host discovery) and prints the results, go no further.

HTML editor

I am working on creating the website for my lab and I had to find a good open source alternative to Dreamweaver.

Bluefish Editor
It is a nice editor but it is not a WYSIWYG. One needs to go back to learning HTML and family and it becomes irritating after sometime.





Quanta Plus
Like Bluefish but more crappy and archaic. I would suggest installing software using aptitude rather than apt-get because it is easy to purge and clean the dependency chain that it creates.




KompoZer
This is best editor. Powered by Mozilla (even looks like Firefox) and is a WYSIWYG with powerful features like tag view, source view and preview. It provides easy layering (div) and properties. It took me 10 minutes to create the same page that took 1 hour in Bluefish. Winner!!!

[Book] Beginning Perl



Beginning Perl by Simon Cozens is available in PDF format online under the Creative Commons license. Nice resource, if you are starting from scratch.

link

[Image courtsey: http://www.amazon.com]

Changing the default size of gnome-terminal

The small window can be a pita sometimes.

Figure out what size you want to make the terminal. (If you have compiz enabled, while dragging the right bottom edge, you can get the size in the center of the screen, else trial-and-error)

Add gnome-terminal to the panel, change the properties and add the following to the command

gnome-terminal --geometry 152x55

(or whatever size you prefer)

[Book] Linux in a nutshell



This O'Reilly book is a good starter for people new to Linux. Here are the list of useful commands from the book.

[Image courtsey http://www.oreillynet.com]

Using cut and uniq

For some weird reason I had to do this, cut and uniq turned out to be quite handy and can be used along with sed

cat /proc/cpuinfo | grep "model name" | uniq | cut -d: -f2

So, instead of this
model name : Intel(R) Core(TM)2 Duo CPU E8500 @ 3.16GHz
model name : Intel(R) Core(TM)2 Duo CPU E8500 @ 3.16GHz


I get this
Intel(R) Core(TM)2 Duo CPU E8500 @ 3.16GHz

OK, another approach is to use awk
cat /proc/cpuinfo | grep "model name" | uniq | awk -F: '{print($2)}'

Emacs command list

Sometimes I tend to forget some of these :D :P

C = Control
M = Meta = Alt|Esc


Basics
C-x C-f "find" file i.e. open/create a file in buffer
C-x C-s save the file
C-x C-w write the text to an alternate name
C-x C-v find alternate file
C-x i insert file at cursor position
C-x b create/switch buffers
C-x C-b show buffer list
C-x k kill buffer
C-z suspend emacs
C-X C-c close down emacs


Basic movement
C-f forward char
C-b backward char
C-p previous line
C-n next line
M-f forward one word
M-b backward one word
C-a beginning of line
C-e end of line
C-v one page up
M-v scroll down one page
M-< beginning of text
M-> end of text


Editing
M-n repeat the following command n times
C-u repeat the following command 4 times
C-u n repeat n times
C-d delete a char
M-d delete word
M-Del delete word backwards
C-k kill line

C-Space Set beginning mark (for region marking for example)
C-W "kill" (delete) the marked region region
M-W copy the marked region
C-y "yank" (paste) the copied/killed region/line
M-y yank earlier text (cycle through kill buffer)
C-x C-x exchange cursor and mark

C-t transpose two chars
M-t transpose two words
C-x C-t transpose lines
M-u make letters uppercase in word from cursor position to end
M-c simply make first letter in word uppercase
M-l opposite to M-u


Important
C-g quit the running/entered command
C-x u undo previous action
M-x revert-buffer RETURN (insert like this) undo all changes since last save
M-x recover-file RETURN Recover text from an autosave-file
M-x recover-session RETURN if you edited several files


Online-Help
C-h c which command does this keystroke invoke
C-h k which command does this keystroke invoke and what does it do?
C-h l what were my last 100 typed keys
C-h w what key-combo does this command have?
C-h f what does this function do
C-h v what's this variable and what is it's value
C-h b show all keycommands for this buffer
C-h t start the emacs tutorial
C-h i start the info reader
C-h C-k start up info reader and go to a certain key-combo point
C-h F show the emacs FAQ
C-h p show infos about the Elisp package on this machine


Search/Replace
C-s Search forward
C-r search backward
C-g return to where search started (if you are still in search mode)
M-% query replace

Space or y replace this occurrence
Del or n don't replace
. only replace this and exit (replace)
, replace and pause (resume with Space or y)
! replace all following occurrences
^ back to previous match
RETURN or q quit replace



Search/Replace with regular expressions
Characters to use in regular expressions:
^ beginning of line
$ end of line
. single char
.* group or null of chars
\< beginning of a word
\> end of a word
[] every char inside the backets (for example [a-z] means every small letter)

M C-s RETURN search for regular expression forward
M C-r RETURN search for regular expression backward
M C-s incremental search
C-s repeat incremental search
M C-r incremental search backwards
C-r repeat backwards
M-x query-replace-regexp search and replace


Window-Commands
C-x 2 split window vertically
C-x o change to other window
C-x 0 delete window
C-x 1 close all windows except the one the cursors in
C-x ^ enlarge window
M-x shrink-window command says it ;-)
M C-v scroll other window
C-x 4 f find file in other window
C-x 4 o change to other window
C-x 4 0 kill buffer and window
C-x 5 2 make new frame
C-x 5 f find file in other frame
C-x 5 o change to other frame
C-x 5 0 close this frame


Bookmark commands
C-x r m set a bookmark at current cursor pos
C-x r b jump to bookmark
M-x bookmark-rename says it
M-x bookmark-delete "
M-x bookmark-save "
C-x r l list bookmarks

d mark bookmark for deletion
r rename bookmark
s save all listed bookmarks
f show bookmark the cursor is over
m mark bookmarks to be shown in multiple window
v show marked bookmarks (or the one the cursor is over)
t toggle listing of the corresponding paths
w " path to this file
x delete marked bookmarks
Del ?
q quit bookmark list

M-x bookmark-write write all bookmarks in given file
M-x bookmark-load load bookmark from given file


Shell
M-x shell starts shell modus
C-c C-c same as C-c under unix (stop running job)
C-d delete char forward
C-c C-d Send EOF
C-c C-z suspend job (C-z under unix)
M-p show previous commands


DIRectory EDitor (dired)
C-x d start up dired
C (large C) copy
d mark for erase
D delete right away
e or f open file or directory
g reread directory structure from file
G change group permissions (chgrp)
k delete line from listing on screen (don't actually delete)
m mark with *
n move to next line
o open file in other window and go there
C-o open file in other window but don't change there
P print file
q quit dired
Q do query-replace in marked files
R rename file
u remove mark
v view file content
x delete files marked with D
z compress file
M-Del remove all marks (whatever kind)
~ mark backup files (name~ files) for deletion
# mark auto-save files (#name#) for deletion
*/ mark directory with * (C-u * removes that mark again)
= compare this file with marked file
M-= compare this file with it's backup file
! apply shell command to this file
M-} change to the next file marked with * od D
M-{ " previous "
% d mark files described through regular expression for deletion
% m " (with *)
+ create directory
> changed to next dir
< change to previous dir
s toggle between sorting by name or date
M-x speedbar starts up a separate window with a directory view


Telnet
M-x telnet starts up telnet-modus
C-d either delete char or send EOF
C-c C-c stop running job (similar to C-c under unix)
C-c C-d send EOF
C-c C-o clear output of last command
C-c C-z suspend execution of command
C-c C-u kill line backwards
M-p recall previous command


Text
Works only in text mode
M-s center line
M-S center paragraph
M-x center-region name says


Macro-commands
C-x ( start macro definition
C-x ) end of macro definition
C-x e execute last definied macro
M-n C-x e execute last defined macro n times
M-x name-last-kbd-macro give name to macro (for saving)
M-x insert-keyboard-macro save named macro into file
M-x load-file load macro
M-x macroname execute macroname


Programming
M C-\ indent region between cursor and mark
M-m move to first (non-space) char in this line
M-^ attach this line to previous
M-; formatize and indent comment

C, C++ and Java Modes
M-a beginning of statement
M-e end of statement
M C-a beginning of function
M C-e end of function
C-c RETURN Set cursor to beginning of function and mark at the end
C-c C-q indent the whole function according to indention style
C-c C-a toggle modus in which after electric signs (like {}:';./*) emacs does the indention
C-c C-d toggle auto hungry mode in which emacs deletes groups of spaces with one del-press
C-c C-u go to beginning of this preprocessor statement
C-c C-c comment out marked area
M-x outline-minor-mode collapses function definitions in a file to a mere {...}
M-x show-subtree If you are in one of the collapsed functions, this un-collapses it


In order to achive some of the feats coming up now you have to run etags *.c *.h *.cpp (or what ever ending you source files have) in the source directory

M-. (Thats Meta dot) If you are in a function call, this will take you to it's definition
M-x tags-search ENTER Searches through all you etaged
M-, (Meta comma) jumps to the next occurence for tags-search
M-x tags-query-replace yum. This lets you replace some text in all the tagged files


GDB (Debugger)
M-x gdb starts up gdm in an extra window

Version Control
C-x v d show all registered files in this dir
C-x v = show diff between versions
C-x v u remove all changes since last checkin
C-x v ~ show certain version in different window
C-x v l print log
C-x v i mark file for version control add
C-x v h insert version control header into file
C-x v r check out named snapshot
C-x v s create named snapshot
C-x v a create changelog file in gnu-style


(Reference: http://lpn.rnbhq.org/tools/xemacs/emacs_ref.html)

Mounting an ISO

sudo mount -t iso9660 -o loop /path/to/the/iso /folder/to/mount/into

In case you are getting errors like ISOFS: Unable to identify CD-ROM format., do a file /path/to/the/iso and confirm that the file that you think is an ISO, actually is.

Using ssh, scp, sftp

SSH
The server node should be running the SSH server

sudo aptitude install openssh-server

Connect to the server

ssh username@ipaddress:port

Connect to the server with X enabled

ssh username@ipaddress:port

Note: specify port only if the default 22 is not being used.

SCP
scp can be used to transfer the files between the nodes (works on top of SSH)

From remote node to local node

cd local_folder
scp username@ipaddress:port:/home/username/path/to/folder_file .


From local node to remote node

scp /path/to/local/folder_file username@ipaddress:port:/home/username/path

SFTP
If you have a luxury of a GUI, you can copy using sftp.

Open nautilus (if using Gnome) and type in the location bar the address to the server.

nautilus --no-dektop --browser
Location Bar: sftp://username@ipaddress:port/home/username


Although you can go to the root folder as well, but is not a good practice to screw around in that area

Getting the VPN to work

Update: Doesn't work

-+-

After connecting to the VPN server, can't access the sites. Need to add stuff to the routing table. Wrote the bash script for that:

Soon after getting connected on VPN, do ifconfig, get the IP address associated to the ppp0 (p-t-p), specify your local router gateway and the interface you are connected through (wireless or wired) and fire the script!

#!bin/sh
# Check for command line
if [ -z "$1" ]; then
  echo "usage: ./vpn.sh [p-t-p] [GW] [Interface]"
  exit
fi

if [ -z "$2" ]; then
  echo "usage: ./vpn.sh [p-t-p] [GW] [Interface]"
  exit
fi

if [ -z "$3" ]; then
  echo "usage: ./vpn.sh [p-t-p] [GW] [Interface]"
  exit
fi

VPN_REMOTE_IP=$1
ALLTARGET="198.82.0.0/16" # All class C addresses
GATEWAY=$2
INTERFACE=$3 #eth0 or eth1

# change!
route add -host $VPN_REMOTE_IP gw $GATEWAY dev $INTERFACE
route add -net $ALLTARGET dev ppp0


Run this script as sudo!

Using VNC over SSH

Okay, now I need to connect to this Linux box in the department which is running Matlab and access it on the wire.

Options:

1. SSH + X11 forwarding [slow]

% ssh -X username@hostname
% matlab &


In order to have SSH forward X11 by default change the following in /etc/ssh/ssh_config and also remove the #

ForwardAgent yes (default no)
ForwardX11 yes (default is no)
StrictHostKeyChecking no (default is ask)


With this you can now use

ssh username@hostname

2. VNC without SSH. [fast]
% vncviewer hostname:display

3. VNC tunneling over SSH [faster]
% ssh -C -L 5901/localhost/5901 username@hostname
% vncviewer localhost:5901


Ensure that vncserver is running on the remote node and also that you have set your vncpasswd. The "display" is the number that you get after starting the vncserver

NOTE:
If you are running compiz\beryl (any composite window manager), Matlab's pre-bundled java does not support it and hence it would not render it properly with option (1). You would have to point to the latest version of the JVM before starting matlab

% export MATLAB_JAVA=/usr/lib/java/blah-blah/jre
% matlab &


Update:
Got a local copy of Matlab and realized that it has the same problem and is solved by pointing to the latest JVM. Create a launcher that takes care of setting up the environment before the launch.

env MATLAB_JAVA="/usr/lib/jvm/java-6-sun/jre" xterm "/home/piyush/matlab-2007b/bin/matlab"

Using goto is not evil.

In fact I find it a much better way of handling exceptions in C as compared to that fucked up if-else nesting that I did for that protocol stack!

Read this.

Block copy and paste in emacs

1. Select the block that you need to cut or copy

Mark set with [control]+[spacebar]. Then move down the cursor to which you need to copy\cut. OR, [control]+[spacebar]+[spacebar] and page down to the desired block in one shot!

2. Copy using [alt]+[w], OR Cut using [control]+[w]

3. Then go to the line\buffer where you want to paste\yank using [control]+[y]


Viewing the initrd image

initrd is a RAM disk file image of the kernel used at the time of boot. On Ubuntu it is created using mkinitramfs or update-initramfs commands when a new kernel is installed. This image is a gunzip\cpio archive.

To see the contents do the following

1. Copy the initrd image which you want to open into a directory somewhere /home/node/temp. Let's say it is initrd-2.6.24-generic

2. Do the following now:

cat initrd-2.6.24-generic | gunzip | cpio -ivdm

OR,

bzcat initrd-2.6.24-generic | cpio -ivdm

your own system call in 5 easy steps

You *might* want to write your system call for various reasons

Assuming the path to your kernel source is "L". Create a new folder L/mysyscall. Inside the folder create the source file mysyscall.c and a Makefile

Step 1. Changing the System Table
L/arch/x86/kernel/syscall_table_32.S

Add your system call at the end of the file.

.long sys_new_system_call

Step 2. Changing the unistd.h
L/linux/include/asm-x86/unistd_32.h

Add your system call at the end of the existing list and append the next number

#define __NR_new_system_call XXX

Where XXX is the existing system call number plus 1. Also update the total system calls (as you just added another)
#define __NR_syscalls XXY
Where XXY is XXX+1

Step 3: Changing syscalls.h
L/include/linux/syscalls.h

Add the declaration of your system call at the end.
asmlinkage long new_system_call (whatever params you want to pass)

Step 4: Changing the kernel Makefile
Add the new folder to the kernel compile

core-y += /kernel /blah /blah /blah /mysyscall

Step 5: Write your system call

Write whatever crap you want to write inside the mysyscall.c file

asmlinkage long new_system_call (whatever params you want to pass)
{
  // whatever you want to do
}


Change the makefile as well and add the following line

obj-y := mysyscall.o

Compile your kernel and test the system call from a user level program. You can create a header file that the user space program can use.

/* header.h */
#include < linux/unistd.h >
#define __NR_new_system_call XXX

/* if you system call returns int and takes no parameter
* use this macro
*/
_syscall0(int,new_system_call)

/* Otherwise, depending on the number of parameters
* being passed use the _syscallN macro, N being the no
* of params, like
_syscall1(int, new_system_call, int)

*/

Last thing to do is to test the code:

/* test client */
#include "header.h"

int main (void)
{
  printf ("System call returned %d \n", new_system_call());
  return 1;
}


NOTE
Starting around kernel 2.6.18, the _syscallXX macros were removed from header files supplied to user space. Instead we need to use syscall() function.

printf ("System call returned %d \n", syscall (__NR_new_system_call, params_if_any));

or, make the following changes in the header.h

/* header.h */
#include < linux/unistd.h >
#include < sys/syscall.h >
#define __NR_new_system_call XXX

long new_system_call (params_if_any)
{
  return syscall (__NR_new_system_call, params_if_any);
}