4 [ -n "${_DATASOURCE_NAME}" ] && return
5 [ -d "${RESULTS_D}" ] && [ -r "${RESULTS_D}/dsname" ] ||
6 { debug 2 "no datasource found"; return 1; }
8 local dsname="" dsmode=""
9 { read dsname < "${RESULTS_D}/dsname"; } >/dev/null 2>&1 ||
10 { debug 1 "failed to read dsname"; return 1; }
13 { debug 1 "empty datasource name"; return 1; }
15 { read dsmode < "${RESULTS_D}/dsmode"; } >/dev/null 2>&1 ||
16 { debug 1 "failed to read dsmode"; return 1; }
18 _DATASOURCE_NAME="$dsname"
19 _DATASOURCE_MODE="$dsmode"
25 local found=0 start="$PWD" items=""
26 cd "${RESULTS_D}/data" || return 1
28 [ -f "$item" ] || continue
29 items="${items}${CR}${item}"
33 [ -n "$items" ] || debug 2 "empty items in $RESULTS_D/data"
35 cd "$start" || { debug 2 "failed to cd back to $start"; return 1; }
36 [ -n "$items" ] && _RET="$items"
40 assert_datasource || return
42 local fpath="${RESULTS_D}/data/$field"
43 if [ ! -f "$fpath" ]; then
44 debug 2 "field $field not available for ds '$_DATASOURCE_NAME'";
47 [ -r "$fpath" ] || { debug 2 "cannot read $field. need root?"; return 1; }
53 [ -f "${RESULTS_D}/data/$1" ]
57 ds_get_item_path "$1" || return
59 # file exists and is readable due to above, if read fails, it is due to EOF.
60 read _RET < "$fpath" || :
64 ds_get_item_path "$1" && cat "$_RET"
68 [ -r /etc/cirros/version ] ||
69 { _RET="unreleased"; return 0; }
70 { read _RET < /etc/cirros/version; } >/dev/null 2>&1
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"
78 [ "$1" = "-f" ] && { rm -f "$af"; shift; }
81 if [ ! -f "$af" ]; then
83 [ "$max" = "0" ] && max=20
85 local agent="cirros/$CIRROS_VERSION ($(uname -m))"
86 local curlcmd="curl --location --silent --fail --max-time $max"
89 if [ "${block}" != "0" ]; then
91 out=$($curlcmd --user-agent "$agent" "$url") && echo "$out" > "$af"
93 sh -c "out=\$($curlcmd --user-agent '$agent' '$url') &&
94 echo \"\${out}\" > '$af'" &
97 { read _RET < "$af"; } >/dev/null 2>&1
98 [ -n "$_RET" ] || _RET="$na"
102 . ${CIRROS_SHLIB:=/lib/cirros/shlib} ||
103 { error "failed to read ${CIRROS_SHLIB}" 1>&2; exit 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
112 RESULTS_D=/run/cirros/datasource
115 CIRROS_VERSION="$_RET"
117 [ ! -r "$CONFIG" ] || . "$CONFIG" ||
118 fail "failed to read $CONFIG"
120 # remove any trailing /
122 BOOT_STATE_D=${BOOT_STATE_D%/}
124 RESULTS_D=${RESULTS_D%/}
126 # vi: ts=4 noexpandtab syntax=sh