From 2bdc491bb1062b8183aa1da93fb22cd1d5412884 Mon Sep 17 00:00:00 2001 From: jandob Date: Fri, 8 Jan 2016 12:34:18 +0100 Subject: flatten hooks directory tree --- builder/dnbd3-rootfs/hooks/cmdline/enable-sysrq.sh | 2 - .../prepare-kernel-command-line-parameter.sh | 75 ----------- .../cmdline/set-dracut-environment-variables.sh | 8 -- .../copy-openslx-configuration-into-newroot.sh | 3 + builder/dnbd3-rootfs/hooks/enable-sysrq.sh | 2 + builder/dnbd3-rootfs/hooks/fetch-config.sh | 44 ++++++ .../hooks/load-custom-kernel-modules.sh | 29 ++++ builder/dnbd3-rootfs/hooks/mount-root-device.sh | 11 ++ builder/dnbd3-rootfs/hooks/mount-tmp.sh | 40 ++++++ .../dnbd3-rootfs/hooks/mount/mount-root-device.sh | 11 -- .../dnbd3-rootfs/hooks/pre-mount/fetch-config.sh | 44 ------ .../hooks/pre-mount/prepare-root-partition.sh | 149 --------------------- .../copy-openslx-configuration-into-newroot.sh | 3 - builder/dnbd3-rootfs/hooks/pre-pivot/mount-tmp.sh | 40 ------ .../hooks/pre-udev/load-custom-kernel-modules.sh | 29 ---- .../hooks/prepare-kernel-command-line-parameter.sh | 75 +++++++++++ .../dnbd3-rootfs/hooks/prepare-root-partition.sh | 149 +++++++++++++++++++++ .../hooks/set-dracut-environment-variables.sh | 8 ++ builder/dnbd3-rootfs/module-setup.sh | 22 +-- 19 files changed, 372 insertions(+), 372 deletions(-) delete mode 100755 builder/dnbd3-rootfs/hooks/cmdline/enable-sysrq.sh delete mode 100755 builder/dnbd3-rootfs/hooks/cmdline/prepare-kernel-command-line-parameter.sh delete mode 100755 builder/dnbd3-rootfs/hooks/cmdline/set-dracut-environment-variables.sh create mode 100755 builder/dnbd3-rootfs/hooks/copy-openslx-configuration-into-newroot.sh create mode 100755 builder/dnbd3-rootfs/hooks/enable-sysrq.sh create mode 100755 builder/dnbd3-rootfs/hooks/fetch-config.sh create mode 100755 builder/dnbd3-rootfs/hooks/load-custom-kernel-modules.sh create mode 100755 builder/dnbd3-rootfs/hooks/mount-root-device.sh create mode 100755 builder/dnbd3-rootfs/hooks/mount-tmp.sh delete mode 100755 builder/dnbd3-rootfs/hooks/mount/mount-root-device.sh delete mode 100755 builder/dnbd3-rootfs/hooks/pre-mount/fetch-config.sh delete mode 100755 builder/dnbd3-rootfs/hooks/pre-mount/prepare-root-partition.sh delete mode 100755 builder/dnbd3-rootfs/hooks/pre-pivot/copy-openslx-configuration-into-newroot.sh delete mode 100755 builder/dnbd3-rootfs/hooks/pre-pivot/mount-tmp.sh delete mode 100755 builder/dnbd3-rootfs/hooks/pre-udev/load-custom-kernel-modules.sh create mode 100755 builder/dnbd3-rootfs/hooks/prepare-kernel-command-line-parameter.sh create mode 100755 builder/dnbd3-rootfs/hooks/prepare-root-partition.sh create mode 100755 builder/dnbd3-rootfs/hooks/set-dracut-environment-variables.sh diff --git a/builder/dnbd3-rootfs/hooks/cmdline/enable-sysrq.sh b/builder/dnbd3-rootfs/hooks/cmdline/enable-sysrq.sh deleted file mode 100755 index 30b12ea0..00000000 --- a/builder/dnbd3-rootfs/hooks/cmdline/enable-sysrq.sh +++ /dev/null @@ -1,2 +0,0 @@ -# Enables magic sysrq kernel supported key combinations. -echo 1 > /proc/sys/kernel/sysrq diff --git a/builder/dnbd3-rootfs/hooks/cmdline/prepare-kernel-command-line-parameter.sh b/builder/dnbd3-rootfs/hooks/cmdline/prepare-kernel-command-line-parameter.sh deleted file mode 100755 index 4c388178..00000000 --- a/builder/dnbd3-rootfs/hooks/cmdline/prepare-kernel-command-line-parameter.sh +++ /dev/null @@ -1,75 +0,0 @@ -#!/usr/bin/env bash -( # subshell for variable scoping -SLX_INITIAL_KERNEL_COMMAND_LINE="$(cat /proc/cmdline | tr --delete '\n')" - -# This location will be used to have a writable kernel command line file -# location. -writeable_proc_cmdline_path='/writable_proc_cmdline/' -mkdir --parents "$writeable_proc_cmdline_path" -# NOTE: The fake writeable overlay have to be a temporary filesystem for the -# hack to work. -mount --types tmpfs tmpfs "$writeable_proc_cmdline_path" -echo -n "$SLX_INITIAL_KERNEL_COMMAND_LINE" > \ - "${writeable_proc_cmdline_path}/cmdline" - -echo -e \ - '-----------------------Kernel-Command-Line:--------------------------\n' \ - "${SLX_INITIAL_KERNEL_COMMAND_LINE}\n" \ - '----------------------------------------------------------------------' - -for parameter in $(getargs BOOTIF=); do - info "PXE given boot interface $parameter" - dracut_interface_name="bootnet:$(echo $parameter | \ - sed --regexp-extended 's/.{2}[:\-]((.{2}[:\-]){5}.{2})/\1/' | \ - sed s/-/:/g)" - info "Dracut interface name is: $dracut_interface_name" - echo -n " ifname=$dracut_interface_name" >> \ - "${writeable_proc_cmdline_path}cmdline" -done -for parameter in $(getargs ip=); do - temp="$parameter:" - set -- - while [ -n "$temp" ]; do - set -- "$@" "${temp%%:*}" - temp=${temp#*:} - done - - [ -n "$1" ] && ip=$1 - [ -n "$2" ] && server_ip=$2 - [ -n "$3" ] && gateway_ip=$3 - [ -n "$4" ] && net_mask=$4 - - info "PXE given net configuration: ip: $ip server_ip: $server_ip gateway_ip: $gateway_ip net_mask: $net_mask" - dracut_ip_configuration="$ip::$gateway_ip:$net_mask::bootnet:off" - info "Dracut ip configuration is: $dracut_ip_configuration" - sed --regexp-extended "s/ip=[^ ]*/ip=$dracut_ip_configuration/g" \ - --in-place "${writeable_proc_cmdline_path}cmdline" -done - -if [ -z "$dracut_ip_configuration" ]; then - warn 'No "ip" parameter found in the kernel command line.' -fi -if [ -z "$dracut_interface_name" ]; then - warn 'No "BOOTIF" parameter found in the kernel command line.' -fi -mount --options bind "${writeable_proc_cmdline_path}cmdline" /proc/cmdline - -if [ -z $(getargs slxsrv=) ]; then - warn 'No "slxsrv" parameter found in the kernel command line.' - exit 1 -fi -if [ -z $(getargs slxbase=) ]; then - warn 'No "slxbase" parameter found in the kernel command line.' - exit 1 -fi - -echo '-----------------------Dracut-Kernel-Command-Line:----------------------' -cat /proc/cmdline -echo '------------------------------------------------------------------------' -) || exit $? -# region vim modline - -# vim: set tabstop=4 shiftwidth=4 expandtab: -# vim: foldmethod=marker foldmarker=region,endregion: - -# endregion diff --git a/builder/dnbd3-rootfs/hooks/cmdline/set-dracut-environment-variables.sh b/builder/dnbd3-rootfs/hooks/cmdline/set-dracut-environment-variables.sh deleted file mode 100755 index a934731e..00000000 --- a/builder/dnbd3-rootfs/hooks/cmdline/set-dracut-environment-variables.sh +++ /dev/null @@ -1,8 +0,0 @@ -# Set rootok and root as dracut expects them to be set by the module preparing -# the root filesystem. - -# Tell dracut that we parsed the command line and all needed parameters are -# available. -rootok=1 -# Tell dracut where the final root fs will be located. -root=block:/dev/devicemapper/root diff --git a/builder/dnbd3-rootfs/hooks/copy-openslx-configuration-into-newroot.sh b/builder/dnbd3-rootfs/hooks/copy-openslx-configuration-into-newroot.sh new file mode 100755 index 00000000..fd00593c --- /dev/null +++ b/builder/dnbd3-rootfs/hooks/copy-openslx-configuration-into-newroot.sh @@ -0,0 +1,3 @@ +source "/etc/openslx" +mkdir --parents "${NEWROOT}${SLX_CONFIGURATION_LOCATION}" +cp "/etc/openslx" "${NEWROOT}${SLX_CONFIGURATION_LOCATION}" diff --git a/builder/dnbd3-rootfs/hooks/enable-sysrq.sh b/builder/dnbd3-rootfs/hooks/enable-sysrq.sh new file mode 100755 index 00000000..30b12ea0 --- /dev/null +++ b/builder/dnbd3-rootfs/hooks/enable-sysrq.sh @@ -0,0 +1,2 @@ +# Enables magic sysrq kernel supported key combinations. +echo 1 > /proc/sys/kernel/sysrq diff --git a/builder/dnbd3-rootfs/hooks/fetch-config.sh b/builder/dnbd3-rootfs/hooks/fetch-config.sh new file mode 100755 index 00000000..299e3135 --- /dev/null +++ b/builder/dnbd3-rootfs/hooks/fetch-config.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env bash +# region imports +type getarg >/dev/null 2>&1 || source /lib/dracut-lib.sh +source "/usr/lib/rebash/core.sh" +core.import exceptions +exceptions.activate +core.import utils +core.import logging +# endregion +( # subshell for variable scoping +logging.set_commands_log_level debug +logging.set_log_level debug + +# TODO make configurable via kernel command line +configuration_file_name='config' + +slx_server="$(getargs slxsrv=)" +slx_server_base="$(getargs slxbase=)" + +logging.info 'Getting configuration file.' +IFS_backup="$IFS" +IFS="," +host +for host in ${slx_server}; do + logging.info "Trying host \"$host\"." + if wget --timeout 5 "http://${host}/${slx_server_base}${configuration_file_name}" \ + --output-document "/etc/openslx"; then + break + fi +done +IFS="$IFS_backup" + +if [[ ! -e "/etc/openslx" ]]; then + logging.warn "Downloading OpenSLX configuration file from any of the servers \"${slx_server}\" at location \"${slx_server_base}${configuration_file_name}\" failed. Return code: $return_code" + exit 1 +fi +) || exit $? +exceptions.deactivate +# region vim modline + +# vim: set tabstop=4 shiftwidth=4 expandtab: +# vim: foldmethod=marker foldmarker=region,endregion: + +# endregion diff --git a/builder/dnbd3-rootfs/hooks/load-custom-kernel-modules.sh b/builder/dnbd3-rootfs/hooks/load-custom-kernel-modules.sh new file mode 100755 index 00000000..02d697d3 --- /dev/null +++ b/builder/dnbd3-rootfs/hooks/load-custom-kernel-modules.sh @@ -0,0 +1,29 @@ +( # subshell for variable scoping +NBD_MOD_PATH="/usr/lib/modules/current/extra/nbd.ko" +DNBD3_MOD_PATH="/usr/lib/modules/current/extra/dnbd3.ko" + +# do we actually have our modules? +if [ ! -e "${NBD_MOD_PATH}" ]; then + warn "No such file of directory: ${NBD_MOD_PATH}" + emergency_shell -n "Error in $0" + exit 1 +fi +if [ ! -e "${DNBD3_MOD_PATH}" ]; then + warn "No such file of directory: ${DNBD3_MOD_PATH}" + emergency_shell -n "Error in $0" + exit 1 +fi + +# load the kernel modules for dnbd3 and nbd +if ! insmod "${DNBD3_MOD_PATH}"; then + warn "Failed to load DNBD3 kernel module..." + emergency_shell -n "Error in $0" + exit 1 +fi + +if ! modprobe nbd max_part=16; then + warn "Failed to load NBD kernel module..." + emergency_shell -n "Error in $0" + exit 1 +fi +) || exit $? diff --git a/builder/dnbd3-rootfs/hooks/mount-root-device.sh b/builder/dnbd3-rootfs/hooks/mount-root-device.sh new file mode 100755 index 00000000..a842d9e3 --- /dev/null +++ b/builder/dnbd3-rootfs/hooks/mount-root-device.sh @@ -0,0 +1,11 @@ +( # subshell for variable scoping +source "/etc/openslx" +mount /dev/mapper/root "$NEWROOT" $SLX_MOUNT_ROOT_OPTIONS +genfstab "$NEWROOT" > "$NEWROOT/etc/fstab" +) || exit $? +# region vim modline + +# vim: set tabstop=4 shiftwidth=4 expandtab: +# vim: foldmethod=marker foldmarker=region,endregion: + +# endregion diff --git a/builder/dnbd3-rootfs/hooks/mount-tmp.sh b/builder/dnbd3-rootfs/hooks/mount-tmp.sh new file mode 100755 index 00000000..c1366877 --- /dev/null +++ b/builder/dnbd3-rootfs/hooks/mount-tmp.sh @@ -0,0 +1,40 @@ +# This script only checks if we found a usable partition for the +# future /tmp. The discovery of that partition is done by udev during +# the initqueue. If a valid partition is found (either GPT with the label +# OPENSLX_TMP or MBR with the type 0x44) its path will be written to +# /tmp/openslx.tmpdisk +OPENSLX_TMP_DISK_FLAG="/tmp/openslx.disk.tmp" + +if [ ! -e "$OPENSLX_TMP_DISK_FLAG" ]; then + warn "'$OPENSLX_TMP_DISK_FLAG' not found!" + warn "Systemd will manage $NEWROOT/tmp on its own." + # no partition for the future /tmp found, just + # let systemd manage it then (probably a tmpfs) + return 1 +fi + +# in /tmp/openslx.disk.tmp is the name of the device +# to mount as /tmp in the real system +# meaning we need to mount it to /sysroot/tmp here. + +OPENSLX_TMP_DISK_DEV="$(cat $OPENSLX_TMP_DISK_FLAG)" + +# sanity check: is the content a block device? +if [ ! -b "$OPENSLX_TMP_DISK_DEV" ]; then + warn "'$OPENSLX_TMP_DISK_DEV' appears not to be a block device!" + warn "Systemd will manage $NEWROOT/tmp on its own." + return 1 +fi + +# all good, keep on +if ! mount -t auto "$OPENSLX_TMP_DISK_DEV" $NEWROOT/tmp; then + # something else went wrong :( + warn "Mounting '$OPENSLX_TMP_DISK_DEV' to '$NEWROOT/tmp' failed with: $!" + warn "Systemd will manage $NEWROOT/tmp on its own." + return 1 +fi + +# still here? mount worked wohoo +# set permissions +chmod a+rwxt $NEWROOT/tmp +return 0 diff --git a/builder/dnbd3-rootfs/hooks/mount/mount-root-device.sh b/builder/dnbd3-rootfs/hooks/mount/mount-root-device.sh deleted file mode 100755 index a842d9e3..00000000 --- a/builder/dnbd3-rootfs/hooks/mount/mount-root-device.sh +++ /dev/null @@ -1,11 +0,0 @@ -( # subshell for variable scoping -source "/etc/openslx" -mount /dev/mapper/root "$NEWROOT" $SLX_MOUNT_ROOT_OPTIONS -genfstab "$NEWROOT" > "$NEWROOT/etc/fstab" -) || exit $? -# region vim modline - -# vim: set tabstop=4 shiftwidth=4 expandtab: -# vim: foldmethod=marker foldmarker=region,endregion: - -# endregion diff --git a/builder/dnbd3-rootfs/hooks/pre-mount/fetch-config.sh b/builder/dnbd3-rootfs/hooks/pre-mount/fetch-config.sh deleted file mode 100755 index 299e3135..00000000 --- a/builder/dnbd3-rootfs/hooks/pre-mount/fetch-config.sh +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/env bash -# region imports -type getarg >/dev/null 2>&1 || source /lib/dracut-lib.sh -source "/usr/lib/rebash/core.sh" -core.import exceptions -exceptions.activate -core.import utils -core.import logging -# endregion -( # subshell for variable scoping -logging.set_commands_log_level debug -logging.set_log_level debug - -# TODO make configurable via kernel command line -configuration_file_name='config' - -slx_server="$(getargs slxsrv=)" -slx_server_base="$(getargs slxbase=)" - -logging.info 'Getting configuration file.' -IFS_backup="$IFS" -IFS="," -host -for host in ${slx_server}; do - logging.info "Trying host \"$host\"." - if wget --timeout 5 "http://${host}/${slx_server_base}${configuration_file_name}" \ - --output-document "/etc/openslx"; then - break - fi -done -IFS="$IFS_backup" - -if [[ ! -e "/etc/openslx" ]]; then - logging.warn "Downloading OpenSLX configuration file from any of the servers \"${slx_server}\" at location \"${slx_server_base}${configuration_file_name}\" failed. Return code: $return_code" - exit 1 -fi -) || exit $? -exceptions.deactivate -# region vim modline - -# vim: set tabstop=4 shiftwidth=4 expandtab: -# vim: foldmethod=marker foldmarker=region,endregion: - -# endregion diff --git a/builder/dnbd3-rootfs/hooks/pre-mount/prepare-root-partition.sh b/builder/dnbd3-rootfs/hooks/pre-mount/prepare-root-partition.sh deleted file mode 100755 index 3b96239b..00000000 --- a/builder/dnbd3-rootfs/hooks/pre-mount/prepare-root-partition.sh +++ /dev/null @@ -1,149 +0,0 @@ -#!/usr/bin/env bash -# region imports -type getarg >/dev/null 2>&1 || source /lib/dracut-lib.sh -source "/usr/lib/rebash/core.sh" -core.import exceptions -exceptions.activate -core.import utils -core.import logging -logging.set_commands_log_level debug -logging.set_log_level debug -# endregion -( # subshell for variable scoping - -SLX_SERVER_BASE="$(getargs slxbase=)" -source /etc/openslx - -# region connect dnbd3 -IFS_backup="$IFS" -IFS="," -return_code=1 -for host in ${SLX_DNBD3_SERVERS}; do - logging.info "Trying host '$host'." - if systemd-preserve-process-marker dnbd3-client --host "$host" --image \ - "${SLX_SERVER_BASE#/}${SLX_DNBD3_IMAGE}" --device "$SLX_DNBD3_DEVICE" \ - --rid "$SLX_DNBD3_RID" - then - return_code=0 - break - fi -done -IFS="$IFS_backup" - -if [[ $return_code != 0 ]]; then - logging.warn "Failed to connect '${SLX_SERVER_BASE#/}${SLX_DNBD3_IMAGE}' from" \ - "one of '$SLX_DNBD3_SERVERS' to '$SLX_DNBD3_DEVICE'." - exit 1 -fi -# endregion - -# region detect read-only partition -read_only_device="$SLX_DNBD3_DEVICE" -if ! qemu-img info --output json "$read_only_device" | grep '"format": "raw"' -then - read_only_device='/dev/nbd0' - systemd-preserve-process-marker qemu-nbd --connect="$read_only_device" \ - "$SLX_DNBD3_DEVICE" --read-only --persistent - - # XXX better way to wait for the device to be made? - i=0 - while [ ! -b "$read_only_device" ]; do - [ $i -ge 20 ] && exit 1 - if [ $UDEVVERSION -ge 143 ]; then - udevadm settle --exit-if-exists="$read_only_device" - else - sleep 0.1 - fi - i=$(($i + 1)) - done - - # NBD doesn't emit uevents when it gets connected, so kick it - echo change > /sys/block/nbd0/uevent - udevadm settle - - # wait for partitions TODO make nicer - #touch $read_only_device - #udevadm settle -else - # workaround to detect partitions on raw disks - # can be omited when the dnbd3 kernel module supports it (like the nbd - # kernel module with parameter 'max_part') - loop_device="$(losetup -f)" - touch $read_only_device - losetup "$loop_device" "$read_only_device" --partscan - udevadm settle - read_only_device="$loop_device" -fi - -for i in 0.5 1 2; do - if read_only_partition=$(utils.find_block_device \ - "$SLX_SYSTEM_PARTITION_IDENTIFIER" "$read_only_device"); then - break - else - logging.warn "Failed to find unique device with identifier" \ - "'${SLX_SYSTEM_PARTITION_IDENTIFIER}'; matched devices:" \ - "'${read_only_partition}'" - sleep $i - fi -done -# endregion - -# region detect writable partition -if ! persistent_device=$(utils.find_block_device \ - "$SLX_WRITABLE_DEVICE_IDENTIFIER"); then - logging.warn "Failed to find unique device with identifier" \ - "'${SLX_WRITABLE_DEVICE_IDENTIFIER}'; matched devices:" \ - "'${persistent_device}'" -fi - -# TODO move somewhere else -#tmp_device="$(utils.find_block_device \ - #'$SLX_TMP_PARTITION_IDENTIFIER')" - -#if [ -n $tmp_device ]; then - #mount --type auto "$tmp_device" "$NEWROOT/tmp" -#fi - -persistent='N' # 'P' for persistent storage, 'N' for not persistent -if [ -n "$persistent_device" ]; then - writable_device="$persistent_device" - if [ "$SLX_WRITABLE_DEVICE_PERSISTENT" = "yes" ]; then - persistent='P' - fi -else - ramdisk_size="$SLX_RAMDISK_SIZE" - if [ "$ramdisk_size" = "" ]; then - ramdisk_size="$(cat /proc/meminfo | awk '/MemTotal/ {print $2}')" - fi - modprobe brd max_part=1 rd_size="$ramdisk_size" - writable_device='/dev/ram0' - persistent='N' -fi - -# persistent storage on a filesystem (e.g. usb stick) -# writable_device="$(losetup -f)" -#dd if=/dev/zero of=persistent_storage.img bs=1k count=1000 -#losetup "$writable_device" persistent_storage.img -#persistent='P' - -# endregion - -# region combine devices with device mapper -partition_size="$(blockdev --getsz "$read_only_partition")" -writable_partition_name='root' -logging.info "Using read-only device: $read_only_partition" -logging.info "Using writable device $writable_device, persistency: $persistent" -chunksize='1' -modprobe dm_snapshot -dmsetup create "$writable_partition_name" --noudevsync --table \ - "0 $partition_size snapshot $read_only_partition $writable_device $persistent $chunksize" -dmsetup mknodes --noudevsync "$writable_partition_name" -# endregion -) || exit $? -exceptions.deactivate -# region vim modline - -# vim: set tabstop=4 shiftwidth=4 expandtab: -# vim: foldmethod=marker foldmarker=region,endregion: - -# endregion diff --git a/builder/dnbd3-rootfs/hooks/pre-pivot/copy-openslx-configuration-into-newroot.sh b/builder/dnbd3-rootfs/hooks/pre-pivot/copy-openslx-configuration-into-newroot.sh deleted file mode 100755 index fd00593c..00000000 --- a/builder/dnbd3-rootfs/hooks/pre-pivot/copy-openslx-configuration-into-newroot.sh +++ /dev/null @@ -1,3 +0,0 @@ -source "/etc/openslx" -mkdir --parents "${NEWROOT}${SLX_CONFIGURATION_LOCATION}" -cp "/etc/openslx" "${NEWROOT}${SLX_CONFIGURATION_LOCATION}" diff --git a/builder/dnbd3-rootfs/hooks/pre-pivot/mount-tmp.sh b/builder/dnbd3-rootfs/hooks/pre-pivot/mount-tmp.sh deleted file mode 100755 index c1366877..00000000 --- a/builder/dnbd3-rootfs/hooks/pre-pivot/mount-tmp.sh +++ /dev/null @@ -1,40 +0,0 @@ -# This script only checks if we found a usable partition for the -# future /tmp. The discovery of that partition is done by udev during -# the initqueue. If a valid partition is found (either GPT with the label -# OPENSLX_TMP or MBR with the type 0x44) its path will be written to -# /tmp/openslx.tmpdisk -OPENSLX_TMP_DISK_FLAG="/tmp/openslx.disk.tmp" - -if [ ! -e "$OPENSLX_TMP_DISK_FLAG" ]; then - warn "'$OPENSLX_TMP_DISK_FLAG' not found!" - warn "Systemd will manage $NEWROOT/tmp on its own." - # no partition for the future /tmp found, just - # let systemd manage it then (probably a tmpfs) - return 1 -fi - -# in /tmp/openslx.disk.tmp is the name of the device -# to mount as /tmp in the real system -# meaning we need to mount it to /sysroot/tmp here. - -OPENSLX_TMP_DISK_DEV="$(cat $OPENSLX_TMP_DISK_FLAG)" - -# sanity check: is the content a block device? -if [ ! -b "$OPENSLX_TMP_DISK_DEV" ]; then - warn "'$OPENSLX_TMP_DISK_DEV' appears not to be a block device!" - warn "Systemd will manage $NEWROOT/tmp on its own." - return 1 -fi - -# all good, keep on -if ! mount -t auto "$OPENSLX_TMP_DISK_DEV" $NEWROOT/tmp; then - # something else went wrong :( - warn "Mounting '$OPENSLX_TMP_DISK_DEV' to '$NEWROOT/tmp' failed with: $!" - warn "Systemd will manage $NEWROOT/tmp on its own." - return 1 -fi - -# still here? mount worked wohoo -# set permissions -chmod a+rwxt $NEWROOT/tmp -return 0 diff --git a/builder/dnbd3-rootfs/hooks/pre-udev/load-custom-kernel-modules.sh b/builder/dnbd3-rootfs/hooks/pre-udev/load-custom-kernel-modules.sh deleted file mode 100755 index 02d697d3..00000000 --- a/builder/dnbd3-rootfs/hooks/pre-udev/load-custom-kernel-modules.sh +++ /dev/null @@ -1,29 +0,0 @@ -( # subshell for variable scoping -NBD_MOD_PATH="/usr/lib/modules/current/extra/nbd.ko" -DNBD3_MOD_PATH="/usr/lib/modules/current/extra/dnbd3.ko" - -# do we actually have our modules? -if [ ! -e "${NBD_MOD_PATH}" ]; then - warn "No such file of directory: ${NBD_MOD_PATH}" - emergency_shell -n "Error in $0" - exit 1 -fi -if [ ! -e "${DNBD3_MOD_PATH}" ]; then - warn "No such file of directory: ${DNBD3_MOD_PATH}" - emergency_shell -n "Error in $0" - exit 1 -fi - -# load the kernel modules for dnbd3 and nbd -if ! insmod "${DNBD3_MOD_PATH}"; then - warn "Failed to load DNBD3 kernel module..." - emergency_shell -n "Error in $0" - exit 1 -fi - -if ! modprobe nbd max_part=16; then - warn "Failed to load NBD kernel module..." - emergency_shell -n "Error in $0" - exit 1 -fi -) || exit $? diff --git a/builder/dnbd3-rootfs/hooks/prepare-kernel-command-line-parameter.sh b/builder/dnbd3-rootfs/hooks/prepare-kernel-command-line-parameter.sh new file mode 100755 index 00000000..4c388178 --- /dev/null +++ b/builder/dnbd3-rootfs/hooks/prepare-kernel-command-line-parameter.sh @@ -0,0 +1,75 @@ +#!/usr/bin/env bash +( # subshell for variable scoping +SLX_INITIAL_KERNEL_COMMAND_LINE="$(cat /proc/cmdline | tr --delete '\n')" + +# This location will be used to have a writable kernel command line file +# location. +writeable_proc_cmdline_path='/writable_proc_cmdline/' +mkdir --parents "$writeable_proc_cmdline_path" +# NOTE: The fake writeable overlay have to be a temporary filesystem for the +# hack to work. +mount --types tmpfs tmpfs "$writeable_proc_cmdline_path" +echo -n "$SLX_INITIAL_KERNEL_COMMAND_LINE" > \ + "${writeable_proc_cmdline_path}/cmdline" + +echo -e \ + '-----------------------Kernel-Command-Line:--------------------------\n' \ + "${SLX_INITIAL_KERNEL_COMMAND_LINE}\n" \ + '----------------------------------------------------------------------' + +for parameter in $(getargs BOOTIF=); do + info "PXE given boot interface $parameter" + dracut_interface_name="bootnet:$(echo $parameter | \ + sed --regexp-extended 's/.{2}[:\-]((.{2}[:\-]){5}.{2})/\1/' | \ + sed s/-/:/g)" + info "Dracut interface name is: $dracut_interface_name" + echo -n " ifname=$dracut_interface_name" >> \ + "${writeable_proc_cmdline_path}cmdline" +done +for parameter in $(getargs ip=); do + temp="$parameter:" + set -- + while [ -n "$temp" ]; do + set -- "$@" "${temp%%:*}" + temp=${temp#*:} + done + + [ -n "$1" ] && ip=$1 + [ -n "$2" ] && server_ip=$2 + [ -n "$3" ] && gateway_ip=$3 + [ -n "$4" ] && net_mask=$4 + + info "PXE given net configuration: ip: $ip server_ip: $server_ip gateway_ip: $gateway_ip net_mask: $net_mask" + dracut_ip_configuration="$ip::$gateway_ip:$net_mask::bootnet:off" + info "Dracut ip configuration is: $dracut_ip_configuration" + sed --regexp-extended "s/ip=[^ ]*/ip=$dracut_ip_configuration/g" \ + --in-place "${writeable_proc_cmdline_path}cmdline" +done + +if [ -z "$dracut_ip_configuration" ]; then + warn 'No "ip" parameter found in the kernel command line.' +fi +if [ -z "$dracut_interface_name" ]; then + warn 'No "BOOTIF" parameter found in the kernel command line.' +fi +mount --options bind "${writeable_proc_cmdline_path}cmdline" /proc/cmdline + +if [ -z $(getargs slxsrv=) ]; then + warn 'No "slxsrv" parameter found in the kernel command line.' + exit 1 +fi +if [ -z $(getargs slxbase=) ]; then + warn 'No "slxbase" parameter found in the kernel command line.' + exit 1 +fi + +echo '-----------------------Dracut-Kernel-Command-Line:----------------------' +cat /proc/cmdline +echo '------------------------------------------------------------------------' +) || exit $? +# region vim modline + +# vim: set tabstop=4 shiftwidth=4 expandtab: +# vim: foldmethod=marker foldmarker=region,endregion: + +# endregion diff --git a/builder/dnbd3-rootfs/hooks/prepare-root-partition.sh b/builder/dnbd3-rootfs/hooks/prepare-root-partition.sh new file mode 100755 index 00000000..3b96239b --- /dev/null +++ b/builder/dnbd3-rootfs/hooks/prepare-root-partition.sh @@ -0,0 +1,149 @@ +#!/usr/bin/env bash +# region imports +type getarg >/dev/null 2>&1 || source /lib/dracut-lib.sh +source "/usr/lib/rebash/core.sh" +core.import exceptions +exceptions.activate +core.import utils +core.import logging +logging.set_commands_log_level debug +logging.set_log_level debug +# endregion +( # subshell for variable scoping + +SLX_SERVER_BASE="$(getargs slxbase=)" +source /etc/openslx + +# region connect dnbd3 +IFS_backup="$IFS" +IFS="," +return_code=1 +for host in ${SLX_DNBD3_SERVERS}; do + logging.info "Trying host '$host'." + if systemd-preserve-process-marker dnbd3-client --host "$host" --image \ + "${SLX_SERVER_BASE#/}${SLX_DNBD3_IMAGE}" --device "$SLX_DNBD3_DEVICE" \ + --rid "$SLX_DNBD3_RID" + then + return_code=0 + break + fi +done +IFS="$IFS_backup" + +if [[ $return_code != 0 ]]; then + logging.warn "Failed to connect '${SLX_SERVER_BASE#/}${SLX_DNBD3_IMAGE}' from" \ + "one of '$SLX_DNBD3_SERVERS' to '$SLX_DNBD3_DEVICE'." + exit 1 +fi +# endregion + +# region detect read-only partition +read_only_device="$SLX_DNBD3_DEVICE" +if ! qemu-img info --output json "$read_only_device" | grep '"format": "raw"' +then + read_only_device='/dev/nbd0' + systemd-preserve-process-marker qemu-nbd --connect="$read_only_device" \ + "$SLX_DNBD3_DEVICE" --read-only --persistent + + # XXX better way to wait for the device to be made? + i=0 + while [ ! -b "$read_only_device" ]; do + [ $i -ge 20 ] && exit 1 + if [ $UDEVVERSION -ge 143 ]; then + udevadm settle --exit-if-exists="$read_only_device" + else + sleep 0.1 + fi + i=$(($i + 1)) + done + + # NBD doesn't emit uevents when it gets connected, so kick it + echo change > /sys/block/nbd0/uevent + udevadm settle + + # wait for partitions TODO make nicer + #touch $read_only_device + #udevadm settle +else + # workaround to detect partitions on raw disks + # can be omited when the dnbd3 kernel module supports it (like the nbd + # kernel module with parameter 'max_part') + loop_device="$(losetup -f)" + touch $read_only_device + losetup "$loop_device" "$read_only_device" --partscan + udevadm settle + read_only_device="$loop_device" +fi + +for i in 0.5 1 2; do + if read_only_partition=$(utils.find_block_device \ + "$SLX_SYSTEM_PARTITION_IDENTIFIER" "$read_only_device"); then + break + else + logging.warn "Failed to find unique device with identifier" \ + "'${SLX_SYSTEM_PARTITION_IDENTIFIER}'; matched devices:" \ + "'${read_only_partition}'" + sleep $i + fi +done +# endregion + +# region detect writable partition +if ! persistent_device=$(utils.find_block_device \ + "$SLX_WRITABLE_DEVICE_IDENTIFIER"); then + logging.warn "Failed to find unique device with identifier" \ + "'${SLX_WRITABLE_DEVICE_IDENTIFIER}'; matched devices:" \ + "'${persistent_device}'" +fi + +# TODO move somewhere else +#tmp_device="$(utils.find_block_device \ + #'$SLX_TMP_PARTITION_IDENTIFIER')" + +#if [ -n $tmp_device ]; then + #mount --type auto "$tmp_device" "$NEWROOT/tmp" +#fi + +persistent='N' # 'P' for persistent storage, 'N' for not persistent +if [ -n "$persistent_device" ]; then + writable_device="$persistent_device" + if [ "$SLX_WRITABLE_DEVICE_PERSISTENT" = "yes" ]; then + persistent='P' + fi +else + ramdisk_size="$SLX_RAMDISK_SIZE" + if [ "$ramdisk_size" = "" ]; then + ramdisk_size="$(cat /proc/meminfo | awk '/MemTotal/ {print $2}')" + fi + modprobe brd max_part=1 rd_size="$ramdisk_size" + writable_device='/dev/ram0' + persistent='N' +fi + +# persistent storage on a filesystem (e.g. usb stick) +# writable_device="$(losetup -f)" +#dd if=/dev/zero of=persistent_storage.img bs=1k count=1000 +#losetup "$writable_device" persistent_storage.img +#persistent='P' + +# endregion + +# region combine devices with device mapper +partition_size="$(blockdev --getsz "$read_only_partition")" +writable_partition_name='root' +logging.info "Using read-only device: $read_only_partition" +logging.info "Using writable device $writable_device, persistency: $persistent" +chunksize='1' +modprobe dm_snapshot +dmsetup create "$writable_partition_name" --noudevsync --table \ + "0 $partition_size snapshot $read_only_partition $writable_device $persistent $chunksize" +dmsetup mknodes --noudevsync "$writable_partition_name" +# endregion +) || exit $? +exceptions.deactivate +# region vim modline + +# vim: set tabstop=4 shiftwidth=4 expandtab: +# vim: foldmethod=marker foldmarker=region,endregion: + +# endregion diff --git a/builder/dnbd3-rootfs/hooks/set-dracut-environment-variables.sh b/builder/dnbd3-rootfs/hooks/set-dracut-environment-variables.sh new file mode 100755 index 00000000..a934731e --- /dev/null +++ b/builder/dnbd3-rootfs/hooks/set-dracut-environment-variables.sh @@ -0,0 +1,8 @@ +# Set rootok and root as dracut expects them to be set by the module preparing +# the root filesystem. + +# Tell dracut that we parsed the command line and all needed parameters are +# available. +rootok=1 +# Tell dracut where the final root fs will be located. +root=block:/dev/devicemapper/root diff --git a/builder/dnbd3-rootfs/module-setup.sh b/builder/dnbd3-rootfs/module-setup.sh index c39119ea..2f00f906 100755 --- a/builder/dnbd3-rootfs/module-setup.sh +++ b/builder/dnbd3-rootfs/module-setup.sh @@ -102,9 +102,9 @@ install() { # TODO analyze qemu-nbd pivot-root bug! inst_script "$moddir/services/dnbd3-generator.sh" \ $systemdutildir/system-generators/dnbd3-generator - inst "$moddir/hooks/pre-mount/prepare-root-partition.sh" \ + inst "$moddir/hooks/prepare-root-partition.sh" \ /sbin/dnbd3root - inst "$moddir/hooks/pre-mount/fetch-config.sh" \ + inst "$moddir/hooks/fetch-config.sh" \ /sbin/fetch-config #region old approach #inst_simple "${moddir}/services/dnbd3root.service" \ @@ -122,10 +122,10 @@ install() { else # get the openslx config from the servers configured in the kernel command # line (${SLX_SERVER}/${SLX_SERVER_BASE}/config) - inst_hook pre-mount 00 "$moddir/hooks/pre-mount/fetch-config.sh" + inst_hook pre-mount 00 "$moddir/hooks/fetch-config.sh" # make the final blockdevice for the root system (dnbd3 -> qemu-nbd -> # device-mapper) - inst_hook pre-mount 10 "$moddir/hooks/pre-mount/prepare-root-partition.sh" + inst_hook pre-mount 10 "$moddir/hooks/prepare-root-partition.sh" fi # region binaries @@ -140,17 +140,17 @@ install() { # region hooks - inst_hook cmdline 00 "$moddir/hooks/cmdline/enable-sysrq.sh" - inst_hook cmdline 10 "$moddir/hooks/cmdline/prepare-kernel-command-line-parameter.sh" - inst_hook cmdline 90 "$moddir/hooks/cmdline/set-dracut-environment-variables.sh" + inst_hook cmdline 00 "$moddir/hooks/enable-sysrq.sh" + inst_hook cmdline 10 "$moddir/hooks/prepare-kernel-command-line-parameter.sh" + inst_hook cmdline 90 "$moddir/hooks/set-dracut-environment-variables.sh" # load nbd.ko, dnbd3.ko - inst_hook pre-udev 00 "$moddir/hooks/pre-udev/load-custom-kernel-modules.sh" + inst_hook pre-udev 00 "$moddir/hooks/load-custom-kernel-modules.sh" - inst_hook mount 10 "$moddir/hooks/mount/mount-root-device.sh" + inst_hook mount 10 "$moddir/hooks/mount-root-device.sh" - inst_hook pre-pivot 00 "$moddir/hooks/pre-pivot/copy-openslx-configuration-into-newroot.sh" - #inst_hook pre-pivot 00 "$moddir/hooks/pre-pivot/mount-tmp.sh" + inst_hook pre-pivot 00 "$moddir/hooks/copy-openslx-configuration-into-newroot.sh" + #inst_hook pre-pivot 00 "$moddir/hooks/mount-tmp.sh" # endregion -- cgit v1.2.3-55-g7522