无法使用 nvidia 从 lxc 容器内部运行 glxdemo

无法使用 nvidia 从 lxc 容器内部运行 glxdemo

我已经创建了lxc包含许多软件包的自定义工具链容器。与图形相关,我已nvidia 367.27使用此容器内的 nvidia 安装程序安装了驱动程序(称为mk7icontainer

nvidia 驱动程序库/二进制文件安装在/usr/share/nvidia该容器内的路径上。

我也在容器内的mesa路径中安装了。/usr

glxdemo使用以下命令编译了源代码:

gcc -g glxdemo.c -Wl,--rpath-link,/usr/share/nvidia/lib -Wl,-rpath,/usr/share/nvidia/lib \
-Wl,--rpath-link,/usr/share/nvidia/lib/tls -Wl,-rpath,/usr/share/nvidia/lib/tls \
-Wl,--rpath-link,/usr/share/nvidia/lib/vdpau  -Wl,-rpath,/usr/share/nvidia/lib/vdpau \
-Wl,--rpath-link,/usr/share/nvidia/lib/xorg/modules -Wl,-rpath,/usr/share/nvidia/lib/xorg/modules \
-Wl,--rpath-link,/usr/share/nvidia/lib/xorg/modules/drivers -Wl,-rpath,/usr/share/nvidia/lib/xorg/modules/drivers \
-Wl,--rpath-link,/usr/share/nvidia/lib/xorg/modules/extensions -Wl,-rpath,/usr/share/nvidia/lib/xorg/modules/extensions -lX11 -lGL -lGLU -lglut -lm -o glxdemo

在容器里面,我有

DISPLAY=":0"
XAUTHORITY=/root/.Xauthority

我正在bash使用以下命令在容器中启动:

sudo lxc-attach -n mk7icontainer --clear-env  -- /usr/bin/bash

我的主机正在运行Ubuntu 16.0.4,我nvidia 367.27使用其安装程序安装了相同的驱动程序。它运行良好。我甚至可以glxdemo在主机上运行而没有任何问题。

但是,当我尝试glxdemo在容器内运行(使用上面提到的命令编译)时,出现以下错误:

X Error of failed request:  BadValue (integer parameter out of range for operation)
Major opcode of failed request:  154 (GLX)
Minor opcode of failed request:  3 (X_GLXCreateContext)
Value in failed request:  0x0
Serial number of failed request:  24
Current serial number in output stream:  25

有人知道这里出了什么问题吗?

我也想知道如何读取这些错误以缩小问题范围。

源代码为glxdemo

#include <GL/gl.h>
#include <GL/glx.h>
#include <stdio.h>
#include <stdlib.h>
static void redraw( Display *dpy, Window w )
{
   printf("Redraw event\n");
   glClear( GL_COLOR_BUFFER_BIT );

   glColor3f( 1.0, 1.0, 0.0 );
   glRectf( -0.8, -0.8, 0.8, 0.8 );
   glXSwapBuffers( dpy, w );
}

static void resize( unsigned int width, unsigned int height )
{
   printf("Resize event\n");
   glViewport( 0, 0, width, height );
   glMatrixMode( GL_PROJECTION );
   glLoadIdentity();
   glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
}

static Window make_rgb_db_window( Display *dpy,
                              unsigned int width, unsigned int height )
{
   int attrib[] = { GLX_RGBA,
                    GLX_RED_SIZE, 1,
                    GLX_GREEN_SIZE, 1,
                    GLX_BLUE_SIZE, 1,
                    GLX_DOUBLEBUFFER,
                    None };
   int scrnum;
   XSetWindowAttributes attr;
   unsigned long mask;
   Window root;
   Window win;
   GLXContext ctx;
   XVisualInfo *visinfo;

   scrnum = DefaultScreen( dpy );
   root = RootWindow( dpy, scrnum );

   visinfo = glXChooseVisual( dpy, scrnum, attrib );
   if (!visinfo) {
      printf("Error: couldn't get an RGB, Double-buffered visual\n");
      exit(1);
   }

   /* window attributes */
   attr.background_pixel = 0;
   attr.border_pixel = 0;
   attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
   attr.event_mask = StructureNotifyMask | ExposureMask;
   mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;

   win = XCreateWindow( dpy, root, 0, 0, width, height,
                    0, visinfo->depth, InputOutput,
                    visinfo->visual, mask, &attr );

   printf("\n XCreateWindow successfull \n");
   ctx = glXCreateContext( dpy, visinfo, NULL, True );
   if (!ctx) {
      printf("Error: glXCreateContext failed\n");
      exit(1);
   }

        printf("\n glXCreateContext successfull \n");

   glXMakeCurrent( dpy, win, ctx );

   return win;
}

static void event_loop( Display *dpy )
{
   XEvent event;

   while (1) {
      XNextEvent( dpy, &event );

      switch (event.type) {
         case Expose:
            redraw( dpy, event.xany.window );
            break;
         case ConfigureNotify:
            resize( event.xconfigure.width, event.xconfigure.height );
            break;
      }
   }
}

int main( int argc, char *argv[] )
{
   Display *dpy;
   Window win;

   dpy = XOpenDisplay(NULL);
        if(!dpy)
        {
                printf("\nError in XOpenDisplay\n");
        }
        printf("\nXOpenDisplay successfull\n");

   win = make_rgb_db_window( dpy, 300, 300 );

   printf("\nmake_rgb_db_window successfull \n");
   glShadeModel( GL_FLAT );
   glClearColor( 0.5, 0.5, 0.5, 1.0 );

   XMapWindow( dpy, win );

   event_loop( dpy );
   return 0;
}

答案1

我能够解决这个问题。

我必须nvidia在配置文件中添加设备节点lxc container

lxc.cgroup.devices.allow = c 195:* rwm
lxc.mount.entry = /dev/nvidia0 dev/nvidia0 none bind,optional,create=file
lxc.mount.entry = /dev/nvidiactl dev/nvidiactl none bind,optional,create=file
lxc.mount.entry = /dev/nvidia-modeset dev/nvidia-modeset none bind,optional,create=file

相关内容