Solaris 10 Zone Creation Script

One of the best things about Solaris 10, from Sun Microsystems, is Zones or Containers.  They allow you to create virtual OS installs on the same box, yet have them quite separate from each other.  Processes are segregated, resources can be capped, the options go on and on.  Here I have a PERL script (have I mentioned that I love PERL lately?), that makes the creation of zones a snap.  The only thing to edit in the script is at the top, where you set your zone base directory, as in the directory that will hold the zones your create.  I am a simple man, and usually just stick them all in /data/zones, with /data usually being a separate mount point and thus separate I/O path.

***NOTE: This script was written a few years ago and I have no Solaris 10 machine to test it on NOW, so I offer this script AS IS with NO WARRANTY AT ALL! I hope it will help you, but if problems arise from it’s use, you cannot hold me responsible. That being said, if you find it useful (as I did when I wrote it) please let me know that it is still working.***

#!/usr/bin/perl

# This will be the base zone dir used with zonedir below
$bzd="/data/zones";

system(clear);
print "\nSolaris Zone Maker\n";
print "---------------------\n\n";

print "What is the name of the new zone to be created? ";
$newzone = ;
chomp($newzone);

print "\nWhat is the name of the directory to be used for this zone? [$newzone] ";
$zonedir = ;
chomp($zonedir);
if (!$zonedir) {
 $zonedir = $newzone;
}

print "\nWhat is the IP address to use? ";
$newip = ;
chomp($newip);

print "\nWhat is name of the ethernet interface to bind the IP address to? (ex: bge0) ";
$ethint = ;
chomp($ethint);

print "\nDo you want to inherit standard directories (lib,platform,sbin,usr) from the global zone? [yN] ";
$inh = ;
chomp($inh);
if (!$inh) {
  $inh = "n";
}
if (($inh eq "y") || ($inh eq "Y")) {
  $isw = "1";
} else {
  $isw = "0";
}

print "\n\nPlease verify the following information:\n\n";
print "           Zone Name: $newzone\n";
print "      Zone Directory: $zonedir\n";
print "     Zone IP Address: $newip\n";
print "  Ethernet Interface: $ethint\n";
print " Inherit Directories: $inh\n";

print "\nAre these entries correct? [Yn] ";
$yn = ;
chomp($yn);
if (!$yn) { $yn = "y"; }
if (($yn == "y") || ($yn == "Y")) {

 $of = "/tmp/zccfs10.tmp";

 # Create the zonecfg command file
 `echo "create -b" > $of`;
 `echo "set zonepath=$bzd/$zonedir" >> $of`;
 `echo "set autoboot=true" >> $of`;
 if ($isw == "1") {
   `echo "add inherit-pkg-dir" >> $of`;
   `echo "set dir=/lib" >> $of`;
   `echo "end" >> $of`;
   `echo "add inherit-pkg-dir" >> $of`;
   `echo "set dir=/platform" >> $of`;
   `echo "end" >> $of`;
   `echo "add inherit-pkg-dir" >> $of`;
   `echo "set dir=/sbin" >> $of`;
   `echo "end" >> $of`;
   `echo "add inherit-pkg-dir" >> $of`;
   `echo "set dir=/usr" >> $of`;
   `echo "end" >> $of`;
 }
 `echo "add net" >> $of`;
 `echo "set address=$newip" >> $of`;
 `echo "set physical=$ethint" >> $of`;
 `echo "end" >> $of`;

 # Make the dir that the zone will live in
 `mkdir $bzd/$zonedir`;
 `chmod 700 $bzd/$zonedir`;

 # Now, create the zone dude!
 print "\nCreating the zone ... \n";
 `zonecfg -z $newzone -f $of`;
 print "Done!\n";

 # Install the zone
 print "Installing the zone, this will take awhile ... \n";
 `zoneadm -z $newzone install`;
 print "Done!\n";

 # Boot the zone
 print "Now booting the zone ... \n";
 `zoneadm -z $newzone boot`;
 print "Done!\n";

 # Remove the config file
 `rm $of`;

 print "\nZone setup complete, connect to the virtual console with the following command: \n";
 print "  -> zlogin -C -e\\@ $newzone <- *Exit by typing @.\n\n";

} else {
 die("Script execution halted.\n");
}

6 thoughts on “Solaris 10 Zone Creation Script”

  1. one thing id like to note. add near the beginning. What is the base dir of all zones and grab that as the basedir variable. that way it reduces the number of queries and keep in mind everyones env and dir structure are different

  2. Hi sanjeev, sorry about the script, I must have missed that it got lost when our forums went down. I am having trouble finding the script now, it’s been a few years, but I am going to see if I can get it off an old backup and post it. Check back in a bit!

Tell me what you are thinking?