--- /usr/src/etc/rc.d/jail	2009-10-25 02:10:29.000000000 +0100
+++ /etc/rc.d/jail	2010-06-24 06:38:45.000000000 +0200
@@ -38,6 +38,7 @@
 	_fdescdir="${_devdir}/fd"
 	_procdir="${_rootdir}/proc"
 	eval _hostname=\"\$jail_${_j}_hostname\"
+	eval _name=\"\$jail_${_j}_name\"
 	eval _ip=\"\$jail_${_j}_ip\"
 	eval _interface=\"\${jail_${_j}_interface:-${jail_interface}}\"
 	eval _exec=\"\$jail_${_j}_exec\"
@@ -51,7 +52,7 @@
 
 	eval _exec_start=\"\${jail_${_j}_exec_start:-${jail_exec_start}}\"
 
-	i=1
+	i=0
 	while : ; do
 		eval _exec_afterstart${i}=\"\${jail_${_j}_exec_afterstart${i}:-\${jail_exec_afterstart${i}}}\"
 		[ -z "$(eval echo \"\$_exec_afterstart${i}\")" ] &&  break
@@ -60,6 +61,13 @@
 
 	i=0
 	while : ; do
+		eval _exec_earlypoststart${i}=\"\${jail_${_j}_exec_earlypoststart${i}:-\${jail_exec_earlypoststart${i}}}\"
+		[ -z "$(eval echo \"\$_exec_earlypoststart${i}\")" ] && break
+		i=$((i + 1))
+	done
+
+	i=0
+	while : ; do
 		eval _exec_poststart${i}=\"\${jail_${_j}_exec_poststart${i}:-\${jail_exec_poststart${i}}}\"
 		[ -z "$(eval echo \"\$_exec_poststart${i}\")" ] && break
 		i=$((i + 1))
@@ -94,6 +102,9 @@
 			fi
 		fi
 	fi
+	
+	# JAIL new style
+	eval _v2=\"\${jail_v2_enable:-"NO"}\"
 
 	# The default jail ruleset will be used by rc.subr if none is specified.
 	eval _ruleset=\"\${jail_${_j}_devfs_ruleset:-${jail_devfs_ruleset}}\"
@@ -110,18 +121,26 @@
 	eval _fstab=\"\${jail_${_j}_fstab:-${jail_fstab}}\"
 	[ -z "${_fstab}" ] && _fstab="/etc/fstab.${_j}"
 	eval _flags=\"\${jail_${_j}_flags:-${jail_flags}}\"
-	[ -z "${_flags}" ] && _flags="-l -U root"
+	if checkyesno _v2; then
+		[ -z "${_flags}" ] && _flags="-l -U root -c"
+	else
+		[ -z "${_flags}" ] && _flags="-l -U root"
+	fi
 	eval _consolelog=\"\${jail_${_j}_consolelog:-${jail_consolelog}}\"
 	[ -z "${_consolelog}" ] && _consolelog="/var/log/jail_${_j}_console.log"
 	eval _fib=\"\${jail_${_j}_fib:-${jail_fib}}\"
+	eval _vnet=\"\${jail_${_j}_vnet_enable:-"NO"}\"
 
 	# Debugging aid
 	#
+	debug "$_j v2 enable: $_v2"
 	debug "$_j devfs enable: $_devfs"
 	debug "$_j fdescfs enable: $_fdescfs"
 	debug "$_j procfs enable: $_procfs"
 	debug "$_j mount enable: $_mount"
+	debug "$_j vnet enable: $_vnet"
 	debug "$_j hostname: $_hostname"
+	debug "$_j name: $_name"
 	debug "$_j ip: $_ip"
 	jail_show_addresses ${_j}
 	debug "$_j interface: $_interface"
@@ -145,7 +164,7 @@
 
 	debug "$_j exec start: $_exec_start"
 
-	i=1
+	i=0
 	while : ; do
 		eval out=\"\${_exec_afterstart${i}:-''}\"
 
@@ -480,6 +499,17 @@
 		"")	continue ;;
 		*)	;;
 		esac
+		# Append address to list of addresses for the jail command.
+		case "${_type}" in
+			"inet") case "${_addrlv4}" in
+				"")     _addrlv4="${_addr}" ;;
+				*)      _addrlv4="${_addrlv4},${_addr}" ;;
+				esac;;
+			"inet6") case "${_addrlv6}" in
+				"")     _addrlv6="${_addr}" ;;
+				*)      _addrlv6="${_addrlv6},${_addr}" ;;
+				esac;;
+		esac
 
 		# Append address to list of addresses for the jail command.
 		case "${_addrl}" in
@@ -567,6 +597,8 @@
 			continue;
 		fi
 		_addrl=""
+		_addrlv4=""
+		_addrlv6=""
 		jail_ips "add"
 		if [ -n "${_fib}" ]; then
 			_setfib="setfib -F '${_fib}'"
@@ -635,12 +667,37 @@
 			i=$((i + 1))
 		done
 
-		eval ${_setfib} jail ${_flags} -i ${_rootdir} ${_hostname} \
-			\"${_addrl}\" ${_exec_start} > ${_tmp_jail} 2>&1
+		if checkyesno _v2; then
+			_start_cmd="${_setfib} jail -J ${_tmp_jail} ${_flags} path=${_rootdir} host.hostname=${_hostname} \
+			name=\"${_name}\""
+			if checkyesno _vnet; then
+				_start_cmd="${_start_cmd} vnet"
+			else
+				_start_cmd="${_start_cmd} ip4.addr=\"${_addrlv4}\" ip6.addr=\"${_addrlv6}\""
+			fi
+			_start_cmd="${_start_cmd} command=${_exec_start}"
+			eval ${_start_cmd} > /dev/null 2>&1
+		else
+			eval ${_setfib} jail ${_flags} -i ${_rootdir} ${_hostname} \
+				\"${_addrl}\" ${_exec_start} > ${_tmp_jail} 2>&1
+		fi
 
 		if [ "$?" -eq 0 ] ; then
-			_jail_id=$(head -1 ${_tmp_jail})
-			i=1
+			if checkyesno _v2; then
+				_jail_id=$(awk -F '=| ' '{print $2}' ${_tmp_jail})
+			else
+				_jail_id=$(head -1 ${_tmp_jail})
+			fi
+
+			i=0
+			while : ; do
+				eval out=\"\${_exec_earlypoststart${i}:-''}\"
+				[ -z "$out" ] && break
+				${out}
+				i=$((i + 1))
+			done
+
+			i=0
 			while : ; do
 				eval out=\"\${_exec_afterstart${i}:-''}\"
 
