Rename host to snf-image-host
[snf-image] / snf-image-host / create
1 #!/bin/bash
2
3 # Copyright 2011 GRNET S.A. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 #
9 #   1. Redistributions of source code must retain the above copyright
10 #      notice, this list of conditions and the following disclaimer.
11 #
12 #  2. Redistributions in binary form must reproduce the above copyright
13 #     notice, this list of conditions and the following disclaimer in the
14 #     documentation and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 # SUCH DAMAGE.
27 #
28 # The views and conclusions contained in the software and documentation are
29 # those of the authors and should not be interpreted as representing official
30 # policies, either expressed or implied, of GRNET S.A.
31
32 set -e
33
34 . common.sh
35
36 case "$IMAGE_TYPE" in
37     extdump)
38         IMAGE_FILE="${IMAGE_DIR}/${IMAGE_NAME}-${ARCH}.extdump";;
39     ntfsdump)
40         IMAGE_FILE="${IMAGE_DIR}/${IMAGE_NAME}-${ARCH}.ntfsdump";;
41     *)
42         log_error "Unknown image type: \`$IMAGE_TYPE'.";
43         exit 1
44 esac
45
46 if [ ! -e "$IMAGE_FILE" ]; then
47     log_error "Image file \`$IMAGE_FILE' does not exit."
48     exit 1
49 fi
50
51 MONITOR="" #Empty if progress monitor support is disabled
52 if [ "$progress_monitor_support" = "yes" ]; then
53     IMAGE_SIZE="$(stat -L -c %s ${IMAGE_FILE})"
54     MONITOR="$PROGRESS_MONITOR -i ${INSTANCE_NAME} -r ${IMAGE_SIZE}"
55 fi
56
57 # If the target device is not a real block device we'll first losetup it.
58 # This is needed for file disks.
59 if [ ! -b $blockdev ]; then
60     ORIGINAL_BLOCKDEV=$blockdev
61     blockdev=$($LOSETUP -sf $blockdev)
62     CLEANUP+=("$LOSETUP -d $blockdev")
63 fi
64
65 format_disk0 $blockdev ${IMAGE_TYPE}
66
67 filesystem_dev=$(map_disk0 $blockdev)
68 CLEANUP+=("unmap_disk0 $blockdev")
69
70 root_dev="${filesystem_dev}-1"
71
72 # dd the dump to its new home :-)
73 # Deploying an image file on a target block device is a streaming
74 # copy operation. Enable the direct I/O flag on the output fd to 
75 # avoid polluting the host cache with useless data.
76 $MONITOR dd bs=4M if=$IMAGE_FILE of=$root_dev oflag=direct
77
78 # Create a floppy image
79 floppy=$(mktemp --tmpdir floppy.XXXXXXXX)
80 CLEANUP+=("rm -f $floppy")
81
82 snf_export_DEV=/dev/vda
83 snf_export_TYPE=${IMG_FORMAT}
84 snf_export_PASSWORD=${IMG_PASSWD}
85 snf_export_HOSTNAME=${instance}
86 if [ -n "$IMG_PERSONALITY" ]; then
87     snf_export_PERSONALITY=${IMG_PERSONALITY}
88 fi
89
90 create_floppy $floppy
91
92 # Invoke the helper vm to do the dirty job...
93 result_file=$(mktemp --tmpdir result.XXXXXXXX)
94 CLEANUP+=("rm -f $result_file")
95
96 $TIMELIMIT -t $HELPER_SOFT_TIMEOUT -T $HELPER_HARD_TIMEOUT \
97     kvm -runas $HELPER_USR -drive file=${HELPER},snapshot=on \
98     -drive file=$root_dev,format=raw,if=virtio,cache=none \
99     -boot c -serial stdio -serial file:$result_file -fda $floppy \
100     -vga none -nographic -parallel none -monitor null -nographic \
101     -kernel ${HELPER_KERNEL} -initrd ${HELPER_INITRD} \
102     -append "quiet ro root=/dev/sda1 console=ttyS0,9600n8" \
103     2>&1 | sed 's|^|HELPER: |g'
104
105 if [ $? -ne 0 ]; then
106     if [ $? -gt 128 ];  then
107         log_error "Helper was terminated. Did not finish on time."
108     fi
109     exit 1
110 fi
111
112 # Read the first line. This will remove \r and \n chars
113 result=$(sed 's|\r||g' $result_file | xargs echo)
114
115 if [ "x$result" != "xSUCCESS" ]; then
116     log_error "Helper VM returned error"
117     exit 1
118 fi
119
120 # Install a new MBR
121 $INSTALL_MBR -p 1 -i n ${blockdev}
122
123 # Execute cleanups
124 cleanup
125 trap - EXIT
126
127 exit 0
128
129 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :