Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add vps-pci script

This script will be executed after moving pci device in container.

[kir@openvz.org: shell script style improvements, added missing C includes]
Signed-off-by: Andrey Vagin <avagin@openvz.org>
  • Loading branch information...
commit 115195e77b59825b84aeb0b68da9cfa83bf6473a 1 parent 10c652a
@avagin avagin authored kolyshkin committed
View
1  include/types.h
@@ -32,6 +32,7 @@
#define VPS_NET_ADD LIB_SCRIPTS_DIR "vps-net_add"
#define VPS_NET_DEL LIB_SCRIPTS_DIR "vps-net_del"
+#define VPS_PCI LIB_SCRIPTS_DIR "vps-pci"
/* User-defined scripts are in VPS_CONF_DIR */
#define USER_CREATE_SCRIPT VPS_CONF_DIR "vps.create"
View
1  scripts/.gitignore
@@ -4,5 +4,6 @@ vps-net_add
vps-net_del
vzevent-reboot
vzevent-stop
+vps-pci
Makefile.in
Makefile
View
3  scripts/Makefile.am
@@ -23,7 +23,8 @@ vzlib_SCRIPTS = \
vps-net_add \
vps-net_del \
vzevent-stop \
- vzevent-reboot
+ vzevent-reboot \
+ vps-pci
EXTRA_DIST = \
$(vzlib_SCRIPTS:%=%.in)
View
95 scripts/vps-pci.in
@@ -0,0 +1,95 @@
+#!/bin/sh
+# Copyright (C) 2010, Parallels, Inc. All rights reserved.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU 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 General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+#
+# This script is executed in the CT0 context after moving
+# PCI devices into or out of CT (vzctl set $CTID --pci_add/--pci_del).
+# It is supposed to take care about actions needed to properly
+# add/remove certain PCI devices.
+#
+# Parameters are passed in environment variables:
+# VEID - container ID
+# PCI - list of PCI devices
+# ADD - boolean flag (1 - add, 0 - del)
+#
+# Currently this only supports adding/removing of NVidia video cards.
+
+VZCTL=vzctl
+SELF=$(basename $0)
+
+# Prepare nVidia video card for using in CT
+handle_nvidia_device()
+{
+ local pci major minor dev card dev_num PCI
+ local nv_proc=/proc/driver/nvidia
+ PCI=$1
+
+ lspci -s $PCI | grep -qi VGA.*nvidia || return
+
+ modprobe nvidia > /dev/null
+ if [ $? -ne 0 ]; then
+ echo "$SELF: can't load module nvidia" 1>&2
+ return
+ fi
+
+ major=$(cat /proc/devices | awk '/nvidia/ {print $1}')
+ for card in $(ls $nv_proc/cards/); do
+ pci=$(cat $nv_proc/cards/$card | awk '/Bus Location/ {print $3}')
+ unify_dev()
+ {
+ echo $1 | sed 's/:/./g' # I love debian!
+ }
+ if [ "$(unify_dev $pci)" = "$(unify_dev $PCI)" ]; then
+ dev_num=$card
+ break
+ fi
+ done
+
+ if [ -z "$dev_num" ]; then
+ echo "$SELF: Unknown device $PCI" 1>&2
+ return
+ fi
+
+ for dev in nvidiactl nvidia$dev_num; do
+ if [ ! -e /dev/$dev ]; then
+ if [ "nvidiactl" = "$dev" ]; then
+ minor=255
+ else
+ minor=$dev_num
+ fi
+ mknod /dev/$dev c $major $minor
+ if [ $? -ne 0 ]; then
+ echo "$SELF: Can't create device $dev" 1>&2
+ return
+ fi
+ fi
+ if [ "$ADD" = 1 ]; then
+ $VZCTL --quiet --skiplock set $VEID --devnodes $dev:rw
+ else
+ $VZCTL --quiet --skiplock set $VEID --devnodes $dev:none
+ fi
+ if [ $? -ne 0 ]; then
+ echo "$SELF: Can't give access to device $dev" 1>&2
+ return
+ fi
+ done
+}
+
+for dev in $PCI; do
+ handle_nvidia_device $dev
+done
+exit 0
View
28 src/lib/dev.c
@@ -28,6 +28,7 @@
#include <linux/vzcalluser.h>
#include "vzerror.h"
+#include "script.h"
#include "util.h"
#include "dev.h"
#include "env.h"
@@ -170,3 +171,30 @@ void free_dev_param(dev_param *dev)
{
free_dev(&dev->dev);
}
+
+int run_pci_script(envid_t veid, int op, list_head_t *pci_h)
+{
+ char *argv[3];
+ char *envp[10];
+ char *script;
+ int ret;
+ char buf[STR_SIZE];
+ int i = 0;
+
+ if (list_empty(pci_h))
+ return 0;
+ snprintf(buf, sizeof(buf), "VEID=%d", veid);
+ envp[i++] = strdup(buf);
+ snprintf(buf, sizeof(buf), "ADD=%d", op == ADD);
+ envp[i++] = strdup(buf);
+ envp[i++] = list2str("PCI", pci_h);
+ envp[i++] = strdup(ENV_PATH);
+ envp[i] = NULL;
+ script = VPS_PCI;
+ argv[0] = script;
+ argv[1] = NULL;
+ ret = run_script(script, argv, envp, 0);
+ free_arg(envp);
+
+ return ret;
+}
View
2  vzctl.spec
@@ -205,7 +205,7 @@ Containers control API library
%attr(755,root,root) %{_pkglibdir}/scripts/vps-create
%attr(755,root,root) %{_pkglibdir}/scripts/vzevent-stop
%attr(755,root,root) %{_pkglibdir}/scripts/vzevent-reboot
-
+%attr(755,root,root) %{_pkglibdir}/scripts/vps-pci
%changelog
* Wed Jun 13 2007 Andy Shevchenko <andriy@asplinux.com.ua> - 3.0.17-1
Please sign in to comment.
Something went wrong with that request. Please try again.