ia64/xen-unstable

changeset 18770:4bfc67b09e9c

tools/hotplug: Separate OS-specific scripts.

Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Nov 04 12:13:42 2008 +0000 (2008-11-04)
parents 6bd021d987d7
children 484cf12ba667
files tools/Makefile tools/examples/Makefile tools/examples/blktap tools/examples/block tools/examples/block-common.sh tools/examples/block-enbd tools/examples/block-nbd tools/examples/external-device-migrate tools/examples/init.d/sysconfig.xendomains tools/examples/init.d/xend tools/examples/init.d/xendomains tools/examples/locking.sh tools/examples/logging.sh tools/examples/network-bridge tools/examples/network-nat tools/examples/network-route tools/examples/vif-bridge tools/examples/vif-common.sh tools/examples/vif-nat tools/examples/vif-route tools/examples/vscsi tools/examples/vtpm tools/examples/vtpm-common.sh tools/examples/vtpm-delete tools/examples/vtpm-hotplug-common.sh tools/examples/vtpm-impl tools/examples/vtpm-migration.sh tools/examples/xen-backend.agent tools/examples/xen-backend.rules tools/examples/xen-hotplug-cleanup tools/examples/xen-hotplug-common.sh tools/examples/xen-network-common.sh tools/examples/xen-script-common.sh tools/hotplug/Linux/Makefile tools/hotplug/Linux/blktap tools/hotplug/Linux/block tools/hotplug/Linux/block-common.sh tools/hotplug/Linux/block-enbd tools/hotplug/Linux/block-nbd tools/hotplug/Linux/external-device-migrate tools/hotplug/Linux/init.d/sysconfig.xendomains tools/hotplug/Linux/init.d/xend tools/hotplug/Linux/init.d/xendomains tools/hotplug/Linux/locking.sh tools/hotplug/Linux/logging.sh tools/hotplug/Linux/network-bridge tools/hotplug/Linux/network-nat tools/hotplug/Linux/network-route tools/hotplug/Linux/vif-bridge tools/hotplug/Linux/vif-common.sh tools/hotplug/Linux/vif-nat tools/hotplug/Linux/vif-route tools/hotplug/Linux/vscsi tools/hotplug/Linux/vtpm tools/hotplug/Linux/vtpm-common.sh tools/hotplug/Linux/vtpm-delete tools/hotplug/Linux/vtpm-hotplug-common.sh tools/hotplug/Linux/vtpm-impl tools/hotplug/Linux/vtpm-migration.sh tools/hotplug/Linux/xen-backend.agent tools/hotplug/Linux/xen-backend.rules tools/hotplug/Linux/xen-hotplug-cleanup tools/hotplug/Linux/xen-hotplug-common.sh tools/hotplug/Linux/xen-network-common.sh tools/hotplug/Linux/xen-script-common.sh tools/hotplug/Makefile tools/hotplug/NetBSD/Makefile tools/hotplug/NetBSD/block-nbsd tools/hotplug/NetBSD/qemu-ifup-nbsd tools/hotplug/NetBSD/vif-bridge-nbsd tools/hotplug/NetBSD/vif-ip-nbsd tools/hotplug/common/Makefile
line diff
     1.1 --- a/tools/Makefile	Tue Nov 04 11:46:00 2008 +0000
     1.2 +++ b/tools/Makefile	Tue Nov 04 12:13:42 2008 +0000
     1.3 @@ -9,6 +9,7 @@ SUBDIRS-y += flask
     1.4  SUBDIRS-y += xenstore
     1.5  SUBDIRS-y += misc
     1.6  SUBDIRS-y += examples
     1.7 +SUBDIRS-y += hotplug
     1.8  SUBDIRS-y += xentrace
     1.9  SUBDIRS-$(CONFIG_XCUTILS) += xcutils
    1.10  SUBDIRS-$(CONFIG_X86) += firmware
     2.1 --- a/tools/examples/Makefile	Tue Nov 04 11:46:00 2008 +0000
     2.2 +++ b/tools/examples/Makefile	Tue Nov 04 12:13:42 2008 +0000
     2.3 @@ -24,41 +24,6 @@ XEN_CONFIGS += xmexample.vti
     2.4  XEN_CONFIGS += xend-pci-quirks.sxp
     2.5  XEN_CONFIGS += xend-pci-permissive.sxp
     2.6  
     2.7 -# Xen script dir and scripts to go there.
     2.8 -XEN_SCRIPT_DIR = /etc/xen/scripts
     2.9 -XEN_SCRIPTS = network-bridge vif-bridge
    2.10 -XEN_SCRIPTS += network-route vif-route
    2.11 -XEN_SCRIPTS += network-nat vif-nat
    2.12 -XEN_SCRIPTS += block
    2.13 -XEN_SCRIPTS += block-enbd block-nbd
    2.14 -XEN_SCRIPTS += blktap
    2.15 -XEN_SCRIPTS += vtpm vtpm-delete
    2.16 -XEN_SCRIPTS += xen-hotplug-cleanup
    2.17 -XEN_SCRIPTS += external-device-migrate
    2.18 -XEN_SCRIPTS += vscsi
    2.19 -XEN_SCRIPT_DATA = xen-script-common.sh locking.sh logging.sh
    2.20 -XEN_SCRIPT_DATA += xen-hotplug-common.sh xen-network-common.sh vif-common.sh
    2.21 -XEN_SCRIPT_DATA += block-common.sh vtpm-common.sh vtpm-hotplug-common.sh
    2.22 -XEN_SCRIPT_DATA += vtpm-migration.sh vtpm-impl
    2.23 -
    2.24 -XEN_HOTPLUG_DIR = /etc/hotplug
    2.25 -XEN_HOTPLUG_SCRIPTS = xen-backend.agent
    2.26 -
    2.27 -UDEV_RULES_DIR = /etc/udev
    2.28 -UDEV_RULES = xen-backend.rules
    2.29 -
    2.30 -DI = $(if $(DISTDIR),$(shell readlink -f $(DISTDIR)),)
    2.31 -DE = $(if $(DESTDIR),$(shell readlink -f $(DESTDIR)),)
    2.32 -ifeq ($(findstring $(DI),$(DE)),$(DI))
    2.33 -HOTPLUGS=install-hotplug install-udev
    2.34 -else
    2.35 -ifeq ($(shell [ -x /usr/bin/udevinfo ] && [ `/usr/bin/udevinfo -V | sed -e 's/^[^0-9]* \([0-9]\{1,\}\)[^0-9]\{0,\}/\1/'` -ge 059 ] && echo 1),1)
    2.36 -HOTPLUGS=install-udev
    2.37 -else
    2.38 -HOTPLUGS=install-hotplug
    2.39 -endif
    2.40 -endif
    2.41 -
    2.42  .PHONY: all
    2.43  all:
    2.44  
    2.45 @@ -66,7 +31,7 @@ all:
    2.46  build:
    2.47  
    2.48  .PHONY: install
    2.49 -install: all install-readmes install-initd install-configs install-scripts $(HOTPLUGS)
    2.50 +install: all install-readmes install-configs $(HOTPLUGS)
    2.51  
    2.52  .PHONY: install-readmes
    2.53  install-readmes:
    2.54 @@ -77,14 +42,6 @@ install-readmes:
    2.55  	    $(INSTALL_DATA) $$i $(DESTDIR)$(XEN_CONFIG_DIR); \
    2.56  	done
    2.57  
    2.58 -.PHONY: install-initd
    2.59 -install-initd:
    2.60 -	[ -d $(DESTDIR)/etc/init.d ] || $(INSTALL_DIR) $(DESTDIR)/etc/init.d
    2.61 -	[ -d $(DESTDIR)/etc/sysconfig ] || $(INSTALL_DIR) $(DESTDIR)/etc/sysconfig
    2.62 -	$(INSTALL_PROG) $(XEND_INITD) $(DESTDIR)/etc/init.d
    2.63 -	$(INSTALL_PROG) $(XENDOMAINS_INITD) $(DESTDIR)/etc/init.d
    2.64 -	$(INSTALL_PROG) $(XENDOMAINS_SYSCONFIG) $(DESTDIR)/etc/sysconfig/xendomains
    2.65 -
    2.66  .PHONY: install-configs
    2.67  install-configs: $(XEN_CONFIGS)
    2.68  	[ -d $(DESTDIR)$(XEN_CONFIG_DIR) ] || \
    2.69 @@ -96,19 +53,6 @@ install-configs: $(XEN_CONFIGS)
    2.70  	    $(INSTALL_DATA) $$i $(DESTDIR)$(XEN_CONFIG_DIR); \
    2.71  	done
    2.72  
    2.73 -.PHONY: install-scripts
    2.74 -install-scripts:
    2.75 -	[ -d $(DESTDIR)$(XEN_SCRIPT_DIR) ] || \
    2.76 -		$(INSTALL_DIR) $(DESTDIR)$(XEN_SCRIPT_DIR)
    2.77 -	set -e; for i in $(XEN_SCRIPTS); \
    2.78 -	    do \
    2.79 -	    $(INSTALL_PROG) $$i $(DESTDIR)$(XEN_SCRIPT_DIR); \
    2.80 -	done
    2.81 -	set -e; for i in $(XEN_SCRIPT_DATA); \
    2.82 -	    do \
    2.83 -	    $(INSTALL_DATA) $$i $(DESTDIR)$(XEN_SCRIPT_DIR); \
    2.84 -	done
    2.85 -
    2.86  .PHONY: install-hotplug
    2.87  install-hotplug:
    2.88  	[ -d $(DESTDIR)$(XEN_HOTPLUG_DIR) ] || \
     3.1 --- a/tools/examples/blktap	Tue Nov 04 11:46:00 2008 +0000
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,93 +0,0 @@
     3.4 -#!/bin/bash
     3.5 -
     3.6 -# Copyright (c) 2005, XenSource Ltd.
     3.7 -
     3.8 -dir=$(dirname "$0")
     3.9 -. "$dir/xen-hotplug-common.sh"
    3.10 -. "$dir/block-common.sh"
    3.11 -
    3.12 -findCommand "$@"
    3.13 -
    3.14 -##
    3.15 -# check_blktap_sharing file mode
    3.16 -#
    3.17 -# Perform the sharing check for the given blktap and mode.
    3.18 -#
    3.19 -check_blktap_sharing()
    3.20 -{
    3.21 -    local file="$1"
    3.22 -    local mode="$2"
    3.23 -
    3.24 -    local base_path="$XENBUS_BASE_PATH/$XENBUS_TYPE"
    3.25 -    for dom in $(xenstore-list "$base_path")
    3.26 -    do
    3.27 -        for dev in $(xenstore-list "$base_path/$dom")
    3.28 -        do
    3.29 -            params=$(xenstore_read "$base_path/$dom/$dev/params" | cut -d: -f2)
    3.30 -            if [ "$file" = "$params" ]
    3.31 -            then
    3.32 -
    3.33 -                if [ "$mode" = 'w' ]
    3.34 -                then
    3.35 -                    if ! same_vm "$dom" 
    3.36 -                    then
    3.37 -                        echo 'guest'
    3.38 -                        return
    3.39 -                    fi
    3.40 -                else 
    3.41 -                    local m=$(xenstore_read "$base_path/$dom/$dev/mode")
    3.42 -                    m=$(canonicalise_mode "$m")
    3.43 -
    3.44 -                    if [ "$m" = 'w' ] 
    3.45 -                    then
    3.46 -                        if ! same_vm "$dom"
    3.47 -                        then
    3.48 -                            echo 'guest'
    3.49 -                            return
    3.50 -                        fi
    3.51 -                    fi
    3.52 -                fi
    3.53 -            fi
    3.54 -        done
    3.55 -    done
    3.56 -
    3.57 -    echo 'ok'
    3.58 -}
    3.59 -
    3.60 -
    3.61 -t=$(xenstore_read_default "$XENBUS_PATH/type" 'MISSING')
    3.62 -if [ -n "$t" ]
    3.63 -then
    3.64 -    p=$(xenstore_read "$XENBUS_PATH/params")
    3.65 -    # if we have a ':', chew from head including :
    3.66 -    if echo $p | grep -q \:
    3.67 -    then
    3.68 -        p=${p#*:}
    3.69 -    fi
    3.70 -fi
    3.71 -# some versions of readlink cannot be passed a regular file
    3.72 -if [ -L "$p" ]; then
    3.73 -    file=$(readlink -f "$p") || fatal "$p link does not exist."
    3.74 -else
    3.75 -    file="$p"
    3.76 -fi
    3.77 -
    3.78 -if [ "$command" = 'add' ]
    3.79 -then
    3.80 -    [ -e "$file" ] || { fatal $file does not exist; }
    3.81 -
    3.82 -    FRONTEND_ID=$(xenstore_read "$XENBUS_PATH/frontend-id")
    3.83 -    FRONTEND_UUID=$(xenstore_read "/local/domain/$FRONTEND_ID/vm")
    3.84 -    mode=$(xenstore_read "$XENBUS_PATH/mode")
    3.85 -    mode=$(canonicalise_mode "$mode")
    3.86 -
    3.87 -    if [ "$mode" != '!' ] 
    3.88 -    then
    3.89 -        result=$(check_blktap_sharing "$file" "$mode")
    3.90 -        [ "$result" = 'ok' ] || ebusy "$file already in use by other domain"
    3.91 -    fi
    3.92 -
    3.93 -    success
    3.94 -fi
    3.95 -
    3.96 -exit 0
     4.1 --- a/tools/examples/block	Tue Nov 04 11:46:00 2008 +0000
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,381 +0,0 @@
     4.4 -#!/bin/bash
     4.5 -
     4.6 -dir=$(dirname "$0")
     4.7 -. "$dir/block-common.sh"
     4.8 -
     4.9 -expand_dev() {
    4.10 -  local dev
    4.11 -  case $1 in
    4.12 -  /*)
    4.13 -    dev=$1
    4.14 -    ;;
    4.15 -  *)
    4.16 -    dev=/dev/$1
    4.17 -    ;;
    4.18 -  esac
    4.19 -  echo -n $dev
    4.20 -}
    4.21 -
    4.22 -
    4.23 -##
    4.24 -# check_sharing device mode
    4.25 -#
    4.26 -# Check whether the device requested is already in use.  To use the device in
    4.27 -# read-only mode, it may be in use in read-only mode, but may not be in use in
    4.28 -# read-write anywhere at all.  To use the device in read-write mode, it must
    4.29 -# not be in use anywhere at all.
    4.30 -#
    4.31 -# Prints one of
    4.32 -#
    4.33 -#    'local': the device may not be used because it is mounted in the current
    4.34 -#             (i.e. the privileged domain) in a way incompatible with the
    4.35 -#             requested mode;
    4.36 -#    'guest': the device may not be used because it already mounted by a guest
    4.37 -#             in a way incompatible with the requested mode; or
    4.38 -#    'ok':    the device may be used.
    4.39 -#
    4.40 -check_sharing()
    4.41 -{
    4.42 -  local dev="$1"
    4.43 -  local mode="$2"
    4.44 -
    4.45 -  local devmm=$(device_major_minor "$dev")
    4.46 -  local file
    4.47 -
    4.48 -  if [ "$mode" = 'w' ]
    4.49 -  then
    4.50 -    toskip="^$"
    4.51 -  else
    4.52 -    toskip="^[^ ]* [^ ]* [^ ]* ro[, ]"
    4.53 -  fi
    4.54 -
    4.55 -  for file in $(cat /proc/mounts | grep -v "$toskip" | cut -f 1 -d ' ')
    4.56 -  do
    4.57 -    if [ -e "$file" ]
    4.58 -    then
    4.59 -      local d=$(device_major_minor "$file")
    4.60 -
    4.61 -      if [ "$d" = "$devmm" ]
    4.62 -      then
    4.63 -        echo 'local'
    4.64 -        return
    4.65 -      fi
    4.66 -    fi
    4.67 -  done
    4.68 -
    4.69 -  local base_path="$XENBUS_BASE_PATH/$XENBUS_TYPE"
    4.70 -  for dom in $(xenstore-list "$base_path")
    4.71 -  do
    4.72 -    for dev in $(xenstore-list "$base_path/$dom")
    4.73 -    do
    4.74 -      d=$(xenstore_read_default "$base_path/$dom/$dev/physical-device" "")
    4.75 -
    4.76 -      if [ "$d" = "$devmm" ]
    4.77 -      then
    4.78 -        if [ "$mode" = 'w' ]
    4.79 -        then
    4.80 -          if ! same_vm $dom
    4.81 -          then
    4.82 -            echo 'guest'
    4.83 -            return
    4.84 -          fi
    4.85 -        else
    4.86 -          local m=$(xenstore_read "$base_path/$dom/$dev/mode")
    4.87 -          m=$(canonicalise_mode "$m")
    4.88 -
    4.89 -          if [ "$m" = 'w' ]
    4.90 -          then
    4.91 -            if ! same_vm $dom
    4.92 -            then
    4.93 -              echo 'guest'
    4.94 -              return
    4.95 -            fi
    4.96 -          fi
    4.97 -        fi
    4.98 -      fi
    4.99 -    done
   4.100 -  done
   4.101 -
   4.102 -  echo 'ok'
   4.103 -}
   4.104 -
   4.105 -
   4.106 -##
   4.107 -# check_device_sharing dev mode
   4.108 -#
   4.109 -# Perform the sharing check for the given physical device and mode.
   4.110 -#
   4.111 -check_device_sharing()
   4.112 -{
   4.113 -  local dev="$1"
   4.114 -  local mode=$(canonicalise_mode "$2")
   4.115 -  local result
   4.116 -
   4.117 -  if [ "x$mode" = 'x!' ]
   4.118 -  then
   4.119 -    return 0
   4.120 -  fi
   4.121 -
   4.122 -  result=$(check_sharing "$dev" "$mode")
   4.123 -
   4.124 -  if [ "$result" != 'ok' ]
   4.125 -  then
   4.126 -    do_ebusy "Device $dev is mounted " "$mode" "$result"
   4.127 -  fi
   4.128 -}
   4.129 -
   4.130 -
   4.131 -##
   4.132 -# check_device_sharing file dev mode
   4.133 -#
   4.134 -# Perform the sharing check for the given file mounted through the given
   4.135 -# loopback interface, in the given mode.
   4.136 -#
   4.137 -check_file_sharing()
   4.138 -{
   4.139 -  local file="$1"
   4.140 -  local dev="$2"
   4.141 -  local mode="$3"
   4.142 -
   4.143 -  result=$(check_sharing "$dev" "$mode")
   4.144 -
   4.145 -  if [ "$result" != 'ok' ]
   4.146 -  then
   4.147 -    do_ebusy "File $file is loopback-mounted through $dev,
   4.148 -which is mounted " "$mode" "$result"
   4.149 -  fi
   4.150 -}
   4.151 -
   4.152 -
   4.153 -##
   4.154 -# do_ebusy prefix mode result
   4.155 -#
   4.156 -# Helper function for check_device_sharing check_file_sharing, calling ebusy
   4.157 -# with an error message constructed from the given prefix, mode, and result
   4.158 -# from a call to check_sharing.
   4.159 -#
   4.160 -do_ebusy()
   4.161 -{
   4.162 -  local prefix="$1"
   4.163 -  local mode="$2"
   4.164 -  local result="$3"
   4.165 -
   4.166 -  if [ "$result" = 'guest' ]
   4.167 -  then
   4.168 -    dom='a guest '
   4.169 -    when='now'
   4.170 -  else
   4.171 -    dom='the privileged '
   4.172 -    when='by a guest'
   4.173 -  fi
   4.174 -
   4.175 -  if [ "$mode" = 'w' ]
   4.176 -  then
   4.177 -    m1=''
   4.178 -    m2=''
   4.179 -  else
   4.180 -    m1='read-write '
   4.181 -    m2='read-only '
   4.182 -  fi
   4.183 -
   4.184 -  release_lock "block"
   4.185 -  ebusy \
   4.186 -"${prefix}${m1}in ${dom}domain,
   4.187 -and so cannot be mounted ${m2}${when}."
   4.188 -}
   4.189 -
   4.190 -
   4.191 -t=$(xenstore_read_default "$XENBUS_PATH/type" 'MISSING')
   4.192 -
   4.193 -case "$command" in
   4.194 -  add)
   4.195 -    phys=$(xenstore_read_default "$XENBUS_PATH/physical-device" 'MISSING')
   4.196 -    if [ "$phys" != 'MISSING' ]
   4.197 -    then
   4.198 -      # Depending upon the hotplug configuration, it is possible for this
   4.199 -      # script to be called twice, so just bail.
   4.200 -      exit 0
   4.201 -    fi
   4.202 -
   4.203 -    if [ -n "$t" ]
   4.204 -    then
   4.205 -      p=$(xenstore_read "$XENBUS_PATH/params")
   4.206 -      mode=$(xenstore_read "$XENBUS_PATH/mode")
   4.207 -    fi
   4.208 -
   4.209 -    case $t in 
   4.210 -      phy)
   4.211 -        dev=$(expand_dev $p)
   4.212 -        FRONTEND_ID=$(xenstore_read "$XENBUS_PATH/frontend-id")
   4.213 -        FRONTEND_UUID=$(xenstore_read_default \
   4.214 -            "/local/domain/$FRONTEND_ID/vm" 'unknown')
   4.215 -
   4.216 -        if [ -L "$dev" ]
   4.217 -        then
   4.218 -          dev=$(readlink -f "$dev") || fatal "$dev link does not exist."
   4.219 -        fi
   4.220 -        test -e "$dev" || fatal "$dev does not exist."
   4.221 -        test -b "$dev" || fatal "$dev is not a block device."
   4.222 -
   4.223 -        claim_lock "block"
   4.224 -        check_device_sharing "$dev" "$mode"
   4.225 -	write_dev "$dev"
   4.226 -        release_lock "block"
   4.227 -	exit 0
   4.228 -	;;
   4.229 -
   4.230 -      file)
   4.231 -        # Canonicalise the file, for sharing check comparison, and the mode
   4.232 -        # for ease of use here.
   4.233 -        file=$(readlink -f "$p") || fatal "$p does not exist."
   4.234 -        test -f "$file" || fatal "$file does not exist."
   4.235 -        mode=$(canonicalise_mode "$mode")
   4.236 -
   4.237 -        claim_lock "block"
   4.238 -
   4.239 -        if [ "$mode" = 'w' ] && ! stat "$file" -c %A | grep -q w
   4.240 -        then
   4.241 -          release_lock "block"
   4.242 -          ebusy \
   4.243 -"File $file is read-only, and so I will not
   4.244 -mount it read-write in a guest domain."
   4.245 -        fi
   4.246 -
   4.247 -        loopdev=''
   4.248 -        for dev in /dev/loop*
   4.249 -        do
   4.250 -          if [ ! -b "$dev" ]
   4.251 -          then
   4.252 -            continue
   4.253 -          fi
   4.254 -
   4.255 -          f=$(losetup "$dev" 2>/dev/null) || f=''
   4.256 -
   4.257 -          if [ "$f" ]
   4.258 -          then
   4.259 -            # $dev is in use.  Check sharing.
   4.260 -            if [ "x$mode" = 'x!' ]
   4.261 -            then
   4.262 -              continue
   4.263 -            fi
   4.264 -
   4.265 -            f=$(echo "$f" | sed -e 's/.*(\(.*\)).*/\1/g')
   4.266 -
   4.267 -            # $f is the filename, as read from losetup, but the loopback
   4.268 -            # driver truncates filenames at 64 characters, so we need to go
   4.269 -            # trawling through the store if it's longer than that.  Truncation
   4.270 -            # is indicated by an asterisk at the end of the filename.
   4.271 -            if expr index "$f" '*' >/dev/null
   4.272 -            then
   4.273 -              found=""
   4.274 -              for dom in $(xenstore-list "$XENBUS_BASE_PATH")
   4.275 -              do
   4.276 -                for domdev in $(xenstore-list "$XENBUS_BASE_PATH/$dom")
   4.277 -                do
   4.278 -                  d=$(xenstore_read_default \
   4.279 -                        "$XENBUS_BASE_PATH/$dom/$domdev/node" "")
   4.280 -                  if [ "$d" = "$dev" ]
   4.281 -                  then
   4.282 -                    f=$(xenstore_read "$XENBUS_BASE_PATH/$dom/$domdev/params")
   4.283 -                    found=1
   4.284 -                    break 2
   4.285 -                  fi
   4.286 -                done
   4.287 -              done
   4.288 -
   4.289 -              if [ ! "$found" ]
   4.290 -              then
   4.291 -                # This loopback device is in use by someone else, so skip it.
   4.292 -                log debug "Loopback sharing check skips device $dev."
   4.293 -                continue
   4.294 -              fi
   4.295 -            fi
   4.296 -
   4.297 -            # Canonicalise the filename for the comparison.
   4.298 -
   4.299 -            # I have seen this readlink fails because the filename given by
   4.300 -            # losetup is only the basename.  This cannot happen when the loop
   4.301 -            # device is set up through this script, because file is
   4.302 -            # canonicalised above, but it may happen when loop devices are set
   4.303 -            # up some other way.  This readlink may also conceivably fail if
   4.304 -            # the file backing this loop device has been removed.
   4.305 -
   4.306 -            # For maximum safety, in the case that $f does not resolve, we
   4.307 -            # assume that $file and $f are in the same directory.
   4.308 -
   4.309 -            # If you create a loopback filesystem, remove it and continue to
   4.310 -            # run on it, and then create another file with the same name, then
   4.311 -            # this check will block that -- don't do that.
   4.312 -
   4.313 -            # If you create loop devices through some other mechanism, use
   4.314 -            # relative filenames, and then use the same filename through this
   4.315 -            # script, then this check will block that -- don't do that either.
   4.316 -
   4.317 -            f=$(readlink -f "$f" || echo $(dirname "$file")/$(basename "$f"))
   4.318 -
   4.319 -
   4.320 -            if [ "$f" = "$file" ]
   4.321 -            then
   4.322 -              check_file_sharing "$file" "$dev" "$mode"
   4.323 -            fi
   4.324 -          else
   4.325 -            # $dev is not in use, so we'll remember it for use later; we want
   4.326 -            # to finish the sharing check first.
   4.327 -
   4.328 -            if [ "$loopdev" = '' ]
   4.329 -            then
   4.330 -              loopdev="$dev"
   4.331 -            fi
   4.332 -          fi
   4.333 -        done
   4.334 -
   4.335 -        if [ "$loopdev" = '' ]
   4.336 -        then
   4.337 -          release_lock "block"
   4.338 -          fatal 'Failed to find an unused loop device'
   4.339 -        fi
   4.340 -
   4.341 -        if LANG=C losetup -h 2>&1 | grep read-only >/dev/null
   4.342 -        then
   4.343 -          roflag="-$mode"; roflag="${roflag#-w}"; roflag="${roflag#-!}"
   4.344 -        else
   4.345 -          roflag=''
   4.346 -        fi
   4.347 -        do_or_die losetup $roflag "$loopdev" "$file"
   4.348 -        xenstore_write "$XENBUS_PATH/node" "$loopdev"
   4.349 -        write_dev "$loopdev"
   4.350 -        release_lock "block"
   4.351 -        exit 0
   4.352 -	;;
   4.353 -
   4.354 -      "")
   4.355 -        claim_lock "block"
   4.356 -        success
   4.357 -        release_lock "block"
   4.358 -	;;
   4.359 -    esac
   4.360 -    ;;
   4.361 -
   4.362 -  remove)
   4.363 -    case $t in 
   4.364 -      phy)
   4.365 -	exit 0
   4.366 -	;;
   4.367 -
   4.368 -      file)
   4.369 -        node=$(xenstore_read "$XENBUS_PATH/node")
   4.370 -	losetup -d "$node"
   4.371 -	exit 0
   4.372 -	;;
   4.373 -
   4.374 -      "")
   4.375 -        exit 0
   4.376 -	;;
   4.377 -    esac
   4.378 -    ;;
   4.379 -
   4.380 -esac
   4.381 -
   4.382 -# If we've reached here, $t is neither phy nor file, so fire a helper script.
   4.383 -[ -x /etc/xen/scripts/block-"$t" ] && \
   4.384 -  /etc/xen/scripts/block-"$t" "$command" $node
     5.1 --- a/tools/examples/block-common.sh	Tue Nov 04 11:46:00 2008 +0000
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,116 +0,0 @@
     5.4 -#
     5.5 -# Copyright (c) 2005 XenSource Ltd.
     5.6 -#
     5.7 -# This library is free software; you can redistribute it and/or
     5.8 -# modify it under the terms of version 2.1 of the GNU Lesser General Public
     5.9 -# License as published by the Free Software Foundation.
    5.10 -#
    5.11 -# This library is distributed in the hope that it will be useful,
    5.12 -# but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.13 -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    5.14 -# Lesser General Public License for more details.
    5.15 -#
    5.16 -# You should have received a copy of the GNU Lesser General Public
    5.17 -# License along with this library; if not, write to the Free Software
    5.18 -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    5.19 -#
    5.20 -
    5.21 -
    5.22 -dir=$(dirname "$0")
    5.23 -. "$dir/xen-hotplug-common.sh"
    5.24 -
    5.25 -findCommand "$@"
    5.26 -
    5.27 -if [ "$command" != "add" ] &&
    5.28 -   [ "$command" != "remove" ]
    5.29 -then
    5.30 -  log err "Invalid command: $command"
    5.31 -  exit 1
    5.32 -fi
    5.33 -
    5.34 -
    5.35 -XENBUS_PATH="${XENBUS_PATH:?}"
    5.36 -
    5.37 -
    5.38 -ebusy()
    5.39 -{
    5.40 -  xenstore_write "$XENBUS_PATH/hotplug-error" "$*" \
    5.41 -                 "$XENBUS_PATH/hotplug-status" busy
    5.42 -  log err "$@"
    5.43 -  exit 1
    5.44 -}
    5.45 -
    5.46 -
    5.47 -##
    5.48 -# Print the given device's major and minor numbers, written in hex and
    5.49 -# separated by a colon.
    5.50 -device_major_minor()
    5.51 -{
    5.52 -  stat -L -c %t:%T "$1"
    5.53 -}
    5.54 -
    5.55 -
    5.56 -##
    5.57 -# Write physical-device = MM,mm to the store, where MM and mm are the major 
    5.58 -# and minor numbers of device respectively.
    5.59 -#
    5.60 -# @param device The device from which major and minor numbers are read, which
    5.61 -#               will be written into the store.
    5.62 -#
    5.63 -write_dev() {
    5.64 -  local mm
    5.65 -  
    5.66 -  mm=$(device_major_minor "$1")
    5.67 - 
    5.68 -  if [ -z $mm ]
    5.69 -  then
    5.70 -    fatal "Backend device does not exist"
    5.71 -  fi
    5.72 - 
    5.73 -  xenstore_write "$XENBUS_PATH/physical-device" "$mm"
    5.74 -
    5.75 -  success
    5.76 -}
    5.77 -
    5.78 -
    5.79 -##
    5.80 -# canonicalise_mode mode
    5.81 -#
    5.82 -# Takes the given mode, which may be r, w, ro, rw, w!, or rw!, or variations
    5.83 -# thereof, and canonicalises them to one of
    5.84 -#
    5.85 -#   'r': perform checks for a new read-only mount;
    5.86 -#   'w': perform checks for a read-write mount; or
    5.87 -#   '!': perform no checks at all.
    5.88 -#
    5.89 -canonicalise_mode()
    5.90 -{
    5.91 -  local mode="$1"
    5.92 -
    5.93 -  if ! expr index "$mode" 'w' >/dev/null
    5.94 -  then
    5.95 -    echo 'r'
    5.96 -  elif ! expr index "$mode" '!' >/dev/null
    5.97 -  then
    5.98 -    echo 'w'
    5.99 -  else
   5.100 -    echo '!'
   5.101 -  fi
   5.102 -}
   5.103 -
   5.104 -
   5.105 -same_vm()
   5.106 -{
   5.107 -  local otherdom="$1"
   5.108 -  # Note that othervm can be MISSING here, because Xend will be racing with
   5.109 -  # the hotplug scripts -- the entries in /local/domain can be removed by
   5.110 -  # Xend before the hotplug scripts have removed the entry in
   5.111 -  # /local/domain/0/backend/.  In this case, we want to pretend that the
   5.112 -  # VM is the same as FRONTEND_UUID, because that way the 'sharing' will be
   5.113 -  # allowed.
   5.114 -  local othervm=$(xenstore_read_default "/local/domain/$otherdom/vm"         \
   5.115 -                  "$FRONTEND_UUID")
   5.116 -
   5.117 -  [ "$FRONTEND_UUID" = "$othervm" ]
   5.118 -}
   5.119 -
     6.1 --- a/tools/examples/block-enbd	Tue Nov 04 11:46:00 2008 +0000
     6.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.3 @@ -1,27 +0,0 @@
     6.4 -#!/bin/bash
     6.5 -
     6.6 -# Usage: block-enbd [bind server ctl_port |unbind node]
     6.7 -#
     6.8 -# The node argument to unbind is the name of the device node we are to
     6.9 -# unbind.
    6.10 -#
    6.11 -# This assumes you're running a correctly configured server at the other end!
    6.12 -
    6.13 -dir=$(dirname "$0")
    6.14 -. "$dir/block-common.sh"
    6.15 -
    6.16 -case "$command" in
    6.17 -  add)
    6.18 -    for dev in /dev/nd*; do
    6.19 -      if nbd-client $2:$3 $dev; then
    6.20 -        write_dev $dev
    6.21 -        exit 0
    6.22 -      fi
    6.23 -    done
    6.24 -    exit 1
    6.25 -    ;;
    6.26 -  remove)
    6.27 -    nbd-client -d $2
    6.28 -    exit 0
    6.29 -    ;;
    6.30 -esac
     7.1 --- a/tools/examples/block-nbd	Tue Nov 04 11:46:00 2008 +0000
     7.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.3 @@ -1,27 +0,0 @@
     7.4 -#!/bin/bash
     7.5 -
     7.6 -# Usage: block-nbd [bind server ctl_port |unbind node]
     7.7 -#
     7.8 -# The node argument to unbind is the name of the device node we are to
     7.9 -# unbind.
    7.10 -#
    7.11 -# This assumes you're running a correctly configured server at the other end!
    7.12 -
    7.13 -dir=$(dirname "$0")
    7.14 -. "$dir/block-common.sh"
    7.15 -
    7.16 -case "$command" in
    7.17 -  add)
    7.18 -    for dev in /dev/nbd*; do
    7.19 -      if nbd-client $2 $3 $dev; then
    7.20 -        write_dev $dev
    7.21 -        exit 0
    7.22 -      fi
    7.23 -    done
    7.24 -    exit 1
    7.25 -    ;;
    7.26 -  remove)
    7.27 -    nbd-client -d $2
    7.28 -    exit 0
    7.29 -    ;;
    7.30 -esac
     8.1 --- a/tools/examples/external-device-migrate	Tue Nov 04 11:46:00 2008 +0000
     8.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.3 @@ -1,98 +0,0 @@
     8.4 -#!/bin/bash
     8.5 -
     8.6 -# Copyright (c) 2005 IBM Corporation
     8.7 -#
     8.8 -# This library is free software; you can redistribute it and/or
     8.9 -# modify it under the terms of version 2.1 of the GNU Lesser General Public
    8.10 -# License as published by the Free Software Foundation.
    8.11 -#
    8.12 -# This library is distributed in the hope that it will be useful,
    8.13 -# but WITHOUT ANY WARRANTY; without even the implied warranty of
    8.14 -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    8.15 -# Lesser General Public License for more details.
    8.16 -#
    8.17 -# You should have received a copy of the GNU Lesser General Public
    8.18 -# License along with this library; if not, write to the Free Software
    8.19 -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    8.20 -#
    8.21 -
    8.22 -set -x
    8.23 -
    8.24 -# This script is called by XenD for migration of external devices
    8.25 -# It does not handle the migration of those devices itself, but
    8.26 -# passes the requests on to further applications
    8.27 -# It handles the low-level command line parsing and some of the
    8.28 -# synchronization
    8.29 -
    8.30 -dir=$(dirname "$0")
    8.31 -. "$dir/logging.sh"
    8.32 -
    8.33 -
    8.34 -function ext_dev_migrate_usage() {
    8.35 -cat <<EOF
    8.36 -Pass the following command line parameters to the script:
    8.37 -
    8.38 --step <n>              : n-th migration step
    8.39 --host <host>           : the destination host
    8.40 --domname <domain name> : name of the domain that is migrating
    8.41 --type <device type>    : the type of device that is migrating
    8.42 --subtype <dev. subtype>: the subtype of the device
    8.43 --recover               : indicates recovery request; an error
    8.44 -                         occurred during migration
    8.45 --help                  : display this help screen
    8.46 -EOF
    8.47 -}
    8.48 -
    8.49 -# Parse the command line paramters. The following parameters must be
    8.50 -# passed as the first ones in the sequence:
    8.51 -#  -step       [required]
    8.52 -#  -host       [required]
    8.53 -#  -domname    [required]
    8.54 -#  -type       [required]
    8.55 -#  -subtype    [optional]
    8.56 -#  -recover    [optional]
    8.57 -# The remaining ones will be passed to the called function.
    8.58 -function evaluate_params()
    8.59 -{
    8.60 -	local step host domname typ recover filename func stype
    8.61 -	stype=""
    8.62 -	while [ $# -ge 1 ]; do
    8.63 -		case "$1" in
    8.64 -		-step)		step=$2; shift; shift;;
    8.65 -		-host)		host=$2; shift; shift;;
    8.66 -		-domname)	domname=$2; shift; shift;;
    8.67 -		-type)		typ=$2; shift; shift;;
    8.68 -		-subtype)	stype=$2; shift; shift;;
    8.69 -		-recover)	recover=1; shift;;
    8.70 -		-help)		ext_dev_migrate_usage; exit 0;;
    8.71 -		*)		break;;
    8.72 -		esac
    8.73 -	done
    8.74 -
    8.75 -	if [ "$step"    = "" -o \
    8.76 -	     "$host"    = "" -o \
    8.77 -	     "$typ"     = "" -o \
    8.78 -	     "$domname" = "" ]; then
    8.79 -	 	echo "Error: Parameter(s) missing (-step/-host/-type/-domname)" 1>&2
    8.80 -		echo "" 1>&2
    8.81 -		echo "$0 -help for usage." 1>&2
    8.82 -		exit 1
    8.83 -	fi
    8.84 -
    8.85 -	filename="$dir/$typ$stype-migration.sh"
    8.86 -	if [ ! -r $filename ]; then
    8.87 -		echo "Error: Could not find script '$filename'"
    8.88 -		return
    8.89 -	fi
    8.90 -	. "$filename"
    8.91 -
    8.92 -	if [ "$recover" = "1" ]; then
    8.93 -		func="$typ"_recover
    8.94 -		eval $func $host $domname $step $*
    8.95 -	else
    8.96 -		func="$typ"_migration_step
    8.97 -		eval $func $host $domname $step $*
    8.98 -	fi
    8.99 -}
   8.100 -
   8.101 -evaluate_params "$@"
     9.1 --- a/tools/examples/init.d/sysconfig.xendomains	Tue Nov 04 11:46:00 2008 +0000
     9.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.3 @@ -1,137 +0,0 @@
     9.4 -## Path: System/xen
     9.5 -## Description: xen domain start/stop on boot
     9.6 -## Type: string
     9.7 -## Default: 
     9.8 -#
     9.9 -# The xendomains script can send SysRq requests to domains on shutdown.
    9.10 -# If you don't want to MIGRATE, SAVE, or SHUTDOWN, this may be a possibility
    9.11 -# to do a quick and dirty shutdown ("s e i u o") or at least sync the disks
    9.12 -# of the domains ("s").
    9.13 -#
    9.14 -XENDOMAINS_SYSRQ=""
    9.15 -
    9.16 -## Type: integer 
    9.17 -## Default: 100000
    9.18 -#
    9.19 -# If XENDOMAINS_SYSRQ is set, this variable determines how long to wait
    9.20 -# (in microseconds) after each SysRq, so the domain has a chance to react.
    9.21 -# If you want to a quick'n'dirty shutdown via SysRq, you may want to set
    9.22 -# it to a relatively high value (1200000).
    9.23 -#
    9.24 -XENDOMAINS_USLEEP=100000
    9.25 -
    9.26 -## Type: integer
    9.27 -## Default: 5000000
    9.28 -#
    9.29 -# When creating a guest domain, it is sensible to allow a little time for it
    9.30 -# to get started before creating another domain or proceeding through the
    9.31 -# boot process.  Without this, the booting guests will thrash the disk as they
    9.32 -# start up.  This timeout (in microseconds) specifies the delay after guest
    9.33 -# domain creation.
    9.34 -#
    9.35 -XENDOMAINS_CREATE_USLEEP=5000000
    9.36 -
    9.37 -## Type: string
    9.38 -## Default: ""
    9.39 -#
    9.40 -# Set this to a non-empty string if you want to migrate virtual machines
    9.41 -# on shutdown. The string will be passed to the xm migrate DOMID command
    9.42 -# as is: It should contain the target IP address of the physical machine
    9.43 -# to migrate to and optionally parameters like --live. Leave empty if
    9.44 -# you don't want to try virtual machine relocation on shutdown.
    9.45 -# If migration succeeds, neither SAVE nor SHUTDOWN will be executed for
    9.46 -# that domain.
    9.47 -#
    9.48 -XENDOMAINS_MIGRATE=""
    9.49 -
    9.50 -## Type: string
    9.51 -## Default: /var/lib/xen/save
    9.52 -#
    9.53 -# Directory to save running domains to when the system (dom0) is
    9.54 -# shut down. Will also be used to restore domains from if # XENDOMAINS_RESTORE
    9.55 -# is set (see below). Leave empty to disable domain saving on shutdown 
    9.56 -# (e.g. because you rather shut domains down).
    9.57 -# If domain saving does succeed, SHUTDOWN will not be executed.
    9.58 -#
    9.59 -XENDOMAINS_SAVE=/var/lib/xen/save
    9.60 -
    9.61 -## Type: string
    9.62 -## Default: "--halt --wait"
    9.63 -#
    9.64 -# If neither MIGRATE nor SAVE were enabled or if they failed, you can
    9.65 -# try to shut down a domain by sending it a shutdown request. To do this,
    9.66 -# set this to "--halt --wait". Omit the "--wait" flag to avoid waiting
    9.67 -# for the domain to be really down. Leave empty to skip domain shutdown.
    9.68 -#
    9.69 -XENDOMAINS_SHUTDOWN="--halt --wait"
    9.70 -
    9.71 -## Type: string
    9.72 -## Default: "--all --halt --wait"
    9.73 -#
    9.74 -# After we have gone over all virtual machines (resp. all automatically
    9.75 -# started ones, see XENDOMAINS_AUTO_ONLY below) in a loop and sent SysRq,
    9.76 -# migrated, saved and/or shutdown according to the settings above, we
    9.77 -# might want to shutdown the virtual machines that are still running
    9.78 -# for some reason or another. To do this, set this variable to
    9.79 -# "--all --halt --wait", it will be passed to xm shutdown.
    9.80 -# Leave it empty not to do anything special here.
    9.81 -# (Note: This will hit all virtual machines, even if XENDOMAINS_AUTO_ONLY
    9.82 -# is set.)
    9.83 -# 
    9.84 -XENDOMAINS_SHUTDOWN_ALL="--all --halt --wait"
    9.85 -
    9.86 -## Type: boolean
    9.87 -## Default: true
    9.88 -#
    9.89 -# This variable determines whether saved domains from XENDOMAINS_SAVE
    9.90 -# will be restored on system startup. 
    9.91 -#
    9.92 -XENDOMAINS_RESTORE=true
    9.93 -
    9.94 -## Type: string
    9.95 -## Default: /etc/xen/auto
    9.96 -#
    9.97 -# This variable sets the directory where domains configurations
    9.98 -# are stored that should be started on system startup automatically.
    9.99 -# Leave empty if you don't want to start domains automatically
   9.100 -# (or just don't place any xen domain config files in that dir).
   9.101 -# Note that the script tries to be clever if both RESTORE and AUTO are 
   9.102 -# set: It will first restore saved domains and then only start domains
   9.103 -# in AUTO which are not running yet. 
   9.104 -# Note that the name matching is somewhat fuzzy.
   9.105 -#
   9.106 -XENDOMAINS_AUTO=/etc/xen/auto
   9.107 -
   9.108 -## Type: boolean
   9.109 -## Default: false
   9.110 -# 
   9.111 -# If this variable is set to "true", only the domains started via config 
   9.112 -# files in XENDOMAINS_AUTO will be treated according to XENDOMAINS_SYSRQ,
   9.113 -# XENDOMAINS_MIGRATE, XENDOMAINS_SAVE, XENDMAINS_SHUTDOWN; otherwise
   9.114 -# all running domains will be. 
   9.115 -# Note that the name matching is somewhat fuzzy.
   9.116 -# 
   9.117 -XENDOMAINS_AUTO_ONLY=false
   9.118 -
   9.119 -## Type: integer
   9.120 -## Default: 300
   9.121 -#
   9.122 -# On xendomains stop, a number of xm commands (xm migrate, save, shutdown,
   9.123 -# shutdown --all) may be executed. In the worst case, these commands may
   9.124 -# stall forever, which will prevent a successful shutdown of the machine.
   9.125 -# If this variable is non-zero, the script will set up a watchdog timer
   9.126 -# for every of these xm commands and time it out after the number of seconds
   9.127 -# specified by this variable.
   9.128 -# Note that SHUTDOWN_ALL will not be called if no virtual machines or only
   9.129 -# zombies are still running, so you don't need to enable this timeout just
   9.130 -# for the zombie case.
   9.131 -# The setting should be large enough to make sure that migrate/save/shutdown
   9.132 -# can succeed. If you do live migrations, keep in mind that live migration
   9.133 -# of a 1GB machine over Gigabit ethernet may actually take something like
   9.134 -# 100s (assuming that live migration uses 10% of the network # bandwidth).
   9.135 -# Depending on the virtual machine, a shutdown may also require a significant
   9.136 -# amount of time. So better setup this variable to a huge number and hope the
   9.137 -# watchdog never fires.
   9.138 -#
   9.139 -XENDOMAINS_STOP_MAXWAIT=300
   9.140 -
    10.1 --- a/tools/examples/init.d/xend	Tue Nov 04 11:46:00 2008 +0000
    10.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.3 @@ -1,66 +0,0 @@
    10.4 -#!/bin/bash
    10.5 -#
    10.6 -# xend		Script to start and stop the Xen control daemon.
    10.7 -#
    10.8 -# Author:       Keir Fraser <keir.fraser@cl.cam.ac.uk>
    10.9 -#
   10.10 -# chkconfig: 2345 98 01
   10.11 -# description: Starts and stops the Xen control daemon.
   10.12 -### BEGIN INIT INFO
   10.13 -# Provides:          xend
   10.14 -# Required-Start:    $syslog $remote_fs
   10.15 -# Should-Start:
   10.16 -# Required-Stop:     $syslog $remote_fs
   10.17 -# Should-Stop:
   10.18 -# Default-Start:     3 4 5
   10.19 -# Default-Stop:      0 1 2 6
   10.20 -# Default-Enabled:   yes
   10.21 -# Short-Description: Start/stop xend
   10.22 -# Description:       Starts and stops the Xen control daemon.
   10.23 -### END INIT INFO
   10.24 -
   10.25 -if ! grep -q "control_d" /proc/xen/capabilities ; then
   10.26 -	exit 0
   10.27 -fi
   10.28 -
   10.29 -# Wait for Xend to be up
   10.30 -function await_daemons_up
   10.31 -{
   10.32 -	i=1
   10.33 -	rets=10
   10.34 -	xend status
   10.35 -	while [ $? -ne 0 -a $i -lt $rets ]; do
   10.36 -	    sleep 1
   10.37 -	    echo -n .
   10.38 -	    i=$(($i + 1))
   10.39 -	    xend status
   10.40 -	done
   10.41 -}
   10.42 -
   10.43 -case "$1" in
   10.44 -  start)
   10.45 -	xend start
   10.46 -	await_daemons_up
   10.47 -	;;
   10.48 -  stop)
   10.49 -	xend stop
   10.50 -	;;
   10.51 -  status)
   10.52 -	xend status
   10.53 -	;;
   10.54 -  reload)
   10.55 -        xend reload
   10.56 -        ;;
   10.57 -  restart|force-reload)
   10.58 -	xend restart
   10.59 -	await_daemons_up
   10.60 -	;;
   10.61 -  *)
   10.62 -	# do not advertise unreasonable commands that there is no reason
   10.63 -	# to use with this device
   10.64 -	echo $"Usage: $0 {start|stop|status|restart|reload|force-reload}"
   10.65 -	exit 1
   10.66 -esac
   10.67 -
   10.68 -exit $?
   10.69 -
    11.1 --- a/tools/examples/init.d/xendomains	Tue Nov 04 11:46:00 2008 +0000
    11.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.3 @@ -1,531 +0,0 @@
    11.4 -#!/bin/bash
    11.5 -#
    11.6 -# /etc/init.d/xendomains
    11.7 -# Start / stop domains automatically when domain 0 boots / shuts down.
    11.8 -#
    11.9 -# chkconfig: 345 99 00
   11.10 -# description: Start / stop Xen domains.
   11.11 -#
   11.12 -# This script offers fairly basic functionality.  It should work on Redhat
   11.13 -# but also on LSB-compliant SuSE releases and on Debian with the LSB package
   11.14 -# installed.  (LSB is the Linux Standard Base)
   11.15 -#
   11.16 -# Based on the example in the "Designing High Quality Integrated Linux
   11.17 -# Applications HOWTO" by Avi Alkalay
   11.18 -# <http://www.tldp.org/HOWTO/HighQuality-Apps-HOWTO/>
   11.19 -#
   11.20 -### BEGIN INIT INFO
   11.21 -# Provides:          xendomains
   11.22 -# Required-Start:    $syslog $remote_fs xend
   11.23 -# Should-Start:
   11.24 -# Required-Stop:     $syslog $remote_fs xend
   11.25 -# Should-Stop:
   11.26 -# Default-Start:     3 4 5
   11.27 -# Default-Stop:      0 1 2 6
   11.28 -# Default-Enabled:   yes
   11.29 -# Short-Description: Start/stop secondary xen domains
   11.30 -# Description:       Start / stop domains automatically when domain 0 
   11.31 -#                    boots / shuts down.
   11.32 -### END INIT INFO
   11.33 -
   11.34 -# Correct exit code would probably be 5, but it's enough 
   11.35 -# if xend complains if we're not running as privileged domain
   11.36 -if ! [ -e /proc/xen/privcmd ]; then
   11.37 -	exit 0
   11.38 -fi
   11.39 -
   11.40 -LOCKFILE=/var/lock/subsys/xendomains
   11.41 -XENDOM_CONFIG=/etc/sysconfig/xendomains
   11.42 -
   11.43 -test -r $XENDOM_CONFIG || { echo "$XENDOM_CONFIG not existing";
   11.44 -	if [ "$1" = "stop" ]; then exit 0;
   11.45 -	else exit 6; fi; }
   11.46 -
   11.47 -. $XENDOM_CONFIG
   11.48 -
   11.49 -# Use the SUSE rc_ init script functions;
   11.50 -# emulate them on LSB, RH and other systems
   11.51 -if test -e /etc/rc.status; then
   11.52 -    # SUSE rc script library
   11.53 -    . /etc/rc.status
   11.54 -else    
   11.55 -    _cmd=$1
   11.56 -    declare -a _SMSG
   11.57 -    if test "${_cmd}" = "status"; then
   11.58 -	_SMSG=(running dead dead unused unknown)
   11.59 -	_RC_UNUSED=3
   11.60 -    else
   11.61 -	_SMSG=(done failed failed missed failed skipped unused failed failed)
   11.62 -	_RC_UNUSED=6
   11.63 -    fi
   11.64 -    if test -e /etc/init.d/functions; then
   11.65 -	# REDHAT
   11.66 -	. /etc/init.d/functions
   11.67 -	echo_rc()
   11.68 -	{
   11.69 -	    #echo -n "  [${_SMSG[${_RC_RV}]}] "
   11.70 -	    if test ${_RC_RV} = 0; then
   11.71 -		success "  [${_SMSG[${_RC_RV}]}] "
   11.72 -	    else
   11.73 -		failure "  [${_SMSG[${_RC_RV}]}] "
   11.74 -	    fi
   11.75 -	}
   11.76 -    elif test -e /lib/lsb/init-functions; then
   11.77 -	# LSB    
   11.78 -    	. /lib/lsb/init-functions
   11.79 -        if alias log_success_msg >/dev/null 2>/dev/null; then
   11.80 -	  echo_rc()
   11.81 -	  {
   11.82 -	       echo "  [${_SMSG[${_RC_RV}]}] "
   11.83 -	  }
   11.84 -        else
   11.85 -	  echo_rc()
   11.86 -	  {
   11.87 -	    if test ${_RC_RV} = 0; then
   11.88 -		log_success_msg "  [${_SMSG[${_RC_RV}]}] "
   11.89 -	    else
   11.90 -		log_failure_msg "  [${_SMSG[${_RC_RV}]}] "
   11.91 -	    fi
   11.92 -	  }
   11.93 -        fi
   11.94 -    else    
   11.95 -	# emulate it
   11.96 -	echo_rc()
   11.97 -	{
   11.98 -	    echo "  [${_SMSG[${_RC_RV}]}] "
   11.99 -	}
  11.100 -    fi
  11.101 -    rc_reset() { _RC_RV=0; }
  11.102 -    rc_failed()
  11.103 -    {
  11.104 -	if test -z "$1"; then 
  11.105 -	    _RC_RV=1;
  11.106 -	elif test "$1" != "0"; then 
  11.107 -	    _RC_RV=$1; 
  11.108 -    	fi
  11.109 -	return ${_RC_RV}
  11.110 -    }
  11.111 -    rc_check()
  11.112 -    {
  11.113 -	return rc_failed $?
  11.114 -    }	
  11.115 -    rc_status()
  11.116 -    {
  11.117 -	rc_failed $?
  11.118 -	if test "$1" = "-r"; then _RC_RV=0; shift; fi
  11.119 -	if test "$1" = "-s"; then rc_failed 5; echo_rc; rc_failed 3; shift; fi
  11.120 -	if test "$1" = "-u"; then rc_failed ${_RC_UNUSED}; echo_rc; rc_failed 3; shift; fi
  11.121 -	if test "$1" = "-v"; then echo_rc; shift; fi
  11.122 -	if test "$1" = "-r"; then _RC_RV=0; shift; fi
  11.123 -	return ${_RC_RV}
  11.124 -    }
  11.125 -    rc_exit() { exit ${_RC_RV}; }
  11.126 -    rc_active() 
  11.127 -    {
  11.128 -	if test -z "$RUNLEVEL"; then read RUNLEVEL REST < <(/sbin/runlevel); fi
  11.129 -	if test -e /etc/init.d/S[0-9][0-9]${1}; then return 0; fi
  11.130 -	return 1
  11.131 -    }
  11.132 -fi
  11.133 -
  11.134 -if ! which usleep >&/dev/null
  11.135 -then
  11.136 -  usleep()
  11.137 -  {
  11.138 -    if [ -n "$1" ]
  11.139 -    then
  11.140 -      sleep $(( $1 / 1000000 ))
  11.141 -    fi
  11.142 -  }
  11.143 -fi
  11.144 -
  11.145 -# Reset status of this service
  11.146 -rc_reset
  11.147 -
  11.148 -##
  11.149 -# Returns 0 (success) if the given parameter names a directory, and that
  11.150 -# directory is not empty.
  11.151 -#
  11.152 -contains_something()
  11.153 -{
  11.154 -  if [ -d "$1" ] && [ `/bin/ls $1 | wc -l` -gt 0 ]
  11.155 -  then
  11.156 -    return 0
  11.157 -  else
  11.158 -    return 1
  11.159 -  fi
  11.160 -}
  11.161 -
  11.162 -# read name from xen config file
  11.163 -rdname()
  11.164 -{
  11.165 -    NM=$(xm create --quiet --dryrun --defconfig "$1" |
  11.166 -         sed -n 's/^.*(name \(.*\))$/\1/p')
  11.167 -}
  11.168 -
  11.169 -rdnames()
  11.170 -{
  11.171 -    NAMES=
  11.172 -    if ! contains_something "$XENDOMAINS_AUTO"
  11.173 -    then 
  11.174 -	return
  11.175 -    fi
  11.176 -    for dom in $XENDOMAINS_AUTO/*; do
  11.177 -	rdname $dom
  11.178 -	if test -z $NAMES; then 
  11.179 -	    NAMES=$NM; 
  11.180 -	else
  11.181 -	    NAMES="$NAMES|$NM"
  11.182 -	fi
  11.183 -    done
  11.184 -}
  11.185 -
  11.186 -parseln()
  11.187 -{
  11.188 -    if [[ "$1" =~ "\(domain" ]]; then
  11.189 -        name=;id=
  11.190 -    else if [[ "$1" =~ "\(name" ]]; then
  11.191 -        name=$(echo $1 | sed -e 's/^.*(name \(.*\))$/\1/')
  11.192 -    else if [[ "$1" =~ "\(domid" ]]; then
  11.193 -        id=$(echo $1 | sed -e 's/^.*(domid \(.*\))$/\1/')
  11.194 -    fi; fi; fi
  11.195 -
  11.196 -    [ -n "$name" -a -n "$id" ] && return 0 || return 1
  11.197 -}
  11.198 -
  11.199 -is_running()
  11.200 -{
  11.201 -    rdname $1
  11.202 -    RC=1
  11.203 -    name=;id=
  11.204 -    while read LN; do
  11.205 -	parseln "$LN" || continue
  11.206 -	if test $id = 0; then continue; fi
  11.207 -	case $name in 
  11.208 -	    ($NM)
  11.209 -		RC=0
  11.210 -		;;
  11.211 -	esac
  11.212 -    done < <(xm list -l | grep '(\(domain\|domid\|name\)')
  11.213 -    return $RC
  11.214 -}
  11.215 -
  11.216 -start() 
  11.217 -{
  11.218 -    if [ -f $LOCKFILE ]; then 
  11.219 -	echo -n "xendomains already running (lockfile exists)"
  11.220 -	return; 
  11.221 -    fi
  11.222 -
  11.223 -    saved_domains=" "
  11.224 -    if [ "$XENDOMAINS_RESTORE" = "true" ] &&
  11.225 -       contains_something "$XENDOMAINS_SAVE"
  11.226 -    then
  11.227 -	mkdir -p $(dirname "$LOCKFILE")
  11.228 -	touch $LOCKFILE
  11.229 -	echo -n "Restoring Xen domains:"
  11.230 -	saved_domains=`ls $XENDOMAINS_SAVE`
  11.231 -        for dom in $XENDOMAINS_SAVE/*; do
  11.232 -            if [ -f $dom ] ; then
  11.233 -                HEADER=`head -c 16 $dom | head -n 1 2> /dev/null`
  11.234 -                if [ $HEADER = "LinuxGuestRecord" ]; then
  11.235 -                    echo -n " ${dom##*/}"
  11.236 -                    xm restore $dom
  11.237 -                    if [ $? -ne 0 ]; then
  11.238 -                        rc_failed $?
  11.239 -                        echo -n '!'
  11.240 -                    else
  11.241 -                        # mv $dom ${dom%/*}/.${dom##*/}
  11.242 -                        rm $dom
  11.243 -                    fi
  11.244 -                fi
  11.245 -            fi
  11.246 -        done
  11.247 -	echo .
  11.248 -    fi
  11.249 -
  11.250 -    if contains_something "$XENDOMAINS_AUTO"
  11.251 -    then
  11.252 -	touch $LOCKFILE
  11.253 -	echo -n "Starting auto Xen domains:"
  11.254 -	# We expect config scripts for auto starting domains to be in
  11.255 -	# XENDOMAINS_AUTO - they could just be symlinks to files elsewhere
  11.256 -
  11.257 -	# Create all domains with config files in XENDOMAINS_AUTO.
  11.258 -	# TODO: We should record which domain name belongs 
  11.259 -	# so we have the option to selectively shut down / migrate later
  11.260 -	# If a domain statefile from $XENDOMAINS_SAVE matches a domain name
  11.261 -	# in $XENDOMAINS_AUTO, do not try to start that domain; if it didn't 
  11.262 -	# restore correctly it requires administrative attention.
  11.263 -	for dom in $XENDOMAINS_AUTO/*; do
  11.264 -	    echo -n " ${dom##*/}"
  11.265 -	    shortdom=$(echo $dom | sed -n 's/^.*\/\(.*\)$/\1/p')
  11.266 -	    echo $saved_domains | grep -w $shortdom > /dev/null
  11.267 -	    if [ $? -eq 0 ] || is_running $dom; then
  11.268 -		echo -n "(skip)"
  11.269 -	    else
  11.270 -		xm create --quiet --defconfig $dom
  11.271 -		if [ $? -ne 0 ]; then
  11.272 -		    rc_failed $?
  11.273 -		    echo -n '!'
  11.274 -		else
  11.275 -		    usleep $XENDOMAINS_CREATE_USLEEP
  11.276 -		fi
  11.277 -	    fi
  11.278 -	done
  11.279 -    fi	
  11.280 -}
  11.281 -
  11.282 -all_zombies()
  11.283 -{
  11.284 -    name=;id=
  11.285 -    while read LN; do
  11.286 -	parseln "$LN" || continue
  11.287 -	if test $id = 0; then continue; fi
  11.288 -	if test "$state" != "-b---d" -a "$state" != "-----d"; then
  11.289 -	    return 1;
  11.290 -	fi
  11.291 -    done < <(xm list -l | grep '(\(domain\|domid\|name\)')
  11.292 -    return 0
  11.293 -}
  11.294 -
  11.295 -# Wait for max $XENDOMAINS_STOP_MAXWAIT for xm $1 to finish;
  11.296 -# if it has not exited by that time kill it, so the init script will
  11.297 -# succeed within a finite amount of time; if $2 is nonnull, it will
  11.298 -# kill the command as well as soon as no domain (except for zombies)
  11.299 -# are left (used for shutdown --all).
  11.300 -watchdog_xm()
  11.301 -{
  11.302 -    if test -z "$XENDOMAINS_STOP_MAXWAIT" -o "$XENDOMAINS_STOP_MAXWAIT" = "0"; then
  11.303 -	exit
  11.304 -    fi
  11.305 -    usleep 20000
  11.306 -    for no in `seq 0 $XENDOMAINS_STOP_MAXWAIT`; do
  11.307 -	# exit if xm save/migrate/shutdown is finished
  11.308 -	PSAX=`ps axlw | grep "xm $1" | grep -v grep`
  11.309 -	if test -z "$PSAX"; then exit; fi
  11.310 -	echo -n "."; sleep 1
  11.311 -	# go to kill immediately if there's only zombies left
  11.312 -	if all_zombies && test -n "$2"; then break; fi
  11.313 -    done
  11.314 -    sleep 1
  11.315 -    read PSF PSUID PSPID PSPPID < <(echo "$PSAX")
  11.316 -    # kill xm $1
  11.317 -    kill $PSPID >/dev/null 2>&1
  11.318 -}
  11.319 -
  11.320 -stop()
  11.321 -{
  11.322 -    # Collect list of domains to shut down
  11.323 -    if test "$XENDOMAINS_AUTO_ONLY" = "true"; then
  11.324 -	rdnames
  11.325 -    fi
  11.326 -    echo -n "Shutting down Xen domains:"
  11.327 -    name=;id=
  11.328 -    while read LN; do
  11.329 -	parseln "$LN" || continue
  11.330 -	if test $id = 0; then continue; fi
  11.331 -	echo -n " $name"
  11.332 -	if test "$XENDOMAINS_AUTO_ONLY" = "true"; then
  11.333 -	    eval "
  11.334 -	    case \"\$name\" in
  11.335 -		($NAMES)
  11.336 -		    # nothing
  11.337 -		    ;;
  11.338 -		(*)
  11.339 -		    echo -n '(skip)'
  11.340 -		    continue
  11.341 -		    ;;
  11.342 -	    esac
  11.343 -	    "
  11.344 -	fi
  11.345 -	# XENDOMAINS_SYSRQ chould be something like just "s" 
  11.346 -	# or "s e i u" or even "s e s i u o"
  11.347 -	# for the latter, you should set XENDOMAINS_USLEEP to 1200000 or so
  11.348 -	if test -n "$XENDOMAINS_SYSRQ"; then
  11.349 -	    for sysrq in $XENDOMAINS_SYSRQ; do
  11.350 -		echo -n "(SR-$sysrq)"
  11.351 -		xm sysrq $id $sysrq
  11.352 -		if test $? -ne 0; then
  11.353 -		    rc_failed $?
  11.354 -		    echo -n '!'
  11.355 -		fi
  11.356 -		# usleep just ignores empty arg
  11.357 -		usleep $XENDOMAINS_USLEEP
  11.358 -	    done
  11.359 -	fi
  11.360 -	if test "$state" = "-b---d" -o "$state" = "-----d"; then
  11.361 -	    echo -n "(zomb)"
  11.362 -	    continue
  11.363 -	fi
  11.364 -	if test -n "$XENDOMAINS_MIGRATE"; then
  11.365 -	    echo -n "(migr)"
  11.366 -	    watchdog_xm migrate &
  11.367 -	    WDOG_PID=$!
  11.368 -	    xm migrate $id $XENDOMAINS_MIGRATE
  11.369 -	    if test $? -ne 0; then
  11.370 -		rc_failed $?
  11.371 -		echo -n '!'
  11.372 -		kill $WDOG_PID >/dev/null 2>&1
  11.373 -	    else
  11.374 -		kill $WDOG_PID >/dev/null 2>&1
  11.375 -		continue
  11.376 -	    fi
  11.377 -	fi
  11.378 -	if test -n "$XENDOMAINS_SAVE"; then
  11.379 -	    echo -n "(save)"
  11.380 -	    watchdog_xm save &
  11.381 -	    WDOG_PID=$!
  11.382 -	    mkdir -p "$XENDOMAINS_SAVE"
  11.383 -	    xm save $id $XENDOMAINS_SAVE/$name
  11.384 -	    if test $? -ne 0; then
  11.385 -		rc_failed $?
  11.386 -		echo -n '!'
  11.387 -		kill $WDOG_PID >/dev/null 2>&1
  11.388 -	    else
  11.389 -		kill $WDOG_PID >/dev/null 2>&1
  11.390 -		continue
  11.391 -	    fi
  11.392 -	fi
  11.393 -	if test -n "$XENDOMAINS_SHUTDOWN"; then
  11.394 -	    # XENDOMAINS_SHUTDOWN should be "--halt --wait"
  11.395 -	    echo -n "(shut)"
  11.396 -	    watchdog_xm shutdown &
  11.397 -	    WDOG_PID=$!
  11.398 -	    xm shutdown $id $XENDOMAINS_SHUTDOWN
  11.399 -	    if test $? -ne 0; then
  11.400 -		rc_failed $?
  11.401 -		echo -n '!'
  11.402 -	    fi
  11.403 -	    kill $WDOG_PID >/dev/null 2>&1
  11.404 -	fi
  11.405 -    done < <(xm list -l | grep '(\(domain\|domid\|name\)')
  11.406 -
  11.407 -    # NB. this shuts down ALL Xen domains (politely), not just the ones in
  11.408 -    # AUTODIR/*
  11.409 -    # This is because it's easier to do ;-) but arguably if this script is run
  11.410 -    # on system shutdown then it's also the right thing to do.
  11.411 -    if ! all_zombies && test -n "$XENDOMAINS_SHUTDOWN_ALL"; then
  11.412 -	# XENDOMAINS_SHUTDOWN_ALL should be "--all --halt --wait"
  11.413 -	echo -n " SHUTDOWN_ALL "
  11.414 -	watchdog_xm shutdown 1 &
  11.415 -	WDOG_PID=$!
  11.416 -	xm shutdown $XENDOMAINS_SHUTDOWN_ALL
  11.417 -	if test $? -ne 0; then
  11.418 -	    rc_failed $?
  11.419 -	    echo -n '!'
  11.420 -	fi
  11.421 -	kill $WDOG_PID >/dev/null 2>&1
  11.422 -    fi
  11.423 -
  11.424 -    # Unconditionally delete lock file
  11.425 -    rm -f $LOCKFILE
  11.426 -}
  11.427 -
  11.428 -check_domain_up()
  11.429 -{
  11.430 -    name=;id=
  11.431 -    while read LN; do
  11.432 -	parseln "$LN" || continue
  11.433 -	if test $id = 0; then continue; fi
  11.434 -	case $name in 
  11.435 -	    ($1)
  11.436 -		return 0
  11.437 -		;;
  11.438 -	esac
  11.439 -    done < <(xm list -l | grep '(\(domain\|domid\|name\)')
  11.440 -    return 1
  11.441 -}
  11.442 -
  11.443 -check_all_auto_domains_up()
  11.444 -{
  11.445 -    if ! contains_something "$XENDOMAINS_AUTO"
  11.446 -    then
  11.447 -      return 0
  11.448 -    fi
  11.449 -    missing=
  11.450 -    for nm in $XENDOMAINS_AUTO/*; do
  11.451 -	rdname $nm
  11.452 -	found=0
  11.453 -	if check_domain_up "$NM"; then 
  11.454 -	    echo -n " $name"
  11.455 -	else 
  11.456 -	    missing="$missing $NM"
  11.457 -	fi
  11.458 -    done
  11.459 -    if test -n "$missing"; then
  11.460 -	echo -n " MISS AUTO:$missing"
  11.461 -	return 1
  11.462 -    fi
  11.463 -    return 0
  11.464 -}
  11.465 -
  11.466 -check_all_saved_domains_up()
  11.467 -{
  11.468 -    if ! contains_something "$XENDOMAINS_SAVE" 
  11.469 -    then
  11.470 -      return 0
  11.471 -    fi
  11.472 -    missing=`/bin/ls $XENDOMAINS_SAVE`
  11.473 -    echo -n " MISS SAVED: " $missing
  11.474 -    return 1
  11.475 -}
  11.476 -
  11.477 -# This does NOT necessarily restart all running domains: instead it
  11.478 -# stops all running domains and then boots all the domains specified in
  11.479 -# AUTODIR.  If other domains have been started manually then they will
  11.480 -# not get restarted.
  11.481 -# Commented out to avoid confusion!
  11.482 -
  11.483 -restart()
  11.484 -{
  11.485 -    stop
  11.486 -    start
  11.487 -}
  11.488 -
  11.489 -reload()
  11.490 -{
  11.491 -    restart
  11.492 -}
  11.493 -
  11.494 -
  11.495 -case "$1" in
  11.496 -    start)
  11.497 -	start
  11.498 -	rc_status
  11.499 -	if test -f $LOCKFILE; then rc_status -v; fi
  11.500 -	;;
  11.501 -
  11.502 -    stop)
  11.503 -	stop
  11.504 -	rc_status -v
  11.505 -	;;
  11.506 -
  11.507 -    restart)
  11.508 -	restart
  11.509 -	;;
  11.510 -    reload)
  11.511 -	reload
  11.512 -	;;
  11.513 -
  11.514 -    status)
  11.515 -	echo -n "Checking for xendomains:" 
  11.516 -	if test ! -f $LOCKFILE; then 
  11.517 -	    rc_failed 3
  11.518 -	else
  11.519 -	    check_all_auto_domains_up
  11.520 -	    rc_status
  11.521 -	    check_all_saved_domains_up
  11.522 -	    rc_status
  11.523 -	fi
  11.524 -	rc_status -v
  11.525 -	;;
  11.526 -
  11.527 -    *)
  11.528 -	echo "Usage: $0 {start|stop|restart|reload|status}"
  11.529 -	rc_failed 3
  11.530 -	rc_status -v
  11.531 -	;;
  11.532 -esac
  11.533 -
  11.534 -rc_exit
    12.1 --- a/tools/examples/locking.sh	Tue Nov 04 11:46:00 2008 +0000
    12.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.3 @@ -1,98 +0,0 @@
    12.4 -#
    12.5 -# Copyright (c) 2005 XenSource Ltd.
    12.6 -#
    12.7 -# This library is free software; you can redistribute it and/or
    12.8 -# modify it under the terms of version 2.1 of the GNU Lesser General Public
    12.9 -# License as published by the Free Software Foundation.
   12.10 -#
   12.11 -# This library is distributed in the hope that it will be useful,
   12.12 -# but WITHOUT ANY WARRANTY; without even the implied warranty of
   12.13 -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   12.14 -# Lesser General Public License for more details.
   12.15 -#
   12.16 -# You should have received a copy of the GNU Lesser General Public
   12.17 -# License along with this library; if not, write to the Free Software
   12.18 -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   12.19 -#
   12.20 -
   12.21 -#
   12.22 -# Serialisation
   12.23 -#
   12.24 -
   12.25 -LOCK_SLEEPTIME=1
   12.26 -LOCK_SPINNING_RETRIES=5
   12.27 -LOCK_RETRIES=100
   12.28 -LOCK_BASEDIR=/var/run/xen-hotplug
   12.29 -
   12.30 -
   12.31 -claim_lock()
   12.32 -{
   12.33 -  local lockdir="$LOCK_BASEDIR/$1"
   12.34 -  mkdir -p "$LOCK_BASEDIR"
   12.35 -  _claim_lock "$lockdir"
   12.36 -}
   12.37 -
   12.38 -
   12.39 -release_lock()
   12.40 -{
   12.41 -  _release_lock "$LOCK_BASEDIR/$1"
   12.42 -}
   12.43 -
   12.44 -
   12.45 -_claim_lock()
   12.46 -{
   12.47 -  local lockdir="$1"
   12.48 -  local owner=$(_lock_owner "$lockdir")
   12.49 -  local retries=0
   12.50 -
   12.51 -  while [ $retries -lt $LOCK_RETRIES ]
   12.52 -  do
   12.53 -    mkdir "$lockdir" 2>/dev/null && trap "release_lock $1; sigerr" ERR &&
   12.54 -      _update_lock_info "$lockdir" && return
   12.55 -
   12.56 -    local new_owner=$(_lock_owner "$lockdir")
   12.57 -    if [ "$new_owner" != "$owner" ]
   12.58 -    then
   12.59 -      owner="$new_owner"
   12.60 -      retries=0
   12.61 -    fi
   12.62 -
   12.63 -    if [ $retries -gt $LOCK_SPINNING_RETRIES ]
   12.64 -    then
   12.65 -      sleep $LOCK_SLEEPTIME
   12.66 -    else
   12.67 -      sleep 0
   12.68 -    fi
   12.69 -    retries=$(($retries + 1))
   12.70 -  done
   12.71 -  _steal_lock "$lockdir"
   12.72 -}
   12.73 -
   12.74 -
   12.75 -_release_lock()
   12.76 -{
   12.77 -  trap sigerr ERR
   12.78 -  rm -rf "$1" 2>/dev/null || true
   12.79 -}
   12.80 -
   12.81 -
   12.82 -_steal_lock()
   12.83 -{
   12.84 -  local lockdir="$1"
   12.85 -  local owner=$(cat "$lockdir/owner" 2>/dev/null || echo "unknown")
   12.86 -  log err "Forced to steal lock on $lockdir from $owner!"
   12.87 -  _release_lock "$lockdir"
   12.88 -  _claim_lock "$lockdir"
   12.89 -}
   12.90 -
   12.91 -
   12.92 -_lock_owner()
   12.93 -{
   12.94 -  cat "$1/owner" 2>/dev/null || echo "unknown"
   12.95 -}
   12.96 -
   12.97 -
   12.98 -_update_lock_info()
   12.99 -{
  12.100 -  echo "$$: $0" >"$1/owner"
  12.101 -}
    13.1 --- a/tools/examples/logging.sh	Tue Nov 04 11:46:00 2008 +0000
    13.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.3 @@ -1,22 +0,0 @@
    13.4 -#
    13.5 -# Copyright (c) 2005 XenSource Ltd.
    13.6 -#
    13.7 -# This library is free software; you can redistribute it and/or
    13.8 -# modify it under the terms of version 2.1 of the GNU Lesser General Public
    13.9 -# License as published by the Free Software Foundation.
   13.10 -#
   13.11 -# This library is distributed in the hope that it will be useful,
   13.12 -# but WITHOUT ANY WARRANTY; without even the implied warranty of
   13.13 -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   13.14 -# Lesser General Public License for more details.
   13.15 -#
   13.16 -# You should have received a copy of the GNU Lesser General Public
   13.17 -# License along with this library; if not, write to the Free Software
   13.18 -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   13.19 -#
   13.20 -
   13.21 -log() {
   13.22 -  local level="$1"
   13.23 -  shift
   13.24 -  logger -p "daemon.$level" -- "$0:" "$@" || echo "$0 $@" >&2
   13.25 -}
    14.1 --- a/tools/examples/network-bridge	Tue Nov 04 11:46:00 2008 +0000
    14.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.3 @@ -1,310 +0,0 @@
    14.4 -#!/bin/bash
    14.5 -#============================================================================
    14.6 -# Default Xen network start/stop script.
    14.7 -# Xend calls a network script when it starts.
    14.8 -# The script name to use is defined in /etc/xen/xend-config.sxp
    14.9 -# in the network-script field.
   14.10 -#
   14.11 -# This script creates a bridge (default ${netdev}), adds a device
   14.12 -# (defaults to the device on the default gateway route) to it, copies
   14.13 -# the IP addresses from the device to the bridge and adjusts the routes
   14.14 -# accordingly.
   14.15 -#
   14.16 -# If all goes well, this should ensure that networking stays up.
   14.17 -# However, some configurations are upset by this, especially
   14.18 -# NFS roots. If the bridged setup does not meet your needs,
   14.19 -# configure a different script, for example using routing instead.
   14.20 -#
   14.21 -# Usage:
   14.22 -#
   14.23 -# network-bridge (start|stop|status) {VAR=VAL}*
   14.24 -#
   14.25 -# Vars:
   14.26 -#
   14.27 -# bridge     The bridge to use (default ${netdev}).
   14.28 -# netdev     The interface to add to the bridge (default gateway device).
   14.29 -# antispoof  Whether to use iptables to prevent spoofing (default no).
   14.30 -#
   14.31 -# Internal Vars:
   14.32 -# pdev="p${netdev}"
   14.33 -# tdev=tmpbridge
   14.34 -#
   14.35 -# start:
   14.36 -# Creates the bridge as tdev
   14.37 -# Copies the IP and MAC addresses from pdev to bridge
   14.38 -# Renames netdev to be pdev 
   14.39 -# Renames tdev to bridge
   14.40 -# Enslaves pdev to bridge
   14.41 -#
   14.42 -# stop:
   14.43 -# Removes pdev from the bridge
   14.44 -# Transfers addresses, routes from bridge to pdev
   14.45 -# Renames bridge to tdev
   14.46 -# Renames pdev to netdev 
   14.47 -# Deletes tdev
   14.48 -#
   14.49 -# status:
   14.50 -# Print addresses, interfaces, routes
   14.51 -#
   14.52 -#============================================================================
   14.53 -
   14.54 -
   14.55 -dir=$(dirname "$0")
   14.56 -. "$dir/xen-script-common.sh"
   14.57 -. "$dir/xen-network-common.sh"
   14.58 -
   14.59 -findCommand "$@"
   14.60 -evalVariables "$@"
   14.61 -
   14.62 -is_network_root () {
   14.63 -    local rootfs=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $3; }}' /etc/mtab)
   14.64 -    local rootopts=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $4; }}' /etc/mtab)
   14.65 -
   14.66 -    [[ "$rootfs" =~ "^nfs" ]] || [[ "$rootopts" =~ "_netdev" ]] && has_nfsroot=1 || has_nfsroot=0
   14.67 -    if [ $has_nfsroot -eq 1 ]; then
   14.68 -        local bparms=$(cat /proc/cmdline)
   14.69 -        for p in $bparms; do
   14.70 -            local ipaddr=$(echo $p | awk /nfsroot=/'{ print substr($1,9,index($1,":")-9) }')
   14.71 -            if [ "$ipaddr" != "" ]; then
   14.72 -                local nfsdev=$(ip route get $ipaddr | awk /$ipaddr/'{ print $3 }')
   14.73 -                [[ "$nfsdev" == "$netdev" ]] && return 0 || return 1
   14.74 -            fi
   14.75 -        done
   14.76 -    fi
   14.77 -    return 1
   14.78 -}
   14.79 -
   14.80 -find_alt_device () {
   14.81 -    local interf=$1
   14.82 -    local prefix=${interf%[[:digit:]]}
   14.83 -    local ifs=$(ip link show | grep " $prefix" |\
   14.84 -                gawk '{ printf ("%s",substr($2,1,length($2)-1)) }' |\
   14.85 -                sed s/$interf//)
   14.86 -    echo "$ifs"
   14.87 -}
   14.88 -
   14.89 -netdev=${netdev:-$(ip route list 0.0.0.0/0  | \
   14.90 -                   sed 's/.*dev \([a-z]\+[0-9]\+\).*$/\1/')}
   14.91 -if is_network_root ; then
   14.92 -    altdevs=$(find_alt_device $netdev)
   14.93 -    for netdev in $altdevs; do break; done
   14.94 -    if [ -z "$netdev" ]; then
   14.95 -        [ -x /usr/bin/logger ] && /usr/bin/logger "network-bridge: bridging not supported on network root; not starting"
   14.96 -        exit
   14.97 -    fi
   14.98 -fi
   14.99 -netdev=${netdev:-eth0}
  14.100 -bridge=${bridge:-${netdev}}
  14.101 -antispoof=${antispoof:-no}
  14.102 -
  14.103 -pdev="p${netdev}"
  14.104 -tdev=tmpbridge
  14.105 -
  14.106 -get_ip_info() {
  14.107 -    addr_pfx=`ip addr show dev $1 | egrep '^ *inet' | sed -e 's/ *inet //' -e 's/ .*//'`
  14.108 -    gateway=`ip route show dev $1 | fgrep default | sed 's/default via //'`
  14.109 -}
  14.110 -    
  14.111 -do_ifup() {
  14.112 -    if ! ifup $1 ; then
  14.113 -        if [ -n "$addr_pfx" ] ; then
  14.114 -            # use the info from get_ip_info()
  14.115 -            ip addr flush $1
  14.116 -            ip addr add ${addr_pfx} dev $1
  14.117 -            ip link set dev $1 up
  14.118 -            [ -n "$gateway" ] && ip route add default via ${gateway}
  14.119 -        fi
  14.120 -    fi
  14.121 -}
  14.122 -
  14.123 -# Usage: transfer_addrs src dst
  14.124 -# Copy all IP addresses (including aliases) from device $src to device $dst.
  14.125 -transfer_addrs () {
  14.126 -    local src=$1
  14.127 -    local dst=$2
  14.128 -    # Don't bother if $dst already has IP addresses.
  14.129 -    if ip addr show dev ${dst} | egrep -q '^ *inet ' ; then
  14.130 -        return
  14.131 -    fi
  14.132 -    # Address lines start with 'inet' and have the device in them.
  14.133 -    # Replace 'inet' with 'ip addr add' and change the device name $src
  14.134 -    # to 'dev $src'.
  14.135 -    ip addr show dev ${src} | egrep '^ *inet ' | sed -e "
  14.136 -s/inet/ip addr add/
  14.137 -s@\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+/[0-9]\+\)@\1@
  14.138 -s/${src}/dev ${dst} label ${dst}/
  14.139 -s/secondary//
  14.140 -" | sh -e
  14.141 -    # Remove automatic routes on destination device
  14.142 -    ip route list | sed -ne "
  14.143 -/dev ${dst}\( \|$\)/ {
  14.144 -  s/^/ip route del /
  14.145 -  p
  14.146 -}" | sh -e
  14.147 -}
  14.148 -
  14.149 -# Usage: transfer_routes src dst
  14.150 -# Get all IP routes to device $src, delete them, and
  14.151 -# add the same routes to device $dst.
  14.152 -# The original routes have to be deleted, otherwise adding them
  14.153 -# for $dst fails (duplicate routes).
  14.154 -transfer_routes () {
  14.155 -    local src=$1
  14.156 -    local dst=$2
  14.157 -    # List all routes and grep the ones with $src in.
  14.158 -    # Stick 'ip route del' on the front to delete.
  14.159 -    # Change $src to $dst and use 'ip route add' to add.
  14.160 -    ip route list | sed -ne "
  14.161 -/dev ${src}\( \|$\)/ {
  14.162 -  h
  14.163 -  s/^/ip route del /
  14.164 -  P
  14.165 -  g
  14.166 -  s/${src}/${dst}/
  14.167 -  s/^/ip route add /
  14.168 -  P
  14.169 -  d
  14.170 -}" | sh -e
  14.171 -}
  14.172 -
  14.173 -
  14.174 -##
  14.175 -# link_exists interface
  14.176 -#
  14.177 -# Returns 0 if the interface named exists (whether up or down), 1 otherwise.
  14.178 -#
  14.179 -link_exists()
  14.180 -{
  14.181 -    if ip link show "$1" >/dev/null 2>/dev/null
  14.182 -    then
  14.183 -        return 0
  14.184 -    else
  14.185 -        return 1
  14.186 -    fi
  14.187 -}
  14.188 -
  14.189 -# Set the default forwarding policy for $dev to drop.
  14.190 -# Allow forwarding to the bridge.
  14.191 -antispoofing () {
  14.192 -    iptables -P FORWARD DROP
  14.193 -    iptables -F FORWARD
  14.194 -    iptables -A FORWARD -m physdev --physdev-in ${pdev} -j ACCEPT
  14.195 -}
  14.196 -
  14.197 -# Usage: show_status dev bridge
  14.198 -# Print ifconfig and routes.
  14.199 -show_status () {
  14.200 -    local dev=$1
  14.201 -    local bridge=$2
  14.202 -    
  14.203 -    echo '============================================================'
  14.204 -    ip addr show ${dev}
  14.205 -    ip addr show ${bridge}
  14.206 -    echo ' '
  14.207 -    brctl show ${bridge}
  14.208 -    echo ' '
  14.209 -    ip route list
  14.210 -    echo ' '
  14.211 -    route -n
  14.212 -    echo '============================================================'
  14.213 -}
  14.214 -
  14.215 -op_start () {
  14.216 -    if [ "${bridge}" = "null" ] ; then
  14.217 -	return
  14.218 -    fi
  14.219 -
  14.220 -    if link_exists "$pdev"; then
  14.221 -        # The device is already up.
  14.222 -        return
  14.223 -    fi
  14.224 -
  14.225 -    create_bridge ${tdev}
  14.226 -
  14.227 -    preiftransfer ${netdev}
  14.228 -    transfer_addrs ${netdev} ${tdev}
  14.229 -    if ! ifdown ${netdev}; then
  14.230 -	# If ifdown fails, remember the IP details.
  14.231 -	get_ip_info ${netdev}
  14.232 -	ip link set ${netdev} down
  14.233 -	ip addr flush ${netdev}
  14.234 -    fi
  14.235 -    ip link set ${netdev} name ${pdev}
  14.236 -    ip link set ${tdev} name ${bridge}
  14.237 -
  14.238 -    setup_bridge_port ${pdev}
  14.239 -
  14.240 -    add_to_bridge2 ${bridge} ${pdev}
  14.241 -    do_ifup ${bridge}
  14.242 -
  14.243 -    if [ ${antispoof} = 'yes' ] ; then
  14.244 -	antispoofing
  14.245 -    fi
  14.246 -}
  14.247 -
  14.248 -op_stop () {
  14.249 -    if [ "${bridge}" = "null" ]; then
  14.250 -	return
  14.251 -    fi
  14.252 -    if ! link_exists "$bridge"; then
  14.253 -	return
  14.254 -    fi
  14.255 -
  14.256 -    transfer_addrs ${bridge} ${pdev}
  14.257 -    if ! ifdown ${bridge}; then
  14.258 -	get_ip_info ${bridge}
  14.259 -    fi
  14.260 -    ip link set ${pdev} down
  14.261 -    ip addr flush ${bridge}
  14.262 -
  14.263 -    brctl delif ${bridge} ${pdev}
  14.264 -    ip link set ${bridge} down
  14.265 -
  14.266 -    ip link set ${bridge} name ${tdev}
  14.267 -    ip link set ${pdev} name ${netdev}
  14.268 -    do_ifup ${netdev}
  14.269 -
  14.270 -    brctl delbr ${tdev}
  14.271 -}
  14.272 -
  14.273 -# adds $dev to $bridge but waits for $dev to be in running state first
  14.274 -add_to_bridge2() {
  14.275 -    local bridge=$1
  14.276 -    local dev=$2
  14.277 -    local maxtries=10
  14.278 -
  14.279 -    echo -n "Waiting for ${dev} to negotiate link."
  14.280 -    ip link set ${dev} up
  14.281 -    for i in `seq ${maxtries}` ; do
  14.282 -	if ifconfig ${dev} | grep -q RUNNING ; then
  14.283 -	    break
  14.284 -	else
  14.285 -	    echo -n '.'
  14.286 -	    sleep 1
  14.287 -	fi
  14.288 -    done
  14.289 -
  14.290 -    if [ ${i} -eq ${maxtries} ] ; then echo -n '(link isnt in running state)' ; fi
  14.291 -    echo
  14.292 -
  14.293 -    add_to_bridge ${bridge} ${dev}
  14.294 -}
  14.295 -
  14.296 -case "$command" in
  14.297 -    start)
  14.298 -	op_start
  14.299 -	;;
  14.300 -    
  14.301 -    stop)
  14.302 -	op_stop
  14.303 -	;;
  14.304 -
  14.305 -    status)
  14.306 -	show_status ${netdev} ${bridge}
  14.307 -	;;
  14.308 -
  14.309 -    *)
  14.310 -	echo "Unknown command: $command" >&2
  14.311 -	echo 'Valid commands are: start, stop, status' >&2
  14.312 -	exit 1
  14.313 -esac
    15.1 --- a/tools/examples/network-nat	Tue Nov 04 11:46:00 2008 +0000
    15.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.3 @@ -1,119 +0,0 @@
    15.4 -#!/bin/bash -x
    15.5 -#============================================================================
    15.6 -# Default Xen network start/stop script when using NAT.
    15.7 -# Xend calls a network script when it starts.
    15.8 -# The script name to use is defined in /etc/xen/xend-config.sxp
    15.9 -# in the network-script field.
   15.10 -#
   15.11 -# Usage:
   15.12 -#
   15.13 -# network-nat (start|stop|status) {VAR=VAL}*
   15.14 -#
   15.15 -# Vars:
   15.16 -#
   15.17 -# netdev     The gateway interface (default eth0).
   15.18 -# antispoof  Whether to use iptables to prevent spoofing (default no).
   15.19 -# dhcp       Whether to alter the local DHCP configuration (default no).
   15.20 -#
   15.21 -#============================================================================
   15.22 -
   15.23 -dir=$(dirname "$0")
   15.24 -. "$dir/xen-script-common.sh"
   15.25 -. "$dir/xen-network-common.sh"
   15.26 -
   15.27 -findCommand "$@"
   15.28 -evalVariables "$@"
   15.29 -
   15.30 -netdev=${netdev:-eth0}
   15.31 -# antispoofing not yet implemented
   15.32 -antispoof=${antispoof:-no}
   15.33 -
   15.34 -# turn on dhcp feature by default if dhcpd is installed
   15.35 -if [ -f /etc/dhcpd.conf ]
   15.36 -then
   15.37 -	dhcp=${dhcp:-yes}
   15.38 -else
   15.39 -	dhcp=${dhcp:-no}
   15.40 -fi
   15.41 -
   15.42 -
   15.43 -if [ "$dhcp" != 'no' ]
   15.44 -then
   15.45 -  dhcpd_conf_file=$(find_dhcpd_conf_file)
   15.46 -  dhcpd_init_file=$(find_dhcpd_init_file)
   15.47 -  if [ -z "$dhcpd_conf_file" ] || [ -z "$dhcpd_init_file" ]
   15.48 -  then
   15.49 -    echo 'Failed to find dhcpd configuration or init file.' >&2
   15.50 -    exit 1
   15.51 -  fi
   15.52 -fi
   15.53 -
   15.54 -
   15.55 -function dhcp_start()
   15.56 -{
   15.57 -  if ! grep -q "subnet 10.0.0.0" "$dhcpd_conf_file"
   15.58 -  then
   15.59 -    echo >>"$dhcpd_conf_file" "subnet 10.0.0.0 netmask 255.255.0.0 {}"
   15.60 -  fi
   15.61 -
   15.62 -  "$dhcpd_init_file" restart
   15.63 -}
   15.64 -
   15.65 -
   15.66 -function dhcp_stop()
   15.67 -{
   15.68 -  local tmpfile=$(mktemp)
   15.69 -  grep -v "subnet 10.0.0.0" "$dhcpd_conf_file" >"$tmpfile"
   15.70 -  if diff "$tmpfile" "$dhcpd_conf_file" >&/dev/null
   15.71 -  then
   15.72 -    rm "$tmpfile"
   15.73 -  else
   15.74 -    mv "$tmpfile" "$dhcpd_conf_file"
   15.75 -  fi
   15.76 -
   15.77 -  "$dhcpd_init_file" restart
   15.78 -}
   15.79 -
   15.80 -
   15.81 -op_start() {
   15.82 -	echo 1 >/proc/sys/net/ipv4/ip_forward
   15.83 -	iptables -t nat -A POSTROUTING -o ${netdev} -j MASQUERADE
   15.84 -        [ "$dhcp" != 'no' ] && dhcp_start
   15.85 -}
   15.86 -
   15.87 -
   15.88 -op_stop() {
   15.89 -        [ "$dhcp" != 'no' ] && dhcp_stop
   15.90 -	iptables -t nat -D POSTROUTING -o ${netdev} -j MASQUERADE
   15.91 -}
   15.92 -
   15.93 -
   15.94 -show_status() {
   15.95 -    echo '============================================================'
   15.96 -    ifconfig
   15.97 -    echo ' '
   15.98 -    ip route list
   15.99 -    echo ' '
  15.100 -    route -n
  15.101 -    echo '============================================================'
  15.102 -
  15.103 -}
  15.104 -
  15.105 -case "$command" in
  15.106 -    start)
  15.107 -        op_start
  15.108 -        ;;
  15.109 -    
  15.110 -    stop)
  15.111 -        op_stop
  15.112 -        ;;
  15.113 -
  15.114 -    status)
  15.115 -        show_status
  15.116 -       ;;
  15.117 -
  15.118 -    *)
  15.119 -       echo "Unknown command: $command" >&2
  15.120 -       echo 'Valid commands are: start, stop, status' >&2
  15.121 -       exit 1
  15.122 -esac
    16.1 --- a/tools/examples/network-route	Tue Nov 04 11:46:00 2008 +0000
    16.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    16.3 @@ -1,27 +0,0 @@
    16.4 -#!/bin/bash
    16.5 -#============================================================================
    16.6 -# Default Xen network start/stop script.
    16.7 -# Xend calls a network script when it starts.
    16.8 -# The script name to use is defined in /etc/xen/xend-config.sxp
    16.9 -# in the network-script field.
   16.10 -#
   16.11 -# Usage:
   16.12 -#
   16.13 -# network-route (start|stop|status) {VAR=VAL}*
   16.14 -#
   16.15 -# Vars:
   16.16 -#
   16.17 -# netdev     The gateway interface (default eth0).
   16.18 -# antispoof  Whether to use iptables to prevent spoofing (default yes).
   16.19 -#
   16.20 -#============================================================================
   16.21 -
   16.22 -dir=$(dirname "$0")
   16.23 -. "$dir/xen-script-common.sh"
   16.24 -
   16.25 -evalVariables "$@"
   16.26 -
   16.27 -netdev=${netdev:-eth${vifnum}}
   16.28 -
   16.29 -echo 1 >/proc/sys/net/ipv4/ip_forward
   16.30 -echo 1 >/proc/sys/net/ipv4/conf/${netdev}/proxy_arp
    17.1 --- a/tools/examples/vif-bridge	Tue Nov 04 11:46:00 2008 +0000
    17.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    17.3 @@ -1,100 +0,0 @@
    17.4 -#!/bin/bash
    17.5 -#============================================================================
    17.6 -# /etc/xen/vif-bridge
    17.7 -#
    17.8 -# Script for configuring a vif in bridged mode.
    17.9 -# The hotplugging system will call this script if it is specified either in
   17.10 -# the device configuration given to Xend, or the default Xend configuration
   17.11 -# in /etc/xen/xend-config.sxp.  If the script is specified in neither of those
   17.12 -# places, then this script is the default.
   17.13 -#
   17.14 -# Usage:
   17.15 -# vif-bridge (add|remove|online|offline)
   17.16 -#
   17.17 -# Environment vars:
   17.18 -# vif         vif interface name (required).
   17.19 -# XENBUS_PATH path to this device's details in the XenStore (required).
   17.20 -#
   17.21 -# Read from the store:
   17.22 -# bridge  bridge to add the vif to (optional).  Defaults to searching for the
   17.23 -#         bridge itself.
   17.24 -# ip      list of IP networks for the vif, space-separated (optional).
   17.25 -#
   17.26 -# up:
   17.27 -# Enslaves the vif interface to the bridge and adds iptables rules
   17.28 -# for its ip addresses (if any).
   17.29 -#
   17.30 -# down:
   17.31 -# Removes the vif interface from the bridge and removes the iptables
   17.32 -# rules for its ip addresses (if any).
   17.33 -#============================================================================
   17.34 -
   17.35 -dir=$(dirname "$0")
   17.36 -. "$dir/vif-common.sh"
   17.37 -
   17.38 -bridge=${bridge:-}
   17.39 -bridge=$(xenstore_read_default "$XENBUS_PATH/bridge" "$bridge")
   17.40 -
   17.41 -if [ -z "$bridge" ]
   17.42 -then
   17.43 -  bridge=$(brctl show | cut -d "
   17.44 -" -f 2 | cut -f 1)
   17.45 -
   17.46 -  if [ -z "$bridge" ]
   17.47 -  then
   17.48 -     fatal "Could not find bridge, and none was specified"
   17.49 -  fi
   17.50 -else
   17.51 -  #
   17.52 -  # Old style bridge setup with netloop, used to have a bridge name
   17.53 -  # of xenbrX, enslaving pethX and vif0.X, and then configuring
   17.54 -  # eth0.
   17.55 -  #
   17.56 -  # New style bridge setup does not use netloop, so the bridge name
   17.57 -  # is ethX and the physical device is enslaved pethX
   17.58 -  #
   17.59 -  # So if...
   17.60 -  #
   17.61 -  #   - User asks for xenbrX
   17.62 -  #   - AND xenbrX doesn't exist
   17.63 -  #   - AND there is a ethX device which is a bridge
   17.64 -  #
   17.65 -  # ..then we translate xenbrX to ethX
   17.66 -  #
   17.67 -  # This lets old config files work without modification
   17.68 -  #
   17.69 -  if [ ! -e "/sys/class/net/$bridge" ] && [ -z "${bridge##xenbr*}" ]
   17.70 -  then
   17.71 -     if [ -e "/sys/class/net/eth${bridge#xenbr}/bridge" ]
   17.72 -     then
   17.73 -        bridge="eth${bridge#xenbr}"
   17.74 -     fi
   17.75 -  fi
   17.76 -fi
   17.77 -
   17.78 -RET=0
   17.79 -ip link show $bridge 1>/dev/null 2>&1 || RET=1
   17.80 -if [ "$RET" -eq 1 ]
   17.81 -then
   17.82 -    fatal "Could not find bridge device $bridge"
   17.83 -fi
   17.84 -
   17.85 -case "$command" in
   17.86 -    online)
   17.87 -	setup_bridge_port "$vif"
   17.88 -	add_to_bridge "$bridge" "$vif"
   17.89 -        ;;
   17.90 -
   17.91 -    offline)
   17.92 -        do_without_error brctl delif "$bridge" "$vif"
   17.93 -        do_without_error ifconfig "$vif" down
   17.94 -        ;;
   17.95 -esac
   17.96 -
   17.97 -handle_iptable
   17.98 -
   17.99 -log debug "Successful vif-bridge $command for $vif, bridge $bridge."
  17.100 -if [ "$command" == "online" ]
  17.101 -then
  17.102 -  success
  17.103 -fi
    18.1 --- a/tools/examples/vif-common.sh	Tue Nov 04 11:46:00 2008 +0000
    18.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    18.3 @@ -1,151 +0,0 @@
    18.4 -#
    18.5 -# Copyright (c) 2005 XenSource Ltd.
    18.6 -#
    18.7 -# This library is free software; you can redistribute it and/or
    18.8 -# modify it under the terms of version 2.1 of the GNU Lesser General Public
    18.9 -# License as published by the Free Software Foundation.
   18.10 -#
   18.11 -# This library is distributed in the hope that it will be useful,
   18.12 -# but WITHOUT ANY WARRANTY; without even the implied warranty of
   18.13 -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   18.14 -# Lesser General Public License for more details.
   18.15 -#
   18.16 -# You should have received a copy of the GNU Lesser General Public
   18.17 -# License along with this library; if not, write to the Free Software
   18.18 -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   18.19 -#
   18.20 -
   18.21 -
   18.22 -dir=$(dirname "$0")
   18.23 -. "$dir/xen-hotplug-common.sh"
   18.24 -. "$dir/xen-network-common.sh"
   18.25 -
   18.26 -findCommand "$@"
   18.27 -
   18.28 -if [ "$command" != "online" ]  &&
   18.29 -   [ "$command" != "offline" ] &&
   18.30 -   [ "$command" != "add" ]     &&
   18.31 -   [ "$command" != "remove" ]
   18.32 -then
   18.33 -  log err "Invalid command: $command"
   18.34 -  exit 1
   18.35 -fi
   18.36 -
   18.37 -case "$command" in
   18.38 -    add | remove)
   18.39 -        exit 0
   18.40 -        ;;
   18.41 -esac
   18.42 -
   18.43 -
   18.44 -# Parameters may be read from the environment, the command line arguments, and
   18.45 -# the store, with overriding in that order.  The environment is given by the
   18.46 -# driver, the command line is given by the Xend global configuration, and
   18.47 -# store details are given by the per-domain or per-device configuration.
   18.48 -
   18.49 -evalVariables "$@"
   18.50 -
   18.51 -ip=${ip:-}
   18.52 -ip=$(xenstore_read_default "$XENBUS_PATH/ip" "$ip")
   18.53 -
   18.54 -# Check presence of compulsory args.
   18.55 -XENBUS_PATH="${XENBUS_PATH:?}"
   18.56 -vif="${vif:?}"
   18.57 -
   18.58 -
   18.59 -vifname=$(xenstore_read_default "$XENBUS_PATH/vifname" "")
   18.60 -if [ "$vifname" ]
   18.61 -then
   18.62 -  if [ "$command" == "online" ] && ! ip link show "$vifname" >&/dev/null
   18.63 -  then
   18.64 -    do_or_die ip link set "$vif" name "$vifname"
   18.65 -  fi
   18.66 -  vif="$vifname"
   18.67 -fi
   18.68 -
   18.69 -
   18.70 -frob_iptable()
   18.71 -{
   18.72 -  if [ "$command" == "online" ]
   18.73 -  then
   18.74 -    local c="-A"
   18.75 -  else
   18.76 -    local c="-D"
   18.77 -  fi
   18.78 -
   18.79 -  iptables "$c" FORWARD -m physdev --physdev-in "$vif" "$@" -j ACCEPT \
   18.80 -    2>/dev/null ||
   18.81 -    [ "$c" == "-D" ] ||
   18.82 -    log err \
   18.83 -     "iptables $c FORWARD -m physdev --physdev-in $vif $@ -j ACCEPT failed.
   18.84 -If you are using iptables, this may affect networking for guest domains."
   18.85 -}
   18.86 -
   18.87 -
   18.88 -##
   18.89 -# Add or remove the appropriate entries in the iptables.  With antispoofing
   18.90 -# turned on, we have to explicitly allow packets to the interface, regardless
   18.91 -# of the ip setting.  If ip is set, then we additionally restrict the packets
   18.92 -# to those coming from the specified networks, though we allow DHCP requests
   18.93 -# as well.
   18.94 -#
   18.95 -handle_iptable()
   18.96 -{
   18.97 -  # Check for a working iptables installation.  Checking for the iptables
   18.98 -  # binary is not sufficient, because the user may not have the appropriate
   18.99 -  # modules installed.  If iptables is not working, then there's no need to do
  18.100 -  # anything with it, so we can just return.
  18.101 -  if ! iptables -L -n >&/dev/null
  18.102 -  then
  18.103 -    return
  18.104 -  fi
  18.105 -
  18.106 -  if [ "$ip" != "" ]
  18.107 -  then
  18.108 -      local addr
  18.109 -      for addr in $ip
  18.110 -      do
  18.111 -        frob_iptable -s "$addr"
  18.112 -      done
  18.113 -
  18.114 -      # Always allow the domain to talk to a DHCP server.
  18.115 -      frob_iptable -p udp --sport 68 --dport 67
  18.116 -  else
  18.117 -      # No IP addresses have been specified, so allow anything.
  18.118 -      frob_iptable
  18.119 -  fi
  18.120 -}
  18.121 -
  18.122 -
  18.123 -##
  18.124 -# ip_of interface
  18.125 -#
  18.126 -# Print the IP address currently in use at the given interface, or nothing if
  18.127 -# the interface is not up.
  18.128 -#
  18.129 -ip_of()
  18.130 -{
  18.131 -  ip addr show "$1" | awk "/^.*inet.*$1\$/{print \$2}" | sed -n '1 s,/.*,,p'
  18.132 -}
  18.133 -
  18.134 -
  18.135 -##
  18.136 -# dom0_ip
  18.137 -#
  18.138 -# Print the IP address of the interface in dom0 through which we are routing.
  18.139 -# This is the IP address on the interface specified as "netdev" as a parameter
  18.140 -# to these scripts, or eth0 by default.  This function will call fatal if no
  18.141 -# such interface could be found.
  18.142 -#
  18.143 -dom0_ip()
  18.144 -{
  18.145 -  local nd=${netdev:-eth0}
  18.146 -  local result=$(ip_of "$nd")
  18.147 -  if [ -z "$result" ]
  18.148 -  then
  18.149 -      fatal
  18.150 -"$netdev is not up.  Bring it up or specify another interface with " \
  18.151 -"netdev=<if> as a parameter to $0."
  18.152 -  fi
  18.153 -  echo "$result"
  18.154 -}
    19.1 --- a/tools/examples/vif-nat	Tue Nov 04 11:46:00 2008 +0000
    19.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    19.3 @@ -1,192 +0,0 @@
    19.4 -#!/bin/bash
    19.5 -#============================================================================
    19.6 -# /etc/xen/vif-nat
    19.7 -#
    19.8 -# Script for configuring a vif in routed-nat mode.
    19.9 -# The hotplugging system will call this script if it is specified either in
   19.10 -# the device configuration given to Xend, or the default Xend configuration
   19.11 -# in /etc/xen/xend-config.sxp.  If the script is specified in neither of those
   19.12 -# places, then vif-bridge is the default.
   19.13 -#
   19.14 -# Usage:
   19.15 -# vif-nat (add|remove|online|offline)
   19.16 -#
   19.17 -# Environment vars:
   19.18 -# vif         vif interface name (required).
   19.19 -# XENBUS_PATH path to this device's details in the XenStore (required).
   19.20 -#
   19.21 -# Parameters:
   19.22 -# dhcp        Whether to alter the local DHCP configuration to include this
   19.23 -#             new host (default no).
   19.24 -#
   19.25 -# Read from the store:
   19.26 -# ip      list of IP networks for the vif, space-separated (default given in
   19.27 -#         this script).
   19.28 -#============================================================================
   19.29 -
   19.30 -
   19.31 -dir=$(dirname "$0")
   19.32 -. "$dir/vif-common.sh"
   19.33 -
   19.34 -# turn on dhcp feature by default if dhcpd is installed
   19.35 -if [ -f /etc/dhcpd.conf ]
   19.36 -then
   19.37 -	dhcp=${dhcp:-yes}
   19.38 -else
   19.39 -	dhcp=${dhcp:-no}
   19.40 -fi
   19.41 -
   19.42 -if [ "$dhcp" != 'no' ]
   19.43 -then
   19.44 -  dhcpd_conf_file=$(find_dhcpd_conf_file)
   19.45 -  dhcpd_init_file=$(find_dhcpd_init_file)
   19.46 -  dhcpd_arg_file=$(find_dhcpd_arg_file)
   19.47 -  if [ -z "$dhcpd_conf_file" ] || [ -z "$dhcpd_init_file" ] || [ -z "$dhcpd_arg_file" ]
   19.48 -  then
   19.49 -    echo 'Failed to find dhcpd configuration or init or args file.' >&2
   19.50 -    exit 1
   19.51 -  fi
   19.52 -fi
   19.53 -
   19.54 -
   19.55 -domid=$(xenstore_read "$XENBUS_PATH/frontend-id")
   19.56 -vifid=$(xenstore_read "$XENBUS_PATH/handle")
   19.57 -vifid=$(( $vifid + 1 ))
   19.58 -
   19.59 -
   19.60 -ip_from_dom()
   19.61 -{
   19.62 -  local domid1=$(( $domid / 256 ))
   19.63 -  local domid2=$(( $domid % 256 ))
   19.64 -
   19.65 -  echo "10.$domid1.$domid2.$vifid/16"
   19.66 -}
   19.67 -
   19.68 -
   19.69 -routing_ip()
   19.70 -{
   19.71 -  echo $(echo $1 | awk -F. '{print $1"."$2"."$3"."$4 + 127}')
   19.72 -}
   19.73 -
   19.74 -
   19.75 -dotted_quad()
   19.76 -{
   19.77 - echo\
   19.78 - $(( ($1 & 0xFF000000) >> 24))\
   19.79 -.$(( ($1 & 0x00FF0000) >> 16))\
   19.80 -.$(( ($1 & 0x0000FF00) >> 8 ))\
   19.81 -.$((  $1 & 0x000000FF       ))
   19.82 -}
   19.83 -
   19.84 -
   19.85 -if [ "$ip" = "" ]
   19.86 -then
   19.87 -  ip=$(ip_from_dom)
   19.88 -fi
   19.89 -
   19.90 -router_ip=$(routing_ip "$ip")
   19.91 -
   19.92 -# Split the given IP/bits pair.
   19.93 -vif_ip=`echo ${ip} | awk -F/ '{print $1}'`
   19.94 -
   19.95 -hostname=$(xenstore_read "$XENBUS_PATH/domain" | tr -- '_.:/+' '-----')
   19.96 -if [ "$vifid" != "1" ]
   19.97 -then
   19.98 -  hostname="$hostname-$vifid"
   19.99 -fi
  19.100 -
  19.101 -dhcparg_remove_entry()
  19.102 -{
  19.103 -  local tmpfile=$(mktemp)
  19.104 -  sed -e "s/$vif //" "$dhcpd_arg_file" >"$tmpfile"
  19.105 -  if diff "$tmpfile" "$dhcpd_arg_file" >/dev/null
  19.106 -  then
  19.107 -    rm "$tmpfile"
  19.108 -  else
  19.109 -    mv "$tmpfile" "$dhcpd_arg_file"
  19.110 -  fi
  19.111 -}
  19.112 -
  19.113 -dhcparg_add_entry()
  19.114 -{
  19.115 -  dhcparg_remove_entry
  19.116 -  local tmpfile=$(mktemp)
  19.117 -  # handle Red Hat, SUSE, and Debian styles, with or without quotes
  19.118 -  sed -e 's/^DHCPDARGS="*\([^"]*\)"*/DHCPDARGS="\1'"$vif "'"/' \
  19.119 -     "$dhcpd_arg_file" >"$tmpfile" && mv "$tmpfile" "$dhcpd_arg_file"
  19.120 -  sed -e 's/^DHCPD_INTERFACE="*\([^"]*\)"*/DHCPD_INTERFACE="\1'"$vif "'"/' \
  19.121 -     "$dhcpd_arg_file" >"$tmpfile" && mv "$tmpfile" "$dhcpd_arg_file"
  19.122 -  sed -e 's/^INTERFACES="*\([^"]*\)"*/INTERFACES="\1'"$vif "'"/' \
  19.123 -     "$dhcpd_arg_file" >"$tmpfile" && mv "$tmpfile" "$dhcpd_arg_file"
  19.124 -  rm -f "$tmpfile"
  19.125 -}
  19.126 -
  19.127 -dhcp_remove_entry()
  19.128 -{
  19.129 -  local tmpfile=$(mktemp)
  19.130 -  grep -v "host $hostname" "$dhcpd_conf_file" >"$tmpfile"
  19.131 -  if diff "$tmpfile" "$dhcpd_conf_file" >/dev/null
  19.132 -  then
  19.133 -    rm "$tmpfile"
  19.134 -  else
  19.135 -    mv "$tmpfile" "$dhcpd_conf_file"
  19.136 -  fi
  19.137 -  dhcparg_remove_entry
  19.138 -}
  19.139 -
  19.140 -
  19.141 -dhcp_up()
  19.142 -{
  19.143 -  claim_lock "vif-nat-dhcp"
  19.144 -  dhcp_remove_entry
  19.145 -  mac=$(xenstore_read "$XENBUS_PATH/mac")
  19.146 -  echo >>"$dhcpd_conf_file" \
  19.147 -"host $hostname { hardware ethernet $mac; fixed-address $vif_ip; option routers $router_ip; option host-name \"$hostname\"; }"
  19.148 -  dhcparg_add_entry
  19.149 -  release_lock "vif-nat-dhcp"
  19.150 -  "$dhcpd_init_file" restart || true
  19.151 -}
  19.152 -
  19.153 -
  19.154 -dhcp_down()
  19.155 -{
  19.156 -  claim_lock "vif-nat-dhcp"
  19.157 -  dhcp_remove_entry
  19.158 -  release_lock "vif-nat-dhcp"
  19.159 -  "$dhcpd_init_file" restart || true # We need to ignore failure because
  19.160 -                                     # ISC dhcpd 3 borks if there is nothing
  19.161 -                                     # for it to do, which is the case if
  19.162 -                                     # the outgoing interface is not
  19.163 -                                     # configured to offer leases and there
  19.164 -                                     # are no vifs.
  19.165 -}
  19.166 -
  19.167 -
  19.168 -case "$command" in
  19.169 -    online)
  19.170 -        if ip route | grep -q "dev $vif"
  19.171 -        then
  19.172 -          log debug "$vif already up"
  19.173 -          exit 0
  19.174 -        fi
  19.175 -
  19.176 -        do_or_die ip link set "$vif" up arp on
  19.177 -        do_or_die ip addr add "$router_ip" dev "$vif"
  19.178 -        do_or_die ip route add "$vif_ip" dev "$vif" src "$router_ip"
  19.179 -        echo 1 >/proc/sys/net/ipv4/conf/${vif}/proxy_arp
  19.180 -        [ "$dhcp" != 'no' ] && dhcp_up
  19.181 -        ;;
  19.182 -    offline)
  19.183 -        [ "$dhcp" != 'no' ] && dhcp_down
  19.184 -        do_without_error ifconfig "$vif" down
  19.185 -        ;;
  19.186 -esac
  19.187 -
  19.188 -
  19.189 -handle_iptable
  19.190 -
  19.191 -log debug "Successful vif-nat $command for $vif."
  19.192 -if [ "$command" = "online" ]
  19.193 -then
  19.194 -  success
  19.195 -fi
    20.1 --- a/tools/examples/vif-route	Tue Nov 04 11:46:00 2008 +0000
    20.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    20.3 @@ -1,56 +0,0 @@
    20.4 -#!/bin/bash
    20.5 -#============================================================================
    20.6 -# /etc/xen/vif-route
    20.7 -#
    20.8 -# Script for configuring a vif in routed mode.
    20.9 -# The hotplugging system will call this script if it is specified either in
   20.10 -# the device configuration given to Xend, or the default Xend configuration
   20.11 -# in /etc/xen/xend-config.sxp.  If the script is specified in neither of those
   20.12 -# places, then vif-bridge is the default.
   20.13 -#
   20.14 -# Usage:
   20.15 -# vif-route (add|remove|online|offline)
   20.16 -#
   20.17 -# Environment vars:
   20.18 -# vif         vif interface name (required).
   20.19 -# XENBUS_PATH path to this device's details in the XenStore (required).
   20.20 -#
   20.21 -# Read from the store:
   20.22 -# ip      list of IP networks for the vif, space-separated (default given in
   20.23 -#         this script).
   20.24 -#============================================================================
   20.25 -
   20.26 -dir=$(dirname "$0")
   20.27 -. "$dir/vif-common.sh"
   20.28 -
   20.29 -main_ip=$(dom0_ip)
   20.30 -
   20.31 -case "$command" in
   20.32 -    online)
   20.33 -        ifconfig ${vif} ${main_ip} netmask 255.255.255.255 up
   20.34 -        echo 1 >/proc/sys/net/ipv4/conf/${vif}/proxy_arp
   20.35 -        ipcmd='add'
   20.36 -        cmdprefix=''
   20.37 -        ;;
   20.38 -    offline)
   20.39 -        do_without_error ifdown ${vif}
   20.40 -        ipcmd='del'
   20.41 -        cmdprefix='do_without_error'
   20.42 -        ;;
   20.43 -esac
   20.44 -
   20.45 -if [ "${ip}" ] ; then
   20.46 -    # If we've been given a list of IP addresses, then add routes from dom0 to
   20.47 -    # the guest using those addresses.
   20.48 -    for addr in ${ip} ; do
   20.49 -      ${cmdprefix} ip route ${ipcmd} ${addr} dev ${vif} src ${main_ip}
   20.50 -    done 
   20.51 -fi
   20.52 -
   20.53 -handle_iptable
   20.54 -
   20.55 -log debug "Successful vif-route $command for $vif."
   20.56 -if [ "$command" = "online" ]
   20.57 -then
   20.58 -  success
   20.59 -fi
    21.1 --- a/tools/examples/vscsi	Tue Nov 04 11:46:00 2008 +0000
    21.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    21.3 @@ -1,22 +0,0 @@
    21.4 -#!/bin/sh
    21.5 -#
    21.6 -# Copyright (c) 2007, FUJITSU Limited
    21.7 -# Based on the block scripts code.
    21.8 -#
    21.9 -
   21.10 -dir=$(dirname "$0")
   21.11 -. "$dir/xen-hotplug-common.sh"
   21.12 -
   21.13 -findCommand "$@"
   21.14 -
   21.15 -case "$command" in
   21.16 -	add)
   21.17 -		success
   21.18 -		;;
   21.19 -	remove)
   21.20 -		# TODO
   21.21 -		exit 0
   21.22 -		;;
   21.23 -esac
   21.24 -
   21.25 -exit 0
    22.1 --- a/tools/examples/vtpm	Tue Nov 04 11:46:00 2008 +0000
    22.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    22.3 @@ -1,22 +0,0 @@
    22.4 -#!/bin/bash
    22.5 -
    22.6 -dir=$(dirname "$0")
    22.7 -. "$dir/vtpm-hotplug-common.sh"
    22.8 -
    22.9 -vtpm_fatal_error=0
   22.10 -
   22.11 -case "$command" in
   22.12 -  add)
   22.13 -    vtpm_create_instance
   22.14 -  ;;
   22.15 -  remove)
   22.16 -    vtpm_remove_instance
   22.17 -  ;;
   22.18 -esac
   22.19 -
   22.20 -if [ $vtpm_fatal_error -eq 0 ]; then
   22.21 -	log debug "Successful vTPM operation '$command'."
   22.22 -	success
   22.23 -else
   22.24 -	fatal "Error while executing vTPM operation '$command'."
   22.25 -fi
    23.1 --- a/tools/examples/vtpm-common.sh	Tue Nov 04 11:46:00 2008 +0000
    23.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    23.3 @@ -1,448 +0,0 @@
    23.4 -#
    23.5 -# Copyright (c) 2005 IBM Corporation
    23.6 -# Copyright (c) 2005 XenSource Ltd.
    23.7 -#
    23.8 -# This library is free software; you can redistribute it and/or
    23.9 -# modify it under the terms of version 2.1 of the GNU Lesser General Public
   23.10 -# License as published by the Free Software Foundation.
   23.11 -#
   23.12 -# This library is distributed in the hope that it will be useful,
   23.13 -# but WITHOUT ANY WARRANTY; without even the implied warranty of
   23.14 -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   23.15 -# Lesser General Public License for more details.
   23.16 -#
   23.17 -# You should have received a copy of the GNU Lesser General Public
   23.18 -# License along with this library; if not, write to the Free Software
   23.19 -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23.20 -#
   23.21 -
   23.22 -dir=$(dirname "$0")
   23.23 -. "$dir/logging.sh"
   23.24 -. "$dir/locking.sh"
   23.25 -
   23.26 -VTPMDB="/var/vtpm/vtpm.db"
   23.27 -
   23.28 -#In the vtpm-impl file some commands should be defined:
   23.29 -#      vtpm_create, vtpm_setup, vtpm_start, etc. (see below)
   23.30 -if [ -r "$dir/vtpm-impl.alt" ]; then
   23.31 -	. "$dir/vtpm-impl.alt"
   23.32 -elif [ -r "$dir/vtpm-impl" ]; then
   23.33 -	. "$dir/vtpm-impl"
   23.34 -else
   23.35 -	function vtpm_create () {
   23.36 -		true
   23.37 -	}
   23.38 -	function vtpm_setup() {
   23.39 -		true
   23.40 -	}
   23.41 -	function vtpm_start() {
   23.42 -		true
   23.43 -	}
   23.44 -	function vtpm_suspend() {
   23.45 -		true
   23.46 -	}
   23.47 -	function vtpm_resume() {
   23.48 -		true
   23.49 -	}
   23.50 -	function vtpm_delete() {
   23.51 -		true
   23.52 -	}
   23.53 -	function vtpm_migrate() {
   23.54 -		echo "Error: vTPM migration accross machines not implemented."
   23.55 -	}
   23.56 -	function vtpm_migrate_local() {
   23.57 -		echo "Error: local vTPM migration not supported"
   23.58 -	}
   23.59 -	function vtpm_migrate_recover() {
   23.60 -		true
   23.61 -	}
   23.62 -fi
   23.63 -
   23.64 -
   23.65 -#Find the instance number for the vtpm given the name of the domain
   23.66 -# Parameters
   23.67 -# - vmname : the name of the vm
   23.68 -# Return value
   23.69 -#  Returns '0' if instance number could not be found, otherwise
   23.70 -#  it returns the instance number in the variable 'instance'
   23.71 -function vtpmdb_find_instance () {
   23.72 -	local vmname ret instance
   23.73 -	vmname=$1
   23.74 -	ret=0
   23.75 -
   23.76 -	instance=$(cat $VTPMDB |                   \
   23.77 -	          awk -vvmname=$vmname             \
   23.78 -	          '{                               \
   23.79 -	             if ( 1 != index($1,"#")) {    \
   23.80 -	               if ( $1 == vmname ) {       \
   23.81 -	                 print $2;                 \
   23.82 -	                 exit;                     \
   23.83 -	               }                           \
   23.84 -	             }                             \
   23.85 -	           }')
   23.86 -	if [ "$instance" != "" ]; then
   23.87 -		ret=$instance
   23.88 -	fi
   23.89 -	echo "$ret"
   23.90 -}
   23.91 -
   23.92 -
   23.93 -# Check whether a particular instance number is still available
   23.94 -# returns "0" if it is not available, "1" otherwise.
   23.95 -function vtpmdb_is_free_instancenum () {
   23.96 -	local instance instances avail i
   23.97 -	instance=$1
   23.98 -	avail=1
   23.99 -	#Allowed instance number range: 1-255
  23.100 -	if [ $instance -eq 0 -o $instance -gt 255 ]; then
  23.101 -		avail=0
  23.102 -	else
  23.103 -		instances=$(cat $VTPMDB |                \
  23.104 -		           gawk                          \
  23.105 -		           '{                            \
  23.106 -		               if (1 != index($1,"#")) { \
  23.107 -		                 printf("%s ",$2);       \
  23.108 -		               }                         \
  23.109 -		            }')
  23.110 -		for i in $instances; do
  23.111 -			if [ $i -eq $instance ]; then
  23.112 -				avail=0
  23.113 -				break
  23.114 -			fi
  23.115 -		done
  23.116 -	fi
  23.117 -	echo "$avail"
  23.118 -}
  23.119 -
  23.120 -
  23.121 -# Get an available instance number given the database
  23.122 -# Returns an unused instance number
  23.123 -function vtpmdb_get_free_instancenum () {
  23.124 -	local ctr instances don found
  23.125 -	instances=$(cat $VTPMDB |                \
  23.126 -	           gawk                          \
  23.127 -	           '{                            \
  23.128 -	               if (1 != index($1,"#")) { \
  23.129 -	                 printf("%s ",$2);       \
  23.130 -	               }                         \
  23.131 -	            }')
  23.132 -	ctr=1
  23.133 -	don=0
  23.134 -	while [ $don -eq 0 ]; do
  23.135 -		found=0
  23.136 -		for i in $instances; do
  23.137 -			if [ $i -eq $ctr ]; then
  23.138 -				found=1;
  23.139 -				break;
  23.140 -			fi
  23.141 -		done
  23.142 -
  23.143 -		if [ $found -eq 0 ]; then
  23.144 -			don=1
  23.145 -			break
  23.146 -		fi
  23.147 -		let ctr=ctr+1
  23.148 -	done
  23.149 -	echo "$ctr"
  23.150 -}
  23.151 -
  23.152 -
  23.153 -# Add a domain name and instance number to the DB file
  23.154 -function vtpmdb_add_instance () {
  23.155 -	local res vmname inst
  23.156 -	vmname=$1
  23.157 -	inst=$2
  23.158 -
  23.159 -	if [ ! -f $VTPMDB ]; then
  23.160 -		echo "#Database for VM to vTPM association" > $VTPMDB
  23.161 -		echo "#1st column: domain name" >> $VTPMDB
  23.162 -		echo "#2nd column: TPM instance number" >> $VTPMDB
  23.163 -	fi
  23.164 -	res=$(vtpmdb_validate_entry $vmname $inst)
  23.165 -	if [ $res -eq 0 ]; then
  23.166 -		echo "$vmname $inst" >> $VTPMDB
  23.167 -	fi
  23.168 -}
  23.169 -
  23.170 -
  23.171 -#Validate whether an entry is the same as passed to this
  23.172 -#function
  23.173 -function vtpmdb_validate_entry () {
  23.174 -	local res rc vmname inst
  23.175 -	rc=0
  23.176 -	vmname=$1
  23.177 -	inst=$2
  23.178 -
  23.179 -	res=$(cat $VTPMDB |            \
  23.180 -	     gawk -vvmname=$vmname     \
  23.181 -	          -vinst=$inst         \
  23.182 -	     '{                        \
  23.183 -	         if ( 1 == index($1,"#")) {\
  23.184 -	         } else                \
  23.185 -	         if ( $1 == vmname &&  \
  23.186 -	              $2 == inst) {    \
  23.187 -	            printf("1");       \
  23.188 -	            exit;              \
  23.189 -	         } else                \
  23.190 -	         if ( $1 == vmname ||  \
  23.191 -	              $2 == inst) {    \
  23.192 -	            printf("2");       \
  23.193 -	            exit;              \
  23.194 -	         }                     \
  23.195 -	     }')
  23.196 -
  23.197 -	if [ "$res" == "1" ]; then
  23.198 -		rc=1
  23.199 -	elif [ "$res" == "2" ]; then
  23.200 -		rc=2
  23.201 -	fi
  23.202 -	echo "$rc"
  23.203 -}
  23.204 -
  23.205 -
  23.206 -#Remove an entry from the vTPM database given its domain name
  23.207 -#and instance number
  23.208 -function vtpmdb_remove_entry () {
  23.209 -	local vmname instance VTPMDB_TMP
  23.210 -	vmname=$1
  23.211 -	instance=$2
  23.212 -	VTPMDB_TMP="$VTPMDB".tmp
  23.213 -
  23.214 -	$(cat $VTPMDB |            \
  23.215 -	 gawk -vvmname=$vmname     \
  23.216 -	 '{                        \
  23.217 -	    if ( $1 != vmname ) {  \
  23.218 -	      print $0;            \
  23.219 -	    }                      \
  23.220 -	 '} > $VTPMDB_TMP)
  23.221 -	if [ -e $VTPMDB_TMP ]; then
  23.222 -		mv -f $VTPMDB_TMP $VTPMDB
  23.223 -		vtpm_delete $instance
  23.224 -	else
  23.225 -		log err "Error creating temporary file '$VTPMDB_TMP'."
  23.226 -	fi
  23.227 -}
  23.228 -
  23.229 -
  23.230 -# Find the reason for the creation of this device:
  23.231 -# Returns 'resume' or 'create'
  23.232 -function vtpm_get_create_reason () {
  23.233 -	local resume
  23.234 -	resume=$(xenstore_read $XENBUS_PATH/resume)
  23.235 -	if [ "$resume" == "True" ]; then
  23.236 -		echo "resume"
  23.237 -	else
  23.238 -		echo "create"
  23.239 -	fi
  23.240 -}
  23.241 -
  23.242 -
  23.243 -#Create a vTPM instance
  23.244 -# If no entry in the TPM database is found, the instance is
  23.245 -# created and an entry added to the database.
  23.246 -function vtpm_create_instance () {
  23.247 -	local res instance domname reason uuid
  23.248 -	uuid=$(xenstore_read "$XENBUS_PATH"/uuid)
  23.249 -	reason=$(vtpm_get_create_reason)
  23.250 -
  23.251 -	claim_lock vtpmdb
  23.252 -
  23.253 -	instance="0"
  23.254 -
  23.255 -	if [ "$uuid" != "" ]; then
  23.256 -		instance=$(vtpmdb_find_instance $uuid)
  23.257 -	fi
  23.258 -	if [ "$instance" == "0" ]; then
  23.259 -		domname=$(xenstore_read "$XENBUS_PATH"/domain)
  23.260 -		instance=$(vtpmdb_find_instance $domname)
  23.261 -	fi
  23.262 -
  23.263 -	if [ "$instance" == "0" -a "$reason" != "create" ]; then
  23.264 -		release_lock vtpmdb
  23.265 -		return
  23.266 -	fi
  23.267 -
  23.268 -	if [ "$instance" == "0" ]; then
  23.269 -		#Try to give the preferred instance to the domain
  23.270 -		instance=$(xenstore_read "$XENBUS_PATH"/pref_instance)
  23.271 -		if [ "$instance" != "" ]; then
  23.272 -			res=$(vtpmdb_is_free_instancenum $instance)
  23.273 -			if [ $res -eq 0 ]; then
  23.274 -				instance=$(vtpmdb_get_free_instancenum)
  23.275 -			fi
  23.276 -		else
  23.277 -			instance=$(vtpmdb_get_free_instancenum)
  23.278 -		fi
  23.279 -
  23.280 -		vtpm_create $instance
  23.281 -
  23.282 -		if [ $vtpm_fatal_error -eq 0 ]; then
  23.283 -			if [ "$uuid" != "" ]; then
  23.284 -				vtpmdb_add_instance $uuid $instance
  23.285 -			else
  23.286 -				vtpmdb_add_instance $domname $instance
  23.287 -			fi
  23.288 -		fi
  23.289 -	else
  23.290 -		if [ "$reason" == "resume" ]; then
  23.291 -			vtpm_resume $instance
  23.292 -		else
  23.293 -			vtpm_start $instance
  23.294 -		fi
  23.295 -	fi
  23.296 -
  23.297 -	release_lock vtpmdb
  23.298 -
  23.299 -	xenstore_write $XENBUS_PATH/instance $instance
  23.300 -}
  23.301 -
  23.302 -
  23.303 -#Remove an instance when a VM is terminating or suspending.
  23.304 -#Since it is assumed that the VM will appear again, the
  23.305 -#entry is kept in the VTPMDB file.
  23.306 -function vtpm_remove_instance () {
  23.307 -	local instance reason domname uuid
  23.308 -	#Stop script execution quietly if path does not exist (anymore)
  23.309 -	xenstore-exists "$XENBUS_PATH"/domain
  23.310 -	uuid=$(xenstore_read "$XENBUS_PATH"/uuid)
  23.311 -
  23.312 -	claim_lock vtpmdb
  23.313 -
  23.314 -	instance="0"
  23.315 -
  23.316 -	if [ "$uuid" != "" ]; then
  23.317 -		instance=$(vtpmdb_find_instance $uuid)
  23.318 -	fi
  23.319 -
  23.320 -	if [ "$instance" == "0" ]; then
  23.321 -		domname=$(xenstore_read "$XENBUS_PATH"/domain)
  23.322 -		instance=$(vtpmdb_find_instance $domname)
  23.323 -	fi
  23.324 -
  23.325 -	if [ "$instance" != "0" ]; then
  23.326 -		vtpm_suspend $instance
  23.327 -	fi
  23.328 -
  23.329 -	release_lock vtpmdb
  23.330 -}
  23.331 -
  23.332 -
  23.333 -#Remove an entry in the VTPMDB file given the domain's name
  23.334 -#1st parameter: The name of the domain
  23.335 -function vtpm_delete_instance () {
  23.336 -	local instance
  23.337 -
  23.338 -	claim_lock vtpmdb
  23.339 -
  23.340 -	instance=$(vtpmdb_find_instance $1)
  23.341 -	if [ "$instance" != "0" ]; then
  23.342 -		vtpmdb_remove_entry $1 $instance
  23.343 -	fi
  23.344 -
  23.345 -	release_lock vtpmdb
  23.346 -}
  23.347 -
  23.348 -# Determine whether the given address is local to this machine
  23.349 -# Return values:
  23.350 -#  "-1" : the given machine name is invalid
  23.351 -#  "0"  : this is not an address of this machine
  23.352 -#  "1"  : this is an address local to this machine
  23.353 -function vtpm_isLocalAddress() {
  23.354 -	local addr res
  23.355 -	addr=$(ping $1 -c 1 |  \
  23.356 -	       gawk '{ print substr($3,2,length($3)-2); exit }')
  23.357 -	if [ "$addr" == "" ]; then
  23.358 -		echo "-1"
  23.359 -		return
  23.360 -	fi
  23.361 -	res=$(ifconfig | grep "inet addr" |  \
  23.362 -	     gawk -vaddr=$addr               \
  23.363 -	     '{                              \
  23.364 -	        if ( addr == substr($2, 6)) {\
  23.365 -	          print "1";                 \
  23.366 -	        }                            \
  23.367 -	     }'                              \
  23.368 -	    )
  23.369 -	if [ "$res" == "" ]; then
  23.370 -		echo "0"
  23.371 -		return
  23.372 -	fi
  23.373 -	echo "1"
  23.374 -}
  23.375 -
  23.376 -# Perform a migration step. This function differentiates between migration
  23.377 -# to the local host or to a remote machine.
  23.378 -# Parameters:
  23.379 -# 1st: destination host to migrate to
  23.380 -# 2nd: name of the domain to migrate
  23.381 -# 3rd: the migration step to perform
  23.382 -function vtpm_migration_step() {
  23.383 -	local res=$(vtpm_isLocalAddress $1)
  23.384 -	if [ "$res" == "0" ]; then
  23.385 -		vtpm_migrate $1 $2 $3
  23.386 -	else
  23.387 -		vtpm_migrate_local
  23.388 -	fi
  23.389 -}
  23.390 -
  23.391 -# Recover from migration due to an error. This function differentiates
  23.392 -# between migration to the local host or to a remote machine.
  23.393 -# Parameters:
  23.394 -# 1st: destination host the migration was going to
  23.395 -# 2nd: name of the domain that was to be migrated
  23.396 -# 3rd: the last successful migration step that was done
  23.397 -function vtpm_recover() {
  23.398 -	local res
  23.399 -	res=$(vtpm_isLocalAddress $1)
  23.400 -	if [ "$res" == "0" ]; then
  23.401 -		vtpm_migrate_recover $1 $2 $3
  23.402 -	fi
  23.403 -}
  23.404 -
  23.405 -
  23.406 -#Determine the domain id given a domain's name.
  23.407 -#1st parameter: name of the domain
  23.408 -#return value: domain id  or -1 if domain id could not be determined
  23.409 -function vtpm_domid_from_name () {
  23.410 -	local id name ids
  23.411 -	ids=$(xenstore-list /local/domain)
  23.412 -	for id in $ids; do
  23.413 -		name=$(xenstore-read /local/domain/$id/name)
  23.414 -		if [ "$name" == "$1" ]; then
  23.415 -			echo "$id"
  23.416 -			return
  23.417 -		fi
  23.418 -	done
  23.419 -	echo "-1"
  23.420 -}
  23.421 -
  23.422 -#Determine the virtual TPM's instance number using the domain ID.
  23.423 -#1st parm: domain ID
  23.424 -function vtpm_uuid_by_domid() {
  23.425 -	echo $(xenstore-read /local/domain/0/backend/vtpm/$1/0/uuid)
  23.426 -}
  23.427 -
  23.428 -
  23.429 -# Determine the vTPM's UUID by the name of the VM
  23.430 -function vtpm_uuid_from_vmname() {
  23.431 -	local domid=$(vtpm_domid_from_name $1)
  23.432 -	if [ "$domid" != "-1" ]; then
  23.433 -		echo $(vtpm_uuid_by_domid $domid)
  23.434 -		return
  23.435 -	fi
  23.436 -	echo ""
  23.437 -}
  23.438 -
  23.439 -#Add a virtual TPM instance number and its associated domain name
  23.440 -#to the VTPMDB file and activate usage of this virtual TPM instance
  23.441 -#by writing the instance number into the xenstore
  23.442 -#1st parm: name of virtual machine
  23.443 -#2nd parm: instance of associated virtual TPM
  23.444 -function vtpm_add_and_activate() {
  23.445 -	local domid=$(vtpm_domid_from_name $1)
  23.446 -	local vtpm_uuid=$(vtpm_uuid_from_vmname $1)
  23.447 -	if [ "$vtpm_uuid" != "" -a "$domid" != "-1" ]; then
  23.448 -		vtpmdb_add_instance $vtpm_uuid $2
  23.449 -		xenstore-write backend/vtpm/$domid/0/instance $2
  23.450 -	fi
  23.451 -}
    24.1 --- a/tools/examples/vtpm-delete	Tue Nov 04 11:46:00 2008 +0000
    24.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    24.3 @@ -1,18 +0,0 @@
    24.4 -#!/bin/bash
    24.5 -
    24.6 -# This scripts must be called the following way:
    24.7 -# vtpm-delete <vtpm uuid>
    24.8 -# or
    24.9 -# vtpm-delete --vmname <vm name>
   24.10 -
   24.11 -dir=$(dirname "$0")
   24.12 -. "$dir/vtpm-common.sh"
   24.13 -
   24.14 -if [ "$1" == "--vmname" ]; then
   24.15 -	vtpm_uuid=$(vtpm_uuid_from_vmname $2)
   24.16 -	if [ "$vtpm_uuid" != "" ];then
   24.17 -		vtpm_delete_instance $vtpm_uuid
   24.18 -	fi
   24.19 -else
   24.20 -	vtpm_delete_instance $1
   24.21 -fi
    25.1 --- a/tools/examples/vtpm-hotplug-common.sh	Tue Nov 04 11:46:00 2008 +0000
    25.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    25.3 @@ -1,35 +0,0 @@
    25.4 -#
    25.5 -# Copyright (c) 2005 IBM Corporation
    25.6 -# Copyright (c) 2005 XenSource Ltd.
    25.7 -#
    25.8 -# This library is free software; you can redistribute it and/or
    25.9 -# modify it under the terms of version 2.1 of the GNU Lesser General Public
   25.10 -# License as published by the Free Software Foundation.
   25.11 -#
   25.12 -# This library is distributed in the hope that it will be useful,
   25.13 -# but WITHOUT ANY WARRANTY; without even the implied warranty of
   25.14 -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   25.15 -# Lesser General Public License for more details.
   25.16 -#
   25.17 -# You should have received a copy of the GNU Lesser General Public
   25.18 -# License along with this library; if not, write to the Free Software
   25.19 -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   25.20 -#
   25.21 -
   25.22 -dir=$(dirname "$0")
   25.23 -. "$dir/xen-hotplug-common.sh"
   25.24 -
   25.25 -findCommand "$@"
   25.26 -if [ "$command" != "online" ]  &&
   25.27 -   [ "$command" != "offline" ] &&
   25.28 -   [ "$command" != "add" ]     &&
   25.29 -   [ "$command" != "remove" ]
   25.30 -then
   25.31 -	log err "Invalid command: $command"
   25.32 -	exit 1
   25.33 -fi
   25.34 -
   25.35 -
   25.36 -XENBUS_PATH="${XENBUS_PATH:?}"
   25.37 -
   25.38 -. "$dir/vtpm-common.sh"
    26.1 --- a/tools/examples/vtpm-impl	Tue Nov 04 11:46:00 2008 +0000
    26.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    26.3 @@ -1,208 +0,0 @@
    26.4 -#!/bin/bash
    26.5 -# ===================================================================
    26.6 -# 
    26.7 -# Copyright (c) 2005, Intel Corp.
    26.8 -# All rights reserved.
    26.9 -#
   26.10 -# Redistribution and use in source and binary forms, with or without 
   26.11 -# modification, are permitted provided that the following conditions 
   26.12 -# are met:
   26.13 -#
   26.14 -#   * Redistributions of source code must retain the above copyright 
   26.15 -#     notice, this list of conditions and the following disclaimer.
   26.16 -#   * Redistributions in binary form must reproduce the above 
   26.17 -#     copyright notice, this list of conditions and the following 
   26.18 -#     disclaimer in the documentation and/or other materials provided 
   26.19 -#     with the distribution.
   26.20 -#   * Neither the name of Intel Corporation nor the names of its 
   26.21 -#     contributors may be used to endorse or promote products derived
   26.22 -#     from this software without specific prior written permission.
   26.23 -#
   26.24 -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
   26.25 -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
   26.26 -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
   26.27 -# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
   26.28 -# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26.29 -# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   26.30 -# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
   26.31 -# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26.32 -# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
   26.33 -# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
   26.34 -# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   26.35 -# OF THE POSSIBILITY OF SUCH DAMAGE.
   26.36 -# ===================================================================
   26.37 -
   26.38 -#            |        SRC        |    TAG  |      CMD SIZE     |        ORD       |mtype|strt
   26.39 -TPM_CMD_OPEN=\\x00\\x00\\x00\\x00\\x01\\xc1\\x00\\x00\\x00\\x11\\x01\\x00\\x00\\x01\\x01\\x01
   26.40 -TPM_CMD_RESM=\\x00\\x00\\x00\\x00\\x01\\xc1\\x00\\x00\\x00\\x11\\x01\\x00\\x00\\x01\\x01\\x02
   26.41 -TPM_CMD_CLOS=\\x00\\x00\\x00\\x00\\x01\\xc1\\x00\\x00\\x00\\x0e\\x01\\x00\\x00\\x02
   26.42 -TPM_CMD_DELE=\\x00\\x00\\x00\\x00\\x01\\xc1\\x00\\x00\\x00\\x0e\\x01\\x00\\x00\\x03
   26.43 -
   26.44 -TPM_TYPE_PVM=\\x01
   26.45 -TPM_TYPE_HVM=\\x02
   26.46 -
   26.47 -TPM_SUCCESS=00000000
   26.48 -
   26.49 -TX_VTPM_MANAGER=/var/vtpm/fifos/from_console.fifo
   26.50 -RX_VTPM_MANAGER=/var/vtpm/fifos/to_console.fifo
   26.51 -
   26.52 -VTPM_MIG=/usr/bin/vtpm_migrator
   26.53 -
   26.54 -# -------------------- Helpers for binary streams -----------
   26.55 -
   26.56 -function str_to_hex32() {
   26.57 - printf "%0.8x" $1
   26.58 -}
   26.59 -
   26.60 -function hex32_to_bin() {
   26.61 - local inst=$(str_to_hex32 $1);
   26.62 - 
   26.63 - local n1=`echo $inst | sed 's/\(..\)....../\\\\x\1/'`
   26.64 - local n2=`echo $inst | sed 's/..\(..\)..../\\\\x\1/'`
   26.65 - local n3=`echo $inst | sed 's/....\(..\)../\\\\x\1/'`
   26.66 - local n4=`echo $inst | sed 's/......\(..\)/\\\\x\1/'`
   26.67 -
   26.68 - echo "$n1$n2$n3$n4"
   26.69 -}
   26.70 -
   26.71 -function vtpm_manager_cmd() {
   26.72 - local cmd=$1;
   26.73 - local inst=$2;
   26.74 - local inst_bin=$(hex32_to_bin $inst);
   26.75 -
   26.76 - claim_lock vtpm_mgr
   26.77 -
   26.78 - #send cmd to vtpm_manager
   26.79 - printf "$cmd$inst_bin" > $TX_VTPM_MANAGER
   26.80 -
   26.81 - #recv response
   26.82 - set +e
   26.83 - local resp_hex=`dd skip=10 bs=1 count=4 if=$RX_VTPM_MANAGER 2> /dev/null | xxd -ps`
   26.84 - set -e
   26.85 -
   26.86 - release_lock vtpm_mgr
   26.87 -
   26.88 - #return whether the command was successful
   26.89 - if [ $resp_hex -ne $TPM_SUCCESS ]; then
   26.90 -   vtpm_fatal_error=1
   26.91 -   false
   26.92 -  else
   26.93 -   true
   26.94 - fi
   26.95 -}
   26.96 -
   26.97 -# Helper to get vm type to pass to vtpm_manager open/resume
   26.98 -function vtpm_get_type() {
   26.99 - local inst=$(xenstore_read $XENBUS_PATH/frontend-id)
  26.100 - local vm=$(xenstore_read /local/domain/$inst/vm)
  26.101 - if [ "$vm" != "" ]; then
  26.102 -  local ostype=$(xenstore-read $vm/image/ostype)
  26.103 -  if [ "$ostype" == "hvm" ]; then
  26.104 -   echo $TPM_TYPE_HVM;
  26.105 -  else
  26.106 -   echo $TPM_TYPE_PVM;
  26.107 -  fi
  26.108 - fi
  26.109 -}
  26.110 -
  26.111 -# ------------------ Command handlers -----------------
  26.112 -
  26.113 -# Create new vtpm instance & set it up for use
  26.114 -function vtpm_create () {
  26.115 - # Creation is handled implicitly by the manager on first setup
  26.116 - # so just set it up for use
  26.117 - $(vtpm_start $1)
  26.118 -}
  26.119 -
  26.120 -# Setup vtpm instance for use.
  26.121 -function vtpm_start() {
  26.122 - local vmtype=$(vtpm_get_type);
  26.123 - $(vtpm_manager_cmd $TPM_CMD_OPEN$vmtype $1)
  26.124 -}
  26.125 -
  26.126 -function vtpm_resume() {
  26.127 - local vmtype=$(vtpm_get_type);
  26.128 - $(vtpm_manager_cmd $TPM_CMD_RESM$vmtype $1)
  26.129 -}
  26.130 -
  26.131 -# Reset the vtpm AKA clear PCRs
  26.132 -function vtpm_reset() {
  26.133 - #not used by current implemenation
  26.134 - true
  26.135 -}
  26.136 -
  26.137 -# Shutdown the vtpm while the vm is down
  26.138 -# This could be a suspend of shutdown
  26.139 -# we cannot distinquish, so save the state
  26.140 -# and decide on startup if we should keep is
  26.141 -function vtpm_suspend() {
  26.142 - $(vtpm_manager_cmd $TPM_CMD_CLOS $1)
  26.143 -}
  26.144 -
  26.145 -
  26.146 -function vtpm_delete() {
  26.147 - local inst=$1
  26.148 - if $(vtpm_manager_cmd $TPM_CMD_DELE $inst); then
  26.149 -   rm -f /var/vtpm/vtpm_dm_$1.data
  26.150 -   true
  26.151 - else 
  26.152 -   vtpm_fatal_error=1
  26.153 -   false
  26.154 - fi
  26.155 -}
  26.156 -
  26.157 -# Perform a migration step. This function differentiates between migration
  26.158 -# to the local host or to a remote machine.
  26.159 -# Parameters:
  26.160 -# 1st: destination host to migrate to
  26.161 -# 2nd: name of the domain to migrate
  26.162 -# 3rd: the migration step to perform
  26.163 -function vtpm_migrate() {
  26.164 - local instance res
  26.165 -
  26.166 - instance=$(vtpmdb_find_instance $2)
  26.167 - if [ "$instance" == "" ]; then
  26.168 -  log err "VTPM Migratoin failed. Unable to translation of domain name"
  26.169 -  echo "Error: VTPM Migration failed while looking up instance number"
  26.170 - fi
  26.171 -
  26.172 - case "$3" in
  26.173 -  0)
  26.174 -   #Incicate migration supported
  26.175 -   echo "0" 
  26.176 -  ;;
  26.177 -
  26.178 -  1)
  26.179 -   # Get Public Key from Destination
  26.180 -   # Call vtpm_manager's migration part 1
  26.181 -   claim_lock vtpm_mgr
  26.182 -   $VTPM_MIG $1 $2 $instance $3
  26.183 -   release_lock vtpm_mgr
  26.184 -  ;;
  26.185 -
  26.186 -  2)
  26.187 -   # Call manager's migration step 2 and send result to destination
  26.188 -   # If successful remove from db
  26.189 -   claim_lock vtpm_mgr
  26.190 -   $VTPM_MIG $1 $2 $instance $3
  26.191 -   release_lock vtpm_mgr
  26.192 -  ;;
  26.193 -
  26.194 -  3)
  26.195 -   if `ps x | grep "$VTPM_MIG $1"`; then
  26.196 -    log err "VTPM Migration failed to complete."
  26.197 -    echo "Error: VTPM Migration failed to complete."
  26.198 -   fi
  26.199 -  ;;
  26.200 - esac
  26.201 - 
  26.202 -}
  26.203 -
  26.204 -
  26.205 -function vtpm_migrate_recover() {
  26.206 - echo "Error: Recovery not supported yet" 
  26.207 -}
  26.208 -
  26.209 -function vtpm_migrate_local() {
  26.210 - echo "Error: local vTPM migration not supported"
  26.211 -}
    27.1 --- a/tools/examples/vtpm-migration.sh	Tue Nov 04 11:46:00 2008 +0000
    27.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    27.3 @@ -1,19 +0,0 @@
    27.4 -#
    27.5 -# Copyright (c) 2005 IBM Corporation
    27.6 -#
    27.7 -# This library is free software; you can redistribute it and/or
    27.8 -# modify it under the terms of version 2.1 of the GNU Lesser General Public
    27.9 -# License as published by the Free Software Foundation.
   27.10 -#
   27.11 -# This library is distributed in the hope that it will be useful,
   27.12 -# but WITHOUT ANY WARRANTY; without even the implied warranty of
   27.13 -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   27.14 -# Lesser General Public License for more details.
   27.15 -#
   27.16 -# You should have received a copy of the GNU Lesser General Public
   27.17 -# License along with this library; if not, write to the Free Software
   27.18 -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   27.19 -#
   27.20 -
   27.21 -dir=$(dirname "$0")
   27.22 -. "$dir/vtpm-common.sh"
    28.1 --- a/tools/examples/xen-backend.agent	Tue Nov 04 11:46:00 2008 +0000
    28.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    28.3 @@ -1,39 +0,0 @@
    28.4 -#! /bin/bash
    28.5 -
    28.6 -PATH=/etc/xen/scripts:$PATH
    28.7 -
    28.8 -. /etc/xen/scripts/locking.sh
    28.9 -
   28.10 -claim_lock xenbus_hotplug_global
   28.11 -
   28.12 -case "$XENBUS_TYPE" in
   28.13 -  tap)
   28.14 -    /etc/xen/scripts/blktap "$ACTION"
   28.15 -    ;;
   28.16 -  vbd)
   28.17 -    /etc/xen/scripts/block "$ACTION"
   28.18 -    ;;
   28.19 -  vtpm)
   28.20 -    /etc/xen/scripts/vtpm "$ACTION"
   28.21 -    ;;
   28.22 -  vif)
   28.23 -    [ -n "$script" ] && $script "$ACTION"
   28.24 -    ;;
   28.25 -  vscsi)
   28.26 -    /etc/xen/scripts/vscsi "$ACTION"
   28.27 -    ;;
   28.28 -esac
   28.29 -
   28.30 -case "$ACTION" in
   28.31 -  add)
   28.32 -    ;;
   28.33 -  remove)
   28.34 -    /etc/xen/scripts/xen-hotplug-cleanup
   28.35 -    ;;
   28.36 -  online)
   28.37 -    ;;
   28.38 -  offline)
   28.39 -    ;;
   28.40 -esac
   28.41 -
   28.42 -release_lock xenbus_hotplug_global
    29.1 --- a/tools/examples/xen-backend.rules	Tue Nov 04 11:46:00 2008 +0000
    29.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    29.3 @@ -1,9 +0,0 @@
    29.4 -SUBSYSTEM=="xen-backend", KERNEL=="tap*", RUN+="/etc/xen/scripts/blktap $env{ACTION}"
    29.5 -SUBSYSTEM=="xen-backend", KERNEL=="vbd*", RUN+="/etc/xen/scripts/block $env{ACTION}"
    29.6 -SUBSYSTEM=="xen-backend", KERNEL=="vtpm*", RUN+="/etc/xen/scripts/vtpm $env{ACTION}"
    29.7 -SUBSYSTEM=="xen-backend", KERNEL=="vif*", ACTION=="online", RUN+="$env{script} online"
    29.8 -SUBSYSTEM=="xen-backend", KERNEL=="vif*", ACTION=="offline", RUN+="$env{script} offline"
    29.9 -SUBSYSTEM=="xen-backend", KERNEL=="vscsi*", RUN+="/etc/xen/scripts/vscsi $env{ACTION}"
   29.10 -SUBSYSTEM=="xen-backend", ACTION=="remove", RUN+="/etc/xen/scripts/xen-hotplug-cleanup"
   29.11 -KERNEL=="evtchn", NAME="xen/%k"
   29.12 -KERNEL=="blktap[0-9]*", NAME="xen/%k"
    30.1 --- a/tools/examples/xen-hotplug-cleanup	Tue Nov 04 11:46:00 2008 +0000
    30.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    30.3 @@ -1,22 +0,0 @@
    30.4 -#! /bin/bash
    30.5 -
    30.6 -dir=$(dirname "$0")
    30.7 -. "$dir/xen-hotplug-common.sh"
    30.8 -
    30.9 -# Claim the lock protecting /etc/xen/scripts/block.  This stops a race whereby
   30.10 -# paths in the store would disappear underneath that script as it attempted to
   30.11 -# read from the store checking for device sharing.
   30.12 -# Any other scripts that do similar things will have to have their lock
   30.13 -# claimed too.
   30.14 -# This is pretty horrible, but there's not really a nicer way of solving this.
   30.15 -claim_lock "block"
   30.16 -
   30.17 -# remove device frontend store entries
   30.18 -xenstore-rm -t \
   30.19 -  $(xenstore-read "$XENBUS_PATH/frontend" 2>/dev/null) 2>/dev/null || true
   30.20 -
   30.21 -# remove device backend store entries
   30.22 -xenstore-rm -t "$XENBUS_PATH"        2>/dev/null || true
   30.23 -xenstore-rm -t "error/$XENBUS_PATH"  2>/dev/null || true
   30.24 -
   30.25 -release_lock "block"
    31.1 --- a/tools/examples/xen-hotplug-common.sh	Tue Nov 04 11:46:00 2008 +0000
    31.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    31.3 @@ -1,93 +0,0 @@
    31.4 -#
    31.5 -# Copyright (c) 2005 XenSource Ltd.
    31.6 -#
    31.7 -# This library is free software; you can redistribute it and/or
    31.8 -# modify it under the terms of version 2.1 of the GNU Lesser General Public
    31.9 -# License as published by the Free Software Foundation.
   31.10 -#
   31.11 -# This library is distributed in the hope that it will be useful,
   31.12 -# but WITHOUT ANY WARRANTY; without even the implied warranty of
   31.13 -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   31.14 -# Lesser General Public License for more details.
   31.15 -#
   31.16 -# You should have received a copy of the GNU Lesser General Public
   31.17 -# License along with this library; if not, write to the Free Software
   31.18 -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   31.19 -#
   31.20 -
   31.21 -
   31.22 -dir=$(dirname "$0")
   31.23 -. "$dir/logging.sh"
   31.24 -. "$dir/xen-script-common.sh"
   31.25 -. "$dir/locking.sh"
   31.26 -
   31.27 -exec 2>>/var/log/xen/xen-hotplug.log
   31.28 -
   31.29 -export PATH="/sbin:/bin:/usr/bin:/usr/sbin:$PATH"
   31.30 -export LANG="POSIX"
   31.31 -unset $(set | grep ^LC_ | cut -d= -f1)
   31.32 -
   31.33 -fatal() {
   31.34 -  xenstore_write "$XENBUS_PATH/hotplug-error" "$*" \
   31.35 -                 "$XENBUS_PATH/hotplug-status" error
   31.36 -  log err "$@"
   31.37 -  exit 1
   31.38 -}
   31.39 -
   31.40 -success() {
   31.41 -  # Tell DevController that backend is "connected"
   31.42 -  xenstore_write "$XENBUS_PATH/hotplug-status" connected
   31.43 -}
   31.44 -
   31.45 -do_or_die() {
   31.46 -  "$@" || fatal "$@ failed"
   31.47 -}
   31.48 -
   31.49 -do_without_error() {
   31.50 -  "$@" 2>/dev/null || log debug "$@ failed"
   31.51 -}
   31.52 -
   31.53 -sigerr() {
   31.54 -  fatal "$0 failed; error detected."
   31.55 -}
   31.56 -
   31.57 -trap sigerr ERR
   31.58 -
   31.59 -
   31.60 -##
   31.61 -# xenstore_read <path>+
   31.62 -#
   31.63 -# Read each of the given paths, returning each result on a separate line, or
   31.64 -# exit this script if any of the paths is missing.
   31.65 -#
   31.66 -xenstore_read() {
   31.67 -  local v=$(xenstore-read "$@" || true)
   31.68 -  [ "$v" != "" ] || fatal "xenstore-read $@ failed."
   31.69 -  echo "$v"
   31.70 -}
   31.71 -
   31.72 -
   31.73 -##
   31.74 -# xenstore_read_default <path> <default>
   31.75 -#
   31.76 -# Read the given path, returning the value there or the given default if the
   31.77 -# path is not present.
   31.78 -#
   31.79 -xenstore_read_default() {
   31.80 -  xenstore-read "$1" 2>/dev/null || echo "$2"
   31.81 -}
   31.82 -
   31.83 -
   31.84 -##
   31.85 -# xenstore_write (<path> <value>)+
   31.86 -#
   31.87 -# Write each of the key/value pairs to the store, and exit this script if any
   31.88 -# such writing fails.
   31.89 -#
   31.90 -xenstore_write() {
   31.91 -  log debug "Writing $@ to xenstore."
   31.92 -  xenstore-write "$@" || fatal "Writing $@ to xenstore failed."
   31.93 -}
   31.94 -
   31.95 -
   31.96 -log debug "$@" "XENBUS_PATH=$XENBUS_PATH"
    32.1 --- a/tools/examples/xen-network-common.sh	Tue Nov 04 11:46:00 2008 +0000
    32.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    32.3 @@ -1,118 +0,0 @@
    32.4 -#
    32.5 -# Copyright (c) 2005 XenSource Ltd.
    32.6 -#
    32.7 -# This library is free software; you can redistribute it and/or
    32.8 -# modify it under the terms of version 2.1 of the GNU Lesser General Public
    32.9 -# License as published by the Free Software Foundation.
   32.10 -#
   32.11 -# This library is distributed in the hope that it will be useful,
   32.12 -# but WITHOUT ANY WARRANTY; without even the implied warranty of
   32.13 -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   32.14 -# Lesser General Public License for more details.
   32.15 -#
   32.16 -# You should have received a copy of the GNU Lesser General Public
   32.17 -# License along with this library; if not, write to the Free Software
   32.18 -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   32.19 -#
   32.20 -
   32.21 -
   32.22 -# Gentoo doesn't have ifup/ifdown, so we define appropriate alternatives.
   32.23 -
   32.24 -# Other platforms just use ifup / ifdown directly.
   32.25 -
   32.26 -##
   32.27 -# preiftransfer
   32.28 -#
   32.29 -# @param $1 The current name for the physical device, which is also the name
   32.30 -#           that the virtual device will take once the physical device has
   32.31 -#           been renamed.
   32.32 -
   32.33 -if ! which ifup >/dev/null 2>/dev/null
   32.34 -then
   32.35 -  preiftransfer()
   32.36 -  {
   32.37 -    true
   32.38 -  }
   32.39 -  ifup()
   32.40 -  {
   32.41 -    false
   32.42 -  }
   32.43 -  ifdown()
   32.44 -  {
   32.45 -    false
   32.46 -  }
   32.47 -else
   32.48 -  preiftransfer()
   32.49 -  {
   32.50 -    true
   32.51 -  }
   32.52 -fi
   32.53 -
   32.54 -
   32.55 -first_file()
   32.56 -{
   32.57 -  t="$1"
   32.58 -  shift
   32.59 -  for file in $@
   32.60 -  do
   32.61 -    if [ "$t" "$file" ]
   32.62 -    then
   32.63 -      echo "$file"
   32.64 -      return
   32.65 -    fi
   32.66 -  done
   32.67 -}
   32.68 -
   32.69 -find_dhcpd_conf_file()
   32.70 -{
   32.71 -  first_file -f /etc/dhcp3/dhcpd.conf /etc/dhcpd.conf
   32.72 -}
   32.73 -
   32.74 -
   32.75 -find_dhcpd_init_file()
   32.76 -{
   32.77 -  first_file -x /etc/init.d/{dhcp3-server,dhcp,dhcpd}
   32.78 -}
   32.79 -
   32.80 -find_dhcpd_arg_file()
   32.81 -{
   32.82 -  first_file -f /etc/sysconfig/dhcpd /etc/defaults/dhcp /etc/default/dhcp3-server
   32.83 -}
   32.84 -
   32.85 -# configure interfaces which act as pure bridge ports:
   32.86 -setup_bridge_port() {
   32.87 -    local dev="$1"
   32.88 -
   32.89 -    # take interface down ...
   32.90 -    ip link set ${dev} down
   32.91 -
   32.92 -    # ... and configure it
   32.93 -    ip addr flush ${dev}
   32.94 -}
   32.95 -
   32.96 -# Usage: create_bridge bridge
   32.97 -create_bridge () {
   32.98 -    local bridge=$1
   32.99 -
  32.100 -    # Don't create the bridge if it already exists.
  32.101 -    if [ ! -e "/sys/class/net/${bridge}/bridge" ]; then
  32.102 -	brctl addbr ${bridge}
  32.103 -	brctl stp ${bridge} off
  32.104 -	brctl setfd ${bridge} 0
  32.105 -    fi
  32.106 -}
  32.107 -
  32.108 -# Usage: add_to_bridge bridge dev
  32.109 -add_to_bridge () {
  32.110 -    local bridge=$1
  32.111 -    local dev=$2
  32.112 -
  32.113 -    # Don't add $dev to $bridge if it's already on a bridge.
  32.114 -    if [ -e "/sys/class/net/${bridge}/brif/${dev}" ]; then
  32.115 -	ip link set ${dev} up || true
  32.116 -	return
  32.117 -    fi
  32.118 -    brctl addif ${bridge} ${dev}
  32.119 -    ip link set ${dev} up
  32.120 -}
  32.121 -
    33.1 --- a/tools/examples/xen-script-common.sh	Tue Nov 04 11:46:00 2008 +0000
    33.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    33.3 @@ -1,44 +0,0 @@
    33.4 -#
    33.5 -# Copyright (c) 2005 XenSource Ltd.
    33.6 -#
    33.7 -# This library is free software; you can redistribute it and/or
    33.8 -# modify it under the terms of version 2.1 of the GNU Lesser General Public
    33.9 -# License as published by the Free Software Foundation.
   33.10 -#
   33.11 -# This library is distributed in the hope that it will be useful,
   33.12 -# but WITHOUT ANY WARRANTY; without even the implied warranty of
   33.13 -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   33.14 -# Lesser General Public License for more details.
   33.15 -#
   33.16 -# You should have received a copy of the GNU Lesser General Public
   33.17 -# License along with this library; if not, write to the Free Software
   33.18 -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   33.19 -#
   33.20 -
   33.21 -
   33.22 -set -e
   33.23 -
   33.24 -
   33.25 -evalVariables()
   33.26 -{
   33.27 -  for arg in "$@"
   33.28 -  do
   33.29 -    if expr 'index' "$arg" '=' '>' '1' >/dev/null
   33.30 -    then
   33.31 -      eval "$arg"
   33.32 -    fi
   33.33 -  done
   33.34 -}
   33.35 -
   33.36 -
   33.37 -findCommand()
   33.38 -{
   33.39 -  for arg in "$@"
   33.40 -  do
   33.41 -    if ! expr 'index' "$arg" '=' >/dev/null
   33.42 -    then
   33.43 -      command="$arg"
   33.44 -      return
   33.45 -    fi
   33.46 -  done
   33.47 -}
    34.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    34.2 +++ b/tools/hotplug/Linux/Makefile	Tue Nov 04 12:13:42 2008 +0000
    34.3 @@ -0,0 +1,97 @@
    34.4 +XEN_ROOT = ../../../
    34.5 +include $(XEN_ROOT)/tools/Rules.mk
    34.6 +
    34.7 +# Init scripts.
    34.8 +XEND_INITD = init.d/xend
    34.9 +XENDOMAINS_INITD = init.d/xendomains
   34.10 +XENDOMAINS_SYSCONFIG = init.d/sysconfig.xendomains
   34.11 +
   34.12 +# Xen configuration dir and configs to go there.
   34.13 +XEN_CONFIG_DIR = /etc/xen
   34.14 +
   34.15 +# Xen script dir and scripts to go there.
   34.16 +XEN_SCRIPT_DIR = /etc/xen/scripts
   34.17 +XEN_SCRIPTS = network-bridge vif-bridge
   34.18 +XEN_SCRIPTS += network-route vif-route
   34.19 +XEN_SCRIPTS += network-nat vif-nat
   34.20 +XEN_SCRIPTS += block
   34.21 +XEN_SCRIPTS += block-enbd block-nbd
   34.22 +XEN_SCRIPTS += blktap
   34.23 +XEN_SCRIPTS += vtpm vtpm-delete
   34.24 +XEN_SCRIPTS += xen-hotplug-cleanup
   34.25 +XEN_SCRIPTS += external-device-migrate
   34.26 +XEN_SCRIPTS += vscsi
   34.27 +XEN_SCRIPT_DATA = xen-script-common.sh locking.sh logging.sh
   34.28 +XEN_SCRIPT_DATA += xen-hotplug-common.sh xen-network-common.sh vif-common.sh
   34.29 +XEN_SCRIPT_DATA += block-common.sh vtpm-common.sh vtpm-hotplug-common.sh
   34.30 +XEN_SCRIPT_DATA += vtpm-migration.sh vtpm-impl
   34.31 +
   34.32 +XEN_HOTPLUG_DIR = /etc/hotplug
   34.33 +XEN_HOTPLUG_SCRIPTS = xen-backend.agent
   34.34 +
   34.35 +UDEV_RULES_DIR = /etc/udev
   34.36 +UDEV_RULES = xen-backend.rules
   34.37 +
   34.38 +DI = $(if $(DISTDIR),$(shell readlink -f $(DISTDIR)),)
   34.39 +DE = $(if $(DESTDIR),$(shell readlink -f $(DESTDIR)),)
   34.40 +ifeq ($(findstring $(DI),$(DE)),$(DI))
   34.41 +HOTPLUGS=install-hotplug install-udev
   34.42 +else
   34.43 +ifeq ($(shell [ -x /usr/bin/udevinfo ] && [ `/usr/bin/udevinfo -V | sed -e 's/^[^0-9]* \([0-9]\{1,\}\)[^0-9]\{0,\}/\1/'` -ge 059 ] && echo 1),1)
   34.44 +HOTPLUGS=install-udev
   34.45 +else
   34.46 +HOTPLUGS=install-hotplug
   34.47 +endif
   34.48 +endif
   34.49 +
   34.50 +.PHONY: all
   34.51 +all:
   34.52 +
   34.53 +.PHONY: build
   34.54 +build:
   34.55 +
   34.56 +.PHONY: install
   34.57 +install: all install-initd install-scripts $(HOTPLUGS)
   34.58 +
   34.59 +.PHONY: install-initd
   34.60 +install-initd:
   34.61 +	[ -d $(DESTDIR)/etc/init.d ] || $(INSTALL_DIR) $(DESTDIR)/etc/init.d
   34.62 +	[ -d $(DESTDIR)/etc/sysconfig ] || $(INSTALL_DIR) $(DESTDIR)/etc/sysconfig
   34.63 +	$(INSTALL_PROG) $(XEND_INITD) $(DESTDIR)/etc/init.d
   34.64 +	$(INSTALL_PROG) $(XENDOMAINS_INITD) $(DESTDIR)/etc/init.d
   34.65 +	$(INSTALL_PROG) $(XENDOMAINS_SYSCONFIG) $(DESTDIR)/etc/sysconfig/xendomains
   34.66 +
   34.67 +.PHONY: install-scripts
   34.68 +install-scripts:
   34.69 +	[ -d $(DESTDIR)$(XEN_SCRIPT_DIR) ] || \
   34.70 +		$(INSTALL_DIR) $(DESTDIR)$(XEN_SCRIPT_DIR)
   34.71 +	set -e; for i in $(XEN_SCRIPTS); \
   34.72 +	    do \
   34.73 +	    $(INSTALL_PROG) $$i $(DESTDIR)$(XEN_SCRIPT_DIR); \
   34.74 +	done
   34.75 +	set -e; for i in $(XEN_SCRIPT_DATA); \
   34.76 +	    do \
   34.77 +	    $(INSTALL_DATA) $$i $(DESTDIR)$(XEN_SCRIPT_DIR); \
   34.78 +	done
   34.79 +
   34.80 +.PHONY: install-hotplug
   34.81 +install-hotplug:
   34.82 +	[ -d $(DESTDIR)$(XEN_HOTPLUG_DIR) ] || \
   34.83 +		$(INSTALL_DIR) $(DESTDIR)$(XEN_HOTPLUG_DIR)
   34.84 +	set -e; for i in $(XEN_HOTPLUG_SCRIPTS); \
   34.85 +	    do \
   34.86 +	    $(INSTALL_PROG) $$i $(DESTDIR)$(XEN_HOTPLUG_DIR); \
   34.87 +	done
   34.88 +
   34.89 +.PHONY: install-udev
   34.90 +install-udev:
   34.91 +	[ -d $(DESTDIR)$(UDEV_RULES_DIR) ] || \
   34.92 +		$(INSTALL_DIR) $(DESTDIR)$(UDEV_RULES_DIR)/rules.d
   34.93 +	set -e; for i in $(UDEV_RULES); \
   34.94 +	    do \
   34.95 +	    $(INSTALL_DATA) $$i $(DESTDIR)$(UDEV_RULES_DIR); \
   34.96 +	    ln -sf ../$$i $(DESTDIR)$(UDEV_RULES_DIR)/rules.d; \
   34.97 +	done
   34.98 +
   34.99 +.PHONY: clean
  34.100 +clean:
    35.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    35.2 +++ b/tools/hotplug/Linux/blktap	Tue Nov 04 12:13:42 2008 +0000
    35.3 @@ -0,0 +1,93 @@
    35.4 +#!/bin/bash
    35.5 +
    35.6 +# Copyright (c) 2005, XenSource Ltd.
    35.7 +
    35.8 +dir=$(dirname "$0")
    35.9 +. "$dir/xen-hotplug-common.sh"
   35.10 +. "$dir/block-common.sh"
   35.11 +
   35.12 +findCommand "$@"
   35.13 +
   35.14 +##
   35.15 +# check_blktap_sharing file mode
   35.16 +#
   35.17 +# Perform the sharing check for the given blktap and mode.
   35.18 +#
   35.19 +check_blktap_sharing()
   35.20 +{
   35.21 +    local file="$1"
   35.22 +    local mode="$2"
   35.23 +
   35.24 +    local base_path="$XENBUS_BASE_PATH/$XENBUS_TYPE"
   35.25 +    for dom in $(xenstore-list "$base_path")
   35.26 +    do
   35.27 +        for dev in $(xenstore-list "$base_path/$dom")
   35.28 +        do
   35.29 +            params=$(xenstore_read "$base_path/$dom/$dev/params" | cut -d: -f2)
   35.30 +            if [ "$file" = "$params" ]
   35.31 +            then
   35.32 +
   35.33 +                if [ "$mode" = 'w' ]
   35.34 +                then
   35.35 +                    if ! same_vm "$dom" 
   35.36 +                    then
   35.37 +                        echo 'guest'
   35.38 +                        return
   35.39 +                    fi
   35.40 +                else 
   35.41 +                    local m=$(xenstore_read "$base_path/$dom/$dev/mode")
   35.42 +                    m=$(canonicalise_mode "$m")
   35.43 +
   35.44 +                    if [ "$m" = 'w' ] 
   35.45 +                    then
   35.46 +                        if ! same_vm "$dom"
   35.47 +                        then
   35.48 +                            echo 'guest'
   35.49 +                            return
   35.50 +                        fi
   35.51 +                    fi
   35.52 +                fi
   35.53 +            fi
   35.54 +        done
   35.55 +    done
   35.56 +
   35.57 +    echo 'ok'
   35.58 +}
   35.59 +
   35.60 +
   35.61 +t=$(xenstore_read_default "$XENBUS_PATH/type" 'MISSING')
   35.62 +if [ -n "$t" ]
   35.63 +then
   35.64 +    p=$(xenstore_read "$XENBUS_PATH/params")
   35.65 +    # if we have a ':', chew from head including :
   35.66 +    if echo $p | grep -q \:
   35.67 +    then
   35.68 +        p=${p#*:}
   35.69 +    fi
   35.70 +fi
   35.71 +# some versions of readlink cannot be passed a regular file
   35.72 +if [ -L "$p" ]; then
   35.73 +    file=$(readlink -f "$p") || fatal "$p link does not exist."
   35.74 +else
   35.75 +    file="$p"
   35.76 +fi
   35.77 +
   35.78 +if [ "$command" = 'add' ]
   35.79 +then
   35.80 +    [ -e "$file" ] || { fatal $file does not exist; }
   35.81 +
   35.82 +    FRONTEND_ID=$(xenstore_read "$XENBUS_PATH/frontend-id")
   35.83 +    FRONTEND_UUID=$(xenstore_read "/local/domain/$FRONTEND_ID/vm")
   35.84 +    mode=$(xenstore_read "$XENBUS_PATH/mode")
   35.85 +    mode=$(canonicalise_mode "$mode")
   35.86 +
   35.87 +    if [ "$mode" != '!' ] 
   35.88 +    then
   35.89 +        result=$(check_blktap_sharing "$file" "$mode")
   35.90 +        [ "$result" = 'ok' ] || ebusy "$file already in use by other domain"
   35.91 +    fi
   35.92 +
   35.93 +    success
   35.94 +fi
   35.95 +
   35.96 +exit 0
    36.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    36.2 +++ b/tools/hotplug/Linux/block	Tue Nov 04 12:13:42 2008 +0000
    36.3 @@ -0,0 +1,381 @@
    36.4 +#!/bin/bash
    36.5 +
    36.6 +dir=$(dirname "$0")
    36.7 +. "$dir/block-common.sh"
    36.8 +
    36.9 +expand_dev() {
   36.10 +  local dev
   36.11 +  case $1 in
   36.12 +  /*)
   36.13 +    dev=$1
   36.14 +    ;;
   36.15 +  *)
   36.16 +    dev=/dev/$1
   36.17 +    ;;
   36.18 +  esac
   36.19 +  echo -n $dev
   36.20 +}
   36.21 +
   36.22 +
   36.23 +##
   36.24 +# check_sharing device mode
   36.25 +#
   36.26 +# Check whether the device requested is already in use.  To use the device in
   36.27 +# read-only mode, it may be in use in read-only mode, but may not be in use in
   36.28 +# read-write anywhere at all.  To use the device in read-write mode, it must
   36.29 +# not be in use anywhere at all.
   36.30 +#
   36.31 +# Prints one of
   36.32 +#
   36.33 +#    'local': the device may not be used because it is mounted in the current
   36.34 +#             (i.e. the privileged domain) in a way incompatible with the
   36.35 +#             requested mode;
   36.36 +#    'guest': the device may not be used because it already mounted by a guest
   36.37 +#             in a way incompatible with the requested mode; or
   36.38 +#    'ok':    the device may be used.
   36.39 +#
   36.40 +check_sharing()
   36.41 +{
   36.42 +  local dev="$1"
   36.43 +  local mode="$2"
   36.44 +
   36.45 +  local devmm=$(device_major_minor "$dev")
   36.46 +  local file
   36.47 +
   36.48 +  if [ "$mode" = 'w' ]
   36.49 +  then
   36.50 +    toskip="^$"
   36.51 +  else
   36.52 +    toskip="^[^ ]* [^ ]* [^ ]* ro[, ]"
   36.53 +  fi
   36.54 +
   36.55 +  for file in $(cat /proc/mounts | grep -v "$toskip" | cut -f 1 -d ' ')
   36.56 +  do
   36.57 +    if [ -e "$file" ]
   36.58 +    then
   36.59 +      local d=$(device_major_minor "$file")
   36.60 +
   36.61 +      if [ "$d" = "$devmm" ]
   36.62 +      then
   36.63 +        echo 'local'
   36.64 +        return
   36.65 +      fi
   36.66 +    fi
   36.67 +  done
   36.68 +
   36.69 +  local base_path="$XENBUS_BASE_PATH/$XENBUS_TYPE"
   36.70 +  for dom in $(xenstore-list "$base_path")
   36.71 +  do
   36.72 +    for dev in $(xenstore-list "$base_path/$dom")
   36.73 +    do
   36.74 +      d=$(xenstore_read_default "$base_path/$dom/$dev/physical-device" "")
   36.75 +
   36.76 +      if [ "$d" = "$devmm" ]
   36.77 +      then
   36.78 +        if [ "$mode" = 'w' ]
   36.79 +        then
   36.80 +          if ! same_vm $dom
   36.81 +          then
   36.82 +            echo 'guest'
   36.83 +            return
   36.84 +          fi
   36.85 +        else
   36.86 +          local m=$(xenstore_read "$base_path/$dom/$dev/mode")
   36.87 +          m=$(canonicalise_mode "$m")
   36.88 +
   36.89 +          if [ "$m" = 'w' ]
   36.90 +          then
   36.91 +            if ! same_vm $dom
   36.92 +            then
   36.93 +              echo 'guest'
   36.94 +              return
   36.95 +            fi
   36.96 +          fi
   36.97 +        fi
   36.98 +      fi
   36.99 +    done
  36.100 +  done
  36.101 +
  36.102 +  echo 'ok'
  36.103 +}
  36.104 +
  36.105 +
  36.106 +##
  36.107 +# check_device_sharing dev mode
  36.108 +#
  36.109 +# Perform the sharing check for the given physical device and mode.
  36.110 +#
  36.111 +check_device_sharing()
  36.112 +{
  36.113 +  local dev="$1"
  36.114 +  local mode=$(canonicalise_mode "$2")
  36.115 +  local result
  36.116 +
  36.117 +  if [ "x$mode" = 'x!' ]
  36.118 +  then
  36.119 +    return 0
  36.120 +  fi
  36.121 +
  36.122 +  result=$(check_sharing "$dev" "$mode")
  36.123 +
  36.124 +  if [ "$result" != 'ok' ]
  36.125 +  then
  36.126 +    do_ebusy "Device $dev is mounted " "$mode" "$result"
  36.127 +  fi
  36.128 +}
  36.129 +
  36.130 +
  36.131 +##
  36.132 +# check_device_sharing file dev mode
  36.133 +#
  36.134 +# Perform the sharing check for the given file mounted through the given
  36.135 +# loopback interface, in the given mode.
  36.136 +#
  36.137 +check_file_sharing()
  36.138 +{
  36.139 +  local file="$1"
  36.140 +  local dev="$2"
  36.141 +  local mode="$3"
  36.142 +
  36.143 +  result=$(check_sharing "$dev" "$mode")
  36.144 +
  36.145 +  if [ "$result" != 'ok' ]
  36.146 +  then
  36.147 +    do_ebusy "File $file is loopback-mounted through $dev,
  36.148 +which is mounted " "$mode" "$result"
  36.149 +  fi
  36.150 +}
  36.151 +
  36.152 +
  36.153 +##
  36.154 +# do_ebusy prefix mode result
  36.155 +#
  36.156 +# Helper function for check_device_sharing check_file_sharing, calling ebusy
  36.157 +# with an error message constructed from the given prefix, mode, and result
  36.158 +# from a call to check_sharing.
  36.159 +#
  36.160 +do_ebusy()
  36.161 +{
  36.162 +  local prefix="$1"
  36.163 +  local mode="$2"
  36.164 +  local result="$3"
  36.165 +
  36.166 +  if [ "$result" = 'guest' ]
  36.167 +  then
  36.168 +    dom='a guest '
  36.169 +    when='now'
  36.170 +  else
  36.171 +    dom='the privileged '
  36.172 +    when='by a guest'
  36.173 +  fi
  36.174 +
  36.175 +  if [ "$mode" = 'w' ]
  36.176 +  then
  36.177 +    m1=''
  36.178 +    m2=''
  36.179 +  else
  36.180 +    m1='read-write '
  36.181 +    m2='read-only '
  36.182 +  fi
  36.183 +
  36.184 +  release_lock "block"
  36.185 +  ebusy \
  36.186 +"${prefix}${m1}in ${dom}domain,
  36.187 +and so cannot be mounted ${m2}${when}."
  36.188 +}
  36.189 +
  36.190 +
  36.191 +t=$(xenstore_read_default "$XENBUS_PATH/type" 'MISSING')
  36.192 +
  36.193 +case "$command" in
  36.194 +  add)
  36.195 +    phys=$(xenstore_read_default "$XENBUS_PATH/physical-device" 'MISSING')
  36.196 +    if [ "$phys" != 'MISSING' ]
  36.197 +    then
  36.198 +      # Depending upon the hotplug configuration, it is possible for this
  36.199 +      # script to be called twice, so just bail.
  36.200 +      exit 0
  36.201 +    fi
  36.202 +
  36.203 +    if [ -n "$t" ]
  36.204 +    then
  36.205 +      p=$(xenstore_read "$XENBUS_PATH/params")
  36.206 +      mode=$(xenstore_read "$XENBUS_PATH/mode")
  36.207 +    fi
  36.208 +
  36.209 +    case $t in 
  36.210 +      phy)
  36.211 +        dev=$(expand_dev $p)
  36.212 +        FRONTEND_ID=$(xenstore_read "$XENBUS_PATH/frontend-id")
  36.213 +        FRONTEND_UUID=$(xenstore_read_default \
  36.214 +            "/local/domain/$FRONTEND_ID/vm" 'unknown')
  36.215 +
  36.216 +        if [ -L "$dev" ]
  36.217 +        then
  36.218 +          dev=$(readlink -f "$dev") || fatal "$dev link does not exist."
  36.219 +        fi
  36.220 +        test -e "$dev" || fatal "$dev does not exist."
  36.221 +        test -b "$dev" || fatal "$dev is not a block device."
  36.222 +
  36.223 +        claim_lock "block"
  36.224 +        check_device_sharing "$dev" "$mode"
  36.225 +	write_dev "$dev"
  36.226 +        release_lock "block"
  36.227 +	exit 0
  36.228 +	;;
  36.229 +
  36.230 +      file)
  36.231 +        # Canonicalise the file, for sharing check comparison, and the mode
  36.232 +        # for ease of use here.
  36.233 +        file=$(readlink -f "$p") || fatal "$p does not exist."
  36.234 +        test -f "$file" || fatal "$file does not exist."
  36.235 +        mode=$(canonicalise_mode "$mode")
  36.236 +
  36.237 +        claim_lock "block"
  36.238 +
  36.239 +        if [ "$mode" = 'w' ] && ! stat "$file" -c %A | grep -q w
  36.240 +        then
  36.241 +          release_lock "block"
  36.242 +          ebusy \
  36.243 +"File $file is read-only, and so I will not
  36.244 +mount it read-write in a guest domain."
  36.245 +        fi
  36.246 +
  36.247 +        loopdev=''
  36.248 +        for dev in /dev/loop*
  36.249 +        do
  36.250 +          if [ ! -b "$dev" ]
  36.251 +          then
  36.252 +            continue
  36.253 +          fi
  36.254 +
  36.255 +          f=$(losetup "$dev" 2>/dev/null) || f=''
  36.256 +
  36.257 +          if [ "$f" ]
  36.258 +          then
  36.259 +            # $dev is in use.  Check sharing.
  36.260 +            if [ "x$mode" = 'x!' ]
  36.261 +            then
  36.262 +              continue
  36.263 +            fi
  36.264 +
  36.265 +            f=$(echo "$f" | sed -e 's/.*(\(.*\)).*/\1/g')
  36.266 +
  36.267 +            # $f is the filename, as read from losetup, but the loopback
  36.268 +            # driver truncates filenames at 64 characters, so we need to go
  36.269 +            # trawling through the store if it's longer than that.  Truncation
  36.270 +            # is indicated by an asterisk at the end of the filename.
  36.271 +            if expr index "$f" '*' >/dev/null
  36.272 +            then
  36.273 +              found=""
  36.274 +              for dom in $(xenstore-list "$XENBUS_BASE_PATH")
  36.275 +              do
  36.276 +                for domdev in $(xenstore-list "$XENBUS_BASE_PATH/$dom")
  36.277 +                do
  36.278 +                  d=$(xenstore_read_default \
  36.279 +                        "$XENBUS_BASE_PATH/$dom/$domdev/node" "")
  36.280 +                  if [ "$d" = "$dev" ]
  36.281 +                  then
  36.282 +                    f=$(xenstore_read "$XENBUS_BASE_PATH/$dom/$domdev/params")
  36.283 +                    found=1
  36.284 +                    break 2
  36.285 +                  fi
  36.286 +                done
  36.287 +              done
  36.288 +
  36.289 +              if [ ! "$found" ]
  36.290 +              then
  36.291 +                # This loopback device is in use by someone else, so skip it.
  36.292 +                log debug "Loopback sharing check skips device $dev."
  36.293 +                continue
  36.294 +              fi
  36.295 +            fi
  36.296 +
  36.297 +            # Canonicalise the filename for the comparison.
  36.298 +
  36.299 +            # I have seen this readlink fails because the filename given by
  36.300 +            # losetup is only the basename.  This cannot happen when the loop
  36.301 +            # device is set up through this script, because file is
  36.302 +            # canonicalised above, but it may happen when loop devices are set
  36.303 +            # up some other way.  This readlink may also conceivably fail if
  36.304 +            # the file backing this loop device has been removed.
  36.305 +
  36.306 +            # For maximum safety, in the case that $f does not resolve, we
  36.307 +            # assume that $file and $f are in the same directory.
  36.308 +
  36.309 +            # If you create a loopback filesystem, remove it and continue to
  36.310 +            # run on it, and then create another file with the same name, then
  36.311 +            # this check will block that -- don't do that.
  36.312 +
  36.313 +            # If you create loop devices through some other mechanism, use
  36.314 +            # relative filenames, and then use the same filename through this
  36.315 +            # script, then this check will block that -- don't do that either.
  36.316 +
  36.317 +            f=$(readlink -f "$f" || echo $(dirname "$file")/$(basename "$f"))
  36.318 +
  36.319 +
  36.320 +            if [ "$f" = "$file" ]
  36.321 +            then
  36.322 +              check_file_sharing "$file" "$dev" "$mode"
  36.323 +            fi
  36.324 +          else
  36.325 +            # $dev is not in use, so we'll remember it for use later; we want
  36.326 +            # to finish the sharing check first.
  36.327 +
  36.328 +            if [ "$loopdev" = '' ]
  36.329 +            then
  36.330 +              loopdev="$dev"
  36.331 +            fi
  36.332 +          fi
  36.333 +        done
  36.334 +
  36.335 +        if [ "$loopdev" = '' ]
  36.336 +        then
  36.337 +          release_lock "block"
  36.338 +          fatal 'Failed to find an unused loop device'
  36.339 +        fi
  36.340 +
  36.341 +        if LANG=C losetup -h 2>&1 | grep read-only >/dev/null
  36.342 +        then
  36.343 +          roflag="-$mode"; roflag="${roflag#-w}"; roflag="${roflag#-!}"
  36.344 +        else
  36.345 +          roflag=''
  36.346 +        fi
  36.347 +        do_or_die losetup $roflag "$loopdev" "$file"
  36.348 +        xenstore_write "$XENBUS_PATH/node" "$loopdev"
  36.349 +        write_dev "$loopdev"
  36.350 +        release_lock "block"
  36.351 +        exit 0
  36.352 +	;;
  36.353 +
  36.354 +      "")
  36.355 +        claim_lock "block"
  36.356 +        success
  36.357 +        release_lock "block"
  36.358 +	;;
  36.359 +    esac
  36.360 +    ;;
  36.361 +
  36.362 +  remove)
  36.363 +    case $t in 
  36.364 +      phy)
  36.365 +	exit 0
  36.366 +	;;
  36.367 +
  36.368 +      file)
  36.369 +        node=$(xenstore_read "$XENBUS_PATH/node")
  36.370 +	losetup -d "$node"
  36.371 +	exit 0
  36.372 +	;;
  36.373 +
  36.374 +      "")
  36.375 +        exit 0
  36.376 +	;;
  36.377 +    esac
  36.378 +    ;;
  36.379 +
  36.380 +esac
  36.381 +
  36.382 +# If we've reached here, $t is neither phy nor file, so fire a helper script.
  36.383 +[ -x /etc/xen/scripts/block-"$t" ] && \
  36.384 +  /etc/xen/scripts/block-"$t" "$command" $node
    37.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    37.2 +++ b/tools/hotplug/Linux/block-common.sh	Tue Nov 04 12:13:42 2008 +0000
    37.3 @@ -0,0 +1,116 @@
    37.4 +#
    37.5 +# Copyright (c) 2005 XenSource Ltd.
    37.6 +#
    37.7 +# This library is free software; you can redistribute it and/or
    37.8 +# modify it under the terms of version 2.1 of the GNU Lesser General Public
    37.9 +# License as published by the Free Software Foundation.
   37.10 +#
   37.11 +# This library is distributed in the hope that it will be useful,
   37.12 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
   37.13 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   37.14 +# Lesser General Public License for more details.
   37.15 +#
   37.16 +# You should have received a copy of the GNU Lesser General Public
   37.17 +# License along with this library; if not, write to the Free Software
   37.18 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   37.19 +#
   37.20 +
   37.21 +
   37.22 +dir=$(dirname "$0")
   37.23 +. "$dir/xen-hotplug-common.sh"
   37.24 +
   37.25 +findCommand "$@"
   37.26 +
   37.27 +if [ "$command" != "add" ] &&
   37.28 +   [ "$command" != "remove" ]
   37.29 +then
   37.30 +  log err "Invalid command: $command"
   37.31 +  exit 1
   37.32 +fi
   37.33 +
   37.34 +
   37.35 +XENBUS_PATH="${XENBUS_PATH:?}"
   37.36 +
   37.37 +
   37.38 +ebusy()
   37.39 +{
   37.40 +  xenstore_write "$XENBUS_PATH/hotplug-error" "$*" \
   37.41 +                 "$XENBUS_PATH/hotplug-status" busy
   37.42 +  log err "$@"
   37.43 +  exit 1
   37.44 +}
   37.45 +
   37.46 +
   37.47 +##
   37.48 +# Print the given device's major and minor numbers, written in hex and
   37.49 +# separated by a colon.
   37.50 +device_major_minor()
   37.51 +{
   37.52 +  stat -L -c %t:%T "$1"
   37.53 +}
   37.54 +
   37.55 +
   37.56 +##
   37.57 +# Write physical-device = MM,mm to the store, where MM and mm are the major 
   37.58 +# and minor numbers of device respectively.
   37.59 +#
   37.60 +# @param device The device from which major and minor numbers are read, which
   37.61 +#               will be written into the store.
   37.62 +#
   37.63 +write_dev() {
   37.64 +  local mm
   37.65 +  
   37.66 +  mm=$(device_major_minor "$1")
   37.67 + 
   37.68 +  if [ -z $mm ]
   37.69 +  then
   37.70 +    fatal "Backend device does not exist"
   37.71 +  fi
   37.72 + 
   37.73 +  xenstore_write "$XENBUS_PATH/physical-device" "$mm"
   37.74 +
   37.75 +  success
   37.76 +}
   37.77 +
   37.78 +
   37.79 +##
   37.80 +# canonicalise_mode mode
   37.81 +#
   37.82 +# Takes the given mode, which may be r, w, ro, rw, w!, or rw!, or variations
   37.83 +# thereof, and canonicalises them to one of
   37.84 +#
   37.85 +#   'r': perform checks for a new read-only mount;
   37.86 +#   'w': perform checks for a read-write mount; or
   37.87 +#   '!': perform no checks at all.
   37.88 +#
   37.89 +canonicalise_mode()
   37.90 +{
   37.91 +  local mode="$1"
   37.92 +
   37.93 +  if ! expr index "$mode" 'w' >/dev/null
   37.94 +  then
   37.95 +    echo 'r'
   37.96 +  elif ! expr index "$mode" '!' >/dev/null
   37.97 +  then
   37.98 +    echo 'w'
   37.99 +  else
  37.100 +    echo '!'
  37.101 +  fi
  37.102 +}
  37.103 +
  37.104 +
  37.105 +same_vm()
  37.106 +{
  37.107 +  local otherdom="$1"
  37.108 +  # Note that othervm can be MISSING here, because Xend will be racing with
  37.109 +  # the hotplug scripts -- the entries in /local/domain can be removed by
  37.110 +  # Xend before the hotplug scripts have removed the entry in
  37.111 +  # /local/domain/0/backend/.  In this case, we want to pretend that the
  37.112 +  # VM is the same as FRONTEND_UUID, because that way the 'sharing' will be
  37.113 +  # allowed.
  37.114 +  local othervm=$(xenstore_read_default "/local/domain/$otherdom/vm"         \
  37.115 +                  "$FRONTEND_UUID")
  37.116 +
  37.117 +  [ "$FRONTEND_UUID" = "$othervm" ]
  37.118 +}
  37.119 +
    38.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    38.2 +++ b/tools/hotplug/Linux/block-enbd	Tue Nov 04 12:13:42 2008 +0000
    38.3 @@ -0,0 +1,27 @@
    38.4 +#!/bin/bash
    38.5 +
    38.6 +# Usage: block-enbd [bind server ctl_port |unbind node]
    38.7 +#
    38.8 +# The node argument to unbind is the name of the device node we are to
    38.9 +# unbind.
   38.10 +#
   38.11 +# This assumes you're running a correctly configured server at the other end!
   38.12 +
   38.13 +dir=$(dirname "$0")
   38.14 +. "$dir/block-common.sh"
   38.15 +
   38.16 +case "$command" in
   38.17 +  add)
   38.18 +    for dev in /dev/nd*; do
   38.19 +      if nbd-client $2:$3 $dev; then
   38.20 +        write_dev $dev
   38.21 +        exit 0
   38.22 +      fi
   38.23 +    done
   38.24 +    exit 1
   38.25 +    ;;
   38.26 +  remove)
   38.27 +    nbd-client -d $2
   38.28 +    exit 0
   38.29 +    ;;
   38.30 +esac
    39.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    39.2 +++ b/tools/hotplug/Linux/block-nbd	Tue Nov 04 12:13:42 2008 +0000
    39.3 @@ -0,0 +1,27 @@
    39.4 +#!/bin/bash
    39.5 +
    39.6 +# Usage: block-nbd [bind server ctl_port |unbind node]
    39.7 +#
    39.8 +# The node argument to unbind is the name of the device node we are to
    39.9 +# unbind.
   39.10 +#
   39.11 +# This assumes you're running a correctly configured server at the other end!
   39.12 +
   39.13 +dir=$(dirname "$0")
   39.14 +. "$dir/block-common.sh"
   39.15 +
   39.16 +case "$command" in
   39.17 +  add)
   39.18 +    for dev in /dev/nbd*; do
   39.19 +      if nbd-client $2 $3 $dev; then
   39.20 +        write_dev $dev
   39.21 +        exit 0
   39.22 +      fi
   39.23 +    done
   39.24 +    exit 1
   39.25 +    ;;
   39.26 +  remove)
   39.27 +    nbd-client -d $2
   39.28 +    exit 0
   39.29 +    ;;
   39.30 +esac
    40.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    40.2 +++ b/tools/hotplug/Linux/external-device-migrate	Tue Nov 04 12:13:42 2008 +0000
    40.3 @@ -0,0 +1,98 @@
    40.4 +#!/bin/bash
    40.5 +
    40.6 +# Copyright (c) 2005 IBM Corporation
    40.7 +#
    40.8 +# This library is free software; you can redistribute it and/or
    40.9 +# modify it under the terms of version 2.1 of the GNU Lesser General Public
   40.10 +# License as published by the Free Software Foundation.
   40.11 +#
   40.12 +# This library is distributed in the hope that it will be useful,
   40.13 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
   40.14 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   40.15 +# Lesser General Public License for more details.
   40.16 +#
   40.17 +# You should have received a copy of the GNU Lesser General Public
   40.18 +# License along with this library; if not, write to the Free Software
   40.19 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   40.20 +#
   40.21 +
   40.22 +set -x
   40.23 +
   40.24 +# This script is called by XenD for migration of external devices
   40.25 +# It does not handle the migration of those devices itself, but
   40.26 +# passes the requests on to further applications
   40.27 +# It handles the low-level command line parsing and some of the
   40.28 +# synchronization
   40.29 +
   40.30 +dir=$(dirname "$0")
   40.31 +. "$dir/logging.sh"
   40.32 +
   40.33 +
   40.34 +function ext_dev_migrate_usage() {
   40.35 +cat <<EOF
   40.36 +Pass the following command line parameters to the script:
   40.37 +
   40.38 +-step <n>              : n-th migration step
   40.39 +-host <host>           : the destination host
   40.40 +-domname <domain name> : name of the domain that is migrating
   40.41 +-type <device type>    : the type of device that is migrating
   40.42 +-subtype <dev. subtype>: the subtype of the device
   40.43 +-recover               : indicates recovery request; an error
   40.44 +                         occurred during migration
   40.45 +-help                  : display this help screen
   40.46 +EOF
   40.47 +}
   40.48 +
   40.49 +# Parse the command line paramters. The following parameters must be
   40.50 +# passed as the first ones in the sequence:
   40.51 +#  -step       [required]
   40.52 +#  -host       [required]
   40.53 +#  -domname    [required]
   40.54 +#  -type       [required]
   40.55 +#  -subtype    [optional]
   40.56 +#  -recover    [optional]
   40.57 +# The remaining ones will be passed to the called function.
   40.58 +function evaluate_params()
   40.59 +{
   40.60 +	local step host domname typ recover filename func stype
   40.61 +	stype=""
   40.62 +	while [ $# -ge 1 ]; do
   40.63 +		case "$1" in
   40.64 +		-step)		step=$2; shift; shift;;
   40.65 +		-host)		host=$2; shift; shift;;
   40.66 +		-domname)	domname=$2; shift; shift;;
   40.67 +		-type)		typ=$2; shift; shift;;
   40.68 +		-subtype)	stype=$2; shift; shift;;
   40.69 +		-recover)	recover=1; shift;;
   40.70 +		-help)		ext_dev_migrate_usage; exit 0;;
   40.71 +		*)		break;;
   40.72 +		esac
   40.73 +	done
   40.74 +
   40.75 +	if [ "$step"    = "" -o \
   40.76 +	     "$host"    = "" -o \
   40.77 +	     "$typ"     = "" -o \
   40.78 +	     "$domname" = "" ]; then
   40.79 +	 	echo "Error: Parameter(s) missing (-step/-host/-type/-domname)" 1>&2
   40.80 +		echo "" 1>&2
   40.81 +		echo "$0 -help for usage." 1>&2
   40.82 +		exit 1
   40.83 +	fi
   40.84 +
   40.85 +	filename="$dir/$typ$stype-migration.sh"
   40.86 +	if [ ! -r $filename ]; then
   40.87 +		echo "Error: Could not find script '$filename'"
   40.88 +		return
   40.89 +	fi
   40.90 +	. "$filename"
   40.91 +
   40.92 +	if [ "$recover" = "1" ]; then
   40.93 +		func="$typ"_recover
   40.94 +		eval $func $host $domname $step $*
   40.95 +	else
   40.96 +		func="$typ"_migration_step
   40.97 +		eval $func $host $domname $step $*
   40.98 +	fi
   40.99 +}
  40.100 +
  40.101 +evaluate_params "$@"
    41.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    41.2 +++ b/tools/hotplug/Linux/init.d/sysconfig.xendomains	Tue Nov 04 12:13:42 2008 +0000
    41.3 @@ -0,0 +1,137 @@
    41.4 +## Path: System/xen
    41.5 +## Description: xen domain start/stop on boot
    41.6 +## Type: string
    41.7 +## Default: 
    41.8 +#
    41.9 +# The xendomains script can send SysRq requests to domains on shutdown.
   41.10 +# If you don't want to MIGRATE, SAVE, or SHUTDOWN, this may be a possibility
   41.11 +# to do a quick and dirty shutdown ("s e i u o") or at least sync the disks
   41.12 +# of the domains ("s").
   41.13 +#
   41.14 +XENDOMAINS_SYSRQ=""
   41.15 +
   41.16 +## Type: integer 
   41.17 +## Default: 100000
   41.18 +#
   41.19 +# If XENDOMAINS_SYSRQ is set, this variable determines how long to wait
   41.20 +# (in microseconds) after each SysRq, so the domain has a chance to react.
   41.21 +# If you want to a quick'n'dirty shutdown via SysRq, you may want to set
   41.22 +# it to a relatively high value (1200000).
   41.23 +#
   41.24 +XENDOMAINS_USLEEP=100000
   41.25 +
   41.26 +## Type: integer
   41.27 +## Default: 5000000
   41.28 +#
   41.29 +# When creating a guest domain, it is sensible to allow a little time for it
   41.30 +# to get started before creating another domain or proceeding through the
   41.31 +# boot process.  Without this, the booting guests will thrash the disk as they
   41.32 +# start up.  This timeout (in microseconds) specifies the delay after guest
   41.33 +# domain creation.
   41.34 +#
   41.35 +XENDOMAINS_CREATE_USLEEP=5000000
   41.36 +
   41.37 +## Type: string
   41.38 +## Default: ""
   41.39 +#
   41.40 +# Set this to a non-empty string if you want to migrate virtual machines
   41.41 +# on shutdown. The string will be passed to the xm migrate DOMID command
   41.42 +# as is: It should contain the target IP address of the physical machine
   41.43 +# to migrate to and optionally parameters like --live. Leave empty if
   41.44 +# you don't want to try virtual machine relocation on shutdown.
   41.45 +# If migration succeeds, neither SAVE nor SHUTDOWN will be executed for
   41.46 +# that domain.
   41.47 +#
   41.48 +XENDOMAINS_MIGRATE=""
   41.49 +
   41.50 +## Type: string
   41.51 +## Default: /var/lib/xen/save
   41.52 +#
   41.53 +# Directory to save running domains to when the system (dom0) is
   41.54 +# shut down. Will also be used to restore domains from if # XENDOMAINS_RESTORE
   41.55 +# is set (see below). Leave empty to disable domain saving on shutdown 
   41.56 +# (e.g. because you rather shut domains down).
   41.57 +# If domain saving does succeed, SHUTDOWN will not be executed.
   41.58 +#
   41.59 +XENDOMAINS_SAVE=/var/lib/xen/save
   41.60 +
   41.61 +## Type: string
   41.62 +## Default: "--halt --wait"
   41.63 +#
   41.64 +# If neither MIGRATE nor SAVE were enabled or if they failed, you can
   41.65 +# try to shut down a domain by sending it a shutdown request. To do this,
   41.66 +# set this to "--halt --wait". Omit the "--wait" flag to avoid waiting
   41.67 +# for the domain to be really down. Leave empty to skip domain shutdown.
   41.68 +#
   41.69 +XENDOMAINS_SHUTDOWN="--halt --wait"
   41.70 +
   41.71 +## Type: string
   41.72 +## Default: "--all --halt --wait"
   41.73 +#
   41.74 +# After we have gone over all virtual machines (resp. all automatically
   41.75 +# started ones, see XENDOMAINS_AUTO_ONLY below) in a loop and sent SysRq,
   41.76 +# migrated, saved and/or shutdown according to the settings above, we
   41.77 +# might want to shutdown the virtual machines that are still running
   41.78 +# for some reason or another. To do this, set this variable to
   41.79 +# "--all --halt --wait", it will be passed to xm shutdown.
   41.80 +# Leave it empty not to do anything special here.
   41.81 +# (Note: This will hit all virtual machines, even if XENDOMAINS_AUTO_ONLY
   41.82 +# is set.)
   41.83 +# 
   41.84 +XENDOMAINS_SHUTDOWN_ALL="--all --halt --wait"
   41.85 +
   41.86 +## Type: boolean
   41.87 +## Default: true
   41.88 +#
   41.89 +# This variable determines whether saved domains from XENDOMAINS_SAVE
   41.90 +# will be restored on system startup. 
   41.91 +#
   41.92 +XENDOMAINS_RESTORE=true
   41.93 +
   41.94 +## Type: string
   41.95 +## Default: /etc/xen/auto
   41.96 +#
   41.97 +# This variable sets the directory where domains configurations
   41.98 +# are stored that should be started on system startup automatically.
   41.99 +# Leave empty if you don't want to start domains automatically
  41.100 +# (or just don't place any xen domain config files in that dir).
  41.101 +# Note that the script tries to be clever if both RESTORE and AUTO are 
  41.102 +# set: It will first restore saved domains and then only start domains
  41.103 +# in AUTO which are not running yet. 
  41.104 +# Note that the name matching is somewhat fuzzy.
  41.105 +#
  41.106 +XENDOMAINS_AUTO=/etc/xen/auto
  41.107 +
  41.108 +## Type: boolean
  41.109 +## Default: false
  41.110 +# 
  41.111 +# If this variable is set to "true", only the domains started via config 
  41.112 +# files in XENDOMAINS_AUTO will be treated according to XENDOMAINS_SYSRQ,
  41.113 +# XENDOMAINS_MIGRATE, XENDOMAINS_SAVE, XENDMAINS_SHUTDOWN; otherwise
  41.114 +# all running domains will be. 
  41.115 +# Note that the name matching is somewhat fuzzy.
  41.116 +# 
  41.117 +XENDOMAINS_AUTO_ONLY=false
  41.118 +
  41.119 +## Type: integer
  41.120 +## Default: 300
  41.121 +#
  41.122 +# On xendomains stop, a number of xm commands (xm migrate, save, shutdown,
  41.123 +# shutdown --all) may be executed. In the worst case, these commands may
  41.124 +# stall forever, which will prevent a successful shutdown of the machine.
  41.125 +# If this variable is non-zero, the script will set up a watchdog timer
  41.126 +# for every of these xm commands and time it out after the number of seconds
  41.127 +# specified by this variable.
  41.128 +# Note that SHUTDOWN_ALL will not be called if no virtual machines or only
  41.129 +# zombies are still running, so you don't need to enable this timeout just
  41.130 +# for the zombie case.
  41.131 +# The setting should be large enough to make sure that migrate/save/shutdown
  41.132 +# can succeed. If you do live migrations, keep in mind that live migration
  41.133 +# of a 1GB machine over Gigabit ethernet may actually take something like
  41.134 +# 100s (assuming that live migration uses 10% of the network # bandwidth).
  41.135 +# Depending on the virtual machine, a shutdown may also require a significant
  41.136 +# amount of time. So better setup this variable to a huge number and hope the
  41.137 +# watchdog never fires.
  41.138 +#
  41.139 +XENDOMAINS_STOP_MAXWAIT=300
  41.140 +
    42.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    42.2 +++ b/tools/hotplug/Linux/init.d/xend	Tue Nov 04 12:13:42 2008 +0000
    42.3 @@ -0,0 +1,66 @@
    42.4 +#!/bin/bash
    42.5 +#
    42.6 +# xend		Script to start and stop the Xen control daemon.
    42.7 +#
    42.8 +# Author:       Keir Fraser <keir.fraser@cl.cam.ac.uk>
    42.9 +#
   42.10 +# chkconfig: 2345 98 01
   42.11 +# description: Starts and stops the Xen control daemon.
   42.12 +### BEGIN INIT INFO
   42.13 +# Provides:          xend
   42.14 +# Required-Start:    $syslog $remote_fs
   42.15 +# Should-Start:
   42.16 +# Required-Stop:     $syslog $remote_fs
   42.17 +# Should-Stop:
   42.18 +# Default-Start:     3 4 5
   42.19 +# Default-Stop:      0 1 2 6
   42.20 +# Default-Enabled:   yes
   42.21 +# Short-Description: Start/stop xend
   42.22 +# Description:       Starts and stops the Xen control daemon.
   42.23 +### END INIT INFO
   42.24 +
   42.25 +if ! grep -q "control_d" /proc/xen/capabilities ; then
   42.26 +	exit 0
   42.27 +fi
   42.28 +
   42.29 +# Wait for Xend to be up
   42.30 +function await_daemons_up
   42.31 +{
   42.32 +	i=1
   42.33 +	rets=10
   42.34 +	xend status
   42.35 +	while [ $? -ne 0 -a $i -lt $rets ]; do
   42.36 +	    sleep 1
   42.37 +	    echo -n .
   42.38 +	    i=$(($i + 1))
   42.39 +	    xend status
   42.40 +	done
   42.41 +}
   42.42 +
   42.43 +case "$1" in
   42.44 +  start)
   42.45 +	xend start
   42.46 +	await_daemons_up
   42.47 +	;;
   42.48 +  stop)
   42.49 +	xend stop
   42.50 +	;;
   42.51 +  status)
   42.52 +	xend status
   42.53 +	;;
   42.54 +  reload)
   42.55 +        xend reload
   42.56 +        ;;
   42.57 +  restart|force-reload)
   42.58 +	xend restart
   42.59 +	await_daemons_up
   42.60 +	;;
   42.61 +  *)
   42.62 +	# do not advertise unreasonable commands that there is no reason
   42.63 +	# to use with this device
   42.64 +	echo $"Usage: $0 {start|stop|status|restart|reload|force-reload}"
   42.65 +	exit 1
   42.66 +esac
   42.67 +
   42.68 +exit $?
   42.69 +
    43.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    43.2 +++ b/tools/hotplug/Linux/init.d/xendomains	Tue Nov 04 12:13:42 2008 +0000
    43.3 @@ -0,0 +1,531 @@
    43.4 +#!/bin/bash
    43.5 +#
    43.6 +# /etc/init.d/xendomains
    43.7 +# Start / stop domains automatically when domain 0 boots / shuts down.
    43.8 +#
    43.9 +# chkconfig: 345 99 00
   43.10 +# description: Start / stop Xen domains.
   43.11 +#
   43.12 +# This script offers fairly basic functionality.  It should work on Redhat
   43.13 +# but also on LSB-compliant SuSE releases and on Debian with the LSB package
   43.14 +# installed.  (LSB is the Linux Standard Base)
   43.15 +#
   43.16 +# Based on the example in the "Designing High Quality Integrated Linux
   43.17 +# Applications HOWTO" by Avi Alkalay
   43.18 +# <http://www.tldp.org/HOWTO/HighQuality-Apps-HOWTO/>
   43.19 +#
   43.20 +### BEGIN INIT INFO
   43.21 +# Provides:          xendomains
   43.22 +# Required-Start:    $syslog $remote_fs xend
   43.23 +# Should-Start:
   43.24 +# Required-Stop:     $syslog $remote_fs xend
   43.25 +# Should-Stop:
   43.26 +# Default-Start:     3 4 5
   43.27 +# Default-Stop:      0 1 2 6
   43.28 +# Default-Enabled:   yes
   43.29 +# Short-Description: Start/stop secondary xen domains
   43.30 +# Description:       Start / stop domains automatically when domain 0 
   43.31 +#                    boots / shuts down.
   43.32 +### END INIT INFO
   43.33 +
   43.34 +# Correct exit code would probably be 5, but it's enough 
   43.35 +# if xend complains if we're not running as privileged domain
   43.36 +if ! [ -e /proc/xen/privcmd ]; then
   43.37 +	exit 0
   43.38 +fi
   43.39 +
   43.40 +LOCKFILE=/var/lock/subsys/xendomains
   43.41 +XENDOM_CONFIG=/etc/sysconfig/xendomains
   43.42 +
   43.43 +test -r $XENDOM_CONFIG || { echo "$XENDOM_CONFIG not existing";
   43.44 +	if [ "$1" = "stop" ]; then exit 0;
   43.45 +	else exit 6; fi; }
   43.46 +
   43.47 +. $XENDOM_CONFIG
   43.48 +
   43.49 +# Use the SUSE rc_ init script functions;
   43.50 +# emulate them on LSB, RH and other systems
   43.51 +if test -e /etc/rc.status; then
   43.52 +    # SUSE rc script library
   43.53 +    . /etc/rc.status
   43.54 +else    
   43.55 +    _cmd=$1
   43.56 +    declare -a _SMSG
   43.57 +    if test "${_cmd}" = "status"; then
   43.58 +	_SMSG=(running dead dead unused unknown)
   43.59 +	_RC_UNUSED=3
   43.60 +    else
   43.61 +	_SMSG=(done failed failed missed failed skipped unused failed failed)
   43.62 +	_RC_UNUSED=6
   43.63 +    fi
   43.64 +    if test -e /etc/init.d/functions; then
   43.65 +	# REDHAT
   43.66 +	. /etc/init.d/functions
   43.67 +	echo_rc()
   43.68 +	{
   43.69 +	    #echo -n "  [${_SMSG[${_RC_RV}]}] "
   43.70 +	    if test ${_RC_RV} = 0; then
   43.71 +		success "  [${_SMSG[${_RC_RV}]}] "
   43.72 +	    else
   43.73 +		failure "  [${_SMSG[${_RC_RV}]}] "
   43.74 +	    fi
   43.75 +	}
   43.76 +    elif test -e /lib/lsb/init-functions; then
   43.77 +	# LSB    
   43.78 +    	. /lib/lsb/init-functions
   43.79 +        if alias log_success_msg >/dev/null 2>/dev/null; then
   43.80 +	  echo_rc()
   43.81 +	  {
   43.82 +	       echo "  [${_SMSG[${_RC_RV}]}] "
   43.83 +	  }
   43.84 +        else
   43.85 +	  echo_rc()
   43.86 +	  {
   43.87 +	    if test ${_RC_RV} = 0; then
   43.88 +		log_success_msg "  [${_SMSG[${_RC_RV}]}] "
   43.89 +	    else
   43.90 +		log_failure_msg "  [${_SMSG[${_RC_RV}]}] "
   43.91 +	    fi
   43.92 +	  }
   43.93 +        fi
   43.94 +    else    
   43.95 +	# emulate it
   43.96 +	echo_rc()
   43.97 +	{
   43.98 +	    echo "  [${_SMSG[${_RC_RV}]}] "
   43.99 +	}
  43.100 +    fi
  43.101 +    rc_reset() { _RC_RV=0; }
  43.102 +    rc_failed()
  43.103 +    {
  43.104 +	if test -z "$1"; then 
  43.105 +	    _RC_RV=1;
  43.106 +	elif test "$1" != "0"; then 
  43.107 +	    _RC_RV=$1; 
  43.108 +    	fi
  43.109 +	return ${_RC_RV}
  43.110 +    }
  43.111 +    rc_check()
  43.112 +    {
  43.113 +	return rc_failed $?
  43.114 +    }	
  43.115 +    rc_status()
  43.116 +    {
  43.117 +	rc_failed $?
  43.118 +	if test "$1" = "-r"; then _RC_RV=0; shift; fi
  43.119 +	if test "$1" = "-s"; then rc_failed 5; echo_rc; rc_failed 3; shift; fi
  43.120 +	if test "$1" = "-u"; then rc_failed ${_RC_UNUSED}; echo_rc; rc_failed 3; shift; fi
  43.121 +	if test "$1" = "-v"; then echo_rc; shift; fi
  43.122 +	if test "$1" = "-r"; then _RC_RV=0; shift; fi
  43.123 +	return ${_RC_RV}
  43.124 +    }
  43.125 +    rc_exit() { exit ${_RC_RV}; }
  43.126 +    rc_active() 
  43.127 +    {
  43.128 +	if test -z "$RUNLEVEL"; then read RUNLEVEL REST < <(/sbin/runlevel); fi
  43.129 +	if test -e /etc/init.d/S[0-9][0-9]${1}; then return 0; fi
  43.130 +	return 1
  43.131 +    }
  43.132 +fi
  43.133 +
  43.134 +if ! which usleep >&/dev/null
  43.135 +then
  43.136 +  usleep()
  43.137 +  {
  43.138 +    if [ -n "$1" ]
  43.139 +    then
  43.140 +      sleep $(( $1 / 1000000 ))
  43.141 +    fi
  43.142 +  }
  43.143 +fi
  43.144 +
  43.145 +# Reset status of this service
  43.146 +rc_reset
  43.147 +
  43.148 +##
  43.149 +# Returns 0 (success) if the given parameter names a directory, and that
  43.150 +# directory is not empty.
  43.151 +#
  43.152 +contains_something()
  43.153 +{
  43.154 +  if [ -d "$1" ] && [ `/bin/ls $1 | wc -l` -gt 0 ]
  43.155 +  then
  43.156 +    return 0
  43.157 +  else
  43.158 +    return 1
  43.159 +  fi
  43.160 +}
  43.161 +
  43.162 +# read name from xen config file
  43.163 +rdname()
  43.164 +{
  43.165 +    NM=$(xm create --quiet --dryrun --defconfig "$1" |
  43.166 +         sed -n 's/^.*(name \(.*\))$/\1/p')
  43.167 +}
  43.168 +
  43.169 +rdnames()
  43.170 +{
  43.171 +    NAMES=
  43.172 +    if ! contains_something "$XENDOMAINS_AUTO"
  43.173 +    then 
  43.174 +	return
  43.175 +    fi
  43.176 +    for dom in $XENDOMAINS_AUTO/*; do
  43.177 +	rdname $dom
  43.178 +	if test -z $NAMES; then 
  43.179 +	    NAMES=$NM; 
  43.180 +	else
  43.181 +	    NAMES="$NAMES|$NM"
  43.182 +	fi
  43.183 +    done
  43.184 +}
  43.185 +
  43.186 +parseln()
  43.187 +{
  43.188 +    if [[ "$1" =~ "\(domain" ]]; then
  43.189 +        name=;id=
  43.190 +    else if [[ "$1" =~ "\(name" ]]; then
  43.191 +        name=$(echo $1 | sed -e 's/^.*(name \(.*\))$/\1/')
  43.192 +    else if [[ "$1" =~ "\(domid" ]]; then
  43.193 +        id=$(echo $1 | sed -e 's/^.*(domid \(.*\))$/\1/')
  43.194 +    fi; fi; fi
  43.195 +
  43.196 +    [ -n "$name" -a -n "$id" ] && return 0 || return 1
  43.197 +}
  43.198 +
  43.199 +is_running()
  43.200 +{
  43.201 +    rdname $1
  43.202 +    RC=1
  43.203 +    name=;id=
  43.204 +    while read LN; do
  43.205 +	parseln "$LN" || continue
  43.206 +	if test $id = 0; then continue; fi
  43.207 +	case $name in 
  43.208 +	    ($NM)
  43.209 +		RC=0
  43.210 +		;;
  43.211 +	esac
  43.212 +    done < <(xm list -l | grep '(\(domain\|domid\|name\)')
  43.213 +    return $RC
  43.214 +}
  43.215 +
  43.216 +start() 
  43.217 +{
  43.218 +    if [ -f $LOCKFILE ]; then 
  43.219 +	echo -n "xendomains already running (lockfile exists)"
  43.220 +	return; 
  43.221 +    fi
  43.222 +
  43.223 +    saved_domains=" "
  43.224 +    if [ "$XENDOMAINS_RESTORE" = "true" ] &&
  43.225 +       contains_something "$XENDOMAINS_SAVE"
  43.226 +    then
  43.227 +	mkdir -p $(dirname "$LOCKFILE")
  43.228 +	touch $LOCKFILE
  43.229 +	echo -n "Restoring Xen domains:"
  43.230 +	saved_domains=`ls $XENDOMAINS_SAVE`
  43.231 +        for dom in $XENDOMAINS_SAVE/*; do
  43.232 +            if [ -f $dom ] ; then
  43.233 +                HEADER=`head -c 16 $dom | head -n 1 2> /dev/null`
  43.234 +                if [ $HEADER = "LinuxGuestRecord" ]; then
  43.235 +                    echo -n " ${dom##*/}"
  43.236 +                    xm restore $dom
  43.237 +                    if [ $? -ne 0 ]; then
  43.238 +                        rc_failed $?
  43.239 +                        echo -n '!'
  43.240 +                    else
  43.241 +                        # mv $dom ${dom%/*}/.${dom##*/}
  43.242 +                        rm $dom
  43.243 +                    fi
  43.244 +                fi
  43.245 +            fi
  43.246 +        done
  43.247 +	echo .
  43.248 +    fi
  43.249 +
  43.250 +    if contains_something "$XENDOMAINS_AUTO"
  43.251 +    then
  43.252 +	touch $LOCKFILE
  43.253 +	echo -n "Starting auto Xen domains:"
  43.254 +	# We expect config scripts for auto starting domains to be in
  43.255 +	# XENDOMAINS_AUTO - they could just be symlinks to files elsewhere
  43.256 +
  43.257 +	# Create all domains with config files in XENDOMAINS_AUTO.
  43.258 +	# TODO: We should record which domain name belongs 
  43.259 +	# so we have the option to selectively shut down / migrate later
  43.260 +	# If a domain statefile from $XENDOMAINS_SAVE matches a domain name
  43.261 +	# in $XENDOMAINS_AUTO, do not try to start that domain; if it didn't 
  43.262 +	# restore correctly it requires administrative attention.
  43.263 +	for dom in $XENDOMAINS_AUTO/*; do
  43.264 +	    echo -n " ${dom##*/}"
  43.265 +	    shortdom=$(echo $dom | sed -n 's/^.*\/\(.*\)$/\1/p')
  43.266 +	    echo $saved_domains | grep -w $shortdom > /dev/null
  43.267 +	    if [ $? -eq 0 ] || is_running $dom; then
  43.268 +		echo -n "(skip)"
  43.269 +	    else
  43.270 +		xm create --quiet --defconfig $dom
  43.271 +		if [ $? -ne 0 ]; then
  43.272 +		    rc_failed $?
  43.273 +		    echo -n '!'
  43.274 +		else
  43.275 +		    usleep $XENDOMAINS_CREATE_USLEEP
  43.276 +		fi
  43.277 +	    fi
  43.278 +	done
  43.279 +    fi	
  43.280 +}
  43.281 +
  43.282 +all_zombies()
  43.283 +{
  43.284 +    name=;id=
  43.285 +    while read LN; do
  43.286 +	parseln "$LN" || continue
  43.287 +	if test $id = 0; then continue; fi
  43.288 +	if test "$state" != "-b---d" -a "$state" != "-----d"; then
  43.289 +	    return 1;
  43.290 +	fi
  43.291 +    done < <(xm list -l | grep '(\(domain\|domid\|name\)')
  43.292 +    return 0
  43.293 +}
  43.294 +
  43.295 +# Wait for max $XENDOMAINS_STOP_MAXWAIT for xm $1 to finish;
  43.296 +# if it has not exited by that time kill it, so the init script will
  43.297 +# succeed within a finite amount of time; if $2 is nonnull, it will
  43.298 +# kill the command as well as soon as no domain (except for zombies)
  43.299 +# are left (used for shutdown --all).
  43.300 +watchdog_xm()
  43.301 +{
  43.302 +    if test -z "$XENDOMAINS_STOP_MAXWAIT" -o "$XENDOMAINS_STOP_MAXWAIT" = "0"; then
  43.303 +	exit
  43.304 +    fi
  43.305 +    usleep 20000
  43.306 +    for no in `seq 0 $XENDOMAINS_STOP_MAXWAIT`; do
  43.307 +	# exit if xm save/migrate/shutdown is finished
  43.308 +	PSAX=`ps axlw | grep "xm $1" | grep -v grep`
  43.309 +	if test -z "$PSAX"; then exit; fi
  43.310 +	echo -n "."; sleep 1
  43.311 +	# go to kill immediately if there's only zombies left
  43.312 +	if all_zombies && test -n "$2"; then break; fi
  43.313 +    done
  43.314 +    sleep 1
  43.315 +    read PSF PSUID PSPID PSPPID < <(echo "$PSAX")
  43.316 +    # kill xm $1
  43.317 +    kill $PSPID >/dev/null 2>&1
  43.318 +}
  43.319 +
  43.320 +stop()
  43.321 +{
  43.322 +    # Collect list of domains to shut down
  43.323 +    if test "$XENDOMAINS_AUTO_ONLY" = "true"; then
  43.324 +	rdnames
  43.325 +    fi
  43.326 +    echo -n "Shutting down Xen domains:"
  43.327 +    name=;id=
  43.328 +    while read LN; do
  43.329 +	parseln "$LN" || continue
  43.330 +	if test $id = 0; then continue; fi
  43.331 +	echo -n " $name"
  43.332 +	if test "$XENDOMAINS_AUTO_ONLY" = "true"; then
  43.333 +	    eval "
  43.334 +	    case \"\$name\" in
  43.335 +		($NAMES)
  43.336 +		    # nothing
  43.337 +		    ;;
  43.338 +		(*)
  43.339 +		    echo -n '(skip)'
  43.340 +		    continue
  43.341 +		    ;;
  43.342 +	    esac
  43.343 +	    "
  43.344 +	fi
  43.345 +	# XENDOMAINS_SYSRQ chould be something like just "s" 
  43.346 +	# or "s e i u" or even "s e s i u o"
  43.347 +	# for the latter, you should set XENDOMAINS_USLEEP to 1200000 or so
  43.348 +	if test -n "$XENDOMAINS_SYSRQ"; then
  43.349 +	    for sysrq in $XENDOMAINS_SYSRQ; do
  43.350 +		echo -n "(SR-$sysrq)"
  43.351 +		xm sysrq $id $sysrq
  43.352 +		if test $? -ne 0; then
  43.353 +		    rc_failed $?
  43.354 +		    echo -n '!'
  43.355 +		fi
  43.356 +		# usleep just ignores empty arg
  43.357 +		usleep $XENDOMAINS_USLEEP
  43.358 +	    done
  43.359 +	fi
  43.360 +	if test "$state" = "-b---d" -o "$state" = "-----d"; then
  43.361 +	    echo -n "(zomb)"
  43.362 +	    continue
  43.363 +	fi
  43.364 +	if test -n "$XENDOMAINS_MIGRATE"; then
  43.365 +	    echo -n "(migr)"
  43.366 +	    watchdog_xm migrate &
  43.367 +	    WDOG_PID=$!
  43.368 +	    xm migrate $id $XENDOMAINS_MIGRATE
  43.369 +	    if test $? -ne 0; then
  43.370 +		rc_failed $?
  43.371 +		echo -n '!'
  43.372 +		kill $WDOG_PID >/dev/null 2>&1
  43.373 +	    else
  43.374 +		kill $WDOG_PID >/dev/null 2>&1
  43.375 +		continue
  43.376 +	    fi
  43.377 +	fi
  43.378 +	if test -n "$XENDOMAINS_SAVE"; then
  43.379 +	    echo -n "(save)"
  43.380 +	    watchdog_xm save &
  43.381 +	    WDOG_PID=$!
  43.382 +	    mkdir -p "$XENDOMAINS_SAVE"
  43.383 +	    xm save $id $XENDOMAINS_SAVE/$name
  43.384 +	    if test $? -ne 0; then
  43.385 +		rc_failed $?
  43.386 +		echo -n '!'
  43.387 +		kill $WDOG_PID >/dev/null 2>&1
  43.388 +	    else
  43.389 +		kill $WDOG_PID >/dev/null 2>&1
  43.390 +		continue
  43.391 +	    fi
  43.392 +	fi
  43.393 +	if test -n "$XENDOMAINS_SHUTDOWN"; then
  43.394 +	    # XENDOMAINS_SHUTDOWN should be "--halt --wait"
  43.395 +	    echo -n "(shut)"
  43.396 +	    watchdog_xm shutdown &
  43.397 +	    WDOG_PID=$!
  43.398 +	    xm shutdown $id $XENDOMAINS_SHUTDOWN
  43.399 +	    if test $? -ne 0; then
  43.400 +		rc_failed $?
  43.401 +		echo -n '!'
  43.402 +	    fi
  43.403 +	    kill $WDOG_PID >/dev/null 2>&1
  43.404 +	fi
  43.405 +    done < <(xm list -l | grep '(\(domain\|domid\|name\)')
  43.406 +
  43.407 +    # NB. this shuts down ALL Xen domains (politely), not just the ones in
  43.408 +    # AUTODIR/*
  43.409 +    # This is because it's easier to do ;-) but arguably if this script is run
  43.410 +    # on system shutdown then it's also the right thing to do.
  43.411 +    if ! all_zombies && test -n "$XENDOMAINS_SHUTDOWN_ALL"; then
  43.412 +	# XENDOMAINS_SHUTDOWN_ALL should be "--all --halt --wait"
  43.413 +	echo -n " SHUTDOWN_ALL "
  43.414 +	watchdog_xm shutdown 1 &
  43.415 +	WDOG_PID=$!
  43.416 +	xm shutdown $XENDOMAINS_SHUTDOWN_ALL
  43.417 +	if test $? -ne 0; then
  43.418 +	    rc_failed $?
  43.419 +	    echo -n '!'
  43.420 +	fi
  43.421 +	kill $WDOG_PID >/dev/null 2>&1
  43.422 +    fi
  43.423 +
  43.424 +    # Unconditionally delete lock file
  43.425 +    rm -f $LOCKFILE
  43.426 +}
  43.427 +
  43.428 +check_domain_up()
  43.429 +{
  43.430 +    name=;id=
  43.431 +    while read LN; do
  43.432 +	parseln "$LN" || continue
  43.433 +	if test $id = 0; then continue; fi
  43.434 +	case $name in 
  43.435 +	    ($1)
  43.436 +		return 0
  43.437 +		;;
  43.438 +	esac
  43.439 +    done < <(xm list -l | grep '(\(domain\|domid\|name\)')
  43.440 +    return 1
  43.441 +}
  43.442 +
  43.443 +check_all_auto_domains_up()
  43.444 +{
  43.445 +    if ! contains_something "$XENDOMAINS_AUTO"
  43.446 +    then
  43.447 +      return 0
  43.448 +    fi
  43.449 +    missing=
  43.450 +    for nm in $XENDOMAINS_AUTO/*; do
  43.451 +	rdname $nm
  43.452 +	found=0
  43.453 +	if check_domain_up "$NM"; then 
  43.454 +	    echo -n " $name"
  43.455 +	else 
  43.456 +	    missing="$missing $NM"
  43.457 +	fi
  43.458 +    done
  43.459 +    if test -n "$missing"; then
  43.460 +	echo -n " MISS AUTO:$missing"
  43.461 +	return 1
  43.462 +    fi
  43.463 +    return 0
  43.464 +}
  43.465 +
  43.466 +check_all_saved_domains_up()
  43.467 +{
  43.468 +    if ! contains_something "$XENDOMAINS_SAVE" 
  43.469 +    then
  43.470 +      return 0
  43.471 +    fi
  43.472 +    missing=`/bin/ls $XENDOMAINS_SAVE`
  43.473 +    echo -n " MISS SAVED: " $missing
  43.474 +    return 1
  43.475 +}
  43.476 +
  43.477 +# This does NOT necessarily restart all running domains: instead it
  43.478 +# stops all running domains and then boots all the domains specified in
  43.479 +# AUTODIR.  If other domains have been started manually then they will
  43.480 +# not get restarted.
  43.481 +# Commented out to avoid confusion!
  43.482 +
  43.483 +restart()
  43.484 +{
  43.485 +    stop
  43.486 +    start
  43.487 +}
  43.488 +
  43.489 +reload()
  43.490 +{
  43.491 +    restart
  43.492 +}
  43.493 +
  43.494 +
  43.495 +case "$1" in
  43.496 +    start)
  43.497 +	start
  43.498 +	rc_status
  43.499 +	if test -f $LOCKFILE; then rc_status -v; fi
  43.500 +	;;
  43.501 +
  43.502 +    stop)
  43.503 +	stop
  43.504 +	rc_status -v
  43.505 +	;;
  43.506 +
  43.507 +    restart)
  43.508 +	restart
  43.509 +	;;
  43.510 +    reload)
  43.511 +	reload
  43.512 +	;;
  43.513 +
  43.514 +    status)
  43.515 +	echo -n "Checking for xendomains:" 
  43.516 +	if test ! -f $LOCKFILE; then 
  43.517 +	    rc_failed 3
  43.518 +	else
  43.519 +	    check_all_auto_domains_up
  43.520 +	    rc_status
  43.521 +	    check_all_saved_domains_up
  43.522 +	    rc_status
  43.523 +	fi
  43.524 +	rc_status -v
  43.525 +	;;
  43.526 +
  43.527 +    *)
  43.528 +	echo "Usage: $0 {start|stop|restart|reload|status}"
  43.529 +	rc_failed 3
  43.530 +	rc_status -v
  43.531 +	;;
  43.532 +esac
  43.533 +
  43.534 +rc_exit
    44.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    44.2 +++ b/tools/hotplug/Linux/locking.sh	Tue Nov 04 12:13:42 2008 +0000
    44.3 @@ -0,0 +1,98 @@
    44.4 +#
    44.5 +# Copyright (c) 2005 XenSource Ltd.
    44.6 +#
    44.7 +# This library is free software; you can redistribute it and/or
    44.8 +# modify it under the terms of version 2.1 of the GNU Lesser General Public
    44.9 +# License as published by the Free Software Foundation.
   44.10 +#
   44.11 +# This library is distributed in the hope that it will be useful,
   44.12 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
   44.13 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   44.14 +# Lesser General Public License for more details.
   44.15 +#
   44.16 +# You should have received a copy of the GNU Lesser General Public
   44.17 +# License along with this library; if not, write to the Free Software
   44.18 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   44.19 +#
   44.20 +
   44.21 +#
   44.22 +# Serialisation
   44.23 +#
   44.24 +
   44.25 +LOCK_SLEEPTIME=1
   44.26 +LOCK_SPINNING_RETRIES=5
   44.27 +LOCK_RETRIES=100
   44.28 +LOCK_BASEDIR=/var/run/xen-hotplug
   44.29 +
   44.30 +
   44.31 +claim_lock()
   44.32 +{
   44.33 +  local lockdir="$LOCK_BASEDIR/$1"
   44.34 +  mkdir -p "$LOCK_BASEDIR"
   44.35 +  _claim_lock "$lockdir"
   44.36 +}
   44.37 +
   44.38 +
   44.39 +release_lock()
   44.40 +{
   44.41 +  _release_lock "$LOCK_BASEDIR/$1"
   44.42 +}
   44.43 +
   44.44 +
   44.45 +_claim_lock()
   44.46 +{
   44.47 +  local lockdir="$1"
   44.48 +  local owner=$(_lock_owner "$lockdir")
   44.49 +  local retries=0
   44.50 +
   44.51 +  while [ $retries -lt $LOCK_RETRIES ]
   44.52 +  do
   44.53 +    mkdir "$lockdir" 2>/dev/null && trap "release_lock $1; sigerr" ERR &&
   44.54 +      _update_lock_info "$lockdir" && return
   44.55 +
   44.56 +    local new_owner=$(_lock_owner "$lockdir")
   44.57 +    if [ "$new_owner" != "$owner" ]
   44.58 +    then
   44.59 +      owner="$new_owner"
   44.60 +      retries=0
   44.61 +    fi
   44.62 +
   44.63 +    if [ $retries -gt $LOCK_SPINNING_RETRIES ]
   44.64 +    then
   44.65 +      sleep $LOCK_SLEEPTIME
   44.66 +    else
   44.67 +      sleep 0
   44.68 +    fi
   44.69 +    retries=$(($retries + 1))
   44.70 +  done
   44.71 +  _steal_lock "$lockdir"
   44.72 +}
   44.73 +
   44.74 +
   44.75 +_release_lock()
   44.76 +{
   44.77 +  trap sigerr ERR
   44.78 +  rm -rf "$1" 2>/dev/null || true
   44.79 +}
   44.80 +
   44.81 +
   44.82 +_steal_lock()
   44.83 +{
   44.84 +  local lockdir="$1"
   44.85 +  local owner=$(cat "$lockdir/owner" 2>/dev/null || echo "unknown")
   44.86 +  log err "Forced to steal lock on $lockdir from $owner!"
   44.87 +  _release_lock "$lockdir"
   44.88 +  _claim_lock "$lockdir"
   44.89 +}
   44.90 +
   44.91 +
   44.92 +_lock_owner()
   44.93 +{
   44.94 +  cat "$1/owner" 2>/dev/null || echo "unknown"
   44.95 +}
   44.96 +
   44.97 +
   44.98 +_update_lock_info()
   44.99 +{
  44.100 +  echo "$$: $0" >"$1/owner"
  44.101 +}
    45.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    45.2 +++ b/tools/hotplug/Linux/logging.sh	Tue Nov 04 12:13:42 2008 +0000
    45.3 @@ -0,0 +1,22 @@
    45.4 +#
    45.5 +# Copyright (c) 2005 XenSource Ltd.
    45.6 +#
    45.7 +# This library is free software; you can redistribute it and/or
    45.8 +# modify it under the terms of version 2.1 of the GNU Lesser General Public
    45.9 +# License as published by the Free Software Foundation.
   45.10 +#
   45.11 +# This library is distributed in the hope that it will be useful,
   45.12 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
   45.13 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   45.14 +# Lesser General Public License for more details.
   45.15 +#
   45.16 +# You should have received a copy of the GNU Lesser General Public
   45.17 +# License along with this library; if not, write to the Free Software
   45.18 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   45.19 +#
   45.20 +
   45.21 +log() {
   45.22 +  local level="$1"
   45.23 +  shift
   45.24 +  logger -p "daemon.$level" -- "$0:" "$@" || echo "$0 $@" >&2
   45.25 +}
    46.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    46.2 +++ b/tools/hotplug/Linux/network-bridge	Tue Nov 04 12:13:42 2008 +0000
    46.3 @@ -0,0 +1,310 @@
    46.4 +#!/bin/bash
    46.5 +#============================================================================
    46.6 +# Default Xen network start/stop script.
    46.7 +# Xend calls a network script when it starts.
    46.8 +# The script name to use is defined in /etc/xen/xend-config.sxp
    46.9 +# in the network-script field.
   46.10 +#
   46.11 +# This script creates a bridge (default ${netdev}), adds a device
   46.12 +# (defaults to the device on the default gateway route) to it, copies
   46.13 +# the IP addresses from the device to the bridge and adjusts the routes
   46.14 +# accordingly.
   46.15 +#
   46.16 +# If all goes well, this should ensure that networking stays up.
   46.17 +# However, some configurations are upset by this, especially
   46.18 +# NFS roots. If the bridged setup does not meet your needs,
   46.19 +# configure a different script, for example using routing instead.
   46.20 +#
   46.21 +# Usage:
   46.22 +#
   46.23 +# network-bridge (start|stop|status) {VAR=VAL}*
   46.24 +#
   46.25 +# Vars:
   46.26 +#
   46.27 +# bridge     The bridge to use (default ${netdev}).
   46.28 +# netdev     The interface to add to the bridge (default gateway device).
   46.29 +# antispoof  Whether to use iptables to prevent spoofing (default no).
   46.30 +#
   46.31 +# Internal Vars:
   46.32 +# pdev="p${netdev}"
   46.33 +# tdev=tmpbridge
   46.34 +#
   46.35 +# start:
   46.36 +# Creates the bridge as tdev
   46.37 +# Copies the IP and MAC addresses from pdev to bridge
   46.38 +# Renames netdev to be pdev 
   46.39 +# Renames tdev to bridge
   46.40 +# Enslaves pdev to bridge
   46.41 +#
   46.42 +# stop:
   46.43 +# Removes pdev from the bridge
   46.44 +# Transfers addresses, routes from bridge to pdev
   46.45 +# Renames bridge to tdev
   46.46 +# Renames pdev to netdev 
   46.47 +# Deletes tdev
   46.48 +#
   46.49 +# status:
   46.50 +# Print addresses, interfaces, routes
   46.51 +#
   46.52 +#============================================================================
   46.53 +
   46.54 +
   46.55 +dir=$(dirname "$0")
   46.56 +. "$dir/xen-script-common.sh"
   46.57 +. "$dir/xen-network-common.sh"
   46.58 +
   46.59 +findCommand "$@"
   46.60 +evalVariables "$@"
   46.61 +
   46.62 +is_network_root () {
   46.63 +    local rootfs=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $3; }}' /etc/mtab)
   46.64 +    local rootopts=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $4; }}' /etc/mtab)
   46.65 +
   46.66 +    [[ "$rootfs" =~ "^nfs" ]] || [[ "$rootopts" =~ "_netdev" ]] && has_nfsroot=1 || has_nfsroot=0
   46.67 +    if [ $has_nfsroot -eq 1 ]; then
   46.68 +        local bparms=$(cat /proc/cmdline)
   46.69 +        for p in $bparms; do
   46.70 +            local ipaddr=$(echo $p | awk /nfsroot=/'{ print substr($1,9,index($1,":")-9) }')
   46.71 +            if [ "$ipaddr" != "" ]; then
   46.72 +                local nfsdev=$(ip route get $ipaddr | awk /$ipaddr/'{ print $3 }')
   46.73 +                [[ "$nfsdev" == "$netdev" ]] && return 0 || return 1
   46.74 +            fi
   46.75 +        done
   46.76 +    fi
   46.77 +    return 1
   46.78 +}
   46.79 +
   46.80 +find_alt_device () {
   46.81 +    local interf=$1
   46.82 +    local prefix=${interf%[[:digit:]]}
   46.83 +    local ifs=$(ip link show | grep " $prefix" |\
   46.84 +                gawk '{ printf ("%s",substr($2,1,length($2)-1)) }' |\
   46.85 +                sed s/$interf//)
   46.86 +    echo "$ifs"
   46.87 +}
   46.88 +
   46.89 +netdev=${netdev:-$(ip route list 0.0.0.0/0  | \
   46.90 +                   sed 's/.*dev \([a-z]\+[0-9]\+\).*$/\1/')}
   46.91 +if is_network_root ; then
   46.92 +    altdevs=$(find_alt_device $netdev)
   46.93 +    for netdev in $altdevs; do break; done
   46.94 +    if [ -z "$netdev" ]; then
   46.95 +        [ -x /usr/bin/logger ] && /usr/bin/logger "network-bridge: bridging not supported on network root; not starting"
   46.96 +        exit
   46.97 +    fi
   46.98 +fi
   46.99 +netdev=${netdev:-eth0}
  46.100 +bridge=${bridge:-${netdev}}
  46.101 +antispoof=${antispoof:-no}
  46.102 +
  46.103 +pdev="p${netdev}"
  46.104 +tdev=tmpbridge
  46.105 +
  46.106 +get_ip_info() {
  46.107 +    addr_pfx=`ip addr show dev $1 | egrep '^ *inet' | sed -e 's/ *inet //' -e 's/ .*//'`
  46.108 +    gateway=`ip route show dev $1 | fgrep default | sed 's/default via //'`
  46.109 +}
  46.110 +    
  46.111 +do_ifup() {
  46.112 +    if ! ifup $1 ; then
  46.113 +        if [ -n "$addr_pfx" ] ; then
  46.114 +            # use the info from get_ip_info()
  46.115 +            ip addr flush $1
  46.116 +            ip addr add ${addr_pfx} dev $1
  46.117 +            ip link set dev $1 up
  46.118 +            [ -n "$gateway" ] && ip route add default via ${gateway}
  46.119 +        fi
  46.120 +    fi
  46.121 +}
  46.122 +
  46.123 +# Usage: transfer_addrs src dst
  46.124 +# Copy all IP addresses (including aliases) from device $src to device $dst.
  46.125 +transfer_addrs () {
  46.126 +    local src=$1
  46.127 +    local dst=$2
  46.128 +    # Don't bother if $dst already has IP addresses.
  46.129 +    if ip addr show dev ${dst} | egrep -q '^ *inet ' ; then
  46.130 +        return
  46.131 +    fi
  46.132 +    # Address lines start with 'inet' and have the device in them.
  46.133 +    # Replace 'inet' with 'ip addr add' and change the device name $src
  46.134 +    # to 'dev $src'.
  46.135 +    ip addr show dev ${src} | egrep '^ *inet ' | sed -e "
  46.136 +s/inet/ip addr add/
  46.137 +s@\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+/[0-9]\+\)@\1@
  46.138 +s/${src}/dev ${dst} label ${dst}/
  46.139 +s/secondary//
  46.140 +" | sh -e
  46.141 +    # Remove automatic routes on destination device
  46.142 +    ip route list | sed -ne "
  46.143 +/dev ${dst}\( \|$\)/ {
  46.144 +  s/^/ip route del /
  46.145 +  p
  46.146 +}" | sh -e
  46.147 +}
  46.148 +
  46.149 +# Usage: transfer_routes src dst
  46.150 +# Get all IP routes to device $src, delete them, and
  46.151 +# add the same routes to device $dst.
  46.152 +# The original routes have to be deleted, otherwise adding them
  46.153 +# for $dst fails (duplicate routes).
  46.154 +transfer_routes () {
  46.155 +    local src=$1
  46.156 +    local dst=$2
  46.157 +    # List all routes and grep the ones with $src in.
  46.158 +    # Stick 'ip route del' on the front to delete.
  46.159 +    # Change $src to $dst and use 'ip route add' to add.
  46.160 +    ip route list | sed -ne "
  46.161 +/dev ${src}\( \|$\)/ {
  46.162 +  h
  46.163 +  s/^/ip route del /
  46.164 +  P
  46.165 +  g
  46.166 +  s/${src}/${dst}/
  46.167 +  s/^/ip route add /
  46.168 +  P
  46.169 +  d
  46.170 +}" | sh -e
  46.171 +}
  46.172 +
  46.173 +
  46.174 +##
  46.175 +# link_exists interface
  46.176 +#
  46.177 +# Returns 0 if the interface named exists (whether up or down), 1 otherwise.
  46.178 +#
  46.179 +link_exists()
  46.180 +{
  46.181 +    if ip link show "$1" >/dev/null 2>/dev/null
  46.182 +    then
  46.183 +        return 0
  46.184 +    else
  46.185 +        return 1
  46.186 +    fi
  46.187 +}
  46.188 +
  46.189 +# Set the default forwarding policy for $dev to drop.
  46.190 +# Allow forwarding to the bridge.
  46.191 +antispoofing () {
  46.192 +    iptables -P FORWARD DROP
  46.193 +    iptables -F FORWARD
  46.194 +    iptables -A FORWARD -m physdev --physdev-in ${pdev} -j ACCEPT
  46.195 +}
  46.196 +
  46.197 +# Usage: show_status dev bridge
  46.198 +# Print ifconfig and routes.
  46.199 +show_status () {
  46.200 +    local dev=$1
  46.201 +    local bridge=$2
  46.202 +    
  46.203 +    echo '============================================================'
  46.204 +    ip addr show ${dev}
  46.205 +    ip addr show ${bridge}
  46.206 +    echo ' '
  46.207 +    brctl show ${bridge}
  46.208 +    echo ' '
  46.209 +    ip route list
  46.210 +    echo ' '
  46.211 +    route -n
  46.212 +    echo '============================================================'
  46.213 +}
  46.214 +
  46.215 +op_start () {
  46.216 +    if [ "${bridge}" = "null" ] ; then
  46.217 +	return
  46.218 +    fi
  46.219 +
  46.220 +    if link_exists "$pdev"; then
  46.221 +        # The device is already up.
  46.222 +        return
  46.223 +    fi
  46.224 +
  46.225 +    create_bridge ${tdev}
  46.226 +
  46.227 +    preiftransfer ${netdev}
  46.228 +    transfer_addrs ${netdev} ${tdev}
  46.229 +    if ! ifdown ${netdev}; then
  46.230 +	# If ifdown fails, remember the IP details.
  46.231 +	get_ip_info ${netdev}
  46.232 +	ip link set ${netdev} down
  46.233 +	ip addr flush ${netdev}
  46.234 +    fi
  46.235 +    ip link set ${netdev} name ${pdev}
  46.236 +    ip link set ${tdev} name ${bridge}
  46.237 +
  46.238 +    setup_bridge_port ${pdev}
  46.239 +
  46.240 +    add_to_bridge2 ${bridge} ${pdev}
  46.241 +    do_ifup ${bridge}
  46.242 +
  46.243 +    if [ ${antispoof} = 'yes' ] ; then
  46.244 +	antispoofing
  46.245 +    fi
  46.246 +}
  46.247 +
  46.248 +op_stop () {
  46.249 +    if [ "${bridge}" = "null" ]; then
  46.250 +	return
  46.251 +    fi
  46.252 +    if ! link_exists "$bridge"; then
  46.253 +	return
  46.254 +    fi
  46.255 +
  46.256 +    transfer_addrs ${bridge} ${pdev}
  46.257 +    if ! ifdown ${bridge}; then
  46.258 +	get_ip_info ${bridge}
  46.259 +    fi
  46.260 +    ip link set ${pdev} down
  46.261 +    ip addr flush ${bridge}
  46.262 +
  46.263 +    brctl delif ${bridge} ${pdev}
  46.264 +    ip link set ${bridge} down
  46.265 +
  46.266 +    ip link set ${bridge} name ${tdev}
  46.267 +    ip link set ${pdev} name ${netdev}
  46.268 +    do_ifup ${netdev}
  46.269 +
  46.270 +    brctl delbr ${tdev}
  46.271 +}
  46.272 +
  46.273 +# adds $dev to $bridge but waits for $dev to be in running state first
  46.274 +add_to_bridge2() {
  46.275 +    local bridge=$1
  46.276 +    local dev=$2
  46.277 +    local maxtries=10
  46.278 +
  46.279 +    echo -n "Waiting for ${dev} to negotiate link."
  46.280 +    ip link set ${dev} up
  46.281 +    for i in `seq ${maxtries}` ; do
  46.282 +	if ifconfig ${dev} | grep -q RUNNING ; then
  46.283 +	    break
  46.284 +	else
  46.285 +	    echo -n '.'
  46.286 +	    sleep 1
  46.287 +	fi
  46.288 +    done
  46.289 +
  46.290 +    if [ ${i} -eq ${maxtries} ] ; then echo -n '(link isnt in running state)' ; fi
  46.291 +    echo
  46.292 +
  46.293 +    add_to_bridge ${bridge} ${dev}
  46.294 +}
  46.295 +
  46.296 +case "$command" in
  46.297 +    start)
  46.298 +	op_start
  46.299 +	;;
  46.300 +    
  46.301 +    stop)
  46.302 +	op_stop
  46.303 +	;;
  46.304 +
  46.305 +    status)
  46.306 +	show_status ${netdev} ${bridge}
  46.307 +	;;
  46.308 +
  46.309 +    *)
  46.310 +	echo "Unknown command: $command" >&2
  46.311 +	echo 'Valid commands are: start, stop, status' >&2
  46.312 +	exit 1
  46.313 +esac
    47.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    47.2 +++ b/tools/hotplug/Linux/network-nat	Tue Nov 04 12:13:42 2008 +0000
    47.3 @@ -0,0 +1,119 @@
    47.4 +#!/bin/bash -x
    47.5 +#============================================================================
    47.6 +# Default Xen network start/stop script when using NAT.
    47.7 +# Xend calls a network script when it starts.
    47.8 +# The script name to use is defined in /etc/xen/xend-config.sxp
    47.9 +# in the network-script field.
   47.10 +#
   47.11 +# Usage:
   47.12 +#
   47.13 +# network-nat (start|stop|status) {VAR=VAL}*
   47.14 +#
   47.15 +# Vars:
   47.16 +#
   47.17 +# netdev     The gateway interface (default eth0).
   47.18 +# antispoof  Whether to use iptables to prevent spoofing (default no).
   47.19 +# dhcp       Whether to alter the local DHCP configuration (default no).
   47.20 +#
   47.21 +#============================================================================
   47.22 +
   47.23 +dir=$(dirname "$0")
   47.24 +. "$dir/xen-script-common.sh"
   47.25 +. "$dir/xen-network-common.sh"
   47.26 +
   47.27 +findCommand "$@"
   47.28 +evalVariables "$@"
   47.29 +
   47.30 +netdev=${netdev:-eth0}
   47.31 +# antispoofing not yet implemented
   47.32 +antispoof=${antispoof:-no}
   47.33 +
   47.34 +# turn on dhcp feature by default if dhcpd is installed
   47.35 +if [ -f /etc/dhcpd.conf ]
   47.36 +then
   47.37 +	dhcp=${dhcp:-yes}
   47.38 +else
   47.39 +	dhcp=${dhcp:-no}
   47.40 +fi
   47.41 +
   47.42 +
   47.43 +if [ "$dhcp" != 'no' ]
   47.44 +then
   47.45 +  dhcpd_conf_file=$(find_dhcpd_conf_file)
   47.46 +  dhcpd_init_file=$(find_dhcpd_init_file)
   47.47 +  if [ -z "$dhcpd_conf_file" ] || [ -z "$dhcpd_init_file" ]
   47.48 +  then
   47.49 +    echo 'Failed to find dhcpd configuration or init file.' >&2
   47.50 +    exit 1
   47.51 +  fi
   47.52 +fi
   47.53 +
   47.54 +
   47.55 +function dhcp_start()
   47.56 +{
   47.57 +  if ! grep -q "subnet 10.0.0.0" "$dhcpd_conf_file"
   47.58 +  then
   47.59 +    echo >>"$dhcpd_conf_file" "subnet 10.0.0.0 netmask 255.255.0.0 {}"
   47.60 +  fi
   47.61 +
   47.62 +  "$dhcpd_init_file" restart
   47.63 +}
   47.64 +
   47.65 +
   47.66 +function dhcp_stop()
   47.67 +{
   47.68 +  local tmpfile=$(mktemp)
   47.69 +  grep -v "subnet 10.0.0.0" "$dhcpd_conf_file" >"$tmpfile"
   47.70 +  if diff "$tmpfile" "$dhcpd_conf_file" >&/dev/null
   47.71 +  then
   47.72 +    rm "$tmpfile"
   47.73 +  else
   47.74 +    mv "$tmpfile" "$dhcpd_conf_file"
   47.75 +  fi
   47.76 +
   47.77 +  "$dhcpd_init_file" restart
   47.78 +}
   47.79 +
   47.80 +
   47.81 +op_start() {
   47.82 +	echo 1 >/proc/sys/net/ipv4/ip_forward
   47.83 +	iptables -t nat -A POSTROUTING -o ${netdev} -j MASQUERADE
   47.84 +        [ "$dhcp" != 'no' ] && dhcp_start
   47.85 +}
   47.86 +
   47.87 +
   47.88 +op_stop() {
   47.89 +        [ "$dhcp" != 'no' ] && dhcp_stop
   47.90 +	iptables -t nat -D POSTROUTING -o ${netdev} -j MASQUERADE
   47.91 +}
   47.92 +
   47.93 +
   47.94 +show_status() {
   47.95 +    echo '============================================================'
   47.96 +    ifconfig
   47.97 +    echo ' '
   47.98 +    ip route list
   47.99 +    echo ' '
  47.100 +    route -n
  47.101 +    echo '============================================================'
  47.102 +
  47.103 +}
  47.104 +
  47.105 +case "$command" in
  47.106 +    start)
  47.107 +        op_start
  47.108 +        ;;
  47.109 +    
  47.110 +    stop)
  47.111 +        op_stop
  47.112 +        ;;
  47.113 +
  47.114 +    status)
  47.115 +        show_status
  47.116 +       ;;
  47.117 +
  47.118 +    *)
  47.119 +       echo "Unknown command: $command" >&2
  47.120 +       echo 'Valid commands are: start, stop, status' >&2
  47.121 +       exit 1
  47.122 +esac
    48.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    48.2 +++ b/tools/hotplug/Linux/network-route	Tue Nov 04 12:13:42 2008 +0000
    48.3 @@ -0,0 +1,27 @@
    48.4 +#!/bin/bash
    48.5 +#============================================================================
    48.6 +# Default Xen network start/stop script.
    48.7 +# Xend calls a network script when it starts.
    48.8 +# The script name to use is defined in /etc/xen/xend-config.sxp
    48.9 +# in the network-script field.
   48.10 +#
   48.11 +# Usage:
   48.12 +#
   48.13 +# network-route (start|stop|status) {VAR=VAL}*
   48.14 +#
   48.15 +# Vars:
   48.16 +#
   48.17 +# netdev     The gateway interface (default eth0).
   48.18 +# antispoof  Whether to use iptables to prevent spoofing (default yes).
   48.19 +#
   48.20 +#============================================================================
   48.21 +
   48.22 +dir=$(dirname "$0")
   48.23 +. "$dir/xen-script-common.sh"
   48.24 +
   48.25 +evalVariables "$@"
   48.26 +
   48.27 +netdev=${netdev:-eth${vifnum}}
   48.28 +
   48.29 +echo 1 >/proc/sys/net/ipv4/ip_forward
   48.30 +echo 1 >/proc/sys/net/ipv4/conf/${netdev}/proxy_arp
    49.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    49.2 +++ b/tools/hotplug/Linux/vif-bridge	Tue Nov 04 12:13:42 2008 +0000
    49.3 @@ -0,0 +1,100 @@
    49.4 +#!/bin/bash
    49.5 +#============================================================================
    49.6 +# /etc/xen/vif-bridge
    49.7 +#
    49.8 +# Script for configuring a vif in bridged mode.
    49.9 +# The hotplugging system will call this script if it is specified either in
   49.10 +# the device configuration given to Xend, or the default Xend configuration
   49.11 +# in /etc/xen/xend-config.sxp.  If the script is specified in neither of those
   49.12 +# places, then this script is the default.
   49.13 +#
   49.14 +# Usage:
   49.15 +# vif-bridge (add|remove|online|offline)
   49.16 +#
   49.17 +# Environment vars:
   49.18 +# vif         vif interface name (required).
   49.19 +# XENBUS_PATH path to this device's details in the XenStore (required).
   49.20 +#
   49.21 +# Read from the store:
   49.22 +# bridge  bridge to add the vif to (optional).  Defaults to searching for the
   49.23 +#         bridge itself.
   49.24 +# ip      list of IP networks for the vif, space-separated (optional).
   49.25 +#
   49.26 +# up:
   49.27 +# Enslaves the vif interface to the bridge and adds iptables rules
   49.28 +# for its ip addresses (if any).
   49.29 +#
   49.30 +# down:
   49.31 +# Removes the vif interface from the bridge and removes the iptables
   49.32 +# rules for its ip addresses (if any).
   49.33 +#============================================================================
   49.34 +
   49.35 +dir=$(dirname "$0")
   49.36 +. "$dir/vif-common.sh"
   49.37 +
   49.38 +bridge=${bridge:-}
   49.39 +bridge=$(xenstore_read_default "$XENBUS_PATH/bridge" "$bridge")
   49.40 +
   49.41 +if [ -z "$bridge" ]
   49.42 +then
   49.43 +  bridge=$(brctl show | cut -d "
   49.44 +" -f 2 | cut -f 1)
   49.45 +
   49.46 +  if [ -z "$bridge" ]
   49.47 +  then
   49.48 +     fatal "Could not find bridge, and none was specified"
   49.49 +  fi
   49.50 +else
   49.51 +  #
   49.52 +  # Old style bridge setup with netloop, used to have a bridge name
   49.53 +  # of xenbrX, enslaving pethX and vif0.X, and then configuring
   49.54 +  # eth0.
   49.55 +  #
   49.56 +  # New style bridge setup does not use netloop, so the bridge name
   49.57 +  # is ethX and the physical device is enslaved pethX
   49.58 +  #
   49.59 +  # So if...
   49.60 +  #
   49.61 +  #   - User asks for xenbrX
   49.62 +  #   - AND xenbrX doesn't exist
   49.63 +  #   - AND there is a ethX device which is a bridge
   49.64 +  #
   49.65 +  # ..then we translate xenbrX to ethX
   49.66 +  #
   49.67 +  # This lets old config files work without modification
   49.68 +  #
   49.69 +  if [ ! -e "/sys/class/net/$bridge" ] && [ -z "${bridge##xenbr*}" ]
   49.70 +  then
   49.71 +     if [ -e "/sys/class/net/eth${bridge#xenbr}/bridge" ]
   49.72 +     then
   49.73 +        bridge="eth${bridge#xenbr}"
   49.74 +     fi
   49.75 +  fi
   49.76 +fi
   49.77 +
   49.78 +RET=0
   49.79 +ip link show $bridge 1>/dev/null 2>&1 || RET=1
   49.80 +if [ "$RET" -eq 1 ]
   49.81 +then
   49.82 +    fatal "Could not find bridge device $bridge"
   49.83 +fi
   49.84 +
   49.85 +case "$command" in
   49.86 +    online)
   49.87 +	setup_bridge_port "$vif"
   49.88 +	add_to_bridge "$bridge" "$vif"
   49.89 +        ;;
   49.90 +
   49.91 +    offline)
   49.92 +        do_without_error brctl delif "$bridge" "$vif"
   49.93 +        do_without_error ifconfig "$vif" down
   49.94 +        ;;
   49.95 +esac
   49.96 +
   49.97 +handle_iptable
   49.98 +
   49.99 +log debug "Successful vif-bridge $command for $vif, bridge $bridge."
  49.100 +if [ "$command" == "online" ]
  49.101 +then
  49.102 +  success
  49.103 +fi
    50.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    50.2 +++ b/tools/hotplug/Linux/vif-common.sh	Tue Nov 04 12:13:42 2008 +0000
    50.3 @@ -0,0 +1,151 @@
    50.4 +#
    50.5 +# Copyright (c) 2005 XenSource Ltd.
    50.6 +#
    50.7 +# This library is free software; you can redistribute it and/or
    50.8 +# modify it under the terms of version 2.1 of the GNU Lesser General Public
    50.9 +# License as published by the Free Software Foundation.
   50.10 +#
   50.11 +# This library is distributed in the hope that it will be useful,
   50.12 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
   50.13 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   50.14 +# Lesser General Public License for more details.
   50.15 +#
   50.16 +# You should have received a copy of the GNU Lesser General Public
   50.17 +# License along with this library; if not, write to the Free Software
   50.18 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   50.19 +#
   50.20 +
   50.21 +
   50.22 +dir=$(dirname "$0")
   50.23 +. "$dir/xen-hotplug-common.sh"
   50.24 +. "$dir/xen-network-common.sh"
   50.25 +
   50.26 +findCommand "$@"
   50.27 +
   50.28 +if [ "$command" != "online" ]  &&
   50.29 +   [ "$command" != "offline" ] &&
   50.30 +   [ "$command" != "add" ]     &&
   50.31 +   [ "$command" != "remove" ]
   50.32 +then
   50.33 +  log err "Invalid command: $command"
   50.34 +  exit 1
   50.35 +fi
   50.36 +
   50.37 +case "$command" in
   50.38 +    add | remove)
   50.39 +        exit 0
   50.40 +        ;;
   50.41 +esac
   50.42 +
   50.43 +
   50.44 +# Parameters may be read from the environment, the command line arguments, and
   50.45 +# the store, with overriding in that order.  The environment is given by the
   50.46 +# driver, the command line is given by the Xend global configuration, and
   50.47 +# store details are given by the per-domain or per-device configuration.
   50.48 +
   50.49 +evalVariables "$@"
   50.50 +
   50.51 +ip=${ip:-}
   50.52 +ip=$(xenstore_read_default "$XENBUS_PATH/ip" "$ip")
   50.53 +
   50.54 +# Check presence of compulsory args.
   50.55 +XENBUS_PATH="${XENBUS_PATH:?}"
   50.56 +vif="${vif:?}"
   50.57 +
   50.58 +
   50.59 +vifname=$(xenstore_read_default "$XENBUS_PATH/vifname" "")
   50.60 +if [ "$vifname" ]
   50.61 +then
   50.62 +  if [ "$command" == "online" ] && ! ip link show "$vifname" >&/dev/null
   50.63 +  then
   50.64 +    do_or_die ip link set "$vif" name "$vifname"
   50.65 +  fi
   50.66 +  vif="$vifname"
   50.67 +fi
   50.68 +
   50.69 +
   50.70 +frob_iptable()
   50.71 +{
   50.72 +  if [ "$command" == "online" ]
   50.73 +  then
   50.74 +    local c="-A"
   50.75 +  else
   50.76 +    local c="-D"
   50.77 +  fi
   50.78 +
   50.79 +  iptables "$c" FORWARD -m physdev --physdev-in "$vif" "$@" -j ACCEPT \
   50.80 +    2>/dev/null ||
   50.81 +    [ "$c" == "-D" ] ||
   50.82 +    log err \
   50.83 +     "iptables $c FORWARD -m physdev --physdev-in $vif $@ -j ACCEPT failed.
   50.84 +If you are using iptables, this may affect networking for guest domains."
   50.85 +}
   50.86 +
   50.87 +
   50.88 +##
   50.89 +# Add or remove the appropriate entries in the iptables.  With antispoofing
   50.90 +# turned on, we have to explicitly allow packets to the interface, regardless
   50.91 +# of the ip setting.  If ip is set, then we additionally restrict the packets
   50.92 +# to those coming from the specified networks, though we allow DHCP requests
   50.93 +# as well.
   50.94 +#
   50.95 +handle_iptable()
   50.96 +{
   50.97 +  # Check for a working iptables installation.  Checking for the iptables
   50.98 +  # binary is not sufficient, because the user may not have the appropriate
   50.99 +  # modules installed.  If iptables is not working, then there's no need to do
  50.100 +  # anything with it, so we can just return.
  50.101 +  if ! iptables -L -n >&/dev/null
  50.102 +  then
  50.103 +    return
  50.104 +  fi
  50.105 +
  50.106 +  if [ "$ip" != "" ]
  50.107 +  then
  50.108 +      local addr
  50.109 +      for addr in $ip
  50.110 +      do
  50.111 +        frob_iptable -s "$addr"
  50.112 +      done
  50.113 +
  50.114 +      # Always allow the domain to talk to a DHCP server.
  50.115 +      frob_iptable -p udp --sport 68 --dport 67
  50.116 +  else
  50.117 +      # No IP addresses have been specified, so allow anything.
  50.118 +      frob_iptable
  50.119 +  fi
  50.120 +}
  50.121 +
  50.122 +
  50.123 +##
  50.124 +# ip_of interface
  50.125 +#
  50.126 +# Print the IP address currently in use at the given interface, or nothing if
  50.127 +# the interface is not up.
  50.128 +#
  50.129 +ip_of()
  50.130 +{
  50.131 +  ip addr show "$1" | awk "/^.*inet.*$1\$/{print \$2}" | sed -n '1 s,/.*,,p'
  50.132 +}
  50.133 +
  50.134 +
  50.135 +##
  50.136 +# dom0_ip
  50.137 +#
  50.138 +# Print the IP address of the interface in dom0 through which we are routing.
  50.139 +# This is the IP address on the interface specified as "netdev" as a parameter
  50.140 +# to these scripts, or eth0 by default.  This function will call fatal if no
  50.141 +# such interface could be found.
  50.142 +#
  50.143 +dom0_ip()
  50.144 +{
  50.145 +  local nd=${netdev:-eth0}
  50.146 +  local result=$(ip_of "$nd")
  50.147 +  if [ -z "$result" ]
  50.148 +  then
  50.149 +      fatal
  50.150 +"$netdev is not up.  Bring it up or specify another interface with " \
  50.151 +"netdev=<if> as a parameter to $0."
  50.152 +  fi
  50.153 +  echo "$result"
  50.154 +}
    51.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    51.2 +++ b/tools/hotplug/Linux/vif-nat	Tue Nov 04 12:13:42 2008 +0000
    51.3 @@ -0,0 +1,192 @@
    51.4 +#!/bin/bash
    51.5 +#============================================================================
    51.6 +# /etc/xen/vif-nat
    51.7 +#
    51.8 +# Script for configuring a vif in routed-nat mode.
    51.9 +# The hotplugging system will call this script if it is specified either in
   51.10 +# the device configuration given to Xend, or the default Xend configuration
   51.11 +# in /etc/xen/xend-config.sxp.  If the script is specified in neither of those
   51.12 +# places, then vif-bridge is the default.
   51.13 +#
   51.14 +# Usage:
   51.15 +# vif-nat (add|remove|online|offline)
   51.16 +#
   51.17 +# Environment vars:
   51.18 +# vif         vif interface name (required).
   51.19 +# XENBUS_PATH path to this device's details in the XenStore (required).
   51.20 +#
   51.21 +# Parameters:
   51.22 +# dhcp        Whether to alter the local DHCP configuration to include this
   51.23 +#             new host (default no).
   51.24 +#
   51.25 +# Read from the store:
   51.26 +# ip      list of IP networks for the vif, space-separated (default given in
   51.27 +#         this script).
   51.28 +#============================================================================
   51.29 +
   51.30 +
   51.31 +dir=$(dirname "$0")
   51.32 +. "$dir/vif-common.sh"
   51.33 +
   51.34 +# turn on dhcp feature by default if dhcpd is installed
   51.35 +if [ -f /etc/dhcpd.conf ]
   51.36 +then
   51.37 +	dhcp=${dhcp:-yes}
   51.38 +else
   51.39 +	dhcp=${dhcp:-no}
   51.40 +fi
   51.41 +
   51.42 +if [ "$dhcp" != 'no' ]
   51.43 +then
   51.44 +  dhcpd_conf_file=$(find_dhcpd_conf_file)
   51.45 +  dhcpd_init_file=$(find_dhcpd_init_file)
   51.46 +  dhcpd_arg_file=$(find_dhcpd_arg_file)
   51.47 +  if [ -z "$dhcpd_conf_file" ] || [ -z "$dhcpd_init_file" ] || [ -z "$dhcpd_arg_file" ]
   51.48 +  then
   51.49 +    echo 'Failed to find dhcpd configuration or init or args file.' >&2
   51.50 +    exit 1
   51.51 +  fi
   51.52 +fi
   51.53 +
   51.54 +
   51.55 +domid=$(xenstore_read "$XENBUS_PATH/frontend-id")
   51.56 +vifid=$(xenstore_read "$XENBUS_PATH/handle")
   51.57 +vifid=$(( $vifid + 1 ))
   51.58 +
   51.59 +
   51.60 +ip_from_dom()
   51.61 +{
   51.62 +  local domid1=$(( $domid / 256 ))
   51.63 +  local domid2=$(( $domid % 256 ))
   51.64 +
   51.65 +  echo "10.$domid1.$domid2.$vifid/16"
   51.66 +}
   51.67 +
   51.68 +
   51.69 +routing_ip()
   51.70 +{
   51.71 +  echo $(echo $1 | awk -F. '{print $1"."$2"."$3"."$4 + 127}')
   51.72 +}
   51.73 +
   51.74 +
   51.75 +dotted_quad()
   51.76 +{
   51.77 + echo\
   51.78 + $(( ($1 & 0xFF000000) >> 24))\
   51.79 +.$(( ($1 & 0x00FF0000) >> 16))\
   51.80 +.$(( ($1 & 0x0000FF00) >> 8 ))\
   51.81 +.$((  $1 & 0x000000FF       ))
   51.82 +}
   51.83 +
   51.84 +
   51.85 +if [ "$ip" = "" ]
   51.86 +then
   51.87 +  ip=$(ip_from_dom)
   51.88 +fi
   51.89 +
   51.90 +router_ip=$(routing_ip "$ip")
   51.91 +
   51.92 +# Split the given IP/bits pair.
   51.93 +vif_ip=`echo ${ip} | awk -F/ '{print $1}'`
   51.94 +
   51.95 +hostname=$(xenstore_read "$XENBUS_PATH/domain" | tr -- '_.:/+' '-----')
   51.96 +if [ "$vifid" != "1" ]
   51.97 +then
   51.98 +  hostname="$hostname-$vifid"
   51.99 +fi
  51.100 +
  51.101 +dhcparg_remove_entry()
  51.102 +{
  51.103 +  local tmpfile=$(mktemp)
  51.104 +  sed -e "s/$vif //" "$dhcpd_arg_file" >"$tmpfile"
  51.105 +  if diff "$tmpfile" "$dhcpd_arg_file" >/dev/null
  51.106 +  then
  51.107 +    rm "$tmpfile"
  51.108 +  else
  51.109 +    mv "$tmpfile" "$dhcpd_arg_file"
  51.110 +  fi
  51.111 +}
  51.112 +
  51.113 +dhcparg_add_entry()
  51.114 +{
  51.115 +  dhcparg_remove_entry
  51.116 +  local tmpfile=$(mktemp)
  51.117 +  # handle Red Hat, SUSE, and Debian styles, with or without quotes
  51.118 +  sed -e 's/^DHCPDARGS="*\([^"]*\)"*/DHCPDARGS="\1'"$vif "'"/' \
  51.119 +     "$dhcpd_arg_file" >"$tmpfile" && mv "$tmpfile" "$dhcpd_arg_file"
  51.120 +  sed -e 's/^DHCPD_INTERFACE="*\([^"]*\)"*/DHCPD_INTERFACE="\1'"$vif "'"/' \
  51.121 +     "$dhcpd_arg_file" >"$tmpfile" && mv "$tmpfile" "$dhcpd_arg_file"
  51.122 +  sed -e 's/^INTERFACES="*\([^"]*\)"*/INTERFACES="\1'"$vif "'"/' \
  51.123 +     "$dhcpd_arg_file" >"$tmpfile" && mv "$tmpfile" "$dhcpd_arg_file"
  51.124 +  rm -f "$tmpfile"
  51.125 +}
  51.126 +
  51.127 +dhcp_remove_entry()
  51.128 +{
  51.129 +  local tmpfile=$(mktemp)
  51.130 +  grep -v "host $hostname" "$dhcpd_conf_file" >"$tmpfile"
  51.131 +  if diff "$tmpfile" "$dhcpd_conf_file" >/dev/null
  51.132 +  then
  51.133 +    rm "$tmpfile"
  51.134 +  else
  51.135 +    mv "$tmpfile" "$dhcpd_conf_file"
  51.136 +  fi
  51.137 +  dhcparg_remove_entry
  51.138 +}
  51.139 +
  51.140 +
  51.141 +dhcp_up()
  51.142 +{
  51.143 +  claim_lock "vif-nat-dhcp"
  51.144 +  dhcp_remove_entry
  51.145 +  mac=$(xenstore_read "$XENBUS_PATH/mac")
  51.146 +  echo >>"$dhcpd_conf_file" \
  51.147 +"host $hostname { hardware ethernet $mac; fixed-address $vif_ip; option routers $router_ip; option host-name \"$hostname\"; }"
  51.148 +  dhcparg_add_entry
  51.149 +  release_lock "vif-nat-dhcp"
  51.150 +  "$dhcpd_init_file" restart || true
  51.151 +}
  51.152 +
  51.153 +
  51.154 +dhcp_down()
  51.155 +{
  51.156 +  claim_lock "vif-nat-dhcp"
  51.157 +  dhcp_remove_entry
  51.158 +  release_lock "vif-nat-dhcp"
  51.159 +  "$dhcpd_init_file" restart || true # We need to ignore failure because
  51.160 +                                     # ISC dhcpd 3 borks if there is nothing
  51.161 +                                     # for it to do, which is the case if
  51.162 +                                     # the outgoing interface is not
  51.163 +                                     # configured to offer leases and there
  51.164 +                                     # are no vifs.
  51.165 +}
  51.166 +
  51.167 +
  51.168 +case "$command" in
  51.169 +    online)
  51.170 +        if ip route | grep -q "dev $vif"
  51.171 +        then
  51.172 +          log debug "$vif already up"
  51.173 +          exit 0
  51.174 +        fi
  51.175 +
  51.176 +        do_or_die ip link set "$vif" up arp on
  51.177 +        do_or_die ip addr add "$router_ip" dev "$vif"
  51.178 +        do_or_die ip route add "$vif_ip" dev "$vif" src "$router_ip"
  51.179 +        echo 1 >/proc/sys/net/ipv4/conf/${vif}/proxy_arp
  51.180 +        [ "$dhcp" != 'no' ] && dhcp_up
  51.181 +        ;;
  51.182 +    offline)
  51.183 +        [ "$dhcp" != 'no' ] && dhcp_down
  51.184 +        do_without_error ifconfig "$vif" down
  51.185 +        ;;
  51.186 +esac
  51.187 +
  51.188 +
  51.189 +handle_iptable
  51.190 +
  51.191 +log debug "Successful vif-nat $command for $vif."
  51.192 +if [ "$command" = "online" ]
  51.193 +then
  51.194 +  success
  51.195 +fi
    52.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    52.2 +++ b/tools/hotplug/Linux/vif-route	Tue Nov 04 12:13:42 2008 +0000
    52.3 @@ -0,0 +1,56 @@
    52.4 +#!/bin/bash
    52.5 +#============================================================================
    52.6 +# /etc/xen/vif-route
    52.7 +#
    52.8 +# Script for configuring a vif in routed mode.
    52.9 +# The hotplugging system will call this script if it is specified either in
   52.10 +# the device configuration given to Xend, or the default Xend configuration
   52.11 +# in /etc/xen/xend-config.sxp.  If the script is specified in neither of those
   52.12 +# places, then vif-bridge is the default.
   52.13 +#
   52.14 +# Usage:
   52.15 +# vif-route (add|remove|online|offline)
   52.16 +#
   52.17 +# Environment vars:
   52.18 +# vif         vif interface name (required).
   52.19 +# XENBUS_PATH path to this device's details in the XenStore (required).
   52.20 +#
   52.21 +# Read from the store:
   52.22 +# ip      list of IP networks for the vif, space-separated (default given in
   52.23 +#         this script).
   52.24 +#============================================================================
   52.25 +
   52.26 +dir=$(dirname "$0")
   52.27 +. "$dir/vif-common.sh"
   52.28 +
   52.29 +main_ip=$(dom0_ip)
   52.30 +
   52.31 +case "$command" in
   52.32 +    online)
   52.33 +        ifconfig ${vif} ${main_ip} netmask 255.255.255.255 up
   52.34 +        echo 1 >/proc/sys/net/ipv4/conf/${vif}/proxy_arp
   52.35 +        ipcmd='add'
   52.36 +        cmdprefix=''
   52.37 +        ;;
   52.38 +    offline)
   52.39 +        do_without_error ifdown ${vif}
   52.40 +        ipcmd='del'
   52.41 +        cmdprefix='do_without_error'
   52.42 +        ;;
   52.43 +esac
   52.44 +
   52.45 +if [ "${ip}" ] ; then
   52.46 +    # If we've been given a list of IP addresses, then add routes from dom0 to
   52.47 +    # the guest using those addresses.
   52.48 +    for addr in ${ip} ; do
   52.49 +      ${cmdprefix} ip route ${ipcmd} ${addr} dev ${vif} src ${main_ip}
   52.50 +    done 
   52.51 +fi
   52.52 +
   52.53 +handle_iptable
   52.54 +
   52.55 +log debug "Successful vif-route $command for $vif."
   52.56 +if [ "$command" = "online" ]
   52.57 +then
   52.58 +  success
   52.59 +fi
    53.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    53.2 +++ b/tools/hotplug/Linux/vscsi	Tue Nov 04 12:13:42 2008 +0000
    53.3 @@ -0,0 +1,22 @@
    53.4 +#!/bin/sh
    53.5 +#
    53.6 +# Copyright (c) 2007, FUJITSU Limited
    53.7 +# Based on the block scripts code.
    53.8 +#
    53.9 +
   53.10 +dir=$(dirname "$0")
   53.11 +. "$dir/xen-hotplug-common.sh"
   53.12 +
   53.13 +findCommand "$@"
   53.14 +
   53.15 +case "$command" in
   53.16 +	add)
   53.17 +		success
   53.18 +		;;
   53.19 +	remove)
   53.20 +		# TODO
   53.21 +		exit 0
   53.22 +		;;
   53.23 +esac
   53.24 +
   53.25 +exit 0
    54.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    54.2 +++ b/tools/hotplug/Linux/vtpm	Tue Nov 04 12:13:42 2008 +0000
    54.3 @@ -0,0 +1,22 @@
    54.4 +#!/bin/bash
    54.5 +
    54.6 +dir=$(dirname "$0")
    54.7 +. "$dir/vtpm-hotplug-common.sh"
    54.8 +
    54.9 +vtpm_fatal_error=0
   54.10 +
   54.11 +case "$command" in
   54.12 +  add)
   54.13 +    vtpm_create_instance
   54.14 +  ;;
   54.15 +  remove)
   54.16 +    vtpm_remove_instance
   54.17 +  ;;
   54.18 +esac
   54.19 +
   54.20 +if [ $vtpm_fatal_error -eq 0 ]; then
   54.21 +	log debug "Successful vTPM operation '$command'."
   54.22 +	success
   54.23 +else
   54.24 +	fatal "Error while executing vTPM operation '$command'."
   54.25 +fi
    55.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    55.2 +++ b/tools/hotplug/Linux/vtpm-common.sh	Tue Nov 04 12:13:42 2008 +0000
    55.3 @@ -0,0 +1,448 @@
    55.4 +#
    55.5 +# Copyright (c) 2005 IBM Corporation
    55.6 +# Copyright (c) 2005 XenSource Ltd.
    55.7 +#
    55.8 +# This library is free software; you can redistribute it and/or
    55.9 +# modify it under the terms of version 2.1 of the GNU Lesser General Public
   55.10 +# License as published by the Free Software Foundation.
   55.11 +#
   55.12 +# This library is distributed in the hope that it will be useful,
   55.13 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
   55.14 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   55.15 +# Lesser General Public License for more details.
   55.16 +#
   55.17 +# You should have received a copy of the GNU Lesser General Public
   55.18 +# License along with this library; if not, write to the Free Software
   55.19 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   55.20 +#
   55.21 +
   55.22 +dir=$(dirname "$0")
   55.23 +. "$dir/logging.sh"
   55.24 +. "$dir/locking.sh"
   55.25 +
   55.26 +VTPMDB="/var/vtpm/vtpm.db"
   55.27 +
   55.28 +#In the vtpm-impl file some commands should be defined:
   55.29 +#      vtpm_create, vtpm_setup, vtpm_start, etc. (see below)
   55.30 +if [ -r "$dir/vtpm-impl.alt" ]; then
   55.31 +	. "$dir/vtpm-impl.alt"
   55.32 +elif [ -r "$dir/vtpm-impl" ]; then
   55.33 +	. "$dir/vtpm-impl"
   55.34 +else
   55.35 +	function vtpm_create () {
   55.36 +		true
   55.37 +	}
   55.38 +	function vtpm_setup() {
   55.39 +		true
   55.40 +	}
   55.41 +	function vtpm_start() {
   55.42 +		true
   55.43 +	}
   55.44 +	function vtpm_suspend() {
   55.45 +		true
   55.46 +	}
   55.47 +	function vtpm_resume() {
   55.48 +		true
   55.49 +	}
   55.50 +	function vtpm_delete() {
   55.51 +		true
   55.52 +	}
   55.53 +	function vtpm_migrate() {
   55.54 +		echo "Error: vTPM migration accross machines not implemented."
   55.55 +	}
   55.56 +	function vtpm_migrate_local() {
   55.57 +		echo "Error: local vTPM migration not supported"
   55.58 +	}
   55.59 +	function vtpm_migrate_recover() {
   55.60 +		true
   55.61 +	}
   55.62 +fi
   55.63 +
   55.64 +
   55.65 +#Find the instance number for the vtpm given the name of the domain
   55.66 +# Parameters
   55.67 +# - vmname : the name of the vm
   55.68 +# Return value
   55.69 +#  Returns '0' if instance number could not be found, otherwise
   55.70 +#  it returns the instance number in the variable 'instance'
   55.71 +function vtpmdb_find_instance () {
   55.72 +	local vmname ret instance
   55.73 +	vmname=$1
   55.74 +	ret=0
   55.75 +
   55.76 +	instance=$(cat $VTPMDB |                   \
   55.77 +	          awk -vvmname=$vmname             \
   55.78 +	          '{                               \
   55.79 +	             if ( 1 != index($1,"#")) {    \
   55.80 +	               if ( $1 == vmname ) {       \
   55.81 +	                 print $2;                 \
   55.82 +	                 exit;                     \
   55.83 +	               }                           \
   55.84 +	             }                             \
   55.85 +	           }')
   55.86 +	if [ "$instance" != "" ]; then
   55.87 +		ret=$instance
   55.88 +	fi
   55.89 +	echo "$ret"
   55.90 +}
   55.91 +
   55.92 +
   55.93 +# Check whether a particular instance number is still available
   55.94 +# returns "0" if it is not available, "1" otherwise.
   55.95 +function vtpmdb_is_free_instancenum () {
   55.96 +	local instance instances avail i
   55.97 +	instance=$1
   55.98 +	avail=1
   55.99 +	#Allowed instance number range: 1-255
  55.100 +	if [ $instance -eq 0 -o $instance -gt 255 ]; then
  55.101 +		avail=0
  55.102 +	else
  55.103 +		instances=$(cat $VTPMDB |                \
  55.104 +		           gawk                          \
  55.105 +		           '{                            \
  55.106 +		               if (1 != index($1,"#")) { \
  55.107 +		                 printf("%s ",$2);       \
  55.108 +		               }                         \
  55.109 +		            }')
  55.110 +		for i in $instances; do
  55.111 +			if [ $i -eq $instance ]; then
  55.112 +				avail=0
  55.113 +				break
  55.114 +			fi
  55.115 +		done
  55.116 +	fi
  55.117 +	echo "$avail"
  55.118 +}
  55.119 +
  55.120 +
  55.121 +# Get an available instance number given the database
  55.122 +# Returns an unused instance number
  55.123 +function vtpmdb_get_free_instancenum () {
  55.124 +	local ctr instances don found
  55.125 +	instances=$(cat $VTPMDB |                \
  55.126 +	           gawk                          \
  55.127 +	           '{                            \
  55.128 +	               if (1 != index($1,"#")) { \
  55.129 +	                 printf("%s ",$2);       \
  55.130 +	               }                         \
  55.131 +	            }')
  55.132 +	ctr=1
  55.133 +	don=0
  55.134 +	while [ $don -eq 0 ]; do
  55.135 +		found=0
  55.136 +		for i in $instances; do
  55.137 +			if [ $i -eq $ctr ]; then
  55.138 +				found=1;
  55.139 +				break;
  55.140 +			fi
  55.141 +		done
  55.142 +
  55.143 +		if [ $found -eq 0 ]; then
  55.144 +			don=1
  55.145 +			break
  55.146 +		fi
  55.147 +		let ctr=ctr+1
  55.148 +	done
  55.149 +	echo "$ctr"
  55.150 +}
  55.151 +
  55.152 +
  55.153 +# Add a domain name and instance number to the DB file
  55.154 +function vtpmdb_add_instance () {
  55.155 +	local res vmname inst
  55.156 +	vmname=$1
  55.157 +	inst=$2
  55.158 +
  55.159 +	if [ ! -f $VTPMDB ]; then
  55.160 +		echo "#Database for VM to vTPM association" > $VTPMDB
  55.161 +		echo "#1st column: domain name" >> $VTPMDB
  55.162 +		echo "#2nd column: TPM instance number" >> $VTPMDB
  55.163 +	fi
  55.164 +	res=$(vtpmdb_validate_entry $vmname $inst)
  55.165 +	if [ $res -eq 0 ]; then
  55.166 +		echo "$vmname $inst" >> $VTPMDB
  55.167 +	fi
  55.168 +}
  55.169 +
  55.170 +
  55.171 +#Validate whether an entry is the same as passed to this
  55.172 +#function
  55.173 +function vtpmdb_validate_entry () {
  55.174 +	local res rc vmname inst
  55.175 +	rc=0
  55.176 +	vmname=$1
  55.177 +	inst=$2
  55.178 +
  55.179 +	res=$(cat $VTPMDB |            \
  55.180 +	     gawk -vvmname=$vmname     \
  55.181 +	          -vinst=$inst         \
  55.182 +	     '{                        \
  55.183 +	         if ( 1 == index($1,"#")) {\
  55.184 +	         } else                \
  55.185 +	         if ( $1 == vmname &&  \
  55.186 +	              $2 == inst) {    \
  55.187 +	            printf("1");       \
  55.188 +	            exit;              \
  55.189 +	         } else                \
  55.190 +	         if ( $1 == vmname ||  \
  55.191 +	              $2 == inst) {    \
  55.192 +	            printf("2");       \
  55.193 +	            exit;              \
  55.194 +	         }                     \
  55.195 +	     }')
  55.196 +
  55.197 +	if [ "$res" == "1" ]; then
  55.198 +		rc=1
  55.199 +	elif [ "$res" == "2" ]; then
  55.200 +		rc=2
  55.201 +	fi
  55.202 +	echo "$rc"
  55.203 +}
  55.204 +
  55.205 +
  55.206 +#Remove an entry from the vTPM database given its domain name
  55.207 +#and instance number
  55.208 +function vtpmdb_remove_entry () {
  55.209 +	local vmname instance VTPMDB_TMP
  55.210 +	vmname=$1
  55.211 +	instance=$2
  55.212 +	VTPMDB_TMP="$VTPMDB".tmp
  55.213 +
  55.214 +	$(cat $VTPMDB |            \
  55.215 +	 gawk -vvmname=$vmname     \
  55.216 +	 '{                        \
  55.217 +	    if ( $1 != vmname ) {  \
  55.218 +	      print $0;            \
  55.219 +	    }                      \
  55.220 +	 '} > $VTPMDB_TMP)
  55.221 +	if [ -e $VTPMDB_TMP ]; then
  55.222 +		mv -f $VTPMDB_TMP $VTPMDB
  55.223 +		vtpm_delete $instance
  55.224 +	else
  55.225 +		log err "Error creating temporary file '$VTPMDB_TMP'."
  55.226 +	fi
  55.227 +}
  55.228 +
  55.229 +
  55.230 +# Find the reason for the creation of this device:
  55.231 +# Returns 'resume' or 'create'
  55.232 +function vtpm_get_create_reason () {
  55.233 +	local resume
  55.234 +	resume=$(xenstore_read $XENBUS_PATH/resume)
  55.235 +	if [ "$resume" == "True" ]; then
  55.236 +		echo "resume"
  55.237 +	else
  55.238 +		echo "create"
  55.239 +	fi
  55.240 +}
  55.241 +
  55.242 +
  55.243 +#Create a vTPM instance
  55.244 +# If no entry in the TPM database is found, the instance is
  55.245 +# created and an entry added to the database.
  55.246 +function vtpm_create_instance () {
  55.247 +	local res instance domname reason uuid
  55.248 +	uuid=$(xenstore_read "$XENBUS_PATH"/uuid)
  55.249 +	reason=$(vtpm_get_create_reason)
  55.250 +
  55.251 +	claim_lock vtpmdb
  55.252 +
  55.253 +	instance="0"
  55.254 +
  55.255 +	if [ "$uuid" != "" ]; then
  55.256 +		instance=$(vtpmdb_find_instance $uuid)
  55.257 +	fi
  55.258 +	if [ "$instance" == "0" ]; then
  55.259 +		domname=$(xenstore_read "$XENBUS_PATH"/domain)
  55.260 +		instance=$(vtpmdb_find_instance $domname)
  55.261 +	fi
  55.262 +
  55.263 +	if [ "$instance" == "0" -a "$reason" != "create" ]; then
  55.264 +		release_lock vtpmdb
  55.265 +		return
  55.266 +	fi
  55.267 +
  55.268 +	if [ "$instance" == "0" ]; then
  55.269 +		#Try to give the preferred instance to the domain
  55.270 +		instance=$(xenstore_read "$XENBUS_PATH"/pref_instance)
  55.271 +		if [ "$instance" != "" ]; then
  55.272 +			res=$(vtpmdb_is_free_instancenum $instance)
  55.273 +			if [ $res -eq 0 ]; then
  55.274 +				instance=$(vtpmdb_get_free_instancenum)
  55.275 +			fi
  55.276 +		else
  55.277 +			instance=$(vtpmdb_get_free_instancenum)
  55.278 +		fi
  55.279 +
  55.280 +		vtpm_create $instance
  55.281 +
  55.282 +		if [ $vtpm_fatal_error -eq 0 ]; then
  55.283 +			if [ "$uuid" != "" ]; then
  55.284 +				vtpmdb_add_instance $uuid $instance
  55.285 +			else
  55.286 +				vtpmdb_add_instance $domname $instance
  55.287 +			fi
  55.288 +		fi
  55.289 +	else
  55.290 +		if [ "$reason" == "resume" ]; then
  55.291 +			vtpm_resume $instance
  55.292 +		else
  55.293 +			vtpm_start $instance
  55.294 +		fi
  55.295 +	fi
  55.296 +
  55.297 +	release_lock vtpmdb
  55.298 +
  55.299 +	xenstore_write $XENBUS_PATH/instance $instance
  55.300 +}
  55.301 +
  55.302 +
  55.303 +#Remove an instance when a VM is terminating or suspending.
  55.304 +#Since it is assumed that the VM will appear again, the
  55.305 +#entry is kept in the VTPMDB file.
  55.306 +function vtpm_remove_instance () {
  55.307 +	local instance reason domname uuid
  55.308 +	#Stop script execution quietly if path does not exist (anymore)
  55.309 +	xenstore-exists "$XENBUS_PATH"/domain
  55.310 +	uuid=$(xenstore_read "$XENBUS_PATH"/uuid)
  55.311 +
  55.312 +	claim_lock vtpmdb
  55.313 +
  55.314 +	instance="0"
  55.315 +
  55.316 +	if [ "$uuid" != "" ]; then
  55.317 +		instance=$(vtpmdb_find_instance $uuid)
  55.318 +	fi
  55.319 +
  55.320 +	if [ "$instance" == "0" ]; then
  55.321 +		domname=$(xenstore_read "$XENBUS_PATH"/domain)
  55.322 +		instance=$(vtpmdb_find_instance $domname)
  55.323 +	fi
  55.324 +
  55.325 +	if [ "$instance" != "0" ]; then
  55.326 +		vtpm_suspend $instance
  55.327 +	fi
  55.328 +
  55.329 +	release_lock vtpmdb
  55.330 +}
  55.331 +
  55.332 +
  55.333 +#Remove an entry in the VTPMDB file given the domain's name
  55.334 +#1st parameter: The name of the domain
  55.335 +function vtpm_delete_instance () {
  55.336 +	local instance
  55.337 +
  55.338 +	claim_lock vtpmdb
  55.339 +
  55.340 +	instance=$(vtpmdb_find_instance $1)
  55.341 +	if [ "$instance" != "0" ]; then
  55.342 +		vtpmdb_remove_entry $1 $instance
  55.343 +	fi
  55.344 +
  55.345 +	release_lock vtpmdb
  55.346 +}
  55.347 +
  55.348 +# Determine whether the given address is local to this machine
  55.349 +# Return values:
  55.350 +#  "-1" : the given machine name is invalid
  55.351 +#  "0"  : this is not an address of this machine
  55.352 +#  "1"  : this is an address local to this machine
  55.353 +function vtpm_isLocalAddress() {
  55.354 +	local addr res
  55.355 +	addr=$(ping $1 -c 1 |  \
  55.356 +	       gawk '{ print substr($3,2,length($3)-2); exit }')
  55.357 +	if [ "$addr" == "" ]; then
  55.358 +		echo "-1"
  55.359 +		return
  55.360 +	fi
  55.361 +	res=$(ifconfig | grep "inet addr" |  \
  55.362 +	     gawk -vaddr=$addr               \
  55.363 +	     '{                              \
  55.364 +	        if ( addr == substr($2, 6)) {\
  55.365 +	          print "1";                 \
  55.366 +	        }                            \
  55.367 +	     }'                              \
  55.368 +	    )
  55.369 +	if [ "$res" == "" ]; then
  55.370 +		echo "0"
  55.371 +		return
  55.372 +	fi
  55.373 +	echo "1"
  55.374 +}
  55.375 +
  55.376 +# Perform a migration step. This function differentiates between migration
  55.377 +# to the local host or to a remote machine.
  55.378 +# Parameters:
  55.379 +# 1st: destination host to migrate to
  55.380 +# 2nd: name of the domain to migrate
  55.381 +# 3rd: the migration step to perform
  55.382 +function vtpm_migration_step() {
  55.383 +	local res=$(vtpm_isLocalAddress $1)
  55.384 +	if [ "$res" == "0" ]; then
  55.385 +		vtpm_migrate $1 $2 $3
  55.386 +	else
  55.387 +		vtpm_migrate_local
  55.388 +	fi
  55.389 +}
  55.390 +
  55.391 +# Recover from migration due to an error. This function differentiates
  55.392 +# between migration to the local host or to a remote machine.
  55.393 +# Parameters:
  55.394 +# 1st: destination host the migration was going to
  55.395 +# 2nd: name of the domain that was to be migrated
  55.396 +# 3rd: the last successful migration step that was done
  55.397 +function vtpm_recover() {
  55.398 +	local res
  55.399 +	res=$(vtpm_isLocalAddress $1)
  55.400 +	if [ "$res" == "0" ]; then
  55.401 +		vtpm_migrate_recover $1 $2 $3
  55.402 +	fi
  55.403 +}
  55.404 +
  55.405 +
  55.406 +#Determine the domain id given a domain's name.
  55.407 +#1st parameter: name of the domain
  55.408 +#return value: domain id  or -1 if domain id could not be determined
  55.409 +function vtpm_domid_from_name () {
  55.410 +	local id name ids
  55.411 +	ids=$(xenstore-list /local/domain)
  55.412 +	for id in $ids; do
  55.413 +		name=$(xenstore-read /local/domain/$id/name)
  55.414 +		if [ "$name" == "$1" ]; then
  55.415 +			echo "$id"
  55.416 +			return
  55.417 +		fi
  55.418 +	done
  55.419 +	echo "-1"
  55.420 +}
  55.421 +
  55.422 +#Determine the virtual TPM's instance number using the domain ID.
  55.423 +#1st parm: domain ID
  55.424 +function vtpm_uuid_by_domid() {
  55.425 +	echo $(xenstore-read /local/domain/0/backend/vtpm/$1/0/uuid)
  55.426 +}
  55.427 +
  55.428 +
  55.429 +# Determine the vTPM's UUID by the name of the VM
  55.430 +function vtpm_uuid_from_vmname() {
  55.431 +	local domid=$(vtpm_domid_from_name $1)
  55.432 +	if [ "$domid" != "-1" ]; then
  55.433 +		echo $(vtpm_uuid_by_domid $domid)
  55.434 +		return
  55.435 +	fi
  55.436 +	echo ""
  55.437 +}
  55.438 +
  55.439 +#Add a virtual TPM instance number and its associated domain name
  55.440 +#to the VTPMDB file and activate usage of this virtual TPM instance
  55.441 +#by writing the instance number into the xenstore
  55.442 +#1st parm: name of virtual machine
  55.443 +#2nd parm: instance of associated virtual TPM
  55.444 +function vtpm_add_and_activate() {
  55.445 +	local domid=$(vtpm_domid_from_name $1)
  55.446 +	local vtpm_uuid=$(vtpm_uuid_from_vmname $1)
  55.447 +	if [ "$vtpm_uuid" != "" -a "$domid" != "-1" ]; then
  55.448 +		vtpmdb_add_instance $vtpm_uuid $2
  55.449 +		xenstore-write backend/vtpm/$domid/0/instance $2
  55.450 +	fi
  55.451 +}
    56.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    56.2 +++ b/tools/hotplug/Linux/vtpm-delete	Tue Nov 04 12:13:42 2008 +0000
    56.3 @@ -0,0 +1,18 @@
    56.4 +#!/bin/bash
    56.5 +
    56.6 +# This scripts must be called the following way:
    56.7 +# vtpm-delete <vtpm uuid>
    56.8 +# or
    56.9 +# vtpm-delete --vmname <vm name>
   56.10 +
   56.11 +dir=$(dirname "$0")
   56.12 +. "$dir/vtpm-common.sh"
   56.13 +
   56.14 +if [ "$1" == "--vmname" ]; then
   56.15 +	vtpm_uuid=$(vtpm_uuid_from_vmname $2)
   56.16 +	if [ "$vtpm_uuid" != "" ];then
   56.17 +		vtpm_delete_instance $vtpm_uuid
   56.18 +	fi
   56.19 +else
   56.20 +	vtpm_delete_instance $1
   56.21 +fi
    57.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    57.2 +++ b/tools/hotplug/Linux/vtpm-hotplug-common.sh	Tue Nov 04 12:13:42 2008 +0000
    57.3 @@ -0,0 +1,35 @@
    57.4 +#
    57.5 +# Copyright (c) 2005 IBM Corporation
    57.6 +# Copyright (c) 2005 XenSource Ltd.
    57.7 +#
    57.8 +# This library is free software; you can redistribute it and/or
    57.9 +# modify it under the terms of version 2.1 of the GNU Lesser General Public
   57.10 +# License as published by the Free Software Foundation.
   57.11 +#
   57.12 +# This library is distributed in the hope that it will be useful,
   57.13 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
   57.14 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   57.15 +# Lesser General Public License for more details.
   57.16 +#
   57.17 +# You should have received a copy of the GNU Lesser General Public
   57.18 +# License along with this library; if not, write to the Free Software
   57.19 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   57.20 +#
   57.21 +
   57.22 +dir=$(dirname "$0")
   57.23 +. "$dir/xen-hotplug-common.sh"
   57.24 +
   57.25 +findCommand "$@"
   57.26 +if [ "$command" != "online" ]  &&
   57.27 +   [ "$command" != "offline" ] &&
   57.28 +   [ "$command" != "add" ]     &&
   57.29 +   [ "$command" != "remove" ]
   57.30 +then
   57.31 +	log err "Invalid command: $command"
   57.32 +	exit 1
   57.33 +fi
   57.34 +
   57.35 +
   57.36 +XENBUS_PATH="${XENBUS_PATH:?}"
   57.37 +
   57.38 +. "$dir/vtpm-common.sh"
    58.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    58.2 +++ b/tools/hotplug/Linux/vtpm-impl	Tue Nov 04 12:13:42 2008 +0000
    58.3 @@ -0,0 +1,208 @@
    58.4 +#!/bin/bash
    58.5 +# ===================================================================
    58.6 +# 
    58.7 +# Copyright (c) 2005, Intel Corp.
    58.8 +# All rights reserved.
    58.9 +#
   58.10 +# Redistribution and use in source and binary forms, with or without 
   58.11 +# modification, are permitted provided that the following conditions 
   58.12 +# are met:
   58.13 +#
   58.14 +#   * Redistributions of source code must retain the above copyright 
   58.15 +#     notice, this list of conditions and the following disclaimer.
   58.16 +#   * Redistributions in binary form must reproduce the above 
   58.17 +#     copyright notice, this list of conditions and the following 
   58.18 +#     disclaimer in the documentation and/or other materials provided 
   58.19 +#     with the distribution.
   58.20 +#   * Neither the name of Intel Corporation nor the names of its 
   58.21 +#     contributors may be used to endorse or promote products derived
   58.22 +#     from this software without specific prior written permission.
   58.23 +#
   58.24 +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
   58.25 +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
   58.26 +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
   58.27 +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
   58.28 +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   58.29 +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   58.30 +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
   58.31 +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   58.32 +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
   58.33 +# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
   58.34 +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   58.35 +# OF THE POSSIBILITY OF SUCH DAMAGE.
   58.36 +# ===================================================================
   58.37 +
   58.38 +#            |        SRC        |    TAG  |      CMD SIZE     |        ORD       |mtype|strt
   58.39 +TPM_CMD_OPEN=\\x00\\x00\\x00\\x00\\x01\\xc1\\x00\\x00\\x00\\x11\\x01\\x00\\x00\\x01\\x01\\x01
   58.40 +TPM_CMD_RESM=\\x00\\x00\\x00\\x00\\x01\\xc1\\x00\\x00\\x00\\x11\\x01\\x00\\x00\\x01\\x01\\x02
   58.41 +TPM_CMD_CLOS=\\x00\\x00\\x00\\x00\\x01\\xc1\\x00\\x00\\x00\\x0e\\x01\\x00\\x00\\x02
   58.42 +TPM_CMD_DELE=\\x00\\x00\\x00\\x00\\x01\\xc1\\x00\\x00\\x00\\x0e\\x01\\x00\\x00\\x03
   58.43 +
   58.44 +TPM_TYPE_PVM=\\x01
   58.45 +TPM_TYPE_HVM=\\x02
   58.46 +
   58.47 +TPM_SUCCESS=00000000
   58.48 +
   58.49 +TX_VTPM_MANAGER=/var/vtpm/fifos/from_console.fifo
   58.50 +RX_VTPM_MANAGER=/var/vtpm/fifos/to_console.fifo
   58.51 +
   58.52 +VTPM_MIG=/usr/bin/vtpm_migrator
   58.53 +
   58.54 +# -------------------- Helpers for binary streams -----------
   58.55 +
   58.56 +function str_to_hex32() {
   58.57 + printf "%0.8x" $1
   58.58 +}
   58.59 +
   58.60 +function hex32_to_bin() {
   58.61 + local inst=$(str_to_hex32 $1);
   58.62 + 
   58.63 + local n1=`echo $inst | sed 's/\(..\)....../\\\\x\1/'`
   58.64 + local n2=`echo $inst | sed 's/..\(..\)..../\\\\x\1/'`
   58.65 + local n3=`echo $inst | sed 's/....\(..\)../\\\\x\1/'`
   58.66 + local n4=`echo $inst | sed 's/......\(..\)/\\\\x\1/'`
   58.67 +
   58.68 + echo "$n1$n2$n3$n4"
   58.69 +}
   58.70 +
   58.71 +function vtpm_manager_cmd() {
   58.72 + local cmd=$1;
   58.73 + local inst=$2;
   58.74 + local inst_bin=$(hex32_to_bin $inst);
   58.75 +
   58.76 + claim_lock vtpm_mgr
   58.77 +
   58.78 + #send cmd to vtpm_manager
   58.79 + printf "$cmd$inst_bin" > $TX_VTPM_MANAGER
   58.80 +
   58.81 + #recv response
   58.82 + set +e
   58.83 + local resp_hex=`dd skip=10 bs=1 count=4 if=$RX_VTPM_MANAGER 2> /dev/null | xxd -ps`
   58.84 + set -e
   58.85 +
   58.86 + release_lock vtpm_mgr
   58.87 +
   58.88 + #return whether the command was successful
   58.89 + if [ $resp_hex -ne $TPM_SUCCESS ]; then
   58.90 +   vtpm_fatal_error=1
   58.91 +   false
   58.92 +  else
   58.93 +   true
   58.94 + fi
   58.95 +}
   58.96 +
   58.97 +# Helper to get vm type to pass to vtpm_manager open/resume
   58.98 +function vtpm_get_type() {
   58.99 + local inst=$(xenstore_read $XENBUS_PATH/frontend-id)
  58.100 + local vm=$(xenstore_read /local/domain/$inst/vm)
  58.101 + if [ "$vm" != "" ]; then
  58.102 +  local ostype=$(xenstore-read $vm/image/ostype)
  58.103 +  if [ "$ostype" == "hvm" ]; then
  58.104 +   echo $TPM_TYPE_HVM;
  58.105 +  else
  58.106 +   echo $TPM_TYPE_PVM;
  58.107 +  fi
  58.108 + fi
  58.109 +}
  58.110 +
  58.111 +# ------------------ Command handlers -----------------
  58.112 +
  58.113 +# Create new vtpm instance & set it up for use
  58.114 +function vtpm_create () {
  58.115 + # Creation is handled implicitly by the manager on first setup
  58.116 + # so just set it up for use
  58.117 + $(vtpm_start $1)
  58.118 +}
  58.119 +
  58.120 +# Setup vtpm instance for use.
  58.121 +function vtpm_start() {
  58.122 + local vmtype=$(vtpm_get_type);
  58.123 + $(vtpm_manager_cmd $TPM_CMD_OPEN$vmtype $1)
  58.124 +}
  58.125 +
  58.126 +function vtpm_resume() {
  58.127 + local vmtype=$(vtpm_get_type);
  58.128 + $(vtpm_manager_cmd $TPM_CMD_RESM$vmtype $1)
  58.129 +}
  58.130 +
  58.131 +# Reset the vtpm AKA clear PCRs
  58.132 +function vtpm_reset() {
  58.133 + #not used by current implemenation
  58.134 + true
  58.135 +}
  58.136 +
  58.137 +# Shutdown the vtpm while the vm is down
  58.138 +# This could be a suspend of shutdown
  58.139 +# we cannot distinquish, so save the state
  58.140 +# and decide on startup if we should keep is
  58.141 +function vtpm_suspend() {
  58.142 + $(vtpm_manager_cmd $TPM_CMD_CLOS $1)
  58.143 +}
  58.144 +
  58.145 +
  58.146 +function vtpm_delete() {
  58.147 + local inst=$1
  58.148 + if $(vtpm_manager_cmd $TPM_CMD_DELE $inst); then
  58.149 +   rm -f /var/vtpm/vtpm_dm_$1.data
  58.150 +   true
  58.151 + else 
  58.152 +   vtpm_fatal_error=1
  58.153 +   false
  58.154 + fi
  58.155 +}
  58.156 +
  58.157 +# Perform a migration step. This function differentiates between migration
  58.158 +# to the local host or to a remote machine.
  58.159 +# Parameters:
  58.160 +# 1st: destination host to migrate to
  58.161 +# 2nd: name of the domain to migrate
  58.162 +# 3rd: the migration step to perform
  58.163 +function vtpm_migrate() {
  58.164 + local instance res
  58.165 +
  58.166 + instance=$(vtpmdb_find_instance $2)
  58.167 + if [ "$instance" == "" ]; then
  58.168 +  log err "VTPM Migratoin failed. Unable to translation of domain name"
  58.169 +  echo "Error: VTPM Migration failed while looking up instance number"
  58.170 + fi
  58.171 +
  58.172 + case "$3" in
  58.173 +  0)
  58.174 +   #Incicate migration supported
  58.175 +   echo "0" 
  58.176 +  ;;
  58.177 +
  58.178 +  1)
  58.179 +   # Get Public Key from Destination
  58.180 +   # Call vtpm_manager's migration part 1
  58.181 +   claim_lock vtpm_mgr
  58.182 +   $VTPM_MIG $1 $2 $instance $3
  58.183 +   release_lock vtpm_mgr
  58.184 +  ;;
  58.185 +
  58.186 +  2)
  58.187 +   # Call manager's migration step 2 and send result to destination
  58.188 +   # If successful remove from db
  58.189 +   claim_lock vtpm_mgr
  58.190 +   $VTPM_MIG $1 $2 $instance $3
  58.191 +   release_lock vtpm_mgr
  58.192 +  ;;
  58.193 +
  58.194 +  3)
  58.195 +   if `ps x | grep "$VTPM_MIG $1"`; then
  58.196 +    log err "VTPM Migration failed to complete."
  58.197 +    echo "Error: VTPM Migration failed to complete."
  58.198 +   fi
  58.199 +  ;;
  58.200 + esac
  58.201 + 
  58.202 +}
  58.203 +
  58.204 +
  58.205 +function vtpm_migrate_recover() {
  58.206 + echo "Error: Recovery not supported yet" 
  58.207 +}
  58.208 +
  58.209 +function vtpm_migrate_local() {
  58.210 + echo "Error: local vTPM migration not supported"
  58.211 +}
    59.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    59.2 +++ b/tools/hotplug/Linux/vtpm-migration.sh	Tue Nov 04 12:13:42 2008 +0000
    59.3 @@ -0,0 +1,19 @@
    59.4 +#
    59.5 +# Copyright (c) 2005 IBM Corporation
    59.6 +#
    59.7 +# This library is free software; you can redistribute it and/or
    59.8 +# modify it under the terms of version 2.1 of the GNU Lesser General Public
    59.9 +# License as published by the Free Software Foundation.
   59.10 +#
   59.11 +# This library is distributed in the hope that it will be useful,
   59.12 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
   59.13 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   59.14 +# Lesser General Public License for more details.
   59.15 +#
   59.16 +# You should have received a copy of the GNU Lesser General Public
   59.17 +# License along with this library; if not, write to the Free Software
   59.18 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   59.19 +#
   59.20 +
   59.21 +dir=$(dirname "$0")
   59.22 +. "$dir/vtpm-common.sh"
    60.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    60.2 +++ b/tools/hotplug/Linux/xen-backend.agent	Tue Nov 04 12:13:42 2008 +0000
    60.3 @@ -0,0 +1,39 @@
    60.4 +#! /bin/bash
    60.5 +
    60.6 +PATH=/etc/xen/scripts:$PATH
    60.7 +
    60.8 +. /etc/xen/scripts/locking.sh
    60.9 +
   60.10 +claim_lock xenbus_hotplug_global
   60.11 +
   60.12 +case "$XENBUS_TYPE" in
   60.13 +  tap)
   60.14 +    /etc/xen/scripts/blktap "$ACTION"
   60.15 +    ;;
   60.16 +  vbd)
   60.17 +    /etc/xen/scripts/block "$ACTION"
   60.18 +    ;;
   60.19 +  vtpm)
   60.20 +    /etc/xen/scripts/vtpm "$ACTION"
   60.21 +    ;;
   60.22 +  vif)
   60.23 +    [ -n "$script" ] && $script "$ACTION"
   60.24 +    ;;
   60.25 +  vscsi)
   60.26 +    /etc/xen/scripts/vscsi "$ACTION"
   60.27 +    ;;
   60.28 +esac
   60.29 +
   60.30 +case "$ACTION" in
   60.31 +  add)
   60.32 +    ;;
   60.33 +  remove)
   60.34 +    /etc/xen/scripts/xen-hotplug-cleanup
   60.35 +    ;;
   60.36 +  online)
   60.37 +    ;;
   60.38 +  offline)
   60.39 +    ;;
   60.40 +esac
   60.41 +
   60.42 +release_lock xenbus_hotplug_global
    61.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    61.2 +++ b/tools/hotplug/Linux/xen-backend.rules	Tue Nov 04 12:13:42 2008 +0000
    61.3 @@ -0,0 +1,9 @@
    61.4 +SUBSYSTEM=="xen-backend", KERNEL=="tap*", RUN+="/etc/xen/scripts/blktap $env{ACTION}"
    61.5 +SUBSYSTEM=="xen-backend", KERNEL=="vbd*", RUN+="/etc/xen/scripts/block $env{ACTION}"
    61.6 +SUBSYSTEM=="xen-backend", KERNEL=="vtpm*", RUN+="/etc/xen/scripts/vtpm $env{ACTION}"
    61.7 +SUBSYSTEM=="xen-backend", KERNEL=="vif*", ACTION=="online", RUN+="$env{script} online"
    61.8 +SUBSYSTEM=="xen-backend", KERNEL=="vif*", ACTION=="offline", RUN+="$env{script} offline"
    61.9 +SUBSYSTEM=="xen-backend", KERNEL=="vscsi*", RUN+="/etc/xen/scripts/vscsi $env{ACTION}"
   61.10 +SUBSYSTEM=="xen-backend", ACTION=="remove", RUN+="/etc/xen/scripts/xen-hotplug-cleanup"
   61.11 +KERNEL=="evtchn", NAME="xen/%k"
   61.12 +KERNEL=="blktap[0-9]*", NAME="xen/%k"
    62.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    62.2 +++ b/tools/hotplug/Linux/xen-hotplug-cleanup	Tue Nov 04 12:13:42 2008 +0000
    62.3 @@ -0,0 +1,22 @@
    62.4 +#! /bin/bash
    62.5 +
    62.6 +dir=$(dirname "$0")
    62.7 +. "$dir/xen-hotplug-common.sh"
    62.8 +
    62.9 +# Claim the lock protecting /etc/xen/scripts/block.  This stops a race whereby
   62.10 +# paths in the store would disappear underneath that script as it attempted to
   62.11 +# read from the store checking for device sharing.
   62.12 +# Any other scripts that do similar things will have to have their lock
   62.13 +# claimed too.
   62.14 +# This is pretty horrible, but there's not really a nicer way of solving this.
   62.15 +claim_lock "block"
   62.16 +
   62.17 +# remove device frontend store entries
   62.18 +xenstore-rm -t \
   62.19 +  $(xenstore-read "$XENBUS_PATH/frontend" 2>/dev/null) 2>/dev/null || true
   62.20 +
   62.21 +# remove device backend store entries
   62.22 +xenstore-rm -t "$XENBUS_PATH"        2>/dev/null || true
   62.23 +xenstore-rm -t "error/$XENBUS_PATH"  2>/dev/null || true
   62.24 +
   62.25 +release_lock "block"
    63.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    63.2 +++ b/tools/hotplug/Linux/xen-hotplug-common.sh	Tue Nov 04 12:13:42 2008 +0000
    63.3 @@ -0,0 +1,93 @@
    63.4 +#
    63.5 +# Copyright (c) 2005 XenSource Ltd.
    63.6 +#
    63.7 +# This library is free software; you can redistribute it and/or
    63.8 +# modify it under the terms of version 2.1 of the GNU Lesser General Public
    63.9 +# License as published by the Free Software Foundation.
   63.10 +#
   63.11 +# This library is distributed in the hope that it will be useful,
   63.12 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
   63.13 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   63.14 +# Lesser General Public License for more details.
   63.15 +#
   63.16 +# You should have received a copy of the GNU Lesser General Public
   63.17 +# License along with this library; if not, write to the Free Software
   63.18 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   63.19 +#
   63.20 +
   63.21 +
   63.22 +dir=$(dirname "$0")
   63.23 +. "$dir/logging.sh"
   63.24 +. "$dir/xen-script-common.sh"
   63.25 +. "$dir/locking.sh"
   63.26 +
   63.27 +exec 2>>/var/log/xen/xen-hotplug.log
   63.28 +
   63.29 +export PATH="/sbin:/bin:/usr/bin:/usr/sbin:$PATH"
   63.30 +export LANG="POSIX"
   63.31 +unset $(set | grep ^LC_ | cut -d= -f1)
   63.32 +
   63.33 +fatal() {
   63.34 +  xenstore_write "$XENBUS_PATH/hotplug-error" "$*" \
   63.35 +                 "$XENBUS_PATH/hotplug-status" error
   63.36 +  log err "$@"
   63.37 +  exit 1
   63.38 +}
   63.39 +
   63.40 +success() {
   63.41 +  # Tell DevController that backend is "connected"
   63.42 +  xenstore_write "$XENBUS_PATH/hotplug-status" connected
   63.43 +}
   63.44 +
   63.45 +do_or_die() {
   63.46 +  "$@" || fatal "$@ failed"
   63.47 +}
   63.48 +
   63.49 +do_without_error() {
   63.50 +  "$@" 2>/dev/null || log debug "$@ failed"
   63.51 +}
   63.52 +
   63.53 +sigerr() {
   63.54 +  fatal "$0 failed; error detected."
   63.55 +}
   63.56 +
   63.57 +trap sigerr ERR
   63.58 +
   63.59 +
   63.60 +##
   63.61 +# xenstore_read <path>+
   63.62 +#
   63.63 +# Read each of the given paths, returning each result on a separate line, or
   63.64 +# exit this script if any of the paths is missing.
   63.65 +#
   63.66 +xenstore_read() {
   63.67 +  local v=$(xenstore-read "$@" || true)
   63.68 +  [ "$v" != "" ] || fatal "xenstore-read $@ failed."
   63.69 +  echo "$v"
   63.70 +}
   63.71 +
   63.72 +
   63.73 +##
   63.74 +# xenstore_read_default <path> <default>
   63.75 +#
   63.76 +# Read the given path, returning the value there or the given default if the
   63.77 +# path is not present.
   63.78 +#
   63.79 +xenstore_read_default() {
   63.80 +  xenstore-read "$1" 2>/dev/null || echo "$2"
   63.81 +}
   63.82 +
   63.83 +
   63.84 +##
   63.85 +# xenstore_write (<path> <value>)+
   63.86 +#
   63.87 +# Write each of the key/value pairs to the store, and exit this script if any
   63.88 +# such writing fails.
   63.89 +#
   63.90 +xenstore_write() {
   63.91 +  log debug "Writing $@ to xenstore."
   63.92 +  xenstore-write "$@" || fatal "Writing $@ to xenstore failed."
   63.93 +}
   63.94 +
   63.95 +
   63.96 +log debug "$@" "XENBUS_PATH=$XENBUS_PATH"
    64.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    64.2 +++ b/tools/hotplug/Linux/xen-network-common.sh	Tue Nov 04 12:13:42 2008 +0000
    64.3 @@ -0,0 +1,118 @@
    64.4 +#
    64.5 +# Copyright (c) 2005 XenSource Ltd.
    64.6 +#
    64.7 +# This library is free software; you can redistribute it and/or
    64.8 +# modify it under the terms of version 2.1 of the GNU Lesser General Public
    64.9 +# License as published by the Free Software Foundation.
   64.10 +#
   64.11 +# This library is distributed in the hope that it will be useful,
   64.12 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
   64.13 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   64.14 +# Lesser General Public License for more details.
   64.15 +#
   64.16 +# You should have received a copy of the GNU Lesser General Public
   64.17 +# License along with this library; if not, write to the Free Software
   64.18 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   64.19 +#
   64.20 +
   64.21 +
   64.22 +# Gentoo doesn't have ifup/ifdown, so we define appropriate alternatives.
   64.23 +
   64.24 +# Other platforms just use ifup / ifdown directly.
   64.25 +
   64.26 +##
   64.27 +# preiftransfer
   64.28 +#
   64.29 +# @param $1 The current name for the physical device, which is also the name
   64.30 +#           that the virtual device will take once the physical device has
   64.31 +#           been renamed.
   64.32 +
   64.33 +if ! which ifup >/dev/null 2>/dev/null
   64.34 +then
   64.35 +  preiftransfer()
   64.36 +  {
   64.37 +    true
   64.38 +  }
   64.39 +  ifup()
   64.40 +  {
   64.41 +    false
   64.42 +  }
   64.43 +  ifdown()
   64.44 +  {
   64.45 +    false
   64.46 +  }
   64.47 +else
   64.48 +  preiftransfer()
   64.49 +  {
   64.50 +    true
   64.51 +  }
   64.52 +fi
   64.53 +
   64.54 +
   64.55 +first_file()
   64.56 +{
   64.57 +  t="$1"
   64.58 +  shift
   64.59 +  for file in $@
   64.60 +  do
   64.61 +    if [ "$t" "$file" ]
   64.62 +    then
   64.63 +      echo "$file"
   64.64 +      return
   64.65 +    fi
   64.66 +  done
   64.67 +}
   64.68 +
   64.69 +find_dhcpd_conf_file()
   64.70 +{
   64.71 +  first_file -f /etc/dhcp3/dhcpd.conf /etc/dhcpd.conf
   64.72 +}
   64.73 +
   64.74 +
   64.75 +find_dhcpd_init_file()
   64.76 +{
   64.77 +  first_file -x /etc/init.d/{dhcp3-server,dhcp,dhcpd}
   64.78 +}
   64.79 +
   64.80 +find_dhcpd_arg_file()
   64.81 +{
   64.82 +  first_file -f /etc/sysconfig/dhcpd /etc/defaults/dhcp /etc/default/dhcp3-server
   64.83 +}
   64.84 +
   64.85 +# configure interfaces which act as pure bridge ports:
   64.86 +setup_bridge_port() {
   64.87 +    local dev="$1"
   64.88 +
   64.89 +    # take interface down ...
   64.90 +    ip link set ${dev} down
   64.91 +
   64.92 +    # ... and configure it
   64.93 +    ip addr flush ${dev}
   64.94 +}
   64.95 +
   64.96 +# Usage: create_bridge bridge
   64.97 +create_bridge () {
   64.98 +    local bridge=$1
   64.99 +
  64.100 +    # Don't create the bridge if it already exists.
  64.101 +    if [ ! -e "/sys/class/net/${bridge}/bridge" ]; then
  64.102 +	brctl addbr ${bridge}
  64.103 +	brctl stp ${bridge} off
  64.104 +	brctl setfd ${bridge} 0
  64.105 +    fi
  64.106 +}
  64.107 +
  64.108 +# Usage: add_to_bridge bridge dev
  64.109 +add_to_bridge () {
  64.110 +    local bridge=$1
  64.111 +    local dev=$2
  64.112 +
  64.113 +    # Don't add $dev to $bridge if it's already on a bridge.
  64.114 +    if [ -e "/sys/class/net/${bridge}/brif/${dev}" ]; then
  64.115 +	ip link set ${dev} up || true
  64.116 +	return
  64.117 +    fi
  64.118 +    brctl addif ${bridge} ${dev}
  64.119 +    ip link set ${dev} up
  64.120 +}
  64.121 +
    65.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    65.2 +++ b/tools/hotplug/Linux/xen-script-common.sh	Tue Nov 04 12:13:42 2008 +0000
    65.3 @@ -0,0 +1,44 @@
    65.4 +#
    65.5 +# Copyright (c) 2005 XenSource Ltd.
    65.6 +#
    65.7 +# This library is free software; you can redistribute it and/or
    65.8 +# modify it under the terms of version 2.1 of the GNU Lesser General Public
    65.9 +# License as published by the Free Software Foundation.
   65.10 +#
   65.11 +# This library is distributed in the hope that it will be useful,
   65.12 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
   65.13 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   65.14 +# Lesser General Public License for more details.
   65.15 +#
   65.16 +# You should have received a copy of the GNU Lesser General Public
   65.17 +# License along with this library; if not, write to the Free Software
   65.18 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   65.19 +#
   65.20 +
   65.21 +
   65.22 +set -e
   65.23 +
   65.24 +
   65.25 +evalVariables()
   65.26 +{
   65.27 +  for arg in "$@"
   65.28 +  do
   65.29 +    if expr 'index' "$arg" '=' '>' '1' >/dev/null
   65.30 +    then
   65.31 +      eval "$arg"
   65.32 +    fi
   65.33 +  done
   65.34 +}
   65.35 +
   65.36 +
   65.37 +findCommand()
   65.38 +{
   65.39 +  for arg in "$@"
   65.40 +  do
   65.41 +    if ! expr 'index' "$arg" '=' >/dev/null
   65.42 +    then
   65.43 +      command="$arg"
   65.44 +      return
   65.45 +    fi
   65.46 +  done
   65.47 +}
    66.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    66.2 +++ b/tools/hotplug/Makefile	Tue Nov 04 12:13:42 2008 +0000
    66.3 @@ -0,0 +1,9 @@
    66.4 +XEN_ROOT = ../../
    66.5 +include $(XEN_ROOT)/tools/Rules.mk
    66.6 +
    66.7 +SUBDIRS-y := common
    66.8 +SUBDIRS-$(CONFIG_NetBSD) += NetBSD
    66.9 +SUBDIRS-$(CONFIG_Linux) += Linux
   66.10 +
   66.11 +.PHONY: all clean install
   66.12 +all clean install: %: subdirs-%
    67.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    67.2 +++ b/tools/hotplug/NetBSD/Makefile	Tue Nov 04 12:13:42 2008 +0000
    67.3 @@ -0,0 +1,41 @@
    67.4 +XEN_ROOT = ../../../
    67.5 +include $(XEN_ROOT)/tools/Rules.mk
    67.6 +
    67.7 +# Xen configuration dir and configs to go there.
    67.8 +XEN_CONFIG_DIR = $(PREFIX)/etc/xen
    67.9 +
   67.10 +# Xen script dir and scripts to go there.
   67.11 +XEN_SCRIPT_DIR = $(PREFIX)/etc/xen/scripts
   67.12 +XEN_SCRIPTS =
   67.13 +XEN_SCRIPTS += block-nbsd
   67.14 +XEN_SCRIPTS += hvm-nbsd
   67.15 +XEN_SCRIPTS += netbsd1-nbsd
   67.16 +XEN_SCRIPTS += qemu-ifup-nbsd
   67.17 +XEN_SCRIPTS += vif-bridge-nbsd
   67.18 +XEN_SCRIPTS += vif-ip-nbsd
   67.19 +
   67.20 +XEN_SCRIPT_DATA =
   67.21 +
   67.22 +.PHONY: all
   67.23 +all:
   67.24 +
   67.25 +.PHONY: build
   67.26 +build:
   67.27 +
   67.28 +.PHONY: install
   67.29 +install: all install-scripts
   67.30 +
   67.31 +.PHONY: install-scripts
   67.32 +install-scripts:
   67.33 +	$(INSTALL_DATA_DIR) $(DESTDIR)$(XEN_SCRIPT_DIR)
   67.34 +	set -e; for i in $(XEN_SCRIPTS); \
   67.35 +	   do \
   67.36 +	   $(INSTALL_DATA) $$i $(DESTDIR)$(XEN_SCRIPT_DIR); \
   67.37 +	done
   67.38 +	set -e; for i in $(XEN_SCRIPT_DATA); \
   67.39 +	   do \
   67.40 +	   $(INSTALL_DATA) $$i $(DESTDIR)$(XEN_SCRIPT_DIR); \
   67.41 +	done
   67.42 +
   67.43 +.PHONY: clean
   67.44 +clean:
    68.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    68.2 +++ b/tools/hotplug/NetBSD/block-nbsd	Tue Nov 04 12:13:42 2008 +0000
    68.3 @@ -0,0 +1,88 @@
    68.4 +#!/bin/sh -e
    68.5 +
    68.6 +# $NetBSD: block-nbsd,v 1.1.1.1 2008/08/07 20:26:57 cegger Exp $
    68.7 +# Called by xenbackendd
    68.8 +# Usage: block xsdir_backend_path state
    68.9 +
   68.10 +PATH=/bin:/usr/bin:/sbin:/usr/sbin
   68.11 +export PATH
   68.12 +
   68.13 +error() {
   68.14 +	echo "$@" >&2
   68.15 +	xenstore_write $xpath/hotplug-status error
   68.16 +	exit 1
   68.17 +}
   68.18 +	
   68.19 +
   68.20 +xpath=$1
   68.21 +xstatus=$2
   68.22 +xtype=$(xenstore-read "$xpath/type")
   68.23 +xparams=$(xenstore-read "$xpath/params")
   68.24 +
   68.25 +case $xstatus in
   68.26 +6)
   68.27 +	# device removed
   68.28 +	case $xtype in
   68.29 +	file)
   68.30 +		vnd=$(xenstore-read "$xpath/vnd" || echo none)
   68.31 +		if [ $vnd != none ]; then
   68.32 +			vnconfig -u $vnd
   68.33 +		fi
   68.34 +		;;
   68.35 +	phy)
   68.36 +		;;
   68.37 +	*)
   68.38 +		echo "unknown type $xtype" >&2
   68.39 +		;;
   68.40 +	esac
   68.41 +	xenstore-rm $xpath
   68.42 +	exit 0
   68.43 +	;;
   68.44 +2)
   68.45 +	case $xtype in
   68.46 +	file)
   68.47 +		# Store the list of available vnd(4) devices in
   68.48 +		#``available_disks'', and mark them as ``free''.
   68.49 +		list=`ls -1 /dev/vnd[0-9]*d | sed "s,/dev/vnd,,;s,d,," | sort -n`
   68.50 +		for i in $list; do
   68.51 +			disk="vnd$i"
   68.52 +			available_disks="$available_disks $disk"
   68.53 +			eval $disk=free
   68.54 +		done
   68.55 +		# Mark the used vnd(4) devices as ``used''.
   68.56 +		for disk in `sysctl hw.disknames`; do
   68.57 +			case $disk in
   68.58 +			vnd[0-9]*) eval $disk=used ;;
   68.59 +			esac
   68.60 +		done
   68.61 +		# Configure the first free vnd(4) device.
   68.62 +		for disk in $available_disks; do
   68.63 +			eval status=\$$disk
   68.64 +			if [ "$status" = "free" ] && \
   68.65 +			    vnconfig /dev/${disk}d $xparams >/dev/null; then
   68.66 +				device=/dev/${disk}d
   68.67 +				echo vnconfig /dev/${disk}d $xparams
   68.68 +				break	
   68.69 +			fi
   68.70 +		done
   68.71 +		if [ x$device = x ] ; then
   68.72 +			error "no available vnd device"
   68.73 +		fi
   68.74 +		echo xenstore-write $xpath/vnd $device
   68.75 +		xenstore-write $xpath/vnd $device
   68.76 +		;;
   68.77 +	phy)
   68.78 +		device=$xparams
   68.79 +		;;
   68.80 +	esac
   68.81 +	physical_device=$(stat -f '%r' "$device")
   68.82 +	echo xenstore-write $xpath/physical-device $physical_device
   68.83 +	xenstore-write $xpath/physical-device $physical_device
   68.84 +	echo xenstore-write $xpath/hotplug-status connected
   68.85 +	xenstore-write $xpath/hotplug-status connected
   68.86 +	exit 0
   68.87 +	;;
   68.88 +*)
   68.89 +	exit 0
   68.90 +	;;
   68.91 +esac
    69.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    69.2 +++ b/tools/hotplug/NetBSD/qemu-ifup-nbsd	Tue Nov 04 12:13:42 2008 +0000
    69.3 @@ -0,0 +1,3 @@
    69.4 +#!/bin/sh
    69.5 +ifconfig $1 up
    69.6 +exec /sbin/brconfig $2 add $1
    70.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    70.2 +++ b/tools/hotplug/NetBSD/vif-bridge-nbsd	Tue Nov 04 12:13:42 2008 +0000
    70.3 @@ -0,0 +1,35 @@
    70.4 +#!/bin/sh -e
    70.5 +
    70.6 +# $NetBSD: vif-bridge-nbsd,v 1.1.1.1 2008/08/07 20:26:57 cegger Exp $
    70.7 +# Called by xenbackendd
    70.8 +# Usage: vif-bridge xsdir_backend_path state
    70.9 +
   70.10 +PATH=/bin:/usr/bin:/sbin:/usr/sbin
   70.11 +export PATH
   70.12 +
   70.13 +xpath=$1
   70.14 +xstatus=$2
   70.15 +
   70.16 +case $xstatus in
   70.17 +6)
   70.18 +	# device removed
   70.19 +	xenstore-rm $xpath
   70.20 +	exit 0
   70.21 +	;;
   70.22 +2)
   70.23 +	xbridge=$(xenstore-read "$xpath/bridge")
   70.24 +	xfid=$(xenstore-read "$xpath/frontend-id")
   70.25 +	xhandle=$(xenstore-read "$xpath/handle")
   70.26 +	iface=xvif$xfid.$xhandle
   70.27 +	echo ifconfig $iface up
   70.28 +	ifconfig $iface up
   70.29 +	brconfig $xbridge add $iface
   70.30 +	echo brconfig $xbridge add $iface
   70.31 +	xenstore-write $xpath/hotplug-status connected
   70.32 +	echo xenstore-write $xpath/hotplug-status connected
   70.33 +	exit 0
   70.34 +	;;
   70.35 +*)
   70.36 +	exit 0
   70.37 +	;;
   70.38 +esac
    71.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    71.2 +++ b/tools/hotplug/NetBSD/vif-ip-nbsd	Tue Nov 04 12:13:42 2008 +0000
    71.3 @@ -0,0 +1,33 @@
    71.4 +#!/bin/sh -e
    71.5 +
    71.6 +# $NetBSD: vif-ip-nbsd,v 1.1.1.1 2008/08/07 20:26:57 cegger Exp $
    71.7 +# Called by xenbackendd
    71.8 +# Usage: vif-ip xsdir_backend_path state
    71.9 +
   71.10 +PATH=/bin:/usr/bin:/sbin:/usr/sbin
   71.11 +export PATH
   71.12 +
   71.13 +xpath=$1
   71.14 +xstatus=$2
   71.15 +
   71.16 +case $xstatus in
   71.17 +6)
   71.18 +	# device removed
   71.19 +	xenstore-rm $xpath
   71.20 +	exit 0
   71.21 +	;;
   71.22 +2)
   71.23 +	xip=$(xenstore-read "$xpath/ip")
   71.24 +	xfid=$(xenstore-read "$xpath/frontend-id")
   71.25 +	xhandle=$(xenstore-read "$xpath/handle")
   71.26 +	iface=xvif$xfid.$xhandle
   71.27 +	echo ifconfig $iface $xip up
   71.28 +	ifconfig $iface $xip up
   71.29 +	xenstore-write $xpath/hotplug-status connected
   71.30 +	echo xenstore-write $xpath/hotplug-status connected
   71.31 +	exit 0
   71.32 +	;;
   71.33 +*)
   71.34 +	exit 0
   71.35 +	;;
   71.36 +esac
    72.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    72.2 +++ b/tools/hotplug/common/Makefile	Tue Nov 04 12:13:42 2008 +0000
    72.3 @@ -0,0 +1,37 @@
    72.4 +XEN_ROOT = ../../../
    72.5 +include $(XEN_ROOT)/tools/Rules.mk
    72.6 +
    72.7 +# OS-independent hotplug scripts go in this directory
    72.8 +
    72.9 +# Xen configuration dir and configs to go there.
   72.10 +XEN_CONFIG_DIR = /etc/xen
   72.11 +
   72.12 +# Xen script dir and scripts to go there.
   72.13 +XEN_SCRIPT_DIR = /etc/xen/scripts
   72.14 +XEN_SCRIPTS =
   72.15 +XEN_SCRIPT_DATA =
   72.16 +
   72.17 +.PHONY: all
   72.18 +all:
   72.19 +
   72.20 +.PHONY: build
   72.21 +build:
   72.22 +
   72.23 +.PHONY: install
   72.24 +install: all install-scripts
   72.25 +
   72.26 +.PHONY: install-scripts
   72.27 +install-scripts:
   72.28 +	[ -d $(DESTDIR)$(XEN_SCRIPT_DIR) ] || \
   72.29 +		$(INSTALL_DIR) $(DESTDIR)$(XEN_SCRIPT_DIR)
   72.30 +	set -e; for i in $(XEN_SCRIPTS); \
   72.31 +	   do \
   72.32 +	   $(INSTALL_PROG) $$i $(DESTDIR)$(XEN_SCRIPT_DIR); \
   72.33 +	done
   72.34 +	set -e; for i in $(XEN_SCRIPT_DATA); \
   72.35 +	   do \
   72.36 +	   $(INSTALL_DATA) $$i $(DESTDIR)$(XEN_SCRIPT_DIR); \
   72.37 +	done
   72.38 +
   72.39 +.PHONY: clean
   72.40 +clean: