virtual-machines: build_image_file.sh

File build_image_file.sh, 5.8 kB (added by ibaldin, 7 years ago)

Bash script to assist in creating ImageProxy? XML image description files

Line 
1#!/bin/bash
2
3#
4# Author: I.Baldine <ibaldin@renci.org> Copyright UNC-Chapel Hill/RENCI 2012
5#
6# The purpose of this script is to simplify the creation of image descriptor XML
7# files to be used with ImageProxy component of ExoGENI.
8# Given a filesystem file and optionally a kernel and a ramdisk and an http:// prefix
9# where they can be found it
10# 1) Verifies that the files are reachable via http
11# 2) Constructs the XML image descriptor file (including computing the SHA-1 hashes)
12# 3) Prints out the URL and the SHA-1 hash of the image descriptor file that can
13# then be used with ExoGENI slices
14#
15# This script requires curl to be installed and getopts to be supported (relatively new
16# version of Bash).
17
18# The following command line options are supported:
19# -u: the HTTP URL prefix where image components can be found [mandatory]
20# -n: the name of the XML image descriptor file that will be created [mandatory]
21# -z: gzipped file system image (sparse or non-sparse)
22# -f: regular (sparse or non-sparse, uncompressed) filesystem image. Either -z or -f must be specified
23# -k: kernel file [optional]
24# -r: ramdisk file [optional]
25
26# Place the prepared image files on the host running HTTP server,
27# execute the script from the directory where the image files are located.
28
29# User-editable parameters:
30
31# Where is curl?
32CURL=/usr/bin/curl
33SHASUM=/usr/bin/sha1sum
34
35#
36# DO NOT EDIT BELOW
37#
38IMAGESET=false
39KERNELSET=false
40RAMDISKSET=false
41URLSET=false
42XMLFILENAMESET=false
43VERSION=""
44
45function executable {
46        fil=$1
47
48        if [ ! -x $fil ]; then
49                echo "ERROR: cannot locate $fil executable. Please install and edit the top portion of the script (if necessary)"
50                exit 1
51        fi
52}
53
54function shasum {
55        fil=$1
56
57        echo `$SHASUM $fil | awk '{print $1}' `
58}
59
60function reachable {
61        url=$1
62        fil=$2
63
64        echo -n "Testing file presence of $fil"
65        if [ ! -e ./$fil ]; then
66                echo
67                echo "ERROR: unable to locate file $fil. You must execute this script from the directories where the files reside. "
68                exit 1
69        fi
70        echo " [OK]"
71
72        echo -n "Testing reachability over HTTP of $fil"
73        HTTPVAL=`$CURL -I $url/$fil 2> /dev/null | grep HTTP | awk '{print $2}'`
74        if [ "$HTTPVAL" != "200" ]; then
75                echo
76                echo "ERROR: unable to retrieve $url/$fil. HTTP code $HTTPVAL. Typical values: 403 - Forbidden, 404 - Not Found"
77                exit 1
78        fi
79        echo " [OK]"
80}
81
82while getopts ":hz:f:k:r:u:n:" opt; do
83  case $opt in
84    n)
85        XMLFILENAME=$OPTARG
86        XMLFILENAMESET=true
87        ;;
88    u)
89        URL=$OPTARG
90        URLSET=true
91        ;;
92    z)
93        ZFILESYSTEM=$OPTARG
94        IMAGESET=true
95        ;;
96    f) 
97        FILESYSTEM=$OPTARG
98        IMAGESET=true
99        ;;
100    k)
101        KERNEL=$OPTARG
102        KERNELSET=true
103        ;;
104    r) 
105        RAMDISK=$OPTARG
106        RAMDISKSET=true
107        ;;
108    h)
109        cat << 'EndOfHelp'
110
111The purpose of this script is to simplify the creation of image descriptor XML
112files to be used with ImageProxy component of ExoGENI.
113https://code.renci.org/gf/project/networkedclouds/wiki/?pagename=ImageProxyMetaFile
114
115Given a filesystem file and optionally a kernel and a ramdisk and an http:// prefix
116where they can be found it
1171) Verifies that the files are reachable via http
1182) Constructs the XML image descriptor file (including computing the SHA-1 hashes)
1193) Prints out the URL and the SHA-1 hash of the image descriptor file that can
120then be used with ExoGENI slices
121
122This script requires curl to be installed and getopts to be supported (relatively new
123version of Bash).
124
125The following command line options are supported:
126-u: the HTTP URL prefix where image components can be found [mandatory]
127-n: the name of the XML image descriptor file that will be created [mandatory]
128-z: gzipped file system image (sparse or non-sparse)
129-f: uncompressed (sparse or non-sparse) filesystem image. Either -z or -f must be specified
130-k: kernel file [optional]
131-r: ramdisk file [optional]
132
133Place the prepared image files on the host running HTTP server,
134execute the script from the directory where the image files are located.
135EndOfHelp
136        exit 0
137      ;;
138    \?)
139      echo "Invalid option: -$OPTARG" >&2
140      ;;
141    :)
142      echo "Option -$OPTARG requires an argument." >&2
143      exit 1
144      ;;
145  esac
146done
147
148executable $CURL
149executable $SHASUM
150
151if ( ! $IMAGESET ); then
152        echo "ERROR: you must specify at least -z or -f"
153        exit 1
154fi
155
156if (! $URLSET ); then
157        echo "ERROR: you must specify URL prefix where the filesystem, kernel and ramdisk files can be found"
158        exit 1
159fi
160
161if ( ! $XMLFILENAMESET ); then
162        echo "ERROR: you must specify the name of the XML image description file that will be created by this script"
163        exit 1
164fi
165
166# test reachability and files
167if [ "$ZFILESYSTEM" != "" ]; then
168        reachable $URL $ZFILESYSTEM
169        FSSUM=`shasum $ZFILESYSTEM`
170        FSTAG="ZFILESYSTEM"
171        FSURL="$URL/$ZFILESYSTEM"
172        FSNAME=$ZFILESYSTEM
173fi
174
175if [ "$FILESYSTEM" != "" ]; then
176        reachable $URL $FILESYSTEM
177        FSSUM=`shasum $FILESYSTEM`
178        FSTAG="FILESYSTEM"
179        FSURL="$URL/$FILESYSTEM"
180        FSNAME=$FILESYSTEM
181fi
182
183if ( $KERNELSET ); then
184        reachable $URL $KERNEL
185        KERNELSUM=`shasum $KERNEL`
186        KERNELURL="$URL/$KERNEL"
187fi
188
189if ( $RAMDISKSET ); then
190        reachable $URL $RAMDISK
191        RAMDISKSUM=`shasum $RAMDISK`
192        RAMDISKURL="$URL/$RAMDISK"
193fi
194
195echo "Creating XML image descriptor file $XMLFILENAME" 
196
197(
198cat << EOF
199<images>            
200     <image>
201          <type>$FSTAG</type>
202          <signature>$FSSUM</signature>
203          <url>$FSURL</url>
204     </image>
205EOF
206) > $XMLFILENAME
207if ( $KERNELSET ); then
208(
209cat << EOF
210     <image>
211          <type>KERNEL</type>
212          <signature>$KERNELSUM</signature>
213          <url>$KERNELURL</url>
214     </image>
215EOF
216) >> $XMLFILENAME
217fi
218if ( $RAMDISKSET ); then
219(
220cat << EOF
221    <image>
222          <type>RAMDISK</type>
223          <signature>$RAMDISKSUM</signature>
224          <url>$RAMDISKURL</url>
225     </image>
226EOF
227) >> $XMLFILENAME
228fi
229
230(
231cat << EOF
232</images>
233EOF
234) >> $XMLFILENAME
235
236
237reachable $URL $XMLFILENAME
238
239XMLHASH=`shasum $XMLFILENAME`
240echo
241echo
242echo "XML image descriptor file SHA1 hash is: $XMLHASH"
243echo "XML image descriptor file URL is: $URL/$XMLFILENAME"