#!/bin/bash set -o errtrace # Let shell functions inherit ERR trap. set -o errexit # Don't locate and remember (hash) commands as they are looked up for # execution (don't remember path): set +h hvtrap_setup() { # Setting ERR trap does implicit `set -o errexit'. trap hvtrap_err ERR trap hvtrap_int INT } hvtrap_err() { echo echo "*** An error occured during ${LFS_STAGE}" exit 1 } hvtrap_int() { echo echo "*** Installation interrupted during ${LFS_STAGE}" exit 1 } # Load list of packages and versions. # They may be overridden in sysinfos. source ../config/packages-list # Load default values source ../config/sysinfos.default # Load user-defined configuration values if [ ! -f ../config/sysinfos ]; then echo "Missing config/sysinfos configuration file." return ${EXIT_FAILURE} else source ../config/sysinfos fi # We want hvtrap_setup called only with the first sourcing of this script. if [ -z "${HVTRAP_SETUP_DONE1}" ]; then hvtrap_setup export HVTRAP_SETUP_DONE1=1 fi # Load stage number definition source ./stage.def if [ -z "${LFS_STAGE}" ]; then echo "LFS_STAGE is undefined (see stage.def)" return ${EXIT_FAILURE} fi if [ "x${LFS_STAGE}" != "xstage0" -a "x${LFS_STAGE}" != "xstage1" ]; then LFS="" fi CLFS=${LFS} CROSS_TOOLS_DIR=${CLFS}/cross-tools TOOLS_DIR=${CLFS}/tools # Load functions source ../functions/fpkg source ../functions/lpkg source ../functions/version # Constants for return codes EXIT_SUCCESS=0 EXIT_WARNING=1 EXIT_FAILURE=2 # Position the cursor at the start of the line SET_CURSOR_START_LINE="\033[1G" # NORMAL prints text in normal color NORMAL="\033[0;39m" # SUCCESS prints text in a green colour SUCCESS="\033[1;32m" # WARNING prints text in a yellow colour WARNING="\033[1;33m" # FAILURE prints text in a red colour FAILURE="\033[1;31m" # Brackets are blue BRACKET="\033[1;34m" TAR_OPTS="-b8" # List of default archive extensions to try DEFAULT_ARCH_EXT_LIST="tar.bz2 tar.gz tgz tar.Z tar.xz zip" DEFAULT_IPKG_SCRIPT="ipkg.def" HVMAKE="make -j ${MAKEJOBS}" # Load default values. They can be overriden # by a target-specific file source ../targets/default if [ -f ../targets/${HVL_TARGET} ]; then source ../targets/${HVL_TARGET} fi # When CLFS_OS is defined, define target as the standard triplet: CLFS_TARGET="${CLFS_ARCH}" if [ x"${CLFS_OS}" != x ]; then CLFS_TARGET+="-${CLFS_OS}-${CLFS_ABI}" fi CLFS_HOST="$(echo $MACHTYPE | \ sed "s/$(echo $MACHTYPE | cut -d- -f2)/cross/")" export CLFS_BUILDFLAGS CLFS_TARGET CLFS_ARCH CLFS_HOST test_presence_of_packages_directory() { if [ ! -d "$(dirname $(pwd))/packages" ]; then echo "Error, could not find packages directory \"$(dirname $(pwd))/packages\", aborting." exit 1 fi } # Create log directory and log file for current stage if necessary # Arg #1: log file name static_init_log_file() { # Checking for correct number of arguments if [ $# -ne 1 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi local LOG_FILE_NAME=${1} # Scripts directory export SCRDIR=$(pwd) test_presence_of_packages_directory export LFS_PKG_BASE="$(dirname $(pwd))/packages" export LFS_PKG_DIR="${LFS_PKG_BASE}/${LFS_STAGE}" export LFS_LOG_DIR=${LFS}/var/log/hvlinux-install/${LFS_STAGE} export LFS_LOG_FILE=${LFS_LOG_DIR}/${LOG_FILE_NAME} export LFS_TMP="${LFS}/tmp" mkdir -p ${LFS_LOG_DIR} && if [ ! -f ${LFS_LOG_FILE} ]; then touch ${LFS_LOG_FILE} || exit 1 fi } # Create log directory and install log file for current stage if necessary init_log_file() { static_init_log_file "install.log" export LFS_PATCHES_DIR="${SCRDIR}/patches" } # Create log directory and update log file for current stage if necessary init_log_file_update() { static_init_log_file "pkg-update.log" export LFS_PATCHES_DIR="${LFS_PKG_DIR}/patches" } # Check that we are logged in as the root user, and exit if this is not the case. check_for_root_user() { if [ "x${USER}" != "xroot" ]; then echo "You must be the superuser to install hvlinux." exit 1 fi } # Extracting the version number from a complete package name. # Arg. #1: Complete package name with version (ex: firefox-3.5.5.source will output 3.5.5) get_pkg_ver() { # Checking for correct number of arguments if [ $# -ne 1 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi echo ${1} | sed "s!.*-\([0-9].*\)!\1!g" } # Extracting the version number from a complete package name using underscore as separator. # Arg. #1: Complete package name with version (ex: ecryptfs-utils_103 will output 103) get_pkg_ver_underscore() { # Checking for correct number of arguments if [ $# -ne 1 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi echo ${1} | sed "s!.*_\([0-9].*\)!\1!g" } # Extracting the version number from a complete package name # when the package name uses the underscore as the separator. # Arg. #1: Complete package name with version (ex: icu4c-54_1 will output 54.1) get_pkg_ver_underscore2() { # Checking for correct number of arguments if [ $# -ne 1 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi local v=$(get_pkg_ver ${1}) # Replace underscore by dot echo ${v} | sed "s!_!\.!g" } # Convert package version dots to uderscore. # Ex: gcc-3.4.6 will output 3_4_6 # Arg. #1: Complete package name with version ver_dots_to_underscore() { # Checking for correct number of arguments if [ $# -ne 1 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi local v=$(get_pkg_ver ${1}) # Replace dots by underscores echo ${v} | sed "s!\.!_!g" } # Extracting the first digit version number from a complete package name. # Arg. #1: Complete package name with version (ex: gcc-3.4.4 will output 3) get_pkg_ver1() { # Checking for correct number of arguments if [ $# -ne 1 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi echo ${1} | sed "s!^.*-\([0-9]*\)\..*!\1!g" } # Extracting the first digit version number from a complete package name, # when the package name uses the underscore as the separator. # Arg. #1: Complete package name with version (ex: icu4c-54_1 will output 54) get_pkg_ver1_underscore() { # Checking for correct number of arguments if [ $# -ne 1 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi echo ${1} | sed "s!^.*-\([0-9]*\)_.*!\1!g" } # Extracting the first 2 digits version number from a complete package name. # Arg. #1: Complete package name with version (ex: gcc-3.4.4 will output 3.4) get_pkg_ver2() { # Checking for correct number of arguments if [ $# -ne 1 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi echo ${1} | sed "s!^.*-\([0-9]*\.[0-9]*\).*!\1!g" } # Extracting the first 3 digit version number from a complete package name. # Arg. #1: Complete package name with version # (ex: linux-2.6.37.1 will output 2.6.37) get_pkg_ver3() { # Checking for correct number of arguments if [ $# -ne 1 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi echo ${1} | sed "s!^.*-\([0-9]*\.[0-9]*\.[0-9]*\).*!\1!g" } # Extracting the name from a complete package name. # Arg. #1: Complete package name with version (ex: gcc-3.4.4 will output gcc) get_pkg_name() { # Checking for correct number of arguments if [ $# -ne 1 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi # SED do not support non-greedy regexp: # We want to match any characters preceeding a dash followed by a number # (shortest match -> non-greedy) echo ${1} | sed "s!\([^-][^0-9]*\)-[0-9].*!\1!" } # Create symbolic links for start/stop scripts in /etc/rc.d # # Arg #1: script name # Arg #2: start number # Arg #3: stop number # Arg #4: level static_bootscript_add() { local SCRIPTNAME=${1} local START=${2} local STOP=${3} local RCLEVEL=${4} local START_LEVELS="" local STOP_LEVELS="" if [ "x${RCLEVEL}" = "xsysinit" ]; then START_LEVELS="sysinit" STOP_LEVELS="0 6" elif [ "x${RCLEVEL}" = "x3" ]; then START_LEVELS="3 4 5" STOP_LEVELS="0 1 2 6" else echo "${FUNCNAME}(), invalid level argument : ${*}" return 1 fi # Making sure bootscript has correct permissions chmod -v 740 ${LFS}/etc/rc.d/init.d/${SCRIPTNAME} # Removing any old links for level in sysinit 0 1 2 3 4 5 6; do cd ${LFS}/etc/rc.d/rc${level}.d rm -v -f $(find . -name "???${SCRIPTNAME}") done if [ ${START} -ne 0 ]; then # Creating new start links for level in ${START_LEVELS}; do cd ${LFS}/etc/rc.d/rc${level}.d ln -v -s ../init.d/${SCRIPTNAME} S${START}${SCRIPTNAME} done fi if [ ${STOP} -ne 0 ]; then # Creating new stop links for level in ${STOP_LEVELS}; do cd ${LFS}/etc/rc.d/rc${level}.d ln -v -s ../init.d/${SCRIPTNAME} K${STOP}${SCRIPTNAME} done fi } # Create symbolic links for start/stop scripts in /etc/rc.d # # Arg #1: level # Arg #2: script name # Arg #3: start number # Arg #4: stop number bootscript_add_manual() { local RCLEVEL=${1} local SCRIPTNAME=${2} local START=${3} local STOP=${4} # Making sure bootscript has correct permissions chmod 740 ${LFS}/etc/rc.d/init.d/${SCRIPTNAME} # Removing any old links cd ${LFS}/etc/rc.d/rc${RCLEVEL}.d rm -v -f $(find . -name "???${SCRIPTNAME}") if [ ${START} -ne 0 ]; then # Creating new start link cd ${LFS}/etc/rc.d/rc${RCLEVEL}.d ln -v -s ../init.d/${SCRIPTNAME} S${START}${SCRIPTNAME} fi if [ ${STOP} -ne 0 ]; then # Creating new stop link cd ${LFS}/etc/rc.d/rc${RCLEVEL}.d ln -v -s ../init.d/${SCRIPTNAME} K${STOP}${SCRIPTNAME} fi } # Create symbolic links for start/stop scripts in /etc/rc.d # # Arg #1: script name # Arg #2: start number # Arg #3: stop number bootscript_add_rc3() { static_bootscript_add ${*} 3 } # Create symbolic links for start/stop scripts in /etc/rc.d # # Arg #1: script name # Arg #2: start number # Arg #3: stop number bootscript_add_rcsysinit() { static_bootscript_add ${*} sysinit } # Add "export" before the variable name # # Arg #1: variable name # Arg #2: filename where the variable is located var_export() { VARIABLE=${1} FILE=${2} # Checking for correct number of arguments if [ $# -ne 2 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi # Checking if file exists if [ ! -f ${FILE} ]; then echo "${FUNCNAME}(), file not found: ${FILE}" return 1 fi # Checking if variable exists if ! grep -q "${VARIABLE}=" ${FILE}; then echo "${FUNCNAME}(), variable not found: ${VARIABLE}" return 1 fi # Checking if variable is already exported when it is defined if grep "${VARIABLE}=" ${FILE} | grep -q "export "; then echo "${FUNCNAME}(), variable already exported in definition: ${VARIABLE}" return 0 fi # Checking if variable is already exported, in # a "export VARIABLE1 VARIABLE2..." statement if grep "export " ${FILE} | grep -q " ${VARIABLE}"; then echo "${FUNCNAME}(), variable already exported in export list: ${VARIABLE}" return 0 fi sed -i "s!\(^${VARIABLE}=.*\)!export \1!" ${FILE} } # Adding a new environment variable to a file. # # Arg #1: variable name # Arg #2: filename where the variable is located # Arg #3: new variable value # Arg #4: separator string (usually a " " or ":") var_add() { VARIABLE=${1} FILE=${2} VALUE="${3}" SEP="${4}" # Checking for correct number of arguments if [ $# -ne 4 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi # Checking if file exists if [ ! -f ${FILE} ]; then echo "${FUNCNAME}(), file not found: ${FILE}" return 1 fi # Checking if variable exists if ! grep -q "${VARIABLE}=" ${FILE}; then echo "${VARIABLE}=\"${VALUE}\"" >> ${FILE} return $? fi # Checking if variable contains the new value if grep "${VARIABLE}=" ${FILE} | grep -q "${VALUE}"; then echo "${FUNCNAME}(), variable ${VARIABLE} already contains value: ${VALUE}" return 0 fi # Adding new value to variable (case where no export before) # We search for the variable name starting at the beginning of the line # For example, this ensures that if the variable name is PATH, then # PATH=... matches but not MANPATH=... if grep -q "^${VARIABLE}=\"" ${FILE}; then # Variable value is enclosed by double-quotes sed -i "s!\(^${VARIABLE}=\".*\)\(\"\)!\1${SEP}${VALUE}\"!" ${FILE} else # Variable value is NOT enclosed by double-quotes sed -i "s!\(^${VARIABLE}=.*\)!\1${SEP}${VALUE}!" ${FILE} fi # Adding new value to variable (case with export before) # We search for the variable name starting after an export statement, # at the beginning of a line. # For example, this ensures that if the variable name is PATH, then # PATH=... matches but not MANPATH=... if grep -q "^export ${VARIABLE}=\"" ${FILE}; then # Variable value is enclosed by double-quotes sed -i "s!\(^export ${VARIABLE}=\".*\)\(\"\)!\1${SEP}${VALUE}\"!" ${FILE} else # Variable value is NOT enclosed by double-quotes sed -i "s!\(^export ${VARIABLE}=.*\)!\1${SEP}${VALUE}!" ${FILE} fi return $? } # Adding a new path environment variable to a file. # # Arg #1: variable name # Arg #2: filename where the variable is located # Arg #3: new variable value var_add_path() { var_add ${1} ${2} "${3}" ":" 1>> ${LFS_LOG_FILE} 2>&1 } # Adding a new string environment variable to a file. # # Arg #1: variable name # Arg #2: filename where the variable is located # Arg #3: new variable value var_add_str() { var_add ${1} ${2} "${3}" " " } # Adding a new string to a file. # # Arg #1: string # Arg #2: filename where the variable is located string_add() { STRING="${1}" FILE=${2} # Checking for correct number of arguments if [ $# -ne 2 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi # Checking if file exists if [ ! -f ${FILE} ]; then echo "${FUNCNAME}(), file not found: ${FILE}" return 1 fi # Checking if string exists if grep -q "${STRING}" ${FILE}; then echo "${FUNCNAME}(), string already defined: ${STRING}" return 0 fi echo "${STRING}" >> ${FILE} } # Adding a new environment variable to a shadow password suite file (login.defs) # # Arg #1: variable name # Arg #2: filename where the variable is located # Arg #3: new variable value var_add_shadow() { VARIABLE=${1} FILE=${2} VALUE="${3}" # Checking for correct number of arguments if [ $# -ne 3 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" return 1 fi # Checking if file exists if [ ! -f ${FILE} ]; then echo "${FUNCNAME}(), file not found: ${FILE}" return 1 fi # Checking if variable contains the new value if egrep "^${VARIABLE}" ${FILE} | grep -q "${VALUE}"; then echo "${FUNCNAME}(), variable ${VARIABLE} already contains value: ${VALUE}" return 0 fi # Adding new value to variable # We search for the variable name starting at the beginning of the line # For example, this ensures that if the variable name is PATH, then # PATH... matches but not MANPATH... sed -i "s!\(^${VARIABLE}.*\)!\1:${VALUE}!" ${FILE} } # Adding a new user, checking if it already exist before # Arguments: same arguments as for standard useradd command. hv_useradd() { arguments="${*}" # The last argument is the user name while [ $# -ne 0 ]; do username=${*} shift done if ! cat /etc/passwd | egrep -q "^${username}:"; then useradd ${arguments} fi } # Applying patch # First argument is the name of the patch # Second argument is the package name # Third argument is the subdirectory inside the patches directory # Fourth argument: optional target directory (default is $LFS_TMP) apply_patch() { local PATCH_FILE=${1} local PACKAGE=${2} local PDIR=${3} local TARGET_DIR="" if [ $# -eq 4 ]; then TARGET_DIR=${4}/${PACKAGE} else TARGET_DIR=${LFS_TMP}/${PACKAGE} fi if [ -z "${PATCH_FILE}" ]; then echo echo "apply_patch(): no patch specified." return ${EXIT_FAILURE} fi if [ ! -f ${PDIR}/${PATCH_FILE} ]; then echo echo "${FUNCNAME}(): patch file '${PATCH_FILE}' not found." return ${EXIT_FAILURE} fi echo "Applying patch: ${PATCH_FILE}" patch -Np1 -d ${TARGET_DIR} -i ${PDIR}/${PATCH_FILE} } # Applying any patch(es) found for the current package. # Will work only if patches have the same base name as # the package, followed by a dash: # ${1}-*.patch # # 1st argument: Package name # 2: optional target directory. apply_patches() { PACKAGE=${1} local TARGET_DIR="" # Checking for correct number of arguments if [ $# -gt 2 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" echo "Usage: ${FUNCNAME} PACKAGE-NAME" exit ${EXIT_FAILURE} fi if [ $# -eq 2 ]; then TARGET_DIR=${2} fi for s in common ${HVL_TARGET}; do pdir=${LFS_PATCHES_DIR}/${s} if [ -d ${pdir} ]; then if ls ${pdir}/${1}-*.patch 1> /dev/null 2>&1; then cd ${pdir} for patch in ${1}-*.patch; do apply_patch ${patch} ${PACKAGE} ${pdir} ${TARGET_DIR} done fi fi done return $? } # Obtain the name of the base directory for the decompressed package. # First argument: package name static_decompressed_dirname() { local PACKAGE=${1} for arch_ext in ${DEFAULT_ARCH_EXT_LIST}; do if [ ! -f ${LFS_PKG_DIR}/${PACKAGE}.${arch_ext} ]; then # Try next archive extension. continue; fi local DIRNAME="" case ${arch_ext} in tar.bz2|tar.gz|tgz|tar.Z|tar.xz) # Remove optional "./" leading component with sed # and extract base directory name with awk. # tar 1.23 reports an error when using pipes, so # remove error message with "2> /dev/null" # (we extract the last line from tar output) DIRNAME=$(tar ${TAR_OPTS} -tf \ ${LFS_PKG_DIR}/${PACKAGE}.${arch_ext} 2> /dev/null | \ tail -n 1 | sed 's!^\./!!' | awk -F \/ '{print $1}') ;; zip) DIRNAME=$(unzip -l \ ${LFS_PKG_DIR}/${PACKAGE}.${arch_ext} | head -n4 | \ sed '$!d' | awk -F " " '{print $4}' | sed 's!/$!!') ;; esac echo ${DIRNAME} return $? done # Failure or file not found echo "${FUNCNAME}(): Missing source package for \"${PACKAGE}\"" > /dev/stderr return ${EXIT_FAILURE} } # Decompression of a package # First argument: package name # Second argument: directory where decompressing (optional, defaults to LFS_TMP) # Third argument: directory levels to strip (optional) decompress_package() { # Checking for correct number of arguments if [ $# -eq 1 ]; then local TOPDIR=${LFS_TMP} elif [ $# -eq 2 ]; then local TOPDIR=${2} elif [ $# -eq 3 ]; then local TOPDIR=${2} local STRIP_LEVEL="--strip-components=${3}" else echo "${FUNCNAME}(): Incorrect number of arguments (must be 1, 2 or 3)" > /dev/stderr return ${EXIT_FAILURE} fi local PACKAGE=${1} for arch_ext in ${DEFAULT_ARCH_EXT_LIST}; do if [ ! -f ${LFS_PKG_DIR}/${PACKAGE}.${arch_ext} ]; then # Try next archive extension. continue; fi if [ -d ${TOPDIR}/${PACKAGE} ]; then # Removing old source directory (if any) rm -rf ${TOPDIR}/${PACKAGE} fi # Decompressing package case ${arch_ext} in tar.bz2|tar.gz|tgz|tar.Z|tar.xz) tar -C ${TOPDIR} ${TAR_OPTS} ${STRIP_LEVEL} -xf \ ${LFS_PKG_DIR}/${PACKAGE}.${arch_ext} ;; zip) # Support STRIP_LEVEL??? unzip -qq -d ${TOPDIR} ${LFS_PKG_DIR}/${PACKAGE}.${arch_ext} ;; esac return $? done # Failure or file not found echo "${FUNCNAME}(): Missing source package for \"${PACKAGE}\"" > /dev/stderr return ${EXIT_FAILURE} } ipkg_trap_setup() { exec 6>&1 # Save current "value" of stdout. trap ipkg_trap_handler INT TERM EXIT ERR } ipkg_trap_end() { trap - INT TERM EXIT ERR # Restore global trap hvtrap_setup } ipkg_trap_handler() { exec 1>&6 6>&- # Restore stdout and close file descriptor #6. print_status failure ipkg_trap_end exit 1 } ipkg_display_build_infos() { echo "hvlinux: configure options:" for opt in ${CONFIGURE_OPTS}; do echo " ${opt}" done echo "hvlinux: environment variables:" echo " HOST: ${CLFS_HOST}" echo " TARGET: ${CLFS_TARGET}" echo " BUILD64: ${BUILD64}" echo " CFLAGS: ${CFLAGS}" echo " CPPFLAGS: ${CPPFLAGS}" echo " CXXFLAGS: ${CXXFLAGS}" echo " LDFLAGS: ${LDFLAGS}" echo " RANLIB: ${RANLIB}" echo " CC: ${CC}" echo " CXX: ${CXX}" echo " AR: ${AR}" echo " AS: ${AS}" echo " PATH: ${PATH}" echo " HVMAKE: ${HVMAKE}" GCCCPUOPT=$(gcc ${MARCH_FLAGS} -E -v - &1 | \ sed -n 's/.* -v - //p') echo " GCC CPU OPTIONS: ${GCCCPUOPT}" } # Arg. #1: Package label indicate_pkg_build_complete() { # Checking for correct number of arguments if [ $# -ne 1 ]; then echo "${FUNCNAME}(): Missing argument(s)" exit ${EXIT_FAILURE} fi local HVLABEL=${1} # The following will be renamed to PACKAGE_DONE in one atomic operation. local PACKAGE_STATUS=${LFS_LOG_DIR}/${HVLABEL}.status local PACKAGE_DONE=${LFS_LOG_DIR}/${HVLABEL}.done ipkg_display_build_infos > ${PACKAGE_STATUS} echo "Source size: " ${SOURCE_SIZE} >> ${PACKAGE_STATUS} echo "Build size: " ${BUILD_SIZE} >> ${PACKAGE_STATUS} echo -n "Build time: " >> ${PACKAGE_STATUS} display_build_time >> ${PACKAGE_STATUS} echo -e "\n" >> ${PACKAGE_STATUS} mv ${PACKAGE_STATUS} ${PACKAGE_DONE} } # Installation of a package # Arg. #1: Package name and version (ex: gcc-4.5.1) # Remaining arguments: Additional configure options # Options: # -c Do not decompress package # -d DIR Change to directory DIR before configuring and building. # -i DIR Installation directory (for cross-compilation, stage1) # -j Disable parallel make for this package # -l Unique identification label in 'install.log' # (default is package name and version) # -m Installation mode: # ac Standard autoconf package, build in separate dir (default) # acnb Standard autoconf package, build in source dir # noac No autoconf (configure) # pm # -t Enable dependency tracking # -s Name of script to source (default is from name of package) ipkg() { START_TIME=$(echo `date +%s`) unset ALT_SCRIPT_NAME export DECOMPRESS="1" export IPKG_MODE="ac" # Default mode if not specified export HVLABEL="" # Global variable export SRC_DIR="" # Global variable, source directory export BUILD_DIR="" # Global variable, build directory export SRC_SUBDIR="" # Global variable export INSTALL_DIR="" # Global variable, installation directory export ENABLE_DEPENDENCY_TRACKING="0" local HVMAKE_ORIG=${HVMAKE} while getopts "cd:i:jl:m:s:t" flag ;do case ${flag} in c) # Do not decompress package DECOMPRESS="0" ;; d) SRC_SUBDIR=${OPTARG} ;; i) INSTALL_DIR=${OPTARG} ;; j) # Disable parallel make only for this package HVMAKE="make" ;; l) # Alternate label HVLABEL=${OPTARG} ;; m) # Installation mode case ${OPTARG} in ac|acnb|noac|pm) IPKG_MODE=${OPTARG} ;; *) echo "${FUNCNAME}(): Unsupported mode: ${OPTARG}." return 1 ;; esac ;; s) # Alternate script name ALT_SCRIPT_NAME=${OPTARG} ;; t) # Do not disable dependency tracking ENABLE_DEPENDENCY_TRACKING="1" ;; ?) echo "${FUNCNAME}(): Invalid option: ${OPTARG}." return 1 ;; esac done shift `expr "${OPTIND}" - 1` unset OPTSTRING unset OPTIND unset OPTARG export PACKAGE=${1} # Checking for correct number of arguments if [ $# -lt 1 ]; then echo echo "${FUNCNAME}(): Missing argument" echo " command was: \"${FUNCNAME}() $*\"" exit ${EXIT_FAILURE} fi shift export CONFIGURE_OPTS=${*} if [ -z "${HVLABEL}" ]; then # Default label = package name and version HVLABEL=${PACKAGE} fi PACKAGE_LOG=${LFS_LOG_DIR}/${HVLABEL}.log PACKAGE_DONE=${LFS_LOG_DIR}/${HVLABEL}.done PACKAGE_STATUS=${LFS_LOG_DIR}/${HVLABEL}.status SRC_DIR="${LFS_TMP}/${PACKAGE}" BUILD_DIR="${LFS_TMP}/${PACKAGE}" if [ -n "${SRC_SUBDIR}" ]; then SRC_DIR+="/${SRC_SUBDIR}" fi case "${IPKG_MODE}" in ac) BUILD_DIR="${LFS_TMP}/${PACKAGE}-build" ;; acnb|noac|xpm) BUILD_DIR="${SRC_DIR}" ;; esac # Checking if package was previously successfully installed if [ -f ${PACKAGE_DONE} ]; then return $EXIT_SUCCESS fi # Create or empty status file cp /dev/null ${PACKAGE_STATUS} # Displaying label MSGSTRING="Installing ${HVLABEL}" display_checkbox_msg ${MSGSTRING} ipkg_trap_setup echo "------------------------" 1>> ${LFS_LOG_FILE} echo "${MSGSTRING}" 1>> ${LFS_LOG_FILE} # All output from commands in this block sent to file $PACKAGE_LOG. exec > ${PACKAGE_LOG} 2>&1 # Sourcing standard ac script. source ${SCRDIR}/../functions/ipkg source ${SCRDIR}/${DEFAULT_IPKG_SCRIPT} ipkg_script ${ALT_SCRIPT_NAME} # Restore original value in case it was modified HVMAKE=${HVMAKE_ORIG} # Make sure we are at a known location cd ${SCRDIR} if [ "x${LFS_STAGE}" != "xstage0" -a "x${LFS_STAGE}" != "xstage1" ]; then # Make sure to update dynamic linker run-time bindings, except for # stages 0 and 1 where we run as the LFS user. ldconfig fi exec 1>&6 # Restore stdout. # Displaying build time after the package name. print_status success # Indicate success in global log file. echo "Completed" 1>> ${LFS_LOG_FILE} # Report successfull installation of the package. indicate_pkg_build_complete ${HVLABEL} ipkg_trap_end return $EXIT_SUCCESS } # Display the action name, run a command, log its output and display it's # status. Write to install log file when successfully completed so it # won't be called again after a successfull installation. # First argument: "once" -> script is run only once # "mult" -> script can be run more than once # Second argument: Message to display during script # Third argument: script name (will be label) rscr() { START_TIME=$(echo `date +%s`) # Checking for correct number of arguments if [ $# -lt 3 ]; then echo "${FUNCNAME}(): Missing argument(s)" exit ${EXIT_FAILURE} fi SCRMODE=${1} MSGSTRING=${2} SCRIPT=${3} HVLABEL=${SCRIPT} shift shift SCRIPT_ARGS=${*} # Make sure we are at a known location cd ${SCRDIR} # Checking script mode: if [ "x${SCRMODE}" != "xonce" -a "x${SCRMODE}" != "xmult" ]; then echo "${FUNCNAME}(): First argument invalid, must be \"once\" or \"mult\" (\"${SCRMODE}\" given)" exit ${EXIT_FAILURE} fi # Checking if script file is found if [ ! -f ${SCRDIR}/${SCRIPT} ]; then echo "${FUNCNAME}(): script not found: ${SCRIPT}" exit ${EXIT_FAILURE} fi # ... and make sure it is executable if [ ! -x ${SCRDIR}/${SCRIPT} ]; then echo "${FUNCNAME}(): execute bit not set: ${SCRIPT}" exit ${EXIT_FAILURE} fi PACKAGE_LOG=${LFS_LOG_DIR}/${HVLABEL}.log PACKAGE_DONE=${LFS_LOG_DIR}/${HVLABEL}.done PACKAGE_STATUS=${LFS_LOG_DIR}/${HVLABEL}.status if [ "x${SCRMODE}" = "xonce" ]; then # Checking if package was previously successfully installed if [ -f ${PACKAGE_DONE} ]; then return $EXIT_SUCCESS fi # Create or empty status file cp /dev/null ${PACKAGE_STATUS} fi display_checkbox_msg ${MSGSTRING} ipkg_trap_setup echo "------------------------" 1>> ${LFS_LOG_FILE} echo "Running script ${HVLABEL}" 1>> ${LFS_LOG_FILE} # Executing script ${SCRDIR}/${SCRIPT} ${SCRIPT_ARGS} 1>> ${PACKAGE_LOG} 2>&1 # Displaying build time after the package name. print_status success # Indicate success in global log file. echo "Completed" 1>> ${LFS_LOG_FILE} if [ "x${SCRMODE}" = "xonce" ]; then # Report successfull installation of the script. indicate_pkg_build_complete ${HVLABEL} fi ipkg_trap_end return $EXIT_SUCCESS } rcmd_trap_setup() { exec 6>&1 # Save current "value" of stdout. trap rcmd_trap_handler INT TERM EXIT ERR } rcmd_trap_end() { trap - INT TERM EXIT ERR # We do not restore global trap } rcmd_trap_handler() { exec 1>&6 6>&- # Restore stdout to fd #6, where it had been saved, # and close file descriptor #6. print_status failure rcmd_trap_end if [ -n "${RCMD_NO_EXIT}" ]; then return ${EXIT_FAILURE} else exit ${EXIT_FAILURE} fi } # Run command, no log # First argument: Message to display during script # Second argument: command + arguments rcmd() { START_TIME=$(echo `date +%s`) # Checking for correct number of arguments if [ $# -lt 2 ]; then echo "${FUNCNAME}(): Missing argument(s)" exit ${EXIT_FAILURE} fi local MSGSTRING=${1} local CMD=${2} shift local CMD_WITH_ARGS=${*} display_checkbox_msg ${MSGSTRING} rcmd_trap_setup echo ${CMD_WITH_ARGS} 1>> ${LFS_LOG_FILE} # Executing command ${CMD_WITH_ARGS} 1>> ${LFS_LOG_FILE} 2>&1 if [ $? -ne 0 ]; then if [ -n "${RCMD_NO_EXIT}" ]; then return ${EXIT_FAILURE} else exit ${EXIT_FAILURE} fi fi # Displaying build time after the package name print_status success rcmd_trap_end return $EXIT_SUCCESS } get_total_build_time() { INFILE=${1} # Checking for correct number of arguments if [ $# -ne 1 ]; then echo "${FUNCNAME}(), wrong number of arguments: ${*}" echo "Usage: ${FUNCNAME} LOGFILE" exit ${EXIT_FAILURE} fi TIMES=$(cat ${LFS_LOG_DIR}/*.done | grep "Build time:" | sed "s!Build time: \(.*\)h!\1!g") for time in ${TIMES}; do HOURS=$(( $HOURS + $(echo ${time} | sed "s!^\([0-9]*\)\..*!\1!g") )) # The first SED command extracts the minutes (fractions of an hour). # The second SED command removed the leading zero, if applicable. MIN=$(( $MIN + $(echo ${time} | sed "s!.*\.\([0-9][0-9]\)!\1!g" | sed "s!^0\([0-9]\)!\1!g" ) )) done HOURS=$(( ${HOURS} + ( ${MIN} / 100 ) )) MIN=$(( ${MIN} % 100 )) echo "${HOURS}.${MIN}" } # Global variable: START_TIME display_build_time() { END_TIME=$(echo `date +%s`) HOURS=$(( ( ${END_TIME} - ${START_TIME} ) / 3600 )) echo -n ${HOURS}"." # Time is inferior to 1 hour... MINUTES=$(( ( ( ${END_TIME} - ${START_TIME} ) % 3600 ) / 36 )) if [ ${MINUTES} -lt 10 ]; then echo -n "0" fi if [ ${MINUTES} -eq 0 ]; then echo -n "1" else echo -n ${MINUTES} fi echo -n "h" } # Global variable: MSGSTRING display_checkbox_msg() { echo -en "${BRACKET}[ ${BRACKET}]${NORMAL} ${MSGSTRING}" } # The print_status prints a coloured "X" letter inside the checkbox to the left # of the screen (the checkbox is already displayed, and the cursor must have # been saved to where to put the mark). # 1st arg: status: success, warning or failure # Global variables: MSGSTRING and START_TIME print_status() { if [ $# -ne 1 ]; then # If no parameters are given to the print_status function, print usage # information. echo "Usage: ${FUNCNAME}() {success|warning|failure}" echo " got: \"${*}\"" return ${EXIT_FAILURE} fi local COLOR="" case "${1}" in success) COLOR=$SUCCESS ;; warning) COLOR=$WARNING ;; failure) COLOR=$FAILURE ;; *) echo "Usage: ${FUNCNAME}() {success|warning|failure}" return ${EXIT_FAILURE} ;; esac # Reposition cursor at start of line echo -en "${SET_CURSOR_START_LINE}" # Display colored 'X' echo -en "${BRACKET}[${COLOR}X${BRACKET}]${NORMAL} ${MSGSTRING}" if [ "x${MSGSTRING}" != "x" ]; then echo -n " (" display_build_time echo -n ")" fi echo } # This function will exit if the stage is already completed check_completed_stage() { if [ -f ${LFS_LOG_DIR}/stage-completed ]; then echo "${LFS_STAGE} completed" exit 0 else return 0 fi } write_completed_stage() { touch ${LFS_LOG_DIR}/stage-completed } display_stage_build_stats() { echo "Total build time: $(get_total_build_time ${LFS_LOG_FILE})h" }