@@ -18,9 +18,7 @@ use bollard::models::{
1818use bollard:: query_parameters:: {
1919 CreateContainerOptionsBuilder , CreateImageOptions , DownloadFromContainerOptionsBuilder ,
2020 ListContainersOptionsBuilder , RemoveContainerOptionsBuilder , StopContainerOptionsBuilder ,
21- UploadToContainerOptionsBuilder ,
2221} ;
23- use bollard:: { Docker , body_full} ;
2422use bytes:: Bytes ;
2523use futures:: { Stream , StreamExt } ;
2624use openshell_core:: cdi:: { CdiContext , CdiSpecDirectory , cdi_spec_mount_path} ;
@@ -63,7 +61,6 @@ use openshell_core::{Config, Error, Result as CoreResult};
6361use opentelemetry:: trace:: TraceContextExt as _;
6462use std:: collections:: { HashMap , HashSet } ;
6563use std:: future:: Future ;
66- use std:: io:: Cursor ;
6764use std:: net:: { IpAddr , Ipv4Addr , SocketAddr } ;
6865use std:: path:: { Path , PathBuf } ;
6966use std:: pin:: Pin ;
@@ -1035,6 +1032,22 @@ impl DockerComputeDriver {
10351032 . map_err ( |status| {
10361033 DockerProvisioningFailure :: new ( "ContainerCreateFailed" , status. message ( ) )
10371034 } ) ?;
1035+ if let Some ( cdi_context) = cdi_context. as_ref ( )
1036+ && let Err ( status) = write_cdi_context_file ( sandbox, & self . config , cdi_context)
1037+ {
1038+ cleanup_cdi_context_file ( sandbox, & self . config ) ;
1039+ return Err ( DockerProvisioningFailure :: new (
1040+ "CdiContextWriteFailed" ,
1041+ status. message ( ) ,
1042+ ) ) ;
1043+ }
1044+ if let Err ( status) = write_sandbox_token_file ( sandbox, & self . config ) . await {
1045+ cleanup_cdi_context_file ( sandbox, & self . config ) ;
1046+ return Err ( DockerProvisioningFailure :: new (
1047+ "SandboxTokenWriteFailed" ,
1048+ status. message ( ) ,
1049+ ) ) ;
1050+ }
10381051 async {
10391052 openshell_otel:: record_error_result (
10401053 self . docker
@@ -1048,9 +1061,7 @@ impl DockerComputeDriver {
10481061 )
10491062 . await
10501063 . map_err ( |err| {
1051- if token_file_created {
1052- cleanup_sandbox_token_file ( sandbox, & self . config ) ;
1053- }
1064+ cleanup_sandbox_state_files ( sandbox, & self . config ) ;
10541065 DockerProvisioningFailure :: from_status (
10551066 "ContainerCreateFailed" ,
10561067 create_status_from_docker_error ( "create docker sandbox container" , err) ,
@@ -1073,24 +1084,6 @@ impl DockerComputeDriver {
10731084 HashMap :: from ( [ ( "container_name" . to_string ( ) , container_name. clone ( ) ) ] ) ,
10741085 ) ;
10751086
1076- if let Some ( cdi_context) = cdi_context
1077- && let Err ( err) = self . upload_cdi_context ( & container_name, & cdi_context) . await
1078- {
1079- self . cleanup_created_container_after_failure (
1080- & sandbox. id ,
1081- & container_name,
1082- "CDI context upload failure" ,
1083- )
1084- . await ;
1085- if token_file_created {
1086- cleanup_sandbox_token_file ( sandbox, & self . config ) ;
1087- }
1088- return Err ( DockerProvisioningFailure :: from_status (
1089- "CdiContextUploadFailed" ,
1090- err,
1091- ) ) ;
1092- }
1093-
10941087 let start_result = async {
10951088 openshell_otel:: record_error_result (
10961089 self . docker . start_container ( & container_name, None ) . await ,
@@ -1111,9 +1104,7 @@ impl DockerComputeDriver {
11111104 "container start failure" ,
11121105 )
11131106 . await ;
1114- if token_file_created {
1115- cleanup_sandbox_token_file ( sandbox, & self . config ) ;
1116- }
1107+ cleanup_sandbox_state_files ( sandbox, & self . config ) ;
11171108 return Err ( DockerProvisioningFailure :: from_status (
11181109 "ContainerStartFailed" ,
11191110 create_status_from_docker_error ( "start docker sandbox container" , err) ,
@@ -1163,27 +1154,6 @@ impl DockerComputeDriver {
11631154 }
11641155 }
11651156
1166- async fn upload_cdi_context (
1167- & self ,
1168- container_name : & str ,
1169- context : & CdiContext ,
1170- ) -> Result < ( ) , Status > {
1171- let archive = build_cdi_context_archive ( context) . map_err ( Status :: internal) ?;
1172- self . docker
1173- . upload_to_container (
1174- container_name,
1175- Some (
1176- UploadToContainerOptionsBuilder :: default ( )
1177- . path ( "/run" )
1178- . no_overwrite_dir_non_dir ( "true" )
1179- . build ( ) ,
1180- ) ,
1181- body_full ( Bytes :: from ( archive) ) ,
1182- )
1183- . await
1184- . map_err ( |err| internal_status ( "upload CDI context to Docker container" , err) )
1185- }
1186-
11871157 async fn delete_sandbox_inner (
11881158 & self ,
11891159 sandbox_id : & str ,
@@ -1211,11 +1181,11 @@ impl DockerComputeDriver {
12111181 . await
12121182 {
12131183 Ok ( ( ) ) => {
1214- cleanup_sandbox_token_file ( & record. sandbox , & self . config ) ;
1184+ cleanup_sandbox_state_files ( & record. sandbox , & self . config ) ;
12151185 return Ok ( true ) ;
12161186 }
12171187 Err ( err) if is_not_found_error ( & err) => {
1218- cleanup_sandbox_token_file ( & record. sandbox , & self . config ) ;
1188+ cleanup_sandbox_state_files ( & record. sandbox , & self . config ) ;
12191189 return Ok ( true ) ;
12201190 }
12211191 Err ( err) => {
@@ -1238,11 +1208,11 @@ impl DockerComputeDriver {
12381208 . await
12391209 {
12401210 Ok ( ( ) ) => {
1241- cleanup_sandbox_token_file_for_delete ( sandbox_id, pending. as_ref ( ) , & self . config ) ;
1211+ cleanup_sandbox_state_files_for_delete ( sandbox_id, pending. as_ref ( ) , & self . config ) ;
12421212 Ok ( true )
12431213 }
12441214 Err ( err) if is_not_found_error ( & err) => {
1245- cleanup_sandbox_token_file_for_delete ( sandbox_id, pending. as_ref ( ) , & self . config ) ;
1215+ cleanup_sandbox_state_files_for_delete ( sandbox_id, pending. as_ref ( ) , & self . config ) ;
12461216 Ok ( pending. is_some ( ) )
12471217 }
12481218 Err ( err) => Err ( internal_status ( "delete docker sandbox container" , err) ) ,
@@ -1258,7 +1228,7 @@ impl DockerComputeDriver {
12581228 if let Some ( task) = record. task {
12591229 task. abort ( ) ;
12601230 }
1261- cleanup_sandbox_token_file ( & record. sandbox , & self . config ) ;
1231+ cleanup_sandbox_state_files ( & record. sandbox , & self . config ) ;
12621232 self . publish_deleted ( record. sandbox . id ) ;
12631233 return Ok ( ( ) ) ;
12641234 }
@@ -1435,7 +1405,7 @@ impl DockerComputeDriver {
14351405 sandbox : & DriverSandbox ,
14361406 failure : & DockerProvisioningFailure ,
14371407 ) {
1438- cleanup_sandbox_token_file ( sandbox, & self . config ) ;
1408+ cleanup_sandbox_state_files ( sandbox, & self . config ) ;
14391409 let snapshot = pending_sandbox_snapshot (
14401410 sandbox,
14411411 & self . config . sandbox_namespace ,
@@ -2717,6 +2687,7 @@ fn docker_volume_is_bind_backed(volume: &bollard::models::Volume) -> bool {
27172687fn build_binds (
27182688 sandbox : & DriverSandbox ,
27192689 config : & DockerDriverRuntimeConfig ,
2690+ gpu_device_ids : Option < & [ String ] > ,
27202691) -> Result < Vec < String > , Status > {
27212692 let mut binds = vec ! [ format!(
27222693 "{}:{}:ro,z" ,
@@ -2743,6 +2714,13 @@ fn build_binds(
27432714 SANDBOX_TOKEN_MOUNT_PATH
27442715 ) ) ;
27452716 }
2717+ if cdi_context_requested ( gpu_device_ids) {
2718+ binds. push ( format ! (
2719+ "{}:{}:ro,z" ,
2720+ cdi_context_host_path( sandbox, config) ?. display( ) ,
2721+ openshell_core:: cdi:: CDI_CONTEXT_PATH
2722+ ) ) ;
2723+ }
27462724 Ok ( binds)
27472725}
27482726
@@ -2769,6 +2747,57 @@ fn sandbox_token_host_path_by_id(
27692747 } )
27702748}
27712749
2750+ fn cdi_context_host_path (
2751+ sandbox : & DriverSandbox ,
2752+ config : & DockerDriverRuntimeConfig ,
2753+ ) -> Result < PathBuf , Status > {
2754+ cdi_context_host_path_by_id ( & sandbox. id , config)
2755+ }
2756+
2757+ fn cdi_context_host_path_by_id (
2758+ sandbox_id : & str ,
2759+ config : & DockerDriverRuntimeConfig ,
2760+ ) -> Result < PathBuf , Status > {
2761+ openshell_core:: driver_utils:: sandbox_token_path (
2762+ "docker-cdi-contexts" ,
2763+ Some ( & config. sandbox_namespace ) ,
2764+ sandbox_id,
2765+ )
2766+ . map ( |path| path. with_file_name ( openshell_core:: cdi:: CDI_CONTEXT_FILE_NAME ) )
2767+ . map_err ( |err| Status :: internal ( format ! ( "resolve CDI context state directory failed: {err}" ) ) )
2768+ }
2769+
2770+ fn write_cdi_context_file (
2771+ sandbox : & DriverSandbox ,
2772+ config : & DockerDriverRuntimeConfig ,
2773+ context : & CdiContext ,
2774+ ) -> Result < ( ) , Status > {
2775+ let path = cdi_context_host_path ( sandbox, config) ?;
2776+ if let Some ( parent) = path. parent ( ) {
2777+ openshell_core:: paths:: create_dir_restricted ( parent) . map_err ( |err| {
2778+ Status :: internal ( format ! (
2779+ "create CDI context directory {} failed: {err}" ,
2780+ parent. display( )
2781+ ) )
2782+ } ) ?;
2783+ }
2784+ let json = serde_json:: to_vec ( context)
2785+ . map_err ( |err| Status :: internal ( format ! ( "encode CDI context failed: {err}" ) ) ) ?;
2786+ std:: fs:: write ( & path, json) . map_err ( |err| {
2787+ Status :: internal ( format ! (
2788+ "write CDI context file {} failed: {err}" ,
2789+ path. display( )
2790+ ) )
2791+ } ) ?;
2792+ openshell_core:: paths:: set_file_owner_only ( & path) . map_err ( |err| {
2793+ Status :: internal ( format ! (
2794+ "restrict CDI context file {} failed: {err}" ,
2795+ path. display( )
2796+ ) )
2797+ } ) ?;
2798+ Ok ( ( ) )
2799+ }
2800+
27722801async fn write_sandbox_token_file (
27732802 sandbox : & DriverSandbox ,
27742803 config : & DockerDriverRuntimeConfig ,
@@ -2809,6 +2838,15 @@ fn cleanup_sandbox_token_file(sandbox: &DriverSandbox, config: &DockerDriverRunt
28092838 cleanup_sandbox_token_file_by_id ( & sandbox. id , config) ;
28102839}
28112840
2841+ fn cleanup_cdi_context_file ( sandbox : & DriverSandbox , config : & DockerDriverRuntimeConfig ) {
2842+ cleanup_cdi_context_file_by_id ( & sandbox. id , config) ;
2843+ }
2844+
2845+ fn cleanup_sandbox_state_files ( sandbox : & DriverSandbox , config : & DockerDriverRuntimeConfig ) {
2846+ cleanup_sandbox_token_file ( sandbox, config) ;
2847+ cleanup_cdi_context_file ( sandbox, config) ;
2848+ }
2849+
28122850fn cleanup_sandbox_token_file_for_delete (
28132851 sandbox_id : & str ,
28142852 pending : Option < & PendingSandboxRecord > ,
@@ -2821,6 +2859,27 @@ fn cleanup_sandbox_token_file_for_delete(
28212859 }
28222860}
28232861
2862+ fn cleanup_cdi_context_file_for_delete (
2863+ sandbox_id : & str ,
2864+ pending : Option < & PendingSandboxRecord > ,
2865+ config : & DockerDriverRuntimeConfig ,
2866+ ) {
2867+ if !sandbox_id. is_empty ( ) {
2868+ cleanup_cdi_context_file_by_id ( sandbox_id, config) ;
2869+ } else if let Some ( record) = pending {
2870+ cleanup_cdi_context_file ( & record. sandbox , config) ;
2871+ }
2872+ }
2873+
2874+ fn cleanup_sandbox_state_files_for_delete (
2875+ sandbox_id : & str ,
2876+ pending : Option < & PendingSandboxRecord > ,
2877+ config : & DockerDriverRuntimeConfig ,
2878+ ) {
2879+ cleanup_sandbox_token_file_for_delete ( sandbox_id, pending, config) ;
2880+ cleanup_cdi_context_file_for_delete ( sandbox_id, pending, config) ;
2881+ }
2882+
28242883fn cleanup_sandbox_token_file_by_id ( sandbox_id : & str , config : & DockerDriverRuntimeConfig ) {
28252884 let Ok ( path) = sandbox_token_host_path_by_id ( sandbox_id, config) else {
28262885 return ;
@@ -2840,6 +2899,25 @@ fn cleanup_sandbox_token_file_by_id(sandbox_id: &str, config: &DockerDriverRunti
28402899 }
28412900}
28422901
2902+ fn cleanup_cdi_context_file_by_id ( sandbox_id : & str , config : & DockerDriverRuntimeConfig ) {
2903+ let Ok ( path) = cdi_context_host_path_by_id ( sandbox_id, config) else {
2904+ return ;
2905+ } ;
2906+ if let Err ( err) = std:: fs:: remove_file ( & path)
2907+ && err. kind ( ) != std:: io:: ErrorKind :: NotFound
2908+ {
2909+ warn ! (
2910+ sandbox_id = %sandbox_id,
2911+ path = %path. display( ) ,
2912+ error = %err,
2913+ "Failed to remove Docker CDI context file"
2914+ ) ;
2915+ }
2916+ if let Some ( dir) = path. parent ( ) {
2917+ let _ = std:: fs:: remove_dir ( dir) ;
2918+ }
2919+ }
2920+
28432921#[ cfg( test) ]
28442922fn build_environment (
28452923 sandbox : & DriverSandbox ,
@@ -3012,64 +3090,6 @@ fn cdi_context_requested(gpu_device_ids: Option<&[String]>) -> bool {
30123090 gpu_device_ids. is_some_and ( |device_ids| !device_ids. is_empty ( ) )
30133091}
30143092
3015- fn build_cdi_context_archive ( context : & CdiContext ) -> Result < Vec < u8 > , String > {
3016- let json = serde_json:: to_vec_pretty ( context) . map_err ( |err| err. to_string ( ) ) ?;
3017- let mut archive = DockerTarArchiveBuilder :: new ( ) ;
3018- archive. append_dir ( "openshell" , 0o700 ) ?;
3019- archive. append_dir ( "openshell/supervisor" , 0o700 ) ?;
3020- archive. append_file (
3021- & format ! (
3022- "openshell/supervisor/{}" ,
3023- openshell_core:: cdi:: CDI_CONTEXT_FILE_NAME
3024- ) ,
3025- 0o600 ,
3026- json,
3027- ) ?;
3028- archive. into_inner ( )
3029- }
3030-
3031- struct DockerTarArchiveBuilder {
3032- inner : tar:: Builder < Vec < u8 > > ,
3033- }
3034-
3035- impl DockerTarArchiveBuilder {
3036- fn new ( ) -> Self {
3037- Self {
3038- inner : tar:: Builder :: new ( Vec :: new ( ) ) ,
3039- }
3040- }
3041-
3042- fn append_dir ( & mut self , path : & str , mode : u32 ) -> Result < ( ) , String > {
3043- let mut header = tar:: Header :: new_gnu ( ) ;
3044- header. set_entry_type ( tar:: EntryType :: Directory ) ;
3045- header. set_size ( 0 ) ;
3046- header. set_mode ( mode) ;
3047- header. set_uid ( 0 ) ;
3048- header. set_gid ( 0 ) ;
3049- header. set_cksum ( ) ;
3050- self . inner
3051- . append_data ( & mut header, path, std:: io:: empty ( ) )
3052- . map_err ( |err| err. to_string ( ) )
3053- }
3054-
3055- fn append_file ( & mut self , path : & str , mode : u32 , contents : Vec < u8 > ) -> Result < ( ) , String > {
3056- let mut header = tar:: Header :: new_gnu ( ) ;
3057- header. set_entry_type ( tar:: EntryType :: Regular ) ;
3058- header. set_size ( u64:: try_from ( contents. len ( ) ) . map_err ( |err| err. to_string ( ) ) ?) ;
3059- header. set_mode ( mode) ;
3060- header. set_uid ( 0 ) ;
3061- header. set_gid ( 0 ) ;
3062- header. set_cksum ( ) ;
3063- self . inner
3064- . append_data ( & mut header, path, Cursor :: new ( contents) )
3065- . map_err ( |err| err. to_string ( ) )
3066- }
3067-
3068- fn into_inner ( self ) -> Result < Vec < u8 > , String > {
3069- self . inner . into_inner ( ) . map_err ( |err| err. to_string ( ) )
3070- }
3071- }
3072-
30733093#[ cfg( test) ]
30743094fn build_container_create_body (
30753095 sandbox : & DriverSandbox ,
@@ -3223,7 +3243,7 @@ fn build_container_create_body_for_image(
32233243 pids_limit : docker_pids_limit ( config. sandbox_pids_limit ) ?,
32243244 device_requests,
32253245 binds : {
3226- let mut binds = build_binds ( sandbox, config) ?;
3246+ let mut binds = build_binds ( sandbox, config, gpu_device_ids ) ?;
32273247 binds. extend ( config. gpu . cdi_spec_bind_strings ( gpu_device_ids) ?) ;
32283248 binds. extend ( user_bind_strings) ;
32293249 Some ( binds)
0 commit comments