Friday, December 21, 2012

Raspberry PI and GPIO Quick Start

Simple Start to Using the GPIO Pins

The Raspberry Pi is a small single board computer aimed at the hobby and education markets. It comes in two flavours, model A and model B. Model A has only one USB port and no Ethernet jack, it retails at $25. Model B is the more popular board includes 2 USB ports and an Ethernet connection. It retails at $35. Both include the GPIO pins so this post will apply to them both, although you will need to do additional work to get the required libraries installed on the model A since you can't download them from the Internet.

Getting Started

I will be using Arch as my distribution of choice and will be programming in Python 3. If you are using Raspbian most of this will still apply, just some translation of the install commands will have to happen. After installing Arch and booting up your Pi you will need to install some software. To do that we will be using pacman.

As root:
pacman -S gcc python python-pip
pip install RPi.GPIO
Once that completes you can then write your test program. Create a new file named button.py and type in the program below.
#!/usr/bin/python

import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)

GPIO.setup(11, GPIO.IN)
count = 0

while True:
        if not GPIO.input(11):
                count += 1
                print ("button pushed", count)
                time.sleep(.2)
Let's walk through exactly what this program is doing. We import the needed libraries at the top, then we set the GPIO mode. The mode allows use to choose whether to  use the Raspberry Pi GPIO number scheme (GPIO.BOARD) or the BCM chip GPIO numbering scheme (GPIO.BCM). I have chosen to use the Raspberry Pi's scheme as it will hopefully be more future compatible. Traditionally when working with microprocessors the microchips numbering scheme is used. In this case we are using GPIO 17 so if we wanted to go that route we could just change the mode to be BCM and line 12 to look like:
 if not GPIO.input(17):
The rest of the program is simple. We set our pin to input mode and initialize a counter. It then goes to an infinite loop checking if the button is pushed. If it is it increments a counter and displays that count then it sleeps for a short time and does it again. The sleep is there to provide for some rudimentary button de-bounce and also to give you a chance to get your finger off the button before the counter is incremented another time.

Circuit Layout

The circuit we are going to build is simple when the button is pushed the LED will light and if the button.py program is running you should see the counter increment. When you get tired of this just hit control C to end the program.

Parts List:
  1. 10K Resistor
  2. 1K Resistor
  3. SPST NO momentary switch
  4. LED of some kind
The circuit should look like what is pictured below.

Remember to use the 3.3v output of the GPIO as the Pi is not 5v tolerant. To test your circuit turn on the Pi and push the button, if everything goes right you should see the LED light up.

Putting it all together

Once you have the LED lighting up it is time to test your program. To do this simply run button.py as root. Every time you push the button you should see the counter increment. This simple example should hopefully give you a good idea on how to get started using the GPIO pins on your Raspberry Pi.

Thursday, December 20, 2012

The $8 Web Server

Control Your World

In my last post I showed how to take an ATMEGA328 chip and add the necessary parts to make something that is Arduino compatible. The next building block is to interface your project to internet.

The heart of this project is the ENC28J60 chip by Microchip. It is a pretty much self contained Ethernet interface device, very few supporting components needed to add Ethernet to almost any project. While you can build the circuit from scratch on a breadboard a more economical way is to buy it pre-assembled from eBay.

Assembly of the parts

Once all of the parts are gathered up you will need to connect everything together.

ENC28J60 ModuleArduino Clone
CSD10
SID11
SOD12
SCKD13
RESETRESET
INTD2
VCC3V3
GNDGND

Loading the Library

The definitive source for the library is maintained on github, it is located at https://github.com/jcw/ethercard. Simply download the code and place it in your libraries directory and then restart the IDE so the files show up. Once that is done fire up one of the examples to check your work.

Wednesday, December 19, 2012

The $4 Arduino Clone

Along Comes Arduino

I have been tinkering with electronics ever since I was little. I got my first taste of micro-controllers using the PIC16C84. At the time the hobbyist electronics market was small and while programming the PIC was easy the learning curve was fairly steep. Along comes the Arduino and suddenly the barrier to entry is gone. Fast forward to present day, I got tired of leaving $30 worth of electronic parts for each project I wanted to do. So I decided to strip down the Arduino to only the most essential parts, so that I could harness the power of Arduino in my projects without feeling like I was throwing money away.

Parts List

PartQtyPrice
ATMEGA328-PU1$3.50
22pF Capacitor2$0.02
100nF Capacitor4$0.04
10µF Capacitor1$0.01
10KΩ Resistor1$0.01
1N4148 Diode1$0.01
16MHz Quartz Crystal1$0.10
6mm Tactile Switch1$0.04
Total:$3.73
CP21021$2.34
Grand Total:$6.07

This is all that is needed to build and program your own Arduino clone. The CP2102 is a USB to TTL converter it is used to program the chip. You only need one of these to program as many chips as you want. The CP2102 however does not work out of the box. You need to solder a male header pin to the DTR pin on the CP2102. More details can be found at http://blog.tarn-vedra.de/2011/09/using-cp2102-on-arduino.html

Assembly

Assembly is best done on a breadboard to start following the schematic below.
You will need to procure a USB A to A cable in order to program this.

Closing Thoughts

The CP2102 is an optional part if you already have an Arduino or some other means of programming the ATMEGA chip. This brings the cost down $3.73 per clone. There are cheaper ways using the ATtiny85 ($1.29) but sometimes you need a little more horsepower. Or maybe you want to build the $8 web server.

References:

  1. http://shrimping.it/blog/
  2. CP2102 Infomation

Tuesday, December 18, 2012

Using ZFS under CentOS

Using ZFS

In my last post I showed how to get ZFS installed on CentOS 6.3. This post will serve as a quick start guide.

Creating a ZFS file system

The first thing you will want to do is to create a zfs file system. The syntax is simple and straight forward.
zpool create store mirror /dev/sda /dev/sdb
The above command creates a mirrored ZFS file system and mounts it at /store. That is the simplest use case you have a mirrored device that provides some hardware failure resilience.

Getting more complex

Creating a simple mirror is fine and good but lets show a little more complex example.
zpool create store raidz /dev/sdc /dev/sdd /dev/sde spare /dev/sdf
The above command create a RAID5 like ZFS file system and mounts it at /store. It also includes a hot spare drive. Other options other than raidz include raidz2 and raidz3 giving you even more redundancy.

You may want to create other ZFS file systems under your /store to give additional control  (for example setting quota's).
zfs create store/user1
zfs set quota=10G store/user1
Some of the other cool things you can do is turn on NFS sharing, enable compression or enable deduplication. To learn more about the more advanced stuff you can do with ZFS I recommend reading the ZFS Administrators Guide.

ZFS on CentOS 6.3

ZFS on CentOS 6.3

ZFS was orignally devolped by Sun in the early 2000's for the Solaris operating system.

What is ZFS

From Wikipedaia:
ZFS is a combined file system and logical volume manager designed by Sun Microsystems. The features of ZFS include data integrity verification against data corruption modes, support for high storage capacities, integration of the concepts of filesystem and volume management, snapshots and copy-on-write clones, continuous integrity checking and automatic repair, RAID-Z and native NFSv4 ACLs. ZFS is implemented as open-source software, licensed under the Common Development and Distribution License (CDDL). The ZFS name was a trademark of Oracle[3] until September 20, 2011.[4]

Building ZFS

Due to license restrictions ZFS can not be distributed with the Linux kernel.  That however does not mean that you can't install in on your machine, you just have to do it yourself. The ZFS on Linux project has done almost all of the leg work for you. You just need to build a few RPM files and install them.

SPL

To get SPL installed you need to do three basic steps:
  1. Download the source from http://zfsonlinux.org/
  2. Build the source
  3. Install resulting RPM files
Downloading the source
As of this writing rc-14 is the latest release. You can download it like this:
wget http://archive.zfsonlinux.org/downloads/zfsonlinux/spl/spl-0.6.0-rc14.tar.gz
Building the source
Install your build enviroment:
yum groupinstall "Development Tools"
yum install zlib-devel libuuid-devel libblkid-devel libselinux-devel parted lsscsi
Unzip your source and go to the directory:
tar zxvf  spl-0.6.0-rc14.tar.gz
cd spl-0.6.0-rc14/
 Compile the source and make the RPM files:
./configure
make rpm
Installing the RPM files
Finally install the resulting RPM files:
yum install spl-*.x86_64.rpm

ZFS

To get ZFS installed the same basic steps as above are followed.

Downloading the source
Again using rc-14
cd ..
wget http://archive.zfsonlinux.org/downloads/zfsonlinux/zfs/zfs-0.6.0-rc14.tar.gz
Building the source
tar zxvf zfs-0.6.0-rc14.tar.gz
cd zfs-0.6.0-rc14
Compile the source and make the RPM files:
./configure
make rpm
Installing the RPM files
yum install zfs-*.x86_64.rpm

Final Thoughts

ZFS is a very powerful file system. If you are like me you may not want a development environment on your production servers, just copy the resulting RPM files to the server and install them like normal. One thing to note is that ZFS is not quite ready to use SELinux so you must disable it. That will change soon but for now it has to be off in order to mount properly. In the next post I will show a quick guide to using it.

Tuesday, July 17, 2012

Sheepdog on CentOS 6

I am in the process of trying to solve a problem at $WORK. The main issue is we try to keep our servers in a hardware rotation schedule. Things like iSCSI disk arrays are hard to work into that rotation, so I am hoping that sheepdog will serve as a way to aggregate the storage that we already purchase with each server giving us a fault tolerant storage that we can easily upgrade as we grow, or replace servers.


What is Sheepdog:

From the sheepdog wiki: Sheepdog is a distributed storage system for QEMU. It provides highly available block level storage volumes that can be attached to QEMU-based virtual machines. Sheepdog scales to several hundreds nodes, and supports advanced volume management features such as snapshot, cloning, and thin provisioning.

Technology Stack

We will be using CentOS as the host. We will then compile Corosync, Qemu and Sheepdog from source to get a working test cluster. My test set up consists of two Dell R300 servers. One thing to note is that I could not get this working in a KVM virtual machine and ended up just using the spare hardware I had available, so be aware of that limitation.

Getting started

I am assuming a few things:
  1. That you have SELinux set to disabled or permissive. Getting SELinux working with this set up is a future goal but for this simple proof of concept the added complexity/security is not needed.
  2. You are starting with a minimal install. This post will list out the required packages to install to be able to compile the programs
  3. You have opened up at least ports 5404-5405 udp (for corosync) and 7000 tcp (for sheepdog) in your firewall
  4. Corosync from the repository is not installed. It is too old to be of use for us.

Install the required packages:

 yum install automake make gcc git nss-devel zlib-devel  


Compile and install userspace_rcu:
 wget http://lttng.org/files/urcu/userspace-rcu-0.7.3.tar.bz2  
 bunzip2 userspace-rcu-0.7.3.tar.bz2 && tar userspace-rcu-0.7.3.tar  
 cd userspace-rcu-0.7.3  
 ./configure  
 make install   



Compile and install corosync:
 git clone git://github.com/corosync/corosync.git  
 cd corosync  
 git checkout -b flatiron origin/flatiron  
 ./autogen.sh  
 ./configure --enable-nss  
 sudo make install  


Compile and install sheepdog:
 git clone git://github.com/collie/sheepdog.git  
 cd sheepdog  
 ./autogen.sh  
 ./configure  
 sudo make install  


Compile and install qemu:
 git clone git://git.sv.gnu.org/qemu.git  
 cd qemu  
 ./configure  
 sudo make install  



 Configure Corosync:

Copy the configuration file provided by the sheepdog wiki. Change the
bindnetaddr to your network ip address. See the man file for more information on this if you are confused.

Start Up and Test:

Start up corosync on each host and look at /var/log/cluster/corosync.log and make sure there are no errors reported. If you are using ext3 or ext4 you need to make sure that your disk is mounted with user_xttr support:
 mount -o remount,user_xattr /  

Next start up sheepdog on each host and format the cluster:
 collie cluster format --copies=2  


Then make sure that the nodes see each other by issuing a collie node list, finally you can create a disk using qemu-img:
 qemu-img create sheepdog:Demo 8G  

Conclustion

You now have a fault tolerant cluster that you can painless scale out your storage as you grow. Before you start running this in production you will want to run some tests to see how it responds to a node going down and other possible scenarios, also turning on and setting up SELinux would be a good thing.

Tuesday, May 1, 2012

LinuxFest Northwest 2012

I attended LFNW this year in Bellingham Washington and gave a presentation on Salt. You can view my slides below:


Monday, January 2, 2012

Who Watches the Watchmen?

Like any good systems administrator I have systems in place that monitor what is going on with my servers and network. To do that I am using OpsView Community Edition. However one thing that has always been an issue is how do you make sure your monitoring solution is up and running? I solved that today with uptimerobot.com. I simply signed up for an account and 3 minutes later I had everything set so that I got an email if my OpsView box did not respond to pings. Simple, easy and best of all free.