diff options
author | Alistair Delva <adelva@google.com> | 2021-04-09 15:10:33 -0700 |
---|---|---|
committer | Alistair Delva <adelva@google.com> | 2021-06-29 18:19:16 +0000 |
commit | 9facea89aec1b964f8f42840ef892b43859f5fed (patch) | |
tree | 51e0f85f0a57e2c5fd59fe62f1bd480e4fd84a1a | |
parent | dd55ba545b5dfd3d1b8ea6963b55c8f3cc738c96 (diff) | |
download | tests-9facea89aec1b964f8f42840ef892b43859f5fed.tar.gz |
Support generating a stub initrd
As part of the rootfs building process, also generate an initrd template
that can be combined with a modular kernel (like GKI) in order to boot
the root filesystem.
Change-Id: I31618ef1ad076a4950555a1f2cc96913e5c807d7
-rwxr-xr-x | net/test/build_rootfs.sh | 62 | ||||
-rw-r--r-- | net/test/rootfs/bullseye.list | 3 | ||||
-rw-r--r-- | net/test/rootfs/common.sh | 5 | ||||
-rwxr-xr-x | net/test/rootfs/stage2.sh | 8 |
4 files changed, 69 insertions, 9 deletions
diff --git a/net/test/build_rootfs.sh b/net/test/build_rootfs.sh index 3b7233f..6e216a8 100755 --- a/net/test/build_rootfs.sh +++ b/net/test/build_rootfs.sh @@ -29,7 +29,7 @@ export LC_ALL=C usage() { echo -n "usage: $0 [-h] [-s bullseye] [-a i386|amd64|armhf|arm64] " - echo "[-m http://mirror/debian] [-n net_test.rootfs.$(date +%Y%m%d)]" + echo "[-m http://mirror/debian] [-n rootfs] [-r initrd]" exit 1 } @@ -37,7 +37,10 @@ mirror=http://ftp.debian.org/debian suite=bullseye arch=amd64 -while getopts ":hs:a:m:n:" opt; do +ramdisk= +rootfs= + +while getopts ":hs:a:m:n:r:" opt; do case "${opt}" in h) usage @@ -64,7 +67,10 @@ while getopts ":hs:a:m:n:" opt; do mirror="${OPTARG}" ;; n) - name="${OPTARG}" + rootfs="${OPTARG}" + ;; + r) + ramdisk="${OPTARG}" ;; \?) echo "Invalid option: ${OPTARG}" >&2 @@ -77,13 +83,20 @@ while getopts ":hs:a:m:n:" opt; do esac done -if [[ -z "${name}" ]]; then - name="net_test.rootfs.${arch}.${suite}.$(date +%Y%m%d)" +if [[ -z "${rootfs}" ]]; then + rootfs="rootfs.${arch}.${suite}.$(date +%Y%m%d)" +fi +rootfs=$(realpath "${rootfs}") + +if [[ -z "${ramdisk}" ]]; then + ramdisk="initrd.${arch}.${suite}.$(date +%Y%m%d)" fi +ramdisk=$(realpath "${ramdisk}") # Sometimes it isn't obvious when the script fails failure() { echo "Filesystem generation process failed." >&2 + rm -f "${rootfs}" "${ramdisk}" } trap failure ERR @@ -118,6 +131,7 @@ sudo mkdir host sudo chroot . root/stage2.sh sudo chroot . root/${suite}.sh +raw_initrd="${PWD}"/boot/initrd.img # Workarounds for bugs in the debootstrap suite scripts for mount in $(cat /proc/mounts | cut -d' ' -f2 | grep -e "^${workdir}"); do @@ -125,7 +139,36 @@ for mount in $(cat /proc/mounts | cut -d' ' -f2 | grep -e "^${workdir}"); do sudo umount "${mount}" done +# Leave the workdir, to process the initrd +cd - + +# New workdir for the initrd extraction +workdir="${tmpdir}/initrd" +mkdir "${workdir}" +chmod 0755 "${workdir}" +sudo chown root:root "${workdir}" + +# Change into workdir to repack initramfs +cd "${workdir}" + +# Process the initrd to remove kernel-specific metadata +lz4 -lcd "${raw_initrd}" | sudo cpio -idum +sudo rm -f "${raw_initrd}" +sudo rm -rf usr/lib/modules +sudo mkdir -p usr/lib/modules + +# Debian symlinks /usr/lib to /lib, but we'd prefer the other way around +# so that it more closely matches what happens in Android initramfs images. +# This enables 'cat ramdiskA.img ramdiskB.img >ramdiskC.img' to "just work". +sudo rm -f lib +sudo mv usr/lib lib +sudo ln -s /lib usr/lib + +# Repack the ramdisk to the final output +find * | sudo cpio -H newc -o --quiet | lz4 -lc9 >"${ramdisk}" + # Leave the workdir, to build the filesystem +workdir="${tmpdir}/_" cd - # For the final image mount @@ -137,11 +180,11 @@ mount_remove() { trap mount_remove EXIT # Create a 1G empty ext3 filesystem -truncate -s 1G "${name}" -mke2fs -F -t ext3 -L ROOT "${name}" +truncate -s 1G "${rootfs}" +mke2fs -F -t ext3 -L ROOT "${rootfs}" # Mount the new filesystem locally -sudo mount -o loop -t ext3 "${name}" "${mount}" +sudo mount -o loop -t ext3 "${rootfs}" "${mount}" image_unmount() { sudo umount "${mount}" mount_remove @@ -155,4 +198,5 @@ sudo cp -a "${workdir}"/* "${mount}" sudo dd if=/dev/zero of="${mount}/sparse" bs=1M 2>/dev/null || true sudo rm -f "${mount}/sparse" -echo "Debian ${suite} for ${arch} filesystem generated at '${name}'." +echo "Debian ${suite} for ${arch} filesystem generated at '${rootfs}'." +echo "Initial ramdisk generated at '${ramdisk}'." diff --git a/net/test/rootfs/bullseye.list b/net/test/rootfs/bullseye.list index f6ab2a2..e908a11 100644 --- a/net/test/rootfs/bullseye.list +++ b/net/test/rootfs/bullseye.list @@ -3,9 +3,11 @@ apt-utils bash-completion bsdmainutils ca-certificates +e2fsprogs file gpgv ifupdown +initramfs-tools insserv iputils-ping iptables @@ -13,6 +15,7 @@ isc-dhcp-client less libnetfilter-conntrack3 libnfnetlink0 +lz4 mime-support netbase netcat-openbsd diff --git a/net/test/rootfs/common.sh b/net/test/rootfs/common.sh index 17afbfe..35e5745 100644 --- a/net/test/rootfs/common.sh +++ b/net/test/rootfs/common.sh @@ -67,6 +67,11 @@ cleanup() { mkdir -p /var/lib/systemd/{coredump,linger,rfkill,timesync} chown systemd-timesync:systemd-timesync /var/lib/systemd/timesync + # If embedding isn't enabled, remove the embedded modules and initrd and + # uninstall the tools to regenerate the initrd, as they're unlikely to + # ever be used + apt-get purge -y initramfs-tools initramfs-tools-core klibc-utils kmod + # Miscellaneous cleanup rm -rf /var/lib/apt/lists/* || true rm -f /root/* || true diff --git a/net/test/rootfs/stage2.sh b/net/test/rootfs/stage2.sh index bb35b78..26be79e 100755 --- a/net/test/rootfs/stage2.sh +++ b/net/test/rootfs/stage2.sh @@ -53,3 +53,11 @@ mkdir -p /var/empty rm -rf /root/stage2.sh /tmp/* find /var/log -type f -exec rm -f '{}' ';' find /var/tmp -type f -exec rm -f '{}' ';' + +# Create an empty initramfs to be combined with modules later +sed -i 's,^COMPRESS=gzip,COMPRESS=lz4,' /etc/initramfs-tools/initramfs.conf +mkdir -p /lib/modules/0.0 +depmod -a 0.0 +update-initramfs -c -k 0.0 +mv /boot/initrd.img-0.0 /boot/initrd.img +rm -rf /lib/modules/0.0 |