The cirros image was rebuilt against the 3.13.0-83 kernel, drivers e1000e, igbvf...
[packages/trusty/cirros-testvm.git] / cirros-testvm / src-cirros / src / lib / cirros / shlib_cirros
1 #!/bin/sh
2
3 assert_datasource() {
4         [ -n "${_DATASOURCE_NAME}" ] && return
5         [ -d "${RESULTS_D}" ] && [ -r "${RESULTS_D}/dsname" ] ||
6                 { debug 2 "no datasource found"; return 1; }
7
8         local dsname="" dsmode=""
9         { read dsname < "${RESULTS_D}/dsname"; } >/dev/null 2>&1 ||
10                 { debug 1 "failed to read dsname"; return 1; }
11
12         [ -n "$dsname" ] ||
13                 { debug 1 "empty datasource name"; return 1; }
14
15         { read dsmode < "${RESULTS_D}/dsmode"; } >/dev/null 2>&1 ||
16                 { debug 1 "failed to read dsmode"; return 1; }
17
18         _DATASOURCE_NAME="$dsname"
19         _DATASOURCE_MODE="$dsmode"
20         _RET="${dsname}"
21 }
22
23 ds_list_items() {
24         set +f;
25         local found=0 start="$PWD" items=""
26         cd "${RESULTS_D}/data" || return 1
27         for item in *; do
28                 [ -f "$item" ] || continue
29                 items="${items}${CR}${item}"
30                 found=$(($found+1))
31         done
32         items=${items#${CR}}
33         [ -n "$items" ] || debug 2 "empty items in $RESULTS_D/data"
34         set -f
35         cd "$start" || { debug 2 "failed to cd back to $start"; return 1; }
36         [ -n "$items" ] && _RET="$items"
37 }
38
39 ds_get_item_path() {
40         assert_datasource || return
41         local field="$1"
42         local fpath="${RESULTS_D}/data/$field"
43         if [ ! -f "$fpath" ]; then
44                 debug 2 "field $field not available for ds '$_DATASOURCE_NAME'";
45                 return 2;
46         fi
47         [ -r "$fpath" ] || { debug 2 "cannot read $field. need root?"; return 1; }
48         _RET="$fpath"
49         return
50 }
51
52 ds_has_item() {
53         [ -f "${RESULTS_D}/data/$1" ]
54 }
55
56 ds_get_item() {
57         ds_get_item_path "$1" || return
58         local fpath="$_RET"
59         # file exists and is readable due to above, if read fails, it is due to EOF.
60         read _RET < "$fpath" || :
61 }
62
63 ds_cat_item() {
64         ds_get_item_path "$1" && cat "$_RET"
65 }
66
67 cirros_version() {
68         [ -r /etc/cirros/version ] ||
69                 { _RET="unreleased"; return 0; }
70         { read _RET < /etc/cirros/version; } >/dev/null 2>&1
71 }
72
73 cirros_version_available() {
74         local na="0.0" af="$DATA_D/cirros_available"
75         local url="http://download.cirros-cloud.net/version/released"
76         [ -d "$DATA_D" ] || mkdir -p "$DATA_D"
77
78         [ "$1" = "-f" ] && { rm -f "$af"; shift; }
79         local block="${1:-0}"
80
81         if [ ! -f "$af" ]; then
82                 local max=$block
83                 [ "$max" = "0" ] && max=20
84
85                 local agent="cirros/$CIRROS_VERSION ($(uname -m))"
86                 local curlcmd="curl --location --silent --fail --max-time $max"
87
88                 echo "$na" > "$af"
89                 if [ "${block}" != "0" ]; then
90                         local out=""
91                         out=$($curlcmd --user-agent "$agent" "$url") && echo "$out" > "$af"
92                 else
93                         sh -c "out=\$($curlcmd --user-agent '$agent' '$url') &&
94                                 echo \"\${out}\" > '$af'" &
95                 fi
96         fi
97         { read _RET < "$af"; } >/dev/null 2>&1
98         [ -n "$_RET" ] || _RET="$na"
99         return
100 }
101
102 . ${CIRROS_SHLIB:=/lib/cirros/shlib} ||
103         { error "failed to read ${CIRROS_SHLIB}" 1>&2; exit 1; }
104
105 INTERNAL=false
106 VERBOSITY=1
107 CONFIG=/etc/cirros-init/config
108 DATA_D=/var/lib/cirros/data
109 STATE_D=/var/lib/cirros/sem
110 BOOT_STATE_D=/run/cirros/sem
111 DS_D=/lib/cirros/ds
112 RESULTS_D=/run/cirros/datasource
113
114 cirros_version
115 CIRROS_VERSION="$_RET"
116
117 [ ! -r "$CONFIG" ] || . "$CONFIG" ||
118         fail "failed to read $CONFIG"
119
120 # remove any trailing /
121 STATE_D=${STATE_D%/}
122 BOOT_STATE_D=${BOOT_STATE_D%/}
123 DS_D=${DS_D%/}
124 RESULTS_D=${RESULTS_D%/}
125
126 # vi: ts=4 noexpandtab syntax=sh