Browse code

xenapi: /boot/guest should point to local SR

Fixes bug 1037516

This patch creates a directory os-guest-kernels inside the local SR, and
sets up /boot/guest to be a symlink to that directory. This way
OpenStack won't pollute Dom0's filesystem.

Change-Id: If8dfe24355bd782a401fed0f2c4b423efd9c11ba

Mate Lakat authored on 2013/03/29 00:02:27
Showing 3 changed files
... ...
@@ -1,10 +1,8 @@
1 1
 #!/bin/bash
2 2
 
3 3
 function xapi_plugin_location {
4
-    for PLUGIN_DIR in "/etc/xapi.d/plugins/" "/usr/lib/xcp/plugins/"
5
-    do
6
-        if [ -d $PLUGIN_DIR ]
7
-        then
4
+    for PLUGIN_DIR in "/etc/xapi.d/plugins/" "/usr/lib/xcp/plugins/"; do
5
+        if [ -d $PLUGIN_DIR ]; then
8 6
             echo $PLUGIN_DIR
9 7
             return 0
10 8
         fi
... ...
@@ -17,7 +15,13 @@ function zip_snapshot_location {
17 17
 }
18 18
 
19 19
 function create_directory_for_kernels {
20
-    mkdir -p "/boot/guest"
20
+    if [ -d "/boot/guest" ]; then
21
+        echo "INFO: /boot/guest directory already exists, using that" >&2
22
+    else
23
+        local LOCALPATH="$(get_local_sr_path)/os-guest-kernels"
24
+        mkdir -p $LOCALPATH
25
+        ln -s $LOCALPATH /boot/guest
26
+    fi
21 27
 }
22 28
 
23 29
 function extract_remote_zipball {
... ...
@@ -53,3 +57,11 @@ function install_xapi_plugins_from_zipball {
53 53
     rm -rf $EXTRACTED_FILES
54 54
     chmod a+x ${XAPI_PLUGIN_DIR}*
55 55
 }
56
+
57
+function get_local_sr {
58
+    xe sr-list name-label="Local storage" --minimal
59
+}
60
+
61
+function get_local_sr_path {
62
+    echo "/var/run/sr-mount/$(get_local_sr)"
63
+}
... ...
@@ -12,6 +12,18 @@ test ! -e "$LIST_OF_DIRECTORIES" && {
12 12
     exit 1
13 13
 }
14 14
 
15
+test ! -e "$XE_RESPONSE" && {
16
+    echo "Mocking is not set up properly."
17
+    echo "XE_RESPONSE should point to an existing file."
18
+    exit 1
19
+}
20
+
21
+test ! -e "$XE_CALLS" && {
22
+    echo "Mocking is not set up properly."
23
+    echo "XE_CALLS should point to an existing file."
24
+    exit 1
25
+}
26
+
15 27
 function mktemp {
16 28
     if test "${1:-}" = "-d";
17 29
     then
... ...
@@ -41,6 +53,10 @@ function rm {
41 41
     echo "rm $@" >> $LIST_OF_ACTIONS
42 42
 }
43 43
 
44
+function ln {
45
+    echo "ln $@" >> $LIST_OF_ACTIONS
46
+}
47
+
44 48
 function [ {
45 49
     if test "${1:-}" = "-d";
46 50
     then
... ...
@@ -57,3 +73,13 @@ function [ {
57 57
     echo "Mock test does not implement the requested function"
58 58
     exit 1
59 59
 }
60
+
61
+function xe {
62
+    cat $XE_RESPONSE
63
+    {
64
+    for i in $(seq "$#")
65
+    do
66
+        eval "echo \"\$$i\""
67
+    done
68
+    } >> $XE_CALLS
69
+}
... ...
@@ -23,15 +23,27 @@ function before_each_test {
23 23
 
24 24
     LIST_OF_ACTIONS=$(mktemp)
25 25
     truncate -s 0 $LIST_OF_ACTIONS
26
+
27
+    XE_RESPONSE=$(mktemp)
28
+    truncate -s 0 $XE_RESPONSE
29
+
30
+    XE_CALLS=$(mktemp)
31
+    truncate -s 0 $XE_CALLS
26 32
 }
27 33
 
28 34
 # Teardown
29 35
 function after_each_test {
30 36
     rm -f $LIST_OF_DIRECTORIES
31 37
     rm -f $LIST_OF_ACTIONS
38
+    rm -f $XE_RESPONSE
39
+    rm -f $XE_CALLS
32 40
 }
33 41
 
34 42
 # Helpers
43
+function setup_xe_response {
44
+    echo "$1" > $XE_RESPONSE
45
+}
46
+
35 47
 function given_directory_exists {
36 48
     echo "$1" >> $LIST_OF_DIRECTORIES
37 49
 }
... ...
@@ -44,6 +56,30 @@ function assert_previous_command_failed {
44 44
     [ "$?" != "0" ] || exit 1
45 45
 }
46 46
 
47
+function assert_xe_min {
48
+    grep -qe "^--minimal\$" $XE_CALLS
49
+}
50
+
51
+function assert_xe_param {
52
+    grep -qe "^$1\$" $XE_CALLS
53
+}
54
+
55
+function mock_out {
56
+    local FNNAME="$1"
57
+    local OUTPUT="$2"
58
+
59
+    . <(cat << EOF
60
+function $FNNAME {
61
+    echo "$OUTPUT"
62
+}
63
+EOF
64
+)
65
+}
66
+
67
+function assert_symlink {
68
+    grep -qe "^ln -s $2 $1\$" $LIST_OF_ACTIONS
69
+}
70
+
47 71
 # Tests
48 72
 function test_plugin_directory_on_xenserver {
49 73
     given_directory_exists "/etc/xapi.d/plugins/"
... ...
@@ -80,9 +116,26 @@ function test_zip_snapshot_location {
80 80
 }
81 81
 
82 82
 function test_create_directory_for_kernels {
83
-    (. mocks && create_directory_for_kernels)
83
+    (
84
+        . mocks
85
+        mock_out get_local_sr uuid1
86
+        create_directory_for_kernels
87
+    )
88
+
89
+    assert_directory_exists "/var/run/sr-mount/uuid1/os-guest-kernels"
90
+    assert_symlink "/boot/guest" "/var/run/sr-mount/uuid1/os-guest-kernels"
91
+}
92
+
93
+function test_create_directory_for_kernels_existing_dir {
94
+    (
95
+        . mocks
96
+        given_directory_exists "/boot/guest"
97
+        create_directory_for_kernels
98
+    )
84 99
 
85
-    assert_directory_exists "/boot/guest"
100
+    diff -u $LIST_OF_ACTIONS - << EOF
101
+[ -d /boot/guest ]
102
+EOF
86 103
 }
87 104
 
88 105
 function test_extract_remote_zipball {
... ...
@@ -107,6 +160,23 @@ function test_find_nova_plugins {
107 107
     rm -rf $tmpdir
108 108
 }
109 109
 
110
+function test_get_local_sr {
111
+    setup_xe_response "uuid123"
112
+
113
+    local RESULT=$(. mocks && get_local_sr)
114
+
115
+    [ "$RESULT" == "uuid123" ]
116
+
117
+    assert_xe_min
118
+    assert_xe_param "sr-list" "name-label=Local storage"
119
+}
120
+
121
+function test_get_local_sr_path {
122
+    local RESULT=$(mock_out get_local_sr "uuid1" && get_local_sr_path)
123
+
124
+    [ "/var/run/sr-mount/uuid1" == "$RESULT" ]
125
+}
126
+
110 127
 # Test runner
111 128
 [ "$1" = "" ] && {
112 129
     grep -e "^function *test_" $0 | cut -d" " -f2