mirror of
https://github.com/AlmaLinux/ks2rootfs.git
synced 2024-11-25 18:43:08 +00:00
205 lines
6.0 KiB
Bash
Executable File
205 lines
6.0 KiB
Bash
Executable File
#!/bin/bash -e
|
|
# description: CI/CD Utility helper. Builds linux rootfs file from kickstart input file in docker/podman enviroment
|
|
# param: 1. kickstart file and 2. rootfs file name to be generated
|
|
# license: MIT.
|
|
|
|
usage() {
|
|
cat 1>&2 <<EOF
|
|
Script to create roofs file from a kickstart file using livemedia-creator
|
|
|
|
ENVIRONMENT VARIABLES:
|
|
======================
|
|
|
|
KICKSTART_FILE : Reuired - Input kickstart source file (.ks)
|
|
IMAGE_NAME : Required - Rootfs output file name
|
|
|
|
BUILD_WORK_DIR : Optional - Working dir for kickstart source and image destination. Defaults to current directory.
|
|
OUTPUT_DIR : Optional - Output directory name in working dir (default - 'result') - Optional
|
|
FLAG_OUTOUT_IN_PWD : Optional - Set this flag to true to write output files in current working directory. Default value is 'false'. When value is set to 'true', any value passed to 'OUTPUT_DIR' will be ignored.
|
|
FLAG_WRITE_META : Optional - Generate meta data about the kickstart build system. Default value is 'true'.
|
|
FLAG_RETAIN_LOG : Optional - When enabled, generated logs files retained under 'logs' output directory. Default value is 'false'.
|
|
BUILD_COMPTYPE : Optional - Build compression type default 'xz', 'gzip' and 'lzma'.
|
|
BUILD_TYPE : Optional - Build type 'base', 'default', 'init', 'micro', 'minimal' and 'wsl', Default value is 'default'.
|
|
|
|
USAGE:
|
|
ks2rootfs KICKSTART_FILE_NAME ROOTFS_FILE_NAME
|
|
|
|
EXAMPLES:
|
|
ks2rootfs os-minimal.ks os-minimal.tar.xz
|
|
EOF
|
|
}
|
|
|
|
run-summary() {
|
|
cat 1>&2 <<EOF
|
|
|
|
ks2rootfs - Script input summary:
|
|
|
|
FLAGS
|
|
-----
|
|
FLAG_OUTOUT_PWD : ${FLAG_OUTOUT_IN_PWD}
|
|
FLAG_WRITE_META : ${FLAG_WRITE_META}
|
|
FLAG_RETAIN_LOG : ${FLAG_RETAIN_LOG}
|
|
|
|
VARIABLES
|
|
---------
|
|
BUILD_WORK_DIR : ${BUILD_WORK_DIR}
|
|
OUTPUT_DIR : ${OUTPUT_DIR}
|
|
BUILD_LOGDIR : ${BUILD_LOGDIR}
|
|
INPUT_KICKSTART : ${BUILD_WORK_DIR}${KICKSTART_FILE}
|
|
OUTPUT_ROOTFS : ${OUTPUT_DIR}/${IMAGE_NAME}
|
|
BUILD_COMPTYPE : ${BUILD_COMPTYPE}
|
|
BUILD_TYPE : ${BUILD_TYPE}
|
|
EOF
|
|
}
|
|
|
|
BUILD_WORK_DIR=${BUILD_WORK_DIR:-./}
|
|
KICKSTART_FILE=${KICKSTART_FILE:-$1}
|
|
IMAGE_NAME=${IMAGE_NAME:-$2}
|
|
FLAG_WRITE_META=${FLAG_WRITE_META:-false}
|
|
FLAG_RETAIN_LOG=${FLAG_RETAIN_LOG:-false}
|
|
FLAG_OUTOUT_IN_PWD=${FLAG_OUTOUT_IN_PWD:-false}
|
|
BUILD_COMPTYPE=${BUILD_COMPTYPE:-xz}
|
|
BUILD_TYPE=${BUILD_TYPE:-default}
|
|
TYPE='default'
|
|
|
|
USE_PWD_OUTPUT=${USE_PWD_OUTPUT:-false}
|
|
|
|
OUTPUT_DIR=${OUTPUT_PREFIX:-result}_$(uname -m)
|
|
BUILD_LOGDIR=${OUTPUT_DIR}/logs
|
|
|
|
if [ ${FLAG_OUTOUT_IN_PWD} == 'true' ]; then
|
|
echo 'Build output files will be in current working folder ... '
|
|
OUTPUT_DIR=${BUILD_WORK_DIR}
|
|
else
|
|
OUTPUT_DIR=${BUILD_WORK_DIR}${OUTPUT_DIR:-result}
|
|
echo "Build output files will be in '${OUTPUT_DIR}' folder ... "
|
|
if [[ -d "${OUTPUT_DIR}" ]]; then
|
|
echo "Output directory ${OUTPUT_DIR} already exists, please remove it"
|
|
exit 1
|
|
fi
|
|
mkdir -p ${OUTPUT_DIR}
|
|
if [[ -d "${OUTPUT_DIR}" ]]; then
|
|
echo "Output directory ${OUTPUT_DIR} created successfully"
|
|
else
|
|
echo "Error in creating output directory: ${OUTPUT_DIR}"
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
case "${BUILD_TYPE}" in
|
|
base|default|init|micro|minimal|wsl)
|
|
TYPE="${BUILD_TYPE}"
|
|
;;
|
|
*)
|
|
echo "Error: unsupported build type ${BUILD_TYPE}" 1>&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
if [ ${TYPE} == 'wsl' ]; then
|
|
BUILD_COMPTYPE='gzip'
|
|
fi
|
|
|
|
BUILD_LOGDIR=${OUTPUT_DIR}/logs
|
|
|
|
if [ -z ${KICKSTART_FILE} ] || [ -z ${IMAGE_NAME} ]
|
|
then
|
|
echo "Please provide two parameters, kickstart source file and rootfs output file name"
|
|
usage
|
|
exit 1
|
|
fi
|
|
|
|
# Write run summary to console
|
|
run-summary
|
|
|
|
# set anaconda base product
|
|
cat << _EOF > /etc/anaconda/product.d/ks2rootfs.conf
|
|
# Anaconda configuration file for Kickstart to Rootfs.
|
|
|
|
[Product]
|
|
product_name = Kickstart to RootFS
|
|
|
|
[Base Product]
|
|
product_name = AlmaLinux
|
|
|
|
[Storage]
|
|
file_system_type =
|
|
|
|
[License]
|
|
eula =
|
|
_EOF
|
|
|
|
# create rootfs
|
|
livemedia-creator --logfile="/tmp/ks2rootfs.log" \
|
|
--make-tar --ks="${BUILD_WORK_DIR}/${KICKSTART_FILE}" --no-virt \
|
|
--image-only --image-name="${IMAGE_NAME}" \
|
|
--anaconda-arg "--product Kickstart to RootFS" \
|
|
--compression ${BUILD_COMPTYPE}
|
|
|
|
# copy rootfs to working dir
|
|
cp -rp /var/tmp/${IMAGE_NAME} ${OUTPUT_DIR}/${IMAGE_NAME}
|
|
echo "Copied disk image from /var/tmp/${IMAGE_NAME} to ${OUTPUT_DIR}/${IMAGE_NAME}"
|
|
# save list of packages installed
|
|
jq .[] -r /tmp/dnf.cache/tempfiles.json | awk -F '/' '{print $5}' | sort > ${OUTPUT_DIR}/rpm-packags
|
|
# cat ${OUTPUT_DIR}/rpm-packags | sed 's/-[0-9].*//g' > ${OUTPUT_DIR}/pkgs-list-only
|
|
# cat ${OUTPUT_DIR}/rpm-packags | sed 's/.el[0-9].*//g' > ${OUTPUT_DIR}/pkgs-list
|
|
echo "Generated '${OUTPUT_DIR}/rpm-packags' file."
|
|
|
|
if [ ${FLAG_WRITE_META} == 'true' ]; then
|
|
# extract os-release info
|
|
tar -xvf /var/tmp/${IMAGE_NAME} -C /tmp/ --strip-components=3 ./usr/lib/os-release
|
|
os_release_id=$(awk -F= '$1=="ID" { print $2 ;}' /tmp/os-release | tr -d '"')
|
|
distro_release=$(grep "${os_release_id}.*-release-" /tmp/anaconda/packaging.log | grep -o "Verifying:.*" | sed -n 's/Verifying: //p')
|
|
if [ -z ${distro_release+x} ]; then
|
|
exit 1
|
|
else
|
|
echo 'Writting meta data ... '
|
|
# save distro release info
|
|
echo "$distro_release" > ${OUTPUT_DIR}/distro-release
|
|
fi
|
|
echo 'Writting meta data completed.'
|
|
else
|
|
echo 'Skip writing meta data.'
|
|
fi
|
|
|
|
if [ ${FLAG_RETAIN_LOG} == 'true' ]; then
|
|
rm -rf ${BUILD_LOGDIR}
|
|
mkdir -p ${BUILD_LOGDIR} ${BUILD_LOGDIR}/anaconda
|
|
echo "Copying logs to '${BUILD_LOGDIR}' ..."
|
|
cp /tmp/ks2rootfs.log rm -rf ${BUILD_LOGDIR}/ks2rootfs.log
|
|
cp -rp /tmp/anaconda/* ${BUILD_LOGDIR}/anaconda
|
|
echo 'Copying logs completed.'
|
|
else
|
|
echo 'Skip copying logs data based on configuration (default). '
|
|
fi
|
|
|
|
STARTCMD=$( cat <<EOF
|
|
|
|
CMD ["/bin/bash"]
|
|
EOF
|
|
)
|
|
|
|
if [ ${TYPE} == 'init' ]; then
|
|
STARTCMD=$( cat <<EOF
|
|
|
|
STOPSIGNAL SIGRTMIN+3
|
|
CMD ["/bin/init"]
|
|
EOF
|
|
)
|
|
fi
|
|
|
|
if [ ${TYPE} == 'micro' ]; then
|
|
STARTCMD=$( cat <<EOF
|
|
|
|
CMD ["/bin/sh"]
|
|
EOF
|
|
)
|
|
fi
|
|
|
|
cat << EOF > "${OUTPUT_DIR}/Dockerfile"
|
|
FROM scratch
|
|
ADD ${IMAGE_NAME} /
|
|
${STARTCMD}
|
|
EOF
|
|
echo "Generating '${OUTPUT_DIR}/Dockerfile' completed."
|