Saturday 14 May 2016

Upgrading my Raspberry Pi software

In a previous blog post (My first Raspberry Pi - day 3) I described how I'd set up my Raspberry Pi to boot from an external hard disc drive. In that setup I created a "spare" partition to allow easy migration to a new operating system. Now, 2½ years later, it's time to find out just how easy (or not) this is by upgrading the Raspbian OS from "Wheezy" to "Jessie".

I downloaded the Raspbian "Jessie" image from the raspberrypi.org downloads page and unzipped the file to extract the image file 2016-03-18-raspbian-jessie.img. Then I copied this file to the Raspberry Pi and installed it as follows.

Install kpartx and then use it to access the two partitions of the image file:
sudo apt-get install kpartx
sudo kpartx -a 2016-03-18-raspbian-jessie.img

Create mount points for the two partitions and mount them:
sudo mkdir /mnt/jessie-boot
sudo mkdir /mnt/jessie-root
sudo mount /dev/mapper/loop0p1 /mnt/jessie-boot
sudo mount /dev/mapper/loop0p2 /mnt/jessie-root

Copy the "Jessie" root partition to /spare. This takes some time as there is over 3 Gigabytes to copy:
sudo rsync -av /mnt/jessie-root/ /spare

Copy the "Jessie" boot partition to /spare/boot.bak. Note this is not the actual /boot partition, we don't want to change that until we're ready to boot into the new OS.
sudo mkdir /spare/boot.bak
sudo rsync -av /mnt/jessie-boot/ /spare/boot.bak

Move the "Jessie" home directory's contents to the /home partition:
sudo rsync -av /spare/home/ /home
sudo rm -rf /spare/home/pi

Copy /etc/fstab to the "Jessie" partition and edit it:
sudo cp /etc/fstab /spare/etc/
sudo vi /spare/etc/fstab

After editing the fstab file is as follows:
proc            /proc           proc    defaults          0       0
/dev/mmcblk0p1  /boot           vfat    defaults,ro       0       2
/dev/sda1       /old_os         ext4    defaults,noatime  0       1
/dev/sda2       /               ext4    defaults,noatime  0       1
/dev/sda3       none            swap    sw                0       0
/dev/sda4       /home           ext4    defaults,noatime  0       1
tmpfs           /tmp            tmpfs   size=256M         0       0

Note that /dev/sda2, which is currently mounted as /spare, will be mounted as / and that /dev/sda1, currently mounted as /, will be mounted as /old_os. This requires a new mount point to be created:
sudo mkdir /spare/old_os

Since my original Rasbperry Pi setup the network configuration file has changed from /etc/network/interfaces to /etc/dhcpcd.conf. As I use a static network address on one of my Pis (because it is my network DHCP server) I needed to edit /spare/etc/dhcpcd.conf before attempting to reboot.

Edit the new boot command line and set it to use /dev/sda2 as the root device:
sudo vi /spare/boot.bak/cmdline.txt

Backup the existing /boot partition, then make it writeable and copy the new boot code to it:
sudo mkdir /boot.bak
sudo rsync -av /boot/ /boot.bak
sudo mount -o remount,rw /boot
sudo rsync -av /spare/boot.bak/ /boot

At this stage it should be possible to boot the Raspberry pi into the new operating system and login as pi:
sudo reboot

If I want to go back to the older operating system at any time I should be able to do so by copying /old_os/boot.bak to /boot and rebooting.

All that remains to do is to add my normal user account jim and then install and configure the current versions of all the software I was running — dnsmasq, xinetd, esmtp, nfs-kernel-server, etc. Being able to view the old configuration files in /old_os/etc is a great help when doing this.