#!/usr/bin/ksh
#
# zhostid - demo changing hostids for Solaris Zones.
#           Written using DTrace (Solaris 10 3/05).
#
# WARNING: This is a demonstration of DTrace, it is not intended as a 
# standard daemon. In particular, hostids are used by Sun to track support 
# calls, so changing hostids may make life somewhat confusing for all.
#
# 21-Jun-2005, ver 0.70		(first release)
#
# USAGE:	zhostid &
#
# Edit the "Configuration" section below to set the zones and
# hostids to what is desirable.
#
# BASED ON: hostid.d by Iain Hayes, and idea by Jon Haslam.
#
# COPYRIGHT: Copyright (c) 2005 Brendan Gregg.
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#  (http://www.gnu.org/copyleft/gpl.html)
#
# 21-Jun-2005	Brendan Gregg	Created this.

#
#  Configuration
#
hostids='
		global		12345678
		workzone1	90abcdef

' # simply modify the above by adding extra lines for each zone.

#
#  Check hostids
#
print "$hostids" | while read zone hostid_hex; do
	### Sanity check hostid
	if [[ "$zone" == "" || "$zone" == "#" ]]; then continue; fi
	if [[ "$hostid_hex" == *[g-zG-Z]* ]]; then
		print "ERROR2: Invalid hostid $hostid_hex. "
		print "Please use hexadecimal.\n"
		exit 2
	fi
	if (( ${#hostid_hex} > 11 )); then
		# see /usr/src/uts/common/conf/param.c for limit.
		print "ERROR3: Length of hostid $hostid_hex too long. "
		print "Limit 11 chars.\n"
		exit 3
	fi

	### Convert hostid to decimal
	typeset -i10 hostid_dec
	hostid_dec=16#$hostid_hex

	### Build DTrace code
	body="$body 
		syscall::systeminfo:return
		/zonename == \"$zone\" && self->command == 7/
		{
			copyoutstr(\"$hostid_dec\", self->buffer, 11);
		}"
done

#
#  Run DTrace
#
exec /usr/sbin/dtrace -n '

 #pragma D option destructive
 #pragma D option quiet
 #pragma D option bufsize=32k
 
 inline string hostid = "'$hostid_dec'";

 syscall::systeminfo:entry
 {
	self->command = arg0;
	self->buffer = arg1;
 }

 '"$body"'

 syscall::systeminfo:return
 {
	self->command = 0;
	self->buffer = 0;
 }
' 
