Skip to content

Commit b2fd961

Browse files
committed
Merge branch 'upstream-add-cap-devs-to-list-command' into 'master'
Add support to "list" command to print /dev based capabilities See merge request nvidia/container-toolkit/libnvidia-container!39
2 parents 554c4e9 + 2bda067 commit b2fd961

File tree

8 files changed

+120
-30
lines changed

8 files changed

+120
-30
lines changed

pkg/deb/libnvidia-container@[email protected]

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ libnvidia-container.so.1 libnvidia-container1 #MINVER#
1010
nvc_context_new@NVC_1.0 1.2.0~rc.3
1111
nvc_device_info_free@NVC_1.0 1.2.0~rc.3
1212
nvc_device_info_new@NVC_1.0 1.2.0~rc.3
13+
nvc_nvcaps_style@NVC_1.0 1.2.0~rc.3
14+
nvc_nvcaps_device_from_proc_path@NVC_1.0 1.2.0~rc.3
1315
nvc_device_mount@NVC_1.0 1.2.0~rc.3
1416
nvc_mig_device_access_caps_mount@NVC_1.0 1.2.0~rc.3
1517
nvc_mig_config_global_caps_mount@NVC_1.0 1.2.0~rc.3

src/cli/cli.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ struct devices {
6969
int new_devices(struct error *err, const struct nvc_device_info *dev, struct devices *d);
7070
void free_devices(struct devices *d);
7171

72+
int print_nvcaps_device_from_proc_file(struct nvc_context *, const char*, const char*);
73+
int print_all_mig_minor_devices(const struct nvc_device_node *);
74+
7275
int select_devices(
7376
struct error *err,
7477
char *devs,

src/cli/common.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* Copyright (c) 2017-2018, NVIDIA CORPORATION. All rights reserved.
33
*/
44

5+
#include <sys/sysmacros.h>
6+
57
#include <inttypes.h>
68
#include <string.h>
79

@@ -464,3 +466,50 @@ free_devices(struct devices *d)
464466
free(d->migs);
465467
memset(d, 0, sizeof(*d));
466468
}
469+
470+
int
471+
print_nvcaps_device_from_proc_file(struct nvc_context *ctx, const char* cap_dir, const char* cap_file)
472+
{
473+
char cap_path[PATH_MAX];
474+
struct nvc_device_node node;
475+
476+
if (path_join(NULL, cap_path, cap_dir, cap_file) < 0)
477+
return (-1);
478+
if (nvc_nvcaps_device_from_proc_path(ctx, cap_path, &node) < 0)
479+
return (-1);
480+
481+
printf("%s\n", node.path);
482+
free(node.path);
483+
484+
return (0);
485+
}
486+
487+
int
488+
print_all_mig_minor_devices(const struct nvc_device_node *node)
489+
{
490+
unsigned int gpu_minor = 0;
491+
unsigned int mig_minor = 0;
492+
char line[PATH_MAX];
493+
char dummy[PATH_MAX];
494+
FILE *fp;
495+
int rv = -1;
496+
497+
if ((fp = fopen(NV_CAPS_MIG_MINORS_PATH, "r")) == NULL) {
498+
goto fail;
499+
}
500+
501+
line[PATH_MAX - 1] = '\0';
502+
while (fgets(line, PATH_MAX - 1, fp)) {
503+
if (sscanf(line, "gpu%u%s %u", &gpu_minor, dummy, &mig_minor) != 3)
504+
continue;
505+
if (gpu_minor != minor(node->id))
506+
continue;
507+
printf(NV_CAPS_DEVICE_PATH "\n", mig_minor);
508+
}
509+
510+
rv = 0;
511+
512+
fail:
513+
fclose(fp);
514+
return (rv);
515+
}

src/cli/list.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,22 +188,38 @@ list_command(const struct context *ctx)
188188
for (size_t i = 0; i < devices.nmigs; ++i) {
189189
printf("%s/%s\n", devices.migs[i]->gi_caps_path, NV_MIG_ACCESS_FILE);
190190
printf("%s/%s\n", devices.migs[i]->ci_caps_path, NV_MIG_ACCESS_FILE);
191+
if (nvc_nvcaps_style() == NVC_NVCAPS_STYLE_DEV) {
192+
print_nvcaps_device_from_proc_file(nvc, devices.migs[i]->gi_caps_path, NV_MIG_ACCESS_FILE);
193+
print_nvcaps_device_from_proc_file(nvc, devices.migs[i]->ci_caps_path, NV_MIG_ACCESS_FILE);
194+
}
191195
}
192196
}
193197
}
194198

195199
/* List the files required for MIG configuration of the visible devices */
196200
if (mig_config_devices.all && mig_config_devices.ngpus) {
197201
printf("%s/%s\n", NV_MIG_CAPS_PATH, NV_MIG_CONFIG_FILE);
202+
if (nvc_nvcaps_style() == NVC_NVCAPS_STYLE_DEV)
203+
print_nvcaps_device_from_proc_file(nvc, NV_MIG_CAPS_PATH, NV_MIG_CONFIG_FILE);
198204
for (size_t i = 0; i < mig_config_devices.ngpus; ++i) {
199205
printf("%s\n", mig_config_devices.gpus[i]->mig_caps_path);
206+
if (nvc_nvcaps_style() == NVC_NVCAPS_STYLE_DEV) {
207+
printf("%s\n", NV_CAPS_DEVICE_DIR);
208+
print_all_mig_minor_devices(&mig_config_devices.gpus[i]->node);
209+
}
200210
}
201211
}
202212
/* List the files required for MIG monitoring of the visible devices */
203213
if (mig_monitor_devices.all && mig_monitor_devices.ngpus) {
204214
printf("%s/%s\n", NV_MIG_CAPS_PATH, NV_MIG_MONITOR_FILE);
215+
if (nvc_nvcaps_style() == NVC_NVCAPS_STYLE_DEV)
216+
print_nvcaps_device_from_proc_file(nvc, NV_MIG_CAPS_PATH, NV_MIG_MONITOR_FILE);
205217
for (size_t i = 0; i < mig_monitor_devices.ngpus; ++i) {
206218
printf("%s\n", mig_monitor_devices.gpus[i]->mig_caps_path);
219+
if (nvc_nvcaps_style() == NVC_NVCAPS_STYLE_DEV) {
220+
printf("%s\n", NV_CAPS_DEVICE_DIR);
221+
print_all_mig_minor_devices(&mig_monitor_devices.gpus[i]->node);
222+
}
207223
}
208224
}
209225

src/libnvidia-container.lds

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ VERSION {
3333
nvc_device_info_free;
3434
nvc_driver_mount;
3535
nvc_device_mount;
36+
nvc_nvcaps_style;
37+
nvc_nvcaps_device_from_proc_path;
3638
nvc_mig_device_access_caps_mount;
3739
nvc_mig_config_global_caps_mount;
3840
nvc_mig_monitor_global_caps_mount;

src/nvc.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ extern "C" {
2020

2121
#define NVC_ARG_MAX 256
2222

23+
#define NVC_NVCAPS_STYLE_NONE 0
24+
#define NVC_NVCAPS_STYLE_PROC 1
25+
#define NVC_NVCAPS_STYLE_DEV 2
26+
2327
struct nvc_context;
2428
struct nvc_container;
2529

@@ -121,6 +125,10 @@ void nvc_driver_info_free(struct nvc_driver_info *);
121125
struct nvc_device_info *nvc_device_info_new(struct nvc_context *, const char *);
122126
void nvc_device_info_free(struct nvc_device_info *);
123127

128+
int nvc_nvcaps_style(void);
129+
130+
int nvc_nvcaps_device_from_proc_path(struct nvc_context *, const char *, struct nvc_device_node *);
131+
124132
int nvc_driver_mount(struct nvc_context *, const struct nvc_container *, const struct nvc_driver_info *);
125133

126134
int nvc_device_mount(struct nvc_context *, const struct nvc_container *, const struct nvc_device *);

src/nvc_info.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <errno.h>
88
#include <limits.h>
9+
#include <nvidia-modprobe-utils.h>
910
#include <stdio.h>
1011
#include <stdlib.h>
1112
#include <string.h>
@@ -788,3 +789,39 @@ nvc_device_info_free(struct nvc_device_info *info)
788789
free(info->gpus);
789790
free(info);
790791
}
792+
793+
int
794+
nvc_nvcaps_style(void)
795+
{
796+
if (nvidia_get_chardev_major(NV_CAPS_MODULE_NAME) >= 0)
797+
return NVC_NVCAPS_STYLE_DEV;
798+
if (file_exists(NULL, NV_PROC_DRIVER_CAPS) >= 0)
799+
return NVC_NVCAPS_STYLE_PROC;
800+
return NVC_NVCAPS_STYLE_NONE;
801+
}
802+
803+
int
804+
nvc_nvcaps_device_from_proc_path(struct nvc_context *ctx, const char *cap_path, struct nvc_device_node *node)
805+
{
806+
char abs_cap_path[PATH_MAX];
807+
char dev_name[PATH_MAX];
808+
int major, minor;
809+
int rv = -1;
810+
811+
if (path_join(&ctx->err, abs_cap_path, ctx->cfg.root, cap_path) < 0)
812+
goto fail;
813+
814+
if (nvidia_cap_get_device_file_attrs(abs_cap_path, &major, &minor, dev_name) == 0) {
815+
error_set(&ctx->err, "unable to get cap device attributes: %s", cap_path);
816+
goto fail;
817+
}
818+
819+
if ((node->path = xstrdup(&ctx->err, dev_name)) == NULL)
820+
goto fail;
821+
node->id = makedev((unsigned int)major, (unsigned int)minor);
822+
823+
rv = 0;
824+
825+
fail:
826+
return (rv);
827+
}

src/nvc_mount.c

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ static int symlink_libraries(struct error *, const struct nvc_container *, cons
4040
static void filter_libraries(const struct nvc_driver_info *, char * [], size_t *);
4141
static int device_mount_dxcore(struct nvc_context *, const struct nvc_container *);
4242
static int device_mount_native(struct nvc_context *, const struct nvc_container *, const struct nvc_device *);
43-
static int cap_device_from_path(struct nvc_context *, const char *, struct nvc_device_node *);
4443
static int cap_device_mount(struct nvc_context *, const struct nvc_container *, const char *);
4544
static int setup_mig_minor_cgroups(struct error *, const struct nvc_container *, int, const struct nvc_device_node *);
4645

@@ -643,40 +642,14 @@ device_mount_native(struct nvc_context *ctx, const struct nvc_container *cnt, co
643642
return (rv);
644643
}
645644

646-
static int
647-
cap_device_from_path(struct nvc_context *ctx, const char *cap_path, struct nvc_device_node *node)
648-
{
649-
char abs_cap_path[PATH_MAX];
650-
char dev_name[PATH_MAX];
651-
int major, minor;
652-
int rv = -1;
653-
654-
if (path_join(&ctx->err, abs_cap_path, ctx->cfg.root, cap_path) < 0)
655-
goto fail;
656-
657-
if (nvidia_cap_get_device_file_attrs(abs_cap_path, &major, &minor, dev_name) == 0) {
658-
error_set(&ctx->err, "unable to get cap device attributes: %s", cap_path);
659-
goto fail;
660-
}
661-
662-
if ((node->path = xstrdup(&ctx->err, dev_name)) == NULL)
663-
goto fail;
664-
node->id = makedev((unsigned int)major, (unsigned int)minor);
665-
666-
rv = 0;
667-
668-
fail:
669-
return (rv);
670-
}
671-
672645
static int
673646
cap_device_mount(struct nvc_context *ctx, const struct nvc_container *cnt, const char *cap_path)
674647
{
675648
char *dev_mnt = NULL;
676649
struct nvc_device_node node = {0};
677650
int rv = -1;
678651

679-
if (cap_device_from_path(ctx, cap_path, &node) < 0)
652+
if (nvc_nvcaps_device_from_proc_path(ctx, cap_path, &node) < 0)
680653
goto fail;
681654

682655
if (!(cnt->flags & OPT_NO_DEVBIND)) {
@@ -978,7 +951,7 @@ nvc_mig_config_global_caps_mount(struct nvc_context *ctx, const struct nvc_conta
978951
if ((dev_mnt = mount_directory(&ctx->err, ctx->cfg.root, cnt, NV_CAPS_DEVICE_DIR)) == NULL)
979952
goto fail;
980953

981-
if (cap_device_from_path(ctx, config, &node) < 0)
954+
if (nvc_nvcaps_device_from_proc_path(ctx, config, &node) < 0)
982955
goto fail;
983956

984957
if (!(cnt->flags & OPT_NO_CGROUPS))
@@ -1041,7 +1014,7 @@ nvc_mig_monitor_global_caps_mount(struct nvc_context *ctx, const struct nvc_cont
10411014
if ((dev_mnt = mount_directory(&ctx->err, ctx->cfg.root, cnt, NV_CAPS_DEVICE_DIR)) == NULL)
10421015
goto fail;
10431016

1044-
if (cap_device_from_path(ctx, monitor, &node) < 0)
1017+
if (nvc_nvcaps_device_from_proc_path(ctx, monitor, &node) < 0)
10451018
goto fail;
10461019

10471020
if (!(cnt->flags & OPT_NO_CGROUPS))

0 commit comments

Comments
 (0)