Cleanup Xen code
[snf-image] / snf-image-host / xen-common.sh
1 get_img_dev() {
2         echo /dev/xvdb
3 }
4
5 launch_helper() {
6         local name helperid rc blockdev floppy
7
8         blockdev="$1"
9         floppy="$2"
10
11         name=$(uuid)
12     xm create /dev/null \
13       kernel="$HELPER_KERNEL" ramdisk="$HELPER_INITRD"  root="/dev/xvda1" \
14       extra="console=hvc0 hypervisor=$HYPERVISOR snf_image_activate_helper quiet ro boot=local" \
15       disk="file:${HELPER_IMG},xvda,w" disk="phy:$blockdev,xvdb,w" \
16       disk="file:$floppy,xvdc,r" vif="mac=aa:00:00:00:00:11,bridge=$XEN_BRIDGE" \
17       memory="256" boot="c" vcpus=1 name="$name"
18
19     if ! xenstore-exists snf-image-helper; then
20         xenstore-write snf-image-helper ""
21                 #add_cleanup xenstore-rm snf-image-helper
22     fi
23
24     helperid=$(xm domid "$name")
25     xenstore-write snf-image-helper/${helperid} ""
26         add_cleanup xenstore-rm snf-image-helper/${helperid}
27     xenstore-chmod snf-image-helper/${helperid} r0 w${helperid}
28
29     brctl delif xenbr "vif${helperid}.0"
30
31     socat EXEC:"./helper-monitor.py ${MONITOR_FD}" INTERFACE:vif${helperid}.0 &
32
33         set +e
34
35     $TIMEOUT -k $HELPER_HARD_TIMEOUT $HELPER_SOFT_TIMEOUT \
36       socat EXEC:"xm console $name",pty STDOUT | sed -u 's|^|HELPER: |g'
37
38         rc=$?
39         set -e
40
41     if [ $rc -ne 0 ]; then
42         if [ $rc -eq 124 ];  then
43             log_error "Image customization was terminated. Did not finish on time."
44         elif [ $rc -eq 137 ]; then # (128 + SIGKILL)
45             log_error "Image customization was killed. Did not finish on time."
46         elif [ $rc -eq 141 ]; then # (128 + SIGPIPE)
47             log_error "Image customization was terminated by a SIGPIPE."
48             log_error "Maybe progress monitor has died unexpectedly."
49         elif [ $rc -eq 125 ]; then
50             log_error "Internal Error. Image customization could not start."
51             log_error "timeout did not manage to run."
52         else
53             log_error "Image customization died unexpectedly (return code $rc)."
54         fi
55         exit 1
56     else
57         echo -n "$(date +%Y:%m:%d-%H:%M:%S.%N)" >&2
58         log_info "Customization VM finished."
59     fi
60
61     result=$(xenstore-read snf-image-helper/$helperid)
62
63     if [ "x$result" != "xSUCCESS" ]; then
64         log_error "Image customization failed."
65         exit 1
66     fi
67
68 }
69