[Level 3] The script for demo creating mass zones on OpenSolaris
Days ago, my colleague will demo about the virtualization techniques on OpenSolari.
So I share the script for him to demo fast creating zones on OpenSolaris.
After test, my lap will create a zone per 3 seconds.
That impress him, and he decided to demo on that session.
The same script that I'd like to share to you, please refer the following script code.
Any question, and suggestion, please feel free to let me know.
Wish this helps.
regards,
Stanley Huang
So I share the script for him to demo fast creating zones on OpenSolaris.
After test, my lap will create a zone per 3 seconds.
That impress him, and he decided to demo on that session.
The same script that I'd like to share to you, please refer the following script code.
Any question, and suggestion, please feel free to let me know.
#!/usr/bin/bash
showUsage() {
cat<<EOF
Usage:
$0 [c]reate [original index] [start index] [end index]
$0 [d]estroy [start index] [end index] [always yes]
$0 [l]ist
Ex.
$0 c 1 2 10 => create zone2, zone3, ... ,zone10
$0 c 1 3 8 => create zone3, zone4, ... ,zone8
$0 d 2 10 [F] => destroy zone2, zone3, ... ,zone10
$0 l
PS.
for solaris 11 only
all indexes must greater then 1, less then 255
Zones:
`listZone`
EOF
}
createOriginZone() {
echo "create Zone (zone$nOriginIndex)..."
zfs create -p $sBaseZoneZFS/zone$nOriginIndex
chmod 700 $sBaseZonePath/zone$nOriginIndex
zonecfg -z zone$nOriginIndex <<EOF
create
set zonepath=$sBaseZonePath/zone$nOriginIndex
add net
set physical=$sIF
set address=$sClassIP.$nOriginIndex/$nNetmask
end
commit
EOF
zoneadm -z zone$nOriginIndex install
}
## build snapshot by zoenadm, and create snapshot for every clone.
## case1 $@
case1() {
declare -i nOriginIndex=${1:-1}
declare -i count=${2:-0}
declare -i nZones=${3:-254}
while [ $count -le $nZones ]
do
echo "create zone$count ....."
zonecfg -z zone$nOriginIndex export -f /tmp/zone$count.cfg
perl -pi -e "s/zone$nOriginIndex/zone$count/g" /tmp/zone$count.cfg
perl -pi -e "s|^set address=.*|set address=$sClassIP.$count/$nNetmask|g" /tmp/zone$count.cfg
zonecfg -z zone$count < /tmp/zone$count.cfg
zoneadm -z zone$count clone zone$nOriginIndex
echo "create zone$count done!"
count=$count+1
done
}
## build snapshot manually, and use one snapshot.
## case2 $@
## not work in OpenSolaris...
case2() {
declare -i nOriginIndex=${1:-1}
declare -i count=${2:-0}
declare -i nZones=${3:-254}
## create first clone zone and use it's snapshot for later...
case1 $nOriginIndex $count $count
sOriginSnapshot=`zfs get -H origin $sBaseZoneZFS/zone$count | cut -d' ' -f3`
## zfs snapshot $sOriginZoneZFS@$sTimestamp # not work
count=$count+1
while [ $count -le $nZones ]
do
echo "create zone$count ....."
zonecfg -z zone$nOriginIndex export -f /tmp/zone$count.cfg
perl -pi -e "s/zone$nOriginIndex/zone$count/g" /tmp/zone$count.cfg
perl -pi -e "s|^set address=.*|set address=$sClassIP.$count/$nNetmask|g" /tmp/zone$count.cfg
zonecfg -z zone$count < /tmp/zone$count.cfg
zoneadm -z zone$count clone -s $sOriginSnapshot zone$nOriginIndex
echo "create zone$count done!"
count=$count+1
done
}
createMassZones() {
declare -i nOriginIndex=${1:-1}
declare -i count=${2:-0}
declare -i nZones=${3:-254}
nSleep=5
sTimestamp=`date "+%Y%m%d%H%M%S"`
if [ `zoneadm list -cv | grep -wc zone$nOriginIndex` -eq 0 ]
then
read -p "Do you want to create the zone (zone$nOriginIndex)? (y/N):" yN
if [ "$yN" == "Y" ] || [ "$yN" == "y" ]
then
createOriginZone
zoneadm -z zone$nOriginIndex boot
zlogin -C zone$nOriginIndex
else
echo "Exit program..."
exit 1
fi
fi
zlogin zone$nOriginIndex init 5
sleep $nSleep
case1 $@
#case2 $@
}
destroyMassZones() {
declare -i count=${1:-0}
declare -i nZones=${2:-254}
declare sF=$3
declare sOrigSnap=""
if [ "$sF" == "F" ] || [ "$sF" == "f" ]
then
sF="-F"
else
sF=""
fi
while [ $count -le $nZones ]
do
echo "destroy zone$count ....."
zoneadm -z zone$count halt
zoneadm -z zone$count uninstall $sF
zonecfg -z zone$count delete $sF
## auto delete snapshot/clone after zoneadm -z zone$count uninstall
#sOrigSnap=`zfs get -H origin $sBaseZoneZFS/zone$count/ROOT/zbe | awk '{print($3)}'`
#zfs destroy -R $sOrigSnap
#echo "zfs destroy -R $sOrigSnap"
echo "destroy zone$count done!"
count=$count+1
done
}
listZone() {
zoneadm list -iv
}
######################################################## main
sVersion="5.11"
sIF=yukonx0
sClassIP=172.31.248
nNetmask=24
sBaseZoneZFS=rpool/zones
sBaseZonePath=/export/home/zones
zfs create -o mountpoint=$sBaseZonePath -p $sBaseZoneZFS
chmod 700 $sBaseZonePath
[ `uname -r` != "$sVersion" ] && echo "For Solaris 11 only, exit program..." && exit 3
case $1 in
c|create)
[ $# -lt 4 ] && echo "Error without enough parameters, exit program..." && showUsage && exit 1
shift
createMassZones $@
;;
d|delete)
[ $# -lt 3 ] && echo "Error without enough parameters, exit program..." && showUsage && exit 1
shift
destroyMassZones $@
;;
l|list)
listZone
;;
*)
echo "Error with wrong action($1), exit program..." && showUsage && exit 2
;;
esac
cat<
Usage:
$0 [c]reate [original index] [start index] [end index]
$0 [d]estroy [start index] [end index] [always yes]
$0 [l]ist
Ex.
$0 c 1 2 10 => create zone2, zone3, ... ,zone10
$0 c 1 3 8 => create zone3, zone4, ... ,zone8
$0 d 2 10 [F] => destroy zone2, zone3, ... ,zone10
$0 l
PS.
for solaris 11 only
all indexes must greater then 1, less then 255
Zones:
`listZone`
EOF
}
createOriginZone() {
echo "create Zone (zone$nOriginIndex)..."
zfs create -p $sBaseZoneZFS/zone$nOriginIndex
chmod 700 $sBaseZonePath/zone$nOriginIndex
zonecfg -z zone$nOriginIndex <<EOF
create
set zonepath=$sBaseZonePath/zone$nOriginIndex
add net
set physical=$sIF
set address=$sClassIP.$nOriginIndex/$nNetmask
end
commit
EOF
zoneadm -z zone$nOriginIndex install
}
## build snapshot by zoenadm, and create snapshot for every clone.
## case1 $@
case1() {
declare -i nOriginIndex=${1:-1}
declare -i count=${2:-0}
declare -i nZones=${3:-254}
while [ $count -le $nZones ]
do
echo "create zone$count ....."
zonecfg -z zone$nOriginIndex export -f /tmp/zone$count.cfg
perl -pi -e "s/zone$nOriginIndex/zone$count/g" /tmp/zone$count.cfg
perl -pi -e "s|^set address=.*|set address=$sClassIP.$count/$nNetmask|g" /tmp/zone$count.cfg
zonecfg -z zone$count < /tmp/zone$count.cfg
zoneadm -z zone$count clone zone$nOriginIndex
echo "create zone$count done!"
count=$count+1
done
}
## build snapshot manually, and use one snapshot.
## case2 $@
## not work in OpenSolaris...
case2() {
declare -i nOriginIndex=${1:-1}
declare -i count=${2:-0}
declare -i nZones=${3:-254}
## create first clone zone and use it's snapshot for later...
case1 $nOriginIndex $count $count
sOriginSnapshot=`zfs get -H origin $sBaseZoneZFS/zone$count | cut -d' ' -f3`
## zfs snapshot $sOriginZoneZFS@$sTimestamp # not work
count=$count+1
while [ $count -le $nZones ]
do
echo "create zone$count ....."
zonecfg -z zone$nOriginIndex export -f /tmp/zone$count.cfg
perl -pi -e "s/zone$nOriginIndex/zone$count/g" /tmp/zone$count.cfg
perl -pi -e "s|^set address=.*|set address=$sClassIP.$count/$nNetmask|g" /tmp/zone$count.cfg
zonecfg -z zone$count < /tmp/zone$count.cfg
zoneadm -z zone$count clone -s $sOriginSnapshot zone$nOriginIndex
echo "create zone$count done!"
count=$count+1
done
}
createMassZones() {
declare -i nOriginIndex=${1:-1}
declare -i count=${2:-0}
declare -i nZones=${3:-254}
nSleep=5
sTimestamp=`date "+%Y%m%d%H%M%S"`
if [ `zoneadm list -cv | grep -wc zone$nOriginIndex` -eq 0 ]
then
read -p "Do you want to create the zone (zone$nOriginIndex)? (y/N):" yN
if [ "$yN" == "Y" ] || [ "$yN" == "y" ]
then
createOriginZone
zoneadm -z zone$nOriginIndex boot
zlogin -C zone$nOriginIndex
else
echo "Exit program..."
exit 1
fi
fi
zlogin zone$nOriginIndex init 5
sleep $nSleep
case1 $@
#case2 $@
}
destroyMassZones() {
declare -i count=${1:-0}
declare -i nZones=${2:-254}
declare sF=$3
declare sOrigSnap=""
if [ "$sF" == "F" ] || [ "$sF" == "f" ]
then
sF="-F"
else
sF=""
fi
while [ $count -le $nZones ]
do
echo "destroy zone$count ....."
zoneadm -z zone$count halt
zoneadm -z zone$count uninstall $sF
zonecfg -z zone$count delete $sF
## auto delete snapshot/clone after zoneadm -z zone$count uninstall
#sOrigSnap=`zfs get -H origin $sBaseZoneZFS/zone$count/ROOT/zbe | awk '{print($3)}'`
#zfs destroy -R $sOrigSnap
#echo "zfs destroy -R $sOrigSnap"
echo "destroy zone$count done!"
count=$count+1
done
}
listZone() {
zoneadm list -iv
}
######################################################## main
sVersion="5.11"
sIF=yukonx0
sClassIP=172.31.248
nNetmask=24
sBaseZoneZFS=rpool/zones
sBaseZonePath=/export/home/zones
zfs create -o mountpoint=$sBaseZonePath -p $sBaseZoneZFS
chmod 700 $sBaseZonePath
[ `uname -r` != "$sVersion" ] && echo "For Solaris 11 only, exit program..." && exit 3
case $1 in
c|create)
[ $# -lt 4 ] && echo "Error without enough parameters, exit program..." && showUsage && exit 1
shift
createMassZones $@
;;
d|delete)
[ $# -lt 3 ] && echo "Error without enough parameters, exit program..." && showUsage && exit 1
shift
destroyMassZones $@
;;
l|list)
listZone
;;
*)
echo "Error with wrong action($1), exit program..." && showUsage && exit 2
;;
esac
Wish this helps.
regards,
Stanley Huang
Comments
Post a Comment