diff options
author | Jonathan Bauer | 2015-04-30 15:29:44 +0200 |
---|---|---|
committer | Jonathan Bauer | 2015-04-30 15:29:44 +0200 |
commit | 2e98b0942de05bcc2eacd41b0bbac6ab70f620f5 (patch) | |
tree | ccd3b8ba6ecdedba0df5294bd54af7c2163a0348 | |
parent | Fix. (diff) | |
download | systemd-init-2e98b0942de05bcc2eacd41b0bbac6ab70f620f5.tar.gz systemd-init-2e98b0942de05bcc2eacd41b0bbac6ab70f620f5.tar.xz systemd-init-2e98b0942de05bcc2eacd41b0bbac6ab70f620f5.zip |
working version - STILL TESTING
-rwxr-xr-x | testModule/binaries/busybox | bin | 0 -> 504912 bytes | |||
-rwxr-xr-x | testModule/hooks/cmdline.sh | 42 | ||||
-rwxr-xr-x | testModule/hooks/mount.sh | 10 | ||||
-rwxr-xr-x | testModule/hooks/nbd-cmdline.sh | 5 | ||||
-rwxr-xr-x | testModule/hooks/pre-mount.sh | 24 | ||||
-rwxr-xr-x | testModule/hooks/pre-udev.sh | 44 | ||||
-rw-r--r-- | testModule/module-setup.sh | 56 | ||||
-rwxr-xr-x | testModule/scripts/r | 3 | ||||
-rwxr-xr-x | testModule/scripts/retry-nbdroot | 5 | ||||
-rwxr-xr-x | testModule/scripts/setup-nbdroot | 12 | ||||
-rwxr-xr-x | testModule/scripts/setup-qcow2 | 118 |
11 files changed, 259 insertions, 60 deletions
diff --git a/testModule/binaries/busybox b/testModule/binaries/busybox Binary files differnew file mode 100755 index 00000000..7eceacf7 --- /dev/null +++ b/testModule/binaries/busybox diff --git a/testModule/hooks/cmdline.sh b/testModule/hooks/cmdline.sh index 340467df..c38f9ae7 100755 --- a/testModule/hooks/cmdline.sh +++ b/testModule/hooks/cmdline.sh @@ -1,29 +1,23 @@ -# Converts ip configuration format given by the pxe pre boot provider to a -# dracut compatible static ip configuration. +# giev powa +echo 1 > /proc/sys/kernel/sysrq -source /usr/lib/test-library.sh +# fakes the cmdline to fix the ip parsing when using +# syslinux's IPAPPEND 1 mask +[ -d /fake ] || mkdir /fake -for parameter in $(getargs ip=); do - local temp="$parameter:" - set -- - while [ -n "$temp" ]; do - set -- "$@" "${temp%%:*}" - temp=${temp#*:} - done +# need to be a tmpfs for the hack to work +mount -t tmpfs tmpfs /fake - [ -n "$1" ] && ip=$1 - [ -n "$2" ] && server_ip=$2 - [ -n "$3" ] && gateway_ip=$3 - [ -n "$4" ] && net_mask=$4 +# append ':hiwi:eth0:none' to the 'ip=' parameter we got +# from syslinux's IPAPPEND 1 +sed 's/\(ip=\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}:\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}:\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}:\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\)/\1:hiwi:eno1:none/' /proc/cmdline > /fake/cmdline - warn "PXE given net configuration: ip: $ip server_ip: $server_ip gateway_ip: $gateway_ip net_mask: $net_mask" - local final_dracut_ip_config="$ip::$gateway_ip:$net_mask:dracut_test_client:enp0s3:none" - warn "Final dracut ip config: $final_dracut_ip_config" - replace_cmd ip "$final_dracut_ip_config" -done +# bind mount it. Can we trust mount return codes here? +# # if so, we should check what we get in $? +mount -o bind /fake/cmdline /proc/cmdline -# NOTE: Doesn't work since dracuts needed version of mount doesn't support -# binds. -#cat /proc/cmdline | sed --regexp-extended 's/ip=[^ ]+//g' \ -# >"$temporary_kernel_commandline_file_path" && \ -#mount -n --rbind -o ro "$temporary_kernel_commandline_file_path" /proc/cmdline +# check if it worked +if ! grep 'hiwi:eth0:none' /proc/cmdline; then + command -v warn >/dev/null || . /lib/dracut-lib.sh + warn 'Haxing cmdline did not work :( sad pandaz...' +fi diff --git a/testModule/hooks/mount.sh b/testModule/hooks/mount.sh index 28958fb6..e893642d 100755 --- a/testModule/hooks/mount.sh +++ b/testModule/hooks/mount.sh @@ -1,7 +1,3 @@ -if [ $MOUNT_READY ]; then - warn '............MOUNT ALREADY DONE.............' -else - export MOUNT_READY=true - warn '............MOUNT.............' - mount /dev/nbd0 /sysroot -fi +#!/bin/bash + +mount /dev/root $NEWROOT diff --git a/testModule/hooks/nbd-cmdline.sh b/testModule/hooks/nbd-cmdline.sh new file mode 100755 index 00000000..6464646f --- /dev/null +++ b/testModule/hooks/nbd-cmdline.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +rootok=1 +root=block:/dev/root + diff --git a/testModule/hooks/pre-mount.sh b/testModule/hooks/pre-mount.sh index 5a93aef3..b22cdc35 100755 --- a/testModule/hooks/pre-mount.sh +++ b/testModule/hooks/pre-mount.sh @@ -1,19 +1,9 @@ #!/usr/bin/bash -if [ $PRE_MOUNT_READY ]; then - warn '............PRE-MOUNT ALREADY DONE.............' -else - export PRE_MOUNT_READY=true - warn '............PRE-MOUNT.............' - insmod /usr/lib/modules/3.10.0-229.1.2.el7.x86_64/extra/dnbd3.ko - local dnbd3_device_node="$(dnbd3-client -h 132.230.4.1 -i \ - stage4/torben/test -r 1 -d /dev/dnbd0)" - # NOTE: Out comment this line if you need more than on dnbd0 device and - # determine it automatically. - dnbd3_device_node=/dev/dnbd0 - qemu-img create -f qcow2 -o \ - backing_file="$dnbd3_device_node",backing_fmt=qcow2 /run/test.qcow2 - insmod /usr/lib/modules/3.10.0-229.1.2.el7.x86_64/extra/nbd.ko - /usr/bin/systemd-preserve-process-marker /usr/bin/qemu-nbd \ - --connect=/dev/nbd0 /run/test.qcow2 --socket /run/qemu-nbd-TEST -fi +while ! /sbin/setup-qcow2; do + sleep 0.5 +done + +while ! /sbin/setup-nbdroot; do + sleep 0.5 +done diff --git a/testModule/hooks/pre-udev.sh b/testModule/hooks/pre-udev.sh new file mode 100755 index 00000000..578169e9 --- /dev/null +++ b/testModule/hooks/pre-udev.sh @@ -0,0 +1,44 @@ +#!/usr/bin/bash + +# include dracut-lib.sh to use 'warn' +command -v warn >/dev/null || . /lib/dracut-lib.sh + +SETUP_SCRIPT="/sbin/setup-qcow2" +NBDROOT_SCRIPT="/sbin/setup-nbdroot" +NBD_MOD_PATH="/usr/lib/modules/current/extra/nbd.ko" +DNBD3_MOD_PATH="/usr/lib/modules/current/extra/dnbd3.ko" + +# sanity checks +if [ ! -e "${SETUP_SCRIPT}" ]; then + warn "No such file of directory: ${SETUP_SCRIPT}" + emergency_shell -n "Error in $0" + return 1 +fi +if [ ! -e "${NBD_MOD_PATH}" ]; then + warn "No such file of directory: ${NBD_MOD_PATH}" + emergency_shell -n "Error in $0" + return 1 +fi +if [ ! -e "${DNBD3_MOD_PATH}" ]; then + warn "No such file of directory: ${DNBD3_MOD_PATH}" + emergency_shell -n "Error in $0" + return 1 +fi + +# load the kernel modules for dnbd3 and nbd +if ! insmod /usr/lib/modules/current/extra/dnbd3.ko; then + warn "Failed to load DNBD3 kernel module..." + emergency_shell -n "Error in $0" + return 1 +fi + +if ! insmod /usr/lib/modules/current/extra/nbd.ko; then + warn "Failed to load NBD kernel module..." + emergency_shell -n "Error in $0" + return 1 +fi + +# now let's "install" the setup script in the initqueue +#/sbin/initqueue --settled --unique "${SETUP_SCRIPT}" +#/sbin/initqueue --settled --unique "${NBDROOT_SCRIPT}" +#/sbin/initqueue --settled --unique "/sbin/retry-nbdroot" diff --git a/testModule/module-setup.sh b/testModule/module-setup.sh index 74a79d57..839e5deb 100644 --- a/testModule/module-setup.sh +++ b/testModule/module-setup.sh @@ -1,7 +1,35 @@ #!/bin/bash check() { - return 0 + if ! type -f qemu-nbd >/dev/null; then + echo "'qemu-nbd' not found on this system, install it to use this module." + return 1 + fi + if ! type -f qemu-img >/dev/null; then + echo "'qemu-img' not found on this system, install it to use this module." + return 1 + fi + if ! type -f nbd-client >/dev/null; then + echo "'nbd-client' not found on this system, install it to use this module." + return 1 + else + # check version + local nbd_client_version="$(nbd-client --help 2>&1 | grep -oE '3\.[0-9]+')" + local nbd_client_version_major="${nbd_client_version%.*}" + local nbd_client_version_minor="${nbd_client_version#*.}" + if [ "${nbd_client_version_major}" -ne 3 ]; then + # nbd-client probably too old + echo "The nbd-client major version is not 3, meaning its probably too old." + return 1 + fi + if [ "${nbd_client_version_minor}" -gt 8 ]; then + # TODO check if ver = 3.9 has the same new syntax... + echo "The nbd-client minor version is greater than 8. A new syntax has been + introduced starting with 3.10 which we do not support." + return 1 + fi + return 0 + fi } depends() { @@ -10,15 +38,15 @@ depends() { } installkernel() { - instmods nbd ext4 + instmods ext4 } install() { - # Loads globally needed useful functions or fixes some native dracut ones. - inst "$moddir/library.sh" /usr/lib/test-library.sh - + # cause we need reboots + inst "$moddir/scripts/r" /usr/bin/r # Needed to mount remote dnbd3 filesystem. inst "$moddir/binaries/dnbd3-client" /usr/bin/dnbd3-client + inst "$moddir/binaries/busybox" /usr/bin/busybox # A generic wrapper program to prepend a "@" to each process spawned by # given nested programs. inst "$moddir/binaries/systemd-preserve-process-marker" \ @@ -32,17 +60,21 @@ install() { # NOTE: Priority has to be lower than the network cmdline parsing hooks # since we have to modify the some kernel parameter before. - inst_hook cmdline 1 "$moddir/hooks/cmdline.sh" + inst_hook cmdline 00 "$moddir/hooks/cmdline.sh" + inst_hook cmdline 90 "$moddir/hooks/nbd-cmdline.sh" + inst_hook pre-udev 00 "$moddir/hooks/pre-udev.sh" inst_hook pre-mount 10 "$moddir/hooks/pre-mount.sh" - # NOTE: Hook "mount" isn't executed on tested version. - inst_hook pre-pivot 10 "$moddir/hooks/mount.sh" + inst_hook mount 10 "$moddir/hooks/mount.sh" + inst "$moddir/scripts/setup-qcow2" /sbin/setup-qcow2 + inst "$moddir/scripts/setup-nbdroot" /sbin/setup-nbdroot + # Debugging Uncomment this version if you need some useful debugging tools # in your iniramfs. - #inst_multiple lsblk ping ip ifconfig sshd htop dhclient tail head cat vim \ - # touch sed lsmod insmod qemu-img sleep route wget find lsof strace \ - # chroot switch_root pivot_root qemu-nbd + inst_multiple lsblk ping ip ifconfig sshd htop tail head cat vim \ + touch sed lsmod insmod qemu-img sleep route wget find lsof strace \ + chroot switch_root pivot_root qemu-nbd mount nbd-client fdisk # Production: - inst_multiple insmod qemu-img qemu-nbd + # inst_multiple insmod qemu-img qemu-nbd return 0 } diff --git a/testModule/scripts/r b/testModule/scripts/r new file mode 100755 index 00000000..9b4fd4fc --- /dev/null +++ b/testModule/scripts/r @@ -0,0 +1,3 @@ +#!/usr/bin/bash + +echo b > /proc/sysrq-trigger diff --git a/testModule/scripts/retry-nbdroot b/testModule/scripts/retry-nbdroot new file mode 100755 index 00000000..8eb4bd27 --- /dev/null +++ b/testModule/scripts/retry-nbdroot @@ -0,0 +1,5 @@ +#!/bin/bash + +[ -f /tmp/qemu-nbd.pid ] || exit 1 + +/sbin/nbdroot eno1 nbd:127.0.0.1:2000 /sysroot diff --git a/testModule/scripts/setup-nbdroot b/testModule/scripts/setup-nbdroot new file mode 100755 index 00000000..38f48dd4 --- /dev/null +++ b/testModule/scripts/setup-nbdroot @@ -0,0 +1,12 @@ +#!/bin/bash + +# exit 1 until qemu-nbd is running +[ -f /tmp/qemu-nbd.pid ] || exit 1 + +# +if nbd-client --systemd-mark --persist 127.0.0.1 2000 /dev/nbd0; then + ln -sf /dev/nbd0 /dev/root + exit 0 +else + exit 1 +fi diff --git a/testModule/scripts/setup-qcow2 b/testModule/scripts/setup-qcow2 new file mode 100755 index 00000000..ba1ef6b1 --- /dev/null +++ b/testModule/scripts/setup-qcow2 @@ -0,0 +1,118 @@ +#!/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 |