Statistics
| Branch: | Revision:

root / common.sh.in @ 970e7307

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
NTFSCLONE="@NTFSCLONE@"
28
XMLSTARLET="@XMLSTARLET@"
29

    
30
@WINSUPPORT_TRUE@windows_support="yes"
31

    
32
CLEANUP=( )
33

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

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

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

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

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

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

    
56
      --) shift; break;;
57

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

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

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

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

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

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

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

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

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

    
201
trap cleanup EXIT
202

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

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

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

    
232
SCRIPT_NAME=$(basename $0)
233
KERNEL_PATH="$INSTANCE_HV_kernel_path"
234

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

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

    
252

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

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