#!/bin/bash # This tool packages the current project as a tarball. # # The specification of the tarball is: # - metadata.json: the project's metadata (name, dependencies, file list, unit names, etc.) # - content.tar: the files to be installed on the target system, with their relative paths and permissions preserved. # # The tool takes its parameters from the environment variables defined in the Makefile: # # - PROJECT_NAME: the name of the project. # - ALL_DEPENDENCIES: the list of all (direct and transitive) dependencies of the project. # - DIRECT_DEPENDENCIES: the list of all direct dependencies of the project. # - TARGET_CHROOT: the target chroot directory containing the files to be included in the tarball. # - BUTANE_BLOCKLIST: the path to a file containing a list of files and directories (one per line) to ignore # (i.e., files and directories that are already part of the CoreOS default installation # or belonging to another package). # - SYSTEMD_MAIN_UNIT_NAMES: the list of systemd main unit names to enable. # # The path to of the output tarball is $1. # set -Eeuo pipefail # Avoid issues with translated output of commands like stat. export LANG=C.UTF-8 export LC_ALL=C.UTF-8 # Create a temporary directory to store intermediate files (metadata.json and content.tar) tmp_dir=$(mktemp -d) trap 'rm -rf "$tmp_dir"' EXIT # Generate the file list from the TARGET_CHROOT, excluding files and directories in the BUTANE_BLOCKLIST declare -A files_to_include=() inner_filelist_file="$tmp_dir/filelist.txt" for path in $(find "$TARGET_CHROOT"); do rel_path="${path#$TARGET_CHROOT}" # Skip files & directories that are already part of the CoreOS default installation if grep -qxF "$rel_path" "$BUTANE_BLOCKLIST"; then continue fi # Skip the root directory and empty paths if [ -z "$rel_path" ] || [[ "$rel_path" == "/" ]]; then continue fi # The leading / is removed from the relative path in order for tar to find the file. echo "${rel_path#/}" >> "$inner_filelist_file" # Although, the absolute path is stored in the metadata file. files_to_include["$rel_path"]="$(stat --format='%u %g %a %F' "$path")" done # Generate metadata.json metadata_file="$tmp_dir/metadata.yaml" exec 3>&1 > "$metadata_file" cat <&2 exit 1 fi cat <&3 # The list of files to be included in the outer tarball. declare -a outer_filelist=( "metadata.json" ) # Convert metadata.yaml to metadata.json yq -o json "$metadata_file" > "$tmp_dir/metadata.json" # Add the project's README file to the tarball if it exists. if [ -f "README.md" ]; then cp "README.md" "$tmp_dir/README.md" outer_filelist+=("README.md") fi # Common tar options to ensure that the tarball is reproducible and does not contain unnecessary metadata declare -a tar_options=( "--no-selinux" "--no-recursion" "--no-xattrs" "--no-acls" ) # Generate content.tar with the files to be included in the tarball, preserving their relative paths and permissions. tar -cf "$tmp_dir/content.tar" "${tar_options[@]}" -C "$TARGET_CHROOT" --verbatim-files-from --files-from="$inner_filelist_file" outer_filelist+=("content.tar") # Generate the final tarball. tar -czf "$1" "${tar_options[@]}" -C "$tmp_dir" --owner=0 --group=0 "${outer_filelist[@]}"