Support os variants
[ext/instance-debootstrap.git] / common.sh.in
1 #
2
3 # Copyright (C) 2007, 2008, 2009 Google Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 # 02110-1301, USA.
19
20
21 log_error() {
22   echo "$@" >&2
23 }
24
25 get_api5_arguments() {
26   GETOPT_RESULT=$*
27   # Note the quotes around `$TEMP': they are essential!
28   eval set -- "$GETOPT_RESULT"
29   while true; do
30     case "$1" in
31       -i|-n) instance=$2; shift 2;;
32
33       -o) old_name=$2; shift 2;;
34
35       -b) blockdev=$2; shift 2;;
36
37       -s) swapdev=$2; shift 2;;
38
39       --) shift; break;;
40
41       *)  log_error "Internal error!" >&2; exit 1;;
42     esac
43   done
44   if [ -z "$instance" -o -z "$blockdev" ]; then
45     log_error "Missing OS API Argument (-i, -n, or -b)"
46     exit 1
47   fi
48   if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev"  ]; then
49     log_error "Missing OS API Argument -s (swapdev)"
50     exit 1
51   fi
52   if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name"  ]; then
53     log_error "Missing OS API Argument -o (old_name)"
54     exit 1
55   fi
56 }
57
58 get_api10_arguments() {
59   if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then
60     log_error "Missing OS API Variable:"
61     log_error "(INSTANCE_NAME HYPERVISOR or DISK_COUNT)"
62     exit 1
63   fi
64   instance=$INSTANCE_NAME
65   if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then
66     log_error "At least one disk is needed"
67     exit 1
68   fi
69   if [ "$SCRIPT_NAME" = "export" ]; then
70     if [ -z "$EXPORT_DEVICE" ]; then
71       log_error "Missing OS API Variable EXPORT_DEVICE"
72     fi
73     blockdev=$EXPORT_DEVICE
74   elif [ "$SCRIPT_NAME" = "import" ]; then
75     if [ -z "$IMPORT_DEVICE" ]; then
76        log_error "Missing OS API Variable IMPORT_DEVICE"
77     fi
78     blockdev=$IMPORT_DEVICE
79   else
80     blockdev=$DISK_0_PATH
81   fi
82   if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then
83     log_error "Missing OS API Variable OLD_INSTANCE_NAME"
84   fi
85   old_name=$OLD_INSTANCE_NAME
86 }
87
88 format_disk0() {
89   # Create one big partition, and make it bootable
90   # some versions of sfdisk need manual specification of 
91   # head/sectors for devices such as drbd which don't 
92   # report geometry
93   sfdisk -H 255 -S 63 --quiet --Linux "$1" <<EOF
94 0,,L,*
95 EOF
96 }
97
98 map_disk0() {
99   blockdev="$1"
100   filesystem_dev_base=`kpartx -l -p- $blockdev | \
101                        grep -m 1 -- "-1.*$blockdev" | \
102                        awk '{print $1}'`
103   if [ -z "$filesystem_dev_base" ]; then
104     log_error "Cannot interpret kpartx output and get partition mapping"
105     exit 1
106   fi
107   kpartx -a -p- $blockdev > /dev/null
108   filesystem_dev="/dev/mapper/$filesystem_dev_base"
109   if [ ! -b "$filesystem_dev" ]; then
110     log_error "Can't find kpartx mapped partition: $filesystem_dev"
111     exit 1
112   fi
113   echo "$filesystem_dev"
114 }
115
116 unmap_disk0() {
117   kpartx -d -p- $1
118 }
119
120 cleanup() {
121   if [ ${#CLEANUP[*]} -gt 0 ]; then
122     LAST_ELEMENT=$((${#CLEANUP[*]}-1))
123     REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
124     for i in $REVERSE_INDEXES; do
125       ${CLEANUP[$i]}
126     done
127   fi
128 }
129
130 DEFAULT_FILE="@sysconfdir@/default/ganeti-instance-debootstrap"
131 if [ -f "$DEFAULT_FILE" ]; then
132     . "$DEFAULT_FILE"
133 fi
134
135 # note: we don't set a default mirror since debian and ubuntu have
136 # different defaults, and it's better to use the default
137
138 # only if the user want to specify a mirror in the defaults file we
139 # will use it, this declaration is to make sure the variable is set
140 : ${MIRROR:=""}
141 : ${PROXY:=""}
142 : ${SUITE:="lenny"}
143 : ${ARCH:=""}
144 : ${EXTRA_PKGS:=""}
145 : ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-debootstrap.d"}
146 : ${VARIANTS_DIR:="@sysconfdir@/ganeti/instance-debootstrap/variants"}
147 : ${GENERATE_CACHE:="yes"}
148 : ${CLEAN_CACHE:="14"} # number of days to keep a cache file
149 if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
150   DEFAULT_PARTITION_STYLE="none"
151 else
152   DEFAULT_PARTITION_STYLE="msdos"
153 fi
154 : ${PARTITION_STYLE:=$DEFAULT_PARTITION_STYLE} # disk partition style
155
156 CACHE_DIR="@localstatedir@/cache/ganeti-instance-debootstrap"
157
158 SCRIPT_NAME=$(basename $0)
159
160 for dir in /lib/udev /sbin; do
161   if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
162     VOL_ID=$dir/vol_id
163   fi
164 done
165
166 if [ -z "$VOL_ID" ]; then
167     log_error "vol_id not found, please install udev"
168     exit 1
169 fi
170
171 if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
172   OS_API_VERSION=5
173   GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
174   if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
175   get_api5_arguments $GETOPT_RESULT
176 elif [ "$OS_API_VERSION" = "10" ]; then
177   get_api10_arguments
178 else
179   log_error "Unknown OS API VERSION $OS_API_VERSION"
180   exit 1
181 fi
182
183 if [ -n "$OS_VARIANT" ]; then
184   if [ ! -d "$VARIANTS_DIR" ]; then
185     log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
186     exit 1
187   fi
188   VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
189   if [ -f "$VARIANTS_CONFIG" ]; then
190     . "$VARIANTS_CONFIG"
191   else
192     if grep -qxF "$OS_VARIANT" variants.list; then
193       log_error "ERROR: instance-debootstrap configuration error"
194       log_error "  Published variant $OS_VARIANT is missing its config file"
195       log_error "  Please create $VARIANT_CONFIG or unpublish the variant"
196       log_error "  (by removing $OS_VARIANT from variants.list)"
197     else
198       log_error "Unofficial variant $OS_VARIANT is unsupported"
199       log_error "Most probably this is a user error, forcing a wrong name"
200       log_error "To support this variant please create file $VARIANT_CONFIG"
201     fi
202     exit 1
203   fi
204 fi
205