#! /bin/bash
#
# unplug-vcpus          Unplug extra VCPUs
#
# chkconfig: 2345 20 01
# description: Unplug extra VCPUs

[ -e /proc/xen ] || exit 0

# Source function library.
. /etc/init.d/functions

start() {
	echo -n $"Unplugging extra VCPUs: "

	# Record boot-time CPU config for xapi
        cat /proc/cpuinfo >/etc/xensource/boot_time_cpus
	touch /etc/xensource/boot_time_info_updated

    # Perform numeric sort on list of cpus, so:
    # cpu1 < cpu 2 < cpu10
    BASE="/sys/devices/system/cpu/cpu"
    POS=$(( ${#BASE} + 1 ))
    CPUS=$(/bin/ls -1d ${BASE}* | /usr/bin/cut -c ${POS}- | /bin/sort -n | /bin/sed "s.^.${BASE}.g")

	for i in ${CPUS}; do
	  cpu=$(basename $i)
	  if [ "${cpu}" != "cpu0" ]; then
	    if [ $(cat $i/online) -ne 0 ]; then
	      echo 0 > $i/online && echo -n "${cpu} "
	    fi
	  fi
	done

	echo -n $""
	success $""
	echo
	return 1
}

stop() {
	return 0
}

case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  *)
	echo $"Usage: $0 {start|stop}"
	exit 1
esac
