Statistics
| Branch: | Revision:

root / common.sh.in @ deaae996

History | View | Annotate | Download (8.8 kB)

1 79224631 Lance Albertson
#
2 79224631 Lance Albertson
3 79224631 Lance Albertson
# Copyright (C) 2007, 2008, 2009 Google Inc.
4 79224631 Lance Albertson
#
5 79224631 Lance Albertson
# This program is free software; you can redistribute it and/or modify
6 79224631 Lance Albertson
# it under the terms of the GNU General Public License as published by
7 79224631 Lance Albertson
# the Free Software Foundation; either version 2 of the License, or
8 79224631 Lance Albertson
# (at your option) any later version.
9 79224631 Lance Albertson
#
10 79224631 Lance Albertson
# This program is distributed in the hope that it will be useful, but
11 79224631 Lance Albertson
# WITHOUT ANY WARRANTY; without even the implied warranty of
12 79224631 Lance Albertson
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 79224631 Lance Albertson
# General Public License for more details.
14 79224631 Lance Albertson
#
15 79224631 Lance Albertson
# You should have received a copy of the GNU General Public License
16 79224631 Lance Albertson
# along with this program; if not, write to the Free Software
17 79224631 Lance Albertson
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 79224631 Lance Albertson
# 02110-1301, USA.
19 79224631 Lance Albertson
20 8d0132dc Lance Albertson
AWK="@AWK@"
21 deaae996 Nikos Skalkotos
MKDIR_P="@MKDIR_P@"
22 8d0132dc Lance Albertson
DUMP="@DUMP@"
23 47dc6386 Nikos Skalkotos
RESTORE="@RESTORE@"
24 8d0132dc Lance Albertson
LOSETUP="@LOSETUP@"
25 8d0132dc Lance Albertson
KPARTX="@KPARTX@"
26 8d0132dc Lance Albertson
SFDISK="@SFDISK@"
27 ab712029 Lance Albertson
QEMU_IMG="@QEMU_IMG@"
28 970e7307 Nikos Skalkotos
NTFSCLONE="@NTFSCLONE@"
29 59aff5e7 Nikos Skalkotos
NTFSRESIZE="@NTFSRESIZE@"
30 47c86659 Nikos Skalkotos
XMLSTARLET="@XMLSTARLET@"
31 8afaedb3 Nikos Skalkotos
INSTALL_MBR="@INSTALL_MBR@"
32 47c86659 Nikos Skalkotos
33 47c86659 Nikos Skalkotos
@WINSUPPORT_TRUE@windows_support="yes"
34 8d0132dc Lance Albertson
35 79224631 Lance Albertson
CLEANUP=( )
36 79224631 Lance Albertson
37 79224631 Lance Albertson
log_error() {
38 79224631 Lance Albertson
  echo "$@" >&2
39 79224631 Lance Albertson
}
40 79224631 Lance Albertson
41 e7a2d1ad Lance Albertson
debug() {
42 9014a39d Lance Albertson
    [ "$IMAGE_DEBUG" == "1" -o "$IMAGE_DEBUG" == "yes" ] &&  $@ || :
43 e7a2d1ad Lance Albertson
}
44 e7a2d1ad Lance Albertson
45 79224631 Lance Albertson
get_api5_arguments() {
46 79224631 Lance Albertson
  GETOPT_RESULT=$*
47 79224631 Lance Albertson
  # Note the quotes around `$TEMP': they are essential!
48 79224631 Lance Albertson
  eval set -- "$GETOPT_RESULT"
49 79224631 Lance Albertson
  while true; do
50 79224631 Lance Albertson
    case "$1" in
51 79224631 Lance Albertson
      -i|-n) instance=$2; shift 2;;
52 79224631 Lance Albertson
53 79224631 Lance Albertson
      -o) old_name=$2; shift 2;;
54 79224631 Lance Albertson
55 79224631 Lance Albertson
      -b) blockdev=$2; shift 2;;
56 79224631 Lance Albertson
57 79224631 Lance Albertson
      -s) swapdev=$2; shift 2;;
58 79224631 Lance Albertson
59 79224631 Lance Albertson
      --) shift; break;;
60 79224631 Lance Albertson
61 79224631 Lance Albertson
      *)  log_error "Internal error!" >&2; exit 1;;
62 79224631 Lance Albertson
    esac
63 79224631 Lance Albertson
  done
64 79224631 Lance Albertson
  if [ -z "$instance" -o -z "$blockdev" ]; then
65 79224631 Lance Albertson
    log_error "Missing OS API Argument (-i, -n, or -b)"
66 79224631 Lance Albertson
    exit 1
67 79224631 Lance Albertson
  fi
68 79224631 Lance Albertson
  if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev"  ]; then
69 79224631 Lance Albertson
    log_error "Missing OS API Argument -s (swapdev)"
70 79224631 Lance Albertson
    exit 1
71 79224631 Lance Albertson
  fi
72 79224631 Lance Albertson
  if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name"  ]; then
73 79224631 Lance Albertson
    log_error "Missing OS API Argument -o (old_name)"
74 79224631 Lance Albertson
    exit 1
75 79224631 Lance Albertson
  fi
76 79224631 Lance Albertson
}
77 79224631 Lance Albertson
78 79224631 Lance Albertson
get_api10_arguments() {
79 79224631 Lance Albertson
  if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then
80 79224631 Lance Albertson
    log_error "Missing OS API Variable:"
81 79224631 Lance Albertson
    log_error "(INSTANCE_NAME HYPERVISOR or DISK_COUNT)"
82 79224631 Lance Albertson
    exit 1
83 79224631 Lance Albertson
  fi
84 79224631 Lance Albertson
  instance=$INSTANCE_NAME
85 79224631 Lance Albertson
  if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then
86 79224631 Lance Albertson
    log_error "At least one disk is needed"
87 79224631 Lance Albertson
    exit 1
88 79224631 Lance Albertson
  fi
89 79224631 Lance Albertson
  if [ "$SCRIPT_NAME" = "export" ]; then
90 79224631 Lance Albertson
    if [ -z "$EXPORT_DEVICE" ]; then
91 79224631 Lance Albertson
      log_error "Missing OS API Variable EXPORT_DEVICE"
92 79224631 Lance Albertson
    fi
93 79224631 Lance Albertson
    blockdev=$EXPORT_DEVICE
94 79224631 Lance Albertson
  elif [ "$SCRIPT_NAME" = "import" ]; then
95 79224631 Lance Albertson
    if [ -z "$IMPORT_DEVICE" ]; then
96 79224631 Lance Albertson
       log_error "Missing OS API Variable IMPORT_DEVICE"
97 79224631 Lance Albertson
    fi
98 79224631 Lance Albertson
    blockdev=$IMPORT_DEVICE
99 79224631 Lance Albertson
  else
100 79224631 Lance Albertson
    blockdev=$DISK_0_PATH
101 79224631 Lance Albertson
  fi
102 79224631 Lance Albertson
  if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then
103 79224631 Lance Albertson
    log_error "Missing OS API Variable OLD_INSTANCE_NAME"
104 79224631 Lance Albertson
  fi
105 79224631 Lance Albertson
  old_name=$OLD_INSTANCE_NAME
106 79224631 Lance Albertson
}
107 79224631 Lance Albertson
108 ab462591 Constantinos Venetsanopoulos
get_api20_parameters() {
109 8e5449f1 Constantinos Venetsanopoulos
  if [ -z "$OSP_IMG_ID" -o -z "$OSP_IMG_FORMAT" -o -z "$OSP_IMG_PASSWD" ]; then
110 ab462591 Constantinos Venetsanopoulos
    log_error "Missing OS API Parameter:"
111 8e5449f1 Constantinos Venetsanopoulos
    log_error "(OSP_IMG_ID or OSP_IMG_FORMAT or OSP_IMG_PASSWD)"
112 ab462591 Constantinos Venetsanopoulos
    exit 1
113 ab462591 Constantinos Venetsanopoulos
  fi
114 ab462591 Constantinos Venetsanopoulos
  IMG_ID=$OSP_IMG_ID
115 8e5449f1 Constantinos Venetsanopoulos
  IMG_FORMAT=$OSP_IMG_FORMAT
116 ab462591 Constantinos Venetsanopoulos
  IMG_PASSWD=$OSP_IMG_PASSWD
117 ab462591 Constantinos Venetsanopoulos
}
118 ab462591 Constantinos Venetsanopoulos
119 b05b1ab6 Lance Albertson
mount_disk0() {
120 77449e7c Lance Albertson
    local target=$1
121 3173a4bc Lance Albertson
    mount $root_dev $target
122 3173a4bc Lance Albertson
    CLEANUP+=("umount $target")
123 77449e7c Lance Albertson
    if [ -n "${boot_dev}" ] ; then
124 77449e7c Lance Albertson
        $MKDIR_P $target/boot
125 77449e7c Lance Albertson
        mount $boot_dev $target/boot
126 77449e7c Lance Albertson
        CLEANUP+=("umount $target/boot")
127 77449e7c Lance Albertson
    fi
128 1c571a34 Lance Albertson
    # sync the file systems before unmounting to ensure everything is flushed
129 1c571a34 Lance Albertson
    # out
130 1c571a34 Lance Albertson
    CLEANUP+=("sync")
131 b05b1ab6 Lance Albertson
}
132 b05b1ab6 Lance Albertson
133 79224631 Lance Albertson
map_disk0() {
134 77449e7c Lance Albertson
    blockdev="$1"
135 77449e7c Lance Albertson
    filesystem_dev_base=`$KPARTX -l -p- $blockdev | \
136 77449e7c Lance Albertson
                            grep -m 1 -- "-1.*$blockdev" | \
137 77449e7c Lance Albertson
                            $AWK '{print $1}'`
138 77449e7c Lance Albertson
    if [ -z "$filesystem_dev_base" ]; then
139 77449e7c Lance Albertson
        log_error "Cannot interpret kpartx output and get partition mapping"
140 77449e7c Lance Albertson
        exit 1
141 77449e7c Lance Albertson
    fi
142 77449e7c Lance Albertson
    $KPARTX -a -p- $blockdev > /dev/null
143 77449e7c Lance Albertson
    filesystem_dev="/dev/mapper/${filesystem_dev_base/%-1/}"
144 77449e7c Lance Albertson
    if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then
145 77449e7c Lance Albertson
        log_error "Can't find kpartx mapped partition: /dev/mapper/$filesystem_dev_base"
146 77449e7c Lance Albertson
        exit 1
147 77449e7c Lance Albertson
    fi
148 494927a8 Lance Albertson
    echo "$filesystem_dev"
149 494927a8 Lance Albertson
}
150 494927a8 Lance Albertson
151 494927a8 Lance Albertson
map_partition() {
152 494927a8 Lance Albertson
    filesystem_dev="$1"
153 494927a8 Lance Albertson
    partition="$2"
154 6738cd80 Nikos Skalkotos
    if [ "${SWAP}" = "yes" -a "${BOOT}" = "yes" ] ; then
155 494927a8 Lance Albertson
        boot_dev="${filesystem_dev}-1"
156 494927a8 Lance Albertson
        swap_dev="${filesystem_dev}-2"
157 494927a8 Lance Albertson
        root_dev="${filesystem_dev}-3"
158 6738cd80 Nikos Skalkotos
    elif [ "${SWAP}" = "no" -a "${BOOT}" = "yes" ] ; then
159 494927a8 Lance Albertson
        boot_dev="${filesystem_dev}-1"
160 494927a8 Lance Albertson
        root_dev="${filesystem_dev}-2"
161 6738cd80 Nikos Skalkotos
    elif [ "${SWAP}" = "yes" -a "${BOOT}" = "no" ] ; then
162 494927a8 Lance Albertson
        swap_dev="${filesystem_dev}-1"
163 494927a8 Lance Albertson
        root_dev="${filesystem_dev}-2"
164 6738cd80 Nikos Skalkotos
    elif [ "${SWAP}" = "no" -a "${BOOT}" = "no" ] ; then
165 494927a8 Lance Albertson
        root_dev="${filesystem_dev}-1"
166 77449e7c Lance Albertson
    fi
167 494927a8 Lance Albertson
    echo "$(eval "echo \${$(echo ${partition}_dev)"})"
168 79224631 Lance Albertson
}
169 79224631 Lance Albertson
170 79224631 Lance Albertson
unmap_disk0() {
171 8d0132dc Lance Albertson
  $KPARTX -d -p- $1
172 79224631 Lance Albertson
}
173 79224631 Lance Albertson
174 79224631 Lance Albertson
cleanup() {
175 31f11819 Nikos Skalkotos
  # if something fails here, it souldn't call cleanup again...
176 31f11819 Nikos Skalkotos
  trap - EXIT
177 31f11819 Nikos Skalkotos
178 79224631 Lance Albertson
  if [ ${#CLEANUP[*]} -gt 0 ]; then
179 79224631 Lance Albertson
    LAST_ELEMENT=$((${#CLEANUP[*]}-1))
180 79224631 Lance Albertson
    REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
181 79224631 Lance Albertson
    for i in $REVERSE_INDEXES; do
182 31f11819 Nikos Skalkotos
        # If something fails here, it's better to retry it for a few times
183 31f11819 Nikos Skalkotos
        # before we give up with an error. This is needed for kpartx when
184 31f11819 Nikos Skalkotos
        # dealing with ntfs partitions mounted through fuse. umount is not
185 31f11819 Nikos Skalkotos
        # synchronous and may return while the partition is still busy. A
186 31f11819 Nikos Skalkotos
        # premature attempt to delete partition mappings through kpartx on a
187 31f11819 Nikos Skalkotos
        # device that hosts previously mounted ntfs partition may fail with an 
188 31f11819 Nikos Skalkotos
        # `device-mapper: remove ioctl failed: Device or resource busy'
189 31f11819 Nikos Skalkotos
        # error. A sensible workaround for this is to wait for a while and then
190 31f11819 Nikos Skalkotos
        # try again.
191 31f11819 Nikos Skalkotos
        local cmd=${CLEANUP[$i]}
192 31f11819 Nikos Skalkotos
        $cmd || for interval in 0.25 0.5 1 2 4; do
193 31f11819 Nikos Skalkotos
            echo "Command $cmd failed!"
194 31f11819 Nikos Skalkotos
            echo "I'll wait for $interval secs and will retry..."
195 31f11819 Nikos Skalkotos
            sleep $interval
196 31f11819 Nikos Skalkotos
            $cmd && break
197 31f11819 Nikos Skalkotos
        done
198 31f11819 Nikos Skalkotos
        test $? -eq 1 && { echo "Giving Up..."; exit 1; }
199 79224631 Lance Albertson
    done
200 79224631 Lance Albertson
  fi
201 31f11819 Nikos Skalkotos
  echo "Clean UP executed"
202 79224631 Lance Albertson
}
203 79224631 Lance Albertson
204 79224631 Lance Albertson
trap cleanup EXIT
205 79224631 Lance Albertson
206 9cdb305c Lance Albertson
DEFAULT_FILE="@DEFAULT_DIR@/ganeti-instance-image"
207 79224631 Lance Albertson
if [ -f "$DEFAULT_FILE" ]; then
208 79224631 Lance Albertson
    . "$DEFAULT_FILE"
209 79224631 Lance Albertson
fi
210 79224631 Lance Albertson
211 79224631 Lance Albertson
# note: we don't set a default mirror since debian and ubuntu have
212 79224631 Lance Albertson
# different defaults, and it's better to use the default
213 79224631 Lance Albertson
214 79224631 Lance Albertson
# only if the user want to specify a mirror in the defaults file we
215 79224631 Lance Albertson
# will use it, this declaration is to make sure the variable is set
216 828670a1 Lance Albertson
: ${CDINSTALL:="no"}
217 6738cd80 Nikos Skalkotos
: ${BOOT:="no"}
218 e21b8802 Lance Albertson
: ${SWAP:="yes"}
219 fd786991 Lance Albertson
: ${SWAP_SIZE:="${INSTANCE_BE_memory}"}
220 4c96e490 Lance Albertson
: ${FILESYSTEM:="ext3"}
221 be751693 Lance Albertson
: ${KERNEL_ARGS=""}
222 84472880 Lance Albertson
: ${OVERLAY=""}
223 134c11c3 Lance Albertson
: ${IMAGE_NAME:=""}
224 be751693 Lance Albertson
: ${IMAGE_TYPE:="dump"}
225 8727c57d Lance Albertson
: ${NOMOUNT:="no"}
226 79224631 Lance Albertson
: ${ARCH:=""}
227 549118f0 Lance Albertson
: ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-image/hooks"}
228 f5aa7725 Lance Albertson
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/instance-image/variants"}
229 7193962d Lance Albertson
: ${NETWORKS_DIR:="@sysconfdir@/ganeti/instance-image/networks"}
230 84472880 Lance Albertson
: ${OVERLAYS_DIR:="@sysconfdir@/ganeti/instance-image/overlays"}
231 2e0988ec Lance Albertson
: ${IMAGE_DIR:="@localstatedir@/cache/ganeti-instance-image"}
232 9014a39d Lance Albertson
: ${IMAGE_DEBUG:="no"}
233 deaae996 Nikos Skalkotos
: ${TOOLS_DIR:="@osdir@/@osname@/tools"}
234 f7959feb Lance Albertson
235 79224631 Lance Albertson
SCRIPT_NAME=$(basename $0)
236 77449e7c Lance Albertson
KERNEL_PATH="$INSTANCE_HV_kernel_path"
237 79224631 Lance Albertson
238 103d261e Lance Albertson
if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
239 93fa6b1e Lance Albertson
    VOL_ID="/sbin/blkid -c /dev/null -o value -s UUID"
240 93fa6b1e Lance Albertson
    VOL_TYPE="/sbin/blkid -c /dev/null -o value -s TYPE"
241 103d261e Lance Albertson
else
242 103d261e Lance Albertson
    for dir in /lib/udev /sbin; do
243 103d261e Lance Albertson
        if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
244 103d261e Lance Albertson
            VOL_ID="$dir/vol_id -u"
245 103d261e Lance Albertson
            VOL_TYPE="$dir/vol_id -t"
246 103d261e Lance Albertson
        fi
247 103d261e Lance Albertson
    done
248 103d261e Lance Albertson
fi
249 103d261e Lance Albertson
250 103d261e Lance Albertson
if [ -z "$VOL_ID" ]; then
251 103d261e Lance Albertson
    log_error "vol_id or blkid not found, please install udev or util-linux"
252 103d261e Lance Albertson
    exit 1
253 103d261e Lance Albertson
fi
254 103d261e Lance Albertson
255 103d261e Lance Albertson
256 79224631 Lance Albertson
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
257 79224631 Lance Albertson
  OS_API_VERSION=5
258 79224631 Lance Albertson
  GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
259 79224631 Lance Albertson
  if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
260 79224631 Lance Albertson
  get_api5_arguments $GETOPT_RESULT
261 79224631 Lance Albertson
elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
262 79224631 Lance Albertson
  get_api10_arguments
263 ab462591 Constantinos Venetsanopoulos
elif [ "$OS_API_VERSION" = "20" ]; then
264 ab462591 Constantinos Venetsanopoulos
  get_api10_arguments
265 ab462591 Constantinos Venetsanopoulos
  get_api20_parameters
266 ab462591 Constantinos Venetsanopoulos
  IMAGE_NAME=$IMG_ID
267 a33d2ab2 Nikos Skalkotos
  IMAGE_TYPE=$IMG_FORMAT
268 79224631 Lance Albertson
else
269 79224631 Lance Albertson
  log_error "Unknown OS API VERSION $OS_API_VERSION"
270 79224631 Lance Albertson
  exit 1
271 79224631 Lance Albertson
fi
272 79224631 Lance Albertson
273 79224631 Lance Albertson
if [ -n "$OS_VARIANT" ]; then
274 79224631 Lance Albertson
  if [ ! -d "$VARIANTS_DIR" ]; then
275 79224631 Lance Albertson
    log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
276 79224631 Lance Albertson
    exit 1
277 79224631 Lance Albertson
  fi
278 79224631 Lance Albertson
  VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
279 79224631 Lance Albertson
  if [ -f "$VARIANT_CONFIG" ]; then
280 79224631 Lance Albertson
    . "$VARIANT_CONFIG"
281 79224631 Lance Albertson
  else
282 79224631 Lance Albertson
    if grep -qxF "$OS_VARIANT" variants.list; then
283 f5aa7725 Lance Albertson
      log_error "ERROR: instance-image configuration error"
284 79224631 Lance Albertson
      log_error "  Published variant $OS_VARIANT is missing its config file"
285 79224631 Lance Albertson
      log_error "  Please create $VARIANT_CONFIG or unpublish the variant"
286 79224631 Lance Albertson
      log_error "  (by removing $OS_VARIANT from variants.list)"
287 79224631 Lance Albertson
    else
288 79224631 Lance Albertson
      log_error "Unofficial variant $OS_VARIANT is unsupported"
289 79224631 Lance Albertson
      log_error "Most probably this is a user error, forcing a wrong name"
290 79224631 Lance Albertson
      log_error "To support this variant please create file $VARIANT_CONFIG"
291 79224631 Lance Albertson
    fi
292 79224631 Lance Albertson
    exit 1
293 79224631 Lance Albertson
  fi
294 79224631 Lance Albertson
fi