#!/usr/bin/shellmod
# This module is used to create a new virtual private server

# This module may be use inside linuxconf
# Do linuxconf --modulemain shellmod --setmod /usr/sbin/newvserver

# Load shellmod support function
. /usr/lib/linuxconf/lib/shellmod-lib.sh

register(){
	qecho regmenu main MENU_MISCSERV "Create a new vserver"
}

check_name(){
	case $1 in
	?|??|???|????)
		return 0
		;;
	*)
		;;
	esac
	return 1
}

check_hostname(){
	case $1 in
	*\.*)
		return 0
		;;
	*)
		;;
	esac
	return 1
}

check_ip(){
	case $1 in
	*\.*\.*\.*)
		return 0
		;;
	*)
		;;
	esac
	return 1
}

check_device(){
	if [ "$1" = "" ] ; then
		return 1;
	fi
	return 0
}


# Point d'entr du module
main(){
	qecho DIALOG
	qecho newf_title top 1 top
	qecho newf_str name "Vserver name (max 4 chars)"
	qecho newf_str desc "Vserver description"
	qecho newf_list clone "Clone vserver" /
	for conf in /etc/vservers/*.conf
	do
		case $conf in
		/etc/vservers/\*.conf)
			;;
		*)
			qecho listitem `basename $conf .conf`
			;;
		esac
	done
	qecho listitem / "Root server"
	qecho newf_chk unify "Unified mode" 1 "Share disk space"

	qecho newf_title Networking 1 Networking
	qecho newf_str hostname "Host name"
	qecho newf_str ip "IP number"
	qecho newf_str ondev "Install IP on device" eth0
	qecho newf_str domainname "NIS domainname"

	qecho newf_title Extra 1 Extra
	qecho newf_chk onboot "Start server" 0 "at boot time"
	qecho newf_str nice "Nice level"
	qecho newf_str flags "Flags" "lock nproc"
	qecho newf_str ulimit "Vserver ulimit" "-H -u 1000"

	qecho newf_title Services 1 Services
	qecho newf_chk crond  "crond"  1 "Scheduled tasks"
	qecho newf_chk httpd  "httpd"  0 "Web server"
	qecho newf_chk sshd   "sshd"   1 "Secure shell server"
	qecho newf_chk syslog "syslog" 1 "Message logger"
	qecho newf_chk xinetd "xinetd" 0 "On demand inet service"

	qecho newf_title "Shared directories" 1 "Shared directories"
	#qecho newf_info "" "Directories from the root server"
	qecho newf_str dir1 "Directory"
	qecho newf_str dir2 "Directory"
	qecho newf_str dir3 "Directory"
	qecho newf_str dir4 "Directory"

	while true
	do
		#echo defval s1 line 1
		#echo defval s1 line 2
		#echo notice =s1
		qecho edit "Vserver basic setup" 
		dispatch
		if [ $CODE != "accept" ] ; then
			break
		elif ! check_name $name ; then
			qecho error "You must provide a name (4 chars max)"
		elif ! check_hostname $hostname ; then
			qecho error "You must provide a valid/fully qualified host name"
		elif ! check_ip $ip ; then
			qecho error "You must provide a valid IP number"
		elif ! check_device $ondev ; then
			qecho error "You must provide a valid network device"
		else
			STARTTIME=`date +%s`
			ONBOOT=no
			if [ "$onboot" = "1" ] ; then
				ONBOOT=yes
			fi
			CONF=/etc/vservers/$name.conf
			mkdir -p /etc/vservers
			rm $f $CONF >/dev/null 2>/dev/null
			echo "# Description: $desc" >>$CONF
			echo >>$CONF
			echo "# Select an unused context (this is optional)" >>$CONF
			echo "# The default is to allocate a free context on the fly" >>$CONF
			echo "# In general you don't need to force a context" >>$CONF
			echo "#S_CONTEXT=" >>$CONF
			echo "# Select the IP number assigned to the virtual server" >>$CONF
			echo "# This IP must be one IP of the server, either an interface" >>$CONF
			echo "# or an IP alias" >>$CONF
			echo "IPROOT=$ip" >>$CONF
			echo "# You can define on which device the IP alias will be done" >>$CONF
			echo "# The IP alias will be set when the server is started and unset" >>$CONF
			echo "# when the server is stopped" >>$CONF
			echo "IPROOTDEV=$ondev" >>$CONF
			echo "# Uncomment the onboot line if you want to enable this" >>$CONF
			echo "# virtual server at boot time" >>$CONF
			echo "ONBOOT=$ONBOOT" >>$CONF
			echo "# You can set a different host name for the vserver" >>$CONF
			echo "# If empty, the host name of the main server is used" >>$CONF
			echo "S_HOSTNAME=$hostname" >>$CONF
			echo "# You can set a different NIS domain for the vserver" >>$CONF
			echo "# If empty, the current on is kept" >>$CONF
			echo "S_DOMAINNAME=$domainname" >>$CONF
			echo "# You can set the priority level (nice) of all process in the vserver" >>$CONF
			echo "# Even root won't be able to raise it" >>$CONF
			echo "S_NICE=$nice" >>$CONF
			echo "# You can set various flags for the new security context" >>$CONF
			echo "# lock: Prevent the vserver from setting new security context" >>$CONF
			echo "# sched: Merge scheduler priority of all processes in the vserver" >>$CONF
			echo "#        so that it acts a like a single one." >>$CONF
			echo "# nproc: Limit the number of processes in the vserver according to ulimit" >>$CONF
			echo "#        (instead of a per user limit, this becomes a per vserver limit)" >>$CONF
			echo "# private: No other process can join this security context. Even root" >>$CONF
			echo "# Do not forget the quotes around the flags" >>$CONF
			echo "S_FLAGS=\"$flags\"" >>$CONF
			echo "# You can set various ulimit flags and they will be inherited by the" >>$CONF
			echo "# vserver. You enter here various command line argument of ulimit" >>$CONF
			echo "# ULIMIT=\"-H -u 200\"" >>$CONF
			echo "# The example above, combined with the nproc S_FLAGS will limit the" >>$CONF
			echo "# vserver to a maximum of 200 processes" >>$CONF
			echo "ULIMIT=\"$ulimit\"" >>$CONF
			echo "# You can set various capabilities. By default, the vserver are run" >>$CONF
			echo "# with a limited set, so you can let root run in a vserver and not" >>$CONF
			echo "# worry about it. He can\'t take over the machine. In some cases" >>$CONF
			echo "# you can to give a little more capabilities \(such as CAP_NET_RAW\)" >>$CONF
			echo "# S_CAPS=\"CAP_NET_RAW\"" >>$CONF
			echo "S_CAPS=\"\"" >>$CONF

			# Now we create the optional companion startup script
			# for the vserver
			SCRIPT=/etc/vservers/$name.sh
			echo "#!/bin/sh" >$SCRIPT
			echo 'case $1 in' >>$SCRIPT
			echo "pre-start)" >>$SCRIPT
			for dir in $dir1 $dir2 $dir3 $dir4  none
			do
				if [ "$dir" != "none" ] ; then
					echo "	mkdir -p /vservers/$name/$dir" >>$SCRIPT
					echo "	mount --bind $dir /vservers/$name/$dir" >>$SCRIPT
				fi
			done
			echo "	;;" >>$SCRIPT
			echo "post-start)" >>$SCRIPT
			echo "	;;" >>$SCRIPT
			echo "pre-stop)" >>$SCRIPT
			echo "	;;" >>$SCRIPT
			echo "post-stop)" >>$SCRIPT
			for dir in $dir1 $dir2 $dir3 $dir4  none
			do
				if [ "$dir" != "none" ] ; then
					echo "	umount /vservers/$name/$dir" >>$SCRIPT
				fi
			done
			echo "	;;" >>$SCRIPT
			echo '*)' >>$SCRIPT
	    	echo '	echo $0 pre-start' >>$SCRIPT
			echo '	echo $0 pre-stop' >>$SCRIPT
			echo '	echo $0 post-start' >>$SCRIPT
			echo '	echo $0 post-stop' >>$SCRIPT
			echo "	;;" >>$SCRIPT
			echo "esac" >>$SCRIPT
			chmod +x $SCRIPT

			LOG=/var/run/newvserver.log.$$
			if [ "$clone" = "/" ] ; then
				# Unification does not work on / yet
				/usr/sbin/vserver $name build >$LOG
			elif [ "$unify" = "0" ] ; then
				cp -ax /vservers/$clone /vservers/$name >$LOG
			else
				/usr/lib/vserver/vbuild --stats /vservers/$clone /vservers/$name >$LOG
			fi
			test "$crond" = 1 && /usr/sbin/vserver $name chkconfig crond on >/dev/null
			test "$httpd" = 1 && /usr/sbin/vserver $name chkconfig httpd on >/dev/null
			test "$sshd" = 1 && /usr/sbin/vserver $name chkconfig sshd on >/dev/null
			test "$syslog" = 1 && /usr/sbin/vserver $name chkconfig syslog on >/dev/null
			test "$xinetd" = 1 && /usr/sbin/vserver $name chkconfig xinetd on >/dev/null
			host0=`echo $hostname | sed 's/\./ /g' | ( read a b; echo $a)`
			echo $ip $hostname $host0 localhost >/vservers/$name/etc/hosts
			# Umount proc and /dev/pts
			/usr/sbin/vserver $name stop >/dev/null
			ENDTIME=`date +%s`
			DURATION=`expr $ENDTIME - $STARTTIME`

			echo defval s1 Server $name was installed in /vservers/$name
			echo defval s1 The configuration file /etc/vservers/$name.conf was created
			echo defval s1 The script /etc/vservers/$name.sh was created
			echo defval s1 Vserver $name was created in $DURATION seconds
			echo notice =s1
			rm -f $LOG
			break
		fi
	done
	qecho end
}

dispatch
