Fix copyright and license notices throughout
[snf-image] / snf-image-host / common.sh.in
1 # Copyright (C) 2011 GRNET S.A. 
2 # Copyright (C) 2007, 2008, 2009 Google Inc.
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 # 02110-1301, USA.
18
19 AWK="awk"
20 KPARTX="kpartx"
21 LOSETUP="losetup"
22 SFDISK="sfdisk"
23 QEMU_IMG="qemu-img"
24 INSTALL_MBR="install-mbr"
25 TIMELIMIT="timelimit"
26 CURL="curl"
27 PROGRESS_MONITOR="snf-progress-monitor"
28
29 progress_monitor_support="@progress_monitor_support@"
30
31 CLEANUP=( )
32
33 add_cleanup() {
34     local cmd=""
35     for arg; do cmd+=$(printf "%q " "$arg"); done
36     CLEANUP+=("$cmd")
37 }
38
39 log_error() {
40     echo "$@" >&2
41 }
42
43 get_api5_arguments() {
44     GETOPT_RESULT=$*
45     # Note the quotes around `$TEMP': they are essential!
46     eval set -- "$GETOPT_RESULT"
47     while true; do
48         case "$1" in
49             -i|-n) instance=$2; shift 2;;
50
51             -o) old_name=$2; shift 2;;
52
53             -b) blockdev=$2; shift 2;;
54
55             -s) swapdev=$2; shift 2;;
56
57             --) shift; break;;
58
59             *)  log_error "Internal error!" >&2; exit 1;;
60         esac
61     done
62     if [ -z "$instance" -o -z "$blockdev" ]; then
63         log_error "Missing OS API Argument (-i, -n, or -b)"
64         exit 1
65     fi
66     if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev"  ]; then
67         log_error "Missing OS API Argument -s (swapdev)"
68         exit 1
69     fi
70     if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name"  ]; then
71         log_error "Missing OS API Argument -o (old_name)"
72         exit 1
73     fi
74 }
75
76 get_api10_arguments() {
77     if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then
78         log_error "Missing OS API Variable:"
79         log_error "(INSTANCE_NAME HYPERVISOR or DISK_COUNT)"
80         exit 1
81     fi
82     instance=$INSTANCE_NAME
83     if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then
84         log_error "At least one disk is needed"
85         exit 1
86     fi
87     if [ "$SCRIPT_NAME" = "export" ]; then
88         if [ -z "$EXPORT_DEVICE" ]; then
89         log_error "Missing OS API Variable EXPORT_DEVICE"
90     fi
91     blockdev=$EXPORT_DEVICE
92     elif [ "$SCRIPT_NAME" = "import" ]; then
93         if [ -z "$IMPORT_DEVICE" ]; then
94         log_error "Missing OS API Variable IMPORT_DEVICE"
95         fi
96         blockdev=$IMPORT_DEVICE
97     else
98         blockdev=$DISK_0_PATH
99     fi
100     if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then
101         log_error "Missing OS API Variable OLD_INSTANCE_NAME"
102     fi
103     old_name=$OLD_INSTANCE_NAME
104 }
105
106 get_api20_arguments() {
107     get_api10_arguments
108     if [ -z "$OSP_IMG_ID" ]; then
109         log_error "Missing OS API Parameter: OSP_IMG_ID"
110         exit 1
111     fi
112     if [ -z "$OSP_IMG_FORMAT" ]; then
113         log_error "Missing OS API Parameter: OSP_IMG_FORMAT"
114         exit 1
115     fi
116     if [ -z "$OSP_IMG_PASSWD" ]; then
117         log_error "Missing OS API Parameter: OSP_IMG_PASSWD"
118         exit 1
119     fi
120     if [ -z "$OSP_IMG_PROPERTIES" ]; then
121         log_error "Missing OS API Parameter: OSP_IMG_PROPERTIES"
122         exit 1
123     fi
124
125     IMG_ID=$OSP_IMG_ID
126     IMG_FORMAT=$OSP_IMG_FORMAT
127     IMG_PASSWD=$OSP_IMG_PASSWD
128     IMG_PROPERTIES=$OSP_IMG_PROPERTIES
129     if [ -n "$OSP_IMG_PERSONALITY" ]; then
130         IMG_PERSONALITY=$OSP_IMG_PERSONALITY
131     fi
132 }
133
134 map_disk0() {
135     blockdev="$1"
136     filesystem_dev_base=$($KPARTX -l -p- $blockdev | \
137                             grep -m 1 -- "-1.*$blockdev" | \
138                             $AWK '{print $1}')
139     if [ -z "$filesystem_dev_base" ]; then
140         log_error "Cannot interpret kpartx output and get partition mapping"
141         exit 1
142     fi
143     $KPARTX -a -p- "$blockdev" > /dev/null
144     filesystem_dev="/dev/mapper/${filesystem_dev_base/%-1/}"
145     if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then
146         log_error "Can't find kpartx mapped partition:" \
147                                             "/dev/mapper/$filesystem_dev_base"
148         exit 1
149     fi
150     echo "$filesystem_dev"
151 }
152
153 unmap_disk0() {
154     $KPARTX -d -p- "$1"
155 }
156
157 format_disk0() {
158     local device="$1"
159     local image_type="$2"
160
161     declare -A part_id=( ['extdump']="83" ["ntfsdump"]="7" )
162
163     # The -f is needed, because we use an optimal alignment and sfdisk complains
164     # about partitions not ending on clylinder boundary.
165     local sfdisk_cmd="$SFDISK -uS -H 255 -S 63 -f --quiet --Linux --DOS $device"
166
167     $sfdisk_cmd > /dev/null <<EOF
168 2048,,${part_id["$image_type"]},*
169 EOF
170 }
171
172 create_floppy() {
173     local img=$1
174
175     local target=$(mktemp -d)
176     add_cleanup rmdir "$target"
177
178     dd bs=512 count=2880 if=/dev/zero of="$img"
179     mkfs.ext2 -F "$img" > /dev/null
180     mount "$img" "$target" -o loop
181     set | egrep ^snf_export_\\w+= | sed -e 's/^snf_export_/export SNF_IMAGE_/' \
182         > "$target/rules"
183     umount "$target"
184 }
185
186 # this one is only to be called by create
187 ganeti_os_main() {
188     if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
189         OS_API_VERSION=5
190         GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
191         if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
192         get_api5_arguments $GETOPT_RESULT
193     elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
194         get_api10_arguments
195     elif [ "$OS_API_VERSION" = "20" ]; then
196         get_api20_arguments
197         IMAGE_NAME=$IMG_ID
198         IMAGE_TYPE=$IMG_FORMAT
199     else
200         log_error "Unknown OS API VERSION $OS_API_VERSION"
201         exit 1
202     fi
203     
204     if [ -n "$OS_VARIANT" ]; then
205         if [ ! -d "$VARIANTS_DIR" ]; then
206             log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
207             exit 1
208         fi
209         VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
210         if [ -f "$VARIANT_CONFIG" ]; then
211             . "$VARIANT_CONFIG"
212         else
213             if grep -qxF "$OS_VARIANT" variants.list; then
214                 log_error "ERROR: instance-image configuration error"
215                 log_error "  Published variant $OS_VARIANT is missing its" \
216                     "config file"
217                 log_error "  Please create $VARIANT_CONFIG or unpublish the" \
218                     "variant"
219                 log_error "  (by removing $OS_VARIANT from variants.list)"
220             else
221                 log_error "Unofficial variant $OS_VARIANT is unsupported"
222                 log_error "Most probably this is a user error, forcing a" \
223                     "wrong name"
224                 log_error "To support this variant please create file" \
225                     "$VARIANT_CONFIG"
226             fi
227             exit 1
228         fi
229     fi
230
231 }
232
233 cleanup() {
234 # if something fails here, it souldn't call cleanup again...
235     trap - EXIT
236     if [ ${#CLEANUP[*]} -gt 0 ]; then
237         LAST_ELEMENT=$((${#CLEANUP[*]}-1))
238         REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
239         for i in $REVERSE_INDEXES; do
240             # If something fails here, it's better to retry it for a few times
241             # before we give up with an error. This is needed for kpartx when
242             # dealing with ntfs partitions mounted through fuse. umount is not
243             # synchronous and may return while the partition is still busy. A
244             # premature attempt to delete partition mappings through kpartx on a
245             # device that hosts previously mounted ntfs partition may fail with
246             # an  `device-mapper: remove ioctl failed: Device or resource busy'
247             # error. A sensible workaround for this is to wait for a while and
248             # then try again.
249             local cmd=${CLEANUP[$i]}
250             $cmd || for interval in 0.25 0.5 1 2 4; do
251             echo "Command $cmd failed!"
252             echo "I'll wait for $interval secs and will retry..."
253             sleep $interval
254             $cmd && break
255         done
256         if [ "$?" != "0" ]; then
257             echo "Giving Up..."
258             exit 1;
259         fi
260     done
261   fi
262 }
263
264 trap cleanup EXIT
265
266 DEFAULT_FILE="@sysconfdir@/default/snf-image"
267 if [ -f "$DEFAULT_FILE" ]; then
268     . "$DEFAULT_FILE"
269 fi
270
271 : ${ARCH:="x86_64"}
272 : ${VARIANTS_DIR:="@sysconfdir@/ganeti/snf-image/variants"}
273 : ${IMAGE_DIR:="@localstatedir@/lib/snf-image"}
274 : ${HELPER_DIR:="@HELPER_DIR@"}
275 : ${HELPER_IMG:="@HELPER_IMG@"}
276 : ${HELPER_KERNEL:="@HELPER_KERNEL@"}
277 : ${HELPER_INITRD:="@HELPER_INITRD@"}
278 : ${HELPER_PKG:="@HELPER_DIR@/snf-image-helper.deb"}
279 : ${HELPER_SOFT_TIMEOUT:=15}
280 : ${HELPER_HARD_TIMEOUT:=5}
281 : ${HELPER_USER:="nobody"}
282 : ${HELPER_CACHE_FILE:="@HELPER_DIR@/cache.tar"}
283 : ${HELPER_EXTRA_PKGS:="linux-image-amd64,e2fsprogs,ntfs-3g,ntfsprogs,xmlstarlet,python,parted,reglookup,chntpw"}
284 : ${HELPER_MIRROR:=""}
285
286
287 SCRIPT_NAME=$(basename $0)
288
289 if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
290     VOL_ID="/sbin/blkid -c /dev/null -o value -s UUID"
291     VOL_TYPE="/sbin/blkid -c /dev/null -o value -s TYPE"
292 else
293     for dir in /lib/udev /sbin; do
294         if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
295             VOL_ID="$dir/vol_id -u"
296             VOL_TYPE="$dir/vol_id -t"
297         fi
298     done
299 fi
300
301 if [ -z "$VOL_ID" ]; then
302     log_error "vol_id or blkid not found, please install udev or util-linux"
303     exit 1
304 fi
305
306 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :