refine strategy to spot ip address, keep on calling guest_ipv4
[build.git] / create-vm.sh
1 #!/bin/bash
2
3 COMMAND=$(basename $0)
4 DIRNAME=$(dirname $0)
5
6 # NOTE - April 2020
7 # this whole lot won't work well with ubuntu containers any longer
8 # tried with webprivacy on bionic, and
9 # (*) the initial populating phase somehow spawned a question screen
10 #     something about asking questions during upgrades..
11 #     so I had to turn off the redirection into ~/machines/<container>.log
12 # (*) more badly, the resulting container does not start its network upon boot
13 #     everything is in place and it could be started manually
14 #     like for the record e.g.
15 #         ip link set dev eth0 up
16 #         ip address add 138.96.119.13/21 dev eth0
17 #         ip route add default dev eth0 via 138.96.112.250
18 #         ping -c 1 8.8.8.8
19 # trying to solve that last issue by apt-get install'ing network-manager
20 # (which was missing indeed) was not enough
21 # a bit more work seems to be needed if that became needed
22
23 BUILD="${HOME}/git-build"
24
25 LOGS=$HOME/machines
26
27 [ -d $LOGS ] || { echo "Creating logs dir $LOGS" ; mkdir -p $LOGS; }
28
29 DOMAIN=pl.sophia.inria.fr
30
31 DEFAULT_DISTRO=f33
32 DEFAULT_MEMORY=16384
33
34 CONFIRM=
35 function usage () {
36   message="$@"
37   echo "usage : $COMMAND [-c] [-f distro] [-i image] [ -m memory ] [ -n hostname ] [-s] container"
38   echo " -c : confirm, will show the command and prompt for confirmation "
39   echo " -f : set distro, default is $DEFAULT_DISTRO"
40   echo " -i : if specified, image is rsynced into /vservers"
41   echo "      warning: we cannot use an image already in /vservers..."
42   echo " -m : memory size in Mb - default is $DEFAULT_MEMORY"
43   echo " -n : specify hostname if different from container"
44   echo " -s : do not start VM"
45   echo " container : used for /vservers/foo as well as the lxc/libvirt name"
46   echo "examples"
47   echo "  create-vm.sh sandbox"
48   echo "    Builds a brand new $DEFAULT_DISTRO 64bits VM named sandbox with hostname sandbox.pl.sophia.inria.fr"
49   echo "  create-vm.sh -i /vservers/migrating/testmaster -n testmaster testmaster.f14"
50   echo "    Create a container named testmaster.f14 from the specified image with hostname testmaster.pl.sophia.inria.fr"
51   [ -n "$message" ] && echo $message
52   exit 1
53 }
54
55 # using HOSTNAME won't work as this is already set in environment
56 while getopts "cf:i:m:n:sh" flag; do
57     case $flag in
58         c) CONFIRM=true ;;
59         f) DISTRO=$OPTARG ;;
60         i) IMAGE=$OPTARG ;;
61         m) MEMORY=$OPTARG ;;
62         n) VM_HOSTNAME=$OPTARG ;;
63         s) DO_NOT_START_VM=true ;;
64         ?|h) usage "" ;;
65     esac
66 done
67 # parse args
68 shift $((OPTIND-1))
69 [[ -z "$@" ]] && usage "no hostname provided"
70 container="$1" ; shift
71 [[ -n "$@" ]] && usage "extra arguments" "$@" "(container=$container)"
72
73 # sanity checks
74 [ -d "$BUILD" ] || usage "Could not find directory $BUILD"
75 [ -d /vservers/$container ] && usage "container $container already exists in /vservers"
76
77 echo "Updating $BUILD"
78 cd $BUILD
79 git pull
80 cd -
81
82 # compute all vars from args
83 [ -z "$DISTRO" ] && DISTRO="$DEFAULT_DISTRO"
84 [ -z "$MEMORY" ] && MEMORY="$DEFAULT_MEMORY"
85 [ -z "$VM_HOSTNAME" ] && VM_HOSTNAME="$container"
86 fqdn=$VM_HOSTNAME.$DOMAIN
87
88 # prepare initvm command
89 initvm="$BUILD/lbuild-initvm.sh"
90 [ -z "$IMAGE" ] && initvm="$initvm -f $DISTRO" || initvm="$initvm -i $IMAGE"
91 initvm="$initvm -n $fqdn"
92 # always use fedora repos
93 initvm="$initvm -u"
94 [ -n "$DO_NOT_START_VM" ] && initvm="$initvm -s"
95 [ -n "$MEMORY" ] && initvm="$initvm -m $MEMORY"
96 initvm="$initvm $container"
97
98 if [ -n "$CONFIRM" ] ; then
99     echo -n "Run $initvm OK ? "
100     read answer ; case $answer in [nN]*) exit 1 ;; esac
101 fi
102
103 echo "Running $initvm"
104 echo "Storing output in $LOGS/$container.log"
105 $initvm >& $LOGS/$container.log