diff options
| author | Simon Rettberg | 2014-04-01 15:35:26 +0200 |
|---|---|---|
| committer | Simon Rettberg | 2014-04-01 15:35:26 +0200 |
| commit | c189f9ce049cbae19aaad06ae74a642b1e0733d4 (patch) | |
| tree | a1db240f7adba119bc7469474bed73d3cd23bb58 /remote/includes | |
| parent | [vmware/vmchooser] Generalize string cleaning function (diff) | |
| parent | [config curitiba] add curitiba config (diff) | |
| download | tm-scripts-c189f9ce049cbae19aaad06ae74a642b1e0733d4.tar.gz tm-scripts-c189f9ce049cbae19aaad06ae74a642b1e0733d4.tar.xz tm-scripts-c189f9ce049cbae19aaad06ae74a642b1e0733d4.zip | |
Merge branch 'master' of simonslx:openslx-ng/tm-scripts
Diffstat (limited to 'remote/includes')
| -rw-r--r-- | remote/includes/binutil.inc | 15 | ||||
| -rw-r--r-- | remote/includes/chroot.inc | 210 | ||||
| -rw-r--r-- | remote/includes/cleanup.inc | 29 | ||||
| -rw-r--r-- | remote/includes/kernel.inc | 97 | ||||
| -rw-r--r-- | remote/includes/packagemanager.inc | 102 |
5 files changed, 384 insertions, 69 deletions
diff --git a/remote/includes/binutil.inc b/remote/includes/binutil.inc index 906b6cc2..73371c25 100644 --- a/remote/includes/binutil.inc +++ b/remote/includes/binutil.inc @@ -136,12 +136,14 @@ get_link_chain() { # sanity checks if [[ "$1" == /* ]]; then - [ -e $1 ] || perror "get_link_chain: no such file: $1" + if [ ! -e $1 -a ! -L $1 ]; then + perror "'$1' is a link but its target '$LINK_TARGET' is not in '${LOCALSEARCHDIR}'" + fi else perror "get_link_chain() requires absolute paths, given: $1" fi if [ $# == 2 ] ; then - [ ! -d $2 ] && perror "get_link_chain: $2 is not a directory." + [ ! -d $2 ] && perror "get_link_chain: '$2' is not a directory. Local search can't work..." # got a prefix local PREFIX=$(readlink -f $2) else @@ -184,7 +186,11 @@ get_link_chain() { if [ "x$PREFIX" != "x" -a "x$PREFIX" != "xnotset" ]; then if [ "x${LINK#$PREFIX}" == "x${LINK}" ]; then # prefix was not in the link - echo "$LINK" + if [ ! -e "$LINK" ]; then + [ -e "$PREFIX/$LINK" ] && echo "./$LINK" + else + echo "$LINK" + fi else # prefix was in the link echo ./"${LINK#$PREFIX}" @@ -206,8 +212,7 @@ get_link_chain() { # - libc.so, ld-linux.so # list_basic_libs() { - for i in $(ldd ${SHELL}) - do + for i in $(ldd ${SHELL}); do [ $(echo $i | grep '^/' | grep -c ld) -eq 1 -o $(echo $i | grep '^/' | grep -c libc.so) -eq 1 ] && get_link_chain $i done } diff --git a/remote/includes/chroot.inc b/remote/includes/chroot.inc new file mode 100644 index 00000000..48ad39d3 --- /dev/null +++ b/remote/includes/chroot.inc @@ -0,0 +1,210 @@ +# ----------------------------------------------------------------------------- +# +# Copyright (c) 2014 - OpenSLX GmbH +# +# This program is free software distributed under the GPL version 2. +# See http://openslx.org/COPYING +# +# If you have any feedback please consult http://openslx.org/feedback and +# send your suggestions, praise, or complaints to feedback@openslx.org +# +# General information about OpenSLX can be found at http://openslx.org/ +# ----------------------------------------------------------------------------- +# +# Common functions for chrooting +# +# ----------------------------------------------------------------------------- + +CHROOT_TEMPDIR="${ROOT_DIR}/remote/chroot.tmp" +CHROOT_MOUNTDIR="${CHROOT_TEMPDIR}/rootmount" +CHROOT_BINDDIR="${CHROOT_TEMPDIR}/rootbind" +CHROOT_LOWERDIR="/" +CHROOT_BINDMOUNTS="/dev /proc /sys /run" + +# Helper function to setup the directory structure +chroot_prepare_dirs() { + # first check if CHROOT_TEMPDIR exists + if [ -d "${CHROOT_TEMPDIR}" ]; then + # try to umount and rmdir CHROOT_MOUNTDIR + umount "${CHROOT_MOUNTDIR}" 2>/dev/null + rmdir "${CHROOT_MOUNTDIR}" || perror "Could not remove '${CHROOT_MOUNTDIR}', meaning it has stuff in it. Aborting..." + + # try to umount and rmdir CHROOT_BINDDIR + umount "${CHROOT_BINDDIR}" 2>/dev/null + rmdir "${CHROOT_BINDDIR}" || perror "Could not remove '${CHROOT_BINDDIR}', meaning it has stuff in it. Aborting..." + + # try to rmdir CHROOT_TEMPDIR + rmdir "${CHROOT_TEMPDIR}" || perror "Could not remove '${CHROOT_TEMPDIR}', meaning it has stuff in it. Aborting..." + fi + + mkdir -p "${CHROOT_TEMPDIR}" || perror "Could not create base directory for mount directories $CHROOT_TEMPDIR." + for DIR in "${CHROOT_BINDDIR}" "${CHROOT_MOUNTDIR}"; do + mkdir -p "${DIR}" || perror "Could not create directory for mount directory $DIR." + done +} + +# Helper to mount the overlay structure: +# - bind mount system / to CHROOT_BINDDIR and make it read-only +# - make an overlay from CHROOT_LOWERDIR CHROOT_UPPERDIR +# - bind mount additional pseudo-fs (as given in CHROOT_BINDMOUNTS) +chroot_prepare_mounts() { + + # first mount / on CHROOT_BINDDIR and remount read-only + mount -o bind "${CHROOT_LOWERDIR}" "${CHROOT_BINDDIR}" || perror "Could not bind-mount '$CHROOT_LOWERDIR' to '$CHROOT_BINDDIR'." + mount -o remount,ro,bind "${CHROOT_BINDDIR}" || perror "Could not remount '$CHROOT_BINDDIR' read-only." + + # check that it really is read-only + [ "x$(mount | grep -E "^/ on ${CHROOT_BINDDIR}" | grep -v '\(.*ro.*\)')" != "x" ] && perror "'${CHROOT_BINDDIR}' is not read-only! Aborting..." + + # safe, go on to make the overlay + mount -t overlayfs overlayfs -o lowerdir="${CHROOT_BINDDIR}",upperdir="${CHROOT_UPPERDIR}" "${CHROOT_MOUNTDIR}" \ + || perror "Could not mount (overlayfs) $CHROOT_LOWERDIR, $CHROOT_UPPERDIR to $CHROOT_BINDDIR." + + # mount pseudo-filesystems + for DIR in $CHROOT_BINDMOUNTS; do + mount -o bind "${DIR}" "${CHROOT_MOUNTDIR}/${DIR}" \ + || perror "Could not bind mount '$DIR' into '$CHROOT_MOUNTDIR/$DIR'." + done +} + +# Helper to generate the mighty autoexec.bat +chroot_gen_autoexec() { + # create the script to be automatically executed. + cat >"${CHROOT_MOUNTDIR}/autoexec.bat"<<-EOF + #!/bin/bash + ####################################################### + # # + # Warning! # + # # + # This file is only meant to be executed within # + # the specially chrooted mltk building environment. # + # # + # Do NOT execute it if you are not sure what you do, # + # it may be very harmful if being run in a normal # + # system environment! # + # # + ####################################################### + echo "chroot started successfully." + EOF + + # dump the piped input to it + cat >> "${CHROOT_MOUNTDIR}/autoexec.bat" + + # make it executable + chmod +x "${CHROOT_MOUNTDIR}/autoexec.bat" +} + +chroot_handle_whiteouts() { + local WHITEOUT_LIST="${CHROOT_UPPERDIR}/overlay.whiteout.list" + rm -f -- "$WHITEOUT_LIST=" + #mkdir -p "$(dirname "$WHITEOUT_LIST")" || perror "Could not create $(dirname "$WHITEOUT_LIST")" + pdebug "Searching for overlayfs-whiteouts ..." + for WHITEOUT in $(find "$CHROOT_UPPERDIR" -lname "(overlay-whiteout)"); do + pdebug "Whiteout found: $WHITEOUT" + echo "/./${WHITEOUT#$CHROOT_UPPERDIR}" >> "$WHITEOUT_LIST" + rm -f -- "$WHITEOUT" || perror "Could not delete whiteout $WHITEOUT!" + done + pinfo "Whiteout list dumped to '${CHROOT_UPPERDIR}/overlay.whiteout.list'" +} + +############################################################################### +# +# MAIN FUNCTION +# +# Main function to be called from the outside +# Usage: +# chroot_run <build_dir> < <code_to_exec_in_chroot> +# +# Example: +# chroot_run /tmp/chroot_build <<-EOF +# echo "This will be executed inside the chroot" +# EOF +# +# It will run: +# - chroot_prepare +# - chroot $CHROOT_TEMPDIR/rootmount +# - executes $CHROOT_TEMPDIR/rootmount/autoexec.bat +# - chroot_cleanup +chroot_run() { + # check args + [ $# -eq 1 ] || perror "'chroot_run' requires exactly 1 parameter. Given $#. Use 'chroot_run <build_dir>'" + + local CHROOT_UPPERDIR="$1" + mkdir -p "$1" + + # first prepare the dir structure + chroot_prepare_dirs || perror "'chroot_prepare_dirs' failed with $?." + chroot_prepare_mounts || perror "'chroot_prepare_mounts' failed with $?." + + # generate the code to be executed when chroot'ing + chroot_gen_autoexec || perror "'chroot_gen_autoexec' failed with $?." + + # do the chroot + exec 0>&8 # This redirection is used for debugging within a chroot + chroot --userspec root:root "${CHROOT_MOUNTDIR}" /autoexec.bat + local RET=$? + if [ "$RET" -eq 0 ]; then + pinfo "chroot executed '${CHROOT_MOUNTDIR}/autoexec.bat' succeeded." + else + perror "Failed to run '$CHROOT_MOUNTDIR/autoexec.bat' inside the chroot to '$CHROOT_MOUNTDIR' with error code: $RET" + fi + + # handle whiteouts + chroot_handle_whiteouts || perror "'chroot_handle_whiteouts' failed with error code: $?" + + # finally cleanup all the mounting stuff we did previously + chroot_cleanup_mounts || perror "'chroot_cleanup' failed with $?." +} + +############################################################################### +# +# CLEANUP FUNCTIONS +# +# Helper to check if the given path is mounted +chroot_check_mount_point() { + [ "$#" -eq 1 ] || perror "'chroot_check_mount_point' called with $# arguements, only 1 accepted." + local MOUNT="$1" + if [ "x$(mount | grep "$(readlink -f $MOUNT)")" != "x" ]; then + # still mounted + pdebug "'$MOUNT' is mounted!" + return 1 + else + pdebug "'$MOUNT' is not mounted." + return 0 + fi +} + +# Helper to umount the given path +chroot_umount() { + [ "$#" -eq 1 ] || perror "'chroot_umount' called with $# arguments, only 1 accepted." + local MOUNT="$1" + + # check if MOUNT is mounted + if ! chroot_check_mount_point "${MOUNT}"; then + # still mounted + if umount -l "${MOUNT}"; then + pdebug "Successfully umounted '${MOUNT}'." + else + pwarning "Could not umount '${MOUNT}'! Trying again..." + # now it gets ugly + for i in `seq 1 5`; do + umount -l "${MOUNT}" && return 0 + done + perror "Could not umount '${MOUNT}' after 5 tries! This shouldn't happen. Check your scripts." + fi + else + pdebug "'${MOUNT}' is not mounted." + fi + + # better be safe than sorry + chroot_check_mount_point "$MOUNT" || perror "'$MOUNT' is still mounted, exiting before something bad happens..." +} + +# Helper to cleanup the temporary mounts +chroot_cleanup_mounts() { + for DIR in $CHROOT_BINDMOUNTS; do + chroot_umount "${CHROOT_MOUNTDIR}/${DIR}" + done + chroot_umount "${CHROOT_MOUNTDIR}" + chroot_umount "${CHROOT_BINDDIR}" +} diff --git a/remote/includes/cleanup.inc b/remote/includes/cleanup.inc new file mode 100644 index 00000000..144d7fd9 --- /dev/null +++ b/remote/includes/cleanup.inc @@ -0,0 +1,29 @@ +# ----------------------------------------------------------------------------- +# +# Copyright (c) 2014 - OpenSLX GmbH +# +# This program is free software distributed under the GPL version 2. +# See http://openslx.org/COPYING +# +# If you have any feedback please consult http://openslx.org/feedback and +# send your suggestions, praise, or complaints to feedback@openslx.org +# +# General information about OpenSLX can be found at http://openslx.org/ +# ----------------------------------------------------------------------------- +# +# Trapped cleanup functions +# +# ----------------------------------------------------------------------------- + +# run 'cleanexit' when CTRL-c is pressed, an abrupt program termination or exit happens +trap cleanexit SIGINT SIGTERM + +# main cleaner function +cleanexit() { + pinfo "SIGINT/SIGTERM triggered - cleaning up ..." + # unmount and remove the temporary chroot stuff + chroot_cleanup_mounts + + # TODO vmware etc/vmware/config stuff here, if it is still needed + +} diff --git a/remote/includes/kernel.inc b/remote/includes/kernel.inc index 4eef36e5..a3c64d9a 100644 --- a/remote/includes/kernel.inc +++ b/remote/includes/kernel.inc @@ -16,36 +16,54 @@ # set global KERNEL_TARGET_NAME KERNEL_TARGET_NAME="kernel" +# set global KERNEL_BASE_DIR as in the directory containing lib/modules and lib/firmware +# for system kernel, that is "/" and for an openslx kernel KERNEL_BUILD_DIR +KERNEL_BASE_DIR="" + get_kernel_version () { # determine kernel version currently running on this machine - KERNEL_CURRENT_VERSION=$(uname -r) + [ -z "$KERNEL_CURRENT_VERSION" ] && declare -rg KERNEL_CURRENT_VERSION="$(uname -r)" [ -z "$KERNEL_CURRENT_VERSION" ] && pwarning "Could not determine kernel version." - # determine kernel version that will be running in the generated system - if [ -e "$MODULES_DIR/kernel/ksrc/include/generated/utsrelease.h" ]; then - SYS_UTS_RELEASE=$(grep 'UTS_RELEASE' "$MODULES_DIR/kernel/ksrc/include/generated/utsrelease.h" | awk -F '"' '{print $2}') - SYS_KERNEL=$(echo "$SYS_UTS_RELEASE" | grep -o -E '^[0-9\.]+') - else - SYS_UTS_RELEASE="(unknown-not-compiled-yet)" - SYS_KERNEL="$SYS_UTS_RELEASE" - fi + + # set empty SYS_UTS_RELEASE and SYS_KERNEL + SYS_UTS_RELEASE="(unknown)" + SYS_KERNEL="$(echo "$SYS_UTS_RELEASE" | grep -o -E '^[0-9\.]+')" } -check_kernel_build_dir() { +check_kernel_base_dir() { + + # check if KERNEL_BASE_DIR was set, if not we don't know + # whether kernel-openslx or kernel-system has been built + # and therefore not were to look for kernel modules, + # firmware and the kernel itself + + if [ -z "${KERNEL_BASE_DIR}" ]; then + # this is bad, abort + perror "KERNEL_BASE_DIR is not set. The kernel module did not run properly" + fi + # all good, keep going + + # old deprecated KERNEL_BUILD_DIR variable, here for compat reasons for now. TODO: delete [ -d "${MODULES_DIR}/kernel/build" ] && KERNEL_BUILD_DIR="${MODULES_DIR}/kernel/build" \ - || perror "No build directory set for the kernel. Was is built?" + || perror "No build directory set for the kernel. Was is built?" + # hack to get the real path of the installed modules - KERNEL_NEW_VERSION=$(ls ${KERNEL_BUILD_DIR}/lib/modules) + #KERNEL_NEW_VERSION=$(ls ${KERNEL_BUILD_DIR}/lib/modules) } copy_kernel_modules() { pinfo "Copying kernel modules for kernel ${KERNEL_CURRENT_VERSION}..." [ -z "${REQUIRED_KERNEL_MODULES}" ] && perror "REQUIRED_KERNEL_MODULES is empty. Check your config file." - check_kernel_build_dir + [ -z "${KERNEL_HEADERS_PATH}" ] && perror "KERNEL_HEADERS_PATH is empty. Kernel headers appears to be missing." + + + check_kernel_base_dir + # # process modules list # - # search for modules in KERNEL_BUILD_DIR - cd "${KERNEL_BUILD_DIR}" || perror "Could not cd to ${KERNEL_BUILD_DIR}" + # search for modules in KERNEL_BASE_DIR + cd "${KERNEL_BASE_DIR}" || perror "Could not cd to ${KERNEL_BASE_DIR}" local KERNEL_MODULES_DIR="lib/modules/${KERNEL_NEW_VERSION}" local KERNEL_MODULES_LIST="" @@ -62,12 +80,12 @@ copy_kernel_modules() { REQUIRED_KERNEL_MODULES_EXPANDED+=" $ELEM" done done - cd - + cd - 2>/dev/null pinfo "Expanded the list of $(echo "$REQUIRED_KERNEL_MODULES" | wc -w) required kernel modules to $(echo "$REQUIRED_KERNEL_MODULES_EXPANDED" | wc -w)" for KERNEL_MODULE in ${REQUIRED_KERNEL_MODULES_EXPANDED}; do local KERNEL_MODULE_PATH="${KERNEL_MODULES_DIR}/${KERNEL_MODULE}" - if grep "^${KERNEL_MODULE}$" "${KERNEL_BUILD_DIR}/${KERNEL_MODULES_DIR}/modules.builtin" >/dev/null; then + if grep "^${KERNEL_MODULE}$" "${KERNEL_BASE_DIR}/${KERNEL_MODULES_DIR}/modules.builtin" >/dev/null; then pdebug "Already built-in ${KERNEL_MODULE}." elif [ -e "${KERNEL_MODULE_PATH}" ]; then pdebug "Copying '${KERNEL_MODULE_PATH}'" @@ -78,7 +96,7 @@ copy_kernel_modules() { fi # check for dependencies - local DEPS=$(grep "${KERNEL_MODULE}:" "${KERNEL_BUILD_DIR}/${KERNEL_MODULES_DIR}/modules.dep" | cut -d ":" -f2-) + local DEPS=$(grep "${KERNEL_MODULE}:" "${KERNEL_BASE_DIR}/${KERNEL_MODULES_DIR}/modules.dep" | cut -d ":" -f2-) if [ ! -z "$DEPS" ]; then for DEP in $DEPS; do pdebug "Adding dep: ${KERNEL_MODULES_DIR}/$DEP" @@ -114,11 +132,13 @@ copy_firmware() { pinfo "Copying firmware for kernel ${KERNEL_CURRENT_VERSION}..." [ -z "${REQUIRED_FIRMWARE}" ] && perror "REQUIRED_FIRMWARE is empty. Check your config file." local OLD_DIR=$(pwd) - check_kernel_build_dir + + check_kernel_base_dir + # # process firmware list # - cd "${KERNEL_BUILD_DIR}" || perror "Could not cd!" + cd "${KERNEL_BASE_DIR}" || perror "Could not cd to ${KERNEL_BASE_DIR}" local FIRMWARE_DIR="lib/firmware" local FIRMWARE_LIST="" for FIRMWARE in ${REQUIRED_FIRMWARE}; do @@ -126,14 +146,21 @@ copy_firmware() { # check for firmware in the build directory of the kernel for CANDIDATE in "${FIRMWARE_DIR}/${FIRMWARE}" "${FIRMWARE_DIR}/${KERNEL_NEW_VERSION}/${FIRMWARE}"; do if [ -e "${CANDIDATE}" ]; then - pdebug "Copying from kernel build dir: '${CANDIDATE}'" + pdebug "Copying from kernel base dir ('$KERNEL_BASE_DIR'): '${CANDIDATE}'" FIRMWARE_LIST+=" ${CANDIDATE}" FOUND=1 fi done - + + # dont look under / if KERNEL_BASE_DIR is already / + if [ "x${KERNEL_BASE_DIR}" == "x/" ]; then + [ $FOUND -ne 1 ] && pwarning "Neither '${FIRMWARE_DIR}/${FIRMWARE}' nor '${FIRMWARE_DIR}/${KERNEL_NEW_VERSION}/${FIRMWARE}' found on the system" + continue + fi + # if we didn't found it in the kernel build directory, check for firmware in the system firmware directory if [ $FOUND -ne 1 ]; then + pdebug "Did not found '$FIRMWARE' in kernel base dir. Searching system..." for CANDIDATE in "/${FIRMWARE_DIR}/${FIRMWARE}" "/${FIRMWARE_DIR}/${KERNEL_CURRENT_VERSION}/${FIRMWARE}"; do if [ -e "${CANDIDATE}" ]; then if [ $(echo "${CANDIDATE}" | grep -c "${KERNEL_CURRENT_VERSION}") -eq 0 ]; then @@ -153,33 +180,37 @@ copy_firmware() { done if [ ! -z "${FIRMWARE_LIST}" ]; then + echo "$FIRMWARE_LIST" > /fwlist local COUNT=$(echo "${FIRMWARE_LIST}" | wc -w) pinfo "Copying $COUNT firmware to target directory." tarcopy "${FIRMWARE_LIST}" "${TARGET_BUILD_DIR}" fi + # only for kernel-openslx # post-process to fix the path of the firmwares found on the system unter /lib/firmware/$(uname -r) # which have to be copied to /lib/firmware/${KERNEL_NEW_VERSION} - if [ -d "${TARGET_BUILD_DIR}/lib/firmware/${KERNEL_CURRENT_VERSION}" ]; then - mkdir -p "${TARGET_BUILD_DIR}/lib/firmware/${KERNEL_NEW_VERSION}/" - cd "${TARGET_BUILD_DIR}/lib/firmware/${KERNEL_CURRENT_VERSION}" || perror "old kernel but no old kernel" - tarcopy "$(ls)" "${TARGET_BUILD_DIR}/lib/firmware/${KERNEL_NEW_VERSION}/" - cd - - rm -r "${TARGET_BUILD_DIR}/lib/firmware/${KERNEL_CURRENT_VERSION}" || perror "something went very wrong..." - else - pdebug "No ${TARGET_BUILD_DIR}/lib/firmware/${KERNEL_CURRENT_VERSION} directory, skipping the merge." + if [ "x${KERNEL_BASE_DIR}" != "x/" ]; then + if [ -d "${TARGET_BUILD_DIR}/lib/firmware/${KERNEL_CURRENT_VERSION}" ]; then + mkdir -p "${TARGET_BUILD_DIR}/lib/firmware/${KERNEL_NEW_VERSION}/" + cd "${TARGET_BUILD_DIR}/lib/firmware/${KERNEL_CURRENT_VERSION}" || perror "old kernel but no old kernel" + tarcopy "$(ls)" "${TARGET_BUILD_DIR}/lib/firmware/${KERNEL_NEW_VERSION}/" + cd - + rm -r "${TARGET_BUILD_DIR}/lib/firmware/${KERNEL_CURRENT_VERSION}" || perror "something went very wrong..." + else + pdebug "No ${TARGET_BUILD_DIR}/lib/firmware/${KERNEL_CURRENT_VERSION} directory, skipping the merge." + fi fi - + cd "$OLD_DIR" } copy_kernel() { - check_kernel_build_dir + check_kernel_base_dir local TOOL_STR="$TOOL_STR copy_kernel:" local KERNEL_DIR="${MODE_DIR}/builds/kernel" pinfo "Copying '${KERNEL_TARGET_NAME}' to '${KERNEL_DIR}'." [ -d "${KERNEL_DIR}" ] || mkdir -p "${KERNEL_DIR}" - cp "${KERNEL_BUILD_DIR}/${KERNEL_TARGET_NAME}" "${KERNEL_DIR}" || perror "Could not copy kernel!" + cp "${KERNEL_BASE_DIR}/${KERNEL_TARGET_NAME}" "${KERNEL_DIR}" || perror "Could not copy kernel!" pinfo "You may want to update your systems firmware/modules to match the current kernel." } diff --git a/remote/includes/packagemanager.inc b/remote/includes/packagemanager.inc index 358dd8b1..66013807 100644 --- a/remote/includes/packagemanager.inc +++ b/remote/includes/packagemanager.inc @@ -1,44 +1,84 @@ # # get all files of required packages by a module # -list_packet_files() { +# Usage: +# list_content_packages +# - lists all files/directories in REQUIRED_CONTENT_PACKAGES +# list_content_packages --files +# - lists all files in REQUIRED_CONTENT_PACKAGES +# list_content_packages --dirs +# - lists all dirs in REQUIRED_CONTENT_PACKAGES +# +# NOTE: additional packages needed to be listed can be given +# through the environment variable EXTRA_PACKAGES + +list_content_packages() { [ -z "$REQUIRED_CONTENT_PACKAGES" ] && pinfo "No required packages for $TOOL" && return 1 + [ $# -gt 2 ] && perror "'list_content_packages' accepts only 1 or no args. $# given." local PACKAGE="" - for PACKAGE in $REQUIRED_CONTENT_PACKAGES; do - local OPTIONAL="$(echo "$PACKAGE" | cut -c 1)" - [ "x$OPTIONAL" = "x@" ] && PACKAGE="$(echo "$PACKAGE" | cut -c 2-)" - local FILES="" - if [ "$PACKET_HANDLER" = "dpkg" ]; then - PACKAGECOMMAND="dpkg -L" - elif [ "$PACKET_HANDLER" = "rpm" ]; then - PACKAGECOMMAND="rpm -ql" - fi + for PACKAGE in $REQUIRED_CONTENT_PACKAGES $EXTRA_PACKAGES; do + list_content_package $1 $PACKAGE + done - if [ -n "$REQUIRED_PACKET_FILES_BLACKLIST" ]; then - FILES="$($PACKAGECOMMAND "$PACKAGE" | grep "^/" | \ - grep -v "$REQUIRED_PACKET_FILES_BLACKLIST" | \ - grep -v -E 'share/(man|doc)|/var/run|/var/log|/etc/init\.d'; \ - echo ":###:${PIPESTATUS[0]}")" - else - FILES="$($PACKAGECOMMAND "$PACKAGE" | grep "^/" | grep -v -E 'share/(man|doc)|/var/run|/var/log|/etc/init\.d'; echo ":###:${PIPESTATUS[0]}")" - fi +} +list_content_package(){ + #[ -z "$EXTRA_PACKAGES" ] || pinfo "Listing additional packages: $EXTRA_PACKAGES" + [ $# -gt 2 ] && perror "'list_content_package' accepts max 2 args. $# given." + local OP="-e" + case "$1" in + --files) + OP="-f" + ;; + --dirs) + OP="-d" + ;; + "") + OP="-e" + ;; + *) + perror "'list_content_packages' invalid argument: $1" + ;; + esac + local PACKAGE="$2" + local OPTIONAL="$(echo "$PACKAGE" | cut -c 1)" + [ "x$OPTIONAL" = "x@" ] && PACKAGE="$(echo "$PACKAGE" | cut -c 2-)" + local FILES="" + if [ "$PACKET_HANDLER" = "dpkg" ]; then + PACKAGECOMMAND="dpkg -L" + elif [ "$PACKET_HANDLER" = "rpm" ]; then + PACKAGECOMMAND="rpm -ql" + fi + + if [ -n "$REQUIRED_PACKET_FILES_BLACKLIST" ]; then + FILES="$($PACKAGECOMMAND "$PACKAGE" | grep "^/" | \ + grep -v "$REQUIRED_PACKET_FILES_BLACKLIST" | \ + grep -v -E 'share/(man|doc)|/var/run|/var/log|/etc/init\.d'; \ + echo ":###:${PIPESTATUS[0]}")" + else + FILES="$($PACKAGECOMMAND "$PACKAGE" | grep "^/" | grep -v -E 'share/(man|doc)|/var/run|/var/log|/etc/init\.d'; echo ":###:${PIPESTATUS[0]}")" + fi # FILES="$(rpm -ql "$PACKAGE" | grep "^/" | grep -v -E 'share/(man|doc)|/var/run|/var/log'; echo ":###:${PIPESTATUS[0]}")" - # ugly hack to get our return value - local LPRET=$(echo "$FILES" | awk -F ':###:' '{printf $2}') - FILES=$(echo "$FILES" | awk -F ':###:' '{print $1}') - if [ "x$LPRET" != "x0" -a "x$OPTIONAL" != "x@" ]; then - pdebug "FILES: '$FILES'" - perror "dpkg/rpm exited with code '$LPRET' for required package ${PACKAGE}." - fi - [ "x$LPRET" != "x0" ] && pwarning "dpkg/rpm exited with code '$LPRET' for optional package ${PACKAGE}." && continue - [ -z "$FILES" ] && pwarning "list_packet_files empty for packet ${PACKAGE}." && continue - pdebug "Packet $PACKAGE has $(echo $FILES | wc -w) files..." - for FILE in $FILES; do - [ ! -d "$FILE" ] && echo "$FILE" - done + # ugly hack to get our return value + local LPRET=$(echo "$FILES" | awk -F ':###:' '{printf $2}') + FILES=$(echo "$FILES" | awk -F ':###:' '{print $1}') + if [ "x$LPRET" != "x0" -a "x$OPTIONAL" != "x@" ]; then + pdebug "FILES: '$FILES'" + perror "dpkg/rpm exited with code '$LPRET' for required package ${PACKAGE}." + fi + [ "x$LPRET" != "x0" ] && pwarning "dpkg/rpm exited with code '$LPRET' for optional package ${PACKAGE}." && continue + [ -z "$FILES" ] && pwarning "list_packet_files empty for packet ${PACKAGE}." && continue + pdebug "Packet $PACKAGE has $(echo $FILES | wc -w) files..." + for FILE in $FILES; do + [ "$OP" "$FILE" ] && echo "$FILE" done } +# +# Convenience function +# +list_packet_files() { + list_content_packages --files +} # # Convenience function |
