7 error() { echo "$@" 1>&2; }
8 fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
10 bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || error "$@"; return 1; }
13 { read _RET idle </proc/uptime; } >/dev/null 2>&1 || _RET=""
17 local start="$1" end="$2" ret="" delta="" t=""
19 start="${_RET%.*}${_RET#*.}"
23 end="${_RET%.*}${_RET#*.}"
24 delta=$(($end-$start))
26 ?) _RET_TIME=0.0$delta;;
27 ??) _RET_TIME=0.$delta;;
30 _RET_TIME="$t.${delta#${t}}"
38 local level=${1}; shift;
39 [ "${level}" -gt "${VERBOSITY:-0}" ] && return
44 _RET=$(route -n | awk '$1 == "0.0.0.0" && $2 != "0.0.0.0" { print $2 }')
46 net_get_nameservers() {
48 local t1 t2 t3 nslist="" ns=""
49 while read t1 t2 t3; do
51 nameserver) nslist="${nslist} ${t2}";;
53 done < /etc/resolv.conf
57 check_ping_gateway() {
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
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"
75 echo "### cat /etc/resolv.conf"
77 if [ -n "${gw}" ]; then
78 echo "### ping -c 5 ${gw}"
81 echo "### gateway not found"
83 local t1 t2 t3 nslist="" ns=""
84 while read t1 t2 t3; do
86 nameserver) nslist="${nslist} ${t2}";;
88 done < /etc/resolv.conf
89 echo "### pinging nameservers"
90 for ns in ${nslist}; do
91 echo "#### ping -c 5 ${ns}"
96 lxc-is-container || { echo "### lsmod"; lsmod; }
97 echo "### dmesg | tail"
99 echo "### tail -n 25 /var/log/messages"
100 tail -n 25 /var/log/messages
101 echo "############ debug end ##############"
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" || {
110 debug 2 "failed mount $dev";
115 "$callback" "$tmpd" "$@"
119 debug 1 "failed umount $dev";
127 # return a list of devices that match filter
128 # where filter is like:
134 out=$(blkid "-t$filter" "-odevice" 2>/dev/null)
136 if [ $rc -eq 0 ]; then
138 for item in ${out}; do
143 elif [ $rc -eq 2 ]; then
144 # 2 is "no matching devices"
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=""
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}"
169 *,UP,*|*,UP\>|\<UP,*) cur_up=up;;
175 [ "${2#*/}" != "$2" ] && {
181 # don't know how to do this
183 [ "${2#*/}" != "$2" ] && cur_ipv6="${2%/*}";;
186 if [ -n "$cur_if" ]; then
187 data="${data}|${cur_if},${cur_up},${cur_ipv4},${cur_nm},${cur_ipv6}"
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
203 # expects that /dev is mounted/populated
207 local cmdline="$1" tok="" val="" key="" consoles=""
208 local last_con="" pref_con=""
212 [ $# -eq 0 ] && read cmdline < /proc/cmdline
213 for tok in $cmdline; do
218 val="/dev/${val#/dev}"
220 [ -e "$val" ] && { echo "" > "$val"; } 2>/dev/null || continue
221 consoles="$consoles $val"
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;;
234 [Nn][Oo][Nn][Ee]|ramdisk) KC_RAMDISK_ROOT=1;;
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# }"
248 [ "$KC_VERBOSE" = "0" ] && return
252 [ "$KC_QUIET" = "1" ] && return
256 echo "FATAL: ====" "$@" "===="
257 echo "Executing /bin/sh. maybe you can help"
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
268 local fs="$1" dev="$2" mp="$3"
269 is_mounted "$fs" "$dev" "$mp" && return
271 mount -t "$fs" "$dev" "$mp" "$@"
277 if [ -n "$_LXC_CONTAINER" ]; then
278 [ "${_LXC_CONTAINER}" != "none" ]
282 [ "$0" = "/init" ] && return 1 # lxc wont use /init (not a ramdisk)
284 if [ -f /proc/1/environ ]; then
288 if ($i ~ /container=./ || $i ~ /LIBVIRT_LXC_UUID=./)
292 END { sub(/container=/,"", found); printf("%s\n",found); }' \
294 [ -n "$x" ] && container="$x"
297 # Detect OpenVZ containers
298 if [ -z "$container" ]; then
299 [ -d /proc/vz ] && [ ! -d /proc/bc ] && container=openvz
302 # Detect Vserver containers
303 if [ -z "$container" -a -f /proc/self/status ]; then
305 [ "$k" = "VxID:" ] && [ ${v:-0} -gt 1 ] &&
306 container=vserver && break;
307 done < /proc/self/status >/dev/null 2>&1
310 _LXC_CONTAINER="${container:-none}"
314 is_not_lxc() { ! is_lxc; }
317 [ -n "$UID" ] || UID=$(id -u) || error "unable to determine UID!"
322 iamroot || sudo="sudo"
327 # path_has(tok, [string=$PATH], [delim=:])
328 local tok="${1%/}" del="${3:-:}" p="${2:-${PATH}}"
330 [ "${p#*${del}${tok}${del}}" != "$p" ] ||
331 [ "${p#*${del}${tok}/${del}}" != "$p" ]
334 # vi: ts=4 noexpandtab syntax=sh