chiark / gitweb /
* Fix readconfig.in to set adt_$var rather than just $var.
[autopkgtest.git] / xen / readconfig.in
1 # This bash script is sourced by the various tools to set the
2 # adt_... variables and some other useful ones.
3
4 set -e${ADT_SHELLX}
5 set -o pipefail
6
7 #---------- useful general functions ----------
8
9 fail () {
10         echo >&2 "xenlvm: ${0##*/}: error: $*"
11         exit 16
12 }
13
14 x () { echo "x $*"; "$@"; }
15
16 case "$BASH_VERSION" in
17 '')     fail 'scripts using adtxenlvm readconfig must be bash scripts' ;;
18 *)      ;;
19 esac
20
21 #---------- default values for simple settings ----------
22
23 adt_nominum=adt
24 adt_testbed_ram=256
25 adt_freeze_ram=32
26 adt_fs_size=3072M
27 adt_fs_snapsize=2560M
28 adt_fs_cowchunk=8
29 adt_playbase=/var/lib/autopkgtest/xenlvm
30 adt_fs_type=ext3
31 adt_fs_mkfs_args=''
32 adt_vm_reduce_retries=10
33 adt_ssh_keytype=dsa
34
35 adt_debootstrap_components='*'
36 adt_debootstrap_include=libc6-xen,openssh-server,ed
37 adt_debootstrap_includemore=
38 adt_debootstrap_mirrors=''
39 adt_debootstrap_script=''
40
41 adt_fw_localmirrors=''
42 adt_fw_prohibnets='192.168.0.0/24 172.16.0.0/12 10.0.0.0/8'
43 adt_fw_allowglobalports='80'
44
45 adt_guest_macaddr=00:16:3e:7c:aa:7f
46 adt_net_vifscript=/etc/xen/scripts/vif-route-adt
47 adt_normaluser=adtxenu
48
49 #---------- arguments and config file ----------
50
51 cmdline_args () {
52         while [ $# -gt 0 ]; do
53                 arg="$1"
54                 case "$arg" in
55                 --*=*)
56                         value="${arg#*=}"
57
58                         var=${arg%%=*}
59                         var=${var#--}
60                         case "$var" in
61                         *-*)    var="$(printf "%s" "$var" | tr -- - _)" ;;
62                         esac
63                         var=${var#adt_}
64
65                         case $var in
66                         *) ;; # @@varlist@@
67                         *) fail "unknown configuration variable \`$var'" ;;
68                         esac
69
70                         eval "adt_$var=\"\$value\""
71                         shift ;;
72
73                 --)     shift; break ;;
74                 -*)     fail "unknown option \`$arg'" ;;
75                 *)      break ;;
76                 esac
77         done
78         nonoptargs=$#
79 }
80
81 cmdline_args "$@"
82 : "${adt_config:=/etc/autopkgtest/xenlvm_${adt_nominum}_config}"
83 test ! -e "${adt_config}" || . "${adt_config}"
84 cmdline_args "$@"
85
86 #---------- calculated defaults for complex settings ----------
87
88 case "$adt_lvm_vg" in
89 '')
90         printf "searching for default volume group ... "
91         vgdisplay_out=`vgdisplay -c 10>&-`
92         case "$vgdisplay_out" in
93         "")     fail 'no volume groups found';;
94         *"
95 "*)             fail 'several volume groups, config must specify which';;
96         esac
97         adt_lvm_vg=${vgdisplay_out%%:*}
98         case "$adt_lvm_vg" in
99         *[^\ ]\ *) fail "volume group name \`$adt_lvm_vg' has spaces!";;
100         esac
101         adt_lvm_vg=${adt_lvm_vg##* }
102         echo "using system's only volume group: $adt_lvm_vg"
103         ;;
104 esac
105
106 if [ "$adt_readconfig_needkernel" ]; then
107  case "$adt_kernel" in
108  '')
109         printf "searching for kernel ... "
110         for f in /boot/*"`uname -r`"*; do
111                 test -e "$f" || continue
112                 printf "(%s:" "$f"
113                 output="$(file -- "$f")"
114                 case "$output" in
115                 *"gzip compressed data"*) ;;
116                 *) printf "!gzip) "; continue ;;
117                 esac
118                 trap 'rm -f -- "$tf"; exit 127' 0
119                 tf=`mktemp -t`
120                 zcat -- "$f" >"$tf"
121                 output="$(file -- "$tf")"
122                 case "$output" in
123                 *"ELF "*" executable,"*) ;;
124                 *) printf "!ELF) "; continue;;
125                 esac
126                 output="$(objdump -j __xen_guest -s -- "$tf")"
127                 rm -f "$tf"; trap '' 0
128                 case "$output" in
129                 *" __xen_guest:"*) ;;
130                 *) printf "!Xen) "; continue
131                 esac
132                 printf "y) "
133                 test "x$adt_kernel" = x || \
134                         fail 'several kernels, config must specify which'
135                 adt_kernel="$f"
136         done
137         [ "x$adt_kernel" != x ] || \
138                 fail 'could not find currently booted Xen kernel'
139         echo "using currently booted kernel: $adt_kernel"
140         ;;
141  esac
142
143  case "$adt_ramdisk" in
144  none)  echo "ramdisk \`none' specified, using static kernel"
145         adt_ramdisk='' ;;
146  '')    adt_ramdisk="${adt_kernel/\/vmlinuz-//initrd.img-}"
147         test -e "$adt_ramdisk" || \
148  fail "calculated default ramdisk initrd.img \`$adt_ramdisk' does not exist"
149         echo "using calculated default ramdisk initrd.img: $adt_ramdisk" ;;
150  *)     ;;
151  esac
152 fi
153
154 case "$adt_modules" in
155 '')     adt_modules="/lib/modules/`uname -r`" ;;
156 *)      ;;
157 esac
158
159 case "$adt_distro" in
160 '')
161         printf "considering which distro to use ... "
162         test -e /etc/lsb-release || \
163                 fail 'no /etc/lsb-version, config must specify adt_distro'
164         . /etc/lsb-release
165         adt_distro=$DISTRIB_CODENAME
166         echo "using same distro as host: $adt_distro"
167         ;;
168 esac
169
170 hostname_from_ipaddr () {
171  eval '
172   if [ x"$adt_'$1'_hostname" = x ] && \
173      [ x"$adt_'$1'_ipaddr" != x ]; then
174    printf "%s" "finding '$1' hostname from IP address $adt_'$1'_ipaddr: "
175    adt_'$1'_hostname=`adnshost -t ptr +Do +Dt +Dc -i $adt_'$1'_ipaddr`
176    printf "%s\n" "${adt_'$1'_hostname}"
177   fi
178  '
179 }
180
181 hostname_from_ipaddr guest
182 hostname_from_ipaddr host
183
184 if [ x"$adt_host_hostname" = x ]; then
185         printf "finding host hostname, supposely our own FQDN: "
186         adt_host_hostname=`hostname -f`
187         printf "%s\n" "$adt_host_hostname"
188 fi
189
190 if [ x"$adt_guest_hostname" = x ]; then
191         case "$adt_guests_domain" in
192         '')     printf "guessing guest hostname from host hostname: "
193  adt_guest_hostname=$adt_distro.$adt_nominum.${adt_host_hostname#*.} ;;
194         .*)     printf "setting guest hostname using distro and nominum: "
195  adt_guest_hostname=$adt_distro.$adt_nominum$adt_guests_domain ;;
196         *)      printf "setting guest hostname using distro and domain: "
197  adt_guest_hostname=$adt_distro.$adt_guests_domain ;;
198         esac
199         printf "%s\n" "$adt_guest_hostname"
200 fi
201
202 ipaddr_from_hostname () {
203  eval '
204   if [ x"$adt_'$1'_ipaddr" = x ] && \
205      [ x"$adt_'$1'_hostname" != x ]; then
206    printf "%s" "finding '$1' IP address from hostname $adt_'$1'_hostname: "
207    adt_'$1'_ipaddr=`adnshost -t a +Do +Dt +Dc - "$adt_'$1'_hostname"`
208    echo "${adt_'$1'_ipaddr}"
209   fi
210  '
211 }
212
213 ipaddr_from_hostname guest
214 ipaddr_from_hostname host
215
216 search_hook () {
217  eval '
218   printf "looking for '$2' hook ... "
219   case "$adt_'$1'_hook" in
220   "")
221     case "$adt_config" in
222     *_config)
223       adt_'$1'_hook=${adt_config%_config}_'$3'
224       if test -e "$adt_'$1'_hook"; then
225               echo "default exists: $adt_'$1'_hook"
226       else
227               echo "default $adt_'$1'_hook is not a file, so: none"
228               adt_'$1'_hook=""
229       fi
230       ;;
231     *)
232       echo "not specified, not computable from config, so: none"
233       adt_'$1'_hook=""
234       ;;
235     esac
236     ;;
237   *)
238     if test -e "$adt_'$1'_hook"; then
239       echo "exists: $adt_'$1'_hook"
240     else
241       echo "specified as $adt_'$1'_hook, but not a file, so: none"
242       adt_'$1'_hook=""
243     fi
244     ;;
245   esac
246  '
247 }
248
249 search_hook fw firewall fwhook
250 search_hook setup setup setuphook
251
252 boolean_config () {
253   eval '
254     case "${adt_'$1'}" in
255     y*|Y*|1*|t*)        adt_'$1'=true ;;
256     n*|N*|0*|f*)        adt_'$1'=false ;;
257     "")                 adt_'$1'=$2 ;;
258     *)                  fail "unknown boolean value \`$1'\'' for adt_'$1'" ;;
259     esac
260   '
261 }
262
263 # SSH
264 : ${adt_ssh_privkey:=/root/.ssh/id_${adt_ssh_keytype}_${adt_nominum}}
265 : ${adt_ssh_pubkey:=${adt_ssh_privkey}.pub}
266 : ${adt_ssh_keyident_args:=-i ${adt_ssh_privkey}}
267 : ${adt_ssh_keygen_args:=-t ${adt_ssh_keytype}}
268
269 # Xen
270 : ${adt_xmname:=${adt_nominum}_${adt_distro}}
271
272 # In-host-file-system playground
273 : ${adt_play:=${adt_playbase}/${adt_nominum}_${adt_distro}}
274 : ${adt_xmconfig:=${adt_play}/xmconfig}
275 : ${adt_lock:=${adt_play}/lock}
276
277 # LVM
278 boolean_config lvm_erasebase true
279 : ${adt_lvm_baselv:=${adt_nominum}_${adt_distro}_base}
280 : ${adt_lvm_cowdatalv:=${adt_nominum}_${adt_distro}_cowdata}
281 : ${adt_devmapper_cowdev:=${adt_nominum}_${adt_distro}_snap}
282
283 : ${adt_fslink_dir:=adt-xenlvm}
284 : ${adt_fslink_name:=${adt_nominum}_${adt_distro}_fs}
285
286 # Firewall
287 : ${adt_fw_testbedclients:=${adt_host_ipaddr}}
288
289 lvm_baselv_namerhs=${adt_lvm_vg}/${adt_lvm_baselv}
290 lvm_baselv_namepath=/dev/${lvm_baselv_namerhs}
291
292 lvm_cowdata_namerhs=${adt_lvm_vg}/${adt_lvm_cowdatalv}
293 lvm_cowdata_namepath=/dev/${lvm_cowdata_namerhs}
294
295 lvm_snapdev=/dev/mapper/${adt_devmapper_cowdev}
296
297 lvm_fslink_ptrrhs=${adt_fslink_dir}/${adt_fslink_name}
298 lvm_fslink_dirpath=/dev/${adt_fslink_dir}
299 lvm_fslink_ptr=/dev/${adt_fslink_dir}/${adt_fslink_name}
300
301 echo "adtxenlvm: configuration for read for nominum=${adt_nominum}"