create-vm.sh: add option -I image - to specify the location of the rootfs
[infrastructure.git] / scripts / create-vm.sh
1 #!/bin/bash
2
3 COMMAND=$(basename $0)
4 DIRNAME=$(dirname $0)
5
6 BUILD="${HOME}/git-build"
7
8 echo "Updating $BUILD"
9 cd $BUILD
10 git pull
11 cd -
12
13 LOGS=$HOME/machines
14
15 [ -d $LOGS ] || { echo "Creating logs dir $LOGS" ; mkdir -p $LOGS; } 
16
17 DOMAIN=pl.sophia.inria.fr
18
19 DEFAULT_DISTRO=f20
20 # do not care about 32 bits anymore
21 #DEFAULT_PERSO=linux64
22
23 INTERACTIVE=
24 DISTRO=$DEFAULT_DISTRO
25 function usage () {
26   message="$@" 
27   echo "usage : $COMMAND [-i] [-f distro] [-I Image] hostname"
28   echo "  default distro is $DEFAULT_DISTRO"
29   [ -n "$message" ] && echo $message
30   exit 1
31 }
32
33 while getopts "f:I:ih" flag; do
34     case $flag in
35         f) DISTRO=$OPTARG;;
36         i) INTERACTIVE=true ;;
37         I) IMAGE=$OPTARG;;
38         ?|h) usage "" ;;
39     esac
40 done
41 shift $((OPTIND-1))
42 [[ -z "$@" ]] && usage "no hostname provided"
43 container="$1" ; shift
44 [[ -n "$@" ]] && usage "extra arguments"  "$@" "(hostname=$container)"
45
46
47 [ -d "$BUILD" ] || usage "Could not find directory $BUILD"
48
49 [ -d /vservers/$container ] && usage "container $container already exists in /vservers"
50
51 fqdn=$container.$DOMAIN
52
53 if [ -n "$IMAGE" ]; then
54   command="$BUILD/lbuild-initvm.sh -f $DISTRO -n $fqdn -i $IMAGE $container"
55 else
56   command="$BUILD/lbuild-initvm.sh -f $DISTRO -n $fqdn $container"
57 fi
58
59 if [ -n "$INTERACTIVE" ] ; then
60     echo -n "Run $command OK ? "
61     read answer ; case $answer in [nN]*) exit 1 ;; esac
62 fi
63
64 echo "Storing output in $LOGS/$container.log"
65 $command >& $LOGS/$container.log
66