3 # Copyright (C) 2007, 2008, 2009 Google Inc.
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.
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.
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
25 get_api5_arguments() {
27 # Note the quotes around `$TEMP': they are essential!
28 eval set -- "$GETOPT_RESULT"
31 -i|-n) instance=$2; shift 2;;
33 -o) old_name=$2; shift 2;;
35 -b) blockdev=$2; shift 2;;
37 -s) swapdev=$2; shift 2;;
41 *) log_error "Internal error!" >&2; exit 1;;
44 if [ -z "$instance" -o -z "$blockdev" ]; then
45 log_error "Missing OS API Argument (-i, -n, or -b)"
48 if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev" ]; then
49 log_error "Missing OS API Argument -s (swapdev)"
52 if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name" ]; then
53 log_error "Missing OS API Argument -o (old_name)"
58 get_api10_arguments() {
59 if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then
60 log_error "Missing OS API Variable:"
61 log_error "(INSTANCE_NAME HYPERVISOR or DISK_COUNT)"
64 instance=$INSTANCE_NAME
65 if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then
66 log_error "At least one disk is needed"
69 if [ "$SCRIPT_NAME" = "export" ]; then
70 if [ -z "$EXPORT_DEVICE" ]; then
71 log_error "Missing OS API Variable EXPORT_DEVICE"
73 blockdev=$EXPORT_DEVICE
74 elif [ "$SCRIPT_NAME" = "import" ]; then
75 if [ -z "$IMPORT_DEVICE" ]; then
76 log_error "Missing OS API Variable IMPORT_DEVICE"
78 blockdev=$IMPORT_DEVICE
82 if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then
83 log_error "Missing OS API Variable OLD_INSTANCE_NAME"
85 old_name=$OLD_INSTANCE_NAME
89 # Create one big partition, and make it bootable
90 # some versions of sfdisk need manual specification of
91 # head/sectors for devices such as drbd which don't
93 sfdisk -H 255 -S 63 --quiet --Linux "$1" <<EOF
100 filesystem_dev_base=`kpartx -l -p- $blockdev | \
101 grep -m 1 -- "-1.*$blockdev" | \
103 if [ -z "$filesystem_dev_base" ]; then
104 log_error "Cannot interpret kpartx output and get partition mapping"
107 kpartx -a -p- $blockdev > /dev/null
108 filesystem_dev="/dev/mapper/$filesystem_dev_base"
109 if [ ! -b "$filesystem_dev" ]; then
110 log_error "Can't find kpartx mapped partition: $filesystem_dev"
113 echo "$filesystem_dev"
121 if [ ${#CLEANUP[*]} -gt 0 ]; then
122 LAST_ELEMENT=$((${#CLEANUP[*]}-1))
123 REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
124 for i in $REVERSE_INDEXES; do
130 DEFAULT_FILE="@sysconfdir@/default/ganeti-instance-debootstrap"
131 if [ -f "$DEFAULT_FILE" ]; then
135 # note: we don't set a default mirror since debian and ubuntu have
136 # different defaults, and it's better to use the default
138 # only if the user want to specify a mirror in the defaults file we
139 # will use it, this declaration is to make sure the variable is set
145 : ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-debootstrap.d"}
146 : ${VARIANTS_DIR:="@sysconfdir@/ganeti/instance-debootstrap/variants"}
147 : ${GENERATE_CACHE:="yes"}
148 : ${CLEAN_CACHE:="14"} # number of days to keep a cache file
149 if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
150 DEFAULT_PARTITION_STYLE="none"
152 DEFAULT_PARTITION_STYLE="msdos"
154 : ${PARTITION_STYLE:=$DEFAULT_PARTITION_STYLE} # disk partition style
156 CACHE_DIR="@localstatedir@/cache/ganeti-instance-debootstrap"
158 SCRIPT_NAME=$(basename $0)
160 for dir in /lib/udev /sbin; do
161 if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
166 if [ -z "$VOL_ID" ]; then
167 log_error "vol_id not found, please install udev"
171 if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
173 GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
174 if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
175 get_api5_arguments $GETOPT_RESULT
176 elif [ "$OS_API_VERSION" = "10" ]; then
179 log_error "Unknown OS API VERSION $OS_API_VERSION"
183 if [ -n "$OS_VARIANT" ]; then
184 if [ ! -d "$VARIANTS_DIR" ]; then
185 log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
188 VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
189 if [ -f "$VARIANTS_CONFIG" ]; then
192 if grep -qxF "$OS_VARIANT" variants.list; then
193 log_error "ERROR: instance-debootstrap configuration error"
194 log_error " Published variant $OS_VARIANT is missing its config file"
195 log_error " Please create $VARIANT_CONFIG or unpublish the variant"
196 log_error " (by removing $OS_VARIANT from variants.list)"
198 log_error "Unofficial variant $OS_VARIANT is unsupported"
199 log_error "Most probably this is a user error, forcing a wrong name"
200 log_error "To support this variant please create file $VARIANT_CONFIG"