@@ -177,6 +177,11 @@ pub struct DockerComputeConfig {
177177 ///
178178 /// Set to `0` to leave Docker's runtime/default PID limit unchanged.
179179 pub sandbox_pids_limit : i64 ,
180+
181+ /// Allow sandbox requests to attach host bind mounts through
182+ /// `template.driver_config`.
183+ #[ serde( default ) ]
184+ pub enable_bind_mounts : bool ,
180185}
181186
182187impl Default for DockerComputeConfig {
@@ -195,6 +200,7 @@ impl Default for DockerComputeConfig {
195200 host_gateway_ip : String :: new ( ) ,
196201 ssh_socket_path : "/run/openshell/ssh.sock" . to_string ( ) ,
197202 sandbox_pids_limit : DEFAULT_SANDBOX_PIDS_LIMIT ,
203+ enable_bind_mounts : false ,
198204 }
199205 }
200206}
@@ -222,6 +228,7 @@ struct DockerDriverRuntimeConfig {
222228 daemon_version : String ,
223229 supports_gpu : bool ,
224230 sandbox_pids_limit : i64 ,
231+ enable_bind_mounts : bool ,
225232}
226233
227234#[ derive( Debug , Clone , PartialEq , Eq ) ]
@@ -268,6 +275,12 @@ struct DockerSandboxDriverConfig {
268275#[ derive( Debug , Clone , serde:: Deserialize ) ]
269276#[ serde( tag = "type" , rename_all = "snake_case" , deny_unknown_fields) ]
270277enum DockerDriverMountConfig {
278+ Bind {
279+ source : String ,
280+ target : String ,
281+ #[ serde( default ) ]
282+ read_only : bool ,
283+ } ,
271284 Volume {
272285 source : String ,
273286 target : String ,
@@ -356,6 +369,7 @@ impl DockerComputeDriver {
356369 daemon_version : version. version . unwrap_or_else ( || "unknown" . to_string ( ) ) ,
357370 supports_gpu,
358371 sandbox_pids_limit : docker_config. sandbox_pids_limit ,
372+ enable_bind_mounts : docker_config. enable_bind_mounts ,
359373 } ,
360374 events : broadcast:: channel ( WATCH_BUFFER ) . 0 ,
361375 pending : Arc :: new ( Mutex :: new ( HashMap :: new ( ) ) ) ,
@@ -420,7 +434,7 @@ impl DockerComputeDriver {
420434 ) ) ;
421435 }
422436
423- let _ = docker_driver_config ( template) ?;
437+ let _ = docker_driver_config ( template, config . enable_bind_mounts ) ?;
424438 let _ = docker_resource_limits ( template) ?;
425439 Ok ( ( ) )
426440 }
@@ -457,7 +471,7 @@ impl DockerComputeDriver {
457471 . as_ref ( )
458472 . and_then ( |spec| spec. template . as_ref ( ) )
459473 . ok_or_else ( || Status :: invalid_argument ( "sandbox.spec.template is required" ) ) ?;
460- let config = docker_driver_config ( template) ?;
474+ let config = docker_driver_config ( template, self . config . enable_bind_mounts ) ?;
461475 for mount in config. mounts {
462476 if let DockerDriverMountConfig :: Volume { source, .. } = mount {
463477 match self . docker . inspect_volume ( source. trim ( ) ) . await {
@@ -1568,6 +1582,7 @@ fn attach_docker_progress_metadata(
15681582
15691583fn docker_driver_config (
15701584 template : & DriverSandboxTemplate ,
1585+ enable_bind_mounts : bool ,
15711586) -> Result < DockerSandboxDriverConfig , Status > {
15721587 let Some ( config) = template. driver_config . as_ref ( ) else {
15731588 return Ok ( DockerSandboxDriverConfig :: default ( ) ) ;
@@ -1577,17 +1592,31 @@ fn docker_driver_config(
15771592 let config: DockerSandboxDriverConfig = serde_json:: from_value ( json) . map_err ( |err| {
15781593 Status :: failed_precondition ( format ! ( "invalid docker driver_config: {err}" ) )
15791594 } ) ?;
1580- validate_docker_driver_mounts ( & config. mounts ) ?;
1595+ validate_docker_driver_mounts ( & config. mounts , enable_bind_mounts ) ?;
15811596 Ok ( config)
15821597}
15831598
1584- fn docker_driver_mounts ( template : & DriverSandboxTemplate ) -> Result < Vec < Mount > , Status > {
1585- let config = docker_driver_config ( template) ?;
1599+ fn docker_driver_mounts (
1600+ template : & DriverSandboxTemplate ,
1601+ enable_bind_mounts : bool ,
1602+ ) -> Result < Vec < Mount > , Status > {
1603+ let config = docker_driver_config ( template, enable_bind_mounts) ?;
15861604 config. mounts . iter ( ) . map ( docker_mount_from_config) . collect ( )
15871605}
15881606
15891607fn docker_mount_from_config ( config : & DockerDriverMountConfig ) -> Result < Mount , Status > {
15901608 match config {
1609+ DockerDriverMountConfig :: Bind {
1610+ source,
1611+ target,
1612+ read_only,
1613+ } => Ok ( Mount {
1614+ typ : Some ( MountTypeEnum :: BIND ) ,
1615+ source : Some ( validate_absolute_mount_source ( source, "bind source" ) ?) ,
1616+ target : Some ( validate_container_mount_target ( target) ?) ,
1617+ read_only : Some ( * read_only) ,
1618+ ..Default :: default ( )
1619+ } ) ,
15911620 DockerDriverMountConfig :: Volume {
15921621 source,
15931622 target,
@@ -1637,10 +1666,22 @@ fn docker_mount_from_config(config: &DockerDriverMountConfig) -> Result<Mount, S
16371666 }
16381667}
16391668
1640- fn validate_docker_driver_mounts ( mounts : & [ DockerDriverMountConfig ] ) -> Result < ( ) , Status > {
1669+ fn validate_docker_driver_mounts (
1670+ mounts : & [ DockerDriverMountConfig ] ,
1671+ enable_bind_mounts : bool ,
1672+ ) -> Result < ( ) , Status > {
16411673 let mut targets = HashSet :: new ( ) ;
16421674 for mount in mounts {
16431675 let target = match mount {
1676+ DockerDriverMountConfig :: Bind { source, target, .. } => {
1677+ if !enable_bind_mounts {
1678+ return Err ( Status :: failed_precondition (
1679+ "docker bind mounts require enable_bind_mounts = true in [openshell.drivers.docker]" ,
1680+ ) ) ;
1681+ }
1682+ validate_absolute_mount_source ( source, "bind source" ) ?;
1683+ target
1684+ }
16441685 DockerDriverMountConfig :: Volume {
16451686 source,
16461687 target,
@@ -1677,6 +1718,16 @@ fn validate_docker_driver_mounts(mounts: &[DockerDriverMountConfig]) -> Result<(
16771718 Ok ( ( ) )
16781719}
16791720
1721+ fn validate_absolute_mount_source ( source : & str , field : & str ) -> Result < String , Status > {
1722+ let source = validate_mount_source ( source, field) ?;
1723+ if !Path :: new ( & source) . is_absolute ( ) {
1724+ return Err ( Status :: failed_precondition ( format ! (
1725+ "{field} must be an absolute host path"
1726+ ) ) ) ;
1727+ }
1728+ Ok ( source)
1729+ }
1730+
16801731fn validate_mount_source ( source : & str , field : & str ) -> Result < String , Status > {
16811732 let source = source. trim ( ) ;
16821733 if source. is_empty ( ) {
@@ -2100,7 +2151,7 @@ fn build_container_create_body(
21002151 . as_ref ( )
21012152 . ok_or_else ( || Status :: invalid_argument ( "sandbox.spec.template is required" ) ) ?;
21022153 let resource_limits = docker_resource_limits ( template) ?;
2103- let user_mounts = docker_driver_mounts ( template) ?;
2154+ let user_mounts = docker_driver_mounts ( template, config . enable_bind_mounts ) ?;
21042155 let mut labels = template. labels . clone ( ) ;
21052156 labels. insert (
21062157 LABEL_MANAGED_BY . to_string ( ) ,
0 commit comments