3 # Copyright (C) 2007, 2008, 2009, 2012 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
26 get_api5_arguments() {
28 # Note the quotes around `$TEMP': they are essential!
29 eval set -- "$GETOPT_RESULT"
32 -i|-n) instance=$2; shift 2;;
34 -o) old_name=$2; shift 2;;
36 -b) blockdev=$2; shift 2;;
38 -s) swapdev=$2; shift 2;;
42 *) log_error "Internal error!" >&2; exit 1;;
45 if [ -z "$instance" -o -z "$blockdev" ]; then
46 log_error "Missing OS API Argument (-i, -n, or -b)"
49 if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev" ]; then
50 log_error "Missing OS API Argument -s (swapdev)"
53 if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name" ]; then
54 log_error "Missing OS API Argument -o (old_name)"
59 get_api10_arguments() {
60 if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then
61 log_error "Missing OS API Variable:"
62 log_error "(INSTANCE_NAME HYPERVISOR or DISK_COUNT)"
65 instance=$INSTANCE_NAME
66 if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then
67 log_error "At least one disk is needed"
70 if [ "$SCRIPT_NAME" = "export" ]; then
71 if [ -z "$EXPORT_DEVICE" ]; then
72 log_error "Missing OS API Variable EXPORT_DEVICE"
74 blockdev=$EXPORT_DEVICE
75 elif [ "$SCRIPT_NAME" = "import" ]; then
76 if [ -z "$IMPORT_DEVICE" ]; then
77 log_error "Missing OS API Variable IMPORT_DEVICE"
79 blockdev=$IMPORT_DEVICE
83 if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then
84 log_error "Missing OS API Variable OLD_INSTANCE_NAME"
86 old_name=$OLD_INSTANCE_NAME
90 # Create one big partition, and make it bootable
91 # some versions of sfdisk need manual specification of
92 # head/sectors for devices such as drbd which don't
94 sfdisk -H 64 -S 32 -u S --quiet --Linux "$1" <<EOF
95 ${PARTITION_ALIGNMENT},,L,*
101 filesystem_dev_base=`kpartx -l -p- $blockdev | \
102 grep -m 1 -- "-1.*$blockdev" | \
104 if [ -z "$filesystem_dev_base" ]; then
105 log_error "Cannot interpret kpartx output and get partition mapping"
108 kpartx -a -p- $blockdev > /dev/null
109 filesystem_dev="/dev/mapper/$filesystem_dev_base"
110 if [ ! -b "$filesystem_dev" ]; then
111 log_error "Can't find kpartx mapped partition: $filesystem_dev"
114 echo "$filesystem_dev"
122 if [ ${#CLEANUP[*]} -gt 0 ]; then
123 LAST_ELEMENT=$((${#CLEANUP[*]}-1))
124 REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
125 for i in $REVERSE_INDEXES; do
133 DEFAULT_FILE="@sysconfdir@/default/ganeti-instance-debootstrap"
134 if [ -f "$DEFAULT_FILE" ]; then
138 # note: we don't set a default mirror since debian and ubuntu have
139 # different defaults, and it's better to use the default
141 # only if the user want to specify a mirror in the defaults file we
142 # will use it, this declaration is to make sure the variable is set
145 : ${SUITE:="squeeze"}
149 : ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-debootstrap/hooks"}
150 : ${VARIANTS_DIR:="@sysconfdir@/ganeti/instance-debootstrap/variants"}
151 : ${GENERATE_CACHE:="yes"}
152 : ${CLEAN_CACHE:="14"} # number of days to keep a cache file
153 : ${PARTITION_ALIGNMENT:=2048} # sectors to align partition (1Mib default)
154 if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
155 DEFAULT_PARTITION_STYLE="none"
157 DEFAULT_PARTITION_STYLE="msdos"
159 : ${PARTITION_STYLE:=$DEFAULT_PARTITION_STYLE} # disk partition style
161 CACHE_DIR="@localstatedir@/cache/ganeti-instance-debootstrap"
163 SCRIPT_NAME=$(basename $0)
165 if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
166 VOL_ID="/sbin/blkid -o value -s UUID"
167 VOL_TYPE="/sbin/blkid -o value -s TYPE"
169 for dir in /lib/udev /sbin; do
170 if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
171 VOL_ID="$dir/vol_id -u"
172 VOL_TYPE="$dir/vol_id -t"
177 if [ -z "$VOL_ID" ]; then
178 log_error "vol_id or blkid not found, please install udev or util-linux"
182 if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
184 GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
185 if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
186 get_api5_arguments $GETOPT_RESULT
187 elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
190 log_error "Unknown OS API VERSION $OS_API_VERSION"
194 if [ -n "$OS_VARIANT" ]; then
195 if [ ! -d "$VARIANTS_DIR" ]; then
196 log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
199 VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
200 if [ -f "$VARIANT_CONFIG" ]; then
203 if grep -qxF "$OS_VARIANT" variants.list; then
204 log_error "ERROR: instance-debootstrap configuration error"
205 log_error " Published variant $OS_VARIANT is missing its config file"
206 log_error " Please create $VARIANT_CONFIG or unpublish the variant"
207 log_error " (by removing $OS_VARIANT from variants.list)"
209 log_error "Unofficial variant $OS_VARIANT is unsupported"
210 log_error "Most probably this is a user error, forcing a wrong name"
211 log_error "To support this variant please create file $VARIANT_CONFIG"