1#!/bin/bash 2 3# This script is based on drgn script for generating Arch Linux bootstrap 4# images. 5# https://github.com/osandov/drgn/blob/master/scripts/vmtest/mkrootfs.sh 6 7set -euo pipefail 8 9usage () { 10 USAGE_STRING="usage: $0 [NAME] 11 $0 -h 12 13Build an Arch Linux root filesystem image for testing libbpf in a virtual 14machine. 15 16The image is generated as a zstd-compressed tarball. 17 18This must be run as root, as most of the installation is done in a chroot. 19 20Arguments: 21 NAME name of generated image file (default: 22 libbpf-vmtest-rootfs-\$DATE.tar.zst) 23 24Options: 25 -h display this help message and exit" 26 27 case "$1" in 28 out) 29 echo "$USAGE_STRING" 30 exit 0 31 ;; 32 err) 33 echo "$USAGE_STRING" >&2 34 exit 1 35 ;; 36 esac 37} 38 39while getopts "h" OPT; do 40 case "$OPT" in 41 h) 42 usage out 43 ;; 44 *) 45 usage err 46 ;; 47 esac 48done 49if [[ $OPTIND -eq $# ]]; then 50 NAME="${!OPTIND}" 51elif [[ $OPTIND -gt $# ]]; then 52 NAME="libbpf-vmtest-rootfs-$(date +%Y.%m.%d).tar.zst" 53else 54 usage err 55fi 56 57pacman_conf= 58root= 59trap 'rm -rf "$pacman_conf" "$root"' EXIT 60pacman_conf="$(mktemp -p "$PWD")" 61cat > "$pacman_conf" << "EOF" 62[options] 63Architecture = x86_64 64CheckSpace 65SigLevel = Required DatabaseOptional 66[core] 67Include = /etc/pacman.d/mirrorlist 68[extra] 69Include = /etc/pacman.d/mirrorlist 70[community] 71Include = /etc/pacman.d/mirrorlist 72EOF 73root="$(mktemp -d -p "$PWD")" 74 75packages=( 76 busybox 77 # libbpf dependencies. 78 libelf 79 zlib 80 # selftests test_progs dependencies. 81 binutils 82 elfutils 83 glibc 84 iproute2 85 # selftests test_verifier dependencies. 86 libcap 87) 88 89pacstrap -C "$pacman_conf" -cGM "$root" "${packages[@]}" 90 91# Remove unnecessary files from the chroot. 92 93# We don't need the pacman databases anymore. 94rm -rf "$root/var/lib/pacman/sync/" 95# We don't need D, Fortran, or Go. 96 rm -f "$root/usr/lib/libgdruntime."* \ 97 "$root/usr/lib/libgphobos."* \ 98 "$root/usr/lib/libgfortran."* \ 99 "$root/usr/lib/libgo."* 100# We don't need any documentation. 101rm -rf "$root/usr/share/{doc,help,man,texinfo}" 102 103"$(dirname "$0")"/mkrootfs_tweak.sh "$root" 104 105tar -C "$root" -c . | zstd -T0 -19 -o "$NAME" 106chmod 644 "$NAME" 107