Tuesday, June 17, 2008

Shell script functions to replace basename and dirname

When writing shell-scripts, it is quite common to use the basename and dirname. Since they are typically provided as shell scripts under /usr/bin/, I wrote my own set of the as Bash functions instead. Feel free to use them as you see fit.
function basename () {
    if [ $# -lt 1 ]; then
        echo "Usage: $0  [  ]" 1>&2
        return 1
        local -r sfx=${2-''}
        local -r trim=${1%/}
        local -r fname=${trim##*/}
        echo ${fname%%$sfx}
        return 0

function dirname () {
    if [ $# -lt 1 ]; then
        echo "Usage: $0 " 1>&2
        return 1
        local -r fname=${1%/?*}
        if [ "x$fname" = "x$1" ]; then
            echo "."
            echo $fname
        return 0

Sunday, January 14, 2007

Setting static IP for a small home network

I have an US Robotics router (USR805461), which is great in many ways except one: there is no MAC-based assignment of IP:s using DHCP... and the port forwarding (and every other feature of the router, with a few exceptions) is based on using IP addresses. So, in order to get port forwarding to work, I need to assign a static IP to the computer. No big deal, I thought, but there were some complications resulting from bad descriptions that could have been avoided if I hadn't been trying to do it to fast.

Since I previously used DHCP, the computer was already configured for using this. It was easy enough to change the IP address to for the nge0 interface using the ifconfig(1M) command:

ifconfig nge0 netmask
Then I wanted to make it persistent over reboots, and that's where I had a bit of trouble. After unsuccessfully following some instructions in various articles, I did the following to assign a static IP address to the computer.
  1. Remove the file /etc/dhcp.nge0. This will prevent dhcpagent(1M) from requesting an IP address using DHCP.
  2. Add the following line to /etc/hostname.nge0:
    netmask broadcast + up
    It is important so separate the text into two lines, and it is important to add the up last, since otherwise the interface will not be brought up.
  3. Add the following line to the /etc/gateways and /etc/defaultrouter file:
    I don't know if it is necessary to add it to the /etc/defaultrouter file, but it seems like a good idea.
Now I want to give the computer the name "capulet", which means adding lines in the files /etc/inet/ipnodes and /etc/inet/hosts, but since DHCP has been adding info there already, I needed to reboot the machine before making the changes. It appears that I have to do this so that DHCP can remove the entries in an orderly fashion (I tried to make the changes below before rebooting the machine, but they disappeared during the reboot). So, the next step is:
  1. Reboot the machine:
    $ reboot -- -r
  2. After that, I added the following line to /etc/inet/ipnodes and /etc/inet/hosts: capulet
  3. ... and last I rebooted the machine a second time to check that all worked.
Now all works find, except that the router seems to have problems with serving DNS queries: the router "hangs" after running a while, and the only way I have managed to get it back in shape is by rebooting the router. I'm either going to make the Solaris machine be the DNS server for the network (while could be a problem since it means keeping it up at all times), or at least configure it to use a DNS server that actually works.

Friday, November 10, 2006

Installing Solaris 10

In order to be able to do some performance measuring and improve performance, I decided to install Solaris 10 to be able to get access to the excellent DTrace utility. If you don't know what it is or what it can do, there is an excellent article about DTrace in The February issue of ACM Queue.

I haven't really been using Solaris for a long time, so I thought it would be great fun to try out Solaris 10 and see what happend in the last years. Never could I imagine that it should have advanced this much! (To be honest, it was quite a while ago. I remember that when we upgraded the workstations 4 to 8 MiB memory, they became blindingly fast and that we were complaining that Solaris was too big because it occupied 2 MiB memory.)

This is going to be really fun!

For the hardware interested: the computer that Solaris was installed in is a AMD 64 X2 4200+ (dual-core, AM2 socket) with 2 GiB 553 Mhz DDR-2 memory. It is built around the ASUS M2NPV-MX motherboard, with a 250 GiB Maxtor Maxline S-ATA disk, giving me good performance and plenty of room for working. It's got a Gigabit Ethernet as well, far beyond what I need to saturate my WLAN.

Installation was straightforward. I downloaded the five CD images from the Downloads Page at OpenSolaris and created CDs from those (there is a DVD images there as well, but I had a big stack of blank CDs within reach, so I chose that). I installed the SUNWonbld package as well (since it's in the instructions in the README file), but I really don't see what it is needed for unless you're building and installing from the source. I'm keeping it around, since it might come in handy later.

The initial installation is quite basic, so there were a few things that needed to be fixed. First of all, there are none of the usual tools that I want (such as gcc, gtar, apache, bzip2, etc.), so I downloaded and installed the Solaris Software Companion DVD. After all that is done, it is necessary to set up the path correctly in /etc/profile. First of all, I don't like to keep . in the path, so I'm setting it up explicitly to contain only what I wanted to be there. Since I wanted to set up the manual path as well, I wrote this little function for use inside /etc/profile:

SetupSoftwareDirs () {
  for D do
    [ -d $D/man ] && MANPATH=$MANPATH${MANPATH:+:}$D/man
    [ -d $D/bin ] && PATH=$PATH${PATH:+:}$D/bin

# Example usage
SetupSoftwareDirs /usr/local /opt/sfw /usr/share /usr
Don't forget to unset when cleaning up at the end of /etc/profile.

Since I'm using a DSL router and have my IP assigned (and regularly reassigned) via DHCP, I needed to install a Dynamic DNS client and create a service for it in order to be able to find the machine via DNS from my laptop. I'll write some more about that adventure next time.

The system works fine, except for one small issue that I don't know how to fix: when I log out on the main console, the mouse pointer disappears. I can still click on buttons, if I can find them, but the pointer is just invisible and does not reappear when I log in again. Anybody got an idea about how to fix it?

Here are some resources that I've discovered are quite handy: