Statistics
| Branch: | Revision:

root / common.sh.in @ ab462591

History | View | Annotate | Download (13.1 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_PASSWD" ]; then
103
    log_error "Missing OS API Parameter:"
104
    log_error "(OSP_IMG_ID or OSP_IMG_PASSWD)"
105
    exit 1
106
  fi
107
  IMG_ID=$OSP_IMG_ID
108
  IMG_PASSWD=$OSP_IMG_PASSWD
109
}
110

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
360
cleanup() {
361
  if [ ${#CLEANUP[*]} -gt 0 ]; then
362
    LAST_ELEMENT=$((${#CLEANUP[*]}-1))
363
    REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
364
    for i in $REVERSE_INDEXES; do
365
      ${CLEANUP[$i]}
366
    done
367
  fi
368
}
369

    
370
trap cleanup EXIT
371

    
372
DEFAULT_FILE="@DEFAULT_DIR@/ganeti-instance-image"
373
if [ -f "$DEFAULT_FILE" ]; then
374
    . "$DEFAULT_FILE"
375
fi
376

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

    
380
# only if the user want to specify a mirror in the defaults file we
381
# will use it, this declaration is to make sure the variable is set
382
: ${CDINSTALL:="no"}
383
: ${SWAP:="yes"}
384
: ${SWAP_SIZE:="${INSTANCE_BE_memory}"}
385
: ${FILESYSTEM:="ext3"}
386
: ${KERNEL_ARGS=""}
387
: ${OVERLAY=""}
388
: ${IMAGE_NAME:=""}
389
: ${IMAGE_TYPE:="dump"}
390
: ${NOMOUNT:="no"}
391
: ${ARCH:=""}
392
: ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-image/hooks"}
393
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/instance-image/variants"}
394
: ${NETWORKS_DIR:="@sysconfdir@/ganeti/instance-image/networks"}
395
: ${OVERLAYS_DIR:="@sysconfdir@/ganeti/instance-image/overlays"}
396
: ${IMAGE_DIR:="@localstatedir@/cache/ganeti-instance-image"}
397
: ${IMAGE_DEBUG:="no"}
398
: ${TOOLS_DIR:="@OS_DIR@/@OS_NAME@/tools"}
399

    
400
SCRIPT_NAME=$(basename $0)
401
KERNEL_PATH="$INSTANCE_HV_kernel_path"
402

    
403
if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
404
    VOL_ID="/sbin/blkid -o value -s UUID"
405
    VOL_TYPE="/sbin/blkid -o value -s TYPE"
406
else
407
    for dir in /lib/udev /sbin; do
408
        if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
409
            VOL_ID="$dir/vol_id -u"
410
            VOL_TYPE="$dir/vol_id -t"
411
        fi
412
    done
413
fi
414

    
415
if [ -z "$VOL_ID" ]; then
416
    log_error "vol_id or blkid not found, please install udev or util-linux"
417
    exit 1
418
fi
419

    
420

    
421
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
422
  OS_API_VERSION=5
423
  GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
424
  if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
425
  get_api5_arguments $GETOPT_RESULT
426
elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
427
  get_api10_arguments
428
elif [ "$OS_API_VERSION" = "20" ]; then
429
  get_api10_arguments
430
  get_api20_parameters
431
  IMAGE_NAME=$IMG_ID
432
else
433
  log_error "Unknown OS API VERSION $OS_API_VERSION"
434
  exit 1
435
fi
436

    
437
if [ -n "$OS_VARIANT" ]; then
438
  if [ ! -d "$VARIANTS_DIR" ]; then
439
    log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
440
    exit 1
441
  fi
442
  VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
443
  if [ -f "$VARIANT_CONFIG" ]; then
444
    . "$VARIANT_CONFIG"
445
  else
446
    if grep -qxF "$OS_VARIANT" variants.list; then
447
      log_error "ERROR: instance-image configuration error"
448
      log_error "  Published variant $OS_VARIANT is missing its config file"
449
      log_error "  Please create $VARIANT_CONFIG or unpublish the variant"
450
      log_error "  (by removing $OS_VARIANT from variants.list)"
451
    else
452
      log_error "Unofficial variant $OS_VARIANT is unsupported"
453
      log_error "Most probably this is a user error, forcing a wrong name"
454
      log_error "To support this variant please create file $VARIANT_CONFIG"
455
    fi
456
    exit 1
457
  fi
458
fi
459