use new strategy to spot guest ipv4 address
authorThierry Parmentelat <thierry.parmentelat@inria.fr>
Wed, 7 Feb 2024 15:08:34 +0000 (16:08 +0100)
committerThierry Parmentelat <thierry.parmentelat@inria.fr>
Wed, 7 Feb 2024 15:08:34 +0000 (16:08 +0100)
lbuild-initvm.sh
lbuild-nightly.sh

index 59ba47e..1d64ce0 100755 (executable)
@@ -872,11 +872,11 @@ EOF
 # 1st version was relying on virsh net-dhcp-leases
 # however this was too fragile, would not work for fedora14 containers
 # WARNING: this code is duplicated in lbuild-nightly.sh
-function guest_ipv4() {
+function guest_ipv4_old() {
     lxc=$1; shift
 
     mac=$(virsh -c lxc:/// domiflist $lxc | grep -E 'network|bridge' | awk '{print $5;}')
-    [ -z "$mac" ] && { echo 1>&2 guest_ipv4 cannot find mac; return 1; }
+    [ -z "$mac" ] && { echo 1>&2 guest_ipv4_old cannot find mac; return 1; }
     ip=$(arp -en | grep "$mac" | awk '{print $1;}')
     # if not known: run a ping and try again
     if [ -z $ip ]; then
@@ -884,10 +884,28 @@ function guest_ipv4() {
            ping -c1 -w1 -W1 $lxc.pl.sophia.inria.fr >& /dev/null
            ip=$(arp -en | grep "$mac" | awk '{print $1;}')
     fi
-    [ -z "$ip" ] && { echo 1>&2 guest_ipv4 cannot find ip; return 1; }
+    [ -z "$ip" ] && { echo 1>&2 guest_ipv4_old cannot find ip; return 1; }
     echo $ip
 }
 
+function guest_ipv4() {
+    lxc=$1; shift
+
+    # this gives us the libvirt_lxc pid for the container
+    local lxc_pid=$(virsh -c lxc:/// dominfo $lxc | grep '^Id:' | awk '{print $2;}' | sed -e "s|-||g")
+    [[ -z "$lxc_pid" ]] && { echo 1>&2 guest_ipv4 cannot find lxc pid; return 1; }
+    # but we need the systemd (pid=1) instance for the container
+    local systemd_pid=$(pgrep -P $lxc_pid systemd)
+    [[ -z "$systemd_pid" ]] && { echo 1>&2 guest_ipv4 cannot systemd pid; return 1; }
+    # from there we can inspect the network interfaces
+    local domip=$(nsenter -t $systemd_pid -n ip -br addr show eth0 \
+                 | awk '{print $3}' \
+                 | cut -d/ -f1 \
+                 )
+    [ -z "$domip" ] && { echo 1>&2 guest_ipv4 cannot find ip; return 1; }
+    echo $domip
+}
+
 function wait_for_ssh () {
     set -x
     set -e
index f7144e5..9991abe 100755 (executable)
@@ -57,11 +57,11 @@ function logfile () {
 # 1st version was relying on virsh net-dhcp-leases
 # however this was too fragile, would not work for fedora14 containers
 # WARNING: this code is duplicated in lbuild-initvm.sh
-function guest_ipv4() {
+function guest_ipv4_old() {
     lxc=$1; shift
 
     mac=$(virsh -c lxc:/// domiflist $lxc | grep -E 'network|bridge' | awk '{print $5;}')
-    [ -z "$mac" ] && { echo 1>&2 guest_ipv4 cannot find mac; return 1; }
+    [ -z "$mac" ] && { echo 1>&2 guest_ipv4_old cannot find mac; return 1; }
     ip=$(arp -en | grep "$mac" | awk '{print $1;}')
     # if not known: run a ping and try again
     if [ -z $ip ]; then
@@ -69,10 +69,28 @@ function guest_ipv4() {
            ping -c1 -w1 -W1 $lxc.pl.sophia.inria.fr >& /dev/null
            ip=$(arp -en | grep "$mac" | awk '{print $1;}')
     fi
-    [ -z "$ip" ] && { echo 1>&2 guest_ipv4 cannot find ip; return 1; }
+    [ -z "$ip" ] && { echo 1>&2 guest_ipv4_old cannot find ip; return 1; }
     echo $ip
 }
 
+function guest_ipv4() {
+    lxc=$1; shift
+
+    # this gives us the libvirt_lxc pid for the container
+    local lxc_pid=$(virsh -c lxc:/// dominfo $lxc | grep '^Id:' | awk '{print $2;}' | sed -e "s|-||g")
+    [[ -z "$lxc_pid" ]] && { echo 1>&2 guest_ipv4 cannot find lxc pid; return 1; }
+    # but we need the systemd (pid=1) instance for the container
+    local systemd_pid=$(pgrep -P $lxc_pid systemd)
+    [[ -z "$systemd_pid" ]] && { echo 1>&2 guest_ipv4 cannot systemd pid; return 1; }
+    # from there we can inspect the network interfaces
+    local domip=$(nsenter -t $systemd_pid -n ip -br addr show eth0 \
+                 | awk '{print $3}' \
+                 | cut -d/ -f1 \
+                 )
+    [ -z "$domip" ] && { echo 1>&2 guest_ipv4 cannot find ip; return 1; }
+    echo $domip
+}
+
 # wrap a quick summary of suspicious stuff
 # this is to focus on installation that go wrong
 # use with care, a *lot* of other things can go bad as well