Skip to content

Creating you own stage image

Magnus Lübeck edited this page Feb 8, 2014 · 5 revisions

Creating your own stage image

A stage image is what I call a raspbian image that is clean and set up to test the deployment of the software.

My preference is to have:

  • A fixed ip address, that is the same every time
  • The hostname is s1wire, so that I can see that I am on my staging system when I am logged in.
  • The default editor is set to vi, so that I can edit crontab easily
  • There is a "bootstrap.sh" script in the home directory, so that I can run predefined commands the first time I log in
  • My id_dsa.pub key is put into ~/.ssh/authorized_keys
  • A set of ssh keys are already put into the .ssh directory, so that I don't have to add a new key to github every time I reinstall a stage system

This is a cookbook recipe on how I do it:

Prereqs

This is what you need to create your own image:

  • A linux system, where you can edit the files
  • A plain vanilla image from raspberrypi.org, for example Raspbian

The image you download from raspberrypi.org is set up to suit most other people's needs, using DHCP to set the ip address, and such. The following instructions will help you set up your own image, so that you can re-install your test system at any time as quickly as possible.

NOTE: When you put this image on your SD card, it will erase all contents of the card. I, for example, have one SD card for my development environment, one card for my test environment, and 3 cards for my staging environment.

Steps

Fetch the image

lnx$ wget -O myStageImage.img.gz http://downloads.raspberrypi.org/raspbian_latest
lnx$ gunzip myStageImage.img.gz

Mount the image

An image file is an image of the whole SD card, which contains the boot loader, a partition table, and two partitions; boot, and root.

  • boot partition, mounted on /boot when you run your Raspberry Pi
  • root partition, mounted on / when you run your Raspberry Pi

First you will need to know the offset to the root partition, so that you can mount it.

lnx$ sudo fdisk -l ./myStageImage.img 

Disk ./myStageImage.img: 2962 MB, 2962227200 bytes
255 heads, 63 sectors/track, 360 cylinders, total 5785600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000981cb

             Device Boot      Start         End      Blocks   Id  System
./myStageImage.img1            8192      122879       57344    c  W95 FAT32 (LBA)
./myStageImage.img2          122880     5785599     2831360   83  Linux

Here we see that the second partition, which is our root partition, starts at 122880. The block size is 512 bytes, so we can calculate the offset as 122880 * 512 = 62914560. You can now mount this partition:

lnx$ sudo mkdir /mnt/myStageImage
lnx$ sudo mount -o loop,offset=62914560 ./myStageImage.img /mnt/myStageImage

Change the configuration to suit your needs

hostname

lnx$ cd /mnt/myStageImage/etc
lnx$ sudo vi hostname
lnx$ cat hostname
s1wire
lnx$ sudo vi hosts
lnx$ grep s1wire hosts
127.0.1.1	s1wire

ssh keys

lnx$ cd /mnt/myStageImage/home/pi
lnx$ mkdir .ssh
lnx$ vi .ssh/authorized_keys ; #put your public key in this file
lnx$ cp ~/path/to/your/favorite/keys/id_dsa* .ssh ; # copy your favorite keys to use with github to .ssh

Network configuration

lnx$ cd /mnt/myStageImage/etc/network
lnx$ cat interfaces ; # this is my network config, so that my stage always gets 192.168.2.63
#--------------------------------------
# Stage image fixed IP
#--------------------------------------
# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.2.63
netmask 255.255.255.0
network 192.168.2.0
broadcast 192.168.2.255
gateway 192.168.2.1
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 192.168.2.1

change the default editor to vi

lnx$ cd /mnt/myStageImage/etc/alternatives
lnx$ ls -la editor
lrwxrwxrwx 1 root root 17 Feb  8 12:47 editor -> /usr/bin/nano
lnx$ sudo ln -s /usr/bin/vim.tiny ./editor
lnx$ ls -la editor
lrwxrwxrwx 1 root root 17 Feb  8 12:47 editor -> /usr/bin/vim.tiny

create a script that I can run the first time I log onto my newly staged raspberry pi

In this example, I am just setting up my bootstrap.sh script, so that it runs raspi-config for me, so that I can expand the image at next boot.

lnx$ cd /mnt/myStageImage/home/pi/
lnx$ vi bootstrap.sh
lnx$ chmod 755 bootstrap.sh
lnx$ cat bootstrap.sh
#!/bin/bash

sudo raspi-config

Finalize your image

Now you just need to unmount the image and write it to your SD card.

cd ~
sudo umount /mnt/myStageImage

And you are done! Copy this image to where you need it.

Writing the stage image to an SD card

Writing the image to an SD card is different in different OS.

MAC

NOTE: Be very careful on your Apple OSX computer. Make sure you KNOW which device is your SD card. For example Time Machine will mount it's backup drive every now and then, and you really don't want to overwrite your backup disk. Believe me, I am speaking out of experience.

  • Fit the SD card in the card reader
  • List your local disks (and your SD card)
  • Make sure you know which disk is your SD card
  • Unmount that disk
  • Write the image to the disk using dd
  • Unmount the disk again
  • Done

List the disk:

mac$ diskutil list
/dev/disk0
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *500.3 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:                  Apple_HFS Macintosh HD            499.4 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3
/dev/disk1
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     Apple_partition_scheme                        *18.8 MB    disk1
   1:        Apple_partition_map                         32.3 KB    disk1s1
   2:                  Apple_HFS Flash Player            18.8 MB    disk1s2
/dev/disk3
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                        *8.1 GB     disk3
   1:             Windows_FAT_32 boot                    58.7 MB    disk3s1
   2:                      Linux                         8.0 GB     disk3s2

In this case, my SD card is on /dev/disk3. Unmount this disk:

mac$ sudo diskutil unmountDisk /dev/disk3

Now you can write the image to the disk, after which you unmount the disk again. This will take about 5 minutes or so.

mac$ time sudo dd if=myStageImage.img of=/dev/rdisk3 bs=1m
mac$ sudo diskutil unmountDisk /dev/disk3