First bake of your Raspberry Pi

22 Sep

Introduction

This is a short tutorial on how to bring up your Raspberry Pi for the first time and make it run something simple and fun.

In order to know that we accomplished what we want to, lets set the goal first. At the end of this short tutorial, we should be able to

  • Boot Raspberry Pi using an OS image (server grade, not desktop grade)
  • Run a webserver and a light blogging software on Raspberry Pi
  • Run a few automated scripts that collects a few simple statistics on the system and present this information as regular blog post

What we need

In order to accomplish our simple goal, we need the following bill of materials

  • Wiring and powering
    • Raspberry Pi device
    • A MicroUSB power supply (The minimum recommended power is 700mA at 5v. Most of the current day devices should support that. I personally use one of the power supplies that came with either of these devices – HTC Android phone, Asus Nexus 7 Android tablet or LG Nexus 4 Andriod phone. In a nutshell – you may not have to buy a power supply, you can use a redundant one that is eating dust around.)
    • A HDMI-HDMI cable (I run my Raspberry Pi without a monitor. I temporarily connect it to my TV during first builds, to do any inital configuration. Rest of the time, I connect to the device using SSH sessions from other computers.)
    • A USB keyboard. Please use a very basic keyboard that doesn’t draw too much power. Finding one should be fairly easy.
    • A class-4 or better SD Card of size 4GB or more.
    • A network cable
  • Display and connectivity
    • A monitor or TV that can take HDMI input. If you have a monitor that supports DVI only, you may have to use a HDMI-DVI cable instead.
    • A router or switch port on your home or office network to connect the Raspberry Pi to. I recommend a DHCP enabled network port for ease of use
    • A computer with network connection and SD card slot to download the OS and build your SD card.

Getting to know your Pi

At this time, I strongly recommend going thru this quick start guide, so that you are familiar with the device’s ports and connections.

Installing the OS on SD Card

I would normally do it the old fashioned way – by downloading the Raspbian Wheezy OS image and keeping it for multiple installations. I use my Mac or Linux system to prepare the SD Card and dumping the OS image using dd command. You need to be very careful and shouldn’t overwrite the primary hard disk on your system while taking this approach.

If you prefer to do interactive installations, download NOOB software from here. The NOOB software is interactive and offers you a few choices while guiding thru the installation process.

The rest of the tutorial is based on Raspbian Wheezy OS. If you prefer to install any other OS, the instructions might slightly change.

Booting the Pi

  • Connect the HDMI cable to monitor or TV
  • Insert the USB keybaord into one of the USB ports
  • Insert the network cable and connect the other end to the switch port or router.
  • As a last step, insert the micro USB power supply. Note that there is no additional power switch to Pi. Once you insert the power cable, it starts booting.

Once the system boots to a login prompt, login with the default username and password ( pi / raspberry ). Check if you are connected to Internet. If you see any issues, you might want to refer to the Raspberry Pi Forum.

Scenario

Here is a very simple scenario we are envisioning to accomplish on this Raspberry Pi.

There are three users setup on the system: boat, pi and tiger. The boat user is the one that collects the metrics and reports. The metrics collected are:

  • How long the system is up, how many users are currently on the system and the load on the system
  • How many pi users are on the system
  • How many tiger users are on the system

The metrics are collected by the boat users once in about 10 minutes and each collection is reported as a blog post.

Additional Software

For accomplishing our scenario, we need

  • A webserver running on the system: Let us use Apache as our webserver. If you prefer, there are other choices like Nginx or Lightttpd. However, this tutorial covers only apache2
  • A blogging software: Blosxom is one of the simplest blogging software around. It works on flat files (no DB installation required) and the blogging process is very simple – add a new post in the form of a new file that has the text either in text or HTML format.

Blosxom works by reading files in a given directory and displaying those files as a blog. Each file is treated as a separate blog entry. The first line of the file is treated as title of the blog post. The timestamp of the file is attributed to the blog post as publish time.

This approach of Blosxom fits our bill – periodically check a few metrics on the system and report these metrics. All we need to do is to collect all the metrics in one file and the file gets displayed as a blog post by Blosxom.

Updating/Installing the packages

It is a good practice to update the lists and upgrade the packages. Use the following commands to udpate the packge lists and then upgrade the packages.

sudo apt-get update
sudo apt-get upgrade

Once you are done, your Raspberry Pi should be running the latest version of the packages installed on the system. Now, I would recommend installing Apache webserver.

sudo apt-get install apache2

Once you install apache2, make sure that you have Apache up and running. Execute the following command and then visit the Raspberry Pi device using a browser (e.g. 192.168.0.109).

sudo service apache2 start

Now you should be greeted with the popular and familiar “It works!” greeting.

Configuring the rest of it

Now, let us create the users boat and tiger.

sudo useradd -m -U -d /home/boat boat
sudo useradd -m -U -d /home/tiger tiger
sudo passwd boat
sudo passwd tiger

Let us now configure the directory for our Blosxom blog and get it ready. For simplicity, I suggest we make this directory to be owned by the boat user.

sudo mkdir /var/www/LifeOfPi
sudo chown -R boat.boat /var/www/LifeOfPi

Once you reach this point, lets install and configure the blogging software – Blosxom. Blosxom is downloadable from here. You must download the file to your desktop and then copy it to the Raspberry Pi (using SCP is what I recommend.)

Assuming that the file (with .tar.gz extension is downloaded and copied over to the home directory of the boat user, you can extract it and copy it over to the apache directory.

Let us start with becoming the boat user. For the rest of the tutorial, the commands are executed with the id of the boat user.

sudo su - boat

Extract the software and copy it over to the web server’s directory.

tar xzf blosxom-2.1.2.tar.gz 
cp blosxom-2.1.2/blosxom.cgi /var/www/LifeOfPi/index.cgi

Now view the file /var/www/LifeOfPi/index.cgi and make the following two changes (if you prefer, you can change more parameters and the code offers decent explanation of each of the parameters):

# What's this blog's title?
$blog_title = "Life Of This Pi";
# Where are this blog's entries kept?
$datadir = "/home/boat/blosxom/data/";

Now, create the directories for  scripts and data.

mkdir /home/boat/blosxom/data
mkdir /home/boat/scripts/

Create the script that collects sample data and dumps to the file. The script is created with the name /home/boat/scripts/whatismylife.sh

#!/bin/sh

myts=`date +%y-%m-%d-%H-%M-%S`
myfile="/home/boat/blosxom/data/post-${myts}.txt"
mydate=`date`
mypi=`who | grep "^pi" `
mynpi=`who | grep "^pi" | wc -l `
mytiger=`who | grep "^tiger" `
myntiger=`who | grep "^tiger" | wc -l `
myuptime=`uptime`

echo "What the boat says at [$mydate]\n" >> $myfile
echo "At [$mydate], I have $mynpi Pi-s and $myntiger Tiger-s.\n<br>" >> $myfile
echo "<br>At that very minute, my sailing status looks like this: \n<pre>\n" >> $myfile
echo "$myuptime" >> $myfile
echo "\n</pre>\nPi-s on the system: \n<pre>\n" >> $myfile
echo "[\n$mypi\n]" >> $myfile
echo "\n</pre>\nTiger-s on the system: \n<pre>\n" >> $myfile
echo "[\n$mytiger\n]" >> $myfile
echo "\n</pre>\n" >> $myfile

Make the script executable

chmod +x /home/boat/scripts/whatismylife.sh

For the boat user, add a crontab entry that looks like the following line, so that the script gets executed every 10 minutes.

*/10 * * * * /home/boat/scripts/whatismylife.sh

To start with, execute the script once manually.

/home/boat/scripts/whatismylife.sh

Now, you can see the blog running on your Raspberry Pi device by visiting the URL of the blog (if your Raspberry Pi’s IP address is 192.168.0.109, then the url would be http://192.168.0.109/LifeOfPi/ ).
Here is a sample screenshot from my Raspberry Pi running Blosxom.

RPi Screenshot

Hope you liked this tutorial and your Raspberry Pi. Happy Baking!

Bind, linux and resilience

11 Mar

Last month was a pleasant milestone for one of the servers I manage – the server is up for more than 1000 days and actively serving public DNS queries.

$ uptime
 19:52:37 up 1013 days,  2:44,  2 users,  load average: 0.00, 0.00, 0.00
$

The configurations on the device change on a weekly basis, the server serves a few thousand queries every hour. The queries served cover few tens of domains and reverse pointers for large IP blocks.

Given the amount of activity and the dynamism involved, this uptime shows the stability and resilience of the bind program and the underlying Ubuntu linux.

Despite being fully satisfied with bind, the latest one I am getting fascinated by is tiny-dns. Key take-aways from tiny-dns include ability to cache queries and being able to serve PTR/A records off the same configuration lines. Segmenting the responses based on originating (source) IP block is also quite simple in tiny-dns.

Hope to bring up a few production systems in tiny-dns to ensure that we have a choice.

Solaris diskspace calculation in Megabytes

04 Nov

Someone asked me how they can compute the diskspace (used/available) in Megabytes. The goal is to get the info in Megabytes only and report it, so that other programs can make use of it in automation.

As a backdrop – the df command in Solaris supports the -k switch, which computes the diskspace in kilobytes. Unlike linux, which supports switches for human readable formats (-h) and blocksize switch (-B), the df command in Solaris has limited number of switches.

Here is a small code snippet that can get you the file system utilization (for a given file system – in this case /) in Megabytes.

 

$ df -k / | tail -1 | awk ‘{ print $1, $2/1024, $3/1024, $4/1024, $5, $6 } ‘

rpool/ROOT/solaris-161 476612 230165 246197 49% /

$

 

The idea is to grab the kilobyte numbers and divide them by 1024. Depending on your need, this snippet can be further improved to display the output in various formats (like sizes in GB or drop some fields.)

 

Dennis Ritchie

14 Oct

For close to two and half decades of my programming life, the name Dennis Ritchie has become a synonym for simplicity, elegance, portability and efficiency.

My love for the C language started very casually and grew with time. Having been exposed to other programming languages like BASIC, Fortran and Pascal, it took me two full days to learn the syntax of the C language. But it took me several years to understand and effectively use the semantics of various aspects of the language. Once I started learning about the early C++ interpreters on Unix language and how object oriented semantics are implemented in C language (using compilers and preprocessors together), my love for the language and its innovators kept on increasing.

The journey with the Unix operating system became more of a life than love itself. Each variant or derivative of Unix I ever worked with, including HP-UX, SCO, UCB, Magnix, Linux and Solaris in particular, led to immense passion and respect towards the fundamentals behind Unix – all the qualities I attributed to Dennis in the first sentence above. These qualities are the reason why the applications and appliances built using the core and its paradigms touch us day in and day out, directly or indirectly. (For more on these qualities, read The Practice of Programming – here and here.)

Even though Dennis is no more today, his impact will still be felt for decades to come.

Quoting Dave Tong (@davetong) on twitter:

If Steve Jobs changed the world then Dennis Ritchie created it.

All I can say is a big thank you to Dennis Ritchie, for his silent yet powerful life. It changed our lives. RIP.

Upgrades to VirtualBox 4.x

02 Apr

I have been running VirtualBox 3.x on my personal Solaris Desktop as well as my work laptop. About 2-3 weeks ago, I upgraded to VirtualBox 4.x on my work laptop. Upgrade was simple and easy. I have seen considerable improvement of the way the VM Screens are handled in 4.x. The fullscreen mode is really fullscreen now and the Host Key functionality improved tremendously.

All these two weeks, I was thinking in the back of my mind about the Solaris version of the same upgrade, for my personal desktop. Didn’t spend much time on that till this morning though. This morning, downloaded VirtualBox 4.0.4 for Solaris and installed it. The installation was fairly straight forward: Uninstall it from the global zone and reinstall the new package. One notable fact though: The older version, during the uninstallation process, never checked if the application is running in the first place. It simply uninstalled, while my VirtualBox is running and a couple of VMs are in suspended state. During the install of the new version, it checked for the presence of running processes and aborted installation with the right error message. I had to stop the application and then reattempt the installation.

Having OpenSolaris 2009-06 (build 111b) installed on my system, I got a warning during installation about USB support for guests (which requires build 124 or later I guess). Other than that, the upgrade on OpenSolaris went on simply fine and I am back with all my VMs. I also installed a new instance of Ubuntu server on VirtualBox, just to test out the functionality. It is working fine (able to identify all the .vdis, grab all the previously uploaded disk images, etc.

 

Unix, Reboots and System Administration

04 Mar

Paul Venezia has written three good articles in the last few days and they are really worth reading. First, he talks about the nine traits of the Veteran Unix Admin, that summarized a bunch of characteristics of seasoned Unix admins. The ninth trait led to a bigger discussion on rebooting and how Unix boxes are commonly perceived as not to be rebooted right away. Then that opened up a much bigger discussion on the simplicity of rebooting in light of virtualization. Based on that, he wrote a nice article on the decline and fall of System Administration.

All these articles are good read for budding system administrators: not only the articles, but also the comments and discussion threads on these topics. They help understand various perspectives on these classic problems and help you make a judgment call based on the specific deployment and operational scenario you are working within. Have a nice time reading!

USB drive mount issue fixed

15 Dec

I had a few issues mounting a 1TB NTFS disk on OpenSolaris last week. After getting a few hints from one of the OpenSolaris support forums, I could get the issue fixed. All it needs is the FSWfsmisc and FSWpart to be installed on the box. The output of rmformat is very helpful to test the raw disk types, etc. The binaries installed as part of these FSW* packages like xlsmounts are very helpful.

One interesting observation is that the NTFS filesystem is mounted a little different on the local host.

root@agent007:~# xlsmounts
PHYSICAL DEVICE                 LOGICAL DEVICE      FS    PID         ADDR Mounted on
/dev/rdsk/c14t0d0p0            /dev/rdsk/c14t0d0p0    ntfs   5950  127.0.0.1:/ /mnt/test
root@agent007:~#

Need another weekend to drill down a bit into that…

Installed Solaris 2009.06 desktop

15 Jun

Installed Solaris 2009.06 on my personal desktop over the weekend. The installation is nice and smooth. One caution for users who use multi-boot environments: you must backup the menu.lst before you install. Just in case you need to manually edit. Read here.

The base OS doesn’t come with many utilities that you would like to have (e.g. OpenOffice) and you need to install them with Package Manager. I think the package manager interface improved a lot over a period of time. As of now, I started download of another 200MB of utilities. Work in progress.