Pages

Thursday, November 22, 2012

PERL IDE, Eclipse with EPIC

Install Eclipse
yum install eclipse

Install pad-walker for debugging
yum install perl-PadWalker

Launch Eclipse
eclipse &

Add Software Source
Select "Help/Install New Software" from menu bar
Click "Available Software Sites"
Click "Add"
Enter following information:
Name "EPIC"
Location "http://e-p-i-c.sf.net/updates"
REF: http://www.epic-ide.org/download.php

Wednesday, October 17, 2012

Redirect Standard Error to Standard Output / Redirect Standard Output to Standard Error / Redirect to File

To Redirect Standard Error to Standard Output
command_1 1>&2 | command_2

To Redirect Standard Output to Standard Error

command_1 2>&1 | command_2

To Redirect Standard Output to File
command > filename
OR
command 1> filename


To Redirect Standard Error to File
command 2> filename


To Redirect Standard Output and Standard Error to Separate Files
command 1> filename1 2> filename2

To Redirect Standard Output and Standard Error to Single File

command &> filename

Friday, September 21, 2012

Modify UUID of A FAT32 or NTFS Partition

The serial number (the 0x43 ~ 0x46 bytes) of FAT32 filesystem is used as UUID in Linux system. You modify UUID of a FAT32 partition by following command:

Dump the first 512 bytes of the FAT32 partition
dd if=/dev/sdb1 of=sblk.bin bs=512 count=1

Modify dump file (e.g. B893-3D6F --> C888-222C)
vi sblk.bin
%!xxd

0000000: eb58 906d 6b64 6f73 6673 0000 0208 2000  .X.mkdosfs.... .
0000010: 0200 0000 00f8 0000 3e00 f900 0000 0000  ........>.......
0000020: 00f8 3f00 f80f 0000 0000 0000 0200 0000  ..?.............
0000030: 0100 0600 0000 0000 0000 0000 0000 0000  ................
0000040: 0000 296f 3d93 b820 2020 2020 2020 2020  ..)o=..        
0000050: 2020 4641 5433 3220 2020 0e1f be77 7cac    FAT32   ...w|.
0000060: 22c0 740b 56b4 0ebb 0700 cd10 5eeb f032  ".t.V.......^..2
0000070: e4cd 16cd 19eb fe54 6869 7320 6973 206e  .......This is n
0000080: 6f74 2061 2062 6f6f 7461 626c 6520 6469  ot a bootable di
0000090: 736b 2e20 2050 6c65 6173 6520 696e 7365  sk.  Please inse
00000a0: 7274 2061 2062 6f6f 7461 626c 6520 666c  rt a bootable fl
00000b0: 6f70 7079 2061 6e64 0d0a 7072 6573 7320  oppy and..press
00000c0: 616e 7920 6b65 7920 746f 2074 7279 2061  any key to try a
00000d0: 6761 696e 202e 2e2e 200d 0a00 0000 0000  gain ... .......
00000e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000100: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000110: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000120: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000130: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000140: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000150: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000160: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000170: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000180: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000190: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001f0: 0000 0000 0000 0000 0000 0000 0000 55aa  ..............U.

%!xxd -r
x! sblk_m.bin

Write back to the partition
ATTENTION: Make sure that the first 512 bytes of modified file are the same as unmodified. (excepting the serial number)
dd if=sblk_m.bin of=/dev/sdb1 bs=512 count=1
Check UUID of the partition
blkid /dev/sdb1

Wednesday, September 19, 2012

Disable / Bring HDD into Sleep State in Linux


When booting Linux from external device you may want to disable internal hard disk drive, you can disable your hard drive without removing your hard drive from your laptop/desktop PC.

Disable / Hot unplug SATA Drive
echo 1 > /sys/block/sda/device/delete

Enable / Scan SATA Devices
echo "- - -" > /sys/class/scsi_host/host0/scan
 PS: The hard disk drive may automatically power down when you disable it.


If you want to put your hard drive into standby / sleep state to save power, and it can automatically weak up when the system try to access it. You may try to use hdparm.

Put HDD into standby state
hdparm -y /dev/sda

Put HDD into sleep state
hdparm -Y /dev/sda

ATTENTION: When using hdparm command to put drive into standby/sleep state, the SATA drive may weak up if your system try to access this drive.

Friday, September 14, 2012

PERL Regular Expression

Modifiers
m      Matching
m/.../

s        Substitution
s/.../.../

i        Do case-insensitive pattern matching.
.../i

g       Global matching.
.../g

Meta-characters

           \        Quote the next metacharacter
           ^       Match the beginning of the line
           .        Match any character (except newline)
           $       Match the end of the line (or before newline at the end)
           |        Alternation
           ()      Grouping
           []      Bracketed Character class

Logical
           [^a-z]     No lowercase letters
            abc|def  abc or def

Sequence
         \w       Match a "word" character (alphanumeric plus "_",
                    plus other connector punctuation chars plus Unicode marks)
         \W      Match a non-"word" character
         \s        Match a whitespace character
         \S       Match a non-whitespace character
         \d       Match a decimal digit character
         \D      Match a non-digit character

Quantifiers
           *           Match 0 or more times
           +           Match 1 or more times
           ?           Match 1 or 0 times
           {n}       Match exactly n times
           {n,}      Match at least n times
           {n,m}   Match at least n but not more than m times


Examples
           s/^([^ ]*) *([^ ]*)/$2 $1/;     # swap first two words
           (ha)+     #Either ha or haha or hahaha or ...
           (eg|le)gs     #Either eggs or legs

Wednesday, September 5, 2012

Altera USB Blaster Linux

Enable USB Blaster for User Access
vi /etc/udev/rules.d/51-usbblaster.rules

# USB-Blaster
BUS=="usb", SYSFS{idVendor}=="09fb", SYSFS{idProduct}=="6001", MODE="0666"
BUS=="usb", SYSFS{idVendor}=="09fb", SYSFS{idProduct}=="6002", MODE="0666"
BUS=="usb", SYSFS{idVendor}=="09fb", SYSFS{idProduct}=="6003", MODE="0666"

# # USB-Blaster II

BUS=="usb", SYSFS{idVendor}=="09fb", SYSFS{idProduct}=="6010", MODE="0666"
BUS=="usb", SYSFS{idVendor}=="09fb", SYSFS{idProduct}=="6810", MODE="0666"

Reload Rules for udev
 udevadm control --reload-rules

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"

Tuesday, July 31, 2012

Linux Create Ad-Hoc Network

Create ad-hoc network
    iwconfig wlan0 mode ad-hoc essid "yourssid"

Specify IP
    ifconfig wlan0 10.0.0.1 netmask 255.0.0.0 up

Monday, July 30, 2012

Linux Ethernet Bonding

Set NICs as slaves
    vi /etc/sysconfig/network-scripts/ifcfg-eth0

    DEVICE=eth0
    USERCTL=no
    ONBOOT=yes
    MASTER=bond0
    SLAVE=yes
    BOOTPROTO=none


    vi /etc/sysconfig/network-scripts/ifcfg-eth1

    DEVICE=eth1
    USERCTL=no
    ONBOOT=yes
    MASTER=bond0
    SLAVE=yes
    BOOTPROTO=none


    vi /etc/sysconfig/network-scripts/ifcfg-bond0

    DEVICE=bond0
    IPADDR=192.168.0.1
    NETMASK=255.255.255.0
    ONBOOT=yes
    BOOTPROTO=none
    USERCTL=no


    vi /etc/modprobe.conf

   alias bond0 bonding
   option bond0 miimon=100 mode=3



mode Description
balance-rr or 0  Round-robin policy: Transmit packets in sequential order from the first available slave through the last. This mode provides load balancing and fault tolerance.

active-backup or 1  Active-backup policy: Only one slave in the bond is active. A different slave becomes active if, and only if, the active slave fails. The bond's MAC address is externally visible on only one port (network adapter) to avoid confusing the switch.

balance-xor or 2  XOR policy: Transmit based on the selected transmit hash policy. The default policy is a simple

broadcast or 3
Broadcast policy: transmits everything on all slave interfaces. This mode provides fault tolerance.
802.3ad or 4
IEEE 802.3ad Dynamic link aggregation. Creates aggregation groups that share the same speed and duplex settings. Utilizes all slaves in the active aggregator according to the 802.3ad specification.

balance-tlb or 5 Adaptive transmit load balancing: channel bonding that does not require any special switch support. The outgoing traffic is distributed according to the current load (computed relative to the speed) on each slave. Incoming traffic is received by the current slave. If the receiving slave fails, another slave takes over the MAC address of the failed receiving slave.

balance-alb or 6  Adaptive load balancing: includes balance-tlb plus receive load balancing (rlb) for IPV4 traffic, and does not require any special switch support. The receive load balancing is achieved by ARP negotiation.

Create Custom Linux Initrd

Extract from Initrd
gunzip < initrd.img | cpio -i --make-directories

Create Initrd
find ./ | cpio -H newc -o > initrd.cpio
gzip initrd.cpio
mv initrd.cpio.gz initrd.img




Create Initrd from System Folder
mkinitrd /boot/initrd.$(uname -r).img $(uname -r) 

Install Chewing Input Method

yum install ibus ibus-chewing

Start Second X Window

startx -- :1

Windows and Linux Spoof MAC Address

Linux
    ifconfig eth0 hw ether 00:11:22:33:44:55

Windows 

Registry
[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\0008]

"NetworkAddress"="001122334455"
or GUI
Start/Settings/Network Connections/Local Area Connection
Properties/Configure ...

Advanced




Advanced (Another example)