Statistics
| Branch: | Revision:

root / common.sh.in @ 47c86659

History | View | Annotate | Download (8.7 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
DUMP="@DUMP@"
22
LOSETUP="@LOSETUP@"
23
KPARTX="@KPARTX@"
24
SFDISK="@SFDISK@"
25
QEMU_IMG="@QEMU_IMG@"
26
MKDIR_P="@MKDIR_P@"
27
XMLSTARLET="@XMLSTARLET@"
28

    
29
@WINSUPPORT_TRUE@windows_support="yes"
30

    
31
CLEANUP=( )
32

    
33
log_error() {
34
  echo "$@" >&2
35
}
36

    
37
debug() {
38
    [ "$IMAGE_DEBUG" == "1" -o "$IMAGE_DEBUG" == "yes" ] &&  $@ || :
39
}
40

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

    
49
      -o) old_name=$2; shift 2;;
50

    
51
      -b) blockdev=$2; shift 2;;
52

    
53
      -s) swapdev=$2; shift 2;;
54

    
55
      --) shift; break;;
56

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

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

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

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

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

    
147
map_partition() {
148
    filesystem_dev="$1"
149
    partition="$2"
150
    if [ "${SWAP}" = "yes" -a -z "${KERNEL_PATH}" ] ; then
151
        boot_dev="${filesystem_dev}-1"
152
        swap_dev="${filesystem_dev}-2"
153
        root_dev="${filesystem_dev}-3"
154
    elif [ "${SWAP}" = "no" -a -z "${KERNEL_PATH}" ] ; then
155
        boot_dev="${filesystem_dev}-1"
156
        root_dev="${filesystem_dev}-2"
157
    elif [ "${SWAP}" = "yes" -a -n "${KERNEL_PATH}" ] ; then
158
        swap_dev="${filesystem_dev}-1"
159
        root_dev="${filesystem_dev}-2"
160
    elif [ "${SWAP}" = "no" -a -n "${KERNEL_PATH}" ] ; then
161
        root_dev="${filesystem_dev}-1"
162
    fi
163
    echo "$(eval "echo \${$(echo ${partition}_dev)"})"
164
}
165

    
166
unmap_disk0() {
167
  $KPARTX -d -p- $1
168
}
169

    
170
cleanup() {
171
  # if something fails here, it souldn't call cleanup again...
172
  trap - EXIT
173

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

    
200
trap cleanup EXIT
201

    
202
DEFAULT_FILE="@DEFAULT_DIR@/ganeti-instance-image"
203
if [ -f "$DEFAULT_FILE" ]; then
204
    . "$DEFAULT_FILE"
205
fi
206

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

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

    
230
SCRIPT_NAME=$(basename $0)
231
KERNEL_PATH="$INSTANCE_HV_kernel_path"
232

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

    
245
if [ -z "$VOL_ID" ]; then
246
    log_error "vol_id or blkid not found, please install udev or util-linux"
247
    exit 1
248
fi
249

    
250

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

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