From ac4a0fa400f4163f08a76c9e3e12b65f4a3ffafa Mon Sep 17 00:00:00 2001 From: Ilya Leoshkevich Date: Fri, 11 Sep 2020 21:27:59 +0200 Subject: [PATCH] vmtest: add debootstrap-based mkrootfs script The existing mkrootfs.sh is based on Arch Linux, which supports only x86_64. Add mkrootfs_debian.sh: a debootstrap-based script. Debian was chosen, because it supports more architectures than other mainstream distros. Move init setup to mkrootfs_tweak.sh, rename the existing Arch script to mkrootfs_arch.sh. Signed-off-by: Ilya Leoshkevich --- .../vmtest/{mkrootfs.sh => mkrootfs_arch.sh} | 54 +--------------- travis-ci/vmtest/mkrootfs_debian.sh | 40 ++++++++++++ travis-ci/vmtest/mkrootfs_tweak.sh | 61 +++++++++++++++++++ 3 files changed, 102 insertions(+), 53 deletions(-) rename travis-ci/vmtest/{mkrootfs.sh => mkrootfs_arch.sh} (66%) create mode 100755 travis-ci/vmtest/mkrootfs_debian.sh create mode 100755 travis-ci/vmtest/mkrootfs_tweak.sh diff --git a/travis-ci/vmtest/mkrootfs.sh b/travis-ci/vmtest/mkrootfs_arch.sh similarity index 66% rename from travis-ci/vmtest/mkrootfs.sh rename to travis-ci/vmtest/mkrootfs_arch.sh index be43b44..9419487 100755 --- a/travis-ci/vmtest/mkrootfs.sh +++ b/travis-ci/vmtest/mkrootfs_arch.sh @@ -100,59 +100,7 @@ rm -rf "$root/var/lib/pacman/sync/" # We don't need any documentation. rm -rf "$root/usr/share/{doc,help,man,texinfo}" -chroot "${root}" /bin/busybox --install +"$(dirname "$0")"/mkrootfs_tweak.sh "$root" -cat > "$root/etc/inittab" << "EOF" -::sysinit:/etc/init.d/rcS -::ctrlaltdel:/sbin/reboot -::shutdown:/sbin/swapoff -a -::shutdown:/bin/umount -a -r -::restart:/sbin/init -EOF -chmod 644 "$root/etc/inittab" - -mkdir -m 755 "$root/etc/init.d" "$root/etc/rcS.d" -cat > "$root/etc/rcS.d/S10-mount" << "EOF" -#!/bin/sh - -set -eux - -/bin/mount proc /proc -t proc - -# Mount devtmpfs if not mounted -if [[ -z $(/bin/mount -l -t devtmpfs) ]]; then - /bin/mount devtmpfs /dev -t devtmpfs -fi - -/bin/mount sysfs /sys -t sysfs -/bin/mount bpffs /sys/fs/bpf -t bpf -/bin/mount debugfs /sys/kernel/debug -t debugfs - -echo 'Listing currently mounted file systems' -/bin/mount -EOF -chmod 755 "$root/etc/rcS.d/S10-mount" - -cat > "$root/etc/rcS.d/S40-network" << "EOF" -#!/bin/sh - -set -eux - -ip link set lo up -EOF -chmod 755 "$root/etc/rcS.d/S40-network" - -cat > "$root/etc/init.d/rcS" << "EOF" -#!/bin/sh - -set -eux - -for path in /etc/rcS.d/S*; do - [ -x "$path" ] && "$path" -done -EOF -chmod 755 "$root/etc/init.d/rcS" - -chmod 755 "$root" tar -C "$root" -c . | zstd -T0 -19 -o "$NAME" chmod 644 "$NAME" diff --git a/travis-ci/vmtest/mkrootfs_debian.sh b/travis-ci/vmtest/mkrootfs_debian.sh new file mode 100755 index 0000000..a74a558 --- /dev/null +++ b/travis-ci/vmtest/mkrootfs_debian.sh @@ -0,0 +1,40 @@ +#!/bin/bash +# This script builds a Debian root filesystem image for testing libbpf in a +# virtual machine. Requires debootstrap >= 1.0.95 and zstd. + +set -e -u -x -o pipefail + +# Check whether we are root now in order to avoid confusing errors later. +if [ "$(id -u)" != 0 ]; then + echo "$0 must run as root" >&2 + exit 1 +fi + +# Create a working directory and schedule its deletion. +root=$(mktemp -d -p "$PWD") +trap 'rm -r "$root"' EXIT + +# Install packages. +packages=binutils,busybox,elfutils,iproute2,libcap2,libelf1,strace,zlib1g +debootstrap --include="$packages" --variant=minbase bullseye "$root" + +# Remove the init scripts (tests use their own). Also remove various +# unnecessary files in order to save space. +rm -rf \ + "$root"/etc/rcS.d \ + "$root"/usr/share/{doc,info,locale,man,zoneinfo} \ + "$root"/var/cache/apt/archives/* \ + "$root"/var/lib/apt/lists/* + +# Save some more space by removing coreutils - the tests use busybox. Before +# doing that, delete the buggy postrm script, which uses the rm command. +rm -f "$root/var/lib/dpkg/info/coreutils.postrm" +chroot "$root" dpkg --remove --force-remove-essential coreutils + +# Apply common tweaks. +"$(dirname "$0")"/mkrootfs_tweak.sh "$root" + +# Save the result. +name="libbpf-vmtest-rootfs-$(date +%Y.%m.%d).tar.zst" +rm -f "$name" +tar -C "$root" -c . | zstd -T0 -19 -o "$name" diff --git a/travis-ci/vmtest/mkrootfs_tweak.sh b/travis-ci/vmtest/mkrootfs_tweak.sh new file mode 100755 index 0000000..b0632d8 --- /dev/null +++ b/travis-ci/vmtest/mkrootfs_tweak.sh @@ -0,0 +1,61 @@ +#!/bin/bash +# This script prepares a mounted root filesystem for testing libbpf in a virtual +# machine. +set -e -u -x -o pipefail +root=$1 +shift + +chroot "${root}" /bin/busybox --install + +cat > "$root/etc/inittab" << "EOF" +::sysinit:/etc/init.d/rcS +::ctrlaltdel:/sbin/reboot +::shutdown:/sbin/swapoff -a +::shutdown:/bin/umount -a -r +::restart:/sbin/init +EOF +chmod 644 "$root/etc/inittab" + +mkdir -m 755 -p "$root/etc/init.d" "$root/etc/rcS.d" +cat > "$root/etc/rcS.d/S10-mount" << "EOF" +#!/bin/sh + +set -eux + +/bin/mount proc /proc -t proc + +# Mount devtmpfs if not mounted +if [[ -z $(/bin/mount -l -t devtmpfs) ]]; then + /bin/mount devtmpfs /dev -t devtmpfs +fi + +/bin/mount sysfs /sys -t sysfs +/bin/mount bpffs /sys/fs/bpf -t bpf +/bin/mount debugfs /sys/kernel/debug -t debugfs + +echo 'Listing currently mounted file systems' +/bin/mount +EOF +chmod 755 "$root/etc/rcS.d/S10-mount" + +cat > "$root/etc/rcS.d/S40-network" << "EOF" +#!/bin/sh + +set -eux + +ip link set lo up +EOF +chmod 755 "$root/etc/rcS.d/S40-network" + +cat > "$root/etc/init.d/rcS" << "EOF" +#!/bin/sh + +set -eux + +for path in /etc/rcS.d/S*; do + [ -x "$path" ] && "$path" +done +EOF +chmod 755 "$root/etc/init.d/rcS" + +chmod 755 "$root"