5d3f638c152534ec857da7c819455a6381829958
[infrastructure.git] / scripts / create-vm-debian.sh
1 #!/bin/bash
2
3 # WARNING: looks like specifying another arch/preso won't work
4
5 COMMAND=$(basename $0)
6 DIRNAME=$(dirname $0)
7
8 #BUILD="${HOME}/git-build"
9 DOMAIN=pl.sophia.inria.fr
10
11 DEFAULT_DISTRO=wheezy
12 DEFAULT_PERSO=linux64
13
14 INTERACTIVE=
15 DISTRO=$DEFAULT_DISTRO
16 PERSO=$DEFAULT_PERSO
17 function usage () {
18   message="$@" 
19   echo "usage : $COMMAND [-i] [-f distro] [-p perso] hostname"
20   echo "  default distro is $DEFAULT_DISTRO"
21   echo "  default perso is $DEFAULT_PERSO"
22   [ -n "$message" ] && echo $message
23   exit 1
24 }
25
26 while getopts "f:p:ih" flag; do
27     case $flag in
28         f) DISTRO=$OPTARG;;
29         p) PERSO=$OPTARG;;
30         i) INTERACTIVE=true ;;
31         ?|h) usage "" ;;
32     esac
33 done
34 shift $((OPTIND-1))
35 [[ -z "$@" ]] && usage "no hostname provided"
36 HOSTNAME="$1" ; shift
37 [[ -n "$@" ]] && usage "extra arguments"  "$@" "(hostname=$HOSTNAME)"
38
39
40 #[ -d "$BUILD" ] || usage "Could not find directory $BUILD"
41
42 [ -d /vservers/$HOSTNAME ] && usage "vserver $HOSTNAME already exists in /vservers"
43 # debian distros not found there - skip that
44 #[ -d /etc/vservers/.distributions/$DISTRO ] || usage "distro $DISTRO not known in /etc/vservers/.distributions/"
45 rpm -q debootstrap >& /dev/null || usage "You need to first run 'yum install debootstrap'"
46
47 fqdn=$HOSTNAME.$DOMAIN
48 ## compute IP
49 host $fqdn | grep -q 'has address' || usage "hostname $fqdn not known to DNS"
50 ip=$(host $fqdn | grep 'has address' | awk '{print $4}')
51
52 #cd $BUILD
53 #git pull
54 #cd -
55
56 # single_ip needs to be turned off, vtest-init-vserver now takes care of that
57 #command="$BUILD/vtest-init-vserver.sh -p $PERSO -f $DISTRO $HOSTNAME none -- --interface eth0:$ip --hostname $fqdn"
58
59 case $PERSO in
60     linux32) arch=i386;;
61     linux64) arch=amd64;;
62 esac
63
64 case $DISTRO in
65     squeeze|wheezy) DEBMIRROR=http://ftp2.fr.debian.org/debian/ ;;
66 # from the vserver man page DEBMIRROR=http://us.archive.ubuntu.com/ubuntu/ ;;
67     oneiric|precise|quantal) DEBMIRROR=http://mir1.ovh.net/ubuntu/ubuntu/ ;;
68     *) echo unknown distro $DISTRO; exit 1;;
69 esac
70
71 command="$PERSO vserver ${HOSTNAME} build -m debootstrap --interface eth0:${ip}/21 --hostname ${fqdn} -- -d ${DISTRO} -m ${DEBMIRROR} -- --verbose --arch=${arch} "
72
73 if [ -n "$INTERACTIVE" ] ; then
74     echo -n "Run $command OK ? "
75     read answer ; case $answer in [nN]*) exit 1 ;; esac
76 fi
77
78 $command
79
80 ## stolen from vbuild-util-vserver (might be that we can actually use that one as-is...)
81 # turns out that with wheezy at least, at this point we're getting 
82 # /vservers/<vs>/var/run -> /run
83 # /vservers/<vs>/var/lock -> /run/lock
84 # trying to fix this with relative links does not appear to work fine
85 # when trying to vserver start we're then getting
86 # + exec /usr/sbin/vspace --mount --fs --new -- /usr/sbin/vserver ----nonamespace debuild09 start
87 # fakerunlevel: open("/var/run/utmp"): No such file or directory
88 # so instead we bluntly create empty dirs and hope for the best
89 [ -h /vservers/${HOSTNAME}/var/run ] && [ ! -d /vservers/${HOSTNAME}/var/run ] && \
90     { rm -f /vservers/${HOSTNAME}/var/run ; mkdir /vservers/${HOSTNAME}/var/run ; }
91 [ -h /vservers/${HOSTNAME}/var/lock ] && [ ! -d /vservers/${HOSTNAME}/var/lock ] && \
92     { rm -f /vservers/${HOSTNAME}/var/lock ; mkdir /vservers/${HOSTNAME}/var/lock ; }
93
94 vserver ${HOSTNAME} start