Statistics
| Branch: | Revision:

root / common.sh.in @ 84472880

History | View | Annotate | Download (12.5 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" ] &&  $@ || :
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_os_type() {
102
    target=$1
103
    if [ -z "${target}" ] ; then
104
        log_error "target is not set in get_os_type"
105
        exit 1
106
    fi
107
    if [ -e ${target}/etc/redhat-release ] ; then
108
        OS_TYPE="redhat"
109
    elif [ -e ${target}/etc/debian_version ] ; then
110
        OS_TYPE="debian"
111
    elif [ -e ${target}/etc/gentoo-release ] ; then
112
        OS_TYPE="gentoo"
113
    elif [ -e ${target}/etc/SuSE-release ] ; then
114
        OS_TYPE="suse"
115
    fi
116
}
117

    
118
get_os() {
119
    target=$1
120
    if [ -z "${target}" ] ; then
121
        log_error "target is not set in get_os"
122
        exit 1
123
    fi
124
    lsb="/usr/bin/lsb_release"
125
    if [ -e $lsb ] ; then
126
        OPERATING_SYSTEM="$(chroot ${target} ${lsb} -i -s | tr "[:upper:]" "[:lower:]")"
127
    elif [ -e ${target}/etc/debian_version ] ; then
128
        OPERATING_SYSTEM="debian"
129
    elif [ -e ${target}/etc/gentoo-release ] ; then
130
        OPERATING_SYSTEM="gentoo"
131
    elif [ -e ${target}/etc/fedora-release ] ; then
132
        OPERATING_SYSTEM="fedora"
133
    elif [ -e ${target}/etc/redhat-release ] ; then
134
        if [ -n "$(grep -i centos ${target}/etc/redhat-release)" ] ; then
135
            OPERATING_SYSTEM="centos"
136
        else
137
            OPERATING_SYSTEM="redhat"
138
        fi
139
    fi
140
}
141

    
142
get_os_release() {
143
    target=$1
144
    if [ -z "${target}" ] ; then
145
        log_error "target is not set in get_os_release"
146
        exit 1
147
    fi
148
    lsb="/usr/bin/lsb_release"
149
    if [ -e $lsb ] ; then
150
        OS_RELEASE="$(chroot ${target} ${lsb} -r -s | tr "[:upper:]" "[:lower:]")"
151
    elif [ -e ${target}/etc/debian_version ] ; then
152
        OS_RELEASE="$(cat ${target}/etc/debian_version)"
153
    elif [ -e ${target}/etc/fedora-release ] ; then
154
        OS_RELEASE="$(cat ${target}/etc/fedora-release | awk '{print $3}')"
155
    elif [ -e ${$target}/etc/redhat-release ] ; then
156
        OS_RELEASE="$(cat ${target}/etc/redhat-release | awk '{print $3}')"
157
    fi
158
}
159

    
160
format_disk0() {
161
    local sfdisk_cmd="$SFDISK -uM -H 255 -S 63 --quiet --Linux $1"
162
    if [  "${SWAP}" = "yes" -a -z "${KERNEL_PATH}" ] ; then
163
        # Create three partitions:
164
        # 1 - 100MB /boot, bootable
165
        # 2 - Size of Memory, swap
166
        # 3 - Rest
167
        $sfdisk_cmd <<EOF
168
,100,L,*
169
,${SWAP_SIZE},S
170
,,L
171
EOF
172
    elif [  "${SWAP}" = "no" -a -z "${KERNEL_PATH}" ] ; then
173
        # Create two partitions:
174
        # 1 - 100MB /boot, bootable
175
        # 2 - Rest
176
        $sfdisk_cmd <<EOF
177
,100,L,*
178
,,L
179
EOF
180
    elif [  "${SWAP}" = "yes" -a -n "${KERNEL_PATH}" ] ; then
181
        # Create two partitions:
182
        # 1 - Size of Memory, swap
183
        # 2 - Rest
184
        $sfdisk_cmd <<EOF
185
,$SWAP_SIZE,S
186
,,L
187
EOF
188
    elif [  "${SWAP}" = "no" -a -n "${KERNEL_PATH}" ] ; then
189
        # Create two partitions:
190
        # 1 - Whole
191
        $sfdisk_cmd <<EOF
192
,,L
193
EOF
194
    fi
195
}
196

    
197
mkfs_disk0() {
198
    local mkfs="mkfs.${FILESYSTEM}"
199
    # Format /
200
    $mkfs -Fq -L / $root_dev
201
    # Format /boot
202
    if [ -n "${boot_dev}" ] ; then
203
        $mkfs -Fq -L /boot $boot_dev
204
    fi
205
    # Format swap
206
    if [ -n "${swap_dev}" ] ; then
207
        # Format swap
208
        mkswap -f $swap_dev
209
    fi
210
}
211

    
212
mount_disk0() {
213
    local target=$1
214
    mount $root_dev $target
215
    CLEANUP+=("umount $target")
216
    if [ -n "${boot_dev}" ] ; then
217
        $MKDIR_P $target/boot
218
        mount $boot_dev $target/boot
219
        CLEANUP+=("umount $target/boot")
220
    fi
221
    # sync the file systems before unmounting to ensure everything is flushed
222
    # out
223
    CLEANUP+=("sync")
224
}
225

    
226
map_disk0() {
227
    blockdev="$1"
228
    filesystem_dev_base=`$KPARTX -l -p- $blockdev | \
229
                            grep -m 1 -- "-1.*$blockdev" | \
230
                            $AWK '{print $1}'`
231
    if [ -z "$filesystem_dev_base" ]; then
232
        log_error "Cannot interpret kpartx output and get partition mapping"
233
        exit 1
234
    fi
235
    $KPARTX -a -p- $blockdev > /dev/null
236
    filesystem_dev="/dev/mapper/${filesystem_dev_base/%-1/}"
237
    if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then
238
        log_error "Can't find kpartx mapped partition: /dev/mapper/$filesystem_dev_base"
239
        exit 1
240
    fi
241
    echo "$filesystem_dev"
242
}
243

    
244
map_partition() {
245
    filesystem_dev="$1"
246
    partition="$2"
247
    if [ "${SWAP}" = "yes" -a -z "${KERNEL_PATH}" ] ; then
248
        boot_dev="${filesystem_dev}-1"
249
        swap_dev="${filesystem_dev}-2"
250
        root_dev="${filesystem_dev}-3"
251
    elif [ "${SWAP}" = "no" -a -z "${KERNEL_PATH}" ] ; then
252
        boot_dev="${filesystem_dev}-1"
253
        root_dev="${filesystem_dev}-2"
254
    elif [ "${SWAP}" = "yes" -a -n "${KERNEL_PATH}" ] ; then
255
        swap_dev="${filesystem_dev}-1"
256
        root_dev="${filesystem_dev}-2"
257
    elif [ "${SWAP}" = "no" -a -n "${KERNEL_PATH}" ] ; then
258
        root_dev="${filesystem_dev}-1"
259
    fi
260
    echo "$(eval "echo \${$(echo ${partition}_dev)"})"
261
}
262

    
263
unmap_disk0() {
264
  $KPARTX -d -p- $1
265
}
266

    
267
setup_fstab() {
268
    local target=$1 fs=${FILESYSTEM}
269
    get_os_type $target
270
    cat > $target/etc/fstab <<EOF
271
# /etc/fstab: static file system information.
272
#
273
# <file system>   <mount point>   <type>  <options>       <dump>  <pass>
274
UUID=$root_uuid   /               $fs     defaults        0       1
275
proc              /proc           proc    defaults        0       0
276
EOF
277

    
278
if [ -n "$boot_dev" -a -n "$boot_uuid" ] ; then
279
    cat >> $target/etc/fstab <<EOF
280
UUID=$boot_uuid   /boot           $fs     defaults        1       2
281
EOF
282
fi
283

    
284
if [ -n "$swap_dev" -a -n "$swap_uuid" ] ; then
285
    cat >> $target/etc/fstab <<EOF
286
UUID=$swap_uuid   swap            swap    defaults        0       0
287
EOF
288
fi
289

    
290
# OS Specific fstabs
291
if [ "$OS_TYPE" = "redhat" ] ; then
292
    cat >> $target/etc/fstab <<EOF
293
tmpfs             /dev/shm        tmpfs   defaults        0       0
294
devpts            /dev/pts        devpts  gid=5,mode=620  0       0
295
sysfs             /sys            sysfs   defaults        0       0
296
EOF
297
fi
298

    
299
if [ "$OS_TYPE" = "gentoo" ] ; then
300
    cat >> $target/etc/fstab <<EOF
301
shm               /dev/shm        tmpfs   nodev,nosuid,noexec 0   0
302
EOF
303
fi
304
}
305

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

    
346
cleanup() {
347
  if [ ${#CLEANUP[*]} -gt 0 ]; then
348
    LAST_ELEMENT=$((${#CLEANUP[*]}-1))
349
    REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
350
    for i in $REVERSE_INDEXES; do
351
      ${CLEANUP[$i]}
352
    done
353
  fi
354
}
355

    
356
trap cleanup EXIT
357

    
358
DEFAULT_FILE="@DEFAULT_DIR@/ganeti-instance-image"
359
if [ -f "$DEFAULT_FILE" ]; then
360
    . "$DEFAULT_FILE"
361
fi
362

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

    
366
# only if the user want to specify a mirror in the defaults file we
367
# will use it, this declaration is to make sure the variable is set
368
: ${CDINSTALL:="yes"}
369
: ${SWAP:="yes"}
370
: ${SWAP_SIZE:="${INSTANCE_BE_memory}"}
371
: ${FILESYSTEM:="ext3"}
372
: ${KERNEL_ARGS=""}
373
: ${OVERLAY=""}
374
: ${IMAGE_NAME:=""}
375
: ${IMAGE_TYPE:="dump"}
376
: ${ARCH:=""}
377
: ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-image/hooks"}
378
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/instance-image/variants"}
379
: ${NETWORKS_DIR:="@sysconfdir@/ganeti/instance-image/networks"}
380
: ${OVERLAYS_DIR:="@sysconfdir@/ganeti/instance-image/overlays"}
381
: ${IMAGE_DIR:="@localstatedir@/cache/ganeti-instance-image"}
382
: ${IMAGE_DEBUG:="0"}
383

    
384
SCRIPT_NAME=$(basename $0)
385
KERNEL_PATH="$INSTANCE_HV_kernel_path"
386

    
387
if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
388
    VOL_ID="/sbin/blkid -o value -s UUID"
389
    VOL_TYPE="/sbin/blkid -o value -s TYPE"
390
else
391
    for dir in /lib/udev /sbin; do
392
        if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
393
            VOL_ID="$dir/vol_id -u"
394
            VOL_TYPE="$dir/vol_id -t"
395
        fi
396
    done
397
fi
398

    
399
if [ -z "$VOL_ID" ]; then
400
    log_error "vol_id or blkid not found, please install udev or util-linux"
401
    exit 1
402
fi
403

    
404

    
405
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
406
  OS_API_VERSION=5
407
  GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
408
  if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
409
  get_api5_arguments $GETOPT_RESULT
410
elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
411
  get_api10_arguments
412
else
413
  log_error "Unknown OS API VERSION $OS_API_VERSION"
414
  exit 1
415
fi
416

    
417
if [ -n "$OS_VARIANT" ]; then
418
  if [ ! -d "$VARIANTS_DIR" ]; then
419
    log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
420
    exit 1
421
  fi
422
  VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
423
  if [ -f "$VARIANT_CONFIG" ]; then
424
    . "$VARIANT_CONFIG"
425
  else
426
    if grep -qxF "$OS_VARIANT" variants.list; then
427
      log_error "ERROR: instance-image configuration error"
428
      log_error "  Published variant $OS_VARIANT is missing its config file"
429
      log_error "  Please create $VARIANT_CONFIG or unpublish the variant"
430
      log_error "  (by removing $OS_VARIANT from variants.list)"
431
    else
432
      log_error "Unofficial variant $OS_VARIANT is unsupported"
433
      log_error "Most probably this is a user error, forcing a wrong name"
434
      log_error "To support this variant please create file $VARIANT_CONFIG"
435
    fi
436
    exit 1
437
  fi
438
fi
439