Statistics
| Branch: | Revision:

root / common.sh.in @ deaae996

History | View | Annotate | Download (8.8 kB)

1
#
2

    
3
# Copyright (C) 2007, 2008, 2009 Google Inc.
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
AWK="@AWK@"
21
MKDIR_P="@MKDIR_P@"
22
DUMP="@DUMP@"
23
RESTORE="@RESTORE@"
24
LOSETUP="@LOSETUP@"
25
KPARTX="@KPARTX@"
26
SFDISK="@SFDISK@"
27
QEMU_IMG="@QEMU_IMG@"
28
NTFSCLONE="@NTFSCLONE@"
29
NTFSRESIZE="@NTFSRESIZE@"
30
XMLSTARLET="@XMLSTARLET@"
31
INSTALL_MBR="@INSTALL_MBR@"
32

    
33
@WINSUPPORT_TRUE@windows_support="yes"
34

    
35
CLEANUP=( )
36

    
37
log_error() {
38
  echo "$@" >&2
39
}
40

    
41
debug() {
42
    [ "$IMAGE_DEBUG" == "1" -o "$IMAGE_DEBUG" == "yes" ] &&  $@ || :
43
}
44

    
45
get_api5_arguments() {
46
  GETOPT_RESULT=$*
47
  # Note the quotes around `$TEMP': they are essential!
48
  eval set -- "$GETOPT_RESULT"
49
  while true; do
50
    case "$1" in
51
      -i|-n) instance=$2; shift 2;;
52

    
53
      -o) old_name=$2; shift 2;;
54

    
55
      -b) blockdev=$2; shift 2;;
56

    
57
      -s) swapdev=$2; shift 2;;
58

    
59
      --) shift; break;;
60

    
61
      *)  log_error "Internal error!" >&2; exit 1;;
62
    esac
63
  done
64
  if [ -z "$instance" -o -z "$blockdev" ]; then
65
    log_error "Missing OS API Argument (-i, -n, or -b)"
66
    exit 1
67
  fi
68
  if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev"  ]; then
69
    log_error "Missing OS API Argument -s (swapdev)"
70
    exit 1
71
  fi
72
  if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name"  ]; then
73
    log_error "Missing OS API Argument -o (old_name)"
74
    exit 1
75
  fi
76
}
77

    
78
get_api10_arguments() {
79
  if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then
80
    log_error "Missing OS API Variable:"
81
    log_error "(INSTANCE_NAME HYPERVISOR or DISK_COUNT)"
82
    exit 1
83
  fi
84
  instance=$INSTANCE_NAME
85
  if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then
86
    log_error "At least one disk is needed"
87
    exit 1
88
  fi
89
  if [ "$SCRIPT_NAME" = "export" ]; then
90
    if [ -z "$EXPORT_DEVICE" ]; then
91
      log_error "Missing OS API Variable EXPORT_DEVICE"
92
    fi
93
    blockdev=$EXPORT_DEVICE
94
  elif [ "$SCRIPT_NAME" = "import" ]; then
95
    if [ -z "$IMPORT_DEVICE" ]; then
96
       log_error "Missing OS API Variable IMPORT_DEVICE"
97
    fi
98
    blockdev=$IMPORT_DEVICE
99
  else
100
    blockdev=$DISK_0_PATH
101
  fi
102
  if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then
103
    log_error "Missing OS API Variable OLD_INSTANCE_NAME"
104
  fi
105
  old_name=$OLD_INSTANCE_NAME
106
}
107

    
108
get_api20_parameters() {
109
  if [ -z "$OSP_IMG_ID" -o -z "$OSP_IMG_FORMAT" -o -z "$OSP_IMG_PASSWD" ]; then
110
    log_error "Missing OS API Parameter:"
111
    log_error "(OSP_IMG_ID or OSP_IMG_FORMAT or OSP_IMG_PASSWD)"
112
    exit 1
113
  fi
114
  IMG_ID=$OSP_IMG_ID
115
  IMG_FORMAT=$OSP_IMG_FORMAT
116
  IMG_PASSWD=$OSP_IMG_PASSWD
117
}
118

    
119
mount_disk0() {
120
    local target=$1
121
    mount $root_dev $target
122
    CLEANUP+=("umount $target")
123
    if [ -n "${boot_dev}" ] ; then
124
        $MKDIR_P $target/boot
125
        mount $boot_dev $target/boot
126
        CLEANUP+=("umount $target/boot")
127
    fi
128
    # sync the file systems before unmounting to ensure everything is flushed
129
    # out
130
    CLEANUP+=("sync")
131
}
132

    
133
map_disk0() {
134
    blockdev="$1"
135
    filesystem_dev_base=`$KPARTX -l -p- $blockdev | \
136
                            grep -m 1 -- "-1.*$blockdev" | \
137
                            $AWK '{print $1}'`
138
    if [ -z "$filesystem_dev_base" ]; then
139
        log_error "Cannot interpret kpartx output and get partition mapping"
140
        exit 1
141
    fi
142
    $KPARTX -a -p- $blockdev > /dev/null
143
    filesystem_dev="/dev/mapper/${filesystem_dev_base/%-1/}"
144
    if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then
145
        log_error "Can't find kpartx mapped partition: /dev/mapper/$filesystem_dev_base"
146
        exit 1
147
    fi
148
    echo "$filesystem_dev"
149
}
150

    
151
map_partition() {
152
    filesystem_dev="$1"
153
    partition="$2"
154
    if [ "${SWAP}" = "yes" -a "${BOOT}" = "yes" ] ; then
155
        boot_dev="${filesystem_dev}-1"
156
        swap_dev="${filesystem_dev}-2"
157
        root_dev="${filesystem_dev}-3"
158
    elif [ "${SWAP}" = "no" -a "${BOOT}" = "yes" ] ; then
159
        boot_dev="${filesystem_dev}-1"
160
        root_dev="${filesystem_dev}-2"
161
    elif [ "${SWAP}" = "yes" -a "${BOOT}" = "no" ] ; then
162
        swap_dev="${filesystem_dev}-1"
163
        root_dev="${filesystem_dev}-2"
164
    elif [ "${SWAP}" = "no" -a "${BOOT}" = "no" ] ; then
165
        root_dev="${filesystem_dev}-1"
166
    fi
167
    echo "$(eval "echo \${$(echo ${partition}_dev)"})"
168
}
169

    
170
unmap_disk0() {
171
  $KPARTX -d -p- $1
172
}
173

    
174
cleanup() {
175
  # if something fails here, it souldn't call cleanup again...
176
  trap - EXIT
177

    
178
  if [ ${#CLEANUP[*]} -gt 0 ]; then
179
    LAST_ELEMENT=$((${#CLEANUP[*]}-1))
180
    REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
181
    for i in $REVERSE_INDEXES; do
182
        # If something fails here, it's better to retry it for a few times
183
        # before we give up with an error. This is needed for kpartx when
184
        # dealing with ntfs partitions mounted through fuse. umount is not
185
        # synchronous and may return while the partition is still busy. A
186
        # premature attempt to delete partition mappings through kpartx on a
187
        # device that hosts previously mounted ntfs partition may fail with an 
188
        # `device-mapper: remove ioctl failed: Device or resource busy'
189
        # error. A sensible workaround for this is to wait for a while and then
190
        # try again.
191
        local cmd=${CLEANUP[$i]}
192
        $cmd || for interval in 0.25 0.5 1 2 4; do
193
            echo "Command $cmd failed!"
194
            echo "I'll wait for $interval secs and will retry..."
195
            sleep $interval
196
            $cmd && break
197
        done
198
        test $? -eq 1 && { echo "Giving Up..."; exit 1; }
199
    done
200
  fi
201
  echo "Clean UP executed"
202
}
203

    
204
trap cleanup EXIT
205

    
206
DEFAULT_FILE="@DEFAULT_DIR@/ganeti-instance-image"
207
if [ -f "$DEFAULT_FILE" ]; then
208
    . "$DEFAULT_FILE"
209
fi
210

    
211
# note: we don't set a default mirror since debian and ubuntu have
212
# different defaults, and it's better to use the default
213

    
214
# only if the user want to specify a mirror in the defaults file we
215
# will use it, this declaration is to make sure the variable is set
216
: ${CDINSTALL:="no"}
217
: ${BOOT:="no"}
218
: ${SWAP:="yes"}
219
: ${SWAP_SIZE:="${INSTANCE_BE_memory}"}
220
: ${FILESYSTEM:="ext3"}
221
: ${KERNEL_ARGS=""}
222
: ${OVERLAY=""}
223
: ${IMAGE_NAME:=""}
224
: ${IMAGE_TYPE:="dump"}
225
: ${NOMOUNT:="no"}
226
: ${ARCH:=""}
227
: ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-image/hooks"}
228
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/instance-image/variants"}
229
: ${NETWORKS_DIR:="@sysconfdir@/ganeti/instance-image/networks"}
230
: ${OVERLAYS_DIR:="@sysconfdir@/ganeti/instance-image/overlays"}
231
: ${IMAGE_DIR:="@localstatedir@/cache/ganeti-instance-image"}
232
: ${IMAGE_DEBUG:="no"}
233
: ${TOOLS_DIR:="@osdir@/@osname@/tools"}
234

    
235
SCRIPT_NAME=$(basename $0)
236
KERNEL_PATH="$INSTANCE_HV_kernel_path"
237

    
238
if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
239
    VOL_ID="/sbin/blkid -c /dev/null -o value -s UUID"
240
    VOL_TYPE="/sbin/blkid -c /dev/null -o value -s TYPE"
241
else
242
    for dir in /lib/udev /sbin; do
243
        if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
244
            VOL_ID="$dir/vol_id -u"
245
            VOL_TYPE="$dir/vol_id -t"
246
        fi
247
    done
248
fi
249

    
250
if [ -z "$VOL_ID" ]; then
251
    log_error "vol_id or blkid not found, please install udev or util-linux"
252
    exit 1
253
fi
254

    
255

    
256
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
257
  OS_API_VERSION=5
258
  GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
259
  if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
260
  get_api5_arguments $GETOPT_RESULT
261
elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
262
  get_api10_arguments
263
elif [ "$OS_API_VERSION" = "20" ]; then
264
  get_api10_arguments
265
  get_api20_parameters
266
  IMAGE_NAME=$IMG_ID
267
  IMAGE_TYPE=$IMG_FORMAT
268
else
269
  log_error "Unknown OS API VERSION $OS_API_VERSION"
270
  exit 1
271
fi
272

    
273
if [ -n "$OS_VARIANT" ]; then
274
  if [ ! -d "$VARIANTS_DIR" ]; then
275
    log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
276
    exit 1
277
  fi
278
  VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
279
  if [ -f "$VARIANT_CONFIG" ]; then
280
    . "$VARIANT_CONFIG"
281
  else
282
    if grep -qxF "$OS_VARIANT" variants.list; then
283
      log_error "ERROR: instance-image configuration error"
284
      log_error "  Published variant $OS_VARIANT is missing its config file"
285
      log_error "  Please create $VARIANT_CONFIG or unpublish the variant"
286
      log_error "  (by removing $OS_VARIANT from variants.list)"
287
    else
288
      log_error "Unofficial variant $OS_VARIANT is unsupported"
289
      log_error "Most probably this is a user error, forcing a wrong name"
290
      log_error "To support this variant please create file $VARIANT_CONFIG"
291
    fi
292
    exit 1
293
  fi
294
fi
295