diff options
author | torben | 2015-11-20 14:33:22 +0100 |
---|---|---|
committer | torben | 2015-11-20 14:33:22 +0100 |
commit | 1a4ae07cc77fa1187ac956e0e7410449b0cd5ea8 (patch) | |
tree | 6f3c632d0f7286fdfa2f40dd68c6ec40fe564de8 /builder/build-initramfs.sh | |
parent | Fix log level in verbose mode. (diff) | |
download | systemd-init-1a4ae07cc77fa1187ac956e0e7410449b0cd5ea8.tar.gz systemd-init-1a4ae07cc77fa1187ac956e0e7410449b0cd5ea8.tar.xz systemd-init-1a4ae07cc77fa1187ac956e0e7410449b0cd5ea8.zip |
Big refactoring while now using rebash ash bash library.
Diffstat (limited to 'builder/build-initramfs.sh')
-rwxr-xr-x | builder/build-initramfs.sh | 349 |
1 files changed, 156 insertions, 193 deletions
diff --git a/builder/build-initramfs.sh b/builder/build-initramfs.sh index 5e5195fe..a1824842 100755 --- a/builder/build-initramfs.sh +++ b/builder/build-initramfs.sh @@ -61,220 +61,183 @@ core.import exceptions exceptions._init -__NAME__='build-initramfs' +# region properties +build_initramfs_verbose='no' +build_initramfs_debug='no' +build_initramfs_create_system_image='no' +build_initramfs_dependencies=(bash cpio git test shift echo mktemp cat rm sed \ + gzip curl tar grep make) -function build-initramfs() { - # Provides the main module scope. - - # region properties - - local _SCOPE='local' && \ - if [[ $(echo "\"$@\"" | grep --extended-regexp \ - '(^"| )(-l|--load-environment)("$| )') != '' ]] - then - local _SCOPE='export' - fi - # NOTE: Only initialize environment if current scope wasn't set yet. - if [ "$UTILS_VERBOSE" == '' ]; then - "$_SCOPE" UTILS_VERBOSE='no' - "$_SCOPE" _LOAD_ENVIRONMENT='no' - "$_SCOPE" _CREATE_SYSTEM_IMAGE='no' - local dependencies=(bash cpio git test shift echo mktemp cat rm sed \ - gzip curl tar grep make) - "$_SCOPE" _DEPENDENCIES="${dependencies[*]}" - fi - - # endregion +# endregion - # region functions +# region functions - ## region command line interface +## region command line interface - function build_initramfs_print_usage_message() { - # Prints a description about how to use this program. - logging.cat << EOF -$__NAME__ provides a generic way to install systemd based remote linux +function build_initramfs_print_usage_message() { + # Prints a description about how to use this program. + logging.cat << EOF +This program provides a generic way to install systemd based remote linux initramfs. EOF - } - function build_initramfs_print_usage_examples() { - # Prints a description about how to use this program by providing - # examples. - logging.cat << EOF - # Start install progress: - # >>> ./build_initramfs.sh +} +function build_initramfs_print_usage_examples() { + # Prints a description about how to use this program by providing examples. + logging.cat << EOF +# Start install progress: +# >>> ./build_initramfs.sh EOF - } - function build_initramfs_print_command_line_option_description() { - # Prints descriptions about each available command line option. - logging.cat << EOF - -h --help Shows this help message. +} +function build_initramfs_print_command_line_option_description() { + # Prints descriptions about each available command line option. + logging.cat << EOF +-h --help Shows this help message. - -v --verbose Tells you what is going on (default: "$_VERBOSE"). +-v --verbose Tells you what is going on (default: "$build_initramfs_verbose"). - -d --debug Gives you any output from all tools which are used - (default: "$_DEBUG"). +-d --debug Gives you any output from all tools which are used + (default: "$build_initramfs_debug"). - -l --load-environment Simple load the install arch linux scope without - doing anything else. +-l --load-environment Simple load the install arch linux scope without + doing anything else. - -c --create-system-image Creates a system image file for given path - (default: "$_CREATE_SYSTEM_IMAGE"). +-c --create-system-image Creates a system image file for given path + (default: "$build_initramfs_create_system_image"). EOF - } - function build_initramfs_print_help_message() { - # Provides a help message for this module. - logging.plain "\nUsage: $0 [options]\n" - build_initramfs_print_usage_message "$@" - logging.plain '\nExamples:\n' - build_initramfs_print_usage_examples "$@" - logging.plain -e '\nOption descriptions:\n' - build_initramfs_print_command_line_option_description "$@" - logging.plain - } - function build_initramfs_command_line_interface() { - # Provides the command line interface and interactive questions. - while true; do - case "$1" in - -h|--help) - shift - build_initramfs_print_help_message "$0" - exit 0 - ;; - -v|--verbose) - shift - logging.set_commands_log_level debug - logging.set_log_level info - ;; - -d|--debug) - shift - _DEBUG='yes' - logging.set_commands_log_level debug - logging.set_log_level debug - ;; - -l|--load-environment) - shift - _LOAD_ENVIRONMENT='yes' - ;; - -c|--create-system-image) - shift - _CREATE_SYSTEM_IMAGE="$1" - shift - ;; - - '') - shift - break - ;; - *) - logging.critical \ - "Given argument: \"$1\" is not available.\n" - if [[ "$_SCOPE" == 'local' ]]; then - build_initramfs_print_help_message "$0" - fi - return 1 - esac - done - if [[ "$UID" != '0' ]]; then - logging.critical \ - "You have to run this script as \"root\" not as \"${USER}\"." - exit 2 - fi - return 0 - } - - ## endregion - - function build_initramfs_initialize_dracut() { - # Downloads and compiles dracut. - # - # Examples: - # - # >>> build_initramfs_initialize_dracut - # ... - mkdir dracut - logging.info 'Download and extract dracut.' - curl --location \ - https://www.kernel.org/pub/linux/utils/boot/dracut/dracut-043.tar.gz | \ - tar --extract --gzip --directory dracut --strip-components 1 - pushd dracut - # NOTE: On virtualbox shared folder symlinks are not allowed. - # make the dracut-install binary (dracut-install resolves dependencies etc.) - logging.info 'Compiling dracut.' - make install/dracut-install - # NOTE: We have to copy the binary to current instead of symlinking - # them since this feature isn't supported in shared virtual box - # machine folders. If symlinks would be available we could simply - # use: - # >>> make dracut-install - cp install/dracut-install dracut-install - popd - return $? - } - function build_initramfs_create_qcow2_system() { - # Packs current distribution in a qcow2 container. - # - # Examples: - # - # >>> build_initramfs_create_qcow2_system - # TODO - qemu-img create -f qcow2 "${CONTAINER_PATH}" "${QCOW_SIZE}" - return $? - } - - # endregion - - # region controller - - if [[ "$0" == *"${__NAME__}.sh" ]]; then - utils.dependency_check "${_DEPENDENCIES[*]}" - build_initramfs_command_line_interface "$@" || return $? - logging.info 'Checking dracut.' - if ! [[ -d ./dracut ]]; then - logging.info "Dracut isn't available yet loading it." - build_initramfs_initialize_dracut - fi - logging.info 'Build initramfs.' - logging.info 'Link dnbd3 plugin into dracut modules folder.' - local dracut_modules_source='../../dnbd3-rootfs' - local dracut_modules_target='dracut/modules.d/90dnbd3-rootfs' - if [[ ! -L "$dracut_modules_target" || "$(readlink \ - "$dracut_modules_target")" != "$dracut_modules_source" ]] - then - ln --symbolic "$dracut_modules_source" "$dracut_modules_target" - # NOTE: If you copy we would have to recompile each binaries every - # time. - #cp --recursive --force --no-target-directory \ - # dnbd3-rootfs/ dracut/modules.d/90dnbd3-rootfs \ - # 1>"$UTILS_STANDARD_OUTPUT" 2>"$UTILS_ERROR_OUTPUT" - fi - local loglevel='' - if [ "$UTILS_VERBOSE" == 'yes' ]; then - loglevel='--verbose' - fi - local modules='dnbd3-rootfs' - if [ "$_DEBUG" == 'yes' ]; then - loglevel="$loglevel --stdlog 4" - modules="$modules i18n" - fi - dracut/dracut.sh --local $loglevel --force --modules "$modules" \ - --no-hostonly /boot/initramfs-test.img - logging.info 'All done!' +} +function build_initramfs_print_help_message() { + # Provides a help message for this module. + logging.plain "\nUsage: $0 [options]\n" + build_initramfs_print_usage_message "$@" + logging.plain '\nExamples:\n' + build_initramfs_print_usage_examples "$@" + logging.plain -e '\nOption descriptions:\n' + build_initramfs_print_command_line_option_description "$@" + logging.plain +} +function build_initramfs_command_line_interface() { + # Provides the command line interface and interactive questions. + while true; do + case "$1" in + -h|--help) + shift + build_initramfs_print_help_message "$0" + exit 0 + ;; + -v|--verbose) + shift + logging.set_commands_log_level debug + logging.set_log_level info + ;; + -d|--debug) + shift + build_initramfs_debug='yes' + logging.set_commands_log_level debug + logging.set_log_level debug + ;; + -c|--create-system-image) + shift + build_initramfs_create_system_image="$1" + shift + ;; + + '') + shift + break + ;; + *) + logging.critical \ + "Given argument: \"$1\" is not available.\n" + build_initramfs_print_help_message "$0" + return 1 + esac + done + if [[ "$UID" != '0' ]]; then + logging.critical \ + "You have to run this script as \"root\" not as \"${USER}\"." + exit 2 fi return 0 +} - # endregion - +## endregion + +function build_initramfs_initialize_dracut() { + # Downloads and compiles dracut. + # + # Examples: + # + # >>> build_initramfs_initialize_dracut + # ... + mkdir dracut + logging.info 'Download and extract dracut.' + curl --location \ + https://www.kernel.org/pub/linux/utils/boot/dracut/dracut-043.tar.gz | \ + tar --extract --gzip --directory dracut --strip-components 1 + pushd dracut + # NOTE: On virtualbox shared folder symlinks are not allowed. + # NOTE: make the dracut-install binary (dracut-install resolves + # dependencies etc.) + logging.info 'Compiling dracut.' + make install/dracut-install + # NOTE: We have to copy the binary to current instead of symlinking them + # since this feature isn't supported in shared virtual box machine folders. + # If symlinks would be available we could simply use: + # >>> make dracut-install + cp install/dracut-install dracut-install + popd + return $? +} +function build_initramfs_create_qcow2_system() { + # Packs current distribution in a qcow2 container. + # + # Examples: + # + # >>> build_initramfs_create_qcow2_system + # TODO + qemu-img create -f qcow2 "${CONTAINER_PATH}" "${QCOW_SIZE}" + return $? } -# region footer +# endregion -if [[ "$0" == *"${__NAME__}.sh" || $(echo "$@" | grep --extended-regexp \ - '(^| )(-l|--load-environment)($| )') ]] +# region controller + +utils.dependency_check "${build_initramfs_dependencies[*]}" +build_initramfs_command_line_interface "$@" +logging.info 'Checking dracut.' +if ! [[ -d ./dracut ]]; then + logging.info "Dracut isn't available yet loading it." + build_initramfs_initialize_dracut +fi +logging.info 'Build initramfs.' +logging.info 'Link dnbd3 plugin into dracut modules folder.' +build_initramfs__dracut_modules_source='../../dnbd3-rootfs' +build_initramfs__dracut_modules_target='dracut/modules.d/90dnbd3-rootfs' +if [[ ! -L "$build_initramfs__dracut_modules_target" || "$(readlink \ + "$build_initramfs__dracut_modules_target")" != \ + "$build_initramfs__dracut_modules_source" ]] then - "$__NAME__" "$@" + ln --symbolic "$build_initramfs__dracut_modules_source" \ + "$build_initramfs__dracut_modules_target" + # NOTE: If you copy we would have to recompile each binaries every time: + # >>> cp --recursive --force --no-target-directory \ + # ... "$(basename "$build_initramfs__dracut_modules_source")" \ + # ... "$build_initramfs__dracut_modules_target" +fi +build_initramfs__loglevel='' +if [ "$build_initramfs_verbose" == 'yes' ]; then + build_initramfs__loglevel='--verbose' +fi +build_initramfs__modules='dnbd3-rootfs' +if [ "$build_initramfs_debug" == 'yes' ]; then + build_initramfs__loglevel="$build_initramfs__loglevel --stdlog 4" + build_initramfs__modules="$build_initramfs__modules i18n" fi +dracut/dracut.sh --local $build_initramfs__loglevel --force --modules \ + "$build_initramfs__modules" --no-hostonly /boot/initramfs-test.img +logging.info 'All done!' # endregion |