From f8af8d50b95fb92cbffb8b0bf16706fffd6a00fb Mon Sep 17 00:00:00 2001 From: Daniele Rondina Date: Mon, 11 Jun 2018 17:21:58 +0200 Subject: [PATCH] Bump first version --- .gitignore | 1 + Dockerfile | 43 ++++++ README.md | 57 ++++++++ scripts/sabayon_molecules.sh | 262 +++++++++++++++++++++++++++++++++++ 4 files changed, 363 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 scripts/sabayon_molecules.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1377554 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.swp diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2dff609 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,43 @@ +# Sabayon ISO Builder image +ARG USER=sabayon +ARG CUSTOM_IMAGE_PREFIX= + +# Set arch +ARG TARGET_ARCH=amd64 +FROM ${USER}/${CUSTOM_IMAGE_PREFIX}base-${TARGET_ARCH} + +MAINTAINER geaaru + +# Add sabayon-stuff files +ADD ./scripts/sabayon_molecules.sh /sabayon_molecules.sh + +# Set locales to en_US.UTF-8 +ENV LC_ALL=en_US.UTF-8 + +ARG SABAYON_MOLECULES_DIR=/sabayon +ENV SABAYON_MOLECULES_DIR=${SABAYON_MOLECULES_DIR} +ARG SABAYON_MOLECULES_GITURL=https://github.com/Sabayon/molecules.git +ENV SABAYON_MOLECULES_GITURL=${SABAYON_MOLECULES_GITURL} +ARG SABAYON_MOLECULES_GIT_OPTS= +ENV SABAYON_MOLECULES_GIT_OPTS=${SABAYON_MOLECULES_GIT_OPTS} +ARG SABAYON_MOLECULES_ENVFILE= +ENV SABAYON_MOLECULES_ENVFILE=${SABAYON_MOLECULES_ENVFILE} + +ARG CACHEBUST=1 +RUN chmod a+x /sabayon_molecules.sh && \ + /sabayon_molecules.sh init && \ + /sabayon_molecules.sh phase1 && \ + /sabayon_molecules.sh clean + +# Set environment variables. +ENV HOME ${SABAYON_MOLECULES_DIR} + +# Define working directory. +WORKDIR ${SABAYON_MOLECULES_DIR} + +# Docker service requirement for systemd mode. +#VOLUME ["/sys/fs/cgroup"] + +# Define default command. +ENTRYPOINT ["/usr/bin/tini", "-s", "--", "/sabayon_molecules.sh", "run"] + diff --git a/README.md b/README.md new file mode 100644 index 0000000..1678149 --- /dev/null +++ b/README.md @@ -0,0 +1,57 @@ +# ISO Builder Image + +Docker Image for build Sabayon ISO images that use [iso_build.sh](https://github.com/Sabayon/molecules.git). + +Image has two mode: + + * SystemD Mode: when SABAYON_MOLECULES_SYSTEMD_MODE is equal to 1 container is + started with SytemdD service and a Docker in Docker service and use internal + Docker service for retrieve Spinbase image to use for create ISO images. + This mode require mount of /sys/fs/cgroup volume. + + + * Normal Mode: when SABAYON_MOLECULES_SYSTEMD_MODE is equal to 0 container + use Docker Host service. This require volume + + +## Usage + +Example of SystemD Mode: + +``` + +$#. docker run --init --device /dev/fuse --tmpfs /run --tmpfs /tmp \ + -v /bigdisk/molecules-chroots:/chroots \ + -v /bigdisk/molecules-sources:/sources \ + -v /sabayon/iso:/iso \ + -v /sys/fs/cgroup:/sys/fs/cgroup:ro \ + --rm --name build-sab-iso \ + --cap-add=SYS_PTRACE --cap-add=SYS_ADMIN --cap-add=NET_ADMIN --cap-add=MKNOD \ + --device=/dev/loop-control:/dev/loop-control \ + --device=/dev/loop0:/dev/loop0 \ + --device=/dev/loop1:/dev/loop1 \ + -e COLUMNS=200 -e LINES=400 \ + -e SABAYON_MOLECULES_CHROOTS=/chroots \ + -e SABAYON_MOLECULES_SOURCES=/sources \ + -e SABAYON_MOLECULES_ISO=/iso \ + sabayon/isobuilder-amd64 + +``` + +On example it is overrided default chroots and sources directory for use disk with more +space. + +ISOs will be available on /sabayon/iso host directory. + + +| Env Variable | Default | Description | +| SABAYON_MOLECULES_GITURL | https://github.com/Sabayon/molecules.git | Git Repository of Sabayon Molecules configurations. | +| SABAYON_MOLECULES_GIT_OPTS | - | Permit to define additional git clone options, like use a specific branch. | +| SABAYON_MOLECULES_DIR | /sabayon | Molecule work directory | +| SABAYON_MOLECULES_CHROOTS | - | If present permit to mount in binding path to molecules chroots directory | +| SABAYON_MOLECULES_SOURCES | - | If present permit to mount in binding selected path to molecules sources directory. | +| SABAYON_MOLECULES_ISO | - | If present permit to mount in binding selected path to molecules iso directory | +| SABAYON_MOLECULES_ENVFILE | $(pwd)/confs/iso_build.env | Optional file to sources for override environment variables. | +| SABAYON_MOLECULES_SYSTEMD_MODE | 0 | Enable SystemD Mode (1) or not (0). +| SABAYON_MOLECULES_POSTSCRIPT | - | Define post script to execute. | + diff --git a/scripts/sabayon_molecules.sh b/scripts/sabayon_molecules.sh new file mode 100644 index 0000000..bc7a673 --- /dev/null +++ b/scripts/sabayon_molecules.sh @@ -0,0 +1,262 @@ +#!/bin/bash +# Author: Geaaru, geaaru@sabayonlinux.org + +SABAYON_MOLECULES_GITURL=${SABAYON_MOLECULES_GITURL:-https://github.com/Sabayon/molecules.git} +SABAYON_MOLECULES_GIT_OPTS=${SABAYON_MOLECULES_GIT_OPTS:-} +SABAYON_MOLECULES_DIR=${SABAYON_MOLECULES_DIR:-/sabayon} +SABAYON_MOLECULES_CHROOTS=${SABAYON_MOLECULES_CHROOTS:-} +SABAYON_MOLECULES_SOURCES=${SABAYON_MOLECULES_SOURCES:-} +SABAYON_MOLECULES_ISO=${SABAYON_MOLECULES_ISO:-} +SABAYON_MOLECULES_ENVFILE=${SABAYON_MOLECULES_ENVFILE:-$(pwd)/confs/iso_build.env} +SABAYON_MOLECULES_SYSTEMD_MODE=${SABAYON_MOLECULES_SYSTEMD_MODE:-0} +SABAYON_MOLECULES_POSTSCRIPT=${SABAYON_MOLECULES_POSTSCRIPT:-} + +sabayon_molecules_info () { + + local args=$@ + local info_args="" + + if [ "${args}"x != ""x ] ; then + info_args="SABAYON_MOLECULES_RUN_ARGS = ${args}" + fi + + echo " +SABAYON_MOLECULES_GITURL = ${SABAYON_MOLECULES_GITURL} +SABAYON_MOLECULES_GIT_OPTS = ${SABAYON_MOLECULES_GIT_OPTS} +SABAYON_MOLECULES_DIR = ${SABAYON_MOLECULES_DIR} +SABAYON_MOLECULES_ENVFILE = ${SABAYON_MOLECULES_ENVFILE} +SABAYON_MOLECULES_SYSTEMD_MODE = ${SABAYON_MOLECULES_SYSTEMD_MODE} +SABAYON_MOLECULES_ISO = ${SABAYON_MOLECULES_ISO} +SABAYON_MOLECULES_CHROOTS = ${SABAYON_MOLECULES_CHROOTS} +SABAYON_MOLECULES_SOURCES = ${SABAYON_MOLECULES_SOURCES} +${info_args} +" + return 0 +} + +PACKAGES_TO_ADD=( + "mail-mta/postfix" + "net-p2p/mktorrent-borg" + "sys-fs/dosfstools" + "dev-util/molecule" + "dev-vcs/git" + "app-misc/ca-certificates" + "app-emulation/docker" + "app-emulation/docker-companion" + "sys-process/tini" + # For isohybrid + "sys-boot/syslinux" +) + +FILES_TO_REMOVE=( + "/etc/entropy/packages/license.accept" +) + +sabayon_init_env () { + + local mirror_name=${1:-sabayonlinux.org} + + if [[ -z "${SABAYON_SKIP_MIRRORSORT}" || "${SABAYON_SKIP_MIRRORSORT}" == "0" ]] ; then + equo repo mirrorsort ${mirror_name} + fi + + if [[ -z "${SABAYON_SKIP_SYNC}" || "${SABAYON_SKIP_SYNC}" == "0" ]] ; then + equo up || return 1 + fi + + if [[ -z "${SABAYON_SKIP_UPGRADE}" || "${SABAYON_SKIP_UPGRADE}" == "0" ]] ; then + ETP_NONINTERACTIVE=1 equo u || return 1 + echo -5 | equo conf update || return 1 + fi + + return 0 +} + +sabayon_molecules_init () { + + local mirror_name=${1:-sabayonlinux.org} + + sabayon_molecules_info + + mkdir -p /usr/portage/licenses || return 1 + rsync -av "rsync://rsync.at.gentoo.org/gentoo-portage/licenses/" \ + "/usr/portage/licenses/" && ls /usr/portage/licenses -1 | xargs -0 > /etc/entropy/packages/license.accept || return 1 + + if [[ -z "${SABAYON_SKIP_MIRRORSORT}" || "${SABAYON_SKIP_MIRRORSORT}" == "0" ]] ; then + equo repo mirrorsort ${mirror_name} + fi + + if [[ -z "${SABAYON_SKIP_SYNC}" || "${SABAYON_SKIP_SYNC}" == "0" ]] ; then + equo up || return 1 + fi + + if [[ -z "${SABAYON_SKIP_UPGRADE}" || "${SABAYON_SKIP_UPGRADE}" == "0" ]] ; then + ETP_NONINTERACTIVE=1 equo u || return 1 + echo -5 | equo conf update || return 1 + fi + + export ETP_NONINTERACTIVE=1 + + return 0 +} + +sabayon_molecules_phase1 () { + + equo i "${PACKAGES_TO_ADD[@]}" || return 1 + + echo -5 | equo conf update || return 1 + + mkdir /etc/systemd/system/docker.service.d || return 1 + echo " +[Service] +ExecStart= +ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2376 -H unix:///var/run/docker.sock -D + +" >> /etc/systemd/system/docker.service.d/00gentoo.conf || return 1 + + # Disable systemd-journal-flush. Doesn't start correctly + systemctl disable systemd-journal-flush.service + ln -s /dev/null /etc/systemd/system/systemd-journal-flush.service + # Disable systemd-update-utmp. Doesn't start correctly + systemctl disable systemd-update-utmp.service + ln -s /dev/null /etc/systemd/system/systemd-update-utmp.service + + systemctl enable docker || return 1 + + return 0 +} + +sabayon_molecules_clean () { + + # Cleaning equo package cache + equo cleanup || return 1 + + # Cleanup + rm -rf "${FILES_TO_REMOVE[@]}" || return 1 + + return 0 +} + +sabayon_molecules_echo () { + local msg=$@ + + echo " +------------------------------------------------------------------------------- +$@ +------------------------------------------------------------------------------- +" + return 0 +} + +sabayon_molecules_run () { + + shift + local opts=$@ + local systemctl=/usr/bin/systemctl + local journaltcl=/usr/bin/journalctl + + # Load environment variables + if [ -e "${SABAYON_MOLECULES_ENVFILE}" ] ; then + echo "Sourcing file ${SABAYON_MOLECULES_ENVFILE}..." + source ${SABAYON_MOLECULES_ENVFILE} + fi + + sabayon_molecules_info || return 1 + + if [ ${SABAYON_MOLECULES_SYSTEMD_MODE} -eq 1 ] ; then + sabayon_molecules_echo "Starting SYSTEMD" + exec /sbin/init --system --show-status=true & + + sabayon_molecules_echo "Waiting for systemd starting...sleep 5" + sleep 5 + + sabayon_molecules_echo "FAILED SYSTEMD SERVICES" + $systemctl --failed || return 1 + + sabayon_molecules_echo "Systemd Services Status" + $systemctl status || return 1 + + sabayon_molecules_echo "JOURNALCTL BOOTSTRAP LOG" + $journaltcl -b --no-pager + fi + + sabayon_molecules_echo \ + "Clone repository ${SABAYON_MOLECULES_GITURL} to ${SABAYON_MOLECULES_DIR}" + git clone ${SABAYON_MOLECULES_GITURL} ${SABAYON_MOLECULES_GIT_OPTS} \ + ${SABAYON_MOLECULES_DIR} || return 1 + + # TODO: Fix this on molecules tree + mkdir ${SABAYON_MOLECULES_DIR}/iso + + echo "Repository ${SABAYON_MOLECULES_GITURL} installed correctly." + + if [ -n "${SABAYON_MOLECULES_CHROOTS}" ] ; then + echo "Mount ${SABAYON_MOLECULES_CHROOTS} to ${SABAYON_MOLECULES_DIR}/chroots..." + mount -o bind ${SABAYON_MOLECULES_CHROOTS} ${SABAYON_MOLECULES_DIR}/chroots || return 1 + fi + + if [ -n "${SABAYON_MOLECULES_SOURCES}" ] ; then + echo "Mount ${SABAYON_MOLECULES_SOURCES} to ${SABAYON_MOLECULES_DIR}/sources..." + mount -o bind ${SABAYON_MOLECULES_SOURCES} ${SABAYON_MOLECULES_DIR}/sources || return 1 + fi + + if [ -n "${SABAYON_MOLECULES_ISO}" ] ; then + echo "Mount ${SABAYON_MOLECULES_ISO} to ${SABAYON_MOLECULES_DIR}/iso..." + mount -o bind ${SABAYON_MOLECULES_ISO} ${SABAYON_MOLECULES_DIR}/iso || return 1 + fi + +# NOTE: loop is needed for mount image +# Example of docker run command: +# docker run --init --device /dev/fuse --cap-add=MKNOD --tmpfs /run --tmpfs /tmp \ +# -v /sdpool/molecules-chroots:/chroots \ +# -v /sdpool/molecules-sources:/sources \ +# -v /sdpool/iso:/iso \ +# -v /sys/fs/cgroup:/sys/fs/cgroup:ro \ +# --rm --name test --cap-add=SYS_PTRACE --cap-add=SYS_ADMIN \ +# --cap-add=NET_ADMIN \ +# --device=/dev/loop-control:/dev/loop-control +# --device=/dev/loop0:/dev/loop0 +# --device=/dev/loop1:/dev/loop1 +# -e COLUMNS=200 -e LINES=400 +# -e SABAYON_MOLECULES_CHROOTS=/chroots +# -e SABAYON_MOLECULES_SOURCES=/sources +# -e SABAYON_MOLECULES_ISO=/iso +# sabayon/isobuilder-amd64 + + local date_end="" + local date_start=$(date +%s) + sabayon_molecules_echo "START iso_build.sh script." + ${SABAYON_MOLECULES_DIR}/scripts/iso_build.sh $@ || return 1 + date_end=$(date +%s) + sabayon_molecules_echo \ + "END iso_build.sh script. Build process time: $((${date_end} - ${date_start})) secs." + + if [ -e "${SABAYON_MOLECULES_POSTSCRIPT}" ] ; then + echo "Sourcing POST script file ${SABAYON_MOLECULES_POSTSCRIPT}..." + source ${SABAYON_MOLECULES_POSTSCRIPT} + fi + + return 0 +} + +case $1 in + init) + sabayon_molecules_init + ;; + phase1) + sabayon_molecules_phase1 + ;; + run) + sabayon_molecules_run $@ + ;; + clean) + sabayon_molecules_clean + ;; + *) + echo "Use init|phase1|run|clean" + exit 1 +esac + +exit $? + +# vim: ts=2 sw=2 expandtab