Samuel Jacob's Weblog

Just another technical blog

Archive for the ‘Softwares’ Category

DIY – Wirless Router and NAS: Software Pieces

without comments

This is the followup post of DIY – RCN. Here I document about the different software used to make my RCN.

Operating System

linux-ubuntu There are two open source choices BSD(FreeBSD) or Linux(ubuntu). After few days of analysis I decided to go with Linux – because in my work I use FreeBSD. In either case I did not want to use FreeNAS or OpenFiler or any other ready made distro. Since I am familiar with Ubuntu, I decided to use the Ubuntu server version.

File System

Wanted to use ZFS on my main storage disk but it is not available on Linux yet, so decided to go with XFS. EXT3/4 on the boot disk because it is natively supported and no extra package needed. The boot media is 8GB flash disk.


Since there is no optical disk drive, installation should be through network or USB. Since most of the Linux distributions supports that I decided to use USB.

  1. Download Ubuntu 10.10 server
  2. Download Universal USB installer
  3. Create bootable install media using the installer
  4. Boot the system with boot media


Although no data is going to be stored in the boot media, it would be good to have separate partitions to store the config files and home directory. Otherwise re-installation would wipe out all the data.

I chose to create 5 partitions

/     - EXT4 - 2GB
/usr  - EXT4 - 2GB
/var  - EXT4 - 2GB
/home - EXT4 - 1GB
swap  -      - 1GB


Since this device will run headless only way to communicate with the system is through network interface. Having SSH access is good but still having a web interface for common administration access is better. Few Linux applications are available for that my choice is Webmin.

sudo vi /etc/apt/sources.list
sudo apt-key add jcameron-key.asc
sudo apt-get update
sudo apt-get install webmin

After this the machine can be controlled from local network – https://hostname:10000/


Shutting down the system should be easy. Since the storage is connected to the system it cant be power off directly. The file system data should be syncd first and using command line or web interface is not realistic. So programming the ATX power switch is the only way – acpid does that.

sudo apt-get install acpid


The goal was to create file based storage which is accessible from my home network. The NAS server should be big enough for at least next 2 years(1TB). It should be fast enough to view videos from it without flickering(64MB ondisk buffer). It should have hardware fault tolerance(RAID).

Although few of my desktop boards had RAID option in the BIOS menu, I never used it and never explored it. I thought RAID chipsets in a motherboard is equivalent to RAID controllers/adapters. It was one of the decideding factor I favoured for Gigabyte(GA-D425TUD) motherboard with JMicron RAID chipset over Intel(D525MO) motherboard.

After configuring RAID in the BIOS and starting Linux I realized it is not true raid. Because Linux recognized as fakeraid. In simple terms fakeraid is a firmware based RAID. That is all the work is still to be done in software yielding no performance benefit. Advantage of fakeraid is multiple OS which runs on same box can utiltize the same RAID. Since my setup wont have multiboot option, I dont want the fakeraid so decided to go with pure software RAID 0. Here is the steps to create software raid 0.

  1. Create software raid using multiple devices(md) interface.
  2. mdadm --create --verbose /dev/md0 --level=1 --raid-devices=2 /dev/sda /dev/sdb
  3. The above command will take some time (around 6 hours) because it needs to sync the contents of both disks.
    While it is doing that the status can be checked by using the following command.
  4. cat /proc/mdstat
  5. Then create a XFS file system on the md device
  6. mkfs.xfs /dev/md0
  7. Store the configuration
  8. <b>mdadm --detail --scan > /etc/mdadm/mdadm.conf
  9. Create mount point and add the mount information in the /etc/fstab
  10.      mkdir /mnt/raid
         echo "/dev/md0        /mnt/raid       xfs     defaults            1       2" >> /etc/fstab

Windows File Sharing

After this /mnt/raid can be made accessible to remote machines through either NFS or through Windows File Sharing. For Windows File Sharing samba service needed to installed. The following command installs samba server.
sudo apt-get install samba

After installing samba server it can be configured using webmin. Use webmin to configure samba “Servers”->”Samba File sharing”. Add the storage mount point here.


The routing functionality is very simple – handle all 3 interfaces with some limitations.

  • First interface eth0 is a Gigabit ethernet interface which is directly connected to the a desktop computer.
  • Second interface eth1 is a Fast ethernet interface which is directly connected to internet(connected to a ADSL modem).
  • Third interface is 802.11n wireless network.

Network and IP

All interfaces are in different networks. All interface should get static IPv4 address while booting up. This router should provide dynamic IP to the other machines.

Modify network interface and dhcp configurations

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
post-up iptables-restore < /etc/iptables.up.rules
up /etc/init.d/dhcp3-server start

#wireless network
auto wlan0
iface wlan0 inet static
up /etc/init.d/dhcp3-server start

#wan interface
auto eth1
iface eth1 inet static

[shell tab='/etc/dhcp3/dhcpd.conf']
subnet netmask {
option domain-name-servers,;
option routers;
option broadcast-address;
default-lease-time 600;
max-lease-time 7200;
subnet netmask {
option domain-name-servers,;
option routers;
option broadcast-address;
default-lease-time 600;
max-lease-time 7200;

Finally enable forwarding in Linux kernel by setting a system tunable.
[shell]echo 1 > /proc/sys/net/ipv4/ip_forward

To set it during boot modify /etc/sysctl.conf

NAT - Network Address Translation

NAT is required on eth1 to translate addresses on any outgoing packets and incoming packets. For the iptable rules should be set correctly, the following script does that.


#set default polices and flush
iptables -P INPUT ACCEPT
iptables -F INPUT
iptables -F OUTPUT
iptables -P FORWARD DROP
iptables -F FORWARD
#setup NAT
iptables -t nat -F
iptables -t nat -A POSTROUTING -o $EXTIF -j MASQUERADE

iptables -A FORWARD -i $EXTIF -o $INTIF -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -i $INTIF -o $EXTIF -j ACCEPT

iptables -A FORWARD -i $EXTIF -o $INTIF1 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -i $INTIF1 -o $EXTIF -j ACCEPT

iptables -A FORWARD -i $INTIF -o $INTIF1 -j ACCEPT
iptables -A FORWARD -i $INTIF1 -o $INTIF -j ACCEPT

#unblock certain services
iptables -A INPUT -p tcp -m tcp --dport 10000 -j ACCEPT


wifiNow it is time to setup the wireless interface. Assuming the wireless are drivers are present in the kernel.
The other tool required is hostapd. hostapd implements IEEE 802.11 access point management.
hostapd configuration





wpa_pairwise=TKIP CCMP

Written by samueldotj

March 12th, 2011 at 8:43 am

CodeLite – IDE for C

with 4 comments

For my C projects I was using Notepad++ as my editor/IDE for some time. Although notepad++ is good, it is lacking the integration with a code browser, source control and debugger. I came to know about a new IDE – CodeLitewhen I was searching for ctags for windows. It has integrated support for Subversion, ctags, gdb.

Since it is advertised to have multi-platform GUI, I was little reluctant to install and try(I have tried lot of multi-platform GUI applications and never satisfied with the look and feel of them). But wxWidgets makes the CodeLite to look like native windows application. CodeLite has lot of features and it looks very similar to Microsoft VC++ IDE.

Code browsing is very easy with CodeLite. It is has a very light explorer and also workspace view through which you can browse your project “easily”. You can switch between header and c file “easily” using the “Swap Header/implementation file” in the context menu. Context menus “Goto declaration” and “Goto Implementation” are handy to browse through code quickly.

If you are using cygwin and custom makefile – you can use a script as custom build command – see Notepad++ as IDE post for more details.

I use the following command to build(because I use gcc cross compiler)
c:\cygwin\bin\bash -l -c 'cd $ACE_ROOT/src;PATH=/usr/cross/i586-elf/bin/:$PATH:;make 2> err; sed -e "s@\(\/cygdrive\/\)\([a-zA-Z]\)@\2:@g" err; rm err’. 
And the following command to clean
c:\cygwin\bin\bash -l -c 'cd $ACE_ROOT/src;PATH=/usr/cross/i586-elf/bin/:$PATH:;make clean’

Codelite supports debugging through gdb; embedded and remote targets also supported. Since I am using a emulator to test my kernel, I have to use remote debugging in GDB to test my OS. Codelite integrates well with GDB remote debugging. I use the following GDB commands as startup gdb commands.

set print pretty on
directory src/kernel

symbol obj/kernel.sys
source img/gdb/general.gdb

source img/gdb/stack.gdb
source img/gdb/vm.gdb

If SVN is configured, using codelite you can see difference between two versions of a same file.
By default codelite uses the default diff viewer. 
It can be changed from Plugins menu -> Subversion->Options and setting the value appropriately. 
I use WinMerge to see difference of source files and have the following settings:
External Diff viewer: C:\Program Files\WinMerge\WinMergeU.exe
Arguments : /dl %bname /dr %mname %base %mine 

Another good thing about CodeLite is it is under development and your bug report/feature requests are solved within short time frame.

You can download CodeLite from It has installer for Windows and Linux also.

Written by samueldotj

September 21st, 2008 at 11:33 pm

Posted in C,Softwares,Windows

Notepad++ as IDE

without comments

I have been using Noetpad++ for more than 2 years. It is a wonderful editor – it is very light and feature rich. I use it for editing any text files (txt, ini, C, C++, html, css..). I was happy with Notepad++ for editing my C project source files. Only thing I didn’t like was, to compile I have to go to command prompt and then to correct errors I have to note down the line numbers and open notepad++ modify….

Yesterday, I started searching for a new notepad replacement cum IDE for my Ace(C on cygwin) project. Dev-Cpp and PSPad were IDEs I got. I downloaded and installed both of them. Here comes the problem, both are heavier than notepad++. I might have not explored well, both don’t have option to run cygwin make and capture output and process it.

So I decided to create a notepad++ plugin for my need. But before that wanted to try existing plug-ins and found NppExec. This plug-in allows to run command inside notepad++ and captures the output. It also opens a C file and goto line numbers if it was in the error output if you double click on a line containing “error:” or “warning:” string (I found this one accidently). Downloaded NppExec and ran “make” inside it; but got what I really expected. The output of cygwin “make” has relative path.

i386/io.c:19: error: `Port’ undeclared (first use in this function)
make[1]: *** [i386/io.o] Error 1
make: *** [kernel] Error 2

Since my project make works by invoking “make” inside each directory, relative path will be little complicated – so decided to use absolute path in the output of gcc warning and errors. So modified the explicit rule like following.

$(CC) -c $(abspath $I./include $(CFLAGS)

Out of topic – good make file

Now the output is like the following

/cygdrive/e/sam/Ace3/src/kernel/i386/io.c:19: error: `Port’ undeclared (first use in this function) make[1]: *** [i386/io.o] Error 1 make: *** [kernel] Error 2

This output was not enough because, if I double click this line, notepad tries to open c:/ /cygdrive/e/sam/Ace3/src/kernel/i386/io.c instead of e:/sam/Ace3/src/kernel/i386/io.c.

To solve this problem the I used the following script in notepad++ run command(hotkey – F6)

c:\cygwin\bin\bash -l -c ‘cd src;make 2> err; sed -e “s@\(\/cygdrive\/\)\([a-zA-Z]\)@\2:@g” err; rm err’

Sed help:

Explanation of this command:

c:\cygwin\bin\bash -l –c

Tells NppExec to execute bash and capture its ouput. –l means login and execute bash scripts and profile – I need this for setup my make environment. –c means execute the commands given as argument and exit. Each command is separated by semi-colon.

cd src; – needs no explanation – not needed may be in your case.

make 2> err; – run make and redirect errors to “err” file.

sed -e “s@\(\/cygdrive\/\)\([a-zA-Z]\)@\2:@g” err; – find the pattern /cygdrive/? And replace it with /cygdrive/?:

rm err – remove the error file.

If double clicking on the error/warning message is not opening the file, then try configuring NppExec’s console output filter by using the following %ABSFILE%:%LINE%: warning:* and %ABSFILE%:%LINE%: error:*

Now I am able to create, edit, compile and modify my project without leaving cygwin. Since I am using SVN no problem for source control also. 
Recently I switched to CodeLite and detail are here CodeLite is lightweight IDE.

Written by samueldotj

October 23rd, 2007 at 10:30 pm

Posted in Softwares,Tools,Windows