Statistics
| Branch: | Revision:

root / common.sh.in @ 171aea62

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

    
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
get_os_type() {
113
    target=$1
114
    if [ -z "${target}" ] ; then
115
        log_error "target is not set in get_os_type"
116
        exit 1
117
    fi
118
    if [ -e ${target}/etc/redhat-release ] ; then
119
        OS_TYPE="redhat"
120
    elif [ -e ${target}/etc/debian_version ] ; then
121
        OS_TYPE="debian"
122
    elif [ -e ${target}/etc/gentoo-release ] ; then
123
        OS_TYPE="gentoo"
124
    elif [ -e ${target}/etc/SuSE-release ] ; then
125
        OS_TYPE="suse"
126
    fi
127
}
128

    
129
get_os() {
130
    target=$1
131
    if [ -z "${target}" ] ; then
132
        log_error "target is not set in get_os"
133
        exit 1
134
    fi
135
    lsb="/usr/bin/lsb_release"
136
    if [ -e ${target}/$lsb ] ; then
137
        OPERATING_SYSTEM="$(chroot ${target} ${lsb} -i -s | tr "[:upper:]" "[:lower:]")"
138
    elif [ -e ${target}/etc/debian_version ] ; then
139
        OPERATING_SYSTEM="debian"
140
    elif [ -e ${target}/etc/gentoo-release ] ; then
141
        OPERATING_SYSTEM="gentoo"
142
    elif [ -e ${target}/etc/fedora-release ] ; then
143
        OPERATING_SYSTEM="fedora"
144
    elif [ -e ${target}/etc/redhat-release ] ; then
145
        if [ -n "$(grep -i centos ${target}/etc/redhat-release)" ] ; then
146
            OPERATING_SYSTEM="centos"
147
        else
148
            OPERATING_SYSTEM="redhat"
149
        fi
150
    fi
151
}
152

    
153
get_os_release() {
154
    target=$1
155
    if [ -z "${target}" ] ; then
156
        log_error "target is not set in get_os_release"
157
        exit 1
158
    fi
159
    lsb="/usr/bin/lsb_release"
160
    if [ -e ${target}/$lsb ] ; then
161
        OS_RELEASE="$(chroot ${target} ${lsb} -r -s | tr "[:upper:]" "[:lower:]")"
162
    elif [ -e ${target}/etc/debian_version ] ; then
163
        OS_RELEASE="$(cat ${target}/etc/debian_version)"
164
    elif [ -e ${target}/etc/fedora-release ] ; then
165
        OS_RELEASE="$(cat ${target}/etc/fedora-release | awk '{print $3}')"
166
    elif [ -e ${$target}/etc/redhat-release ] ; then
167
        OS_RELEASE="$(cat ${target}/etc/redhat-release | awk '{print $3}')"
168
    fi
169
}
170

    
171
format_disk0() {
172
    local sfdisk_cmd="$SFDISK -uM -H 255 -S 63 --quiet --Linux --DOS $1"
173
    if [  "${SWAP}" = "yes" -a -z "${KERNEL_PATH}" ] ; then
174
        # Create three partitions:
175
        # 1 - 100MB /boot, bootable
176
        # 2 - Size of Memory, swap
177
        # 3 - Rest
178
        $sfdisk_cmd > /dev/null <<EOF
179
,100,L,*
180
,${SWAP_SIZE},S
181
,,L
182
EOF
183
    elif [  "${SWAP}" = "no" -a -z "${KERNEL_PATH}" ] ; then
184
        # Create two partitions:
185
        # 1 - 100MB /boot, bootable
186
        # 2 - Rest
187
        $sfdisk_cmd > /dev/null <<EOF
188
,100,L,*
189
,,L
190
EOF
191
    elif [  "${SWAP}" = "yes" -a -n "${KERNEL_PATH}" ] ; then
192
        # Create two partitions:
193
        # 1 - Size of Memory, swap
194
        # 2 - Rest
195
        $sfdisk_cmd > /dev/null <<EOF
196
,$SWAP_SIZE,S
197
,,L
198
EOF
199
    elif [  "${SWAP}" = "no" -a -n "${KERNEL_PATH}" ] ; then
200
        # Create two partitions:
201
        # 1 - Whole
202
        $sfdisk_cmd > /dev/null <<EOF
203
,,L
204
EOF
205
    fi
206
}
207

    
208
mkfs_disk0() {
209
    local mkfs="mkfs.${FILESYSTEM}"
210
    # Format /
211
    $mkfs -Fq -L / $root_dev > /dev/null
212
    # Format /boot
213
    if [ -n "${boot_dev}" ] ; then
214
        $mkfs -Fq -L /boot $boot_dev > /dev/null
215
    fi
216
    # Format swap
217
    if [ -n "${swap_dev}" ] ; then
218
        # Format swap
219
        mkswap -f $swap_dev > /dev/null
220
    fi
221
    # During reinstalls, ext4 needs a little time after a mkfs so add it here
222
    # and also run a sync to be sure.
223
    sync
224
    sleep 2
225
}
226

    
227
mount_disk0() {
228
    local target=$1
229
    mount $root_dev $target
230
    CLEANUP+=("umount $target")
231
    if [ -n "${boot_dev}" ] ; then
232
        $MKDIR_P $target/boot
233
        mount $boot_dev $target/boot
234
        CLEANUP+=("umount $target/boot")
235
    fi
236
    # sync the file systems before unmounting to ensure everything is flushed
237
    # out
238
    CLEANUP+=("sync")
239
}
240

    
241
map_disk0() {
242
    blockdev="$1"
243
    filesystem_dev_base=`$KPARTX -l -p- $blockdev | \
244
                            grep -m 1 -- "-1.*$blockdev" | \
245
                            $AWK '{print $1}'`
246
    if [ -z "$filesystem_dev_base" ]; then
247
        log_error "Cannot interpret kpartx output and get partition mapping"
248
        exit 1
249
    fi
250
    $KPARTX -a -p- $blockdev > /dev/null
251
    filesystem_dev="/dev/mapper/${filesystem_dev_base/%-1/}"
252
    if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then
253
        log_error "Can't find kpartx mapped partition: /dev/mapper/$filesystem_dev_base"
254
        exit 1
255
    fi
256
    echo "$filesystem_dev"
257
}
258

    
259
map_partition() {
260
    filesystem_dev="$1"
261
    partition="$2"
262
    if [ "${SWAP}" = "yes" -a -z "${KERNEL_PATH}" ] ; then
263
        boot_dev="${filesystem_dev}-1"
264
        swap_dev="${filesystem_dev}-2"
265
        root_dev="${filesystem_dev}-3"
266
    elif [ "${SWAP}" = "no" -a -z "${KERNEL_PATH}" ] ; then
267
        boot_dev="${filesystem_dev}-1"
268
        root_dev="${filesystem_dev}-2"
269
    elif [ "${SWAP}" = "yes" -a -n "${KERNEL_PATH}" ] ; then
270
        swap_dev="${filesystem_dev}-1"
271
        root_dev="${filesystem_dev}-2"
272
    elif [ "${SWAP}" = "no" -a -n "${KERNEL_PATH}" ] ; then
273
        root_dev="${filesystem_dev}-1"
274
    fi
275
    echo "$(eval "echo \${$(echo ${partition}_dev)"})"
276
}
277

    
278
unmap_disk0() {
279
  $KPARTX -d -p- $1
280
}
281

    
282
setup_fstab() {
283
    local target=$1 fs=${FILESYSTEM}
284
    get_os_type $target
285
    cat > $target/etc/fstab <<EOF
286
# /etc/fstab: static file system information.
287
#
288
# <file system>   <mount point>   <type>  <options>       <dump>  <pass>
289
UUID=$root_uuid   /               $fs     defaults        0       1
290
proc              /proc           proc    defaults        0       0
291
EOF
292

    
293
if [ -n "$boot_dev" -a -n "$boot_uuid" ] ; then
294
    cat >> $target/etc/fstab <<EOF
295
UUID=$boot_uuid   /boot           $fs     defaults        1       2
296
EOF
297
fi
298

    
299
if [ -n "$swap_dev" -a -n "$swap_uuid" ] ; then
300
    cat >> $target/etc/fstab <<EOF
301
UUID=$swap_uuid   swap            swap    defaults        0       0
302
EOF
303
fi
304

    
305
# OS Specific fstabs
306
if [ "$OS_TYPE" = "redhat" ] ; then
307
    cat >> $target/etc/fstab <<EOF
308
tmpfs             /dev/shm        tmpfs   defaults        0       0
309
devpts            /dev/pts        devpts  gid=5,mode=620  0       0
310
sysfs             /sys            sysfs   defaults        0       0
311
EOF
312
fi
313

    
314
if [ "$OS_TYPE" = "gentoo" ] ; then
315
    cat >> $target/etc/fstab <<EOF
316
shm               /dev/shm        tmpfs   nodev,nosuid,noexec 0   0
317
EOF
318
fi
319
}
320

    
321
setup_console() {
322
    local target=$1
323
    if [ -z "$target" ] ; then
324
        log_error "target not set for setup_console"
325
        exit 1
326
    fi
327
    # Upstart is on this system, so do this instead
328
    if [ -e ${target}/etc/event.d/tty1 ] ; then
329
        cat ${target}/etc/event.d/tty1 | sed -re 's/tty1/ttyS0/' \
330
            > ${target}/etc/event.d/ttyS0
331
        return
332
    fi
333
    # upstart in karmic and newer
334
    if [ -e ${target}/etc/init/tty1.conf ] ; then
335
        cat ${target}/etc/init/tty1.conf | \
336
        sed -re 's/^exec.*/exec \/sbin\/getty -L 115200 ttyS0 vt102/' \
337
            > ${target}/etc/init/ttyS0.conf
338
        sed -ie 's/tty1/ttyS0/g' ${target}/etc/init/ttyS0.conf
339
        return
340
    fi
341
    get_os $target
342
    case $OPERATING_SYSTEM in
343
        gentoo)
344
            sed -i -e 's/.*ttyS0.*/s0:12345:respawn:\/sbin\/agetty 115200 ttyS0 vt100/' \
345
                ${target}/etc/inittab
346
            ;;
347
        centos)
348
            echo "s0:12345:respawn:/sbin/agetty 115200 ttyS0 vt100" >> \
349
                ${target}/etc/inittab
350
            ;;
351
        debian|ubuntu)
352
            sed -i -e 's/.*T0.*/T0:23:respawn:\/sbin\/getty -L ttyS0 115200 vt100/' \
353
                ${target}/etc/inittab
354
            ;;
355
        *)
356
            echo "No support for your OS in instance-image, skipping..."
357
            ;;
358
    esac
359
}
360

    
361
filesystem_check() {
362
    local target=$1
363
    if [ -z "$target" ] ; then
364
        log_error "target not set for filesystem_check"
365
        exit 1
366
    fi
367

    
368
    get_os $target
369

    
370
    if [ "${OPERATING_SYSTEM}" = "fedora" ]; then
371
        # we have to force a filesystem relabeling for SELinux after messing
372
        # around with the filesystem in fedora
373
        echo "Enforce an automatic relabeling in the initial boot process..."
374
        touch $target/.autorelabel
375
    fi
376
}
377

    
378
cleanup() {
379
  # if something fails here, it souldn't call cleanup again...
380
  trap - EXIT
381

    
382
  if [ ${#CLEANUP[*]} -gt 0 ]; then
383
    LAST_ELEMENT=$((${#CLEANUP[*]}-1))
384
    REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
385
    for i in $REVERSE_INDEXES; do
386
        # If something fails here, it's better to retry it for a few times
387
        # before we give up with an error. This is needed for kpartx when
388
        # dealing with ntfs partitions mounted through fuse. umount is not
389
        # synchronous and may return while the partition is still busy. A
390
        # premature attempt to delete partition mappings through kpartx on a
391
        # device that hosts previously mounted ntfs partition may fail with an 
392
        # `device-mapper: remove ioctl failed: Device or resource busy'
393
        # error. A sensible workaround for this is to wait for a while and then
394
        # try again.
395
        local cmd=${CLEANUP[$i]}
396
        $cmd || for interval in 0.25 0.5 1 2 4; do
397
            echo "Command $cmd failed!"
398
            echo "I'll wait for $interval secs and will retry..."
399
            sleep $interval
400
            $cmd && break
401
        done
402
        test $? -eq 1 && { echo "Giving Up..."; exit 1; }
403
    done
404
  fi
405
  echo "Clean UP executed"
406
}
407

    
408
trap cleanup EXIT
409

    
410
DEFAULT_FILE="@DEFAULT_DIR@/ganeti-instance-image"
411
if [ -f "$DEFAULT_FILE" ]; then
412
    . "$DEFAULT_FILE"
413
fi
414

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

    
418
# only if the user want to specify a mirror in the defaults file we
419
# will use it, this declaration is to make sure the variable is set
420
: ${CDINSTALL:="no"}
421
: ${SWAP:="yes"}
422
: ${SWAP_SIZE:="${INSTANCE_BE_memory}"}
423
: ${FILESYSTEM:="ext3"}
424
: ${KERNEL_ARGS=""}
425
: ${OVERLAY=""}
426
: ${IMAGE_NAME:=""}
427
: ${IMAGE_TYPE:="dump"}
428
: ${NOMOUNT:="no"}
429
: ${ARCH:=""}
430
: ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-image/hooks"}
431
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/instance-image/variants"}
432
: ${NETWORKS_DIR:="@sysconfdir@/ganeti/instance-image/networks"}
433
: ${OVERLAYS_DIR:="@sysconfdir@/ganeti/instance-image/overlays"}
434
: ${IMAGE_DIR:="@localstatedir@/cache/ganeti-instance-image"}
435
: ${IMAGE_DEBUG:="no"}
436
: ${TOOLS_DIR:="@OS_DIR@/@OS_NAME@/tools"}
437

    
438
SCRIPT_NAME=$(basename $0)
439
KERNEL_PATH="$INSTANCE_HV_kernel_path"
440

    
441
if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
442
    VOL_ID="/sbin/blkid -c /dev/null -o value -s UUID"
443
    VOL_TYPE="/sbin/blkid -c /dev/null -o value -s TYPE"
444
else
445
    for dir in /lib/udev /sbin; do
446
        if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
447
            VOL_ID="$dir/vol_id -u"
448
            VOL_TYPE="$dir/vol_id -t"
449
        fi
450
    done
451
fi
452

    
453
if [ -z "$VOL_ID" ]; then
454
    log_error "vol_id or blkid not found, please install udev or util-linux"
455
    exit 1
456
fi
457

    
458

    
459
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
460
  OS_API_VERSION=5
461
  GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
462
  if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
463
  get_api5_arguments $GETOPT_RESULT
464
elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
465
  get_api10_arguments
466
elif [ "$OS_API_VERSION" = "20" ]; then
467
  get_api10_arguments
468
  get_api20_parameters
469
  IMAGE_NAME=$IMG_ID
470
else
471
  log_error "Unknown OS API VERSION $OS_API_VERSION"
472
  exit 1
473
fi
474

    
475
if [ -n "$OS_VARIANT" ]; then
476
  if [ ! -d "$VARIANTS_DIR" ]; then
477
    log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
478
    exit 1
479
  fi
480
  VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
481
  if [ -f "$VARIANT_CONFIG" ]; then
482
    . "$VARIANT_CONFIG"
483
  else
484
    if grep -qxF "$OS_VARIANT" variants.list; then
485
      log_error "ERROR: instance-image configuration error"
486
      log_error "  Published variant $OS_VARIANT is missing its config file"
487
      log_error "  Please create $VARIANT_CONFIG or unpublish the variant"
488
      log_error "  (by removing $OS_VARIANT from variants.list)"
489
    else
490
      log_error "Unofficial variant $OS_VARIANT is unsupported"
491
      log_error "Most probably this is a user error, forcing a wrong name"
492
      log_error "To support this variant please create file $VARIANT_CONFIG"
493
    fi
494
    exit 1
495
  fi
496
fi
497