#!/bin/ksh
# Ralf Gans Version 2.6 7.8.2006
# install openv
#
# Ralf Gans Version 2.5a 7.8.2006
# create new ssh-keys inside the zone
#
# Ralf Gans Version 2.4 3.8.2006
# disable all services that are disabled in the global zone # # Ralf Gans Version 2.3 3.8.2006 # copy resolv.conf, skel into the zone # and setting nsswitch.dns as default # # Ralf Gans Version 2.2 3.8.2006 # disabled autoboot # # Ralf Gans Version 2.1 3.8.2006 # remove unwanted packages form zone # # Ralf Gans Version 2.0 18.7.2006 # less things to do manualy # # autozone by olaf.bohlen@btc-ag.com # sets up zones automagically

source=$1

if [ -z "${source}" ]; then
        echo "Usage: $0 hosts-file"
        exit 1
fi

cat ${source} | \
grep -v '^#' | while read zoneip zoneadm zonebac zoneapp zonename junk; do
        echo "I: setting up ${zonename}/${zoneip}"
        mkdir -p /export/zones/${zonename}
        chmod 700 /export/zones/${zonename}
        sed -e 's,%%zonename%%,'${zonename}',g;
                s,%%zoneip%%,'${zoneip}',g;
                s,%%zoneadm%%,'${zoneadm}',g;
                s,%%zoneapp%%,'${zoneapp}',g;
                s,%%zonebac%%,'${zonebac}',g;' > ZoneWork.$$ << EOF create -b set zonepath=/export/zones/%%zonename%%
set autoboot=false
add net
set address=%%zoneip%%
set physical=ce0
end
add net
set address=%%zoneapp%%
set physical=ce0
end
add net
set address=%%zoneadm%%
set physical=ce1
end
add net
set address=%%zonebac%%
set physical=ce5
end
EOF
        zonecfg -z ${zonename} < ZoneWork.$$
        rm ZoneWork.$$
        zoneadm -z ${zonename} install
        echo "I: echo setting up sysid for ${zonename}"
        cfg=/export/zones/${zonename}/root/etc/sysidcfg
        rm -f $cfg
        echo "network_interface=primary {hostname=$zonename netmask=255.255.252.0 default_route=NONE}" > $cfg
        echo "system_locale=C" >> $cfg
        echo "nfs4_domain=optimus" >> $cfg
        echo "terminal=xterms" >> $cfg
        echo "timeserver=localhost" >> $cfg
        echo "security_policy=NONE" >> $cfg
        echo "name_service=NONE" >> $cfg
        echo "timezone=MET" >> $cfg
        echo "root_password=qGOaHVxVsB4vU" >> $cfg
        echo "I: NOT booting zone ${zonename}"
        #RPG rm /export/zones/${zonename}/root/etc/.UNCONFIGURED
        # set NFS4 domain
        cp /dev/null /export/zones/${zonename}/root/etc/.NFS4inst_state.domain
        chmod 644 /export/zones/${zonename}/root/etc/.NFS4inst_state.domain
        chown root:root /export/zones/${zonename}/root/etc/.NFS4inst_state.domain
        echo NFS4_DOMAIN=optimus >> /export/zones/${zonename}/root/etc/.NFS4inst_state.domain
        # change root home
        #cp /export/zones/${zonename}/root/etc/passwd /export/zones/${zonename}/root/etc/passwd.$$
        #sed 's#:Super-User:/:#:Super-User:/root:#' < /export/zones/${zonename}/root/etc/passwd.$$ > /export/zones/${zonename}/root/etc/passwd

        # copy templatefiles to zone
        cd ~root/ZoneTools/LocalTemplate && find . -depth -print | cpio -pudm /export/zones/${zonename}/root/
        cp /etc/resolv.conf /export/zones/${zonename}/root/etc
        cp /etc/default/init /export/zones/${zonename}/root/etc/default/
        cp -r /etc/skel /export/zones/${zonename}/root/etc
        cp /export/zones/${zonename}/root/etc/nsswitch.dns /export/zones/${zonename}/root/etc/nsswitch.conf
        svcs -a | grep 'disabled' | awk '{print "/usr/sbin/svcadm disable",$NF}' >> /export/zones/${zonename}/root/etc/rc2.d/S02zone_finish
        echo 'rm /etc/ssh/*key*' >> /export/zones/${zonename}/root/etc/rc2.d/S02zone_finish
        echo '/lib/svc/method/sshd -c' >> /export/zones/${zonename}/root/etc/rc2.d/S02zone_finish
        echo 'cd /etc/inet && cat inetd.conf.openv >> inetd.conf' >> /export/zones/${zonename}/root/etc/rc2.d/S02zone_finish
        echo 'cd /etc/inet && cat services.openv >> services' >> /export/zones/${zonename}/root/etc/rc2.d/S02zone_finish
        echo '/usr/sbin/inetconv' >> /export/zones/${zonename}/root/etc/rc2.d/S02zone_finish
        echo 'rm /etc/rc2.d/S02zone_finish' >> /export/zones/${zonename}/root/etc/rc2.d/S02zone_finish
        touch /export/zones/${zonename}/root/etc/ZoneRev_2.6

        #RPG zoneadm -z ${zonename} boot
done
echo "I: all done"
