Statistics
| Branch: | Revision:

root / common.sh.in @ 6f3e718d

History | View | Annotate | Download (14.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
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
    if [ -e ${target}/etc/debian_version ] ; then
136
        if [ -e ${target}/etc/lsb-release ] ; then
137
            ID=$(grep ^DISTRIB_ID= ${target}/etc/lsb-release | cut -d= -f2)
138
        fi
139
        if [ "a$ID" = "aUbuntu" ] ; then
140
            OPERATING_SYSTEM="ubuntu"
141
        else
142
            OPERATING_SYSTEM="debian"
143
        fi
144
    elif [ -e ${target}/etc/gentoo-release ] ; then
145
        OPERATING_SYSTEM="gentoo"
146
    elif [ -e ${target}/etc/fedora-release ] ; then
147
        OPERATING_SYSTEM="fedora"
148
    elif [ -e ${target}/etc/redhat-release ] ; then
149
        if [ -n "$(grep -i centos ${target}/etc/redhat-release)" ] ; then
150
            OPERATING_SYSTEM="centos"
151
        else
152
            OPERATING_SYSTEM="redhat"
153
        fi
154
    fi
155
}
156

    
157
get_os_release() {
158
    target=$1
159
    if [ -z "${target}" ] ; then
160
        log_error "target is not set in get_os_release"
161
        exit 1
162
    fi
163
    if [ -e ${target}/etc/debian_version ] ; then
164
        OS_RELEASE="$(cat ${target}/etc/debian_version)"
165
    elif [ -e ${target}/etc/fedora-release ] ; then
166
        OS_RELEASE="$(cat ${target}/etc/fedora-release | awk '{print $3}')"
167
    elif [ -e ${$target}/etc/redhat-release ] ; then
168
        OS_RELEASE="$(cat ${target}/etc/redhat-release | awk '{print $3}')"
169
    fi
170
}
171

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

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

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

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

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

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

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

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

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

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

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

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

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

    
369
    get_os_type $target
370
    if [ "${OS_TYPE}" = "redhat" ]; then
371
        # we have to force a filesystem relabeling for SELinux after messing
372
        # around with the filesystem in redhat-derived OSs
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
: ${BOOT:="no"}
422
: ${SWAP:="yes"}
423
: ${SWAP_SIZE:="${INSTANCE_BE_memory}"}
424
: ${FILESYSTEM:="ext3"}
425
: ${KERNEL_ARGS=""}
426
: ${OVERLAY=""}
427
: ${IMAGE_NAME:=""}
428
: ${IMAGE_TYPE:="dump"}
429
: ${NOMOUNT:="no"}
430
: ${ARCH:=""}
431
: ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-image/hooks"}
432
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/instance-image/variants"}
433
: ${NETWORKS_DIR:="@sysconfdir@/ganeti/instance-image/networks"}
434
: ${OVERLAYS_DIR:="@sysconfdir@/ganeti/instance-image/overlays"}
435
: ${IMAGE_DIR:="@localstatedir@/cache/ganeti-instance-image"}
436
: ${IMAGE_DEBUG:="no"}
437
: ${TOOLS_DIR:="@OS_DIR@/@OS_NAME@/tools"}
438

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

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

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

    
459

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

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