@@ -55,6 +55,19 @@ const QEMU_ARGS: &[&[&str]] = &[
5555 "if=virtio,format=raw,discard=unmap,cache.writeback=on,cache.direct=on,cache.no-flush=on,file=job-config.img" ,
5656 ] ,
5757] ;
58+ const QEMU_ARGS_SWTPM : & [ & [ & str ] ] = & [
59+ & [ "-chardev" , "socket,id=chrtpm,path=swtpm.ctrl" ] ,
60+ & [ "-tpmdev" , "emulator,id=tpm0,chardev=chrtpm" ] ,
61+ & [ "-device" , "tpm-tis,tpmdev=tpm0" ] ,
62+ ] ;
63+
64+ const SWTPM_CMD : & str = "/usr/bin/swtpm" ;
65+ const SWTPM_ARGS : & [ & [ & str ] ] = & [
66+ & [ "socket" ] ,
67+ & [ "--tpm2" ] ,
68+ & [ "--tpmstate" , "backend-uri=file://tpm.swtpm" ] ,
69+ & [ "--ctrl" , "type=unixio,path=swtpm.ctrl" ] ,
70+ ] ;
5871
5972#[ derive( PartialEq , Clone , Copy , Debug ) ]
6073pub ( super ) enum Status {
@@ -415,6 +428,30 @@ impl Machine {
415428 [ "-virtfs" . into ( ) , arg] . into_iter ( )
416429 } ) ;
417430
431+ // Spawn a software TPM emulation if a state file is present.
432+ // We do not monitor the status of this process or wait for the control
433+ // socket to be ready.
434+ // Instead we just hope that it starts successfully.
435+ let ( _swtpm, swtpm_args) = {
436+ let inner = self . inner ( ) ;
437+ let pwd = inner. run_dir . as_ref ( ) . unwrap ( ) ;
438+
439+ if pwd. path ( ) . join ( "tpm.swtpm" ) . exists ( ) {
440+ let mut swtpm = Command :: new ( SWTPM_CMD ) ;
441+
442+ swtpm
443+ . kill_on_drop ( true )
444+ . current_dir ( pwd. path ( ) )
445+ . args ( SWTPM_ARGS . iter ( ) . flat_map ( |arg_list| * arg_list) ) ;
446+
447+ let child = swtpm. spawn ( ) ?;
448+
449+ ( Some ( child) , QEMU_ARGS_SWTPM )
450+ } else {
451+ ( None , [ ] . as_slice ( ) )
452+ }
453+ } ;
454+
418455 // Assemble the complete set of arguments to pass to the qemu command.
419456 let mut qemu = {
420457 let inner = self . inner ( ) ;
@@ -431,6 +468,7 @@ impl Machine {
431468 . arg ( "-smp" )
432469 . arg ( & smp)
433470 . args ( QEMU_ARGS . iter ( ) . flat_map ( |arg_list| * arg_list) )
471+ . args ( swtpm_args. iter ( ) . flat_map ( |arg_list| * arg_list) )
434472 . args ( virtfs_args) ;
435473
436474 qemu
0 commit comments