Revision fcae4e91 snf-image-helper/common.sh

b/snf-image-helper/common.sh
1
# Copyright (C) 2011 GRNET S.A. 
1
# Copyright (C) 2011, 2012, 2013 GRNET S.A.
2 2
# Copyright (C) 2007, 2008, 2009 Google Inc.
3 3
#
4 4
# This program is free software; you can redistribute it and/or modify
......
41 41

  
42 42
STDERR_LINE_SIZE=10
43 43

  
44
add_cleanup() {
45
    local cmd=""
46
    for arg; do cmd+=$(printf "%q " "$arg"); done
47
    CLEANUP+=("$cmd")
48
}
44 49

  
45
# hypervisor param is passed in /proc/cmdline
46
case "$hypervisor" in
47
  kvm)
48
    FLOPPY_DEV=/dev/fd0
49
    IMG_DEV=/dev/vda
50
    RESULT=/dev/ttyS1
51
    MONITOR=/dev/ttyS2
52
    ;;
53
  xen-hvm|xen-pvm)
54
    FLOPPY_DEV=/dev/xvdc1
55
    IMG_DEV=/dev/xvdb
56
    ;;
57
esac
58

  
59
to_monitor() {
60

  
61
    case $hypervisor in
62
      kvm)
63
          echo "HELPER_MONITOR_$@" > "$MONITOR"
64
          ;;
65
      xen-pvm|xen-hvm)
66
          echo  "HELPER_MONITOR_$@" | socat STDIO INTERFACE:eth0
67
          ;;
68
    esac
50
close_fd() {
51
    local fd=$1
52

  
53
    exec {fd}>&-
69 54
}
70 55

  
71
to_result() {
56
prepare_helper() {
57
	local cmdline item key val hypervisor domid
72 58

  
73
    case $hypervisor in
74
      kvm)
75
        echo "HELPER_RESULT_$@" > "$RESULT"
59
	read -a cmdline	 < /proc/cmdline
60
	for item in "${cmdline[@]}"; do
61
        key=$(cut -d= -f1 <<< "$item")
62
        if [ "$key" = "hypervisor" ]; then
63
            val=$(cut -d= -f2 <<< "$item")
64
            hypervisor="$val"
65
        fi
66
	done
67

  
68
    case "$hypervisor" in
69
    kvm)
70
        FLOPPY_DEV=/dev/fd0
71
        exec {RESULT_FD}> /dev/ttyS1
72
        add_cleanup close_fd ${RESULT_FD}
73
        exec {MONITOR_FD}> /dev/ttyS2
74
        add_cleanup close_fd ${MONITOR_FD}
76 75
        ;;
77
      xen-pvm|xen-hvm)
76
    xen-hvm|xen-pvm)
77
		mount -t xenfs xenfs /proc/xen
78
		iptables -P OUTPUT DROP
79
		ip6tables -P OUTPUT DROP
80
		ip link set eth0 up
81
        FLOPPY_DEV=/dev/xvdc
78 82
        domid=$(xenstore-read domid)
79
        xenstore-write /local/domain/0/helper/$domid "HELPER_RESULT_$@"
83
        exec {RESULT_FD}> >(xargs -l1 xenstore-write /local/domain/0/snf-image-helper/$domid)
84
        add_cleanup close_fd ${RESULT_FD}
85
        exec {MONITOR_FD}> >(socat STDIN INTERFACE:eth0)
86
        add_cleanup close_fd ${MONITOR_FD}
87
        ;;
88
    *)
89
        echo "ERROR: Unknown hypervisor: \`$hypervisor'" >&2
90
        exit 1
80 91
        ;;
81 92
    esac
82 93

  
83
}
84

  
85

  
86
add_cleanup() {
87
    local cmd=""
88
    for arg; do cmd+=$(printf "%q " "$arg"); done
89
    CLEANUP+=("$cmd")
94
    export RESULT_FD MONITOR_FD
90 95
}
91 96

  
92 97
log_error() {
93 98
    ERRORS+=("$@")
94 99
    echo "ERROR: $@" >&2
95
    to_result "ERROR: $@"
100
	echo "ERROR: $@" >&${MONITOR_FD}
96 101
    exit 1
97 102
}
98 103

  
99 104
warn() {
100 105
    echo "Warning: $@" >&2
101
    to_monitor "WARNING: $@"
106
	echo "WARNING: $@" >&${MONITOR_FD}
102 107
}
103 108

  
104 109
report_task_start() {
105
    to_monitor "$MSG_TYPE_TASK_START:${PROGNAME:2}"
110
    echo "$MSG_TYPE_TASK_START:${PROGNAME:2}" >&${MONITOR_FD}
106 111
}
107 112

  
108 113
report_task_end() {
109
    to_monitor "$MSG_TYPE_TASK_END:${PROGNAME:2}"
114
    echo "$MSG_TYPE_TASK_END:${PROGNAME:2}" >&${MONITOR_FD}
110 115
}
111 116

  
112 117
report_error() {
113 118
    if [ ${#ERRORS[*]} -eq 0 ]; then
114 119
        # No error message. Print stderr
115
	      lines="$(tail --lines=${STDERR_LINE_SIZE} "$STDERR_FILE")"
116
        cnt=$(echo $lines | wc -l)
117
        for line in lines; do
118
          to_monitor "$STDERR:$cnt: $line"
119
          let cnt--
120
        done
120
        local lines=$(tail --lines=${STDERR_LINE_SIZE} "$STDERR_FILE" | wc -l)
121
        echo -n "STDERR:${lines}:" >&${MONITOR_FD}
122
        tail --lines=$lines  "$STDERR_FILE" >&${MONITOR_FD}
121 123
    else
122 124
        for line in "${ERRORS[@]}"; do
123
            to_monitor "ERROR: $line"
125
            echo "ERROR:$line" >&${MONITOR_FD}
124 126
        done
125 127
    fi
126 128
}
......
432 434

  
433 435

  
434 436
return_success() {
435

  
436
    to_result SUCCESS
437

  
437
    echo SUCCESS >&${RESULT_FD}
438 438
}
439 439

  
440 440
trap cleanup EXIT

Also available in: Unified diff