Statistics
| Branch: | Revision:

root / common.sh.in @ 611fa6b0

History | View | Annotate | Download (6.5 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
get_api5_arguments() {
35
  GETOPT_RESULT=$*
36
  # Note the quotes around `$TEMP': they are essential!
37
  eval set -- "$GETOPT_RESULT"
38
  while true; do
39
    case "$1" in
40
      -i|-n) instance=$2; shift 2;;
41

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

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

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

    
48
      --) shift; break;;
49

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

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

    
97
format_disk0() {
98
    if [ "${SWAP}" = "yes" ] ; then
99
        # Create three partitions:
100
        # 1 - 100MB /boot, bootable
101
        # 2 - Size of Memory, swap
102
        # 3 - Rest
103
        $SFDISK -uM -H 255 -S 63 --quiet --Linux "$1" <<EOF
104
,100,L,*
105
,$INSTANCE_BE_memory,S
106
,,L
107
EOF
108
    else
109
        # Create two partitions:
110
        # 1 - 100MB /boot, bootable
111
        # 2 - Rest
112
        $SFDISK -uM -H 255 -S 63 --quiet --Linux "$1" <<EOF
113
,100,L,*
114
,,L
115
EOF
116
    fi
117
}
118

    
119
mount_disk0() {
120
    local target=$1 root_dev=$2 boot_dev=$3
121
    mount $root_dev $target
122
    CLEANUP+=("umount $target")
123
    $MKDIR_P $target/boot
124
    mount $boot_dev $target/boot
125
    CLEANUP+=("umount $target/boot")
126
}
127

    
128
map_disk0() {
129
  blockdev="$1"
130
  filesystem_dev_base=`$KPARTX -l -p- $blockdev | \
131
                       grep -m 1 -- "-1.*$blockdev" | \
132
                       $AWK '{print $1}'`
133
  if [ -z "$filesystem_dev_base" ]; then
134
    log_error "Cannot interpret kpartx output and get partition mapping"
135
    exit 1
136
  fi
137
  $KPARTX -a -p- $blockdev > /dev/null
138
  filesystem_dev="/dev/mapper/${filesystem_dev_base/-1/}"
139
  if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then
140
    log_error "Can't find kpartx mapped partition: /dev/mapper/$filesystem_dev_base"
141
    exit 1
142
  fi
143
  echo "$filesystem_dev"
144
}
145

    
146
unmap_disk0() {
147
  $KPARTX -d -p- $1
148
}
149

    
150
cleanup() {
151
  if [ ${#CLEANUP[*]} -gt 0 ]; then
152
    LAST_ELEMENT=$((${#CLEANUP[*]}-1))
153
    REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
154
    for i in $REVERSE_INDEXES; do
155
      ${CLEANUP[$i]}
156
    done
157
  fi
158
}
159

    
160
trap cleanup EXIT
161

    
162
DEFAULT_FILE="@DEFAULTS_DIR@/ganeti-instance-image"
163
if [ -f "$DEFAULT_FILE" ]; then
164
    . "$DEFAULT_FILE"
165
fi
166

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

    
170
# only if the user want to specify a mirror in the defaults file we
171
# will use it, this declaration is to make sure the variable is set
172
: ${CDINSTALL:="yes"}
173
: ${SWAP:="yes"}
174
: ${IMAGE_NAME:=""}
175
: ${IMAGE_TYPE:="qemu"}
176
: ${ARCH:=""}
177
: ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-image.d"}
178
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/instance-image/variants"}
179
: ${IMAGE_DIR:="@localstatedir@/cache/ganeti-instance-image"}
180
: ${IMAGE_DEBUG:="0"}
181

    
182
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
183
  DEFAULT_PARTITION_STYLE="none"
184
else
185
  DEFAULT_PARTITION_STYLE="msdos"
186
fi
187
: ${PARTITION_STYLE:=$DEFAULT_PARTITION_STYLE} # disk partition style
188

    
189

    
190
SCRIPT_NAME=$(basename $0)
191

    
192
if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
193
  VOL_ID="/sbin/blkid -o value -s UUID"
194
  VOL_TYPE="/sbin/blkid -o value -s TYPE"
195
else
196
  for dir in /lib/udev /sbin; do
197
    if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
198
      VOL_ID="$dir/vol_id -u"
199
      VOL_TYPE="$dir/vol_id -t"
200
    fi
201
  done
202
fi
203

    
204
if [ -z "$VOL_ID" ]; then
205
  log_error "vol_id or blkid not found, please install udev or util-linux"
206
  exit 1
207
fi
208

    
209
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
210
  OS_API_VERSION=5
211
  GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
212
  if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
213
  get_api5_arguments $GETOPT_RESULT
214
elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
215
  get_api10_arguments
216
else
217
  log_error "Unknown OS API VERSION $OS_API_VERSION"
218
  exit 1
219
fi
220

    
221
if [ -n "$OS_VARIANT" ]; then
222
  if [ ! -d "$VARIANTS_DIR" ]; then
223
    log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
224
    exit 1
225
  fi
226
  VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
227
  if [ -f "$VARIANT_CONFIG" ]; then
228
    . "$VARIANT_CONFIG"
229
  else
230
    if grep -qxF "$OS_VARIANT" variants.list; then
231
      log_error "ERROR: instance-image configuration error"
232
      log_error "  Published variant $OS_VARIANT is missing its config file"
233
      log_error "  Please create $VARIANT_CONFIG or unpublish the variant"
234
      log_error "  (by removing $OS_VARIANT from variants.list)"
235
    else
236
      log_error "Unofficial variant $OS_VARIANT is unsupported"
237
      log_error "Most probably this is a user error, forcing a wrong name"
238
      log_error "To support this variant please create file $VARIANT_CONFIG"
239
    fi
240
    exit 1
241
  fi
242
fi
243