Wednesday, September 3, 2014

Basic Raspberry Pi Hacks

I've put a fair amount of time into setting up Raspberry Pi images lately. Between work and fun, my son and I have four serious Pi projects on our immediate backlog:

  1. AirPi Tracker: using a cheap USB SDR dongle, track local flight paths. Why? I'm not interested in flight paths actually, just interested in Pi and using it with SDR. This project comes from the fantastic book "Raspberry Pi Hacks" (loaded with great ideas)
  2. PiGame: another project from Raspberry Pi Hacks, this sets up your Pi as an emulated game console for a series of classic games. Having three teen sons, I am thinking this one is going to be a hit.
  3. War Pi: this is the main project for the time being, creating a "war walking" 802.11 analysis device. With an Alpha wireless adapter and a GlobalSat BU-353S4 GPS, I can walk around client buildings and assess the location and signal strength of surrounding 802.11 access points (which pose the threat of employees connecting to external wireless points).
  4. Pi Fence: this project is in its infancy, but the idea is to build a Pi distro for home network security, including a firewall and an authenticated proxy (authenticated by user, not by device).
Fun projects - they all require a good basic Pi image. This article captures my lessons learned in creating that base image.

Base Pi Image

The great Pi community has created a number of Pi images (found here). They maintain and update these images often--this is the place to go to get that beginning image. I went with Raspbian for my current projects, simply because it's tried and true. For Pi Fence, I may go with a lighter OS, but for now this works.

Before you can really get started, you need to download your image. Not that difficult....

Next, you'll need get the image onto your SD card. An image isn't really a set of files, it's actually a bit-by-bit copy of a disk. You can't just drag and drop it onto an SD card and have a functioning Pi. You need to "image" it to the card.

Windows absolutely stinks at this. My Win8 machine is at the point where it can't even see a card with a Pi image on it - it's very frustrating. Luckily I do a lot of work in Linux (my personal project laptop runs Mint, and my workstation runs Ubuntu), so I have been able to work very efficiently with the SD card process. If you don't have a Linux machine, you could try running Linux in a Virtual Box virtual machine. If you have a USB SD card reader, you could (rather ironically) use a Pi to do all of this. That's what this blog post is all about - getting the image onto your card, upgrading it, customizing it, and then storing your personal image.

Step 1: Clear Your Card

You can skip this step if your card is brand-new and unformatted. Otherwise, you'll want to get rid of any file partitions and the likes which are present on your SD card. To do this, you'll want GParted (it can be done with the "fdisk" command, but it's cumbersome).

Boot GParted (it will require you to enter your password) and make sure the selected device is your SD card. It'll look roughly like this:


If the device in the upper right corner is /dev/sda, do NOT proceed. That's your computer's hard disk, and what you are about to do will completely nuke your hard drive. Select /dev/mmcblk0 or whatever is showing that's roughly the size of your SD card.

How do you know what your SD card is? Just after you insert your SD card, in a terminal window type the command dmesg. The last few lines of output will look like this:

[64576.492079] mmc0: card 0007 removed
[64580.605642] mmc0: new high speed SDHC card at address 0007
[64580.612048] mmcblk0: mmc0:0007 SD8GB 7.42 GiB 
[64580.616186]  mmcblk0: p1 p2
[64580.873309] FAT-fs (mmcblk0p1): Volume was not properly unmounted. Some data may be corrupt. Please run fsck.
[64581.005227] EXT4-fs (mmcblk0p2): recovery complete
[64581.005237] EXT4-fs (mmcblk0p2): mounted filesystem with ordered data mode. Opts: (null)
The third line of output is the path to the SD card, proceeded by /dev/. So in my case, /dev/mmcblk0.

If your card has already had a Pi build on it, it'll look like the one above. Select each partition (in my case, /dev/mmcblk0p1 and /dev/mmcblk0p2) and from the Partition menu, select "Delete". When you've selected all the partitions to delete, you have to click the "Apply all operations" button (the last button on the left). Otherwise, none of your changes actually occur.

NOTE: deleting a partition is permanent...

Step 2: Image Your Card

The next step is to get the bits from the Pi OS image onto your card. You'll use the dd command for this. From the command line, type:
sudo dd if="base_pi_image.img" of="/dev/mmcblk0"
Notes:
  • The "if" option is the source file. Enter the path to the image you have downloaded from the Raspberry OS downloads page.
  • The "of" option is the destination--in this case, enter whatever your SD card is
Now just wait. It looks like nothing is happening, but slowly, bit by bit, that 2.x GB image is being created on your SD card. Be patient.

Once the image has been copied, you are almost ready to boot your Pi--but not quite. One of the next steps will be to upgrade the base image to include the latest kernel updates and such (stuff that's not rolled back into the base image, but that you'll want). It's a long process to upgrade your Pi OS and I think it's worth doing before you create your personal Pi image. The problem is, the base Pi image is small and leaves little room to download and install updates, so you'll need to temporarily grow your Pi image just a bit.

Step 3: Grow Your Pi Image

You may have noticed that, when you used the dd command to image your SD card, there was a large chunk of unallocated space. Don't worry - you'll get to use that space eventually. For now, we just want to grab a little bit of it, so you can upgrade your Pi OS and then create your own, updated, base image. 
You can skip this step if you don't care about having an updated personal image lying around (with 4 projects going, I wanted to do this only once).

To grow your image,
  1. Open GParted again
  2. Click on the file system partition (usually /dev/mmcblk0p2 for me)
  3. From the "Partition" menu, select "Resize/Move"
  4. Increase the size by about 500 MB (just add 500 or so to the "New Size"

Easy as Pi. Sorry, bad joke... But now that you have some extra space, you will be able to boot your Pi and update/upgrade the image.

Step 4: Update and Upgrade

The nice thing about the Linux community is that there's just so much going on out there, and people take a lot of time to actually help each other. With your Pi image set, go ahead and boot up your Pi and get connected to the network (from a personal perspective, I prefer to use ethernet but wireless is fine). Open a terminal window and type the following command:

sudo apt-get update

This will cause your Pi to search the corners of the Internet looking for updates to the software packages installed in the base OS. 

Next, you're going to start a rather long process - the process of updating the kernel itself. In the terminal window, type

sudo apt-get upgrade -y

Now would be a good time to walk the dog, play with the kids, read a book, or chat with a loved one. This can take quite some time.

When that's done, the next command is

sudo apt-get clean

This command cleans off any temporary files left over from the update and upgrade processes.

Step 5: Make Any Other Changes

There are a number of additional changes you could make at this point:
  • Networking: you have already set up networking so you could get to the Internet, but if you want to make any changes that will be part of your base image, do so. For me, I disable the wireless network and I specify a static IP address so when my Pi boots, it's a known host on my internal LAN.
  • Firewall: you may want to enable a firewall at this point, or install anti-virus (Clam is a great AV tool for Linux)
  • Add or remove applications: if there are applications you know you won't need (perhaps Scratch or another programming application) you can remove those at this time. If there are other apps you need or want, install them.
  • Disable services: there may be services running which you don't need, and disabling them means you free up system resources. Go for it.
At this point, your Pi is as up to date as it gets, and it's time to create your personal base image. 

Step 6: Create Your Personal Base Image

In this step, you'll create a base image which you can use going forward. I'm a bit of a geek, so I actually perform an added step, which is reducing the size of the base image - it's not needed, I just like to do it. I learned it from a very helpful blogpost by xalixo on the Raspberry Pi forum.
  1. Back on your main computer, insert your SD card and use the dmesg command again to make sure you know how to reference it.
  2. If you want to shrink your image again, do the following
  • Open Gparted again
  • See how much space is used on the /dev/mmcblk0p2 partition and how much is free
  1. Next, you are going to use the dd command, but in this instance you'll reverse directions and copy from the SD card to a base image.
  2. In the terminal window type sudo dd if="/dev/mmcblk0" of="personal_pi_image.img"
  3. OPTIONAL If you want to shrink your image, you can take a chance, modify the dd command, and copy fewer bytes from your card. The dd command looks like this: sudo dd if="/dev/mmcblk0" of="personal_pi_image.img" bs=1M count=2700
  • Note: the value for count should be the desired size in MB of your final image. That's what you found in step 2. Add some free space just in case.
Now you have a base image that's updated, configured how you like it, and ready to use. If you have other SD cards (I have one for each project I'm working on), copy that image onto them using the dd command from step 2 above.

Storing Your Cards

I hate having clutter on my desk. I have a lot of clutter, but I do hate it. I poked around for a "lifehack" for storing SD cards and didn't find anything I liked. Then I stumbled across an Altoids Smalls tin and realized it's perfect! I took some foam and lined the tin (with a piece in the middle to separate stacks), and I used snippets of an Avery shipping label to identify the card:

There are some who will point out there's a risk of static discharge because I'm using a metal container. True. Images are easily recreated and cards are cheap so I'm not all that concerned - that attitude will likely change when my first card is zapped. Meanwhile I *am* looking for a non-metal alternative, but needed something short-term to help.

Conclusion

That's about it - you now have one or more base images which you can use to tweak however you need. Enjoy!

No comments:

Post a Comment