Create a zone using ZFS in 1 second (or less)

Following up on yesterday's blog entry, I decided to let my curiosity get the best of me. I created a script to create a zone using the steps I outlined in that blog. Using the "date" command, I timed it to 1 second (or so).

Below is the script output and the script, which was done in a hurry and does a lousy job of error checking (use at your own risk). It can be a framework from something more formal, though.

Here is the output from my test:


root@sicilian /var/tmp> ./createzfszone.bash
\*\*\*\*\* WARNING \*\*\*\*\* WARNING \*\*\*\*\* WARNING \*\*\*\*\* WARNING \*\*\*\*\*
The zone configured by this script is not supported
Because this script is a hack, it may not work in future solaris versions

The script requires pre-requisite steps to be taken:
1) Create a zone on a zfs mountpoint using normal zone creation steps
2) Create a snapshot of the zone \*before\* booting it

Example:
# zonecfg -z myzone -f myzone.template
# zoneadm -z myzone install
# zfs snapshot  @initial
#
## where  is the zfs dataset where the zone is installed
Running zfs list ...
NAME                   USED  AVAIL  REFER  MOUNTPOINT
usbpool               4.09G  32.9G     8K  /usbpool
usbpool/jesclone      25.6M  32.9G  3.55G  /zones2
usbpool/zones         4.06G  32.9G  2.84G  /zones
usbpool/zones@initial   323M      -  3.54G  -
usbpool/zones@PostAppserver  6.81M      -  3.74G  -

Enter zfs basline dataset
usbpool/zones@initial

Enter zfs dataset mountpoint
/zonestest

Enter zone directory (should be under the ZFS mountpoint)
/zonestest/jes

Enter network interface for zone [e.g. hme0]
e1000g0

Enter an IP address for the zone
192.168.200.5

Enter dataset name for zfs clone
usbpool/zonestest

Enter the name of the new zone
zonestest
Thu Dec  8 09:05:44 PST 2005
Thu Dec  8 09:05:45 PST 2005
root@sicilian /var/tmp> zoneadm -z zonestest boot
zoneadm: zone 'zonestest': WARNING: e1000g0:1: no matching subnet found in netmasks(4) for 192.168.200.5; using default of 255.255.255.0.
root@sicilian /var/tmp>


Here is the script:

#!/bin/bash

TEMPLATE=/tmp/zone.template
TMPINDEX=/tmp/index


################################
# Print user warning
################################

user_warning() {
echo "\*\*\*\*\* WARNING \*\*\*\*\* WARNING \*\*\*\*\* WARNING \*\*\*\*\* WARNING \*\*\*\*\*"
echo "The zone configured by this script is not supported"
echo "Because this script is a hack, it may not work in future solaris versions"echo
echo "The script requires pre-requisite steps to be taken:"
echo "1) Create a zone on a zfs mountpoint using normal zone creation steps"
echo "2) Create a snapshot of the zone \*before\* booting it"
echo
echo "Example:"
echo "# zonecfg -z myzone -f myzone.template"
echo "# zoneadm -z myzone install"
echo "# zfs snapshot ${zone_dataset} ${zone_dataset}@initial"
echo "#"
echo "## where ${zone_dataset} is the zfs dataset where the zone is installed"
}

################################
# Select zfs dataset to snapshot
################################

get_user_options() {
echo "Running zfs list ..."

ret_val=1

while [ ${ret_val} -ne 0 ];
do
   zfs list
   echo
   echo
   echo Enter zfs basline dataset
   read DATASET

   zfs list | grep "${DATASET}" > /dev/null 2>&1
   ret_val=$?

   if [ ${ret_val} -ne 0 ];
   then
      echo Invalid dataset name
   fi

done

echo
echo "Enter zfs dataset mountpoint"
read ZFS_MOUNT

if [ ! -d ${ZFS_MOUNT} ];
then
   echo "Directory does not exist. Please create the directory"
   echo "or select a new directory"

   exit 1
fi

echo
echo "Enter zone directory (should be under the ZFS mountpoint)"
read ZONE_PATH

echo
echo "Enter network interface for zone [e.g. hme0] "
read INTERFACE_NAME

interfaces=`/usr/sbin/ifconfig -a`
echo $interfaces | grep ${INTERFACE_NAME} > /dev/null 2>&1
ret_val=$?

if [ ${ret_val} -ne 0 ];
then
    echo "${INTERFACE_NAME} is an invalid interface"
    exit 1
fi

echo
echo "Enter an IP address for the zone"
read IP_ADDRESS

echo
echo "Enter dataset name for zfs clone"
read CLONE_NAME

echo
echo "Enter the name of the new zone"
read ZONE_NAME

return
}

###################################
# Create the zone template
# (input to zonecfg)
###################################

create_zone_template() {
#
# Create the zone template
#
cat > ${TEMPLATE} << EOF
create -b
set zonepath=${ZONE_PATH}
set autoboot=false

add net
set physical=${INTERFACE_NAME}
set address=${IP_ADDRESS}
end

verify
commit
EOF
}

#########################################
# Get options from the user
#########################################

user_warning
get_user_options

date

#########################################
# Create the zone template
#########################################

create_zone_template

zonecfg -z ${ZONE_NAME} -f ${TEMPLATE}

zfs clone ${DATASET} ${CLONE_NAME}
zfs set mountpoint=${ZFS_MOUNT} ${CLONE_NAME}
cp /etc/zones/index ${TMPINDEX}
cat ${TMPINDEX} | sed "s/configured/installed/" > /etc/zones/index

rm ${TEMPLATE}

date
The End.

Comments:

Awesome.

Posted by Tom on December 09, 2005 at 11:19 AM PST #

I have a question. Is there a limit to the number of datasets that can be added to a zone? I've tried creating a zone on an X4500 machine and noticed that only the first 10 datasets were accessible from the zone. The rest are not even displayed when I run "zfs list" nor "df -h".

Posted by edcel on April 29, 2007 at 04:45 PM PDT #

Great question. I haven't tried this or come across info on it. Try posting to the OpenSolaris zones alias:

http://www.opensolaris.org/os/community/zones/discussions/

Posted by John Clingan on April 29, 2007 at 04:55 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

John Clingan

Search

Archives
« July 2014
SunMonTueWedThuFriSat
  
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
31
  
       
Today