common.sh: output all errors to stderr
[ext/instance-debootstrap.git] / common.sh
1
2 log_error() {
3   echo "$@" >&2
4 }
5
6 SCRIPT_NAME=$(basename $0)
7
8 for dir in /lib/udev /sbin; do
9   if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
10     VOL_ID=$dir/vol_id
11   fi
12 done
13
14 if [ -z "$VOL_ID" ]; then
15     log_error "vol_id not found, please install udev"
16     exit 1
17 fi
18
19 get_api5_arguments() {
20   TEMP=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
21   if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
22   # Note the quotes around `$TEMP': they are essential!
23   eval set -- "$TEMP"
24   while true; do
25     case "$1" in
26       -i|-n) instance=$2; shift 2;;
27
28       -o) old_name=$2; shift 2;;
29
30       -b) blockdev=$2; shift 2;;
31
32       -s) swapdev=$2; shift 2;;
33
34       --) shift; break;;
35
36       *)  log_error "Internal error!" >&2; exit 1;;
37     esac
38   done
39   if [ -z "$instance" -o -z "$blockdev" ]; then
40     log_error "Missing OS API Argument (-i, -n, or -b)"
41     exit 1
42   fi
43   if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev"  ]; then
44     log_error "Missing OS API Argument -s (swapdev)"
45     exit 1
46   fi
47   if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name"  ]; then
48     log_error "Missing OS API Argument -o (old_name)"
49     exit 1
50   fi
51 }
52
53 get_api10_arguments() {
54   if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then
55     log_error "Missing OS API Variable:"
56     log_error "(INSTANCE_NAME HYPERVISOR or DISK_COUNT)"
57     exit 1
58   fi
59   instance=$INSTANCE_NAME
60   if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then
61     log_error "At least one disk is needed"
62     exit 1
63   fi
64   blockdev=$DISK_0_PATH
65   if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then
66     log_error "Missing OS API Variable OLD_INSTANCE_NAME"
67   fi
68   old_name=$OLD_INSTANCE_NAME
69 }
70
71 if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
72   OS_API_VERSION=5
73   get_api5_arguments
74 elif [ "$OS_API_VERSION" = "10" ]; then
75   get_api10_arguments
76   if [ $SCRIPT_NAME = "import" -o $SCRIPT_NAME = "export" ]; then
77     log_error "import/export still not compatible with API version 10"
78     exit 1
79   fi
80 else
81   log_error "Unknown OS API VERSION $OS_API_VERSION"
82   exit 1
83 fi