From aba3da2a7fcd51bb1f48cbb06620e1648235a115 Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Thu, 23 Oct 2014 18:08:52 +0200 Subject: [PATCH] Moved common patches --- .../common-patches/usr/local/bin/oc-metadata | 22 ++++++ .../usr/local/bin/oc-metadata-json | 15 ++++ .../usr/sbin/nbd-disconnect-root | 74 +++++++++++++++++++ .../usr/sbin/oc-add-extra-volumes | 70 ++++++++++++++++++ .../usr/sbin/oc-sync-kernel-modules | 29 ++++++++ 5 files changed, 210 insertions(+) create mode 100755 images/common-patches/usr/local/bin/oc-metadata create mode 100755 images/common-patches/usr/local/bin/oc-metadata-json create mode 100755 images/common-patches/usr/sbin/nbd-disconnect-root create mode 100755 images/common-patches/usr/sbin/oc-add-extra-volumes create mode 100755 images/common-patches/usr/sbin/oc-sync-kernel-modules diff --git a/images/common-patches/usr/local/bin/oc-metadata b/images/common-patches/usr/local/bin/oc-metadata new file mode 100755 index 0000000..7dfc7e1 --- /dev/null +++ b/images/common-patches/usr/local/bin/oc-metadata @@ -0,0 +1,22 @@ +#!/bin/sh +# description "executable which retrieves server metadata (TEXT)" +# author "Online Labs Cloud Team " + +CODE=0 +while [ $CODE -ne 200 ] +do + METADATA_URL=${METADATA_URL:-"http://169.254.42.42/conf"} + RESPONSE=$(curl --silent --write-out "\n%{http_CODE}\n" $METADATA_URL) + CODE=$(echo "$RESPONSE" | sed -n '$p') + BODY=$(echo "$RESPONSE" | sed '$d') + + test $CODE -eq 200 && break + sleep 5 +done + +if [ "$#" -ne 1 ]; then + echo "$BODY" +else + key="$1" + echo "$BODY" | grep "^$key=" | cut -d= -f2 | sed "s/^['\"]//;s/['\"]$//" +fi diff --git a/images/common-patches/usr/local/bin/oc-metadata-json b/images/common-patches/usr/local/bin/oc-metadata-json new file mode 100755 index 0000000..a3bb610 --- /dev/null +++ b/images/common-patches/usr/local/bin/oc-metadata-json @@ -0,0 +1,15 @@ +#!/bin/sh +# description "executable which retrieves server metadata (JSON)" +# author "Online Labs Cloud Team " + +CODE=0 +while [ $CODE -ne 200 ] +do + RESPONSE=$(curl --silent --write-out "\n%{http_CODE}\n" http://169.254.42.42/conf?format=json) + CODE=$(echo "$RESPONSE" | sed -n '$p') + BODY=$(echo "$RESPONSE" | sed '$d') + test $CODE -eq 200 && break + sleep 5 +done + +echo "$BODY" diff --git a/images/common-patches/usr/sbin/nbd-disconnect-root b/images/common-patches/usr/sbin/nbd-disconnect-root new file mode 100755 index 0000000..518e257 --- /dev/null +++ b/images/common-patches/usr/sbin/nbd-disconnect-root @@ -0,0 +1,74 @@ +#!/bin/sh +# Thanks to the LTSP project +# If the root /dev/nbd0 device is unmounted on shutdown then nbd read +# errors occur, and if it isn't, then # the nbd-server process on the server +# doesn't terminate. +# Called by init scripts on reboot or shutdown. + +case "$RUNLEVEL" in + 0) + key="o" + command="poweroff -f" + ;; + 6) + key="b" + command="reboot" + ;; + *) + echo "nbd-disconnect should only be called by initscripts on reboot/shutdown." >&2 + exit 1 + ;; +esac + +disconnect() { + # Stop trapping + trap - 0 HUP INT QUIT KILL SEGV PIPE TERM + + # ltsp-client-core.upstart needs "console output" to show stderr + echo "nbd-disconnect executing: " >&2 + # Cache the command in order to use it after nbd-client disconnects + $command --version >/dev/null 2>&1 + nbd-client -d "$root" + $command + + # Hopefully this should never be reached + echo "$key" > /proc/sysrq-trigger +} + +# Disconnect swap nbd devices first +while read device etc; do + case "$device" in + /dev/nbd[0-9]) + swapoff "$device" + nbd-client -d "$device" + ;; + /dev/mapper/swap[0-9]) + nbd_device=$(cryptsetup status "$device" | awk '/device:/{print $2}') + swapoff "$device" + cryptsetup remove "$device" + case "$nbd_device" in + /dev/nbd[1-9]) + nbd-client -d "$nbd_device" + ;; + esac + ;; + esac +done < /proc/swaps + +# If we're not using an nbd root, exit +unset root +for param in $(cat /proc/cmdline); do + case "$param" in + root=/dev/nbd[0-9]) + root="${param#root=}" + ;; + esac +done +test -n "$root" || exit 0 + +trap "disconnect" 0 HUP INT QUIT KILL SEGV PIPE TERM +sync +# Give up to 5 seconds for other services to be called. +# If they finish before that time, process termination will start, and the trap +# will be called. +sleep 5 diff --git a/images/common-patches/usr/sbin/oc-add-extra-volumes b/images/common-patches/usr/sbin/oc-add-extra-volumes new file mode 100755 index 0000000..72571fd --- /dev/null +++ b/images/common-patches/usr/sbin/oc-add-extra-volumes @@ -0,0 +1,70 @@ +#!/bin/bash + +METADATA_CACHE=`mktemp -u` + +get_metadata() { + if [ ! -f $METADATA_CACHE ]; then + /usr/local/bin/oc-metadata > $METADATA_CACHE + fi +} + +get_value() { + # Get value from metadata + key="$1" + grep "^$key=" "$METADATA_CACHE" | cut -d= -f2 | sed "s/^['\"]//;s/['\"]$//" +} + +get_nbd_client_conf() { + keys=$(get_value VOLUMES) + +cat < raw (no other setup than to run the client) + cat < /etc/nbd-client +nbd_clients_connection +rm $METADATA_CACHE diff --git a/images/common-patches/usr/sbin/oc-sync-kernel-modules b/images/common-patches/usr/sbin/oc-sync-kernel-modules new file mode 100755 index 0000000..ca39e91 --- /dev/null +++ b/images/common-patches/usr/sbin/oc-sync-kernel-modules @@ -0,0 +1,29 @@ +#!/bin/sh +# description "synchronizes kernel module" +# author "Online Labs Cloud Team " + +DIR=/lib/modules +mkdir -p $DIR +TMP_DIR=`mktemp -d -p $DIR` +KVERSION=`uname -r` +TIMEOUT=10 + +clean() { + rm -rf "$TMP_DIR" 2>/dev/null +} +trap 'clean' INT TERM EXIT + +if [ ! -d $DIR/${KVERSION} ] +then + wget --timeout=${TIMEOUT} --quiet --no-check-certificate -r --no-parent \ + --reject "index.html*" --reject "robots.txt" \ + --no-host-directories --cut-dirs 3 --directory-prefix \ + $TMP_DIR http://mirror.cloud.online.net/kernel/${KVERSION}/modules/${KVERSION}/ + + if [ $? -eq 0 ] + then + mkdir -p $DIR/${KVERSION} + mv $TMP_DIR/${KVERSION} $DIR + fi +fi +