532e505f29e67230e0f61e548900eb68ab1ae7ef
[packages/trusty/cirros-testvm.git] / cirros-testvm / src-cirros / src / lib / cirros / shlib
1 #!/bin/sh
2 set -f
3
4 CR="
5 "
6
7 error() { echo "$@" 1>&2; }
8 fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
9
10 bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || error "$@"; return 1; }
11 read_uptime() {
12         local idle
13         { read _RET idle </proc/uptime; } >/dev/null 2>&1 || _RET=""
14 }
15
16 time_call() {
17         local start="$1" end="$2" ret="" delta="" t=""
18         read_uptime
19         start="${_RET%.*}${_RET#*.}"
20         "$@"
21         ret=$?
22         read_uptime
23         end="${_RET%.*}${_RET#*.}"
24         delta=$(($end-$start))
25         case $delta in
26                 ?)  _RET_TIME=0.0$delta;;
27                 ??) _RET_TIME=0.$delta;;
28                 *)
29                         t=${delta%??}
30                         _RET_TIME="$t.${delta#${t}}"
31                         ;;
32         esac
33         return $ret
34 }
35
36
37 debug() {
38         local level=${1}; shift;
39         [ "${level}" -gt "${VERBOSITY:-0}" ] && return
40         error "${@}"
41 }
42
43 net_get_gateway() {
44         _RET=$(route -n | awk '$1 == "0.0.0.0" && $2 != "0.0.0.0" { print $2 }')
45 }
46 net_get_nameservers() {
47         local gw nslist
48         local t1 t2 t3 nslist="" ns=""
49         while read t1 t2 t3; do
50                 case "$t1" in
51                         nameserver) nslist="${nslist} ${t2}";;
52                 esac
53         done < /etc/resolv.conf
54         _RET="$nslist"
55 }
56
57 check_ping_gateway() {
58         local gw="$1"
59         [ -z "$gw" ] && net_get_gateway && gw=$_RET
60         [ -n "$gw" ] || { debug 2 "No gateway found"; return 1; }
61         ping -c 1 $gw -W 3 >/dev/null 2>&1
62 }
63
64 debug_connection() {
65         local gw=""
66
67         echo "############ debug start ##############"
68         echo "### /etc/init.d/sshd start"
69         /etc/init.d/sshd start
70         net_get_gateway && gw=$_RET
71         echo "### ifconfig -a"
72         ifconfig -a
73         echo "### route -n"
74         route -n
75         echo "### cat /etc/resolv.conf"
76         cat /etc/resolv.conf
77         if [ -n "${gw}" ]; then
78                 echo "### ping -c 5 ${gw}"
79                 ping -c 5 ${gw}
80         else
81                 echo "### gateway not found"
82         fi
83         local t1 t2 t3 nslist="" ns=""
84         while read t1 t2 t3; do
85                 case "$t1" in
86                         nameserver) nslist="${nslist} ${t2}";;
87                 esac
88         done < /etc/resolv.conf
89         echo "### pinging nameservers"
90         for ns in ${nslist}; do
91                 echo "#### ping -c 5 ${ns}"
92                 ping -c 5 ${ns}
93         done
94         echo "### uname -a"
95         uname -a
96         lxc-is-container || { echo "### lsmod"; lsmod; }
97         echo "### dmesg | tail"
98         dmesg | tail
99         echo "### tail -n 25 /var/log/messages"
100         tail -n 25 /var/log/messages
101         echo "############ debug end   ##############"
102 }
103
104 mount_callback_umount() {
105         # dev, opts, callback, args
106         local dev="$1" opts="$2" callback="$3" ret=""
107         local tmpd=$(mktemp -d "${TMPDIR:-/tmp}/${0##*/}.mp.XXXXXX")
108         mount "$dev" $opts "$tmpd" || {
109                 ret=$?;
110                 debug 2 "failed mount $dev";
111                 rmdir "$tmpd"
112                 return $ret;
113         }
114         shift 3;
115         "$callback" "$tmpd" "$@"
116         ret=$?
117         umount "$tmpd" || {
118                 ret=$?;
119                 debug 1 "failed umount $dev";
120                 return $ret;
121         }
122         rmdir "$tmpd"
123         return 0
124 }
125
126 find_devs_with() {
127         # return a list of devices that match filter
128         # where filter is like:
129     #  TYPE=<filesystem>
130     #  LABEL=<label>
131     #  UUID=<uuid>
132         local filter="$1"
133         local out rc ret=""
134         out=$(blkid "-t$filter" "-odevice" 2>/dev/null)
135         rc=$?
136         if [ $rc -eq 0 ]; then
137                 local item=""
138                 for item in ${out}; do
139                         ret="${ret} ${item}"
140                 done
141                 _RET=${ret# }
142                 return 0
143         elif [ $rc -eq 2 ]; then
144                 # 2 is "no matching devices"
145                 _RET=""
146                 return 0
147         fi
148         return $rc
149 }
150
151 ipinfo() {
152         # return a list of ip info
153         # each line contain: ifname,up/down,ipv4,ipv4mask
154         local cur_if="" cur_up=0 cur_inargs=0 cur_good=0 cur_ipv4="" cur_ipv6="" cur_nm
155         local data="" dline=""
156         while read line; do
157                 case "$line" in
158                         [0-9]:*|[0-9][0-9]:*|[0-9][0-9][0-9]:*)
159                                 if [ -n "$cur_if" ]; then
160                                         dline="${cur_if},${cur_up},${cur_ipv4},${cur_nm},${cur_ipv6}"
161                                         data="${data}|${dline}"
162                                 fi
163                                 set -- ${line}
164                                 cur_if=${2%:}
165                                 cur_up=down
166                                 cur_ipv4=""
167                                 cur_ipv6=""
168                                 case "$3" in
169                                         *,UP,*|*,UP\>|\<UP,*) cur_up=up;;
170                                 esac
171                 esac
172                 case "$line" in
173                         *inet\ *)
174                                 set -- $line
175                                 [ "${2#*/}" != "$2" ] && {
176                                         cur_ipv4="${2%/*}";
177                                         cur_nm="${2#*/}";
178                                 }
179                                 ;;
180                         *inet6\ *) :
181                                 # don't know how to do this
182                                 set -- $line
183                                 [ "${2#*/}" != "$2" ] && cur_ipv6="${2%/*}";;
184                 esac
185         done
186         if [ -n "$cur_if" ]; then
187                 data="${data}|${cur_if},${cur_up},${cur_ipv4},${cur_nm},${cur_ipv6}"
188         fi
189         _RET="${data#|}"
190         return 0
191 }
192
193 # upon return, the following globals are set (KC=Kernel Commandline)
194 # KC_CONSOLES : full path (including /dev) to all console= entries that exist
195 # KC_CONSOLE : the last entry on the kernel command line
196 # KC_PREF_CONSOLE : the last existing entry on kernel command line
197 # KC_ROOT : root= variable
198 # KC_DEBUG_INITRAMFS : set to '1' if debug-initramfs is on commandline
199 # KC_VERBOSE: set to '1' if 'verbose' on commandline
200 # KC_RAMDISK_ROOT: set to 1 if cmdline said not to mount a root (0 otherwise)
201 # KC_INIT: init= variable
202 parse_cmdline() {
203         # expects that /dev is mounted/populated
204         KC_VERBOSE=0
205         KC_INIT="/sbin/init"
206         KC_RAMDISK_ROOT=0
207         local cmdline="$1" tok="" val="" key="" consoles=""
208         local last_con="" pref_con=""
209         is_lxc && return 0
210         KC_CONSOLE=/dev/tty0
211         set -f
212         [ $# -eq 0 ] && read cmdline < /proc/cmdline
213         for tok in $cmdline; do
214                 val=${tok#*=}
215                 key=${tok%%=*}
216                 case $key in
217                         console)
218                                 val="/dev/${val#/dev}"
219                                 last_con="$val"
220                                 [ -e "$val" ] && { echo "" > "$val"; } 2>/dev/null || continue
221                                 consoles="$consoles $val"
222                                 pref_con="$val"
223                                 KC_CONSOLE="$val"
224                                 ;;
225                         rdroot) KC_RAMDISK_ROOT=1;;
226                         root) KC_ROOT="$val";;
227                         init) KC_INIT="$val";;
228                         debug-initramfs) KC_DEBUG_INITRAMFS=1;;
229                         verbose) KC_VERBOSE=1;;
230                 esac
231         done
232
233         case "$KC_ROOT" in
234                 [Nn][Oo][Nn][Ee]|ramdisk) KC_RAMDISK_ROOT=1;;
235         esac
236
237         # set KC_PREF_CONSOLE to the last writable console
238         # if that is the same as the last
239         [ "$pref_con" = "$last_con" ] && pref_con=""
240         KC_PREF_CONSOLE="$pref_con"
241         KC_CONSOLES="${consoles# }"
242
243         set +f
244         return
245 }
246
247 idebug() {
248         [ "$KC_VERBOSE" = "0" ] && return
249         echo "debug:" "$@"
250 }
251 iinfo() {
252         [ "$KC_QUIET" = "1" ] && return
253         echo "info:" "$@"
254 }
255 failure() {
256         echo "FATAL: ====" "$@" "===="
257         echo "Executing /bin/sh. maybe you can help"
258         exec /bin/sh
259 }
260
261 is_mounted() {
262         awk '($3 == fs || fs == "") && ( $2 == mp || mp == "") && \
263                 ( $1 == dev || dev == "" ) { e=0; };  END { exit(e); }' \
264                 e=1 "fs=$1" "dev=$2" "mp=$3" /proc/self/mounts
265 }
266
267 mount_once() {
268         local fs="$1" dev="$2" mp="$3"
269         is_mounted "$fs" "$dev" "$mp" && return
270         shift 3
271         mount -t "$fs" "$dev" "$mp" "$@"
272 }
273
274 is_lxc() {
275         local container=""
276
277         if [ -n "$_LXC_CONTAINER" ]; then
278                 [ "${_LXC_CONTAINER}" != "none" ]
279                 return
280         fi
281
282         [ "$0" = "/init" ] && return 1 # lxc wont use /init (not a ramdisk)
283
284         if [ -f /proc/1/environ ]; then
285                 local x=""
286                 x=$(awk -F'\0' '{
287                         for(i=1;i<=NF;i++) {
288                                 if ($i ~ /container=./ || $i ~ /LIBVIRT_LXC_UUID=./)
289                                         found=$i;
290                         }
291             }
292                         END { sub(/container=/,"", found); printf("%s\n",found); }' \
293                         /proc/1/environ)
294                 [ -n "$x" ] && container="$x"
295         fi
296
297         # Detect OpenVZ containers
298         if [ -z "$container" ]; then
299                 [ -d /proc/vz ] && [ ! -d /proc/bc ] && container=openvz
300         fi
301
302         # Detect Vserver containers
303         if [ -z "$container" -a -f /proc/self/status ]; then
304                 while read k v; do
305                         [ "$k" = "VxID:" ] && [ ${v:-0} -gt 1 ] &&
306                                 container=vserver && break;
307                 done < /proc/self/status >/dev/null 2>&1
308         fi
309
310         _LXC_CONTAINER="${container:-none}"
311         [ -n "$container" ]
312         return
313 }
314 is_not_lxc() { ! is_lxc; }
315
316 iamroot() {
317         [ -n "$UID" ] || UID=$(id -u) || error "unable to determine UID!"
318         [ "$UID" = "0" ]
319 }
320 asroot() {
321         local sudo=""
322         iamroot || sudo="sudo"
323         $sudo "$@"
324 }
325
326 path_has() {
327         # path_has(tok, [string=$PATH], [delim=:])
328         local tok="${1%/}" del="${3:-:}" p="${2:-${PATH}}"
329         p="${del}$p${del}"
330         [ "${p#*${del}${tok}${del}}" != "$p" ] ||
331                 [ "${p#*${del}${tok}/${del}}" != "$p" ]
332 }
333
334 # vi: ts=4 noexpandtab syntax=sh