#!/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]" echo -e "Options:" echo -e "--all create ready to burn image of Mageia ${MAGEIA_VERSION}" echo -e "--build-path Path to the build directory of the image of Mageia ${MAGEIA_VERSION}" echo -e "--clean Clean all (suppress all) to make a new image" echo -e "--log [file] logs the output into mga4arm-YYYY-MM-DD-HH-MM-\${config}.log or specific file." echo -e "--config [name | path] A name of a default config or a path to a config files" 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 "--bootfs filesystem of boot partition (ext4 or vfat) default: ext4" echo -e "--nonfree activate nonfree repos" echo -e "--size size of image default: 7Go" echo -e "--tainted activate tainted repos" echo -e "--desktop [xfce|...] add a desktop environment task" echo -e "--package \"pkg1 pkg2\" One space separated list of package to install" 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 "\nFor image size, make sure it fit on physical support. (Default size is 7 Go)" } CMD_LINE="${@}" if [ 0 -ne $(echo "${CMD_LINE}" | grep -c '\-\-log') ]; then if [ 0 -ne $(echo "${CMD_LINE}" | grep -c '\-\-config') ]; then CONFIG_NAME="$(echo "${CMD_LINE}" | sed -e 's/^.*--config //' | sed -e 's/[[:space:]]*--.*$//' | sed -e 's/.*\/\([-_+[:alnum:]]*\)$/\1/' | tr ' ' '_')" if [ "x" = "${CONFIG_NAME}x" ]; then CONFIG_NAME="noconf" fi else CONFIG_NAME="noconf" fi LOGFILE="$(echo "${CMD_LINE}" | sed -e 's/^.*--log //' | sed -e 's/[[:space:]]*--.*$//' )" if [ "x" = "${LOGFILE}x" ]; then mkdir -p ./log LOGFILE="./log/mga4arm-$(date "+%Y-%m-%d-%H:%M:%S")-${CONFIG_NAME}.log" LOGCMD="--log " else LOGCMD="--log ${lOGFILE}" fi CMD_LINE="$(echo ${CMD_LINE} | sed -e "s/${LOGCMD}//" )" touch ${LOGFILE} /usr/bin/chown ${USER}:${USER} ${LOGFILE} exec ${0} ${CMD_LINE} 2>&1 | tee ${LOGFILE} exit fi 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" 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 add-urpmimedia,add-checksum,all,help,clean,create-chroot,prepare-chroot,jump-chroot,create-image,config:,compress,target:,target-version:,chroot,bootfs:,install-basesystem,update-mirror,build-path:,size:,sign,nonfree,tainted -n ${CMDNAME} -- "${@}") if [ ${?} -ne 0 ] ; then error "line ${LINENO} Failed parsing options." >&2 ; exit ${ERR_1} ; fi eval set -- "${TEMP}" echo "${TEMP}" while true; do case "${1}" in --add-urpmimedia) if [ -z ${OPT} ]; then OPT="addurpmimedia" fi shift ;; --add-checksum) GEN_CHECKSUM="true" shift ;; -a|--all) OPT="all" shift 1 ;; --bootfs) BOOTFS_P=${2} shift 2 ;; --build-path) mkdir -p ${2} INSTALL_PATH_P=$(readlink -f "${2}") shift 2 ;; --chroot) if [ -z ${OPT} ]; then OPT="chroot" fi shift ;; --clean) OPT="clean" shift ;; --compress) GEN_GZ="true" shift ;; --config) PATH_NOT_NAME=$(echo "${2}" | grep '/' 2>&1 1>/dev/null && echo "true" || echo "false" ) if [ ${PATH_NOT_NAME} == true ] ; then CONFIG_PATH="${2}" else CONFIG_PATH="${PLATFORMS_PATH}/${2}" fi shift 2 ;; --create-chroot) if [ -z ${OPT} ]; then OPT="createchroot" fi shift ;; --create-image) if [ -z ${OPT} ]; then OPT="createimage" fi shift ;; --desktop) DESKTOP="${2}" DESKTOP_TASK="task-${DESKTOP}" shift 2 ;; -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 ;; --package) PACKAGE="${2}" shift ;; --prepare-chroot) if [ -z ${OPT} ]; then OPT="preparechroot" fi shift ;; --sign) GEN_SIGN="true" 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=2 #Set a default size in case it is missing. #Redefined priority : commandline, then specific board config file. 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}" #name of the image # TODO Add nonfree if image contains nonfree drivers for example. DESKTOP_NAME="${DESKTOP:="noDE"}" IMAGE_BASE="Mageia-${MAGEIA_VERSION}-${TARGET}" IMAGE="${IMAGE_BASE}-${DESKTOP_NAME}.img" 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}" # 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 OPT=${OPT:="--help"} 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 check_extrapackage createImageWrap #Create the empty .img mountPartitions 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 extrapackage copyingsystem bunrningBootloader unmountingPartitions unloopingImage warning "You can now burn the image ( ${INSTALL_PATH}/${IMAGE} ) on SD card" info "pv \"${INSTALL_PATH}/${IMAGE}\" | dd of=/dev/mmcblk#" generateExtra ;; "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 unset exit exit ${ERRORN}