-
Notifications
You must be signed in to change notification settings - Fork 71
/
dev_container
executable file
·1427 lines (1290 loc) · 45.7 KB
/
dev_container
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# SPDX-FileCopyrightText: Copyright (c) 2022-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Error out if a command fails
set -e
#===============================================================================
# Default values for environment variables.
#===============================================================================
init_globals() {
if [ "$0" != "/bin/bash" ] && [ "$0" != "bash" ]; then
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
export RUN_SCRIPT_FILE="$(readlink -f "$0")"
else
export RUN_SCRIPT_FILE="$(readlink -f "${BASH_SOURCE[0]}")"
fi
export HOLOHUB_ROOT=$(dirname "${RUN_SCRIPT_FILE}")
HOLOSCAN_PY_EXE=${HOLOSCAN_PY_EXE:-"python3"}
export HOLOSCAN_PY_EXE
HOLOSCAN_DOCKER_EXE=${HOLOSCAN_DOCKER_EXE:-"docker"}
export HOLOSCAN_DOCKER_EXE
HOLOSCAN_SDK_VERSION="sdk-latest"
export HOLOSCAN_SDK_VERSION
HOLOHUB_CONTAINER_BASE_NAME=holohub
export HOLOHUB_CONTAINER_BASE_NAME
DO_DRY_RUN="false" # print commands but do not execute them. Used by run_command
}
################################################################################
# Utility functions
################################################################################
#######################################
# Get list of available commands from a given input file.
#
# Available commands and command summary are extracted by checking a pattern
# "_desc() { c_echo '".
# Section title is extracted by checking a pattern "# Section: ".
# This command is used for listing available commands in CLI.
#
# e.g.)
# "# Section: String/IO functions"
# => "# String/IO functions"
# "to_lower_desc() { c_echo 'Convert to lower case"
# => "to_lower ----------------- Convert to lower case"
#
# Arguments:
# $1 - input file that defines commands
# Returns:
# Print list of available commands from $1
#######################################
get_list_of_available_commands() {
local mode="color"
if [ "${1:-}" = "color" ]; then
mode="color"
shift
elif [ "${1:-}" = "nocolor" ]; then
mode="nocolor"
shift
fi
local file_name="$1"
if [ ! -e "$1" ]; then
echo "$1 doesn't exist!"
fi
local line_str='--------------------------------'
local IFS= cmd_lines="$(IFS= cat "$1" | grep -E -e "^(([[:alpha:]_[:digit:]]+)_desc\(\)|# Section: )" | sed "s/_desc() *{ *c_echo '/ : /")"
local line
while IFS= read -r line; do
local cmd=$(echo "$line" | cut -d":" -f1)
local desc=$(echo "$line" | cut -d":" -f2-)
if [ "$cmd" = "# Section" ]; then
c_echo ${mode} B "${desc}"
else
# there is no substring operation in 'sh' so use 'cut'
local dash_line="$(echo "${line_str}" | cut -c ${#cmd}-)" # = "${line_str:${#cmd}}"
c_echo ${mode} Y " ${cmd}" w " ${dash_line} ${desc}"
fi
# use <<EOF, not '<<<"$cmd_lines"' to be executable in sh
done <<EOF
$cmd_lines
EOF
}
my_cat_prefix() {
local IFS
local prefix="$1"
local line
while IFS= read -r line; do
echo "${prefix}${line}" # -e option doesn't work in 'sh' so disallow escaped characters
done <&0
}
c_str() {
local old_color=39
local old_attr=0
local color=39
local attr=0
local text=""
local mode="color"
if [ "${1:-}" = "color" ]; then
mode="color"
shift
elif [ "${1:-}" = "nocolor" ]; then
mode="nocolor"
shift
fi
for i in "$@"; do
case "$i" in
r | R)
color=31
;;
g | G)
color=32
;;
y | Y)
color=33
;;
b | B)
color=34
;;
p | P)
color=35
;;
c | C)
color=36
;;
w | W)
color=37
;;
z | Z)
color=0
;;
esac
case "$i" in
l | L | R | G | Y | B | P | C | W)
attr=1
;;
n | N | r | g | y | b | p | c | w)
attr=0
;;
z | Z)
attr=0
;;
*)
text="${text}$i"
;;
esac
if [ "${mode}" = "color" ]; then
if [ ${old_color} -ne ${color} ] || [ ${old_attr} -ne ${attr} ]; then
text="${text}\033[${attr};${color}m"
old_color=$color
old_attr=$attr
fi
fi
done
/bin/echo -en "$text"
}
c_echo() {
# Select color/nocolor based on the first argument
local mode="color"
if [ "${1:-}" = "color" ]; then
mode="color"
shift
elif [ "${1:-}" = "nocolor" ]; then
mode="nocolor"
shift
else
if [ ! -t 1 ]; then
mode="nocolor"
fi
fi
local old_opt="$(shopt -op xtrace)" # save old xtrace option
set +x # unset xtrace
if [ "${mode}" = "color" ]; then
local text="$(c_str color "$@")"
/bin/echo -e "$text\033[0m"
else
local text="$(c_str nocolor "$@")"
/bin/echo -e "$text"
fi
eval "${old_opt}" # restore old xtrace option
}
echo_err() {
echo >&2 "$@"
}
c_echo_err() {
c_echo >&2 "$@"
}
fatal() {
if [ -n "$*" ]; then
c_echo_err R "$(date -u '+%Y-%m-%d %H:%M:%S') [FATAL] " Z "$@"
fi
if [ -n "${SCRIPT_DIR}" ]; then
exit 1
else
kill -INT $$ # kill the current process instead of exit in shell environment.
fi
}
run_command() {
local status=0
local cmd="$*"
if [ "${DO_DRY_RUN}" != "true" ]; then
c_echo_err B "$(date -u '+%Y-%m-%d %H:%M:%S') " W "\$ " G "${cmd}"
else
c_echo_err B "$(date -u '+%Y-%m-%d %H:%M:%S') " C "[dryrun] " W "\$ " G "${cmd}"
fi
[ "$(echo -n "$@")" = "" ] && return 1 # return 1 if there is no command available
if [ "${DO_DRY_RUN}" != "true" ]; then
"$@"
status=$?
fi
return $status
}
run_docker() {
$(./run docker_cmd "-u $(id -u):$(id -g)") "$@"
}
get_group_id() {
local group=$1
cat /etc/group | grep $group | cut -d: -f3
}
get_ucx_options() {
local prefix="${1:- }"
local postfix="${2:- }"
local ucx_opt="${prefix}--ipc=host${postfix}"
ucx_opt+="${prefix}--cap-add=CAP_SYS_PTRACE${postfix}"
ucx_opt+="${prefix}--ulimit=memlock=-1${postfix}"
ucx_opt+="${prefix}--ulimit=stack=67108864" # last item doesn't need postfix
echo "${ucx_opt}"
}
get_conditional_options() {
local use_tini=$1
local persistent=$2
local prefix="${3:- }"
local postfix="${4:- }"
local conditional_opt=""
if [ -e /usr/lib/libvideomasterhd.so ]; then
conditional_opt+="${prefix}--volume=/usr/lib/libvideomasterhd.so:/usr/lib/libvideomasterhd.so${postfix}"
fi
if [ -d /opt/deltacast/videomaster/Include ]; then
conditional_opt+="${prefix}--volume=/opt/deltacast/videomaster/Include:/usr/local/deltacast/Include${postfix}"
fi
if [ -d /opt/yuan/qcap/include ]; then
conditional_opt+="${prefix}--volume=/opt/yuan/qcap/include:/opt/yuan/qcap/include${postfix}"
fi
if [ -d /opt/yuan/qcap/lib ]; then
conditional_opt+="${prefix}--volume=/opt/yuan/qcap/lib:/opt/yuan/qcap/lib${postfix}"
fi
# add tegra directory
if [ -d /usr/lib/aarch64-linux-gnu/tegra ]; then
conditional_opt+="${prefix}--volume=/usr/lib/aarch64-linux-gnu/tegra:/usr/lib/aarch64-linux-gnu/tegra${postfix}"
fi
# add user in container to group video to access video devices, e.g. /dev/video0
video_id=$(get_group_id video)
if [ -n "$video_id" ]; then
conditional_opt+="${prefix}--group-add=${video_id}${postfix}"
fi
render_id=$(get_group_id render)
if [ -n "$render_id" ]; then
conditional_opt+="${prefix}--group-add=${render_id}${postfix}"
fi
# Add docker group to enable DooD
conditional_opt+="${prefix}--group-add=$(get_group_id docker)${postfix}"
if [[ $nsys_profile == true ]]; then
# enable usage of the the perf_event_open call, see
# https://docs.nvidia.com/nsight-systems/UserGuide/index.html#docker-profiling
conditional_opt+="${prefix}--cap-add=SYS_ADMIN${postfix}"
fi
# Allow X11 forwarding over SSH
if [[ $ssh_x11 -gt 0 ]]; then
XAUTH=/tmp/.docker.xauth
xauth nlist $DISPLAY | sed -e 's/^..../ffff/' | xauth -f $XAUTH nmerge -
chmod 777 $XAUTH
conditional_opt+="${prefix}--volume=$XAUTH:$XAUTH${postfix}"
conditional_opt+="${prefix}-e XAUTHORITY=$XAUTH${postfix}"
fi
# Use tini
if [[ $use_tini -gt 0 ]]; then
conditional_opt+="${prefix}--init${postfix}"
fi
# Persistent container
if [[ $persistent == 0 ]]; then
conditional_opt+="${prefix}--rm${postfix}"
fi
# if running on AGX Orin (iGPU only) or on IGX Orin in iGPU mode
if lsmod | grep -q nvgpu ; then
if [ -e /dev/nvgpu/igpu0/nvsched ]; then
conditional_opt+="${prefix}--device=/dev/nvgpu/igpu0/nvsched${postfix}"
fi
if [ -e /dev/nvhost-ctrl-nvdec ]; then
conditional_opt+="${prefix}--device=/dev/nvhost-ctrl-nvdec${postfix}"
fi
if [ -e /dev/nvhost-ctxsw-gpu ]; then
conditional_opt+="${prefix}--device=/dev/nvhost-ctxsw-gpu${postfix}"
fi
if [ -e /dev/nvhost-nvsched-gpu ]; then
conditional_opt+="${prefix}--device=/dev/nvhost-nvsched-gpu${postfix}"
fi
if [ -e /dev/nvhost-sched-gpu ]; then
conditional_opt+="${prefix}--device=/dev/nvhost-sched-gpu${postfix}"
fi
if [ -e /dev/nvidia0 ]; then
conditional_opt+="${prefix}--device=/dev/nvidia0${postfix}"
fi
if [ -e /dev/nvidia-modeset ]; then
conditional_opt+="${prefix}--device=/dev/nvidia-modeset${postfix}"
fi
fi
# Define path for cupy's kernel cache, needed since $HOME does
# not exist when running with `-u id:group`
conditional_opt+="${prefix}-e CUPY_CACHE_DIR=/workspace/holohub/.cupy/kernel_cache" # last item doesn't need postfix
echo "${conditional_opt}"
}
get_mount_device_options() {
local prefix="${1:- }"
local postfix="${2:- }"
local mount_device_opt=""
for video_dev in $(find /dev -regex '/dev/video[0-9]+'); do
mount_device_opt+="${prefix}--device=$video_dev${postfix}"
done
for capture_dev in $(find /dev -regex '/dev/capture-vi-channel[0-9]+'); do
mount_device_opt+="${prefix}--device=$capture_dev${postfix}"
done
for video_dev in $(find /dev -regex '/dev/ajantv2[0-9]+'); do
mount_device_opt+="${prefix}--device=$video_dev:$video_dev${postfix}"
done
# Deltacast capture boards and Videomaster SDK
for i in 0 1 2 3; do
# Deltacast SDI capture board
if [ -e /dev/delta-x380${i} ]; then
mount_device_opt+="${prefix}--device=/dev/delta-x380${i}:/dev/delta-x380${i}${postfix}"
fi
# Deltacast HDMI capture board
if [ -e /dev/delta-x350${i} ]; then
mount_device_opt+="${prefix}--device=/dev/delta-x350${i}:/dev/delta-x350${i}${postfix}"
fi
done
# Find all audio devices
if [ -d /dev/snd ]; then
audio_devices=$(find /dev/snd -type c)
# Mount all found audio devices
for audio_dev in $audio_devices; do
mount_device_opt+="${prefix}--device=$audio_dev${postfix}"
done
fi
# mounts the ALSA configuration for the user running the script
if [ -e /etc/asound.conf ]; then
mount_device_opt+="${prefix}--mount=source=/etc/asound.conf,target=/etc/asound.conf,readonly,type=bind${postfix}"
fi
# Mount ConnectX device nodes
if [ -e /dev/infiniband/rdma_cm ]; then
mount_device_opt+="${prefix}--device=/dev/infiniband/rdma_cm${postfix}"
fi
for uverbs_dev in $(find /dev -regex '/dev/infiniband/uverbs[0-9]+'); do
mount_device_opt+="${prefix}--device=${uverbs_dev}${postfix}"
done
# grants access to the sound group
sound_group_gid=$(getent group audio | cut -d: -f3)
mount_device_opt+="${prefix}--group-add=$sound_group_gid" # last item does not need postfix
echo "${mount_device_opt}"
}
check_nvidia_ctk() {
# Check NVIDIA CTK version
min_ctk_version="1.12.0"
recommended_ctk_version="1.14.1"
if ! command -v nvidia-ctk >/dev/null; then
fatal G "nvidia-ctk" W " not found. Please install the NVIDIA Container Toolkit."
fi
ctk_version_output=$(nvidia-ctk --version | grep version)
ctk_version_pattern="([0-9]+\.[0-9]+\.[0-9]+)"
if [[ "$ctk_version_output" =~ $ctk_version_pattern ]]; then
ctk_version="${BASH_REMATCH[1]}"
if [[ "$(echo -e "$ctk_version\n$min_ctk_version" | sort -V | head -n1)" == "$ctk_version" ]]; then
fatal "Found nvidia-ctk Version $ctk_version. Version $min_ctk_version+ is required ($recommended_ctk_version+ recommended)."
fi
else
c_echo_err R "Could not extract nvidia-ctk version number."
c_echo_err " Version $min_ctk_version+ required."
c_echo_err " Version $recommended_ctk_version+ recommended."
fi
}
#===============================================================================
# Section: Build
#===============================================================================
get_host_gpu() {
if ! command -v nvidia-smi >/dev/null; then
echo "Could not find any GPU drivers on host. Defaulting build to target dGPU/CPU stack."
echo -n "dgpu";
elif [[ ! $(nvidia-smi --query-gpu=name --format=csv,noheader) ]] || \
[[ $(nvidia-smi --query-gpu=name --format=csv,noheader -i 0) =~ "Orin (nvgpu)" ]]; then
echo -n "igpu";
else
echo -n "dgpu";
fi
}
get_default_base_img() {
echo -n "nvcr.io/nvidia/clara-holoscan/holoscan:v2.6.0-"$(get_host_gpu)
}
get_default_img() {
echo -n "holohub:ngc-v2.6.0-"$(get_host_gpu)
}
# This function returns the compute capacity of the system's GPU (8.6, 8.9, etc.)
# Compute capacity is a version number that represents the GPU's features & specs
get_compute_capacity() {
echo -n $(nvidia-smi --query-gpu=compute_cap --format=csv,noheader | head -n 1)
}
build_desc() { c_echo 'Build dev image
Usage: ./dev_container build [options]
--base_img : Fully qualified base image name, e.g. holoscan-sdk-dev:latest
--docker_file : path to Dockerfile to use for building container.
--img : Specify fully qualified container name
--verbose : Print variables passed to docker build command
--no-cache : Do not use cache when building the image
--build-args : provides extra arguments to docker build command
'
}
build() {
# Choose NGC Holoscan SDK base image based on local platform, default is dGPU
local docker_file_path="${HOLOHUB_ROOT}/Dockerfile"
local gpu_type=$(get_host_gpu)
local base_img=$(get_default_base_img)
local img=$(get_default_img)
local compute_capacity=$(get_compute_capacity)
local print_verbose=0
local no_cache=
local extra_args=
# Check if buildx exists
if ! $(docker buildx version &>/dev/null); then
c_echo_err G "docker buildx plugin" W " is missing. Please install " G "docker-buildx-plugin" W ":
https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository"
exit
fi
# Parse CLI arguments next
while [[ $# -gt 0 ]]; do
case "$1" in
--base_img)
if [[ -n "$2" && ! "$2" =~ ^-- ]]; then
base_img="$2"
shift 2
else
echo "Error: --base_img requires a value"
build_desc
exit 1
fi
;;
--docker_file)
if [[ -n "$2" && ! "$2" =~ ^-- ]]; then
docker_file_path="$2"
shift 2
else
echo "Error: --docker_file requires a value"
build_desc
exit 1
fi
;;
--img)
if [[ -n "$2" && ! "$2" =~ ^-- ]]; then
img="$2"
shift 2
else
echo "Error: --img requires a value"
build_desc
exit 1
fi
;;
--build-args)
if [[ -n "$2" ]]; then
extra_args+=" $2"
shift 2
else
echo "Error: --build-args requires a value"
build_desc
exit 1
fi
;;
--no-cache)
no_cache="--no-cache"
shift
;;
--verbose)
print_verbose=1
shift
;;
*)
echo "Error: Invalid argument '$1'"
build_desc
exit 1
;;
esac
done
if [[ $print_verbose -gt 0 ]]; then
c_echo W "Build (HOLOHUB_ROOT:" G "${HOLOHUB_ROOT}" W ")..."
c_echo W "Build (gpu_type_type:" G "${gpu_type}" W ")..."
c_echo W "Build (base_img:" G "${base_img}" W ")..."
c_echo W "Build (docker_file_path:" G "${docker_file_path}" W ")..."
c_echo W "Build (img:" G "${img}" W ")..."
fi
# Docker build
run_command export DOCKER_BUILDKIT=1
run_command docker build \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--build-arg BASE_IMAGE=${base_img} \
--build-arg GPU_TYPE=${gpu_type} \
--build-arg COMPUTE_CAPACITY=${compute_capacity} \
--network=host \
${extra_args} \
${no_cache} \
-f ${docker_file_path} \
-t ${img} \
${HOLOHUB_ROOT}
}
#===============================================================================
# Section: Launch
#===============================================================================
launch_desc() { c_echo 'Launch Docker container
Usage: ./dev_container launch [options]
--img : Fully qualified image name, e.g. holohub:sdk-local-latest-dgpu
--local_sdk_root : Path to Holoscan SDK used for building local Holoscan SDK container
--ssh_x11 : Enable X11 forwarding of graphical HoloHub applications over SSH
--nsys_profile : Support Nsight Systems profiling in container
--init : Support tini entry point
--persistent : Does not delete container after it is run
--verbose : Print variables passed to docker run command
--add-volume : Mount additional volume
--as_root : Run the container with root permissions
--docker_opts : Additional options to pass to the Docker launch
-- : Any trailing arguments after "--" are forwarded to `docker run`
'
}
launch() {
local build_path="${CMAKE_BUILD_PATH:-build}"
local working_dir=${1:-${build_path}}
local mount_device_opt=""
local conditional_opt=""
local print_verbose=0
local local_sdk_root="undefined"
local ssh_x11=0
local use_tini=0
local persistent=0
local nsys_profile=false
local as_root=false
local docker_opts=""
# Choose NGC Holoscan SDK base image based on local platform, default is dGPU
local gpu_type=$(get_host_gpu)
local img=$(get_default_img)
# Initialize an empty string to hold additional volume mounts
additional_volumes=""
# Parse CLI arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--img)
if [[ -n "$2" && ! "$2" =~ ^-- ]]; then
img="$2"
shift 2
else
echo "Error: --img requires a value"
launch_desc
exit 1
fi
;;
--verbose)
print_verbose=1
shift
;;
--local_sdk_root)
if [[ -n "$2" && ! "$2" =~ ^-- ]]; then
local_sdk_root="$2"
shift 2
else
echo "Error: --local_sdk_root requires a value"
launch_desc
exit 1
fi
;;
--ssh_x11)
ssh_x11=1
shift
;;
--init)
use_tini=1
shift
;;
--persistent)
persistent=1
shift
;;
--nsys_profile)
nsys_profile=true
shift
;;
--as_root)
as_root=true
shift
;;
--add-volume)
if [[ -n "$2" && ! "$2" =~ ^-- ]]; then
path="$2"
base=$(basename "$path")
additional_volumes+="-v $path:/workspace/volumes/$base "
shift 2
else
echo "Error: --add-volume requires a value"
launch_desc
exit 1
fi
;;
--docker_opts)
if [[ -n "$2" ]]; then
docker_opts="$2"
shift 2
else
echo "Error: --docker_opts requires a value"
launch_desc
exit 1
fi
;;
--)
# Any trailing arguments after "--" are forwarded to `docker run`
shift
break
;;
*)
echo "Error: Invalid argument '$1'"
launch_desc
exit 1
;;
esac
done
check_nvidia_ctk
mount_device_opt="$(get_mount_device_options)"
conditional_opt="$(get_conditional_options $use_tini $persistent)"
# display - use XDG_SESSION_TYPE to detect X11 or Wayland
# see https://www.freedesktop.org/software/systemd/man/latest/pam_systemd.html#%24XDG_SESSION_TYPE
display_server_opt=""
if [ -n "${XDG_SESSION_TYPE-}" ]; then
display_server_opt+=" -e XDG_SESSION_TYPE"
if [ "${XDG_SESSION_TYPE}" == "wayland" ]; then
display_server_opt+=" -e WAYLAND_DISPLAY"
fi
fi
if [ -n "${XDG_RUNTIME_DIR-}" ]; then
display_server_opt+=" -e XDG_RUNTIME_DIR"
if [ -d ${XDG_RUNTIME_DIR} ]; then
display_server_opt+=" -v ${XDG_RUNTIME_DIR}:${XDG_RUNTIME_DIR}"
fi
fi
# if XDG_SESSION_TYPE is not set or set to tty or x11, use X11
if [ -z "${XDG_SESSION_TYPE-}" ] || [ "${XDG_SESSION_TYPE}" == "x11" ] || [ "${XDG_SESSION_TYPE}" == "tty" ]; then
# Allow the docker group to access X11.
# Note: not necessary for WSL2 (`SI:localuser:wslg` is added by default)
if [ -v DISPLAY ] && command -v xhost >/dev/null; then
run_command xhost +local:docker
fi
display_server_opt+=" -v /tmp/.X11-unix:/tmp/.X11-unix"
display_server_opt+=" -e DISPLAY"
fi
# when using a locally built Holoscan SDK container it is necessary to provide the path to Holoscan SDK
# to map Holoscan SDK into the container for building Holohub
local local_sdk_opt=""
local pythonpath="/workspace/holohub/benchmarks/holoscan_flow_benchmarking"
if [ -d "$local_sdk_root" ]; then
local_sdk_opt+=" -v $local_sdk_root:/workspace/holoscan-sdk"
# Define paths needed by the python applications
local_sdk_opt+=" -e HOLOSCAN_LIB_PATH=/workspace/holoscan-sdk/build/lib"
local_sdk_opt+=" -e HOLOSCAN_SAMPLE_DATA_PATH=/workspace/holoscan-sdk/data"
local_sdk_opt+=" -e HOLOSCAN_TESTS_DATA_PATH=/workspace/holoscan-sdk/tests/data"
pythonpath="/workspace/holoscan-sdk/build/python/lib:${pythonpath}"
else
pythonpath="/opt/nvidia/holoscan/python/lib:${pythonpath}"
fi
local_sdk_opt+=" -e PYTHONPATH=${pythonpath}"
# Find the nvoptix.bin file
if [ -f "/usr/share/nvidia/nvoptix.bin" ]; then
mount_nvoptix_bin="-v /usr/share/nvidia/nvoptix.bin:/usr/share/nvidia/nvoptix.bin:ro"
fi
# Options needed for UCX
local ucx_opt="$(get_ucx_options)"
# Only enable TTY if supported
local use_tty=""
if [ -t 1 ]; then
use_tty="--tty"
fi
# Allow root permissions on request
if [ $as_root == true ]; then
user="root"
else
user="$(id -u):$(id -g)"
fi
# DOCKER PARAMETERS
#
# --interactive (-i)
# The container needs to be interactive to be able to interact with the X11 windows
#
# -u $(id -u):$(id -g)
# -v /etc/group:/etc/group:ro
# -v /etc/passwd:/etc/passwd:ro
# Ensures the generated files (build, install...) are owned by $USER and not root,
# and provide the configuration files to avoid warning for user and group names
#
# -v ${HOLOHUB_ROOT}:/workspace/holohub
# Mount the source directory
#
# -w /workspace/holohub/${working_dir}
# Start in the build or install directory
#
# --runtime=nvidia \
# -e NVIDIA_DRIVER_CAPABILITIES=graphics,video,compute,utility,display
# Enable GPU acceleration
#
# ${mount_nvoptix_bin}
# Mount Optix denoiser weights.
if [[ $print_verbose -gt 0 ]]; then
c_echo W "Launch (HOLOHUB_ROOT: " G "${HOLOHUB_ROOT}" W ")..."
c_echo W "Launch (gpu_type: " G "${gpu_type}" W ")..."
c_echo W "Launch (mount_device_opt: " G "${mount_device_opt}" W ")..."
c_echo W "Launch (conditional_opt: " G "${conditional_opt}" W ")..."
c_echo W "Launch (display_server_opt: " G "${display_server_opt}" W ")..."
c_echo W "Launch (local_sdk_opt: " G "${local_sdk_opt}" W ")..."
c_echo W "Launch (ucx_opt: " G "${ucx_opt}" W ")..."
c_echo W "Launch (docker_opts: " G "${docker_opts}" W ")..."
c_echo W "Launch (image: " G "${img}" W ")..."
c_echo W "Launch (trailing args: " G "$@" W ")..."
fi
run_command ${HOLOSCAN_DOCKER_EXE} run --net host \
--interactive ${use_tty} \
-u $user \
-v /etc/group:/etc/group:ro \
-v /etc/passwd:/etc/passwd:ro \
-v ${HOLOHUB_ROOT}:/workspace/holohub \
-w /workspace/holohub \
--runtime=nvidia \
--gpus all \
--cap-add CAP_SYS_PTRACE \
--ipc=host \
-v /dev:/dev \
--device-cgroup-rule "c 81:* rmw" \
--device-cgroup-rule "c 189:* rmw" \
-e NVIDIA_DRIVER_CAPABILITIES=graphics,video,compute,utility,display \
-e HOME=/workspace/holohub \
${additional_volumes} \
${mount_device_opt} \
${conditional_opt} \
${display_server_opt} \
${local_sdk_opt} \
${mount_nvoptix_bin} \
${ucx_opt} \
${docker_opts} \
${img} \
"$@"
}
build_and_install_desc() { c_echo 'Build the application and install the binaries into install/<app> if supported
Usage: ./dev_container build_and_install <application_name>
View options with `./dev_container build_and_run_desc`
'
}
build_and_install() {
build_and_run --install --no_run "$@"
}
build_and_run_desc() { c_echo 'Build and run a requested application in a Docker container
Usage: ./dev_container build_and_run <application_name> [options]
Options:
--base_img : Fully qualified base image name, e.g. holoscan-sdk-dev:latest
--docker_file : Path to Dockerfile to use for building container.
Defaults to:
- Application-provided "Dockerfile", if it exists;
- Otherwise the top-level HoloHub "Dockerfile"
If `--img` is not specified then a custom image tag will be defined.
--img : Specify fully qualified output container name
--container_args: Additional Docker run args.
--language : Specify the app language implementation to run.
Some applications provide both `cpp` and `python` implementations.
--no_build : Launch the app without attempting to build it first
--no_run : Skip launching the application.
--install: Install the application into install/<app> if supported.
--build_args : Build the app with additional CMake configuration arguments
--build_docker_args : additional docker arguments when building the container
--build_with : List of optional operators that should be built separated by semicolons (;)
--run_args : Run the app with additional args
--verbose : Print extra output to console
--dryrun : View build and run commands without doing anything
--parallel_jobs: Set the # of parallel build jobs, e.g. $(($(nproc)-1))
'
}
build_and_run() {
local app_name=""
local app_language=""
local container_build_args=()
local container_launch_args=""
local docker_opts="--entrypoint=bash"
local build_app=1
local image_name=""
local docker_file=""
local extra_build_args=""
local extra_build_with=""
local extra_run_args=""
local run_app=1
local install=""
local configure_args=""
local parallel_jobs=""
# Parse CLI arguments next
while [[ $# -gt 0 ]]; do
case "$1" in
--base_img)
if [[ -n "$2" && ! "$2" =~ ^-- ]]; then
container_build_args+=(--base_img $2)
shift 2
else
echo "Error: --base_img requires a value"
build_and_run_desc
exit 1
fi
;;
--docker_file)
if [[ -n "$2" && ! "$2" =~ ^-- ]]; then
docker_file="$2"
shift 2
else
echo "Error: --docker_file requires a value"
build_and_run_desc
exit 1
fi
;;
--img)
if [[ -n "$2" && ! "$2" =~ ^-- ]]; then
image_name="$2"
shift 2
else
echo "Error: --img requires a value"
build_and_run_desc
exit 1
fi
;;
--no_build)
build_app=0
shift
;;
--run_args)
if [[ -n "$2" ]]; then
extra_run_args="--extra_args '$2'"
shift 2
else
echo "Error: --run_args requires a value"
build_and_run_desc
exit 1
fi
;;
--build_with)
if [[ -n "$2" ]]; then
extra_build_with="--with '$2'"
shift 2
else
echo "Error: --build_with requires a value"
build_and_run_desc
exit 1
fi
;;
--build_args)
if [[ -n "$2" ]]; then
configure_args="${configure_args} '$2'"
shift 2
else
echo "Error: --build_args requires a value"
build_and_run_desc
exit 1
fi
;;
--build_docker_args)
if [[ -n "$2" ]]; then
container_build_args+=(--build-args)
container_build_args+=("$2")
shift 2
else
echo "Error: --build_docker_args requires a value"
build_and_run_desc
exit 1
fi
;;
--run_args)
if [[ -n "$2" ]]; then
extra_run_args="--extra_args '$2'"
shift 2
else
echo "Error: --run_args requires a value"
build_and_run_desc
exit 1
fi
;;
--no_run)
run_app=0
shift 1
;;
--install)
install="--install"
shift 1
;;
--verbose)
container_build_args+=(--verbose)
container_launch_args+=" --verbose"
shift
;;
--language)
if [[ -n "$2" && ! "$2" =~ ^-- ]]; then