Files
SalimTerryLi e3f008aee4 container: rootless operation
pseudo is carried as an on-demand operation that handles xattr only.

Assisted-by: GPT-5.6-Sol
2026-08-31 13:40:23 +08:00

127 lines
3.0 KiB
Bash
Executable File

#!/usr/bin/env sh
# shellcheck shell=dash disable=SC2317
# Unreachable Detection is disabled as the script relies on indirect call.
set -o errexit
set -o nounset
. ./logging.sh
export PSEUDO_PREFIX="${PSEUDO_PREFIX:-/usr}"
export PSEUDO_LOCALSTATEDIR="${PSEUDO_LOCALSTATEDIR:-/var/pseudo}"
RECEIPE_DIR=""
ROOTFS_DIR=""
OUTPUT_ARCHIVE=""
PACKAGE_MANAGER=""
COMPRESSION=""
. ./receipe_helper.sh
generate_rootfs() {
SCRIPT_ENV_SETUP_CMD="
export ROOTFS_DIR=${ROOTFS_DIR} &&
"
setup_repo
install_packages "${SCRIPT_ENV_SETUP_CMD}"
post_scripts "${SCRIPT_ENV_SETUP_CMD}"
}
create_archive() {
case "${COMPRESSION}" in
zstd)
logcmd pseudo tar --use-compress-program=zstd --xattrs --xattrs-include='*' \
-cf "${OUTPUT_ARCHIVE}" -C "${ROOTFS_DIR}" .
;;
*)
loge "Unsupported compression mode: ${COMPRESSION}"
exit 1
;;
esac
}
print_usage() {
# Keep help message within 80 cols for special consoles.
cat << EOF
Usage: $0 [-h] -i receipe -o archive -p package_manager -c compression
-h Print this help message.
-i receipe Path to directory containing the receipe
to make the rootfs.
-o archive Path to the output rootfs archive.
-p package_manager
Specify which package manager should be
used.
-c compression Specify the archive compression method.
Environment Variables:
LOG_LEVEL Controls the log level of this script.
One of: DEBUG, INFO, WARN, ERROR
Defaults to: INFO
EOF
}
main() {
# Handling args
while getopts "i:o:p:c:h" o; do
case "${o}" in
h)
print_usage
exit 0
;;
i)
RECEIPE_DIR=${OPTARG}
;;
o)
OUTPUT_ARCHIVE=${OPTARG}
;;
p)
PACKAGE_MANAGER=${OPTARG}
;;
c)
COMPRESSION=${OPTARG}
;;
*)
exit 1
;;
esac
done
shift $((OPTIND-1))
logi "RootFS Builder"
# Validate inputs
if [ -z "${RECEIPE_DIR}" ]; then
loge "No receipe is specified!"
exit 1
fi
if [ -z "${OUTPUT_ARCHIVE}" ]; then
loge "No output archive is specified!"
exit 1
fi
if [ -z "${PACKAGE_MANAGER}" ]; then
loge "No package manager is specified!"
exit 1
fi
if [ -z "${COMPRESSION}" ]; then
loge "No compression method is specified!"
exit 1
fi
ROOTFS_DIR="$(mktemp -d)"
trap 'rm -rf "${ROOTFS_DIR}"' EXIT
logi "Using receipe: ${RECEIPE_DIR}"
logi "- OUTPUT_ARCHIVE: ${OUTPUT_ARCHIVE}"
logi "- PACKAGE_MANAGER: ${PACKAGE_MANAGER}"
logi "- COMPRESSION: ${COMPRESSION}"
generate_rootfs
create_archive
logi "RootFS archive generated at: ${OUTPUT_ARCHIVE}"
exit 0
}
main "$@"