Statistics
| Branch: | Revision:

root / common.sh.in @ 9f4b2c31

History | View | Annotate | Download (11.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" ] &&  $@ || :
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="$(which lsb_release)"
125
    if [ -n "$lsb" ] ; then
126
        OPERATING_SYSTEM="$(${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="$(which lsb_release)"
149
    if [ -n "$lsb" ] ; then
150
        OS_RELEASE="$(${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
,$INSTANCE_BE_memory,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
,$INSTANCE_BE_memory,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
    # Format /
199
    mkfs.ext3 -Fq -L / $root_dev
200
    # Format /boot
201
    if [ -n "${boot_dev}" ] ; then
202
        mkfs.ext3 -Fq -L /boot $boot_dev
203
    fi
204
    # Format swap
205
    if [ -n "${swap_dev}" ] ; then
206
        # Format swap
207
        mkswap -f $swap_dev
208
    fi
209
}
210

    
211
mount_disk0() {
212
    local target=$1
213
    mount $root_dev $target
214
    CLEANUP+=("umount $target")
215
    if [ -n "${boot_dev}" ] ; then
216
        $MKDIR_P $target/boot
217
        mount $boot_dev $target/boot
218
        CLEANUP+=("umount $target/boot")
219
    fi
220
}
221

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

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

    
259
unmap_disk0() {
260
  $KPARTX -d -p- $1
261
}
262

    
263
setup_fstab() {
264
    local target=$1
265
    get_os_type
266
    cat > $target/etc/fstab <<EOF
267
# /etc/fstab: static file system information.
268
#
269
# <file system>   <mount point>   <type>  <options>       <dump>  <pass>
270
UUID=$root_uuid   /               ext3    defaults        0       1
271
proc              /proc           proc    defaults        0       0
272
EOF
273

    
274
[ -n "$boot_dev" -a -n "$boot_uuid" ] && cat >> $target/etc/fstab <<EOF
275
UUID=$boot_uuid   /boot           ext3    defaults        1       2
276
EOF
277

    
278
[ -n "$swap_dev" -a -n "$swap_uuid" ] && cat >> $target/etc/fstab <<EOF
279
UUID=$swap_uuid   swap            swap    defaults        0       0
280
EOF
281

    
282
# OS Specific fstabs
283
[ "$OS_TYPE" = "redhat" ] && cat >> $target/etc/fstab <<EOF
284
tmpfs             /dev/shm        tmpfs   defaults        0       0
285
devpts            /dev/pts        devpts  gid=5,mode=620  0       0
286
sysfs             /sys            sysfs   defaults        0       0
287
EOF
288

    
289
[ "$OS_TYPE" = "gentoo" ] && cat >> $target/etc/fstab <<EOF
290
shm               /dev/shm        tmpfs   nodev,nosuid,noexec 0   0
291
EOF
292
}
293

    
294
setup_console() {
295
    local target=$1
296
    if [ -z "$target" ] ; then
297
        log_error "target not set for setup_console"
298
        exit 1
299
    fi
300
    # Upstart is on this system, so do this instead
301
    if [ -e ${target}/etc/event.d/tty1 ] ; then
302
        cat ${target}/etc/event.d/tty1 | sed -re 's/tty1/ttyS0/' \
303
            > ${target}/etc/event.d/ttyS0
304
        return
305
    fi
306
    get_os $target
307
    case $OPERATING_SYSTEM in
308
        gentoo)
309
            sed -i -e 's/.*ttyS0.*/s0:12345:respawn:\/sbin\/agetty 115200 ttyS0 vt100/' \
310
                ${target}/etc/inittab
311
            ;;
312
        centos)
313
            echo "s0:12345:respawn:/sbin/mingetty 115200 ttyS0 vt100" >> \
314
                ${target}/etc/inittab
315
            ;;
316
        debian|ubuntu)
317
            sed -i -e 's/.*T0.*/T0:23:respawn:/sbin/getty -L ttyS0 115200 vt100' \
318
                ${target}/etc/inittab
319
            ;;
320
        *)
321
            echo "No support for your OS in instance-image, skipping..."
322
            ;;
323
    esac
324
}
325

    
326
cleanup() {
327
  if [ ${#CLEANUP[*]} -gt 0 ]; then
328
    LAST_ELEMENT=$((${#CLEANUP[*]}-1))
329
    REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
330
    for i in $REVERSE_INDEXES; do
331
      ${CLEANUP[$i]}
332
    done
333
  fi
334
}
335

    
336
trap cleanup EXIT
337

    
338
DEFAULT_FILE="@DEFAULTS_DIR@/ganeti-instance-image"
339
if [ -f "$DEFAULT_FILE" ]; then
340
    . "$DEFAULT_FILE"
341
fi
342

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

    
346
# only if the user want to specify a mirror in the defaults file we
347
# will use it, this declaration is to make sure the variable is set
348
: ${CDINSTALL:="yes"}
349
: ${SWAP:="yes"}
350
: ${IMAGE_NAME:=""}
351
: ${IMAGE_TYPE:="qemu"}
352
: ${ARCH:=""}
353
: ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-image.d"}
354
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/instance-image/variants"}
355
: ${IMAGE_DIR:="@localstatedir@/cache/ganeti-instance-image"}
356
: ${IMAGE_DEBUG:="0"}
357

    
358
SCRIPT_NAME=$(basename $0)
359
KERNEL_PATH="$INSTANCE_HV_kernel_path"
360

    
361
if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
362
    VOL_ID="/sbin/blkid -o value -s UUID"
363
    VOL_TYPE="/sbin/blkid -o value -s TYPE"
364
else
365
    for dir in /lib/udev /sbin; do
366
        if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
367
            VOL_ID="$dir/vol_id -u"
368
            VOL_TYPE="$dir/vol_id -t"
369
        fi
370
    done
371
fi
372

    
373
if [ -z "$VOL_ID" ]; then
374
    log_error "vol_id or blkid not found, please install udev or util-linux"
375
    exit 1
376
fi
377

    
378

    
379
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
380
  OS_API_VERSION=5
381
  GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
382
  if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
383
  get_api5_arguments $GETOPT_RESULT
384
elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
385
  get_api10_arguments
386
else
387
  log_error "Unknown OS API VERSION $OS_API_VERSION"
388
  exit 1
389
fi
390

    
391
if [ -n "$OS_VARIANT" ]; then
392
  if [ ! -d "$VARIANTS_DIR" ]; then
393
    log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
394
    exit 1
395
  fi
396
  VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
397
  if [ -f "$VARIANT_CONFIG" ]; then
398
    . "$VARIANT_CONFIG"
399
  else
400
    if grep -qxF "$OS_VARIANT" variants.list; then
401
      log_error "ERROR: instance-image configuration error"
402
      log_error "  Published variant $OS_VARIANT is missing its config file"
403
      log_error "  Please create $VARIANT_CONFIG or unpublish the variant"
404
      log_error "  (by removing $OS_VARIANT from variants.list)"
405
    else
406
      log_error "Unofficial variant $OS_VARIANT is unsupported"
407
      log_error "Most probably this is a user error, forcing a wrong name"
408
      log_error "To support this variant please create file $VARIANT_CONFIG"
409
    fi
410
    exit 1
411
  fi
412
fi
413