Statistics
| Branch: | Revision:

root / common.sh.in @ e7a2d1ad

History | View | Annotate | Download (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
    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
    if [ "${SWAP}" = "yes" ] ; then
115
        # Create three partitions:
116
        # 1 - 100MB /boot, bootable
117
        # 2 - Size of Memory, swap
118
        # 3 - Rest
119
        $SFDISK -uM -H 255 -S 63 --quiet --Linux "$1" <<EOF
120
,100,L,*
121
,$INSTANCE_BE_memory,S
122
,,L
123
EOF
124
    else
125
        # Create two partitions:
126
        # 1 - 100MB /boot, bootable
127
        # 2 - Rest
128
        $SFDISK -uM -H 255 -S 63 --quiet --Linux "$1" <<EOF
129
,100,L,*
130
,,L
131
EOF
132
    fi
133
}
134

    
135
mkfs_disk0() {
136
    local boot_dev=$1 root_dev=$2 swap_dev=$3
137
    # Format /boot
138
    mke2fs -Fjq -L /boot $boot_dev
139
    # Format /
140
    mke2fs -Fjq -L / $root_dev
141
    if [ "${SWAP}" = "yes" ] ; then
142
        # Format swap
143
        mkswap $swap_dev
144
    fi
145
}
146

    
147
mount_disk0() {
148
    local target=$1 root_dev=$2 boot_dev=$3
149
    mount $root_dev $target
150
    CLEANUP+=("umount $target")
151
    $MKDIR_P $target/boot
152
    mount $boot_dev $target/boot
153
    CLEANUP+=("umount $target/boot")
154
}
155

    
156
map_disk0() {
157
  blockdev="$1"
158
  filesystem_dev_base=`$KPARTX -l -p- $blockdev | \
159
                       grep -m 1 -- "-1.*$blockdev" | \
160
                       $AWK '{print $1}'`
161
  if [ -z "$filesystem_dev_base" ]; then
162
    log_error "Cannot interpret kpartx output and get partition mapping"
163
    exit 1
164
  fi
165
  $KPARTX -a -p- $blockdev > /dev/null
166
  filesystem_dev="/dev/mapper/${filesystem_dev_base/-1/}"
167
  if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then
168
    log_error "Can't find kpartx mapped partition: /dev/mapper/$filesystem_dev_base"
169
    exit 1
170
  fi
171
  echo "$filesystem_dev"
172
}
173

    
174
unmap_disk0() {
175
  $KPARTX -d -p- $1
176
}
177

    
178
cleanup() {
179
  if [ ${#CLEANUP[*]} -gt 0 ]; then
180
    LAST_ELEMENT=$((${#CLEANUP[*]}-1))
181
    REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
182
    for i in $REVERSE_INDEXES; do
183
      ${CLEANUP[$i]}
184
    done
185
  fi
186
}
187

    
188
trap cleanup EXIT
189

    
190
DEFAULT_FILE="@DEFAULTS_DIR@/ganeti-instance-image"
191
if [ -f "$DEFAULT_FILE" ]; then
192
    . "$DEFAULT_FILE"
193
fi
194

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

    
198
# only if the user want to specify a mirror in the defaults file we
199
# will use it, this declaration is to make sure the variable is set
200
: ${CDINSTALL:="yes"}
201
: ${SWAP:="yes"}
202
: ${IMAGE_NAME:=""}
203
: ${IMAGE_TYPE:="qemu"}
204
: ${ARCH:=""}
205
: ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-image.d"}
206
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/instance-image/variants"}
207
: ${IMAGE_DIR:="@localstatedir@/cache/ganeti-instance-image"}
208
: ${IMAGE_DEBUG:="0"}
209

    
210
SCRIPT_NAME=$(basename $0)
211

    
212
if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
213
    VOL_ID="/sbin/blkid -o value -s UUID"
214
    VOL_TYPE="/sbin/blkid -o value -s TYPE"
215
else
216
    for dir in /lib/udev /sbin; do
217
        if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
218
            VOL_ID="$dir/vol_id -u"
219
            VOL_TYPE="$dir/vol_id -t"
220
        fi
221
    done
222
fi
223

    
224
if [ -z "$VOL_ID" ]; then
225
    log_error "vol_id or blkid not found, please install udev or util-linux"
226
    exit 1
227
fi
228

    
229

    
230
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
231
  OS_API_VERSION=5
232
  GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
233
  if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
234
  get_api5_arguments $GETOPT_RESULT
235
elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
236
  get_api10_arguments
237
else
238
  log_error "Unknown OS API VERSION $OS_API_VERSION"
239
  exit 1
240
fi
241

    
242
if [ -n "$OS_VARIANT" ]; then
243
  if [ ! -d "$VARIANTS_DIR" ]; then
244
    log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
245
    exit 1
246
  fi
247
  VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
248
  if [ -f "$VARIANT_CONFIG" ]; then
249
    . "$VARIANT_CONFIG"
250
  else
251
    if grep -qxF "$OS_VARIANT" variants.list; then
252
      log_error "ERROR: instance-image configuration error"
253
      log_error "  Published variant $OS_VARIANT is missing its config file"
254
      log_error "  Please create $VARIANT_CONFIG or unpublish the variant"
255
      log_error "  (by removing $OS_VARIANT from variants.list)"
256
    else
257
      log_error "Unofficial variant $OS_VARIANT is unsupported"
258
      log_error "Most probably this is a user error, forcing a wrong name"
259
      log_error "To support this variant please create file $VARIANT_CONFIG"
260
    fi
261
    exit 1
262
  fi
263
fi
264