From 5587ba7a77cf7faa890d7836b4a6b4aad98d8e33 Mon Sep 17 00:00:00 2001 From: Bala Raman Date: Wed, 23 Jun 2021 12:56:33 -0400 Subject: [PATCH] Fix #1, #2, #3 - Script update, params, documentaion and test scripts Signed-off-by: Bala Raman --- Dockerfile | 9 +- README.md | 30 ++++- scripts/ks2rootfs | 117 ++++++++++++++---- .../kickstarts/almalinux-8-default.x86_64.ks | 99 +++++++++++++++ 4 files changed, 229 insertions(+), 26 deletions(-) create mode 100644 tests/kickstarts/almalinux-8-default.x86_64.ks diff --git a/Dockerfile b/Dockerfile index e6ce15e..6e0cdf5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/README.md b/README.md index 058038d..b5548dc 100644 --- a/README.md +++ b/README.md @@ -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 +``` diff --git a/scripts/ks2rootfs b/scripts/ks2rootfs index b33a0cc..76d3763 100755 --- a/scripts/ks2rootfs +++ b/scripts/ks2rootfs @@ -5,28 +5,83 @@ usage() { cat 1>&2 <&2 < /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 diff --git a/tests/kickstarts/almalinux-8-default.x86_64.ks b/tests/kickstarts/almalinux-8-default.x86_64.ks new file mode 100644 index 0000000..9e22732 --- /dev/null +++ b/tests/kickstarts/almalinux-8-default.x86_64.ks @@ -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