Script to Determine Solaris 10 Zone Type

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

5 thoughts on “Script to Determine Solaris 10 Zone Type

  1. 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

  2. Pingback: ZoneType.sh Version 2.0 | spiralbound.net

  3. That works good for determining if you’re in a non-global zone, but what if you’re on an LDom or just a plain ol’ Solaris OS?

    There are probably a hundred other ways to do this, but this works pretty well. Don’t even need to run it as root. It’s just quick and dirty and doesn’t get rid of valid cfgadm output…

    /usr/sbin/cfgadm 2>/dev/null
    ERR_CODE=$?
    case $ERR_CODE in
    0)
    echo Server is plain ol OS install.
    ;;
    1)
    echo Server is Zone.
    ;;
    2)
    echo Server is LDom.
    ;;
    esac

  4. I was asking this same question, here is what I have found. the “/usr/bin/zonename” command is not available on branded zones so that command is not always useful or available. the looking at the init process id is also good but not entirely useful as the global zone will show it’s /sbin/init process as an ID of 1 but will also show the /etc/init processes of all the zones on the server. The local zone will only show it’s own /etc/init process with a non-0 PID. Helpful but looking for two different init processes depending upon server type which is what you are trying to figure out in the first place.

    This last example from Eric is the best I think. But I would modify the command slightly.

    try…..

    #> /usr/sbin/cfgadm > /dev/null 2>&1

    This will send ALL output to /dev/null and give you back only the return code and then use that in his case statement. I found that if you run this on a zone server you get no data back but on the global zone you do and its not helpful.

    Thank you very much for the great options though.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>