Statistics
| Branch: | Revision:

root / common.sh.in @ dcd7b382

History | View | Annotate | Download (6 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 one big partition, and make it bootable
98
  # some versions of sfdisk need manual specification of 
99
  # head/sectors for devices such as drbd which don't 
100
  # report geometry
101
  $SFDISK -H 255 -S 63 --quiet --Linux "$1" <<EOF
102
0,,L,*
103
EOF
104
}
105

    
106
map_disk0() {
107
  blockdev="$1"
108
  filesystem_dev_base=`$KPARTX -l -p- $blockdev | \
109
                       grep -m 1 -- "-1.*$blockdev" | \
110
                       $AWK '{print $1}'`
111
  if [ -z "$filesystem_dev_base" ]; then
112
    log_error "Cannot interpret kpartx output and get partition mapping"
113
    exit 1
114
  fi
115
  $KPARTX -a -p- $blockdev > /dev/null
116
  filesystem_dev="/dev/mapper/$filesystem_dev_base"
117
  if [ ! -b "$filesystem_dev" ]; then
118
    log_error "Can't find kpartx mapped partition: $filesystem_dev"
119
    exit 1
120
  fi
121
  echo "$filesystem_dev"
122
}
123

    
124
unmap_disk0() {
125
  $KPARTX -d -p- $1
126
}
127

    
128
cleanup() {
129
  if [ ${#CLEANUP[*]} -gt 0 ]; then
130
    LAST_ELEMENT=$((${#CLEANUP[*]}-1))
131
    REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
132
    for i in $REVERSE_INDEXES; do
133
      ${CLEANUP[$i]}
134
    done
135
  fi
136
}
137

    
138
trap cleanup EXIT
139

    
140
DEFAULT_FILE="@sysconfdir@/default/ganeti-instance-debootstrap"
141
if [ -f "$DEFAULT_FILE" ]; then
142
    . "$DEFAULT_FILE"
143
fi
144

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

    
148
# only if the user want to specify a mirror in the defaults file we
149
# will use it, this declaration is to make sure the variable is set
150
: ${BARE:="no"}
151
: ${IMAGE:=""}
152
: ${ARCH:=""}
153
: ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-debootstrap.d"}
154
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/instance-debootstrap/variants"}
155
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
156
  DEFAULT_PARTITION_STYLE="none"
157
else
158
  DEFAULT_PARTITION_STYLE="msdos"
159
fi
160
: ${PARTITION_STYLE:=$DEFAULT_PARTITION_STYLE} # disk partition style
161

    
162
SCRIPT_NAME=$(basename $0)
163

    
164
if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
165
  VOL_ID="/sbin/blkid -o value -s UUID"
166
  VOL_TYPE="/sbin/blkid -o value -s TYPE"
167
else
168
  for dir in /lib/udev /sbin; do
169
    if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
170
      VOL_ID="$dir/vol_id -u"
171
      VOL_TYPE="$dir/vol_id -t"
172
    fi
173
  done
174
fi
175

    
176
if [ -z "$VOL_ID" ]; then
177
  log_error "vol_id or blkid not found, please install udev or util-linux"
178
  exit 1
179
fi
180

    
181
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
182
  OS_API_VERSION=5
183
  GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
184
  if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
185
  get_api5_arguments $GETOPT_RESULT
186
elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
187
  get_api10_arguments
188
else
189
  log_error "Unknown OS API VERSION $OS_API_VERSION"
190
  exit 1
191
fi
192

    
193
if [ -n "$OS_VARIANT" ]; then
194
  if [ ! -d "$VARIANTS_DIR" ]; then
195
    log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
196
    exit 1
197
  fi
198
  VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
199
  if [ -f "$VARIANT_CONFIG" ]; then
200
    . "$VARIANT_CONFIG"
201
  else
202
    if grep -qxF "$OS_VARIANT" variants.list; then
203
      log_error "ERROR: instance-debootstrap configuration error"
204
      log_error "  Published variant $OS_VARIANT is missing its config file"
205
      log_error "  Please create $VARIANT_CONFIG or unpublish the variant"
206
      log_error "  (by removing $OS_VARIANT from variants.list)"
207
    else
208
      log_error "Unofficial variant $OS_VARIANT is unsupported"
209
      log_error "Most probably this is a user error, forcing a wrong name"
210
      log_error "To support this variant please create file $VARIANT_CONFIG"
211
    fi
212
    exit 1
213
  fi
214
fi
215