Tuesday Feb 09, 2010

Retrieving MAC Address in Solaris using C as a non-root user

I needed to find a way to get the physical (MAC) address using C. From what I could gather from searching opensolaris.org, there are two methods for retrieving it: libdlpi and arp. libdlpi is the more elegant solution as it requires a simple call to dlpi_get_physaddr(). This is how ifconfig prints your network interface's MAC address. Unfortunately, libdlpi calls are only permitted as root.

As explained by James Carlson:

The reason it was like this was historical: getting the MAC address in
ifconfig meant opening up the DLPI node and talking to the driver. As
the drivers didn't have discrete privileges for each operation, and
you had to be almighty root to touch them, 'ifconfig' didn't show the
MAC address when not privileged.

The second solution is to use arp. In Solaris you can determine the physical address by looking at the arp tables directly (`arp -a | grep <INTERFACE>` or `netstat -p | grep <INTERFACE>`). With C, this can be done by using the if sockets and arp libraries.

I wrote up a solution called "getmac" using both methods. You can gather it here.

  • Directions
    $ wget http://www.pauliesworld.org/project/getmac.c
    $ gcc getmac.c -o getmac -lsocket -ldlpi
    $ ./getmac <interface_name>
    arp:	ffffffffffff
    dlpi:	dlpi failure, are you root?
    $ pfexec ./getmac <interface_name>
    arp:	ffffffffffff
    dlpi:	ffffffffffff
Remember to use pfexec for the libdlpi method.

Wednesday Feb 03, 2010

Compiling libupnp on Solaris

libupnp 1.6.6 is a little tricky to compile on Solaris. After downloading the source from Source Forge, you will want to extract the bzip2 and cd to the libupnp-1.6.6 directory, then do the following.
vi upnp/src/api/upnpapi.c
On line 59, there is a bug. Change
#if defined(_sun)
#if defined(__sun)
The change is adding an extra underscore. Otherwise sockio.h will not be recognized properly and you will get some missing networking variables when you try to build. After that is taken care of...
$ ./configure CFLAGS="-DSPARC_SOLARIS" --disable-samples
$ gmake
# gmake install

Thursday Jan 21, 2010

OpenSolaris + Fit-PC2 + Mediasonic Pro Box 4 Bay Enclosure

After the failure of the SATA and USB ports on my Intel D945GCL Atom board, I decided to build out a new file server. Sticking to the Atom theme, I decided to go small and get the CompuLab FIT-PC2. This little toy uses the Z530 1.6Ghz CPU that apparently uses only 6 watts of power. I'm assuming that means *without* a hard drive installed.

Measuring in at around 115 x 101 x 27mm (~ 4.5"x4.0"x1.0"), it is only big enough to hold one laptop sized 2.5" SATA drive.

The drive I installed only has 80GB of space. That would run out real quick with my needs, so I decided to get a MediaSonic USB disk enclosure to link up with my server. It can hold up to 4 SATA drives.

The PC sits on top of the enclosure on my bookshelf taking up 8.5" x 5.0" x 6.5" amount of space. This is not only power efficient, but space efficient since I am using 4 x 1TB drives. 4TB total (theoretical), ~2.6TB in a ZFS raidz. If I were to have purchased the 2TB drives, it would be even better.

Doug's blog on the FIT-PC2 gives a good overview on the features of the device and what works. There is no wifi driver and Xorg doesn't work, so you may want to install OpenSolaris on another machine before installing the internal HDD. My server is headless and uses the built-in gigabit ethernet, so I don't care about those issues.

Links and prices Total = $748

Monday Dec 14, 2009

Capture Live Webcam Images and Create Time-Lapse Videos with Linksys WVC54GCA

On occasion I get an e-mail asking me about my live webcam. What model is it? Does it support FTP/SSH? How does it automatically upload to your site? And so on...

Some answers:

Model: Linksys WVC54GCA Wireless G Home Monitoring Camera
FTP/SSH support: Nope

Capturing Live Images

So how do I automate taking pictures with the thing? Well, a hidden feature to the camera is to navigate to the static picture site of the webcam. For my example, I use DNS to map my camera hostname garfunkel to its IP address. You may want to enter whatever IP your camera uses instead of garfunkel.
To grab a picture at any given time, use wget.
wget http://garfunkel/img/snapshot.cgi?size=3\\&quality=1 -O webcam.jpg
If you have a UNIX-based or UNIX-like OS, you can schedule a cron job to grab this image as often as you like. I prefer capturing once every half hour, so I use this template:
\*/30 \* \* \* \* /path/to/wget/script
Now, if you have your own web page, you can use ftp/sftp/scp to upload the picture to your server using one script.

Creating Time-Lapse Videos

If you can capture a picture once every minute, you can create a pretty neat time-lapse video every day. This can be accomplished using wget and ffmpeg. For my camera, I point it outside, so it's only worth capturing between certain hours, say 6am to 6pm. After 6pm, I will have created over 700 pictures. I can then use ffmpeg to stitch them together to form a short video on the day's weather. It's a little complicated to discuss every detail behind this, so I'll just post the bourne script I use.

hour=`date +%H`
captime=`date +%H%M`
img=`ls ${dirpath}\*jpg | wc -l`;
expr=`expr 1 + $img`

	# File format will be Month.Day.Year.flv (flv for flash)
	date=`date +%m.%d.%y.flv`

	# ffmpeg reads in each image and incrementally makes a flash video at
	# 16 fps
	cd ${dirpath}
	ffmpeg -i %04d.jpg -r 16 ${dirpath}${date}

        # Cleanup, upload time-lapse to server and remove all jpg files
	# scp user@host:location
	# rm ${dirpath}\*jpg

        # ffmpeg expects pictures in the format 0001.jpg ... 0001.jpg so
        # we need to add a fluff of zeros to make each pic 4 digits long

	if [ $expr -lt 10 ]

	elif [ $expr -lt 100 ]

	elif [ $expr -lt 1000 ]

        wget http://garfunkel/img/snapshot.cgi?size=3\\&quality=1 --output-document=${dirpath}${expr}.jpg

case "$hour" in
# Eliminate the hours of the day that are too dark to capture
# If it is 6:00pm (18), time to make a video
	if [ $captime -eq 1800 ]
# Every other hour is assumed to have light, so take a pic
You might want to change the dirpath variable to wherever you want to store the pictures. Finally, add a cron entry to run every minute:
\* \* \* \* \* /path/to/timelapse/script
After 6pm, you will have an flv file. This is a flash file that can be played by various flash players for viewing on the web. You can change the file format to avi or mpg instead of flv if you just want to view it on your computer.

Sample video from my camera

Wednesday Dec 09, 2009

Can't use NumPad in OpenSolaris?

Today I thought I broke the numpad on my keyboard; none of then numbers were working. Switching on and off the Num Lock key didn't work. However, anytime I hit a number and held the key down, the mouse cursor would move across the screen. By some sort of black magic, I found a secret keyboard combo to turn on Mouse control via keypad.

Solution to turn it off
System ⇾ Preferences ⇾ Assistive Technologies ⇾ Keyboard Accessibility ⇾ Mouse Keys

Uncheck 'Pointer can be controlled using the keypad'
I must admit, however, this can be a handy feature when working with a mouse-less machine.

Wednesday Nov 25, 2009

Latex for OpenSolaris

Ever wanted to experiment with latex to write a paper or redesign your resume, but unsure how to install a latex package or compose a latex document? I'll try to explain it simply using OpenSolaris.

Install Latex

d its dependencies from our friends at sunfreeware.com
cd /tmp
wget ftp://ftp.sunfreeware.com/pub/freeware/intel/10/libgcc-3.3-sol10-intel-local.gz
wget ftp://ftp.sunfreeware.com/pub/freeware/intel/10/libiconv-1.11-sol10-x86-local.gz
wget ftp://ftp.sunfreeware.com/pub/freeware/intel/10/ncurses-5.6-sol10-x86-local.gz
wget ftp://ftp.sunfreeware.com/pub/freeware/intel/10/tetex-3.0-sol10-x86-local.gz
gzip -d \*.gz
  • Install the packages
  • pfexec pkgadd -d libgcc-3.3-sol10-intel-local
    pfexec pkgadd -d ncurses-5.6-sol10-x86-local
    pfexec pkgadd -d libiconv-1.11-sol10-x86-local
    pfexec pkgadd -d tetex-3.0-sol10-x86-local

    Play with Latex
    For my simple uses of latex, I use two binaries to compose my documents: latex and dvipdf. Latex requires a tex file to generate a document. For this example, I will use my favorite resume template created by David Grant.
    cd /tmp
    wget http://www.davidgrant.ca/sites/www.davidgrant.ca/files/resume.tex.txt
    wget http://www.davidgrant.ca/sites/www.davidgrant.ca/files/shading.sty.txt
    mv resume.tex.txt resume.tex
    mv shading.sty.txt shading.sty
    Now, let's use the binaries I mentioned earlier to create a pdf file.
    /usr/local/teTeX/bin/i386-pc-solaris2.10/latex resume.tex
    /usr/local/teTeX/bin/i386-pc-solaris2.10/dvipdft resume.dvi
    You should find a pdf file in /tmp called resume.pdf. View it with acroread or evince to get an idea of how awesome latex is. I won't go into too much detail on how to create the resume.tex file, but viewing and editing it will you give you a good understanding on its syntax. This is David's resume that is generated: http://www.davidgrant.ca/sites/www.davidgrant.ca/files/resume.pdf.

    Tuesday Sep 29, 2009

    Compiling Alpine on OpenSolaris 2009.06

    For Alpine 2.00, the configure file looks around in the wrong directories for the OpenSSL header files.
          if test -d /usr/sfw/include/openssl ; then
          elif test -d /opt/csw/include/openssl ; then
            if test -d /opt/csw/ssl/certs ; then
    Unfortunately, configure never looks for the new location: /usr/include/openssl. Thankfully, there is an easy fix:
    ./configure --with-ssl-include-dir=/usr/include/openssl

    Wednesday May 20, 2009

    Cannot remove directory: File exists

    If you try to delete a directory over NFS and get an error such as...
    rm: cannot remove directory `asdf/': File exists
    ... you will notice that conventional methods won't be good enough to remove it.
    $ chmod -R 777 asdf/
    $ rm -rf asdf/
    $ rm: cannot remove directory `asdf/': File exists
    Usually there is a file like .nfs234B inside the directory than can be displayed with ls -la
    $ ls -la
    total 146
    drwxr-xr-x 2 user group    512 2009-05-20 08:51 .
    drwxr-xr-x 3 user group    512 2009-05-19 08:59 ..
    -rwxr-xr-x 1 user group 134888 2009-05-18 12:45 .nfs234B
    Removing this file only replaces it with another. There are two solutions: manually delete the file on the NFS server, or (if you don't have that type of access) kill its process.
    $fuser -u asdf/.\*
    asdf/.:     9070c(user)
    asdf/..:    28690c(user)   24845c(user)
    asdf/.nfs934B:     9070tm(user)
    9070 is the offending process. Kill it with fire.
    $ kill -9 9070
    Should be able to remove that directory now.

    Monday Jan 05, 2009

    Testing Network Performance in Solaris

    So you think your wireless link is slow, or your gigabit ethernet card is under performing. How can you tell? Here are two tools I use to test network throughput.


    Created by Kai Uwe Rommel, it tests by sending and receiving packets of varying sizes and reports throughput in kilobytes per second.

  • x86
  • # wget ftp://ftp.sunfreeware.com/pub/freeware/intel/10/netio-1.26-sol10-x86-local.gz
    # gzip -d netio-1.26-sol10-x86-local.gz
    # pkgadd -d netio-1.26-sol10-x86-local
  • # wget ftp://ftp.sunfreeware.com/pub/freeware/sparc/10/netio-1.26-sol10-sparc-local.gz
    # gzip -d netio-1.26-sol10-sparc-local.gz
    # pkgadd -d netio-1.26-sol10-sparc-local
  • Server-side
  • # netio -u -s
  • Client-side
  • # netio -u SERVER_IP_ADDRESS

    Here are results on a 100Mbps link:
    UDP connection established.
    Packet size  1k bytes:  11913 KByte/s (0%) Tx,  11468 KByte/s (0%) Rx.
    Packet size  2k bytes:  11954 KByte/s (0%) Tx,  11509 KByte/s (0%) Rx.
    Packet size  4k bytes:  12274 KByte/s (0%) Tx,  11687 KByte/s (0%) Rx.
    Packet size  8k bytes:  12284 KByte/s (0%) Tx,  11697 KByte/s (0%) Rx.
    Packet size 16k bytes:  12292 KByte/s (0%) Tx,  11702 KByte/s (0%) Rx.
    Packet size 32k bytes:  12348 KByte/s (0%) Tx,  11714 KByte/s (0%) Rx.
    Sending and receiving hovered around 11-12MB/s which is on par with 100Mbps.


    Created by Rick Jones and discovers the maximum throughput of a link, reporting in megabits per second.

    # wget ftp://ftp.netperf.org/netperf/netperf-2.4.4.tar.gz
    # tar zxvf netperf-2.4.4.tar.gz
    # cd netperf-2.4.4
    # export CFLAGS="-lsocket -lnsl -lkstat"
    # ./configure
    # make
    # make install
  • Server-side
  • # netserver
  • Client-side
  • # netperf -H SERVER_IP_ADDRESS

    Here are results on a 100Mbps link:
    Recv   Send    Send                          
    Socket Socket  Message  Elapsed              
    Size   Size    Size     Time     Throughput  
    bytes  bytes   bytes    secs.    10\^6bits/sec  
     49152  49152  49152    10.00      94.88

    94.88 Mbps is the final result, not bad.

    Thursday Dec 18, 2008

    OpenSolaris Installation Checklist

    When I reinstall OpenSolaris, I have a small checklist of things I execute to create a familiar environment between my systems. This might be helpful to others.

  • Allow NIS user (really any user) access to the root role (lets them access packagemanager)
  • # vi /etc/user_attr
    Append to end of file:
    USERNAME::::profiles=Primary Administrator;roles=root

  • Enable NTP
  • # vi /etc/inet/ntp.conf
    server 0.pool.ntp.org
    server 1.pool.ntp.org
    server 2.pool.ntp.org
    # svcadm enable ntp
    # svcadm restart ntp

  • Install OpenOffice
  • # pkg install openoffice

  • Install gconf-editor
  • # pkg install SUNWgnome-config-editor

  • Disable Audible Alert Sound
  • I hate the audible alert sound. If you wear headphones while you're computin' and use tab-completion excessively on your shell or have a mail client that beeps on every new message, you know what I'm talking about.
    However, when I click on System->Preferences->Sound, the Play Alert Sound option is grayed out. Here is the solution:
    $ gconf-editor
    /apps/metacity/general/ and decheck audible bell

  • Allow root user to login directly
  • Don't do this.
    # rolemod -K type=normal root

    Thursday Dec 11, 2008

    2008.11 on the Asus EEE 701

    Things that have been fixed/work out of the box since 2008.05

  • Keyboard/mouse now fully functional throughout the install
  • Sound works by default
  • Webcam *should* work by default (my model doesn't have one so I can't test)
  • Atheros wi-fi still requires an additional download
    $ wget http://opensolaris.org/os/community/laptop/downloads/ath-x86-ar5007eg-pkg.tar.gz
    $ tar zxvf ath-x86-ar5007eg-pkg.tar.gz
    # pkgadd -d SUNWatheros
    # init 6
  • Attansic L1/L2 ethernet is now functional thanks to our good friend Masayuki Murayama
    $ wget http://homepage2.nifty.com/mrym3/taiyodo/atge-2.6.2.tar.gz
    $ tar zxvf atge-2.6.2.tar.gz
    $ cd atge-2.6.2
    # make install
    # ./adddrv.sh
    # devfsadm -i atge0
    # ifconfig atge0 plumb

    Asus EEE 701 vs. 30 inch Dell Monitor
    800x480 vs. 2560x1600 resolution
    Longs Peak in the background
  • Monday Jul 28, 2008

    APC UPS on OpenSolaris (apcupsd)

    One of the best consumer UPS manufacturer on the market is APC. They make great products that can interface nicely with any UNIX-based system, whether it be Linux, BSD, or Solaris. Using their daemon (apcupsd), you can download the source and create a nice link-up with your UPS so you can be notified anytime your power goes out (or if you accidentally unplug it)

    My home model is the APC ES550 which is USB-based and has a weird USB->RJ45 cable to link a computer with the UPS. Let me explain how to get this running with OpenSolaris with at least this model.

    First you will need the latest apcupsd tarball located on Source Forge. Then, follow some standard compiling directions:

        $ tar zxvf apcupsd-3.14.4.tar.gz && cd apcupsd-3.14.4
        $ ./configure --enable-usb
        $ make
        # make install
    If your model is USB-based, you need the enable-usb flag with the configure script. Now when you make, however, it will most likely fail with the following error.
    libusb.h:9:34: /usr/sfw/include/usb.h: No such file or directory
    ... lots of errors ...

    'make' fails on OpenSolaris because by default it is missing a usb.h header file located in /usr/sfw/include. This can be retrieved by running this command inside that directory as root.

        # wget http://src.opensolaris.org/source/raw/sfw/usr/src/lib/libusb/inc/usb.h
    Binaries are stored inside /etc/opt/apcupsd/sbin.

    The file apcupsd.conf is needed to configure this USB device. Two lines that say the following must be included in this file.

    UPSCABLE usb
    UPSTYPE usb
    Delete any other UPSCABLE/UPSTYPE lines. The apcupsd.conf file documents other types of APC UPS devices as well, usually those that rely on serial as opposed to usb.

    Now, you should be able to run the daemon and you can verify that it will notify you by simply pulling the plug on the UPS. If you modify your root alias in /etc/mail/aliases the UPS will send you an e-mail when the power goes out. This sounds handy if I'm campus and I lose the electricity in my apartment, so I'll have plenty of time to power off my machine remotely.

    Friday Jul 25, 2008

    ZFS NAS with the Intel Atom

    I've been looking to build a network attached storage (NAS) device for some time to store all my music, photos, films, etc. in one global location. I had a few specific requirements that were as follows:

  • Supports two disks (2TB RAID mirroring preferable)
  • Supports UPnP server (media sharing)
  • Low power (always on)
  • Bonus points: ZFS, SSH, Samba, etc.

    There are a few ready-to-go NAS solutions available on the market that are compatible with my demands. The Linksys NAS200, at $130, supports two disks with Twonky Media server for UPnP and is low power. The downside is that reviews indicate it being very slow and it comes with no gigabit ethernet. On the high-end side, however, the QNAP TS-209 Pro also comes with the two disk slots, UPnP support, is fast, along with a bunch of extra goodies like samba and an Itunes server. The price tag at $400 makes it a bit too much for a diskless system, so I decided on a different solution... why not build it myself?

    I had a Thermaltake LANBOX Lite Mini-ITX/ATX case lying around along with a 200W power supply, so all I needed to do was find some cheap, low power hardware to support it. Intel recently released a new type of processor called the Atom, which is aimed at bringing x86 into the embedded market. I'm not so sure how successful they will be at this venture, but it fits my needs perfectly. According to their specs, it draws 2W TDP for the 1.6ghz version which is pretty amazing. The power output turns out to be a bit of marketing hype, but considering it is now being used in the ASUS Eee desktop and laptops, it should prove to be a viable candidate. To encourage the hobbyist market, Intel created a combo of motherboard + Atom (BOXD945GCLF) that is available on Newegg for $75. After getting it along with a pair of 1TB drives at $170, I had a workable system shipped to me for under $500.

    My first reaction when I got all the parts is that the Intel board is TINY. I can fit my hand around entire thing. Even in the media center case that I use, it has quite a bit of extra room.

    The LANBOX Lite is fully modular which makes installation much easier. If you have ever built a computer from scratch, then you probably understand how tedious it can be to screw in motherboards, install drives, etc. This case allows you to pull every section out to make installation a breeze. It also comes with a nice silo to install both the terabyte drives.

    The next step is to turn this into a fully functional NAS. After installing FreeBSD 7.0, I wanted to setup both my disks to mirror. Since the BSD family has such a friendly license, ZFS is included in the distribution. And ZFS mirroring makes things incredibly simple to setup.
    [root@bojangles ~]# zpool attach tank ad4s1d ad6s1d
    And that's it! Now to ensure that things went as expected.
    [root@bojangles ~]# zpool list
    NAME                    SIZE    USED   AVAIL    CAP  HEALTH     ALTROOT
    tank                    928G   72.9G    855G     7%  ONLINE     -
    [root@bojangles ~]# zpool status
      pool: tank
     state: ONLINE
     scrub: none requested
    	tank        ONLINE       0     0     0
    	  mirror    ONLINE       0     0     0
    	    ad4s1d  ONLINE       0     0     0
    	    ad6s1d  ONLINE       0     0     0
    errors: No known data errors
    Now with ports, I can get Samba and Ushare up and running in no time.
    [root@bojangles ~]# cd /usr/ports/net/samba3 && make install clean
    [root@bojangles ~]# cd /usr/ports/net/ushare && make install clean
    The final product in my closet in a makeshift cabinet.

  • About

    Hiya, my name is Paul Johnson and I'm a software engineer working on the ZFS storage appliance .


    « April 2014