Migrate from Raspbian Buster to Debian Bullseye (and Devuan Chimaera)

This page describes the steps required to upgrade/migrate a Raspberry Pi running Raspbian Buster to a vanilla Debian Bullseye distribution, without data loss, having to reformat, or otherwise needing hands on the device. If like me you are unhappy about Raspbian (lack of) security policy and were wary of attempting this migration, fear no more, the instructions below will (hopefully) get you through this ordeal :)

Disclaimer

The information and methods described herein are provided “AS-IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED. Use the concepts, examples and information at your own risk. There may be errors and inaccuracies, that could be damaging to your devices. Proceed with caution, and although it is highly unlikely that accidents will happen because of following advice or procedures described in this document, the author does not take any responsibility for any damage claimed to be caused by doing so.

Procedure

I have tested this procedure on a Raspberry Pi 2. I would probably not try it on older devices which don’t exactly align between Raspbian and Debian. The environment I migrated was headless. It Worked For Me™ but as usual Your Mileage May Vary (also ™). Having a backup of important data before beginning may be a good idea :)

Preparation

All the commands listed below must be run as user root.

First, login to your device and cleanup as much as possible unneeded packages: the less packages to transition the less risk for trouble. debfoster is what I used to do this.

You will also want to make sure that your boot partition is “large enough” to accomodate 2 kernels (256MB should be good), alternatively you can also edit /etc/initramfs-tools/initramfs.conf and make sure that MODULES=dep is set there. This will drastically reduce the size of the initramfs, which, as an added bonus, will reduce the time needed to generate said initramfs, and the time needed to boot.

Next, you’ll want to edit /etc/apt/sources.list. The only lines I kept here are:

deb http://deb.debian.org/debian bullseye main contrib non-free
deb http://deb.debian.org/debian-security/ bullseye-security main contrib non-free
deb http://deb.debian.org/debian bullseye-updates main contrib non-free

Note: the non-free section is required to be able to fetch the Raspberry Pi firmware (which is non-free).

Do not forget to disable or remove any extra sources in /etc/apt/sources.list.d/.

Now we need a minor extra step for these new sources to be fully functional. Execute:

apt-get update --allow-insecure-repositories
apt-get install debian-archive-keyring --allow-unauthenticated
apt-get update

This reads the new sources in a (temporarily) insecure manner, then installs the Debian Archive keyring and then revalidates the sources securely this time.

Kernel update

Now we are about to enter the danger zone: the first thing that we need to do is to move from the Raspbian kernel to the stock Debian kernel.

This procedure involves:

  • Removing the existing running kernel and firmware files
  • Changing the boot partition mountpoint
  • Installing the new kernel and firmware files

Note: should anything bad happen during this particular step, the device may be left in a non-bootable state. Of particular importance is power supply reliability here: you do NOT want to lose power until this is over!

Because of the potential risk, the proposed commands below are tailored to complete as fast as possible on the device, with only the absolute minimal amount of changes required.

When you are ready, execute the following commands in order:

apt-get remove --purge raspberrypi-bootloader raspberrypi-kernel raspi-config plymouth
umount /boot
mkdir /boot/firmware
sed -i s+/boot+/boot/firmware+ /etc/fstab
mount /boot/firmware
apt-get install linux-image-armmp-lpae raspi-firmware

Once this is done, you are mostly out of the danger zone.

System upgrade

The next actions deal with upgrading the entire system in an orderly fashion:

apt-get upgrade
apt-get autoremove
apt-get dist-upgrade
apt-get remove --purge raspbian-archive-keyring
apt-get autoremove
apt-get clean

Once that is done the migration is practically complete: you can reboot.

Your system may still have some leftover packages from Raspbian which no longer exist in Debian. One way to remove these packages is to run the following command:

apt list --installed | grep ',local' | sed s+/.*++ | xargs apt-get -s remove --purge

This searches for “local” installed packages, i.e. packages that are not present in any repository, and passes the resulting list to apt-get remove in simulation mode. If you are happy with the result, you can replace the -s from this line with -y and let apt-get clean out the cruft.

That’s it, you’re done!

Devuan

From this point, should you want to get rid of the abomination that systemd is, you can easily migrate to Devuan Chimaera by following to the letter the instructions provided here, with one caveat: in the suggested sources.list you will still need to add the non-free section for the firmware. That aside, it works: I have successfully tested that too.