Rework the blockdev handling
[ext/instance-debootstrap.git] / common.sh.in
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
21 log_error() {
22   echo "$@" >&2
23 }
24
25 get_api5_arguments() {
26   GETOPT_RESULT=$*
27   # Note the quotes around `$TEMP': they are essential!
28   eval set -- "$GETOPT_RESULT"
29   while true; do
30     case "$1" in
31       -i|-n) instance=$2; shift 2;;
32
33       -o) old_name=$2; shift 2;;
34
35       -b) blockdev=$2; shift 2;;
36
37       -s) swapdev=$2; shift 2;;
38
39       --) shift; break;;
40
41       *)  log_error "Internal error!" >&2; exit 1;;
42     esac
43   done
44   if [ -z "$instance" -o -z "$blockdev" ]; then
45     log_error "Missing OS API Argument (-i, -n, or -b)"
46     exit 1
47   fi
48   if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev"  ]; then
49     log_error "Missing OS API Argument -s (swapdev)"
50     exit 1
51   fi
52   if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name"  ]; then
53     log_error "Missing OS API Argument -o (old_name)"
54     exit 1
55   fi
56 }
57
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)"
62     exit 1
63   fi
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"
67     exit 1
68   fi
69   if [ "$SCRIPT_NAME" = "export" ]; then
70     if [ -z "$EXPORT_DEVICE" ]; then
71       log_error "Missing OS API Variable EXPORT_DEVICE"
72     fi
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"
77     fi
78     blockdev=$IMPORT_DEVICE
79   else
80     blockdev=$DISK_0_PATH
81   fi
82   if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then
83     log_error "Missing OS API Variable OLD_INSTANCE_NAME"
84   fi
85   old_name=$OLD_INSTANCE_NAME
86 }
87
88 format_disk0() {
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 
92   # report geometry
93   sfdisk -H 255 -S 63 --quiet --Linux "$1" <<EOF
94 0,,L,*
95 EOF
96 }
97
98 map_disk0() {
99   blockdev="$1"
100   block_base=`basename $blockdev`
101   filesystem_dev_base=`kpartx -l -p- $blockdev | \
102                        grep -m 1 "^$block_base.*$blockdev" | \
103                        awk '{print $1}'`
104   kpartx -a -p- $blockdev
105   echo "/dev/mapper/$filesystem_dev_base"
106 }
107
108 unmap_disk0() {
109   kpartx -d -p- $1
110 }
111
112 cleanup() {
113   if [ ${#CLEANUP[*]} -gt 0 ]; then
114     LAST_ELEMENT=$((${#CLEANUP[*]}-1))
115     REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
116     for i in $REVERSE_INDEXES; do
117       ${CLEANUP[$i]}
118     done
119   fi
120 }
121
122 DEFAULT_FILE="@sysconfdir@/default/ganeti-instance-debootstrap"
123 if [ -f "$DEFAULT_FILE" ]; then
124     . "$DEFAULT_FILE"
125 fi
126
127 # note: we don't set a default mirror since debian and ubuntu have
128 # different defaults, and it's better to use the default
129
130 # only if the user want to specify a mirror in the defaults file we
131 # will use it, this declaration is to make sure the variable is set
132 : ${MIRROR:=""}
133 : ${PROXY:=""}
134 : ${SUITE:="lenny"}
135 : ${ARCH:=""}
136 : ${EXTRA_PKGS:=""}
137 : ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-debootstrap.d"}
138 : ${GENERATE_CACHE:="yes"}
139 : ${CLEAN_CACHE:="14"} # number of days to keep a cache file
140 if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
141   DEFAULT_PARTITION_STYLE="none"
142 else
143   DEFAULT_PARTITION_STYLE="msdos"
144 fi
145 : ${PARTITION_STYLE:=$DEFAULT_PARTITION_STYLE} # disk partition style
146
147 CACHE_DIR="@localstatedir@/cache/ganeti-instance-debootstrap"
148
149 SCRIPT_NAME=$(basename $0)
150
151 for dir in /lib/udev /sbin; do
152   if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
153     VOL_ID=$dir/vol_id
154   fi
155 done
156
157 if [ -z "$VOL_ID" ]; then
158     log_error "vol_id not found, please install udev"
159     exit 1
160 fi
161
162 if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
163   OS_API_VERSION=5
164   GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
165   if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
166   get_api5_arguments $GETOPT_RESULT
167 elif [ "$OS_API_VERSION" = "10" ]; then
168   get_api10_arguments
169 else
170   log_error "Unknown OS API VERSION $OS_API_VERSION"
171   exit 1
172 fi