It seems silly to use our limited SD write cycles to upgrade the software shipped on the images. So, how can we upgrade said software before flashing the image to an SD card?
The Hard Way
Preparing your system - Debian/Ubuntu
I know this doesn't work on Ubuntu 10.04 LTS, because some of the packages are too old.
Ensure your own system is up to date.
$ sudo apt-get update
$ sudo apt-get upgrade
Install some new software
$ sudo apt-get install binfmt-support qemu qemu-user-static unzip
qemu
is an ARM emulator, and qemu-user-static
and binfmt-support
allows us to run ARM executables without emulating the ARM kernel. (How cool is that!?!)
Preparing your system - Arch
I can't find a statically linked qemu
in the Arch repositories, so we will have to compile from source.
- Download the latest release from http://git.savannah.gnu.org/cgit/qemu.git
Unzip and run
./configure --disable-kvm --target-list=arm-linux-user --static
Build using
make
and install usingsudo make install
.Run the following as
root
echo ':arm:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/usr/local/bin/qemu-arm:' > /proc/sys/fs/binfmt_misc/register
echo ':armeb:M::\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff:/usr/local/bin/qemu-armeb:' > /proc/sys/fs/binfmt_misc/register
Warning You shouldn't run arbitrary commands you find online as
root
- these were taken fromqemu-binfmt-conf.sh
under the ARM cpu type. Please extract the commands from this file and run those.
Download and unzip the image
Go to raspberrypi.org and download the image you want. Unzip it and save the .img
file somewhere useful.
$ sudo mkdir -p /images/debian-squeeze
$ sudo wget "http://files.velocix.com/c1410/images/debian/6/debian6-19-04-2012/debian6-19-04-2012.zip" -O "/images/debian-squeeze.zip"
$ sudo unzip "/images/debian-squeeze.zip" -d /images/debian-squeeze
$ sudo rm /images/debian-squeeze.zip
Find the correct partition
The .img
will contain 3 partitions, including the boot partition.
$ cd /images/debian-squeeze/debian6-19-04-2012/
$ fdisk -lu debian6-19-04-2012.img
Disk debian6-19-04-2012.img: 1949 MB, 1949999616 bytes
4 heads, 32 sectors/track, 29754 cylinders, total 3808593 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: 0x000ee283
Device Boot Start End Blocks Id System
debian6-19-04-2012.img1 2048 155647 76800 c W95 FAT32 (LBA)
debian6-19-04-2012.img2 157696 3414015 1628160 83 Linux
debian6-19-04-2012.img3 3416064 3807231 195584 82 Linux swap / Solaris
We need to know the offset of the Linux partition, in this case it is 157696
sectors, and the boot partition, which is at 2048
sectors. Each sector is 512 bytes, so the root offset is 157696*512=80740352
bytes and the boot offset is 2048*512=1048576
.
Mount the image as a loopback device
Next, we need to mount the image as a file system. This can be done using a loopback device. We use the offset from the previous section to tell mount
which partitions to mount and where. The order of these commands is important.
$ sudo mount -o loop,offset=80740352 "/images/debian-squeeze/debian6-19-04-2012/debian6-19-04-2012.img" /mnt
$ sudo mount -o loop,offset=1048576 "/images/debian-squeeze/debian6-19-04-2012/debian6-19-04-2012.img" /mnt/boot
Preparing the filesystem.
We're nearly ready to chroot
into our file system and start installing new software. First, we must install the emulator into our image, as it won't be available once we use chroot
.
Debian/Ubuntu
$ sudo cp /usr/bin/qemu-arm-static /mnt/usr/bin/
Arch Linux
$ sudo cp /usr/local/bin/qemu-arm /mnt/usr/local/bin/
All host systems
We also need to provide access to certain other parts of the system.
$ sudo mount --rbind /dev /mnt/dev
$ sudo mount -t proc none /mnt/proc
$ sudo mount -o bind /sys /mnt/sys
chroot
We are done! chroot
away...
$ sudo chroot /mnt
You are now in your Raspberry Pi, but the services aren't running etc. Be careful, you are root!
Update/Install software - Debian Image
To update the software, we use apt-get
.
# apt-get update
# apt-get upgrade
You can also install software using apt-get install
as per usual.
Update/Install software - Arch Image
To update the software, we use pacman
.
# pacman -Syu
You can also install software using pacman -S
as per usual.
NOTE You can run
pacman
natively by following the instructions on How Do I Run My Native Pacman Against A Mounted Image?.
Exiting
You can exit the chroot
by using Ctrl+D and unmount the system by running sudo umount /mnt
- you will have to unmount each mount point separately.
You should remove qemu-user-static
from /usr/bin
or qemu-arm
from /usr/local/bin
on the RPi, then the image is ready to be flashed.
Final Words
This is a little long and tedious, but do it once and you'll learn loads about how this all works!
The Easy Way - piimg
I've started work on a utility for doing a lot of this for you. It is called piimg and can be found at github.com/alexchamberlain/piimg.
So far, it can mount the SD card for you by running
piimg mount /images/debian-squeeze/debian6-19-04-2012/debian6-19-04-2012.img /mnt
and unmount them again by running
piimg umount /mnt
You just need to install qemu
and chroot
away.