Jumpstart and Zone Creation


One of the many cool features available in Solaris is Zones. As Sean has already blogged automation is the name of the game, and part of this is creating required zones when we jumpstart a machine.

The example here is quite striped down, and creates three very simple zones once a machine is jumpstarted, with very limited configuration - but using zonecfg(1M) and the various resource management utilities you can modify this to suit your needs. This example was tested on the latest Solaris Express.

Anyway, enough talk, you can download the script here, and for your persual...

 

#!/bin/sh
#
# create (very) simple zones after a jumpstart
# 
# this finish script gives an example of how you can create
# some zones post jumpstart. You need to know the name(s) of
# the zones you wish to create, their ip addresses and the 
# network interface you wish to use. It is assumed that netmasks
# have been set up correctly during your install
#
# In the example here we create three zones on a 192.1.1 subnet
# each using bge0 as their interface. 
#
# The script runs as follows
#
#	Section 1 - As a jumpstart finish script
#	-------------------------------------------------
#	1. append details on the zone to your hosts file
#	   where applicable
#	2. create the initial directory for the zone(s)
#	3. create the zonecfg input file
#	4. Create the rc3.d script we will use after reboot
#
#	Section 2 - After initial reboot
#	-------------------------------------------------
#	1. create and install zone(s)
#	2. set zone state to ready
#	3. boot zone
#
#
# Each zone is based on a very simple spare root zone, for further
# details on Zones and the various configuration options consult the
# manpages for zones(5), zoneadm(1M) and zonecfg(1M)
#
# Additional documentation on zones along with tips and articles
# can be found in the Zones section of BigAdmin at
# http://www.sun.com/bigadmin/content/zones/
#
# ZONE_DETAILS - colon seperated listed of zones, containing
#		 zone-name:ip address:interface to use
#

ZONE_DETAILS="test-js-zone-1:192.1.1.1:bge0
test-js-zone-2:192.1.1.2:bge0
test-jes-zone-3:192.1.1.3:bge0"

# ZONE_BASE - root directory for your zones to be created in
#
 
ZONE_BASE="/export/zones"

appendToHostFile() {

	LZONE_NAME=$1
	LZONE_IP=$2

	if [ ! -f $MOUNT_PREFIX/etc/hosts.prezone ]
	then
		cp $MOUNT_PREFIX/etc/hosts $MOUNT_PREFIX/etc/hosts.prezone
	fi

	grep -v $LZONE_NAME $MOUNT_PREFIX/etc/hosts > /tmp/hosts 
	echo "$LZONE_IP	$LZONE_NAME" >> /tmp/hosts
	mv /tmp/hosts $MOUNT_PREFIX/etc/hosts
}

createZoneCfg() {

	LZONE_NAME=$1
	LZONE_IP=$2
	LZONE_INTERFACE=$3

	cat >$MOUNT_PREFIX/$ZONE_BASE/zonecfg/$LZONE_NAME.zcf<<EOF_zonecfg
create -b
set zonepath=$ZONE_BASE/$LZONE_NAME
set autoboot=true
add inherit-pkg-dir
set dir=/lib
end
add inherit-pkg-dir
set dir=/platform
end
add inherit-pkg-dir
set dir=/sbin
end
add inherit-pkg-dir
set dir=/usr
end
add net
set address=$LZONE_IP
set physical=$LZONE_INTERFACE
end
verify
commit
EOF_zonecfg
}

createZoneDirs() {
	
	LZONE_NAME=$1

	if [ ! -d $ZONE_BASE/$LZONE_NAME ]
	then
		mkdir -p $MOUNT_PREFIX/$ZONE_BASE/$LZONE_NAME
		chmod 700 $MOUNT_PREFIX/$ZONE_BASE/$LZONE_NAME
	fi
}

if [ -d /a ]
then
	MOUNT_PREFIX=/a
else
	MOUNT_PREFIX=/
fi

if [ ! -d $MOUNT_PREFIX/$ZONE_BASE/zonecfg ]
then
	mkdir -p $MOUNT_PREFIX/$ZONE_BASE/zonecfg
fi

cat > $MOUNT_PREFIX/etc/rc3.d/S99zonejumpstartexample <<EOF_zonerc
#!/bin/sh

EOF_zonerc

for i in $ZONE_DETAILS
do
	ZONE_NAME=`echo $i | cut -d":" -f1`
	ZONE_IP=`echo $i | cut -d":" -f2`
	ZONE_INTERFACE=`echo $i | cut -d":" -f3`

	createZoneDirs $ZONE_NAME
	createZoneCfg $ZONE_NAME $ZONE_IP $ZONE_INTERFACE
	appendToHostFile $ZONE_NAME $ZONE_IP

cat >> $MOUNT_PREFIX/etc/rc3.d/S99zonejumpstartexample <<EOF_zonerc

/usr/sbin/zonecfg -z $ZONE_NAME -f $ZONE_BASE/zonecfg/$ZONE_NAME.zcf
/usr/sbin/zoneadm -z $ZONE_NAME install
rm -f $ZONE_BASE/$ZONE_NAME/root/etc/.UNCONFIGURED
touch $ZONE_BASE/$ZONE_NAME/root/etc/.NFS4inst_state.domain
/usr/sbin/zoneadm -z $ZONE_NAME ready
/usr/sbin/zoneadm -z $ZONE_NAME boot

EOF_zonerc

done

cat >> $MOUNT_PREFIX/etc/rc3.d/S99zonejumpstartexample <<EOF_zonerc

rm /etc/rc3.d/S99zonejumpstartexample
EOF_zonerc

chmod +x $MOUNT_PREFIX/etc/rc3.d/S99zonejumpstartexample

One caveat, to set the root password do

bash-3.00# zlogin test-js-zone-2
[Connected to zone 'test-js-zone-2' pts/51]
# passwd -r files root
New Password:
Re-enter new Password:
passwd: password successfully changed for root

Technorati Tag(s) : ,
Comments:

Post a Comment:
Comments are closed for this entry.
About

fintanr

Search

Archives
« April 2014
MonTueWedThuFriSatSun
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today