root / snf-image-host / create @ db0078b9
History | View | Annotate | Download (4.7 kB)
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 |
set -o pipefail |
34 |
|
35 |
. common.sh |
36 |
|
37 |
ganeti_os_main |
38 |
|
39 |
if [ "$IMAGE_TYPE" = custom ]; then |
40 |
image_file=/dev/null |
41 |
image_size=0 |
42 |
else |
43 |
image_file="$IMAGE_DIR/$IMAGE_NAME-$ARCH.$IMAGE_TYPE" |
44 |
image_size="$(stat -L -c %s "$image_file")" |
45 |
fi |
46 |
|
47 |
if [ ! -e "$image_file" ]; then |
48 |
log_error "Image file \`$image_file' does not exit." |
49 |
exit 1 |
50 |
fi |
51 |
|
52 |
monitor="" #Empty if progress monitor support is disabled |
53 |
if [ "$progress_monitor_support" = "yes" ]; then |
54 |
monitor="$(printf "%q" "$PROGRESS_MONITOR") \ |
55 |
-i $(printf "%q" "$INSTANCE_NAME") -r $image_size" |
56 |
fi |
57 |
|
58 |
# If the target device is not a real block device we'll first losetup it. |
59 |
# This is needed for file disks. |
60 |
if [ ! -b "$blockdev" ]; then |
61 |
original_blockdev="$blockdev" |
62 |
blockdev=$("$LOSETUP" -sf "$blockdev") |
63 |
add_cleanup "$LOSETUP" -d "$blockdev" |
64 |
fi |
65 |
|
66 |
case "$IMAGE_TYPE" in |
67 |
ntfsdump, extdump) |
68 |
# Create partitions |
69 |
format_disk0 "$blockdev" "$IMAGE_TYPE" |
70 |
|
71 |
# Install a new MBR |
72 |
"$INSTALL_MBR" -p 1 -i n "$blockdev" |
73 |
|
74 |
target="$(map_disk0 "$blockdev")-1" #the root device |
75 |
add_cleanup unmap_disk0 "$blockdev" |
76 |
snf_export_ROOT=1 |
77 |
;; |
78 |
diskdump, custom) |
79 |
target="$blockdev" |
80 |
;; |
81 |
esac |
82 |
|
83 |
if [ "$IMAGE_TYPE" = "custom" ]; then |
84 |
wget -O - $IMAGE_NAME | $monitor dd bs=4M of="$target" oflag=direct |
85 |
else |
86 |
# dd the dump to its new home :-) |
87 |
# Deploying an image file on a target block device is a streaming copy |
88 |
# operation. Enable the direct I/O flag on the output fd to avoid polluting |
89 |
# the host cache with useless data. |
90 |
$monitor dd bs=4M if="$image_file" of="$target" oflag=direct |
91 |
fi |
92 |
|
93 |
# Create a floppy image |
94 |
floppy=$(mktemp --tmpdir floppy.XXXXXX) |
95 |
add_cleanup rm "$floppy" |
96 |
|
97 |
snf_export_DEV=/dev/vda |
98 |
snf_export_TYPE="$IMG_FORMAT" |
99 |
snf_export_PASSWORD="$IMG_PASSWD" |
100 |
snf_export_HOSTNAME="$instance" |
101 |
snf_export_PROPERTIES="$IMG_PROPERTIES" |
102 |
if [ -n "$IMG_PERSONALITY" ]; then |
103 |
snf_export_PERSONALITY="$IMG_PERSONALITY" |
104 |
fi |
105 |
|
106 |
create_floppy "$floppy" |
107 |
|
108 |
# Invoke the helper vm to do the dirty job... |
109 |
result_file=$(mktemp --tmpdir result.XXXXXX) |
110 |
add_cleanup rm "$result_file" |
111 |
|
112 |
snapshot=$(mktemp --tmpdir helperXXXXXX.img) |
113 |
add_cleanup rm "$snapshot" |
114 |
|
115 |
"$QEMU_IMG" create -f qcow2 -b "$HELPER_IMG" "$snapshot" |
116 |
|
117 |
echo "$(date +%Y:%m:%d-%H:%M:%S.%N) Starting helper VM..." |
118 |
"$TIMELIMIT" -t "$HELPER_SOFT_TIMEOUT" -T "$HELPER_HARD_TIMEOUT" \ |
119 |
kvm -runas "$HELPER_USER" -drive file="$snapshot" \ |
120 |
-drive file="$blockdev",format=raw,if=virtio,cache=none \ |
121 |
-boot c -serial stdio -serial file:"$result_file" \ |
122 |
-fda "$floppy" -vga none -nographic -parallel none -monitor null \ |
123 |
-kernel "$HELPER_KERNEL" -initrd "$HELPER_INITRD" \ |
124 |
-append "quiet ro root=/dev/sda1 console=ttyS0,9600n8 snf_image_activate_helper" \ |
125 |
2>&1 | sed 's|^|HELPER: |g' |
126 |
echo "$(date +%Y:%m:%d-%H:%M:%S.%N) Helper VM finished." |
127 |
|
128 |
if [ $? -ne 0 ]; then |
129 |
if [ $? -gt 128 ]; then |
130 |
log_error "Helper was terminated. Did not finish on time." |
131 |
fi |
132 |
exit 1 |
133 |
fi |
134 |
|
135 |
# Read the first line. This will remove \r and \n chars |
136 |
result=$(sed 's|\r||g' "$result_file" | head -1) |
137 |
|
138 |
if [ "x$result" != "xSUCCESS" ]; then |
139 |
log_error "Helper VM returned error" |
140 |
exit 1 |
141 |
fi |
142 |
|
143 |
# Execute cleanups |
144 |
cleanup |
145 |
trap - EXIT |
146 |
|
147 |
exit 0 |
148 |
|
149 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : |