6. June 2020

Automatic initrd/initramfs creation for Raspian/Raspberry Pi OS

A very techie post on my blog, so I don't expect this to be of general interest. Try browsing the categories for something more interesting...

The perennial problem of ensuring your initrd or initramfs file has been correctly generated for the appropriate Pi architecture


First, I am not good at bash scripting. I have managed to scratch a personal itch using the ideas outlined in this post. I am posting this as it may help someone resolve their own problems, but there is no guarantee that this will work on your set-up. You have been warned. Infinitely brighter minds than mine have had a go at this problem and not resolved it. Let that fact be your guiding light. Again, I am not good at scripting ,and for all I know, my additions are utterly horrible. You may find your system unbootable if you simply try my ideas without understanding and making your own decisions.

I prefer to run the jfs filesystem on my Raspberry Pis that use USB disks rather than the SD card for data or the main file system. The problem with choosing anything other than ext4 or nfs is that nothing else is supported by the standard kernel and boot arrangement of any of the Raspberry Pis. So if you want to experiment with ZFS, or learn more about BTRFS, or, as in my case, be ultra cautious about the data on my disks, and so choose the one filesystem with which I have never lost data, you need to create an initrd file or initram file.

The problem with using an initramfs on Raspbian/Raspberry Pi OS is that the /boot directory, on which the initramfs should reside, is formatted as VFAT.  The initramfs file MUST be generated for the specific kernel version you are running, and on other systems, this is usually done by automatic generation of the initramfs, then symlinking the correct one under /boot.  Symlinking is not possible under VFAT.

I'll not go into the creation of an initrd are it is well covered elsewhere.  I will also not go in to the full details of how the initramfs works, as if you need it, you will no doubt know all about it. Suffice to say that, in my case, the following is necessary:

  • Install initramfs-tools package (should already be there)
  • Add "jfs" to /etc/initramfs-tools/modules
  • Add a script that includes jfs fsck binary files to /etc/initramfs-tools/hooks (example below)
  • Edit /etc/default/raspberrypi-kernel to say "INITRD=YES"
  • Add "initramfs initramfs.gz followkernel" to /boot/config.txt (or whatever you wish to call your initramfs)
  • Generate the initramfs in the standard way.
The script to add the correct hooks, in my case and for jfs, looks like this, and must be chmod'ed executable:
. /usr/share/initramfs-tools/scripts/functions
. /usr/share/initramfs-tools/hook-functions

copy_exec /sbin/fsck.jfs /sbin
copy_exec /sbin/jfs_debugfs /sbin
copy_exec /sbin/jfs_fsck /sbin
copy_exec /sbin/jfs_fscklog /sbin
copy_exec /sbin/jfs_logdump /sbin
copy_exec /sbin/jfs_mkfs /sbin
copy_exec /sbin/jfs_tune /sbin
The trouble comes when a new kernel comes along.  The initramfs-tools will merrily create a series of initrd-* files suitable for all the Raspberry Pi architectures. But because of that symlink problem, it is then necessary to make sure you manually copy the correct version to /boot/initramfs.gz (if using the example above) or altering the /boot/config.txt file with the initram of choice.
There have been attempts to automate this but they have never worked satisfactorily for me, and from time to time, I have found myself with an unbootable Pi. These attempts usually take tje form of an additional script in the /etc/kernel/postinst.d directory, which are executed after the new kernel is installed. One option by-passed the standard initramfs-tools utilities and hard-created a /boot/initramfs.gz file, but I found this rather hit and miss. Another option tries to modify the /boot/config.txt file, but that seems risky to me, as it could do other damage to the file as well as edit out and replace the entries you need.
So I have been working on some additions to the standard script in /etc/kernel/postinst.d/initramfs-tools script. This script is called several times when installing a new kernel (or re-installing an existing one).  On each occasion it is called, the kernel version for which it needs to create an initramfs file is given as an argument.  These additions "simply" identify the currently running Raspbian/Raspberry Pi OS architecture after the creation of the initrd<KERNEL_VERSION-<ARCHITECTURE> file, then copies the existing initramfs.gz file, then copies the correct (hopefully) file to /boot/initramfs.gz. It provides some feedback so that you know what it has done and allows you to inspect the results. It keeps all the original files intact. The script addition also provides feedback for architectures that do not apply, to be sure, to be sure.
If you can improve on this, and I am sure that would be trivial, do drop me an email. Similarly, if you can see glaring use-cases where this will fail, let me know.

My additions are to  the /etc/kernel/initramfs-tools script and come after the last line, which should be:

INITRAMFS_TOOLS_KERNEL_HOOK=1 update-initramfs -c -k "${version}" ${bootopt} >&2

Add the following:

########### Stevan Hack to install create initramfs version ###################

echo " "

echo "======================================================================="

echo "Selecting /boot/initramfs.gz for appropriate Pi architecture"

CURRENT_KERNEL_TYPE=`uname -a | sed 's/.*-//' | cut -d" " -f1`

WORKINGON=`echo ${version}| sed 's/.*-//' | cut -d" " -f1`



    echo "uname architecture is " `uname -m`

    echo "Selected initrd is "$CHOSEN_INITRD

    echo "Selected initrd to copy as initramfs.gz is " $CHOSEN_INITRD

#    echo ""

    echo "First moving current initramsfs to safety"

    cp -v /boot/initramfs.gz /boot/initramfs.gz-`date +"%d%m%Y_%H%M"`

    echo "Now creating correct initramfs.gz....."

    cp -v /boot/$CHOSEN_INITRD /boot/initramfs.gz

    echo "Compare. File size should be indentical...."

    ls -al /boot/$CHOSEN_INITRD /boot/initramfs.gz

    echo " "


    echo "      NOTE: This is not the correct initramsfs for this Pi architecture "${version}

    echo "      initramfs.gz not copied for this version"



echo "initramfstools completed for kernel version "$CHOSEN_INITRD

echo "======================================================================="

echo ""

########## end of Stevan Hack ###########################

10. May 2020

A New Inverter for our Off-grid Power System

StevanPics-122920-10052020.jpg, May 2020

Thunderstorms at the beginning of the year prompted a desire to have a back-up inverter

Continue reading

4. April 2020

Nextcloud Talk and the missing piece of the puzzle

It worked, but wouldn't tell us, because of an obscure issue

Continue reading

31. January 2020

An Unexpected Day

A personal view of the decision to leave the Union of European Nations

Continue reading

12. January 2020

Power Problems - When Thunderstorms Strike

An off-grid life means taking responsibility when things go wrong

Continue reading

26. December 2019

Almost Synonyms - The linguistic families 6000 miles apart

Scots words and their Afrikaans equivalents have long fascinated me. Here are some.

Continue reading

3. December 2019

Tweeting the weather

Some digital glue automates tweeting the current weather conditions

Continue reading

21. September 2019

Peggy - 2003-2018

The life and times of a full-of-life Yorkie

Continue reading

18. July 2019

USB Scanner - scans a black page - Resolved

Under Linux, this scanner should Just Work, but doesn't. The fix is very simple

Continue reading

16. May 2019

Daily walks

Some recent pictures from our morning walks around Clachtoll and Stoer

Continue reading

25. February 2019

The Perennial Problem of the new Linux Laptop

Choices, choices, choices, some dead-ends, such as being far too expensive, some nice-to-haves, and finally some solutions.

Continue reading

1. December 2018

Nextcloud on a Raspberry Pi

There is a lot on the 'net about running your own sync and other cloud services with Nextcloud and the Raspberry Pi, many concluding it's not viable for continuous use, or many losing interest after a while. In fact, with some thought-through choices, a production environment for, say 5 or fewer users is perfectly feasible.

Continue reading

16. October 2018

Revisiting server-based antispam with Bogofilter

Some simple maintenance makes life interesting, but simplicity saves the day

Continue reading

2. October 2018

Latest update - off-grid Linux IT services

The latest (October 2018) update on hardware and software choices when your power supply is limited

This is a technical blog post which may not be of general interest, and assumes a certain level of technical understanding

Continue reading

18. August 2018

Off-grid - making do and mending

A bit of resourcefulness and "it'll-come-in-handy-some-day" hoarding saves the day

Continue reading

2. May 2018

Weather Station Software

Our weather data goes back to 2011. How to maintain that collection while changing the weather station software

Continue reading

5. February 2018

Raspberry Pi home server - setting up OpenDKIM with postfix

Some vague notes about setting up openDKIM against multiple domains on a single instance, all domains using one key.

Continue reading

14. September 2017

Preparing digital photo files for use on the Web

Reducing size, adding copyright information and other watermarking is useful for pictures one may wish to use on the Internet. Selecting the images, or using drag 'n' drop graphically is easier than pushing specific files through a script. 

Note - I am no programmer, and my scripting abilities are severely limited. This works for me, but may not work for you. Use these ideas at your own risk.


Continue reading

23. August 2017

Lightweight anti-spam alternative for small servers

Spamassassin may be the standard anti-spam utility for servers, but it can't be considered to be either fast or low on resources.  Bogofilter may offer some advantages, but unlike spamassassin, tutorials and how-to's are thin on the ground

Warning: this is a technical post, full of jargon and an expectation that it will be read by system administrators, so may not be of interest to all readers.

Integrating postfix, dovecot and bogofilter on a Raspberry Pi.

Edit: Some months have passed since installing bogofilter.  It is not as fast as a daemon, but not as slow as spamassassin, either.  As expected, it has taken a couple of months to build up accuracy, but the system is excellent, and now reliably marks some spam that always got through spamassassin. This seems a good way forward for lightweight email systems.

Additional Addendum: See the note at the end of the article. Bogofilter remains a good server-based option for dealing with spam.

Continue reading

16. July 2017

The Calendar Hokey Cokey

A problem with the Nextcloud calendar can be resolved with a lot of to-ing and fro-ing, thanks to standards

Continue reading

- page 1 of 4