#!/usr/bin/bash # dracut-lib to use debugging functions command -v warn >/dev/null || . /lib/dracut-lib.sh ############################################################################### # GLOBALS # # TODO make this configurable [ -f /opt/openslx/config ] && . /opt/openslx/config declare -rg DNBD3_DEVICE="/dev/dnbd0" declare -rg DNBD3_SERVER="132.230.4.1" declare -rg DNBD3_IMAGE="stage4/joe/centos7" declare -rg DNBD3_RID="4" # # END GLOBALS ############################################################################### ############################################################################### # FUNCTION DEFINITIONS # # helper to do some sanity checks check_dnbd3() { if [ ! command -v "dnbd3-client" >/dev/null ]; then warn "No 'dnbd3-client' found. Was the initramfs built correctly?" emergency_shell -n "Error in $0" return 1 fi return 0 } # helper to connect to the dnbd3-server connect_dnbd3() { # check if it already connected local current_image_name="$(cat /sys/block/${DNBD3_DEVICE#/dev/}/net/image_name)" [ "x${current_image_name}" != "x(null)" ] && return 0 # not connected yet, do it if ! dnbd3-client -h "${DNBD3_SERVER}" \ -i "${DNBD3_IMAGE}" \ -r "${DNBD3_RID}" \ -d "${DNBD3_DEVICE}" ; then warn "Failed to mount $DNBD3_IMAGE from $DNBD3_SERVER to $DNBD3_DEVICE" emergency_shell -n "Error in $0" return 1 fi return 0 } # helper to create the qcow2 container file using # DNBD3_DEVICE as the base of the filesystem # /run/test.qcow2 as the writable file # (our future rootfs) create_qcow() { # check if we already created the qcow2-container [ -e /run/test.qcow2 ] && return 0 # we did not, let's create it if ! qemu-img create -f qcow2 -o \ backing_file="$DNBD3_DEVICE",backing_fmt=qcow2 /run/test.qcow2; then warn "Failed to create qcow2-Container from $DNBD3_DEVICE" emergency_shell -n "Error in $0" rm -f -- /run/test.qcow2 return 1 fi return 0 } # helper to start qemu-nbd on localhost:2000 # use our wrapper to set argv[0][0] to '@' # this keeps qemu-nbd running after switching root export_qcow() { # check if we already have a qemu-nbd if [ -e /tmp/qemu-nbd.pid ]; then kill -0 $(cat /tmp/qemu-nbd.pid) && return 0 fi # since we use the wrapper, we need a little more logic to see if it runs /usr/bin/systemd-preserve-process-marker \ /usr/bin/qemu-nbd -t -p 2000 /run/test.qcow2 & # the wrapper returns 255 if the qemu-nbd binary is missing local qemu_nbd_pid="$!" for i in 0.5 1 2; do sleep $i if ! kill -0 $qemu_nbd_pid; then # not running wait $qemu_nbd_pid local ret_wrapper="$?" if [ "${ret_wrapper}" -eq 127 ]; then # wrapper was not found by bash warn "No such file or directory: /usr/bin/systemd-preserve-process-marker" elif [ "${ret_wrapper}" -eq 255 ]; then # qemu-nbd was not found warn "No such file or directory: /usr/bin/qemu-nbd" fi emergency_shell -n "Error in $0" return 1 else # all good, qemu-nbd is running, remember its pid echo $qemu_nbd_pid > /tmp/qemu-nbd.pid return 0 fi done # fallback return 1 } # # END FUNCTION DEFINITIONS ############################################################################### ############################################################################### # MAIN CODE # #check_dnbd3 || return 1 connect_dnbd3 || exit 1 create_qcow || exit 1 export_qcow || exit 1 # all good, we are done :) exit 0