99//! table. Everything read from a disk is untrusted: kind handlers use the
1010//! measured app compose as their source of policy and cryptographic identity.
1111
12+ use std:: collections:: HashMap ;
1213use std:: io:: Read ;
1314use std:: os:: unix:: ffi:: OsStrExt ;
1415use std:: os:: unix:: fs:: MetadataExt ;
@@ -96,13 +97,24 @@ fn mount_all(compose_path: PathBuf) -> Result<()> {
9697 requested = compose. verity_volumes. len( ) ,
9798 "discovered dstack volumes"
9899 ) ;
100+ let mut active_roots: HashMap < [ u8 ; 32 ] , PathBuf > = HashMap :: new ( ) ;
99101 for ( index, requested) in compose. verity_volumes . iter ( ) . enumerate ( ) {
100- activate_requested ( index, requested, & volumes) . with_context ( || {
102+ if let Some ( mapped) = active_roots. get ( & requested. verity_root ) {
103+ mount_volume ( requested, mapped) . with_context ( || {
104+ format ! (
105+ "failed to mount required volume {index} at {}" ,
106+ requested. target. display( )
107+ )
108+ } ) ?;
109+ continue ;
110+ }
111+ let mapped = activate_requested ( index, requested, & volumes) . with_context ( || {
101112 format ! (
102113 "failed to activate required volume {index} at {}" ,
103114 requested. target. display( )
104115 )
105116 } ) ?;
117+ active_roots. insert ( requested. verity_root , mapped) ;
106118 }
107119 Ok ( ( ) )
108120}
@@ -126,10 +138,17 @@ fn status(compose_path: PathBuf) -> Result<()> {
126138 let attached = volumes
127139 . iter ( )
128140 . any ( |volume| volume. root_hash == requested. verity_root ) ;
129- let mapper_name = format ! ( "dstack-verity{index}" ) ;
130- let active = Path :: new ( "/dev/mapper" ) . join ( & mapper_name) . exists ( )
131- && mapping_root ( & mapper_name) ?
132- . eq_ignore_ascii_case ( & hex:: encode ( requested. verity_root ) ) ;
141+ let expected_root = hex:: encode ( requested. verity_root ) ;
142+ let mut active = false ;
143+ for mapper_index in 0 ..compose. verity_volumes . len ( ) {
144+ let mapper_name = format ! ( "dstack-verity{mapper_index}" ) ;
145+ if Path :: new ( "/dev/mapper" ) . join ( & mapper_name) . exists ( )
146+ && mapping_root ( & mapper_name) ?. eq_ignore_ascii_case ( & expected_root)
147+ {
148+ active = true ;
149+ break ;
150+ }
151+ }
133152 println ! (
134153 "{index}\t root={}\t target={}\t attached={attached}\t active={active}" ,
135154 hex:: encode( requested. verity_root) ,
@@ -275,7 +294,7 @@ fn activate_requested(
275294 index : usize ,
276295 requested : & RequestedVolume ,
277296 volumes : & [ VerityVolume ] ,
278- ) -> Result < ( ) > {
297+ ) -> Result < PathBuf > {
279298 let candidate = volumes
280299 . iter ( )
281300 . find ( |volume| volume. root_hash == requested. verity_root )
@@ -293,7 +312,7 @@ fn activate_requested(
293312 return Err ( err) ;
294313 }
295314 info ! ( mapper = mapper_name, "reused active verity mapping" ) ;
296- return Ok ( ( ) ) ;
315+ return Ok ( mapped ) ;
297316 }
298317 run_cmd ! ( veritysetup close $mapper_name) . context ( "closing stale verity mapping" ) ?;
299318 }
@@ -308,7 +327,7 @@ fn activate_requested(
308327 let _ = run_cmd ! ( veritysetup close $mapper_name) ;
309328 return Err ( err) ;
310329 }
311- Ok ( ( ) )
330+ Ok ( mapped )
312331}
313332
314333fn verify_first_block ( path : & Path ) -> Result < ( ) > {
0 commit comments