Statistics
| Branch: | Revision:

root / common.sh.in @ 31f11819

History | View | Annotate | Download (8.6 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

    
28
CLEANUP=( )
29

    
30
log_error() {
31
  echo "$@" >&2
32
}
33

    
34
debug() {
35
    [ "$IMAGE_DEBUG" == "1" -o "$IMAGE_DEBUG" == "yes" ] &&  $@ || :
36
}
37

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

    
46
      -o) old_name=$2; shift 2;;
47

    
48
      -b) blockdev=$2; shift 2;;
49

    
50
      -s) swapdev=$2; shift 2;;
51

    
52
      --) shift; break;;
53

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

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

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

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

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

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

    
163
unmap_disk0() {
164
  $KPARTX -d -p- $1
165
}
166

    
167
cleanup() {
168
  # if something fails here, it souldn't call cleanup again...
169
  trap - EXIT
170

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

    
197
trap cleanup EXIT
198

    
199
DEFAULT_FILE="@DEFAULT_DIR@/ganeti-instance-image"
200
if [ -f "$DEFAULT_FILE" ]; then
201
    . "$DEFAULT_FILE"
202
fi
203

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

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

    
227
SCRIPT_NAME=$(basename $0)
228
KERNEL_PATH="$INSTANCE_HV_kernel_path"
229

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

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

    
247

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

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