Statistics
| Branch: | Revision:

root / common.sh.in @ cc927a76

History | View | Annotate | Download (6.5 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 ab712029 Lance Albertson
QEMU_IMG="@QEMU_IMG@"
26 8d0132dc Lance Albertson
MKDIR_P="@MKDIR_P@"
27 8d0132dc Lance Albertson
28 79224631 Lance Albertson
CLEANUP=( )
29 79224631 Lance Albertson
30 79224631 Lance Albertson
log_error() {
31 79224631 Lance Albertson
  echo "$@" >&2
32 79224631 Lance Albertson
}
33 79224631 Lance Albertson
34 79224631 Lance Albertson
get_api5_arguments() {
35 79224631 Lance Albertson
  GETOPT_RESULT=$*
36 79224631 Lance Albertson
  # Note the quotes around `$TEMP': they are essential!
37 79224631 Lance Albertson
  eval set -- "$GETOPT_RESULT"
38 79224631 Lance Albertson
  while true; do
39 79224631 Lance Albertson
    case "$1" in
40 79224631 Lance Albertson
      -i|-n) instance=$2; shift 2;;
41 79224631 Lance Albertson
42 79224631 Lance Albertson
      -o) old_name=$2; shift 2;;
43 79224631 Lance Albertson
44 79224631 Lance Albertson
      -b) blockdev=$2; shift 2;;
45 79224631 Lance Albertson
46 79224631 Lance Albertson
      -s) swapdev=$2; shift 2;;
47 79224631 Lance Albertson
48 79224631 Lance Albertson
      --) shift; break;;
49 79224631 Lance Albertson
50 79224631 Lance Albertson
      *)  log_error "Internal error!" >&2; exit 1;;
51 79224631 Lance Albertson
    esac
52 79224631 Lance Albertson
  done
53 79224631 Lance Albertson
  if [ -z "$instance" -o -z "$blockdev" ]; then
54 79224631 Lance Albertson
    log_error "Missing OS API Argument (-i, -n, or -b)"
55 79224631 Lance Albertson
    exit 1
56 79224631 Lance Albertson
  fi
57 79224631 Lance Albertson
  if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev"  ]; then
58 79224631 Lance Albertson
    log_error "Missing OS API Argument -s (swapdev)"
59 79224631 Lance Albertson
    exit 1
60 79224631 Lance Albertson
  fi
61 79224631 Lance Albertson
  if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name"  ]; then
62 79224631 Lance Albertson
    log_error "Missing OS API Argument -o (old_name)"
63 79224631 Lance Albertson
    exit 1
64 79224631 Lance Albertson
  fi
65 79224631 Lance Albertson
}
66 79224631 Lance Albertson
67 79224631 Lance Albertson
get_api10_arguments() {
68 79224631 Lance Albertson
  if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then
69 79224631 Lance Albertson
    log_error "Missing OS API Variable:"
70 79224631 Lance Albertson
    log_error "(INSTANCE_NAME HYPERVISOR or DISK_COUNT)"
71 79224631 Lance Albertson
    exit 1
72 79224631 Lance Albertson
  fi
73 79224631 Lance Albertson
  instance=$INSTANCE_NAME
74 79224631 Lance Albertson
  if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then
75 79224631 Lance Albertson
    log_error "At least one disk is needed"
76 79224631 Lance Albertson
    exit 1
77 79224631 Lance Albertson
  fi
78 79224631 Lance Albertson
  if [ "$SCRIPT_NAME" = "export" ]; then
79 79224631 Lance Albertson
    if [ -z "$EXPORT_DEVICE" ]; then
80 79224631 Lance Albertson
      log_error "Missing OS API Variable EXPORT_DEVICE"
81 79224631 Lance Albertson
    fi
82 79224631 Lance Albertson
    blockdev=$EXPORT_DEVICE
83 79224631 Lance Albertson
  elif [ "$SCRIPT_NAME" = "import" ]; then
84 79224631 Lance Albertson
    if [ -z "$IMPORT_DEVICE" ]; then
85 79224631 Lance Albertson
       log_error "Missing OS API Variable IMPORT_DEVICE"
86 79224631 Lance Albertson
    fi
87 79224631 Lance Albertson
    blockdev=$IMPORT_DEVICE
88 79224631 Lance Albertson
  else
89 79224631 Lance Albertson
    blockdev=$DISK_0_PATH
90 79224631 Lance Albertson
  fi
91 79224631 Lance Albertson
  if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then
92 79224631 Lance Albertson
    log_error "Missing OS API Variable OLD_INSTANCE_NAME"
93 79224631 Lance Albertson
  fi
94 79224631 Lance Albertson
  old_name=$OLD_INSTANCE_NAME
95 79224631 Lance Albertson
}
96 79224631 Lance Albertson
97 f6f223b8 Lance Albertson
get_os_type() {
98 f6f223b8 Lance Albertson
    if [ -e ${TARGET}/etc/redhat-release ] ; then
99 f6f223b8 Lance Albertson
        OS_TYPE="redhat"
100 f6f223b8 Lance Albertson
    elif [ -e ${TARGET}/etc/debian_version ] ; then
101 f6f223b8 Lance Albertson
        OS_TYPE="debian"
102 f6f223b8 Lance Albertson
    elif [ -e ${TARGET}/etc/gentoo-release ] ; then
103 f6f223b8 Lance Albertson
        OS_TYPE="gentoo"
104 f6f223b8 Lance Albertson
    elif [ -e ${TARGET}/etc/SuSE-release ] ; then
105 f6f223b8 Lance Albertson
        OS_TYPE="suse"
106 f6f223b8 Lance Albertson
    fi
107 f6f223b8 Lance Albertson
}
108 f6f223b8 Lance Albertson
109 79224631 Lance Albertson
format_disk0() {
110 e21b8802 Lance Albertson
    if [ "${SWAP}" = "yes" ] ; then
111 e21b8802 Lance Albertson
        # Create three partitions:
112 e21b8802 Lance Albertson
        # 1 - 100MB /boot, bootable
113 e21b8802 Lance Albertson
        # 2 - Size of Memory, swap
114 e21b8802 Lance Albertson
        # 3 - Rest
115 e21b8802 Lance Albertson
        $SFDISK -uM -H 255 -S 63 --quiet --Linux "$1" <<EOF
116 22b570d6 Lance Albertson
,100,L,*
117 240140b7 Lance Albertson
,$INSTANCE_BE_memory,S
118 22b570d6 Lance Albertson
,,L
119 79224631 Lance Albertson
EOF
120 e21b8802 Lance Albertson
    else
121 e21b8802 Lance Albertson
        # Create two partitions:
122 e21b8802 Lance Albertson
        # 1 - 100MB /boot, bootable
123 e21b8802 Lance Albertson
        # 2 - Rest
124 e21b8802 Lance Albertson
        $SFDISK -uM -H 255 -S 63 --quiet --Linux "$1" <<EOF
125 e21b8802 Lance Albertson
,100,L,*
126 e21b8802 Lance Albertson
,,L
127 e21b8802 Lance Albertson
EOF
128 e21b8802 Lance Albertson
    fi
129 79224631 Lance Albertson
}
130 79224631 Lance Albertson
131 cc927a76 Lance Albertson
mkfs_disk0() {
132 cc927a76 Lance Albertson
    local boot_dev=$1 root_dev=$2 swap_dev=$3
133 cc927a76 Lance Albertson
    # Format /boot
134 cc927a76 Lance Albertson
    mke2fs -Fjq -L /boot $boot_dev
135 cc927a76 Lance Albertson
    # Format /
136 cc927a76 Lance Albertson
    mke2fs -Fjq -L / $root_dev
137 cc927a76 Lance Albertson
    if [ "${SWAP}" = "yes" ] ; then
138 cc927a76 Lance Albertson
        # Format swap
139 cc927a76 Lance Albertson
        mkswap $swap_dev
140 cc927a76 Lance Albertson
    fi
141 cc927a76 Lance Albertson
}
142 cc927a76 Lance Albertson
143 b05b1ab6 Lance Albertson
mount_disk0() {
144 3173a4bc Lance Albertson
    local target=$1 root_dev=$2 boot_dev=$3
145 3173a4bc Lance Albertson
    mount $root_dev $target
146 3173a4bc Lance Albertson
    CLEANUP+=("umount $target")
147 3173a4bc Lance Albertson
    $MKDIR_P $target/boot
148 3173a4bc Lance Albertson
    mount $boot_dev $target/boot
149 3173a4bc Lance Albertson
    CLEANUP+=("umount $target/boot")
150 b05b1ab6 Lance Albertson
}
151 b05b1ab6 Lance Albertson
152 79224631 Lance Albertson
map_disk0() {
153 79224631 Lance Albertson
  blockdev="$1"
154 8d0132dc Lance Albertson
  filesystem_dev_base=`$KPARTX -l -p- $blockdev | \
155 79224631 Lance Albertson
                       grep -m 1 -- "-1.*$blockdev" | \
156 8d0132dc Lance Albertson
                       $AWK '{print $1}'`
157 79224631 Lance Albertson
  if [ -z "$filesystem_dev_base" ]; then
158 79224631 Lance Albertson
    log_error "Cannot interpret kpartx output and get partition mapping"
159 79224631 Lance Albertson
    exit 1
160 79224631 Lance Albertson
  fi
161 8d0132dc Lance Albertson
  $KPARTX -a -p- $blockdev > /dev/null
162 a1d43218 Lance Albertson
  filesystem_dev="/dev/mapper/${filesystem_dev_base/-1/}"
163 25135be9 Lance Albertson
  if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then
164 25135be9 Lance Albertson
    log_error "Can't find kpartx mapped partition: /dev/mapper/$filesystem_dev_base"
165 79224631 Lance Albertson
    exit 1
166 79224631 Lance Albertson
  fi
167 79224631 Lance Albertson
  echo "$filesystem_dev"
168 79224631 Lance Albertson
}
169 79224631 Lance Albertson
170 79224631 Lance Albertson
unmap_disk0() {
171 8d0132dc Lance Albertson
  $KPARTX -d -p- $1
172 79224631 Lance Albertson
}
173 79224631 Lance Albertson
174 79224631 Lance Albertson
cleanup() {
175 79224631 Lance Albertson
  if [ ${#CLEANUP[*]} -gt 0 ]; then
176 79224631 Lance Albertson
    LAST_ELEMENT=$((${#CLEANUP[*]}-1))
177 79224631 Lance Albertson
    REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
178 79224631 Lance Albertson
    for i in $REVERSE_INDEXES; do
179 79224631 Lance Albertson
      ${CLEANUP[$i]}
180 79224631 Lance Albertson
    done
181 79224631 Lance Albertson
  fi
182 79224631 Lance Albertson
}
183 79224631 Lance Albertson
184 79224631 Lance Albertson
trap cleanup EXIT
185 79224631 Lance Albertson
186 c4661256 Lance Albertson
DEFAULT_FILE="@DEFAULTS_DIR@/ganeti-instance-image"
187 79224631 Lance Albertson
if [ -f "$DEFAULT_FILE" ]; then
188 79224631 Lance Albertson
    . "$DEFAULT_FILE"
189 79224631 Lance Albertson
fi
190 79224631 Lance Albertson
191 79224631 Lance Albertson
# note: we don't set a default mirror since debian and ubuntu have
192 79224631 Lance Albertson
# different defaults, and it's better to use the default
193 79224631 Lance Albertson
194 79224631 Lance Albertson
# only if the user want to specify a mirror in the defaults file we
195 79224631 Lance Albertson
# will use it, this declaration is to make sure the variable is set
196 fcce5224 Lance Albertson
: ${CDINSTALL:="yes"}
197 e21b8802 Lance Albertson
: ${SWAP:="yes"}
198 134c11c3 Lance Albertson
: ${IMAGE_NAME:=""}
199 b05b1ab6 Lance Albertson
: ${IMAGE_TYPE:="qemu"}
200 79224631 Lance Albertson
: ${ARCH:=""}
201 f5aa7725 Lance Albertson
: ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-image.d"}
202 f5aa7725 Lance Albertson
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/instance-image/variants"}
203 2e0988ec Lance Albertson
: ${IMAGE_DIR:="@localstatedir@/cache/ganeti-instance-image"}
204 611fa6b0 Lance Albertson
: ${IMAGE_DEBUG:="0"}
205 f7959feb Lance Albertson
206 79224631 Lance Albertson
SCRIPT_NAME=$(basename $0)
207 79224631 Lance Albertson
208 79224631 Lance Albertson
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
209 79224631 Lance Albertson
  OS_API_VERSION=5
210 79224631 Lance Albertson
  GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
211 79224631 Lance Albertson
  if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
212 79224631 Lance Albertson
  get_api5_arguments $GETOPT_RESULT
213 79224631 Lance Albertson
elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
214 79224631 Lance Albertson
  get_api10_arguments
215 79224631 Lance Albertson
else
216 79224631 Lance Albertson
  log_error "Unknown OS API VERSION $OS_API_VERSION"
217 79224631 Lance Albertson
  exit 1
218 79224631 Lance Albertson
fi
219 79224631 Lance Albertson
220 79224631 Lance Albertson
if [ -n "$OS_VARIANT" ]; then
221 79224631 Lance Albertson
  if [ ! -d "$VARIANTS_DIR" ]; then
222 79224631 Lance Albertson
    log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
223 79224631 Lance Albertson
    exit 1
224 79224631 Lance Albertson
  fi
225 79224631 Lance Albertson
  VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
226 79224631 Lance Albertson
  if [ -f "$VARIANT_CONFIG" ]; then
227 79224631 Lance Albertson
    . "$VARIANT_CONFIG"
228 79224631 Lance Albertson
  else
229 79224631 Lance Albertson
    if grep -qxF "$OS_VARIANT" variants.list; then
230 f5aa7725 Lance Albertson
      log_error "ERROR: instance-image configuration error"
231 79224631 Lance Albertson
      log_error "  Published variant $OS_VARIANT is missing its config file"
232 79224631 Lance Albertson
      log_error "  Please create $VARIANT_CONFIG or unpublish the variant"
233 79224631 Lance Albertson
      log_error "  (by removing $OS_VARIANT from variants.list)"
234 79224631 Lance Albertson
    else
235 79224631 Lance Albertson
      log_error "Unofficial variant $OS_VARIANT is unsupported"
236 79224631 Lance Albertson
      log_error "Most probably this is a user error, forcing a wrong name"
237 79224631 Lance Albertson
      log_error "To support this variant please create file $VARIANT_CONFIG"
238 79224631 Lance Albertson
    fi
239 79224631 Lance Albertson
    exit 1
240 79224631 Lance Albertson
  fi
241 79224631 Lance Albertson
fi