root / snf-image-host / create @ 21be5a41
History | View | Annotate | Download (4.9 kB)
1 |
#!/bin/bash |
---|---|
2 |
|
3 |
# Copyright (C) 2011 GRNET S.A. |
4 |
# |
5 |
# This program is free software; you can redistribute it and/or modify |
6 |
# it under the terms of the GNU General Public License as published by |
7 |
# the Free Software Foundation; either version 2 of the License, or |
8 |
# (at your option) any later version. |
9 |
# |
10 |
# This program is distributed in the hope that it will be useful, but |
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 |
# General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with this program; if not, write to the Free Software |
17 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
18 |
# 02110-1301, USA. |
19 |
|
20 |
set -e |
21 |
set -o pipefail |
22 |
|
23 |
. common.sh |
24 |
|
25 |
ganeti_os_main |
26 |
|
27 |
case $BACKEND_TYPE in |
28 |
local) |
29 |
image_file="$IMAGE_DIR/$(echo "$IMAGE_NAME" | sed 's/^file://').$IMAGE_TYPE" |
30 |
if [ ! -e "$image_file" ]; then |
31 |
log_error "Image file \`$image_file' does not exit." |
32 |
exit 1 |
33 |
fi |
34 |
image_size="$(stat -L -c %s "$image_file")" |
35 |
;; |
36 |
network) |
37 |
image_cmd="$CURL $(printf "%q" "$IMAGE_NAME") 2> /dev/null" |
38 |
image_size=$($CURL -sI "$IMAGE_NAME" | grep ^Content-Length: | cut -d" " -f2) |
39 |
;; |
40 |
pithos) |
41 |
cmd_args="--db $(printf "%q" "${PITHOS_DB}") \ |
42 |
--data $(printf "%q" "${PITHOS_DATA}") \ |
43 |
$(printf "%q" "${IMAGE_NAME}")" |
44 |
image_cmd="./pithcat $cmd_args" |
45 |
image_size=$(./pithcat -s $cmd_args) |
46 |
;; |
47 |
esac |
48 |
|
49 |
monitor="" # Empty if no progress monitor program is not defined. |
50 |
if [ -n "$PROGRESS_MONITOR" ]; then |
51 |
monitor="$PROGRESS_MONITOR \ |
52 |
-i $(printf "%q" "$INSTANCE_NAME") -r $image_size" |
53 |
fi |
54 |
|
55 |
# If the target device is not a real block device we'll first losetup it. |
56 |
# This is needed for file disks. |
57 |
if [ ! -b "$blockdev" ]; then |
58 |
original_blockdev="$blockdev" |
59 |
blockdev=$($LOSETUP -sf "$blockdev") |
60 |
add_cleanup $LOSETUP -d "$blockdev" |
61 |
fi |
62 |
|
63 |
case "$IMAGE_TYPE" in |
64 |
ntfsdump|extdump) |
65 |
# Create partitions |
66 |
format_disk0 "$blockdev" "$IMAGE_TYPE" |
67 |
|
68 |
# Install a new MBR |
69 |
$INSTALL_MBR -p 1 -i n "$blockdev" |
70 |
|
71 |
target="$(map_disk0 "$blockdev")-1" #the root device |
72 |
add_cleanup unmap_disk0 "$blockdev" |
73 |
snf_export_PROPERTY_ROOT_PARTITION=1 |
74 |
if [ "$IMAGE_TYPE" = "ntfsdump" ]; then |
75 |
snf_export_PROPERTY_OSFAMILY="windows" |
76 |
else |
77 |
snf_export_PROPERTY_OSFAMILY="linux" |
78 |
fi |
79 |
;; |
80 |
diskdump) |
81 |
target="$blockdev" |
82 |
;; |
83 |
*) |
84 |
log_error "Unknown Image format: \`$IMAGE_TYPE'" |
85 |
exit 1 |
86 |
;; |
87 |
esac |
88 |
|
89 |
if [ "$BACKEND_TYPE" = "local" ]; then |
90 |
# dd the dump to its new home :-) |
91 |
# Deploying an image file on a target block device is a streaming copy |
92 |
# operation. Enable the direct I/O flag on the output fd to avoid polluting |
93 |
# the host cache with useless data. |
94 |
$monitor dd bs=4M if="$image_file" of="$target" oflag=direct |
95 |
else |
96 |
$image_cmd | $monitor dd bs=4M of="$target" oflag=direct |
97 |
fi |
98 |
|
99 |
# Create a floppy image |
100 |
floppy=$(mktemp --tmpdir floppy.XXXXXX) |
101 |
add_cleanup rm "$floppy" |
102 |
|
103 |
snf_export_DEV=/dev/vda |
104 |
snf_export_TYPE="$IMG_FORMAT" |
105 |
snf_export_PASSWORD="$IMG_PASSWD" |
106 |
snf_export_HOSTNAME="$instance" |
107 |
if [ -n "$IMG_PROPERTIES" ]; then |
108 |
snf_export_PROPERTIES="$IMG_PROPERTIES" |
109 |
fi |
110 |
if [ -n "$IMG_PERSONALITY" ]; then |
111 |
snf_export_PERSONALITY="$IMG_PERSONALITY" |
112 |
fi |
113 |
|
114 |
create_floppy "$floppy" |
115 |
|
116 |
# Invoke the helper vm to do the dirty job... |
117 |
jail=$(mktemp -d --tmpdir tmpfsXXXXXXX) |
118 |
add_cleanup rmdir "$jail" |
119 |
|
120 |
mount tmpfs -t tmpfs "$jail" -o size=50M |
121 |
add_cleanup umount "$jail" |
122 |
|
123 |
result_file=$(mktemp --tmpdir="$jail" result.XXXXXX) |
124 |
add_cleanup rm "$result_file" |
125 |
|
126 |
snapshot=$(mktemp --tmpdir="$jail" helperXXXXXX.img) |
127 |
add_cleanup rm "$snapshot" |
128 |
|
129 |
"$QEMU_IMG" create -f qcow2 -b "$HELPER_IMG" "$snapshot" |
130 |
|
131 |
echo "$(date +%Y:%m:%d-%H:%M:%S.%N) Starting helper VM..." |
132 |
$TIMELIMIT -t "$HELPER_SOFT_TIMEOUT" -T "$HELPER_HARD_TIMEOUT" \ |
133 |
kvm -runas "$HELPER_USER" -drive file="$snapshot" \ |
134 |
-drive file="$blockdev",format=raw,if=virtio,cache=none \ |
135 |
-boot c -serial stdio -serial file:"$result_file" \ |
136 |
-fda "$floppy" -vga none -nographic -parallel none -monitor null \ |
137 |
-kernel "$HELPER_KERNEL" -initrd "$HELPER_INITRD" \ |
138 |
-append "quiet ro root=/dev/sda1 console=ttyS0,9600n8 snf_image_activate_helper" \ |
139 |
2>&1 | sed 's|^|HELPER: |g' |
140 |
echo "$(date +%Y:%m:%d-%H:%M:%S.%N) Helper VM finished." |
141 |
|
142 |
if [ $? -ne 0 ]; then |
143 |
if [ $? -gt 128 ]; then |
144 |
log_error "Helper was terminated. Did not finish on time." |
145 |
fi |
146 |
exit 1 |
147 |
fi |
148 |
|
149 |
# Read the first line. This will remove \r and \n chars |
150 |
result=$(sed 's|\r||g' "$result_file" | head -1) |
151 |
|
152 |
if [ "x$result" != "xSUCCESS" ]; then |
153 |
log_error "Helper VM returned error:" |
154 |
log_error "$result" |
155 |
exit 1 |
156 |
fi |
157 |
|
158 |
# Execute cleanups |
159 |
cleanup |
160 |
trap - EXIT |
161 |
|
162 |
exit 0 |
163 |
|
164 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : |