@@ -693,6 +693,56 @@ pub struct RunningQemu {
693693impl RunningQemu {
694694 /// Spawn QEMU
695695 pub async fn spawn ( mut config : QemuConfig ) -> Result < Self > {
696+ // Spawn all virtiofsd processes first
697+ let mut awaiting_virtiofsd = Vec :: new ( ) ;
698+ let virtiofsd_configs = config
699+ . main_virtiofs_config
700+ . iter ( )
701+ . chain ( config. virtiofs_configs . iter ( ) ) ;
702+ for config in virtiofsd_configs {
703+ let process = spawn_virtiofsd_async ( config) . await ?;
704+ awaiting_virtiofsd. push ( ( process, config. socket_path . clone ( ) ) ) ;
705+ }
706+
707+ // Wait for all virtiofsd to be ready
708+ let mut virtiofsd_processes = Vec :: new ( ) ;
709+ while let Some ( ( proc, socket_path) ) = awaiting_virtiofsd. pop ( ) {
710+ let socket_path = & socket_path;
711+ let query_exists = async move {
712+ loop {
713+ if socket_path. exists ( ) {
714+ break ;
715+ }
716+ tokio:: time:: sleep ( Duration :: from_millis ( 100 ) ) . await ;
717+ }
718+ } ;
719+ tokio:: pin!( query_exists) ;
720+ let timeout_val = Duration :: from_secs ( 60 ) ;
721+ let timeout = tokio:: time:: sleep ( timeout_val) ;
722+ tokio:: pin!( timeout) ;
723+ debug ! ( "Waiting for socket at {socket_path}" ) ;
724+ let mut output: Pin < Box < dyn Future < Output = std:: io:: Result < Output > > > > =
725+ Box :: pin ( proc. wait_with_output ( ) ) ;
726+ tokio:: select! {
727+ output = & mut output => {
728+ tracing:: trace!( "virtiofsd exited" ) ;
729+ let output = output?;
730+ let status = output. status;
731+ let stderr = String :: from_utf8_lossy( & output. stderr) ;
732+ return Err ( eyre!(
733+ "virtiofsd failed to start for socket {socket_path}\n Exit status: {status:?}\n Output: {stderr}"
734+ ) ) ;
735+ }
736+ _ = timeout => {
737+ return Err ( eyre!( "timed out waiting for virtiofsd socket {} to be created (waited {timeout_val:?})" , socket_path) ) ;
738+ }
739+ _ = query_exists => {
740+ }
741+ }
742+ virtiofsd_processes. push ( output) ;
743+ tracing:: debug!( "virtiofsd socket created: {socket_path}" ) ;
744+ }
745+
696746 let vsockdata = if let Some ( vhost_fd) = config. vhost_fd . take ( ) {
697747 // Get a unique guest CID using dynamic allocation
698748 // If /dev/vhost-vsock is not available, fall back to disabled vsock
@@ -793,56 +843,6 @@ impl RunningQemu {
793843 } )
794844 . unwrap_or_default ( ) ;
795845
796- // Spawn all virtiofsd processes first
797- let mut awaiting_virtiofsd = Vec :: new ( ) ;
798- let virtiofsd_configs = config
799- . main_virtiofs_config
800- . iter ( )
801- . chain ( config. virtiofs_configs . iter ( ) ) ;
802- for config in virtiofsd_configs {
803- let process = spawn_virtiofsd_async ( config) . await ?;
804- awaiting_virtiofsd. push ( ( process, config. socket_path . clone ( ) ) ) ;
805- }
806-
807- // Wait for all virtiofsd to be ready
808- let mut virtiofsd_processes = Vec :: new ( ) ;
809- while let Some ( ( proc, socket_path) ) = awaiting_virtiofsd. pop ( ) {
810- let socket_path = & socket_path;
811- let query_exists = async move {
812- loop {
813- if socket_path. exists ( ) {
814- break ;
815- }
816- tokio:: time:: sleep ( Duration :: from_millis ( 100 ) ) . await ;
817- }
818- } ;
819- tokio:: pin!( query_exists) ;
820- let timeout_val = Duration :: from_secs ( 60 ) ;
821- let timeout = tokio:: time:: sleep ( timeout_val) ;
822- tokio:: pin!( timeout) ;
823- debug ! ( "Waiting for socket at {socket_path}" ) ;
824- let mut output: Pin < Box < dyn Future < Output = std:: io:: Result < Output > > > > =
825- Box :: pin ( proc. wait_with_output ( ) ) ;
826- tokio:: select! {
827- output = & mut output => {
828- tracing:: trace!( "virtiofsd exited" ) ;
829- let output = output?;
830- let status = output. status;
831- let stderr = String :: from_utf8_lossy( & output. stderr) ;
832- tracing:: trace!( "returnign spawn error" ) ;
833- return Err ( eyre!(
834- "virtiofsd failed to start for socket {socket_path}\n Exit status: {status:?}\n Output: {stderr}"
835- ) ) ;
836- }
837- _ = timeout => {
838- return Err ( eyre!( "timed out waiting for virtiofsd socket {} to be created (waited {timeout_val:?})" , socket_path) ) ;
839- }
840- _ = query_exists => {
841- }
842- }
843- virtiofsd_processes. push ( output) ;
844- tracing:: debug!( "virtiofsd socket created: {socket_path}" ) ;
845- }
846846 // Spawn QEMU process with additional VSOCK credential if needed
847847 let qemu_process = spawn ( & config, & creds, vsockdata) ?;
848848
0 commit comments