Statistics
| Branch: | Revision:

root / common.sh.in @ 2e0988ec

History | View | Annotate | Download (6 kB)

1 79224631 Lance Albertson
#
2 79224631 Lance Albertson
3 79224631 Lance Albertson
# Copyright (C) 2007, 2008, 2009 Google Inc.
4 79224631 Lance Albertson
#
5 79224631 Lance Albertson
# This program is free software; you can redistribute it and/or modify
6 79224631 Lance Albertson
# it under the terms of the GNU General Public License as published by
7 79224631 Lance Albertson
# the Free Software Foundation; either version 2 of the License, or
8 79224631 Lance Albertson
# (at your option) any later version.
9 79224631 Lance Albertson
#
10 79224631 Lance Albertson
# This program is distributed in the hope that it will be useful, but
11 79224631 Lance Albertson
# WITHOUT ANY WARRANTY; without even the implied warranty of
12 79224631 Lance Albertson
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 79224631 Lance Albertson
# General Public License for more details.
14 79224631 Lance Albertson
#
15 79224631 Lance Albertson
# You should have received a copy of the GNU General Public License
16 79224631 Lance Albertson
# along with this program; if not, write to the Free Software
17 79224631 Lance Albertson
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 79224631 Lance Albertson
# 02110-1301, USA.
19 79224631 Lance Albertson
20 8d0132dc Lance Albertson
AWK="@AWK@"
21 8d0132dc Lance Albertson
DUMP="@DUMP@"
22 8d0132dc Lance Albertson
LOSETUP="@LOSETUP@"
23 8d0132dc Lance Albertson
KPARTX="@KPARTX@"
24 8d0132dc Lance Albertson
SFDISK="@SFDISK@"
25 8d0132dc Lance Albertson
MKDIR_P="@MKDIR_P@"
26 8d0132dc Lance Albertson
27 79224631 Lance Albertson
CLEANUP=( )
28 79224631 Lance Albertson
29 79224631 Lance Albertson
log_error() {
30 79224631 Lance Albertson
  echo "$@" >&2
31 79224631 Lance Albertson
}
32 79224631 Lance Albertson
33 79224631 Lance Albertson
get_api5_arguments() {
34 79224631 Lance Albertson
  GETOPT_RESULT=$*
35 79224631 Lance Albertson
  # Note the quotes around `$TEMP': they are essential!
36 79224631 Lance Albertson
  eval set -- "$GETOPT_RESULT"
37 79224631 Lance Albertson
  while true; do
38 79224631 Lance Albertson
    case "$1" in
39 79224631 Lance Albertson
      -i|-n) instance=$2; shift 2;;
40 79224631 Lance Albertson
41 79224631 Lance Albertson
      -o) old_name=$2; shift 2;;
42 79224631 Lance Albertson
43 79224631 Lance Albertson
      -b) blockdev=$2; shift 2;;
44 79224631 Lance Albertson
45 79224631 Lance Albertson
      -s) swapdev=$2; shift 2;;
46 79224631 Lance Albertson
47 79224631 Lance Albertson
      --) shift; break;;
48 79224631 Lance Albertson
49 79224631 Lance Albertson
      *)  log_error "Internal error!" >&2; exit 1;;
50 79224631 Lance Albertson
    esac
51 79224631 Lance Albertson
  done
52 79224631 Lance Albertson
  if [ -z "$instance" -o -z "$blockdev" ]; then
53 79224631 Lance Albertson
    log_error "Missing OS API Argument (-i, -n, or -b)"
54 79224631 Lance Albertson
    exit 1
55 79224631 Lance Albertson
  fi
56 79224631 Lance Albertson
  if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev"  ]; then
57 79224631 Lance Albertson
    log_error "Missing OS API Argument -s (swapdev)"
58 79224631 Lance Albertson
    exit 1
59 79224631 Lance Albertson
  fi
60 79224631 Lance Albertson
  if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name"  ]; then
61 79224631 Lance Albertson
    log_error "Missing OS API Argument -o (old_name)"
62 79224631 Lance Albertson
    exit 1
63 79224631 Lance Albertson
  fi
64 79224631 Lance Albertson
}
65 79224631 Lance Albertson
66 79224631 Lance Albertson
get_api10_arguments() {
67 79224631 Lance Albertson
  if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then
68 79224631 Lance Albertson
    log_error "Missing OS API Variable:"
69 79224631 Lance Albertson
    log_error "(INSTANCE_NAME HYPERVISOR or DISK_COUNT)"
70 79224631 Lance Albertson
    exit 1
71 79224631 Lance Albertson
  fi
72 79224631 Lance Albertson
  instance=$INSTANCE_NAME
73 79224631 Lance Albertson
  if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then
74 79224631 Lance Albertson
    log_error "At least one disk is needed"
75 79224631 Lance Albertson
    exit 1
76 79224631 Lance Albertson
  fi
77 79224631 Lance Albertson
  if [ "$SCRIPT_NAME" = "export" ]; then
78 79224631 Lance Albertson
    if [ -z "$EXPORT_DEVICE" ]; then
79 79224631 Lance Albertson
      log_error "Missing OS API Variable EXPORT_DEVICE"
80 79224631 Lance Albertson
    fi
81 79224631 Lance Albertson
    blockdev=$EXPORT_DEVICE
82 79224631 Lance Albertson
  elif [ "$SCRIPT_NAME" = "import" ]; then
83 79224631 Lance Albertson
    if [ -z "$IMPORT_DEVICE" ]; then
84 79224631 Lance Albertson
       log_error "Missing OS API Variable IMPORT_DEVICE"
85 79224631 Lance Albertson
    fi
86 79224631 Lance Albertson
    blockdev=$IMPORT_DEVICE
87 79224631 Lance Albertson
  else
88 79224631 Lance Albertson
    blockdev=$DISK_0_PATH
89 79224631 Lance Albertson
  fi
90 79224631 Lance Albertson
  if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then
91 79224631 Lance Albertson
    log_error "Missing OS API Variable OLD_INSTANCE_NAME"
92 79224631 Lance Albertson
  fi
93 79224631 Lance Albertson
  old_name=$OLD_INSTANCE_NAME
94 79224631 Lance Albertson
}
95 79224631 Lance Albertson
96 79224631 Lance Albertson
format_disk0() {
97 22b570d6 Lance Albertson
  # Create three partitions:
98 22b570d6 Lance Albertson
  # 1 - 100MB /boot, bootable
99 22b570d6 Lance Albertson
  # 2 - Size of Memory, swap
100 22b570d6 Lance Albertson
  # 3 - Rest
101 22b570d6 Lance Albertson
  $SFDISK -uM -H 255 -S 63 --quiet --Linux "$1" <<EOF
102 22b570d6 Lance Albertson
,100,L,*
103 240140b7 Lance Albertson
,$INSTANCE_BE_memory,S
104 22b570d6 Lance Albertson
,,L
105 79224631 Lance Albertson
EOF
106 79224631 Lance Albertson
}
107 79224631 Lance Albertson
108 79224631 Lance Albertson
map_disk0() {
109 79224631 Lance Albertson
  blockdev="$1"
110 8d0132dc Lance Albertson
  filesystem_dev_base=`$KPARTX -l -p- $blockdev | \
111 79224631 Lance Albertson
                       grep -m 1 -- "-1.*$blockdev" | \
112 8d0132dc Lance Albertson
                       $AWK '{print $1}'`
113 79224631 Lance Albertson
  if [ -z "$filesystem_dev_base" ]; then
114 79224631 Lance Albertson
    log_error "Cannot interpret kpartx output and get partition mapping"
115 79224631 Lance Albertson
    exit 1
116 79224631 Lance Albertson
  fi
117 8d0132dc Lance Albertson
  $KPARTX -a -p- $blockdev > /dev/null
118 a1d43218 Lance Albertson
  filesystem_dev="/dev/mapper/${filesystem_dev_base/-1/}"
119 25135be9 Lance Albertson
  if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then
120 25135be9 Lance Albertson
    log_error "Can't find kpartx mapped partition: /dev/mapper/$filesystem_dev_base"
121 79224631 Lance Albertson
    exit 1
122 79224631 Lance Albertson
  fi
123 79224631 Lance Albertson
  echo "$filesystem_dev"
124 79224631 Lance Albertson
}
125 79224631 Lance Albertson
126 79224631 Lance Albertson
unmap_disk0() {
127 8d0132dc Lance Albertson
  $KPARTX -d -p- $1
128 79224631 Lance Albertson
}
129 79224631 Lance Albertson
130 79224631 Lance Albertson
cleanup() {
131 79224631 Lance Albertson
  if [ ${#CLEANUP[*]} -gt 0 ]; then
132 79224631 Lance Albertson
    LAST_ELEMENT=$((${#CLEANUP[*]}-1))
133 79224631 Lance Albertson
    REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
134 79224631 Lance Albertson
    for i in $REVERSE_INDEXES; do
135 79224631 Lance Albertson
      ${CLEANUP[$i]}
136 79224631 Lance Albertson
    done
137 79224631 Lance Albertson
  fi
138 79224631 Lance Albertson
}
139 79224631 Lance Albertson
140 79224631 Lance Albertson
trap cleanup EXIT
141 79224631 Lance Albertson
142 c4661256 Lance Albertson
DEFAULT_FILE="@DEFAULTS_DIR@/ganeti-instance-image"
143 79224631 Lance Albertson
if [ -f "$DEFAULT_FILE" ]; then
144 79224631 Lance Albertson
    . "$DEFAULT_FILE"
145 79224631 Lance Albertson
fi
146 79224631 Lance Albertson
147 79224631 Lance Albertson
# note: we don't set a default mirror since debian and ubuntu have
148 79224631 Lance Albertson
# different defaults, and it's better to use the default
149 79224631 Lance Albertson
150 79224631 Lance Albertson
# only if the user want to specify a mirror in the defaults file we
151 79224631 Lance Albertson
# will use it, this declaration is to make sure the variable is set
152 fcce5224 Lance Albertson
: ${CDINSTALL:="yes"}
153 134c11c3 Lance Albertson
: ${IMAGE_NAME:=""}
154 79224631 Lance Albertson
: ${ARCH:=""}
155 f5aa7725 Lance Albertson
: ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-image.d"}
156 f5aa7725 Lance Albertson
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/instance-image/variants"}
157 2e0988ec Lance Albertson
: ${IMAGE_DIR:="@localstatedir@/cache/ganeti-instance-image"}
158 79224631 Lance Albertson
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
159 79224631 Lance Albertson
  DEFAULT_PARTITION_STYLE="none"
160 79224631 Lance Albertson
else
161 79224631 Lance Albertson
  DEFAULT_PARTITION_STYLE="msdos"
162 79224631 Lance Albertson
fi
163 79224631 Lance Albertson
: ${PARTITION_STYLE:=$DEFAULT_PARTITION_STYLE} # disk partition style
164 79224631 Lance Albertson
165 134c11c3 Lance Albertson
166 79224631 Lance Albertson
SCRIPT_NAME=$(basename $0)
167 79224631 Lance Albertson
168 79224631 Lance Albertson
if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
169 79224631 Lance Albertson
  VOL_ID="/sbin/blkid -o value -s UUID"
170 79224631 Lance Albertson
  VOL_TYPE="/sbin/blkid -o value -s TYPE"
171 79224631 Lance Albertson
else
172 79224631 Lance Albertson
  for dir in /lib/udev /sbin; do
173 79224631 Lance Albertson
    if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
174 79224631 Lance Albertson
      VOL_ID="$dir/vol_id -u"
175 79224631 Lance Albertson
      VOL_TYPE="$dir/vol_id -t"
176 79224631 Lance Albertson
    fi
177 79224631 Lance Albertson
  done
178 79224631 Lance Albertson
fi
179 79224631 Lance Albertson
180 79224631 Lance Albertson
if [ -z "$VOL_ID" ]; then
181 79224631 Lance Albertson
  log_error "vol_id or blkid not found, please install udev or util-linux"
182 79224631 Lance Albertson
  exit 1
183 79224631 Lance Albertson
fi
184 79224631 Lance Albertson
185 79224631 Lance Albertson
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
186 79224631 Lance Albertson
  OS_API_VERSION=5
187 79224631 Lance Albertson
  GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
188 79224631 Lance Albertson
  if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
189 79224631 Lance Albertson
  get_api5_arguments $GETOPT_RESULT
190 79224631 Lance Albertson
elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
191 79224631 Lance Albertson
  get_api10_arguments
192 79224631 Lance Albertson
else
193 79224631 Lance Albertson
  log_error "Unknown OS API VERSION $OS_API_VERSION"
194 79224631 Lance Albertson
  exit 1
195 79224631 Lance Albertson
fi
196 79224631 Lance Albertson
197 79224631 Lance Albertson
if [ -n "$OS_VARIANT" ]; then
198 79224631 Lance Albertson
  if [ ! -d "$VARIANTS_DIR" ]; then
199 79224631 Lance Albertson
    log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
200 79224631 Lance Albertson
    exit 1
201 79224631 Lance Albertson
  fi
202 79224631 Lance Albertson
  VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
203 79224631 Lance Albertson
  if [ -f "$VARIANT_CONFIG" ]; then
204 79224631 Lance Albertson
    . "$VARIANT_CONFIG"
205 79224631 Lance Albertson
  else
206 79224631 Lance Albertson
    if grep -qxF "$OS_VARIANT" variants.list; then
207 f5aa7725 Lance Albertson
      log_error "ERROR: instance-image configuration error"
208 79224631 Lance Albertson
      log_error "  Published variant $OS_VARIANT is missing its config file"
209 79224631 Lance Albertson
      log_error "  Please create $VARIANT_CONFIG or unpublish the variant"
210 79224631 Lance Albertson
      log_error "  (by removing $OS_VARIANT from variants.list)"
211 79224631 Lance Albertson
    else
212 79224631 Lance Albertson
      log_error "Unofficial variant $OS_VARIANT is unsupported"
213 79224631 Lance Albertson
      log_error "Most probably this is a user error, forcing a wrong name"
214 79224631 Lance Albertson
      log_error "To support this variant please create file $VARIANT_CONFIG"
215 79224631 Lance Albertson
    fi
216 79224631 Lance Albertson
    exit 1
217 79224631 Lance Albertson
  fi
218 79224631 Lance Albertson
fi