Statistics
| Branch: | Revision:

root / common.sh.in @ 494927a8

History | View | Annotate | Download (8.2 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
    if [ -e ${TARGET}/etc/redhat-release ] ; then
103
        OS_TYPE="redhat"
104
    elif [ -e ${TARGET}/etc/debian_version ] ; then
105
        OS_TYPE="debian"
106
    elif [ -e ${TARGET}/etc/gentoo-release ] ; then
107
        OS_TYPE="gentoo"
108
    elif [ -e ${TARGET}/etc/SuSE-release ] ; then
109
        OS_TYPE="suse"
110
    fi
111
}
112

    
113
format_disk0() {
114
    local sfdisk_cmd="$SFDISK -uM -H 255 -S 63 --quiet --Linux $1"
115
    if [  "${SWAP}" = "yes" -a -z "${KERNEL_PATH}" ] ; then
116
        # Create three partitions:
117
        # 1 - 100MB /boot, bootable
118
        # 2 - Size of Memory, swap
119
        # 3 - Rest
120
        $sfdisk_cmd <<EOF
121
,100,L,*
122
,$INSTANCE_BE_memory,S
123
,,L
124
EOF
125
    elif [  "${SWAP}" = "no" -a -z "${KERNEL_PATH}" ] ; then
126
        # Create two partitions:
127
        # 1 - 100MB /boot, bootable
128
        # 2 - Rest
129
        $sfdisk_cmd <<EOF
130
,100,L,*
131
,,L
132
EOF
133
    elif [  "${SWAP}" = "yes" -a -n "${KERNEL_PATH}" ] ; then
134
        # Create two partitions:
135
        # 1 - Size of Memory, swap
136
        # 2 - Rest
137
        $sfdisk_cmd <<EOF
138
,$INSTANCE_BE_memory,S
139
,,L
140
EOF
141
    elif [  "${SWAP}" = "no" -a -n "${KERNEL_PATH}" ] ; then
142
        # Create two partitions:
143
        # 1 - Whole
144
        $sfdisk_cmd <<EOF
145
,,L
146
EOF
147
    fi
148
}
149

    
150
mkfs_disk0() {
151
    # Format /
152
    mkfs.ext3 -Fq -L / $root_dev
153
    # Format /boot
154
    if [ -n "${boot_dev}" ] ; then
155
        mkfs.ext3 -Fq -L /boot $boot_dev
156
    fi
157
    # Format swap
158
    if [ -n "${swap_dev}" ] ; then
159
        # Format swap
160
        mkswap -f $swap_dev
161
    fi
162
}
163

    
164
mount_disk0() {
165
    local target=$1
166
    mount $root_dev $target
167
    CLEANUP+=("umount $target")
168
    if [ -n "${boot_dev}" ] ; then
169
        $MKDIR_P $target/boot
170
        mount $boot_dev $target/boot
171
        CLEANUP+=("umount $target/boot")
172
    fi
173
}
174

    
175
map_disk0() {
176
    blockdev="$1"
177
    filesystem_dev_base=`$KPARTX -l -p- $blockdev | \
178
                            grep -m 1 -- "-1.*$blockdev" | \
179
                            $AWK '{print $1}'`
180
    if [ -z "$filesystem_dev_base" ]; then
181
        log_error "Cannot interpret kpartx output and get partition mapping"
182
        exit 1
183
    fi
184
    $KPARTX -a -p- $blockdev > /dev/null
185
    filesystem_dev="/dev/mapper/${filesystem_dev_base/%-1/}"
186
    if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then
187
        log_error "Can't find kpartx mapped partition: /dev/mapper/$filesystem_dev_base"
188
        exit 1
189
    fi
190
    echo "$filesystem_dev"
191
}
192

    
193
map_partition() {
194
    filesystem_dev="$1"
195
    partition="$2"
196
    if [ "${SWAP}" = "yes" -a -z "${KERNEL_PATH}" ] ; then
197
        boot_dev="${filesystem_dev}-1"
198
        swap_dev="${filesystem_dev}-2"
199
        root_dev="${filesystem_dev}-3"
200
    elif [ "${SWAP}" = "no" -a -z "${KERNEL_PATH}" ] ; then
201
        boot_dev="${filesystem_dev}-1"
202
        root_dev="${filesystem_dev}-2"
203
    elif [ "${SWAP}" = "yes" -a -n "${KERNEL_PATH}" ] ; then
204
        swap_dev="${filesystem_dev}-1"
205
        root_dev="${filesystem_dev}-2"
206
    elif [ "${SWAP}" = "no" -a -n "${KERNEL_PATH}" ] ; then
207
        root_dev="${filesystem_dev}-1"
208
    fi
209
    echo "$(eval "echo \${$(echo ${partition}_dev)"})"
210
}
211

    
212
unmap_disk0() {
213
  $KPARTX -d -p- $1
214
}
215

    
216
cleanup() {
217
  if [ ${#CLEANUP[*]} -gt 0 ]; then
218
    LAST_ELEMENT=$((${#CLEANUP[*]}-1))
219
    REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
220
    for i in $REVERSE_INDEXES; do
221
      ${CLEANUP[$i]}
222
    done
223
  fi
224
}
225

    
226
trap cleanup EXIT
227

    
228
DEFAULT_FILE="@DEFAULTS_DIR@/ganeti-instance-image"
229
if [ -f "$DEFAULT_FILE" ]; then
230
    . "$DEFAULT_FILE"
231
fi
232

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

    
236
# only if the user want to specify a mirror in the defaults file we
237
# will use it, this declaration is to make sure the variable is set
238
: ${CDINSTALL:="yes"}
239
: ${SWAP:="yes"}
240
: ${IMAGE_NAME:=""}
241
: ${IMAGE_TYPE:="qemu"}
242
: ${ARCH:=""}
243
: ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-image.d"}
244
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/instance-image/variants"}
245
: ${IMAGE_DIR:="@localstatedir@/cache/ganeti-instance-image"}
246
: ${IMAGE_DEBUG:="0"}
247

    
248
SCRIPT_NAME=$(basename $0)
249
KERNEL_PATH="$INSTANCE_HV_kernel_path"
250

    
251
if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
252
    VOL_ID="/sbin/blkid -o value -s UUID"
253
    VOL_TYPE="/sbin/blkid -o value -s TYPE"
254
else
255
    for dir in /lib/udev /sbin; do
256
        if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
257
            VOL_ID="$dir/vol_id -u"
258
            VOL_TYPE="$dir/vol_id -t"
259
        fi
260
    done
261
fi
262

    
263
if [ -z "$VOL_ID" ]; then
264
    log_error "vol_id or blkid not found, please install udev or util-linux"
265
    exit 1
266
fi
267

    
268

    
269
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
270
  OS_API_VERSION=5
271
  GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
272
  if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
273
  get_api5_arguments $GETOPT_RESULT
274
elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
275
  get_api10_arguments
276
else
277
  log_error "Unknown OS API VERSION $OS_API_VERSION"
278
  exit 1
279
fi
280

    
281
if [ -n "$OS_VARIANT" ]; then
282
  if [ ! -d "$VARIANTS_DIR" ]; then
283
    log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
284
    exit 1
285
  fi
286
  VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
287
  if [ -f "$VARIANT_CONFIG" ]; then
288
    . "$VARIANT_CONFIG"
289
  else
290
    if grep -qxF "$OS_VARIANT" variants.list; then
291
      log_error "ERROR: instance-image configuration error"
292
      log_error "  Published variant $OS_VARIANT is missing its config file"
293
      log_error "  Please create $VARIANT_CONFIG or unpublish the variant"
294
      log_error "  (by removing $OS_VARIANT from variants.list)"
295
    else
296
      log_error "Unofficial variant $OS_VARIANT is unsupported"
297
      log_error "Most probably this is a user error, forcing a wrong name"
298
      log_error "To support this variant please create file $VARIANT_CONFIG"
299
    fi
300
    exit 1
301
  fi
302
fi
303