Statistics
| Branch: | Revision:

root / common.sh.in @ b05b1ab6

History | View | Annotate | Download (6.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
MKDIR_P="@MKDIR_P@"
26

    
27
CLEANUP=( )
28

    
29
log_error() {
30
  echo "$@" >&2
31
}
32

    
33
get_api5_arguments() {
34
  GETOPT_RESULT=$*
35
  # Note the quotes around `$TEMP': they are essential!
36
  eval set -- "$GETOPT_RESULT"
37
  while true; do
38
    case "$1" in
39
      -i|-n) instance=$2; shift 2;;
40

    
41
      -o) old_name=$2; shift 2;;
42

    
43
      -b) blockdev=$2; shift 2;;
44

    
45
      -s) swapdev=$2; shift 2;;
46

    
47
      --) shift; break;;
48

    
49
      *)  log_error "Internal error!" >&2; exit 1;;
50
    esac
51
  done
52
  if [ -z "$instance" -o -z "$blockdev" ]; then
53
    log_error "Missing OS API Argument (-i, -n, or -b)"
54
    exit 1
55
  fi
56
  if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev"  ]; then
57
    log_error "Missing OS API Argument -s (swapdev)"
58
    exit 1
59
  fi
60
  if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name"  ]; then
61
    log_error "Missing OS API Argument -o (old_name)"
62
    exit 1
63
  fi
64
}
65

    
66
get_api10_arguments() {
67
  if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then
68
    log_error "Missing OS API Variable:"
69
    log_error "(INSTANCE_NAME HYPERVISOR or DISK_COUNT)"
70
    exit 1
71
  fi
72
  instance=$INSTANCE_NAME
73
  if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then
74
    log_error "At least one disk is needed"
75
    exit 1
76
  fi
77
  if [ "$SCRIPT_NAME" = "export" ]; then
78
    if [ -z "$EXPORT_DEVICE" ]; then
79
      log_error "Missing OS API Variable EXPORT_DEVICE"
80
    fi
81
    blockdev=$EXPORT_DEVICE
82
  elif [ "$SCRIPT_NAME" = "import" ]; then
83
    if [ -z "$IMPORT_DEVICE" ]; then
84
       log_error "Missing OS API Variable IMPORT_DEVICE"
85
    fi
86
    blockdev=$IMPORT_DEVICE
87
  else
88
    blockdev=$DISK_0_PATH
89
  fi
90
  if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then
91
    log_error "Missing OS API Variable OLD_INSTANCE_NAME"
92
  fi
93
  old_name=$OLD_INSTANCE_NAME
94
}
95

    
96
format_disk0() {
97
  # Create three partitions:
98
  # 1 - 100MB /boot, bootable
99
  # 2 - Size of Memory, swap
100
  # 3 - Rest
101
  $SFDISK -uM -H 255 -S 63 --quiet --Linux "$1" <<EOF
102
,100,L,*
103
,$INSTANCE_BE_memory,S
104
,,L
105
EOF
106
}
107

    
108
mount_disk0() {
109
    local tmpdir=$1 root_dev=$2 boot_dev=$3
110
    mount $root_dev $tmpdir
111
    CLEANUP+=("umount $tmpdir")
112
    $MKDIR_P $tmpdir/boot
113
    mount $boot_dev $tmpdir/boot
114
    CLEANUP+=("umount $tmpdir/boot")
115
}
116

    
117
map_disk0() {
118
  blockdev="$1"
119
  filesystem_dev_base=`$KPARTX -l -p- $blockdev | \
120
                       grep -m 1 -- "-1.*$blockdev" | \
121
                       $AWK '{print $1}'`
122
  if [ -z "$filesystem_dev_base" ]; then
123
    log_error "Cannot interpret kpartx output and get partition mapping"
124
    exit 1
125
  fi
126
  $KPARTX -a -p- $blockdev > /dev/null
127
  filesystem_dev="/dev/mapper/${filesystem_dev_base/-1/}"
128
  if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then
129
    log_error "Can't find kpartx mapped partition: /dev/mapper/$filesystem_dev_base"
130
    exit 1
131
  fi
132
  echo "$filesystem_dev"
133
}
134

    
135
unmap_disk0() {
136
  $KPARTX -d -p- $1
137
}
138

    
139
cleanup() {
140
  if [ ${#CLEANUP[*]} -gt 0 ]; then
141
    LAST_ELEMENT=$((${#CLEANUP[*]}-1))
142
    REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
143
    for i in $REVERSE_INDEXES; do
144
      ${CLEANUP[$i]}
145
    done
146
  fi
147
}
148

    
149
trap cleanup EXIT
150

    
151
DEFAULT_FILE="@DEFAULTS_DIR@/ganeti-instance-image"
152
if [ -f "$DEFAULT_FILE" ]; then
153
    . "$DEFAULT_FILE"
154
fi
155

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

    
159
# only if the user want to specify a mirror in the defaults file we
160
# will use it, this declaration is to make sure the variable is set
161
: ${CDINSTALL:="yes"}
162
: ${IMAGE_NAME:=""}
163
: ${IMAGE_TYPE:="qemu"}
164
: ${ARCH:=""}
165
: ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-image.d"}
166
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/instance-image/variants"}
167
: ${IMAGE_DIR:="@localstatedir@/cache/ganeti-instance-image"}
168
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
169
  DEFAULT_PARTITION_STYLE="none"
170
else
171
  DEFAULT_PARTITION_STYLE="msdos"
172
fi
173
: ${PARTITION_STYLE:=$DEFAULT_PARTITION_STYLE} # disk partition style
174

    
175

    
176
SCRIPT_NAME=$(basename $0)
177

    
178
if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
179
  VOL_ID="/sbin/blkid -o value -s UUID"
180
  VOL_TYPE="/sbin/blkid -o value -s TYPE"
181
else
182
  for dir in /lib/udev /sbin; do
183
    if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
184
      VOL_ID="$dir/vol_id -u"
185
      VOL_TYPE="$dir/vol_id -t"
186
    fi
187
  done
188
fi
189

    
190
if [ -z "$VOL_ID" ]; then
191
  log_error "vol_id or blkid not found, please install udev or util-linux"
192
  exit 1
193
fi
194

    
195
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
196
  OS_API_VERSION=5
197
  GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
198
  if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
199
  get_api5_arguments $GETOPT_RESULT
200
elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
201
  get_api10_arguments
202
else
203
  log_error "Unknown OS API VERSION $OS_API_VERSION"
204
  exit 1
205
fi
206

    
207
if [ -n "$OS_VARIANT" ]; then
208
  if [ ! -d "$VARIANTS_DIR" ]; then
209
    log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
210
    exit 1
211
  fi
212
  VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
213
  if [ -f "$VARIANT_CONFIG" ]; then
214
    . "$VARIANT_CONFIG"
215
  else
216
    if grep -qxF "$OS_VARIANT" variants.list; then
217
      log_error "ERROR: instance-image configuration error"
218
      log_error "  Published variant $OS_VARIANT is missing its config file"
219
      log_error "  Please create $VARIANT_CONFIG or unpublish the variant"
220
      log_error "  (by removing $OS_VARIANT from variants.list)"
221
    else
222
      log_error "Unofficial variant $OS_VARIANT is unsupported"
223
      log_error "Most probably this is a user error, forcing a wrong name"
224
      log_error "To support this variant please create file $VARIANT_CONFIG"
225
    fi
226
    exit 1
227
  fi
228
fi
229