Add support for GUID partition tables
[snf-image] / snf-image-helper / snf-image-helper.in
index 89e4078..afb6ee8 100644 (file)
@@ -1,62 +1,64 @@
 #!/bin/bash
 
-# Copyright 2011 GRNET S.A. All rights reserved.
+# Copyright (C) 2011 GRNET S.A. 
 #
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions
-# are met:
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
 #
-#   1. Redistributions of source code must retain the above copyright
-#      notice, this list of conditions and the following disclaimer.
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
 #
-#  2. Redistributions in binary form must reproduce the above copyright
-#     notice, this list of conditions and the following disclaimer in the
-#     documentation and/or other materials provided with the distribution.
-#
-# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
-# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
-# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-# SUCH DAMAGE.
-#
-# The views and conclusions contained in the software and documentation are
-# those of the authors and should not be interpreted as representing official
-# policies, either expressed or implied, of GRNET S.A.
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301, USA.
 
 . @commondir@/common.sh
 
 set -e
 
+if [ "x$1" != "x--force" ]; then
+    echo "WARNING: Exiting, this command would cause the system to halt." >&2
+    echo "Use --force if you know what you're doing." >&2
+    exit 1
+fi
+
 # terminate helper vm when the script exits
-CLEANUP+=("telinit 0")
+add_cleanup telinit 0
 
-if [ ! -b $FLOPPY_DEV ]; then
+if [ ! -b "$FLOPPY_DEV" ]; then
     log_error "Floppy device is not present!"
 fi
 
-floppy=$(mktemp -d --tmpdir floppy.XXXXXXXX)
-CLEANUP+=("rmdir $floppy")
+floppy=$(mktemp -d --tmpdir floppy.XXXXXX)
+add_cleanup rmdir "$floppy"
 
 mount $FLOPPY_DEV $floppy
-CLEANUP+=("umount $floppy")
+add_cleanup umount "$floppy"
 
-if [ -f $floppy/rules ]; then
-    source $floppy/rules
+if [ -f "$floppy/rules" ]; then
+    source "$floppy/rules"
 else
     log_error "Floppy does not contain \`rules\' file"
 fi
 
+if [ -n "$SNF_IMAGE_PROPERTIES" ]; then
+    properties=$(mktemp --tmpdir properties.XXXXXX)
+    add_cleanup rm "$properties"
+    echo "$SNF_IMAGE_PROPERTIES" |
+        "@scriptsdir@/decode-properties.py" "$properties"
+    source "$properties"
+fi
+
 # Image mount point...
-target=$(mktemp -d --tmpdir target.XXXXXXXX)
-CLEANUP+=("rmdir $target")
+target=$(mktemp -d --tmpdir target.XXXXXX)
+add_cleanup rmdir "$target"
 
-export SNF_IMAGE_TARGET=$target
+export SNF_IMAGE_TARGET="$target"
 
 if [ ! -d "@tasksdir@" ]; then
     log_error "snf-image/tasks directory is missing"
@@ -64,17 +66,34 @@ fi
 
 RUN_PARTS=$(which run-parts)
 if [ -z "$RUN_PARTS" ]; then
-    log_error "run-parts programe is missing from the system"
+    log_error "run-parts program is missing from the system"
 fi
 
 # If something goes wrong with the tasks, try to umount the target filesystem
 # in case it is left mounted...
-trap '{ umount $target; }' ERR
+trap '{ umount "$target"; }' ERR
+
+if [ -z "$SNF_IMAGE_PROPERTY_EXCLUDE_ALL_TASKS" ]; then
+
+    export SNF_IMAGE_RESIZE_PART="$(get_partition_to_resize "$SNF_IMAGE_DEV")"
+
+    if [[ ! "$SNF_IMAGE_PROPERTY_OSFAMILY" =~ ^(linux|windows)$ ]]; then
+        log_error "Supported values for OSFAMILY property are: linux|windows"
+    fi
+
+    # Redirect standard error to standard output,
+    # prepend a timestamp before each line of output.
+    echo "Execute all snf-image tasks...."
+    $RUN_PARTS -v --exit-on-error "@tasksdir@" 2>&1|
+        while IFS= read -r line; do
+            echo $(date +%Y:%m:%d-%H:%M:%S.%N) "$line"
+        done
+fi
 
-echo "Execute all snf-image tasks...." 
-$RUN_PARTS -v --exit-on-error "@tasksdir@"
+# Disable the trap. If code reaches here, the filesystem is unmounted.
+trap - ERR
 
-echo "SUCCESS" > $RESULT
+echo "SUCCESS" > "$RESULT"
 
 cleanup
 trap - EXIT