Pages

Thursday, August 30, 2012

Add Custom Linux Service


Location of Service Program
/root
Program Name
mysvcd
Runlevel
235
Start Priority
(for dependency)
90
Stop Priority
(100 - Start Priority)
10

Add Script for Your Service
       vi /etc/init.d/mysvcd

 ATTENTION: The chkconfig and  description must be specified in the script 
(e.g. runlevel = 235, start priority= 90, stop priority= 10)

                #!/bin/bash
                # chkconfig: 235 90 10
                # description: This is mysvcd script.
\
                #                      more description
                prog=mysvcd

                start() {
                                #code for start service
                                echo "Start mysvcd!!" 

                                /root/$prog&
                }

                stop() {
                                #code for stop service
                                echo "Stop mysvcd!!"
                }

                status(){
                                #code for status check
                                echo "Status of mysvcd!!"
                }

                restart() {
                                #code for restart service
                                echo "Restart mysvcd!!"
                                stop
                                start
                }

                case "$1" in
                        start)
                                start
                                ;;
                        stop)
                                stop
                                ;;
                        status)
                                status
                                ;;
                        restart)
                                restart
                                ;;
                        *)
                                echo "Usage: $prog {start|stop|restart}"
                esac


Add Service
                chkconfig --add mysvcd

If you want to start service immediately, run the following command:
                service mysvcd start

Tuesday, August 28, 2012

gdb and ddd Tutorial

Compile with debug support
    gcc -g myprog.c -o myprog

Start ddd and load program
    ddd myprog

Load a program. (You can skip this step because we load the program in previous step)
     file myprog


 Breakpoints
Set a breakpoint at specific line (e.g. break at line 7 of myprog.c)

     break myprog.c:7

Set a breakpoint at when calling specific function (e.g break when myfunc is called)
     break myfunc

Set a conditional breakpoint (e.g. variable myvar <= 3)
     break myprog.c:7 if myvar <= 3

Show breakpoints
     info breakpoints

Ignore specific breakpoint (e.g. ignore breakpoint 1 for 100 times. You can get number from "show breakpoints")
      ignore 1 100

Delete a breakpoint (Delete breakpoint 1. You can get number from "show breakpoints")
     delete 1


Control
Set watchpoint. (Stop execution when program changes the specific variable)
      watch myvar

Continue running after it hit a breakpoint
     continue

Finish (Finish current function)
     finish

Step Over (Trace into a function)
     step

Next (Treat function as a normal statement)
     next

Run/Restart the program
     run



Variables
Print content of specific variable (e.g. print content of myvar)
     print myvar

Print in hexadecimal
     print/X myvar

Monday, August 27, 2012

Drivers for Linux

You devices may be supported by extra kernel modules.
You can try to install kernel-modules-extra before searching a driver for your devices.

To install kernel-modules-extra
    yum install kernel-modules-extra

Reboot to take effect
    reboot

Vim Persistent Undo (Undo persistence)

Enable persistent undo
vi  ~/.vimrc
#Location of undo data
set undodir=~/.vim/undodir
#By setting the 'undofile' option, Vim will automatically save your undo history
#when you write a file and restore undo history when you edit the file again. (udf)
set undofile
#Maximum number of changes that can be undone.(ul)
set undolevels=1000
#Save the whole buffer for undo when reloading it.(ur)
set undoreload=10000

Create undodir
mkdir ~/.vim/undodir


Now, you can undo even you quit vim.

Saturday, August 18, 2012

Linux Command Line Network Setup

Setup IP and Netmask
(Interface eth0, IP = 192.168.1.1, Netmask = 255.255.255.0)
    ifconfig eth0 192.168.1.1 netmask 255.255.255.0 up

Setup Default Gateway
(Interface eth0, Gateway IP = 192.168.1.254)
    route add default gw 192.168.1.254 eth0

Setup DNS Server and Search Domain
    vi /etc/resolv.conf
        nameserver 8.8.8.8
        search csie.ntu.edu.tw

Tuesday, August 14, 2012

Compile A Kernel Module / Driver

To Install Kernel Header
yum install kernel-devel

To Install GCC
yum install gcc

<compile module source code>
make

<install your compiled module>
make install

To Generate module.dep for New Module
depmod

To Add New Module
modprobe module_name

PS: You may need to update initrd by mkinitrd for some boot devices.

Create Custom Linux Kernel

Download Kernel Source Code
    wget http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.5.1.tar.bz2

Extract Source Code
    tar jxvf linux-3.5.1.tar.bz2

Create Config with menuconfig
    cd linux-3.5.1

    (for full text mode)

    make menuconfig

    (for X window system)
    make xconfig

    (for GTK)
    make gconfig

PS: You can load .configuration from /usr/src/kernels/x.y.z/.config (current kernel)

Compile (4-parallel for multi-core CPU)
    make -j 4


Rename Network Interfces

 To rename network interface
    vi /etc/udev/rules.d/70-persistent-net.rules

To modify name
    SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:11:22:33:44:55", ATTR{type}=="1", KERNEL=="wlan*", NAME="wlan0"