root / snf-image-host / create @ b30e4454
History | View | Annotate | Download (4.7 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 |
if [ "$IMAGE_DEBUG" = "yes" ]; then |
28 |
PS4='$(date "+%s.%N ($LINENO) + ")' |
29 |
set -x |
30 |
elif [ "$IMAGE_DEBUG" != "no" ]; then |
31 |
echo "Warning: Unsupported IMAGE_DEBUG value: \`$IMAGE_DEBUG'" |
32 |
fi |
33 |
|
34 |
monitor_pipe=$(mktemp -u) |
35 |
mkfifo -m 600 "$monitor_pipe" |
36 |
add_cleanup rm -f "$monitor_pipe" |
37 |
|
38 |
if [ -n "$PROGRESS_MONITOR" ]; then |
39 |
{ sleep 1; $PROGRESS_MONITOR "$instance" < "$monitor_pipe" ; } & |
40 |
monitor_pid="$!" |
41 |
else |
42 |
sed -u 's|^|[MONITOR] |g' < "$monitor_pipe" & |
43 |
monitor_pid="$!" |
44 |
fi |
45 |
|
46 |
# Create file descriptor to monitor_pipe |
47 |
exec {MONITOR_FD}>${monitor_pipe} |
48 |
add_cleanup close_fd ${MONITOR_FD} |
49 |
|
50 |
# Ignore sigpipe signals. If progress monitor is dead and snf-image tries to |
51 |
# output something to the opened pipe, then a sigpipe will be raised. If we do |
52 |
# not catch this, the program will terminate. |
53 |
trap "" SIGPIPE |
54 |
|
55 |
trap report_and_cleanup EXIT |
56 |
|
57 |
case $BACKEND_TYPE in |
58 |
local) |
59 |
image_file="$IMAGE_DIR/$(echo "$IMAGE_NAME" | sed 's/^file://').$IMAGE_TYPE" |
60 |
if [ ! -e "$image_file" ]; then |
61 |
log_error "Image file \`$image_file' does not exit." |
62 |
report_error "Unable to retrieve image file." |
63 |
exit 1 |
64 |
fi |
65 |
image_size="$(stat -L -c %s "$image_file")" |
66 |
;; |
67 |
null) |
68 |
image_file=/dev/null |
69 |
image_size=0 |
70 |
# Treat it as local file from now on... |
71 |
BACKEND_TYPE="local" |
72 |
;; |
73 |
network) |
74 |
image_cmd="$CURL $(printf "%q" "$IMAGE_NAME") 2> /dev/null" |
75 |
image_size=$($CURL -sI "$IMAGE_NAME" | grep ^Content-Length: | cut -d" " -f2) |
76 |
;; |
77 |
pithos) |
78 |
# For security reasons pass the database url to pithcat as an |
79 |
# environmental variable. |
80 |
export PITHCAT_INPUT_DB="$PITHOS_DB" |
81 |
export PITHCAT_INPUT_DATA="$PITHOS_DATA" |
82 |
cmd_args="$(printf "%q" "${IMAGE_NAME}")" |
83 |
image_cmd="./pithcat $cmd_args" |
84 |
image_size=$(./pithcat -s $cmd_args) |
85 |
;; |
86 |
esac |
87 |
|
88 |
# If the target device is not a real block device we'll first losetup it. |
89 |
# This is needed for file disks. |
90 |
if [ ! -b "$blockdev" ]; then |
91 |
original_blockdev="$blockdev" |
92 |
blockdev=$($LOSETUP -sf "$blockdev") |
93 |
add_cleanup $LOSETUP -d "$blockdev" |
94 |
fi |
95 |
|
96 |
case "$IMAGE_TYPE" in |
97 |
ntfsdump|extdump) |
98 |
# Create partitions |
99 |
format_disk0 "$blockdev" "$IMAGE_TYPE" |
100 |
|
101 |
# Install a new MBR |
102 |
$INSTALL_MBR -p 1 -i n "$blockdev" |
103 |
|
104 |
target="$(map_disk0 "$blockdev")-1" #the root device |
105 |
add_cleanup unmap_disk0 "$blockdev" |
106 |
snf_export_PROPERTY_ROOT_PARTITION=1 |
107 |
if [ "$IMAGE_TYPE" = "ntfsdump" ]; then |
108 |
snf_export_PROPERTY_OSFAMILY="windows" |
109 |
else |
110 |
snf_export_PROPERTY_OSFAMILY="linux" |
111 |
fi |
112 |
;; |
113 |
diskdump) |
114 |
target="$blockdev" |
115 |
;; |
116 |
*) |
117 |
log_error "Unknown Image format: \`$IMAGE_TYPE'" |
118 |
report_error "Unknown Image Format" |
119 |
exit 1 |
120 |
;; |
121 |
esac |
122 |
|
123 |
report_info "Starting image copy..." |
124 |
monitor="./copy-monitor.py -o $MONITOR_FD -r $image_size" |
125 |
if [ "$BACKEND_TYPE" = "local" ]; then |
126 |
# dd the dump to its new home :-) |
127 |
# Deploying an image file on a target block device is a streaming copy |
128 |
# operation. Enable the direct I/O flag on the output fd to avoid polluting |
129 |
# the host cache with useless data. |
130 |
$monitor dd bs=4M if="$image_file" of="$target" oflag=direct |
131 |
else |
132 |
$image_cmd | $monitor dd bs=4M of="$target" oflag=direct |
133 |
fi |
134 |
report_info "Image copy finished." |
135 |
|
136 |
# Create a floppy image |
137 |
floppy=$(mktemp --tmpdir floppy.XXXXXX) |
138 |
add_cleanup rm "$floppy" |
139 |
|
140 |
snf_export_TYPE="$IMG_FORMAT" |
141 |
snf_export_PASSWORD="$IMG_PASSWD" |
142 |
snf_export_HOSTNAME="$instance" |
143 |
if [ -n "$IMG_PROPERTIES" ]; then |
144 |
snf_export_PROPERTIES="$IMG_PROPERTIES" |
145 |
fi |
146 |
if [ -n "$IMG_PERSONALITY" ]; then |
147 |
snf_export_PERSONALITY="$IMG_PERSONALITY" |
148 |
fi |
149 |
|
150 |
snf_export_DEV=$(get_img_dev) |
151 |
|
152 |
create_floppy "$floppy" |
153 |
|
154 |
launch_helper "$blockdev" "$floppy" |
155 |
|
156 |
report_info "Image customization finished successfully." |
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 : |