#!/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 | --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 -g CONTAINER_NAME='rx3-mageia${DISTRIB}' 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" if [[ "${DIRECT}" != "TRUE" ]] || [[ "${ROOT_DIR}" != "/" ]] then echo_error "Only direct ${name} and / Root Dir supported: Recall..." cmd_exec 'chroot "${ROOT_DIR}" '"mk_base_image ${arg} --direct --root / ${RECALL_OPTS}" else 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 fi } #----------------------------------------------------------------------------------------------------------------------------------- # Version #----------------------------------------------------------------------------------------------------------------------------------- function mbi_version_print() { version_print # echo "MBI Version: [${VERSION}]" } #----------------------------------------------------------------------------------------------------------------------------------- # Help #----------------------------------------------------------------------------------------------------------------------------------- function mbi_help_print() { mbi_version_print help_print } #----------------------------------------------------------------------------------------------------------------------------------- # Arg Parse #----------------------------------------------------------------------------------------------------------------------------------- function mbi_args_parse() { tmp_args=$(getopt -o d:Dhl:L:m:pr:Tt:suvV --long distrib:,direct,help,lang:,language:,localtime:,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;; -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 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}\" scratch >/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 if [[ "${BUILDAH_ISOLATION:-}" != "rootless" ]] then echo_error "Unsharing..." cmd_exec 'buildah unshare "'$0'" ${ORIG_ARGS[@]}' exit $? fi ROOT_DIR=$(buildah mount ${CONTAINER_NAME}) fi } #----------------------------------------------------------------------------------------------------------------------------------- # Base Install Pre #----------------------------------------------------------------------------------------------------------------------------------- function mbi_base_install_pre() { cmd_exec 'urpmi-setup --verbose --distrib "${DISTRIB}"' cmd_exec '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' cmd_exec 'rpm --root ${ROOT_DIR} --initdb' cmd_exec 'rpm --root ${ROOT_DIR} -Uvh --nodeps --noscripts ${TMP_DIR}/rpms/filesystem*' cmd_exec 'rm ${TMP_DIR}/rpms/filesystem*' cmd_exec 'rpm --root ${ROOT_DIR} -Uvh --nodeps --noscripts ${TMP_DIR}/rpms/makedev*' cmd_exec 'rm ${TMP_DIR}/rpms/makedev*' cmd_exec 'rpm --root ${ROOT_DIR} -Uvh --nodeps ${TMP_DIR}/rpms/*' cmd_exec 'rm -R ${TMP_DIR}' # Enable Neworking cmd_exec 'echo -e "nameserver ${NAME_SERVER_0}\nnameserver ${NAME_SERVER_1}" >"${ROOT_DIR}/etc/resolv.conf"' cmd_exec 'echo -e "NETWORKING=yes" >"${ROOT_DIR}/etc/sysconfig/network"' } #----------------------------------------------------------------------------------------------------------------------------------- # 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" } #----------------------------------------------------------------------------------------------------------------------------------- # 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" } #----------------------------------------------------------------------------------------------------------------------------------- # 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" } #----------------------------------------------------------------------------------------------------------------------------------- # 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}] Root_Dir: [${ROOT_DIR}] Tmp_Dir: [${TMP_DIR}] Lang: [${LANG}] Language: [${LANGUAGE}] Local_Time: [${LOCALTIME}]" eval CONTAINER_NAME=${CONTAINER_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 ;; "POST") mbi_base_install_post ;; "STRIP") mbi_base_strip ;; "UNSTRIP") mbi_base_unstrip ;; esac