lbuild vms now rely on NetworkManager
[build.git] / lbuild-initvm.sh
index 720cf0a..5dedf7f 100755 (executable)
@@ -51,28 +51,27 @@ DEBIAN_PREINSTALLED="openssh-server openssh-client"
 ########## networking utilities
 function gethostbyname () {
     local hostname=$1
-    python -c "import socket; print socket.gethostbyname('"$hostname"')" 2> /dev/null
+    python3 -c "import socket; print(socket.gethostbyname('"$hostname"'))" 2> /dev/null
 }
 
 # e.g. 21 -> 255.255.248.0
 function masklen_to_netmask () {
     local masklen=$1; shift
-    python <<EOF
+    python3 <<EOF
 import sys
-masklen=$masklen
-if not (masklen>=1 and masklen<=32):
-  print "Wrong masklen",masklen
+masklen = $masklen
+if not (1 <= masklen <= 32):
+  print("Wrong masklen", masklen)
   exit(1)
-result=[]
+result = []
 for i in range(4):
-    if masklen>=8:
+    if masklen >= 8:
        result.append(8)
-       masklen-=8
+       masklen -= 8
     else:
        result.append(masklen)
-       masklen=0
-print ".".join([ str(256-2**(8-i)) for i in result ])
-
+       masklen = 0
+print(".".join([ str(256-2**(8-i)) for i in result ]))
 EOF
 }
 
@@ -784,13 +783,17 @@ function wait_for_ssh () {
 
     local counter=1
     while [ "$current_time" -lt "$stop_time" ] ; do
-         echo "$counter-th attempt to reach sshd in container $lxc ..."
-         [ -z "$guest_ip" ] && guest_ip=$(guest_ipv4 $lxc)
-         [ -n "$guest_ip" ] && ssh -o "StrictHostKeyChecking no" $guest_ip 'uname -i' && {
-                 success=true; echo "SSHD in container $lxc is UP on IP $guest_ip"; break ; } || :
-         counter=$(($counter+1))
-         sleep 10
-         current_time=$(date +%s)
+        echo "$counter-th attempt to reach sshd in container $lxc ..."
+        [ -z "$guest_ip" ] && guest_ip=$(guest_ipv4 $lxc)
+        [ -n "$guest_ip" ] && ssh -o "StrictHostKeyChecking no" $guest_ip 'uname -i' && {
+            success=true; echo "SSHD in container $lxc is UP on IP $guest_ip"; break ; } || :
+        # some of our boxes have gone through a long upgrade historically, and
+        # so they don't end up with the same gid mapping for the ssh_keys
+        # group as the ones in the guest that result from a fresh install
+        virsh -c lxc:/// lxc-enter-namespace $lxc /bin/bash -c "chown root:ssh_keys /etc/ssh/*_key" || :
+        counter=$(($counter+1))
+        sleep 10
+        current_time=$(date +%s)
     done
 
     # Thierry: this is fatal, let's just exit with a failure here
@@ -864,6 +867,8 @@ function main () {
     local lxc_root=$(lxcroot $lxc)
 
     # rainchecks
+    # when using with the -i option, checking that $lxc_root is void
+    # is a little too much stress..
     almost_empty $lxc_root || \
         { echo "container $lxc already exists in $lxc_root - exiting" ; exit 1 ; }
     virsh -c lxc:/// domuuid $lxc >& /dev/null && \
@@ -872,9 +877,13 @@ function main () {
 
     # if IMAGE, copy the provided rootfs to lxc_root
     if [ -n "$IMAGE" ] ; then
-        [ ! -d "$IMAGE" ] && \
-        { echo "$IMAGE rootfs folder does not exist - exiting" ; exit 1 ; }
-        rsync -a $IMAGE/ $lxc_root/
+        if [ ! -d "$IMAGE" ]; then
+            echo "$IMAGE rootfs folder does not exist - exiting"
+            exit 1
+        else
+            echo "Copying $IMAGE into $lxc_root with rsync --archive --delete"
+            rsync --archive --delete $IMAGE/ $lxc_root/
+        fi
     fi
 
     # check we've exhausted the arguments