#!/bin/bash #----------------------------------------------------------------------------------------------------------------------------------- # # Mk Base Image # # Copyright (C) 2026 Arnaud G. GIBERT # mailto:arnaud@rx3.net # # This is free software: you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published # by the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; If not, see # . # #----------------------------------------------------------------------------------------------------------------------------------- #----------------------------------------------------------------------------------------------------------------------------------- # Includes #----------------------------------------------------------------------------------------------------------------------------------- . /usr/lib/rx3/docker_tools.bash #----------------------------------------------------------------------------------------------------------------------------------- # Global Variables #----------------------------------------------------------------------------------------------------------------------------------- declare -g VERSION=1.0.0 declare -g NAME="MBI" declare -g HELP="usage: mk_base_image [[-h | --help] | [-V | --version] | [-p | --pre] | [-P | --post] | [-s | --strip] | [-u | --unstrip]] [-D | --direct] [-d | --distrib ] [-l | --lang ] [-L | --language ] [-m | --localtime ] [-r | --root ] [-t | --tmp ] [-T | --test] [-v | --verbose]" declare -g TMP_DIR_DEF="/tmp/mk_base_image" declare -g ROOT_DIR_DEF="/var/tmp/base_root" declare -g DISTRIB_DEF="9" declare -g LANG_DEF="en_US.UTF-8" declare -g LANGUAGE_DEF="en_US.UTF-8:en_US:en" declare -g LOCALTIME_DEF="Europe/Paris" declare -Ag CONTAINER_TAB CONTAINER_TAB["Builder,Name"]='rx3-builder' CONTAINER_TAB["Builder,Source"]='docker.io/library/mageia:9' CONTAINER_TAB["Root,Name"]='rx3-mageia${DISTRIB}' CONTAINER_TAB["Root,Source"]='scratch' declare -g BUILDER_DIR="/" declare -g ROOT_DIR="${ROOT_DIR_DEF}" declare -g TMP_DIR="${TMP_DIR_DEF}" declare -g DISTRIB="${DISTRIB_DEF}" declare -g LANG="${LANG_DEF}" declare -g LANGUAGE="${LANGUAGE_DEF}" declare -g LOCALTIME="${LOCALTIME_DEF}" declare -g MODE="DEFAULT" declare -g VERBOSE="FALSE" declare -g DRY_RUN="FALSE" declare -g DIRECT="FALSE" declare -g NAME_SERVER_0='10.10.0.254' declare -g NAME_SERVER_1='8.8.8.8' #----------------------------------------------------------------------------------------------------------------------------------- # Func Exec #----------------------------------------------------------------------------------------------------------------------------------- function mbi_func_exec() { func="$1" arg="$2" name="$3" builder_mode="$4" echo_error "Executing Name: [${name}] Func: [${func}] Arg: [${arg}] Builder_Mode: [${builder_mode}]" if [[ "${builder_mode}" == "TRUE" ]] then if [[ "${BUILDER_DIR}" != "/" ]] then echo_error "Only direct ${name} and '/' Builder Root dir supported: Recall in ..." mkdir -p "${BUILDER_DIR}/${ROOT_DIR_DEF}" mount --bind "${ROOT_DIR}" "${BUILDER_DIR}/${ROOT_DIR_DEF}" cmd_exec 'chroot "'"${BUILDER_DIR}"'" '"mk_base_image ${arg} --direct --root ${ROOT_DIR_DEF} ${RECALL_OPTS}" umount "${BUILDER_DIR}/${ROOT_DIR_DEF}" return fi else if [[ "${ROOT_DIR}" != "/" ]] then echo_error "Only direct ${name} and '/' Target Root dir supported: Recall..." cmd_exec 'chroot "${ROOT_DIR}" '"mk_base_image ${arg} --direct --root / ${RECALL_OPTS}" return fi fi set +e # find / 2>/dev/null | grep -v -e "^/dev" -e "^/proc" -e "^/run" -e "^/sys/" >/root/before declare -f ${func} | sed -n '/^{/,/^}/p' | sed -e '1d;$d' -e 's/;$//' | grep -v -E '^$|^#' | while read -r line do cmd_exec "${line}" done # find / 2>/dev/null | grep -v -e "^/dev" -e "^/proc" -e "^/run" -e "^/sys/" >/root/after } #----------------------------------------------------------------------------------------------------------------------------------- # Version #----------------------------------------------------------------------------------------------------------------------------------- function mbi_version_print() { version_print } #----------------------------------------------------------------------------------------------------------------------------------- # Help #----------------------------------------------------------------------------------------------------------------------------------- function mbi_help_print() { mbi_version_print help_print } #----------------------------------------------------------------------------------------------------------------------------------- # Arg Parse #----------------------------------------------------------------------------------------------------------------------------------- function mbi_args_parse() { tmp_args=$(getopt -o d:Dhl:L:m:pPr:Tt:suvV --long distrib:,direct,help,lang:,language:,localtime:,pre,post,root:,test,tmp:,strip,unstrip,verbose,version -- "$@") if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi # Note the quotes around `$tmp_args': they are essential! eval set -- "${tmp_args}" while true ; do case "$1" in # Options -d|--distrib) shift; DISTRIB="$1"; shift; RECALL_OPTS+=" --distrib ${DISTRIB}";; -D|--direct) DIRECT="TRUE"; shift;; # Local options -l|--lang) shift; LANG="$1"; shift; RECALL_OPTS+=" --lang ${LANG}";; -L|--language) shift; LANGUAGE="$1"; shift; RECALL_OPTS+=" --language ${LANGUAGE}";; -m|--localtime) shift; LOCALTIME="$1"; shift; RECALL_OPTS+=" --localtime ${LOCALTIME}";; # Path options -t|--tmp) shift; TMP_DIR="$1"; shift;; -r|--root) shift; ROOT_DIR="$1"; shift;; # Mode switches -h|--help) MODE="EXIT"; mbi_help_print; shift;; -p|--pre) MODE="PRE"; shift;; -s|--post) MODE="POST"; shift;; -s|--strip) MODE="STRIP"; shift;; -u|--unstrip) MODE="UNSTRIP"; shift;; -V|--version) MODE="EXIT"; mbi_version_print; shift;; # Global options -T|--test) DRY_RUN="TRUE"; shift; RECALL_OPTS+=" --test";; -v|--verbose) VERBOSE="TRUE"; shift; RECALL_OPTS+=" --verbose";; # --) shift; break;; *) echo "args_parse internal error [$1] !"; exit 1;; esac done } #----------------------------------------------------------------------------------------------------------------------------------- # Env Init #----------------------------------------------------------------------------------------------------------------------------------- function mbi_env_init() { root_clean="$1" if [[ -d "${TMP_DIR}" ]] then echo_error "Removing Tmp Dir: [${TMP_DIR}] content..." cmd_exec "echo rm -R \"${TMP_DIR}/*\"" else echo_error "Creating Tmp Dir: [${TMP_DIR}]..." cmd_exec "mkdir -p \"${TMP_DIR}\"" fi if [[ "${DIRECT}" == "TRUE" ]] then if [[ -d "${ROOT_DIR}" ]] then if [[ "${root_clean}" == "TRUE" ]] then echo_error "Removing Root Dir: [${ROOT_DIR}] content..." cmd_exec "echo rm -R \"${ROOT_DIR}/*\"" else echo_error "Keeping Root Dir content..." fi else if [[ "${root_clean}" == "TRUE" ]] then echo_error "Creating Root Dir: [${ROOT_DIR}]..." cmd_exec "mkdir -p \"${ROOT_DIR}\"" else echo_error "Error: Root Dir: [${ROOT_DIR}] not found!" exit 1 fi fi else for container_id in "Builder" "Root" do container_name="${CONTAINER_TAB["${container_id},Name"]}" if [[ "${root_clean}" == "TRUE" ]] then if [[ $( buildah ls | grep ${container_name}) != "" ]] then cmd_exec "buildah rm \"${container_name}\" >/dev/null" fi cmd_exec "buildah from --name \"${container_name}\" ${CONTAINER_TAB["${container_id},Source"]} >/dev/null" cmd_exec "buildah config --label maintainer=\"Arnaud G. GIBERT \" \"${container_name}\"" else if [[ $( buildah ls | grep ${container_name}) == "" ]] then echo_error "buildah container: [${container_name}] not found!" exit 1 fi fi done if [[ "${BUILDAH_ISOLATION:-}" != "rootless" ]] then echo_error "Unsharing..." cmd_exec 'buildah unshare "'$0'" ${ORIG_ARGS[@]}' exit $? else BUILDER_DIR=$(buildah mount ${CONTAINER_TAB["Builder,Name"]}) ROOT_DIR=$(buildah mount ${CONTAINER_TAB["Root,Name"]}) echo_error "Making Builder..." cmd_exec 'chroot "${BUILDER_DIR}" '"bash -c 'curl -L http://mirror.rx3.net/rx3/sbin/urpmi-setup-9 | bash'" cmd_exec 'chroot "${BUILDER_DIR}" urpmi --force docker_tools-devel' fi fi } #----------------------------------------------------------------------------------------------------------------------------------- # Env DeInit #----------------------------------------------------------------------------------------------------------------------------------- function mbi_env_deinit() { if [[ -d "${TMP_DIR}" ]] then echo_error "Removing Tmp Dir: [${TMP_DIR}] content..." cmd_exec "echo rm -R \"${TMP_DIR}/*\"" fi if [[ "${DIRECT}" != "TRUE" ]] then container_name="${CONTAINER_TAB["Builder,Name"]}" if [[ $( buildah ls | grep ${container_name}) != "" ]] then cmd_exec "buildah umount \"${container_name}\" >/dev/null" cmd_exec "buildah rm \"${container_name}\" >/dev/null" fi container_name="${CONTAINER_TAB["Root,Name"]}" if [[ $( buildah ls | grep ${container_name}) != "" ]] then cmd_exec "buildah commit \"${container_name}\" >/dev/null" fi fi } #----------------------------------------------------------------------------------------------------------------------------------- # Base Install Pre #----------------------------------------------------------------------------------------------------------------------------------- function _mbi_base_install_pre() { urpmi --root ${ROOT_DIR} --force --no-install --no-recommends --nolock --reinstall --download-all ${TMP_DIR} --downloader curl makedev filesystem curl urpmi rx3-base docker_tools-devel rpm --root ${ROOT_DIR} --initdb rpm --root ${ROOT_DIR} -Uvh --nodeps --noscripts ${TMP_DIR}/rpms/filesystem* rm ${TMP_DIR}/rpms/filesystem* rpm --root ${ROOT_DIR} -Uvh --nodeps --noscripts ${TMP_DIR}/rpms/makedev* rm ${TMP_DIR}/rpms/makedev* rpm --root ${ROOT_DIR} -Uvh --nodeps ${TMP_DIR}/rpms/* rm -R ${TMP_DIR} # Enable Neworking echo -e "nameserver ${NAME_SERVER_0}\nnameserver ${NAME_SERVER_1}" >"${ROOT_DIR}/etc/resolv.conf" echo -e "NETWORKING=yes" >"${ROOT_DIR}/etc/sysconfig/network" } function mbi_base_install_pre() { mbi_func_exec "_mbi_base_install_pre" "--pre" "pre-installing" "TRUE" } #----------------------------------------------------------------------------------------------------------------------------------- # Base Install Post #----------------------------------------------------------------------------------------------------------------------------------- function _mbi_base_install_post() { urpmi-setup --verbose --distib "${DISTRIB}" urpmi --force basesystem-minimal locales locales-en shared-mime-info vim-minimal mpm echo -e "LANG=${LANG}\nLANGUAGE=${LANGUAGE}" >"${ROOT_DIR}/etc/locale.conf" ln -s /usr/share/zoneinfo/${LOCALTIME} "${ROOT_DIR}/etc/localtime" } function mbi_base_install_post() { mbi_func_exec "_mbi_base_install_post" "--post" "post-installing" "FALSE" } #----------------------------------------------------------------------------------------------------------------------------------- # Base Strip #----------------------------------------------------------------------------------------------------------------------------------- function _mbi_base_strip() { # RPM force uninstall rpm -e --nodeps cracklib-dicts buildah rpm -e dhcp-common dhcp-client cronie-anacron opencontainers-runc lib64gpgme11 gnupg2 lib64ksba8 lib64usb1.0_0 lib64npth0 lib64assuan0 # Docker mounts tmpfs at /dev and procfs at /proc so we can remove them rm -rf /dev /proc 2>/tmp/null mkdir -p /dev /proc 2>/tmp/null # Keep only en_US.UTF8 locale rm -rf /usr/share/locale/!("en_US.UTF-8"|"locale.alias"|"locale-archive") rm -rf /usr/{{lib,lib64}/gconv,bin/localedef,sbin/build-locale-archive} # Docs rm -rf /usr/share/{man,doc,info,gnome/help} # sln rm -rf /sbin/sln # ldconfig rm -rf /etc/ld.so.cache /var/cache/ldconfig mkdir -p --mode=0755 /var/cache/ldconfig # UDev rm -rf /usr/lib/udev rm -rf /etc/udev/hwdb.bin rm -rf /usr/lib/.build-id # Cache & Tmp rm -rf /var/cache/urpmi/rpms 2>/tmp/null rm -rf /tmp/* } function mbi_base_strip() { mbi_func_exec "_mbi_base_strip" "--strip" "stripping" "FALSE" } #----------------------------------------------------------------------------------------------------------------------------------- # Base UnStrip #----------------------------------------------------------------------------------------------------------------------------------- function _mbi_base_unstrip() { urpmi.update -a # Install uninstalled packages urpmi --force cracklib-dicts buildah dhcp-common dhcp-client cronie-anacron buildah opencontainers-runc lib64gpgme11 gnupg2 lib64ksba8 lib64usb1.0_0 lib64npth0 lib64assuan0 # Force reinstall all packages urpmi --force --replacepkgs $(rpm -qa | grep -v gpg-pubkey) } function mbi_base_unstrip() { mbi_func_exec "_mbi_base_unstrip" "--unstrip" "unstripping" "FALSE" } #----------------------------------------------------------------------------------------------------------------------------------- # Main #----------------------------------------------------------------------------------------------------------------------------------- ORIG_ARGS=("$@") for var in $(env | grep -e LC_); do unset ${var/=*/}; done export LC_ALL="C" mbi_args_parse "$@" if [[ ${MODE} == "EXIT" ]] then exit 0 fi echo_error "MBI: Mode: [${MODE}] Verbose: [${VERBOSE}] Distrib: [${DISTRIB}] Direct: [${DIRECT}] Dry_Run: [${DRY_RUN}] Lang: [${LANG}] Language: [${LANGUAGE}] Local_Time: [${LOCALTIME}] Builder_Dir: [${BUILDER_DIR}] Root_Dir: [${ROOT_DIR}] Tmp_Dir: [${TMP_DIR}]" eval CONTAINER_TAB["Root,Name"]=${CONTAINER_TAB["Root,Name"]} if [[ "${MODE}" == "DEFAULT" ]] then mbi_env_init "TRUE" else mbi_env_init "FALSE" fi case "${MODE}" in "DEFAULT") mbi_base_install_pre mbi_base_install_post mbi_base_strip mbi_env_deinit ;; "PRE") mbi_base_install_pre ;; "POST") mbi_base_install_post ;; "STRIP") mbi_base_strip ;; "UNSTRIP") mbi_base_unstrip ;; esac