#!/bin/bash
# Copyright (c) 1999-2017, Parallels International GmbH
# Copyright (c) 2017-2019 Virtuozzo International GmbH. All rights reserved.
#
# This file is part of OpenVZ libraries. OpenVZ is free software; you can
# redistribute it and/or modify it under the terms of the GNU Lesser General
# Public License as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
# Our contact details: Virtuozzo International GmbH, Vordergasse 59, 8200
# Schaffhausen, Switzerland.
#
# This script is run in host context right before container start.
# Currently it loads kernel modules that might be needed for a CT.
#
# Parameters are passed in environment variables.
# Required parameters:
#   VEID          - container id

. /usr/libexec/libvzctl/scripts/vz-functions

vzcheckvar VEID

load_modules()
{
	local mod msg modules devnodes features

	eval $(. /etc/vz/conf/${VEID}.conf && \
		echo devnodes=\"$DEVNODES\" features=\"$FEATURES\" veth=\"NETIF\")

	# process tun/tap
	if echo $devnodes | grep -Fq 'net/tun:'; then
		modules="$modules tun"
	fi

	# process pptp
	if echo $features | grep -Fwq 'ppp:on'; then
		modules="$modules ppp_generic"
	fi

	# process veth, bridge
	if echo $features | grep -Fwq 'bridge:on'; then
		modules="$modules veth bridge"
	elif [ -n "$veth" ]; then
		modules="$modules veth"
	fi

	# process nfs and nfsd
	for mod in nfs nfsd; do
		if echo $features | grep -Fwq "${mod}:on"; then
			[ "$mod" = "nfs" ] && mod="nfs nfsv4"
			modules="$modules $mod"
		fi
	done

	for mod in ${modules}; do
		if /sbin/lsmod | grep -qw $mod; then
			continue
		fi
		msg=`/sbin/modprobe $mod 2>&1 >/dev/null`
		[ $? -ne 0 ] && echo "Warning: failed to load $mod: $msg" 1>&2
	done
}

load_modules

exit 0
# end of script
