1

I would like to create an initramfs image for Debian Stretch which includes additional configuration files (say /etc/a.conf).

What do I have to do prior to running mkinitramfs -o initrd.img in order for the image to include such files?

rookie099
  • 2,201
  • 2
  • 26
  • 52

2 Answers2

3

I have chosen to do this with an initramfs hook for this ensures (if I understand correctly) that the change will also persist across future kernel upgrades. The hook takes the form of a script /etc/initramfs-tools/hooks/copy_etc as follows:

#!/bin/sh -e

if [ "$1" = "prereqs" ]; then exit 0; fi
. /usr/share/initramfs-tools/hook-functions

cp /etc/a.conf $DESTDIR/etc/a.conf
rookie099
  • 2,201
  • 2
  • 26
  • 52
2

The tool you want for modifying the initrd/initramfs is called cpio. You can find a bunch of tutorials on this out on the internet, now that you know what to look for. Here's a quick example:

mkdir initrd-tmp
cd initrd-tmp
lzma -dc -S .lz /mnt/casper/initrd.lz | cpio -id

And then when done:

find . | cpio --quiet --dereference -o -H newc | lzma -7 > ~/new-initrd.lz

Source: https://wiki.ubuntu.com/CustomizeLiveInitrd

Note that a fun property of cpio archives is that you can simply append to them and later files overwrite earlier files—probably due to their heritage as a file system for tape archival. So if you don't want the hassle of actually unpacking the whole archive (especially as it may require root to create paths like /proc), you can simply append your customization files to it. See:

https://wiki.debian.org/DebianInstaller/NetbootFirmware#Example_.231:_add_debs_from_firmware.cpio.gz

mikepurvis
  • 1,568
  • 2
  • 19
  • 28
  • So if I understand correctly what you are doing here is not building a new initramfs image from scratch (while adding `/etc/a.conf` presumably with a `mkinitramfs` hook), but unpacking the existing image, adding only `/etc/a.conf` to the unpacked version, and packaging it again. Correct? – rookie099 Jan 12 '19 at 06:38
  • 1
    @rookie099 Yes, that's correct. This answer is geared more toward customizing the installation/live environment, which tends to be a one-off thing (or implemented in whatever CI process generates your customized installer). If you need ongoing customization, then yes, use the mkinitramfs hook. – mikepurvis Jan 14 '19 at 15:37