$NetBSD: patch-xe-update-guest-attrs,v 1.2 2012/03/15 14:06:08 sborrill Exp $

--- xe-update-guest-attrs.orig	2011-09-23 15:39:33.000000000 +0100
+++ xe-update-guest-attrs	2011-09-23 15:44:30.000000000 +0100
@@ -35,11 +35,11 @@
 export LANG
 
 
-XE_LINUX_DISTRIBUTION_CACHE=/var/cache/xe-linux-distribution
+XE_LINUX_DISTRIBUTION_CACHE=@VARBASE@/cache/xe-linux-distribution
 
 IPADDR_RE="\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\}"
 
-export PATH=/usr/sbin:/usr/bin:/sbin:/bin
+export PATH=/usr/sbin:/usr/bin:/sbin:/bin:@PREFIX@/sbin:@PREFIX@/bin
 XENSTORE=${XENSTORE:-xenstore}
 
 XENSTORE_UPDATED=0
@@ -56,7 +56,7 @@
 
 xenstore_write_cached() {
     key="$1" newval="$2"
-    cache=/var/cache/xenstore/$key
+    cache=@VARBASE@/cache/xenstore/$key
     if [ -f $cache ] ; then
 	# cache exists
 	oldval=$(cat "$cache")
@@ -84,13 +84,13 @@
 
 # If we detect a domain change then delete our cache and force a refresh
 domid=$(xenstore-read "domid")
-cache=/var/cache/xenstore/unique-domain-id
+cache=@VARBASE@/cache/xenstore/unique-domain-id
 newval=$(xenstore-read "/local/domain/${domid}/unique-domain-id")
 if [ -e $cache ]; then
     oldval=$(cat "$cache")
     if [ "$oldval" != "$newval" ]; then
 	# domain changed
-	rm -rf /var/cache/xenstore
+	rm -rf @VARBASE@/cache/xenstore
     fi
 fi
 mkdir -p $(dirname "$cache")
@@ -98,7 +98,7 @@
 
 xenstore_rm_cached() {
     key="$1"
-    cache=/var/cache/xenstore/$key
+    cache=@VARBASE@/cache/xenstore/$key
     if [ ! -e $cache ] ; then
 	return 1
     fi
@@ -112,7 +112,7 @@
 }
 
 xenstore_list_interfaces_cached() {
-    topdir=/var/cache/xenstore/attr
+    topdir=@VARBASE@/cache/xenstore/attr
     if [ -d $topdir ] ; then
 	cd $topdir 
 	for dir in * ; do 
@@ -133,49 +133,45 @@
 
 
 
-# e.g.
-# $ ip addr show
-# 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue 
-#     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
-#     inet 127.0.0.1/8 scope host lo
-#     inet6 ::1/128 scope host 
-#        valid_lft forever preferred_lft forever
-# 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
-#     link/ether 00:13:20:95:e8:74 brd ff:ff:ff:ff:ff:ff
-#     inet 172.31.0.57/20 brd 172.31.15.255 scope global eth0
-#     inet6 fe80::213:20ff:fe95:e874/64 scope link 
-#        valid_lft forever preferred_lft forever
-# 3: sit0: <NOARP> mtu 1480 qdisc noop 
-#     link/sit 0.0.0.0 brd 0.0.0.0
-
-#eval $(ip addr show | \
-#       sed -n -e 's/^[[:digit:]]*: \([a-z0-9]*\): .*/ifs="\$ifs \1"; current="\1"; /gp;' \
-#	      -e 's/^[[:space:]]\{4\}inet \('${IPADDR_RE}'\)\/.*/eval inet_\${current}="\1"; /gp;')
-
-# e.g.
-# eth0      Link encap:Ethernet  HWaddr 00:13:20:95:E8:74  
-#           inet addr:172.31.0.57  Bcast:172.31.15.255  Mask:255.255.240.0
-#           inet6 addr: fe80::213:20ff:fe95:e874/64 Scope:Link
-#           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
-#           RX packets:98001128 errors:0 dropped:0 overruns:0 frame:0
-#           TX packets:87728920 errors:0 dropped:0 overruns:0 carrier:0
-#           collisions:0 txqueuelen:1000 
-#           RX bytes:35864034092 (33.4 GiB)  TX bytes:27544025180 (25.6 GiB)
-#           Interrupt:177 
-# 
-# lo        Link encap:Local Loopback  
-#           inet addr:127.0.0.1  Mask:255.0.0.0
-#           inet6 addr: ::1/128 Scope:Host
-#           UP LOOPBACK RUNNING  MTU:16436  Metric:1
-#           RX packets:32928 errors:0 dropped:0 overruns:0 frame:0
-#           TX packets:32928 errors:0 dropped:0 overruns:0 carrier:0
-#           collisions:0 txqueuelen:0 
-#           RX bytes:3604609 (3.4 MiB)  TX bytes:3604609 (3.4 MiB)
-
-eval $(/sbin/ifconfig | \
-        sed -n -e '/^[0-9a-z][0-9a-z]*\:/,/^$/d' \
-               -e 's/^\([0-9a-z][0-9a-z]*\) .*/ifs="\$ifs \1"; current="\1"; /gp;' \
-               -e 's/  *inet addr:\('$IPADDR_RE'\) .*/eval inet_\${current}="\1"; /gp;')
+#xennet0: flags=8863<UP,BROADCAST,NOTRAILERS,RUNNING,SIMPLEX,MULTICAST> mtu 1500
+#	capabilities=2800<TCP4CSUM_Tx,UDP4CSUM_Tx>
+#	enabled=0
+#	address: 0e:7e:86:20:7b:82
+#	inet 192.168.1.109 netmask 0xffffff00 broadcast 192.168.1.255
+#	inet6 fe80::c7e:86ff:fe20:7b82%xennet0 prefixlen 64 scopeid 0x1
+#lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 33184
+#	inet 127.0.0.1 netmask 0xff000000
+#	inet6 ::1 prefixlen 128
+#	inet6 fe80::1%lo0 prefixlen 64 scopeid 0x2
+
+#/sbin/ifconfig | \
+#        sed -n -e '/^[0-9a-z][0-9a-z]*\:/,/^$/d' \
+#               -e 's/^\([0-9a-z][0-9a-z]*\) .*/ifs="\$ifs \1"; current="\1"; /gp;' \
+#               -e 's/  *inet \('$IPADDR_RE'\) .*/eval inet_\${current}="\1"; /gp;'
+
+eval $(/sbin/ifconfig -a | \
+	awk '{
+		if ($0~"^[a-z][a-z0-9]*[0-9]:") {
+			if ($0~"^xennet[0-9]+:") {
+				current = $1
+				sub(":.*", "", current)
+				sub("xennet", "eth", current)
+				iflist = iflist " " current
+			} else {
+				current =""
+			}
+		}
+		if ($1 == "inet" && current != "") {
+			ifs[current] = $2
+			current = ""
+		}
+	}
+	END {
+		sub("^ ", "", iflist)
+		printf "ifs=%c%s%c\n", 39, iflist, 39
+		for (i in ifs)
+			printf "inet_%s=%s\n", i, ifs[i]
+	}')
 
 # network
 for if in $ifs ; do
@@ -206,16 +202,18 @@
 fi
 
 # whether I support ballooning or not
-xenstore_write_cached "control/feature-balloon" "1"
-
-# whether I support ballooning or not
-xenstore_write_cached "control/feature-balloon" "1"
+sysctl -n machdep.xen.balloon.current > /dev/null 2>&1
+if [ "$?" = 0 ]; then
+	xenstore_write_cached "control/feature-balloon" "1"
+else
+	xenstore_write_cached "control/feature-balloon" "0"
+fi
 
 # build time addons
-xenstore_write_cached "attr/PVAddons/MajorVersion" "5"
-xenstore_write_cached "attr/PVAddons/MinorVersion" "6"
-xenstore_write_cached "attr/PVAddons/MicroVersion" "0" 
-xenstore_write_cached "attr/PVAddons/BuildVersion" "39674"
+xenstore_write_cached "attr/PVAddons/MajorVersion" "@XENMAJOR@"
+xenstore_write_cached "attr/PVAddons/MinorVersion" "@XENMINOR@"
+xenstore_write_cached "attr/PVAddons/MicroVersion" "@XENTEENY@" 
+xenstore_write_cached "attr/PVAddons/BuildVersion" "@XENBUILD@"
 xenstore_write_cached "attr/PVAddons/Installed" "1" 
 
 # update xenstore if necc
