Fix #1, #2, #3 - Script update, params, documentaion and test scripts

Signed-off-by: Bala Raman <srbala@gmail.com>
This commit is contained in:
Bala Raman 2021-06-23 12:56:33 -04:00
parent 18d0fdad4c
commit 5587ba7a77
No known key found for this signature in database
GPG Key ID: 23A07BF21B5D6625
4 changed files with 229 additions and 26 deletions

View File

@ -1,7 +1,11 @@
# ----------------------------------------------------------------------------
# Multi stage build, using Micro version AlmaLinux as foundation for end image
# Final image designed for specific purposes, don't have dnf, microdnf or yum
# ----------------------------------------------------------------------------
FROM almalinux:8 as builder
RUN mkdir -p /mnt/system-root /mnt/system-root/build; \
dnf install --installroot /mnt/system-root --releasever 8 --setopt=install_weak_deps=false --setopt=tsflags=nodocs -y coreutils-single \
dnf install --installroot /mnt/system-root --releasever 8 --setopt=install_weak_deps=False --setopt=tsflags=nodocs -y coreutils-single \
bash \
glibc-minimal-langpack \
anaconda-tui \
@ -9,6 +13,7 @@ RUN mkdir -p /mnt/system-root /mnt/system-root/build; \
jq \
tar \
policycoreutils \
# Optional include to avoid runtime warning -- starts
libblockdev-mdraid \
libblockdev-crypto \
libblockdev-lvm \
@ -17,11 +22,13 @@ RUN mkdir -p /mnt/system-root /mnt/system-root/build; \
libblockdev-loop \
libblockdev-nvdimm \
libblockdev-mpath \
# Optional include to avoid runtime warning -- ends
rootfiles; \
rm -rf /mnt/system-root/var/cache/* ; \
dnf clean all; \
cp /etc/yum.repos.d/* /mnt/system-root/etc/yum.repos.d/ ; \
rm -rf /var/cache/yum; \
# TODO: commands below move to side script or remove?
# generate build time file for compatibility with CentOS
/bin/date +%Y%m%d_%H%M > /mnt/system-root/etc/BUILDTIME ;\
# set DNF infra variable to container for compatibility with CentOS

View File

@ -5,8 +5,10 @@ This project provides the ability build `rootfs` file from `kickstart` input fil
## HOW-TO
Image yet ot published in `hub.docker.com`, until then use local build.
### Building local
```
```sh
docker build -t srbala/ks2rootfs .
```
@ -14,9 +16,33 @@ docker build -t srbala/ks2rootfs .
Following command uses the `kickstarts/almalinux-8-default.x86_64.ks` file to build.
```
```sh
docker run --rm --privileged -v "$PWD:/build:z" \
-e BUILD_KICKSTART=kickstarts/almalinux-8-default.x86_64.ks \
-e BUILD_ROOTFS=almalinux-8-default-docker.x86_64.tar.gz \
srbala/ks2rootfs
```
### Environment variables
Container startup script `ks2rootfs` supports multiple environment varible to customize the output. The environment variables and their use as follows
```sh
ENVIRONMENT VARIABLES:
======================
BUILD_KICKSTART : Input kickstart source file (.ks) - Required
BUILD_ROOTFS : Rootfs output file name - Required
BUILD_WORK_DIR : Working dir for kickstart source and image destination (default current directory) - Optional
BUILD_OUTDIR : Output directory name in working dir - Optional
BUILD_FLAG_OUTOUT_IN_PWD : 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 'BUILD_OUTDIR' will be ignored.
BUILD_FLAG_WRITE_META : Generate meta data about the kickstart build system - Optional
BUILD_FLAG_RETAIN_LOG : Retain generated output log files under 'logs' output directory - Optional
USAGE:
ks2rootfs KICKSTART_FILE_NAME ROOTFS_FILE_NAME
EXAMPLES:
ks2rootfs os-minimal.ks os-minimal.tar.xz
```

View File

@ -5,28 +5,83 @@
usage() {
cat 1>&2 <<EOF
Script to create roofs from kickstart file using livemedia-creator
Script to create roofs file from a kickstart file using livemedia-creator
ENVIRONMENT VARS:
BUILD_WORK_DIR Working dir for kickstart source and image destination (default current directory)
BUILD_KICKSTART Input kickstart source file (.ks)
BUILD_ROOTFS Rootfs output file name
ENVIRONMENT VARIABLES:
======================
BUILD_KICKSTART : Input kickstart source file (.ks) - Required
BUILD_ROOTFS : Rootfs output file name - Required
BUILD_WORK_DIR : Working dir for kickstart source and image destination (default current directory) - Optional
BUILD_OUTDIR : Output directory name in working dir - Optional
BUILD_FLAG_OUTOUT_IN_PWD : 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 `BUILD_OUTDIR` will be ignored.
BUILD_FLAG_WRITE_META : Generate meta data about the kickstart build system - Optional
BUILD_FLAG_RETAIN_LOG : Retain generated output log files under 'logs' output directory - Optional
USAGE:
ks2rootfs KICKSTART_FILE_NAME ROOTFS_FILE_NAME
EXAMPLES:
ks2rootfs centos8-minimal.ks centos8-minimal.tar.xz
ks2rootfs os-minimal.ks os-minimal.tar.xz
EOF
}
run-summary() {
cat 1>&2 <<EOF
ks2rootfs - Script input summary:
FLAGS
-----
FLAG_OUTOUT_PWD : ${BUILD_FLAG_OUTOUT_IN_PWD}
FLAG_WRITE_META : ${BUILD_FLAG_WRITE_META}
FLAG_RETAIN_LOG : ${BUILD_FLAG_RETAIN_LOG}
VARIABLES
---------
BUILD_WORK_DIR : ${BUILD_WORK_DIR}
BUILD_OUTDIR : ${BUILD_OUTDIR}
BUILD_LOGDIR : ${BUILD_LOGDIR}
INPUT_KICKSTART : ${BUILD_WORK_DIR}${BUILD_KICKSTART}
OUTPUT_ROOTFS : ${BUILD_OUTDIR}/${BUILD_ROOTFS}
EOF
}
BUILD_WORK_DIR=${BUILD_WORK_DIR:-./}
BUILD_KICKSTART=${BUILD_KICKSTART:-$1}
BUILD_ROOTFS=${BUILD_ROOTFS:-$2}
BUILD_OUTDIR=${BUILD_OUTDIR:-./result}
BUILD_WRITE_META=${BUILD_WRITE_META:-true}
BUILD_RETAIN_LOG=${BUILD_RETAIN_LOG:-false}
BUILD_FLAG_WRITE_META=${BUILD_FLAG_WRITE_META:-true}
BUILD_FLAG_RETAIN_LOG=${BUILD_FLAG_RETAIN_LOG:-false}
BUILD_FLAG_OUTOUT_IN_PWD=${BUILD_FLAG_OUTOUT_IN_PWD:-false}
USE_PWD_OUTPUT=${USE_PWD_OUTPUT:-false}
BUILD_OUTDIR=${BUILD_OUTDIR:-result}
BUILD_LOGDIR=${BUILD_OUTDIR}/logs
if [ ${BUILD_FLAG_OUTOUT_IN_PWD} == 'true' ]; then
echo 'Build output files will be in current working folder ... '
BUILD_OUTDIR=${BUILD_WORK_DIR}
else
BUILD_OUTDIR=${BUILD_WORK_DIR}${BUILD_OUTDIR:-result}
echo "Build output files will be in '${BUILD_OUTDIR}' folder ... "
if [[ -d "${BUILD_OUTDIR}" ]]; then
echo "Output directory ${BUILD_OUTDIR} already exists, please remove it"
exit 1
fi
mkdir -p ${BUILD_OUTDIR}
if [[ -d "${BUILD_OUTDIR}" ]]; then
echo "Output directory ${BUILD_OUTDIR} created successfully"
else
echo "Error in creating output directory: ${BUILD_OUTDIR}"
exit 1
fi
fi
BUILD_LOGDIR=${BUILD_OUTDIR}/logs
if [ -z ${BUILD_KICKSTART} ] || [ -z ${BUILD_ROOTFS} ]
then
@ -35,6 +90,9 @@ then
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.
@ -53,25 +111,38 @@ eula =
_EOF
# create rootfs
livemedia-creator --logfile="/tmp/rootfs-creator.log" \
livemedia-creator --logfile="/tmp/ks2rootfs.log" \
--make-tar --ks="${BUILD_WORK_DIR}/${BUILD_KICKSTART}" --no-virt \
--image-only --image-name="${BUILD_ROOTFS}" \
--anaconda-arg "--product Kickstart to Rootfs"
# move rootfs to working dir
cp -rp /var/tmp/${BUILD_ROOTFS} ${BUILD_WORK_DIR}/${BUILD_ROOTFS}
# copy rootfs to working dir
cp -rp /var/tmp/${BUILD_ROOTFS} ${BUILD_OUTDIR}/${BUILD_ROOTFS}
# extract os-release info
tar -xvf /var/tmp/${BUILD_ROOTFS} -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
if [ ${BUILD_FLAG_WRITE_META} == 'true' ]; then
# extract os-release info
tar -xvf /var/tmp/${BUILD_ROOTFS} -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" > ${BUILD_OUTDIR}/distro-release
fi
# save list of packages installed
jq .[] -r /tmp/dnf.cache/tempfiles.json | awk -F '/' '{print $5}' | sort > ${BUILD_OUTDIR}/pkgs-list
else
# save distro release info
echo "$distro_release" > ${BUILD_WORK_DIR}/distro-release
echo 'Skip writing meta data based on configuration.'
fi
# save list of packages installed
jq .[] -r /tmp/dnf.cache/tempfiles.json | awk -F '/' '{print $5}' | sort > ${BUILD_WORK_DIR}/pkgs-list
if [ ${BUILD_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
else
echo 'Skip writing logs data based on configuration (default). '
fi

View File

@ -0,0 +1,99 @@
# AlmaLinux 8 kickstart file for x86_64 base Docker image
# install
url --url https://repo.almalinux.org/almalinux/8/BaseOS/x86_64/os/
lang en_US.UTF-8
keyboard us
timezone --nontp --utc UTC
network --activate --bootproto=dhcp --device=link --onboot=on
firewall --disabled
selinux --disabled
bootloader --disable
zerombr
clearpart --all --initlabel
autopart --fstype=ext4 --type=plain --nohome --noboot --noswap
rootpw --iscrypted --lock almalinux
shutdown
%packages --ignoremissing --excludedocs --instLangs=en --nocore --excludeWeakdeps
almalinux-release
bash
binutils
coreutils-single
dnf
findutils
glibc-minimal-langpack
hostname
iputils
less
rootfiles
tar
vim-minimal
yum
xz
-brotli
-crypto-policies-scripts
-firewalld
-diffutils
-elfutils-debuginfod-client
-gettext*
-glibc-langpack-en
-gnupg2-smime
-grub\*
-iptables
-kernel
-libevent
-openssl
-os-prober
-open-vm-tools
-pinentry
-platform-python-pip
-shared-mime-info
-trousers
-unbound-libs
-xkeyboard-config
%end
%post --erroronfail --log=/root/anaconda-post.log
# generate build time file for compatibility with CentOS
/bin/date +%Y%m%d_%H%M > /etc/BUILDTIME
# set DNF infra variable to container for compatibility with CentOS
echo 'container' > /etc/dnf/vars/infra
# import AlmaLinux PGP key
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-AlmaLinux
# install only en_US.UTF-8 locale files, see
# https://fedoraproject.org/wiki/Changes/Glibc_locale_subpackaging for details
echo '%_install_langs en_US.UTF-8' > /etc/rpm/macros.image-language-conf
# force each container to have a unique machine-id
> /etc/machine-id
# create tmp directories because there is no tmpfs support in Docker
umount /run
systemd-tmpfiles --create --boot
# disable login prompt and mounts
systemctl mask console-getty.service \
dev-hugepages.mount \
getty.target \
systemd-logind.service \
sys-fs-fuse-connections.mount \
systemd-remount-fs.service
# remove unnecessary files
rm -f /var/lib/dnf/history.* \
/run/nologin
rm -fr /var/log/* \
/tmp/* /tmp/.* \
/boot || true
%end