We use a lot of local zones in our Solaris 10 environment. We also use cfengine pretty heavily and there are some instances when we need to include or exclude certain automated tasks based on what type of zone we are working with. I wrote this little script that checks to see what type of zone we are dealing with. Based on the return value, I can set a cfengine class and control what gets run and where.
- Return 0 if the machine is a global zone with 1 or more local zones
- Return 1 if the machine is either a local zone or a global zone with 0 local zones
#! /bin/bash
#
# When issued with the -g or --global flag, this script will return:
# 0 if the machine is a global zone and has one or more local zones.
# Otherwise, it will return 1
#
# Wen issued with the -l or --local flag, this script will return:
# 0 if if is a local zone and 1 if it is not
#
list=( `/usr/sbin/zoneadm list -civ | awk '{ print $1 }'`)
case "$1" in
-g|--global)
# If the third element in our array is null, set it to 0
if [ "${list[2]}" == "" ]; then
list[2]=0
fi
# This is a global zone only if it has one or more local zones.
if [ ${list[1]} -eq 0 ] && [ ${list[2]} -ge 1 ]; then
# 1 is returned if we have a global and local zone, otherwise, we return 0
exit 0
else
exit 1
fi
;;
-l|--local)
# If the second element in our array is = or > 1, it is a local zone.
if [ ${list[1]} -ge 1 ]; then
# Return 1 if this is a local zone, otherwise return 0.
exit 0
else
exit 1
fi
;;
*)
echo "Usage: /local/adm/zonetype.sh {-l | --local | -g | --global}"
exit 1
esac
This entry was posted
on Tuesday, March 3rd, 2009 at 6:46 pm and is filed under Data and Technology.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

I love using UNIX because there are so many ways to do the same thing.
My tip for finding if I’m in a local zone or global is to look at the init process id.
Global zone id = 1
Local zone != 1
Andy
Thanks for the tip. Here is another way to skin this cat:
[ `/usr/bin/zonename` = "global" ] && exit 0 || exit 1
Juan
[...] Script to Determine Solaris 10 Zone TypeSolaris X86 Compatible RAID ControllerInstall Solaris Package in Alternate Base Directory [...]