From 8989b603a3b3478f93980bee008612374ddc0322 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 25 Feb 2013 17:39:59 +0100 Subject: fix build_core --- server/build_core | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'server') diff --git a/server/build_core b/server/build_core index 6d975984..411036df 100755 --- a/server/build_core +++ b/server/build_core @@ -26,16 +26,17 @@ STAGE32_DIR=${ROOT_DIR}/remote/stage3.2 # initial checks initial_checks() { - [ -d ${STAGE32_DIR} ] || perror "No stage3.2 directory found. Please run './mltk tools build' first." + local TOOL_STR="$TOOL_STR initial_checks:" + [ -d "${STAGE32_DIR}" ] || perror "No stage3.2 directory found. Please run './mltk tools build' first." - [ -e ${STAGE32_DIR}/openslx/bin/busybox ] || perror "Busybox not found, run './mltk tools build busybox' first." + [ -e "${STAGE32_DIR}/openslx/bin/busybox" ] || perror "Busybox not found, run './mltk tools build busybox' first." # shouldn't squashfs-tools be automatically installed as we need them nevertheless!? # what if it is already on the machine? faster to check with which than the apt-get check? - if [ -z $(which mksquashfs) ] && perror "mksquashfs not found, please install squashfs-tools first." + [ -z $(which mksquashfs) ] && perror "mksquashfs not found, please install squashfs-tools first." #check for kernel modules, if not present copy from system - if [ ! -d ${STAGE32_DIR}/lib/modules ]; + if [ ! -d "${STAGE32_DIR}/lib/modules" ]; then pinfo "Couldn't find kernel modules in stage3.2." pinfo "Copying modules for kernel $(uname -r)..." @@ -49,6 +50,7 @@ initial_checks() { } generate_rootfs() { + local TOOL_STR="$TOOL_STR generate_rootfs:" # produce stage3.1 mkdir -p "${STAGE31_DIR}" || perror "Cannot create stage3.1 dir '${STAGE31_DIR}'" @@ -61,7 +63,7 @@ generate_rootfs() { "${STAGE31_DIR}"/dev || perror "Cannot copy devices from running system" # copy busybox, its libs and static data to stage3.1 - cp -r "${STAGE32_DIR}/openslx/*" "${STAGE31_STATIC_DIR}/*" "${STAGE31_DIR}" || perror "Copying busybox, its libs and static data failed." + cp -r "${STAGE32_DIR}"/openslx/* "${STAGE31_STATIC_DIR}"/* "${STAGE31_DIR}" || perror "Copying busybox, its libs and static data failed." # fix for aufs & squashfs modules needed for stage 3.1 mkdir -p "${STAGE31_DIR}/lib/modules.0/" || perror "Creating modules.0 dir for aufs & squashfs in stage 3.1 failed." @@ -82,18 +84,20 @@ generate_rootfs() { } generate_squashfs() { + local TOOL_STR="$TOOL_STR generate_squashfs:" # finalize the initramfs target [ -e "${STAGE31_DIR}/mnt/openslx.sqfs" ] && rm "${STAGE31_DIR}/mnt/openslx.sqfs" - mksquashfs "${STAGE32_DIR}" "${STAGE31_DIR}/mnt/openslx.sqfs" -comp xz -b 1M -no-recovery || perror "mksquashfs failed ($?)." + mksquashfs "${STAGE32_DIR}" "${STAGE31_DIR}/mnt/openslx.sqfs" -comp xz -b 1M -no-recovery >&6 || perror "mksquashfs failed ($?)." } generate_initramfs() { + local TOOL_STR="$TOOL_STR generate_initramfs:" cd "${STAGE31_DIR}" || perror "Cannot cd to '${STAGE31_DIR}'" find . | cpio --format="newc" --create | gzip -9 > "${MODULE_DIR}/initramfs" - local PS=($PIPESTATUS[*]) - [ "x${PS[0]}" != "x0" ] && perror "'find .' failed." + local PS=(${PIPESTATUS[*]}) + [ "x${PS[0]}" != "x0" ] && perror "'find .' in '$(pwd)' failed." [ "x${PS[1]}" != "x0" ] && perror "cpio create failed." [ "x${PS[2]}" != "x0" ] && perror "gzip to '${MODULE_DIR}/initramfs' failed." cd - @@ -108,13 +112,14 @@ generate_stage31() { # TODO better kernel copy cp "/boot/vmlinuz-$(uname -r)" "${MODULE_DIR}/kernel" || perror "Cannot copy kernel from '/boot/vmlinuz-$(uname -r)' to '${MODULE_DIR}/kernel'" - pinfo "[core] kernel $(uname -r) copied to ${MODULE_DIR}/kernel" + pinfo "kernel $(uname -r) copied to ${MODULE_DIR}/kernel" } clean_core() { + local TOOL_STR="$TOOL_STR clean_core:" pinfo "Cleaning '${STAGE31_DIR}'..." - [ -d ${STAGE31_DIR} ] && { rm -rf ${STAGE31_DIR} || perror "rm -rf failed." } + [ -d ${STAGE31_DIR} ] && { rm -rf ${STAGE31_DIR} || perror "rm -rf failed."; } pinfo "Cleaning '${MODULE_DIR}/initramfs'..." - [ -e ${MODULE_DIR}/initramfs ] && { rm ${MODULE_DIR}/initramfs || perror "rm failed." } + [ -e ${MODULE_DIR}/initramfs ] && { rm ${MODULE_DIR}/initramfs || perror "rm failed."; } } -- cgit v1.2.3-55-g7522 From bccbda5a0d09d5218db2c7af339794a7eb6ee598 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 26 Feb 2013 18:49:21 +0100 Subject: Implemented more flexible command line parsing --- helper/logging.inc | 10 ++-- mltk | 170 ++++++++++++++++++++++++++++++++--------------------- server/build_core | 4 +- 3 files changed, 110 insertions(+), 74 deletions(-) (limited to 'server') diff --git a/helper/logging.inc b/helper/logging.inc index 2d79c67c..42906e1e 100644 --- a/helper/logging.inc +++ b/helper/logging.inc @@ -11,10 +11,10 @@ LOG_DIR=${ROOT_DIR}/logs mkdir -p "$LOG_DIR" set_quiet () { - if [ "x$DEBUG" != "x1" -a "x$MLTK_QUIET" != "x1" ]; then - exec 6>&1 > "$LOG_DIR/stdout.log" - exec 7>&2 2> "$LOG_DIR/stderr.log" - MLTK_QUIET="1" + if [ "x$MLTK_QUIET" != "x1" ]; then + exec 6>&1 > "$LOG_DIR/stdout.log" + exec 7>&2 2> "$LOG_DIR/stderr.log" + MLTK_QUIET="1" fi } @@ -42,7 +42,7 @@ pwarning () { } pdebug () { - if [ "x$DEBUG" != "x1" ]; then + if [ "x$MLTK_QUIET" = "x1" ]; then echo -e "[DEBUG] $TOOL_STR $@" >&2 else echo -e "\033[38;5;6m[debug]\033[0m $TOOL_STR $@" >&6 diff --git a/mltk b/mltk index 40c49cbc..207a635c 100755 --- a/mltk +++ b/mltk @@ -16,8 +16,8 @@ # # ----------------------------------------------------------------------------- -SELF=$(readlink -f $0) -ROOT_DIR=$(dirname ${SELF}) +SELF="$(readlink -f $0)" +ROOT_DIR="$(dirname "${SELF}")" MLTK_PID="$$" qnd_exit() { @@ -45,21 +45,27 @@ banner () { print_usage() { echo "Toolkit for creating preboot mini-linux for OpenSLX NG (mltk)" - echo "Usage: $(basename ${SELF}) MODULE [OPTIONS]" + echo "Usage: $(basename ${SELF}) --[target] [-b] [-c] [-d] [module]*" echo -e "" - echo -e " MODULE:" - echo -e " core \t minimal initramfs (stage 3.1) to mount the system-container (stage 3.2)." - echo -e " tools \t minimal systemd-based rootfs including basic tools." - echo -e " all \t all of the above." + echo -e " Target:" + echo -e " --core \t minimal initramfs (stage 3.1) to mount the system-container (stage 3.2)." + echo -e " --tools \t minimal systemd-based rootfs including basic tools (required for --core)." echo -e "" - echo -e " OPTIONS:" - echo -e " build, -b \t builds [MODULE]" - echo -e " clean, -c \t clean build files for [MODULE]" - echo -e " debug, -d \t activates debug output." + echo -e " Target options:" + echo -e " -b \t build current target" + echo -e " -c \t clean current target" + echo -e " -d \t activates debug output for current target" echo -e "" - echo -e " Module specific options:" - echo -e " tools [OPTION] [TOOL]" - echo -e " TOOL can be: \t $(echo $(ls ${ROOT_DIR}/remote/tools))" + echo -e " For target tools, you can pass names of specific modules to clean/build." + echo -e " Otherwise, all modules will be built/cleaned." + echo -e "" + echo -e " Examples:" + echo -e " --tools -c -b base policykit sshd (clean all tools, build base, policykit and sshd)" + echo -e " --core -c -b --tools -c -b (clean all tools, build all tools, clean core, build core)" + echo -e " --tools -c base sshd -b sshd, ldm (clean base and sshd, build sshd and ldm, be verbose)" + echo -e "" + echo -e " Existing modules for --tools are:" + echo -e " $(echo $(ls ${ROOT_DIR}/remote/tools))" } initial_checks() { @@ -71,74 +77,104 @@ initial_checks() { fi # setup_tools and build_core - BUILD_CORE=${ROOT_DIR}/server/build_core - SETUP_TOOLS=${ROOT_DIR}/remote/setup_tools + BUILD_CORE="${ROOT_DIR}/server/build_core" + SETUP_TOOLS="${ROOT_DIR}/remote/setup_tools" - [ ! -e ${BUILD_CORE} ] && echo "Missing script build_core, re-clone. Exiting." && exit 1 - [ ! -e ${SETUP_TOOLS} ] && echo "Missing script setup_tools, re-clone. Exiting." && exit 1 + [ ! -e "${BUILD_CORE}" ] && perror "Missing script build_core, re-clone. Exiting." + [ ! -e "${SETUP_TOOLS}" ] && perror "Missing script setup_tools, re-clone. Exiting." } read_params() { - MODULE=$1 - case "$1" in - core) - TARGET_CORE=1 - ;; - tools) - TARGET_TOOLS=1 - ;; - all) - TARGET_CORE=1 - TARGET_TOOLS=1 - ;; - *) - print_usage - exit 1 - ;; - esac - shift - - while true ; do - case "$1" in - -c|clean) - CLEAN=1 - shift - ;; - -b|build) - BUILD=1 - shift - ;; - -d|debug) - DEBUG=1 - shift - ;; - *) - break - ;; - esac + local MODE="" + local SUBMODE="" + while [ "$#" -gt "0" ]; do + local PARAM="$1" + shift + + # select target: core or tools + if [[ "$PARAM" == "--"* ]]; then + case "$PARAM" in + --core) + MODE="CORE" + ;; + --tools) + MODE="TOOLS" + ;; + *) + pwarning "Unknown target: $PARAM" + print_usage + exit 1 + ;; + esac + SUBMODE="" + continue + fi + + [ "x$MODE" = "x" ] && pwarning "Cannot handle param $PARAM - no target selected yet" && print_usage && exit 1 + + # options to current target + if [[ "$PARAM" == "-"* ]]; then + case "$PARAM" in + -c) + SUBMODE="CLEAN" + ;; + -b) + SUBMODE="BUILD" + ;; + -d) + eval ${MODE}_DEBUG="1" + continue + ;; + *) + pwarning "Unknown flag to target: $PARAM" + print_usage + exit 1 + ;; + esac + eval ${MODE}_${SUBMODE}="1" + continue + fi + + # module name + [[ $MODE != TOOLS ]] && pwarning "You cannot specify module names for target CORE." && print_usage && exit 1 + [[ $SUBMODE != CLEAN && $SUBMODE != BUILD ]] && pwarning "Module name given for --tools, but no action specified (eg. build)" && print_usage && exit 1 + eval "${MODE}_LIST_${SUBMODE}=\"\$${MODE}_LIST_${SUBMODE} \$PARAM\"" done + #pinfo "tools clean: $TOOLS_CLEAN -$TOOLS_LIST_CLEAN" + #pinfo "tools build: $TOOLS_BUILD -$TOOLS_LIST_BUILD" + #pinfo "core clean: $CORE_CLEAN" + #pinfo "core build: $CORE_BUILD" + # exit if no command - [ "x$BUILD" = "x" -a "x$CLEAN" = "x" ] && print_usage && exit 1 - - TOOLS="$@" + [[ $CORE_CLEAN == 0 && $CORE_BUILD == 0 && $TOOLS_CLEAN == 0 && $TOOLS_BUILD == 0 ]] && print_usage && exit 1 } run() { - [ "x$DEBUG" != "x1" ] && set_quiet - if [ $TARGET_TOOLS ]; then - . ${SETUP_TOOLS} - [ $CLEAN ] && clean_tools $TOOLS - [ $BUILD ] && generate_stage32 $TOOLS + if [[ $TOOLS_CLEAN == 1 || $TOOLS_BUILD == 1 ]]; then + [[ $TOOLS_DEBUG == 1 ]] && unset_quiet || set_quiet + . "${SETUP_TOOLS}" || perror "Cannot source ${SETUP_TOOLS}" + [[ $TOOLS_CLEAN == 1 ]] && clean_tools $TOOLS_LIST_CLEAN + [[ $TOOLS_BUILD == 1 ]] && generate_stage32 $TOOLS_LIST_BUILD fi - if [ $TARGET_CORE ]; then + if [[ $CORE_CLEAN == 1 || $CORE_BUILD == 1 ]]; then + [[ $CORE_DEBUG == 1 ]] && unset_quiet || set_quiet local TOOL_STR="[CORE]" - . ${BUILD_CORE} - [ $CLEAN ] && clean_core - [ $BUILD ] && generate_stage31 + . "${BUILD_CORE}" || perror "Cannot source ${BUILD_CORE}" + [[ $CORE_CLEAN == 1 ]] && clean_core + [[ $CORE_BUILD == 1 ]] && generate_stage31 fi } +CORE_DEBUG="0" +CORE_BUILD="0" +CORE_CLEAN="0" +TOOLS_DEBUG="0" +TOOLS_CLEAN="0" +TOOLS_BUILD="0" +TOOLS_LIST_CLEAN="" +TOOLS_LIST_BUILD="" + initial_checks read_params $@ diff --git a/server/build_core b/server/build_core index 411036df..03ff6eca 100755 --- a/server/build_core +++ b/server/build_core @@ -27,9 +27,9 @@ STAGE32_DIR=${ROOT_DIR}/remote/stage3.2 # initial checks initial_checks() { local TOOL_STR="$TOOL_STR initial_checks:" - [ -d "${STAGE32_DIR}" ] || perror "No stage3.2 directory found. Please run './mltk tools build' first." + [ -d "${STAGE32_DIR}" ] || perror "No stage3.2 directory found. Please run './mltk --tools -b' first." - [ -e "${STAGE32_DIR}/openslx/bin/busybox" ] || perror "Busybox not found, run './mltk tools build busybox' first." + [ -e "${STAGE32_DIR}/openslx/bin/busybox" ] || perror "Busybox not found, run './mltk --tools -b busybox' first." # shouldn't squashfs-tools be automatically installed as we need them nevertheless!? # what if it is already on the machine? faster to check with which than the apt-get check? -- cgit v1.2.3-55-g7522