Make monitoring work with xen
authorNikos Skalkotos <skalkoto@grnet.gr>
Wed, 20 Feb 2013 17:49:08 +0000 (19:49 +0200)
committerNikos Skalkotos <skalkoto@grnet.gr>
Wed, 20 Feb 2013 17:49:08 +0000 (19:49 +0200)
* Add custom vif script for the helper VM
* Cleanup helper code

snf-image-helper/common.sh
snf-image-host/Makefile.am
snf-image-host/common.sh.in
snf-image-host/defaults.in
snf-image-host/helper-monitor.py
snf-image-host/vif-snf-image [new file with mode: 0755]
snf-image-host/xen-common.sh

index 0dd8c14..6a87fcd 100644 (file)
@@ -56,6 +56,22 @@ close_fd() {
     exec {fd}>&-
 }
 
+send_result_kvm() {
+    echo "$@" > /dev/ttyS1
+}
+
+send_monitor_message_kvm() {
+    echo "$@" > /dev/ttyS2
+}
+
+send_result_xen() {
+    xenstore-write /local/domain/0/snf-image-helper/$DOMID "$*"
+}
+
+send_monitor_message_xen() {
+    echo "$@" | socat STDIN INTERFACE:eth0
+}
+
 prepare_helper() {
        local cmdline item key val hypervisor domid
 
@@ -73,21 +89,15 @@ prepare_helper() {
 
     case "$hypervisor" in
     kvm)
-        exec {RESULT_FD}> /dev/ttyS1
-        add_cleanup close_fd ${RESULT_FD}
-        exec {MONITOR_FD}> /dev/ttyS2
-        add_cleanup close_fd ${MONITOR_FD}
+        HYPERVISOR=kvm
         ;;
     xen-hvm|xen-pvm)
-               $MOUNT -t xenfs xenfs /proc/xen
-               iptables -P OUTPUT DROP
-               ip6tables -P OUTPUT DROP
-               ip link set eth0 up
-        domid=$(xenstore-read domid)
-        exec {RESULT_FD}> >(xargs -l1 xenstore-write /local/domain/0/snf-image-helper/$domid)
-        add_cleanup close_fd ${RESULT_FD}
-        exec {MONITOR_FD}> >(socat STDIN INTERFACE:eth0)
-        add_cleanup close_fd ${MONITOR_FD}
+        $MOUNT -t xenfs xenfs /proc/xen
+        iptables -P OUTPUT DROP
+        ip link set eth0 arp off
+        ip link set eth0 up
+        export DOMID=$(xenstore-read domid)
+        HYPERVISOR=xen
         ;;
     *)
         echo "ERROR: Unknown hypervisor: \`$hypervisor'" >&2
@@ -95,29 +105,31 @@ prepare_helper() {
         ;;
     esac
 
-    export RESULT_FD MONITOR_FD
+    export HYPERVISOR
 }
 
 report_error() {
+    msg=""
     if [ ${#ERRORS[*]} -eq 0 ]; then
         # No error message. Print stderr
         local lines
         lines=$(tail --lines=${STDERR_LINE_SIZE} "$STDERR_FILE" | wc -l)
-        echo -n "STDERR:${lines}:" >&${MONITOR_FD}
-        tail --lines=$lines  "$STDERR_FILE" >&${MONITOR_FD}
+        msg="STDERR:${lines}"
+        msg+=$(tail --lines=$lines  "$STDERR_FILE")
     else
         for line in "${ERRORS[@]}"; do
-            echo "ERROR:$line" >&${MONITOR_FD}
+            msg+="ERROR:$line"$'\n'
         done
     fi
+
+    send_monitor_message_${HYPERVISOR} "$msg"
 }
 
 log_error() {
     ERRORS+=("$*")
-    echo "ERROR: $@" >&2
-    echo "ERROR: $@" >&${MONITOR_FD}
 
-    echo "ERROR" >&${RUSULT_FD}
+    send_monitor_message_${HYPERVISOR} "ERROR: $@"
+    send_result_${HYPERVISOR} "ERROR: $@"
 
     # Use return instead of exit. The set -x options will terminate the script
     # but will also trigger ERR traps if defined.
@@ -126,15 +138,15 @@ log_error() {
 
 warn() {
     echo "Warning: $@" >&2
-       echo "WARNING: $@" >&${MONITOR_FD}
+    send_monitor_message_${HYPERVISOR} "WARNING: $@"
 }
 
 report_task_start() {
-    echo "$MSG_TYPE_TASK_START:${PROGNAME:2}" >&${MONITOR_FD}
+    send_monitor_message_${HYPERVISOR} "$MSG_TYPE_TASK_START:${PROGNAME:2}"
 }
 
 report_task_end() {
-    echo "$MSG_TYPE_TASK_END:${PROGNAME:2}" >&${MONITOR_FD}
+    send_monitor_message_${HYPERVISOR} "$MSG_TYPE_TASK_END:${PROGNAME:2}"
 }
 
 system_poweroff() {
@@ -487,7 +499,7 @@ check_if_excluded() {
 
 
 return_success() {
-    echo SUCCESS >&${RESULT_FD}
+    send_result_${HYPERVISOR} "SUCCESS"
 }
 
 trap cleanup EXIT
index d9ed791..76155ad 100644 (file)
@@ -3,6 +3,7 @@ osdir=$(OS_DIR)/$(osname)
 defaultdir=$(DEFAULT_DIR)
 exampledir = $(datarootdir)/doc/$(PACKAGE)
 variantsdir=${sysconfdir}/ganeti/snf-image/variants
+xenscriptsdir=${sysconfdir}/xen/scripts
 
 dist_os_SCRIPTS = ${srcdir}/create ${srcdir}/import ${srcdir}/export \
        ${srcdir}/rename ${srcdir}/verify ${srcdir}/pithcat \
@@ -13,6 +14,8 @@ dist_os_SCRIPTS = ${srcdir}/create ${srcdir}/import ${srcdir}/export \
 dist_os_DATA = ${srcdir}/ganeti_api_version ${srcdir}/parameters.list \
                ${srcdir}/variants.list
 
+dist_xenscripts_SCRIPTS = ${srcdir}/vif-snf-image
+
 dist_bin_SCRIPTS = snf-image-update-helper
 
 os_DATA = common.sh
index 61f486b..d657c62 100644 (file)
@@ -376,7 +376,7 @@ fi
 : ${PITHOS_DATA:="@localstatedir@/lib/pithos/data/"}
 : ${PROGRESS_MONITOR:="@PROGRESS_MONITOR@"}
 : ${UNATTEND:="@UNATTEND@"}
-: ${XEN_BRIDGE:="xenbr"}
+: ${XEN_SCRIPTS_DIR="@sysconfdir@/xen/scripts"}
 : ${MULTISTRAP_CONFIG:="@MULTISTRAP_CONFIG@"}
 : ${MULTISTRAP_APTPREFDIR:="@MULTISTRAP_APTPREFDIR@"}
 
index 1700528..5c9c7dc 100644 (file)
@@ -46,6 +46,9 @@
 # files will be injected to the helper image before multistrap is called.
 # MULTISTRAP_APTPREFDIR="@MULTISTRAP_APTPREFDIR@"
 
+# XEN_SCRIPTS_DIR: Directory where the Xen scripts are stored
+# XEN_SCRIPTS_DIR=="@sysconfdir@/xen/scripts"
+
 # PITHOS_DB: Pithos database in SQLAlchemy format
 # PITHOS_DB="sqlite:////@localstatedir@/lib/pithos/backend.db"
 
index 08e3d28..2d8575a 100755 (executable)
@@ -23,20 +23,19 @@ import time
 import json
 import re
 
-# add HELPER_MONITOR_
-LINESIZE = 512+16
-BUFSIZE = 512+16
+LINESIZE = 512
+BUFSIZE = 512
 PROGNAME = os.path.basename(sys.argv[0])
 STDERR_MAXLINES = 10
 MAXLINES = 100
 MSG_TYPE = 'image-helper'
 
 PROTOCOL = {
-    'HELPER_MONITOR_TASK_START': ('task-start', 'task'),
-    'HELPER_MONITOR_TASK_END': ('task-end', 'task'),
-    'HELPER_MONITOR_WARNING': ('warning', 'messages'),
-    'HELPER_MONITOR_STDERR': ('error', 'stderr'),
-    'HELPER_MONITOR_ERROR': ('error', 'messages')}
+    'TASK_START': ('task-start', 'task'),
+    'TASK_END': ('task-end', 'task'),
+    'WARNING': ('warning', 'messages'),
+    'STDERR': ('error', 'stderr'),
+    'ERROR': ('error', 'messages')}
 
 
 def error(msg):
@@ -105,7 +104,7 @@ if __name__ == "__main__":
                 stderr += "%s\n" % line
                 lines_left -= 1
                 if lines_left == 0:
-                    send(fd, "HELPER_MONITROR_STDERR", stderr)
+                    send(fd, "STDERR", stderr)
                     stderr = ""
                 line = ""
                 continue
@@ -114,8 +113,8 @@ if __name__ == "__main__":
             if len(line) == 0:
                 continue
 
-            if line.startswith("HELPER_MONITOR_STDERR:"):
-                m = re.match("HELPER_MONITOR_STDERR:(\d+):(.*)", line)
+            if line.startswith("STDERR:"):
+                m = re.match("STDERR:(\d+):(.*)", line)
                 if not m:
                     error("Invalid syntax for STDERR line")
                 try:
@@ -133,16 +132,15 @@ if __name__ == "__main__":
                     lines_left -= 1
 
                 if lines_left == 0:
-                    send(fd, "HELPER_MONITOR_STDERR", stderr)
+                    send(fd, "STDERR", stderr)
                     stderr = ""
-            elif line.startswith("HELPER_MONITOR_TASK_START:") \
-                or line.startswith("HELPER_MONITOR_TASK_END:") \
-                or line.startswith("HELPER_MONITOR_WARNING:") \
-                or line.startswith("HELPER_MONITOR_ERROR:"):
+            elif line.startswith("TASK_START:") \
+                or line.startswith("TASK_END:") \
+                or line.startswith("WARNING:") \
+                or line.startswith("ERROR:"):
                 (msg_type, _, value) = line.partition(':')
 
-                if line.startswith("HELPER_MONITOR_WARNING:") \
-                  or line.startswith("HELPER_MONITOR_ERROR:"):
+                if line.startswith("WARNING:") or line.startswith("ERROR:"):
                     value = [value]
                 send(fd, msg_type, value)
             else:
diff --git a/snf-image-host/vif-snf-image b/snf-image-host/vif-snf-image
new file mode 100755 (executable)
index 0000000..b143da0
--- /dev/null
@@ -0,0 +1,12 @@
+#!/bin/bash
+
+dir=$(dirname "$0")
+. "$dir"/vif-common.sh
+
+echo 0 > /proc/sys/net/ipv6/conf/$vif/autoconf
+echo 1 > /proc/sys/net/ipv6/conf/$vif/disable_ipv6
+
+ip link set $vif up
+
+success
+
index 9f0ed88..aec5876 100644 (file)
@@ -2,24 +2,29 @@ get_img_dev() {
        echo /dev/xvdb
 }
 
+#create_mac() {
+#    # MAC address inside the range 00:16:3e:xx:xx:xx are reserved for Xen
+#    echo  "aa:$(cat /proc/interrupts | md5sum | sed -r 's/^(.{10}).*$/\1/; s/([0-9a-f]{2})/\1:/g; s/:$//;')"
+#}
+
 launch_helper() {
-       local name helperid rc blockdev floppy
+    local name helperid rc blockdev floppy host_mac helper_mac
 
-       blockdev="$1"
-       floppy="$2"
+    blockdev="$1"
+    floppy="$2"
 
        name=$(uuid)
 
-       report_info "Starting customization VM..."
-       echo "$($DATE +%Y:%m:%d-%H:%M:%S.%N) VM START" >&2
+    report_info "Starting customization VM..."
+    echo "$($DATE +%Y:%m:%d-%H:%M:%S.%N) VM START" >&2
 
     xm create /dev/null \
       kernel="$HELPER_DIR/kernel-xen" ramdisk="$HELPER_DIR/initrd-xen" \
          root="/dev/xvda1" memory="256" boot="c" vcpus=1 name="$name" \
       extra="console=hvc0 hypervisor=$HYPERVISOR snf_image_activate_helper \
-      rules_dev=/dev/xvdc quiet ro boot=local init=/usr/bin/snf-image-helper" \
+         ipv6.disable=1 rules_dev=/dev/xvdc ro boot=local init=/usr/bin/snf-image-helper" \
       disk="file:$HELPER_DIR/image,xvda,r" disk="phy:$blockdev,xvdb,w" \
-      disk="file:$floppy,xvdc,r" vif="mac=aa:00:00:00:00:11,bridge=$XEN_BRIDGE" \
+      disk="file:$floppy,xvdc,r" vif="script=${XEN_SCRIPTS_DIR}/vif-snf-image"
 
     if ! xenstore-exists snf-image-helper; then
         xenstore-write snf-image-helper ""
@@ -28,20 +33,18 @@ launch_helper() {
 
     helperid=$(xm domid "$name")
     xenstore-write snf-image-helper/${helperid} ""
-       add_cleanup xenstore-rm snf-image-helper/${helperid}
+    add_cleanup xenstore-rm snf-image-helper/${helperid}
     xenstore-chmod snf-image-helper/${helperid} r0 w${helperid}
 
-    brctl delif xenbr "vif${helperid}.0"
-
     socat EXEC:"./helper-monitor.py ${MONITOR_FD}" INTERFACE:vif${helperid}.0 &
 
-       set +e
+    set +e
 
     $TIMEOUT -k $HELPER_HARD_TIMEOUT $HELPER_SOFT_TIMEOUT \
       socat EXEC:"xm console $name",pty STDOUT | sed -u 's|^|HELPER: |g'
 
-       rc=$?
-       set -e
+    rc=$?
+    set -e
 
     echo "$($DATE +%Y:%m:%d-%H:%M:%S.%N) VM STOP" >&2
     if [ $rc -ne 0 ]; then
@@ -76,3 +79,4 @@ launch_helper() {
     fi
 }
 
+# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :