#!/usr/bin/bash # Copyright (C) 2017-2018 Daniel Tartavel-jeannot # Jean-Baptiste Biernacki # Copyright (C) 2020 Neal Gompa # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License,dnf 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 . #Change the local to the most global export LC_ALL=C source "$(dirname $(realpath "$0"))/functions.sh" function help() { echo -e "${CMDNAME} [option] [size of image in Go]" echo -e "Options:" echo -e "--all create ready to burn image of Mageia ${MAGEIA_VERSION}" echo -e "--clean Clean all (suppress all) to make a new image" echo -e "--size size of image default: 7Go" echo -e "--build-path Path to the build directory of the image of Mageia ${MAGEIA_VERSION}" echo -e "--target target system (for now rpi, odroid)" echo -e "--target-version version of the target (0, 1, 2, 3 for rpi, 3 or 4 for odroid)" echo -e "--config Path to config files" echo -e "--bootfs filesystem of boot partition (ext4 or vfat) default: ext4" echo -e "--nonfree activate nonfree repos" echo -e "--tainted activate tainted repos" echo -e "\nBuild levels:" echo -e "--create-chroot Create the chroot directory" echo -e "--install-basesystem install base system" echo -e "--add-urpmimedia add mirrors for urpmi" echo -e "--chroot chroot to arm directory and launch packages installation" echo -e "--create-image Create the image of Mageia ${MAGEIA_VERSION}" echo -e "--config Path to config files (rpi1 rpi2 rpi3 rpi3+ xu4)" echo -e "\nFor image size, make sure it fit on physical support. (Default size is 7 Go)" } info ${0} #Check dependencies : #if [ -e /usr/bin/qemu-arm-static ]; then # error "qemu-user-static package is needed." # exit ${ERR_DEPENDENCY_MISSING} #fi if [ 0 -ne $(id -u) ]; then error "Script need to be run as root." exit ${ERR_NOT_ROOT} fi SOURCE_PATH="$(/usr/bin/dirname "$(readlink -f "${0}")")" INSTALL_PATH="$(pwd)/build" # FILES_PATH="${SOURCE_PATH}/files" PLATFORMS_PATH="${SOURCE_PATH}/platforms" INSTALL_METHOD="urpmi" CMDNAME=$(/usr/bin/basename "${0}") if [ ${#} == 0 ]; then help exit fi # parsing commandline TEMP=$(getopt -o h,a --long all,help,clean,create-chroot,prepare-chroot,jump-chroot,add-urpmimedia,create-image,config:,target:,target-version:,chroot,bootfs:,install-basesystem,update-mirror,build-path:,size:,nonfree,tainted -n ${CMDNAME} -- "${@}") if [ ${?} -ne 0 ] ; then error "line ${LINENO} Failed parsing options." >&2 ; exit ${ERR_1} ; fi eval set -- "${TEMP}" echo "${TEMP}" # Note the quotes around `$TEMP': they are essential! Or not, $( ) do the same as ` `. But there are the ` ' used ? while true; do case "${1}" in --add-urpmimedia) if [ -z ${OPT} ]; then OPT="addurpmimedia" fi shift ;; -a|--all) OPT="all" shift 1 ;; --bootfs) BOOTFS_P=${2} shift 2 ;; --build-path) INSTALL_PATH_P=$(readlink -f "${2}") shift 2 ;; --chroot) if [ -z ${OPT} ]; then OPT="chroot" fi shift ;; --clean) OPT="clean" shift ;; --config) CONFIG_PATH="${PLATFORMS_PATH}/${2}" shift 2 ;; --create-chroot) if [ -z ${OPT} ]; then OPT="createchroot" fi shift ;; --create-image) if [ -z ${OPT} ]; then OPT="createimage" fi shift ;; -h|--help) help exit 0 ;; --install-basesystem) if [ -z ${OPT} ]; then OPT="installbasesystem" fi shift ;; --jump-chroot) if [ -z ${OPT} ]; then OPT="jumpchroot" fi shift ;; --nonfree) NONFREE_P=1 shift ;; --prepare-chroot) if [ -z ${OPT} ]; then OPT="preparechroot" fi shift ;; --size) IMAGE_SIZE_P=${2} shift 2 ;; --tainted) TAINTED_P=1 shift ;; --target) TARGET_P=${2} shift 2 ;; --target-version) TARGET_VERSION_P=${2} shift 2 ;; --update-mirror) if [ -z ${OPT} ]; then OPT="updatemirror" fi shift ;; # --) # shift # break;; *) error "Parameter ${1} does not exists " exit ${ERR_1};; esac done # path of config file if ! [ -d "${CONFIG_PATH}" ]; then info " Config path does not exists, defaulting to ./platforms/${TARGET}" CONFIG_PATH="${PLATFORMS_PATH}/${TARGET}" warning "Do you want to create it and to copy the template file in? [Y|n] " read yn if [ -z ${yn} ] || [ ${yn} = "Y" ] || [ ${yn} = "y" ]; then /usr/bin/mkdir "${CONFIG_PATH}/" /usr/bin/cp --preserve=mode "${SOURCE_PATH}/mageia4arm.cfg.template" "${CONFIG_PATH}/mageia4arm.cfg" warning "You need now to modify the config file (${CONFIG_PATH}/mageia4arm.cfg) and relaunch the script" exit ${ERR_DEFAULT_CONFIG} else error "Error: Can't continue without config file, exiting" exit ${ERR_NO_CONFIG_FILE} fi fi if [ -e "${CONFIG_PATH}/mageia4arm.cfg" ]; then info "using ${CONFIG_PATH}/mageia4arm.cfg as config" source "${CONFIG_PATH}/mageia4arm.cfg" #NOTE1 : Here is sourced a file with variables. else warning "Config file does not exists, do you want to copy template ? [Y|n] " read yn if [ -z ${yn} ] || [ ${yn} = "Y" ] || [ ${yn} = "y" ]; then /usr/bin/cp --preserve=mode "${SOURCE_PATH}/mageia4arm.cfg.template" "${CONFIG_PATH}/mageia4arm.cfg" warning "You need now to modify the config file (${CONFIG_PATH}/mageia4arm.cfg) and relaunch the script" exit ${ERR_DEFAULT_CONFIG} fi fi # VARIABLE_A=${VARIABLE_B_IF_EXISTS:=${VARIABLE_C_IF_B_DOESNT_EXISTS}} IMAGE_SIZE=${IMAGE_SIZE_P:=${IMAGE_SIZE}} INSTALL_PATH=${INSTALL_PATH_P:=${INSTALL_PATH}} TARGET=${TARGET_P:=${TARGET}} TARGET_VERSION=${TARGET_VERSION_P:=${TARGET_VERSION}} BOOTFS=${BOOTFS_P:=${BOOTFS}} NONFREE=${NONFREE_P:=0} TAINTED=${TAINTED_P:=0} info "Option: "${OPT} BUILD_PATH="${INSTALL_PATH}/build-${TARGET}${TARGET_VERSION}" DNF_CONFIGPATH="$(dirname $(realpath "$0"))/mageia4arm-dnf.conf" # Assigne a script for creating a particular target image (rpi, odroid, ...) if [ -e "${CONFIG_PATH}/specialFunctions.sh" ]; then source "${CONFIG_PATH}/specialFunctions.sh" fi info "target : ${TARGET}" info "target_version : ${TARGET_VERSION}" info "source path: ${SOURCE_PATH}" info "Install path: ${INSTALL_PATH}" info "Build path: ${BUILD_PATH}" info "Commande : ${CMDNAME}" info "Firmware path : ${FIRMWARE_PATH}" info "Firmware dir : ${FIRMWARE_DIR}" info "Installation method : ${INSTALL_METHOD}" # Defining default image size to 7 go if [[ "${IMAGE_SIZE}" -lt 2 ]] || [[ "${IMAGE_SIZE}" -gt 128 ]]; then IMAGE_SIZE=7 fi # change dir to install path and create it if not existing if [ "${OPT}" != "clean" ]; then info "cd ${INSTALL_PATH}" if ! [ -e "${INSTALL_PATH}" ]; then /usr/bin/mkdir -p "${INSTALL_PATH}" if [ -z ${?} ]; then error "line ${LINENO} can't make directory ${INSTALL_PATH} , exiting" exit ${ERR_1} fi fi cd "${INSTALL_PATH}" if [ -z ${?} ]; then error "line ${LINENO} can't change to directory ${INSTALL_PATH} , exiting" exit ${ERR_1} fi fi #if no parameters then display help message if [ -z ${OPT} ]; then OPT="--help" fi info "Image size is: ${IMAGE_SIZE} GB" case ${OPT} in "clean") clean ;; "all") verify_disk_space if [ ${?} -ne 0 ]; then echo -e "Not enough space on disk\nDo you want to continue anyway ? [Y,n]" read yn if [ ${yn} = "n" ]; then exit ${ERR_NO_SPACE} fi fi createImageWrap #Create the empty .img createchroot #Check qemu and activate it if [ ${INSTALL_METHOD} = "urpmi" ]; then addurpmimedia else genusemirroroptions #Generate mirror options, if needed fi installbasesystem #Generate the rootfs, rootfiles, ... mkfstab preparechroot jumpchroot mountPartitions copyingsystem bunrningBootloader unmountingPartitions unloopingImage ;; "createchroot") createchroot ;; "preparechroot") preparechroot ;; "jumpchroot") jumpchroot ;; "chroot") jumpchroot ;; "addurpmimedia") addurpmimedia ;; "createimage") verify_disk_space if [ ${?} -ne 0 ]; then warning "Not enough space on disk" exit ${ERR_NO_SPACE} fi createImageWrap ;; "installbasesystem") installbasesystem ;; esac if [ ${ERRORN} -ne 0 ]; then warning "Some errors occurs : ${ERRORN} errors" fi exit ${ERRORN}