Merge "Support sending VM log output to a file."
diff --git a/vm/src/main.rs b/vm/src/main.rs
index 2c93ec4..b79f42a 100644
--- a/vm/src/main.rs
+++ b/vm/src/main.rs
@@ -43,6 +43,10 @@
         /// Detach VM from the terminal and run in the background
         #[structopt(short, long)]
         daemonize: bool,
+
+        /// Path to file for VM log output.
+        #[structopt(short, long)]
+        log: Option<PathBuf>,
     },
     /// Stop a virtual machine running in the background
     Stop {
@@ -73,7 +77,9 @@
         .context("Failed to find VirtualizationService")?;
 
     match opt {
-        Opt::Run { config, daemonize } => command_run(service, &config, daemonize),
+        Opt::Run { config, daemonize, log } => {
+            command_run(service, &config, daemonize, log.as_deref())
+        }
         Opt::Stop { cid } => command_stop(service, cid),
         Opt::List => command_list(service),
         Opt::CreatePartition { path, size } => command_create_partition(service, &path, size),
diff --git a/vm/src/run.rs b/vm/src/run.rs
index ab4222f..ec95646 100644
--- a/vm/src/run.rs
+++ b/vm/src/run.rs
@@ -36,12 +36,21 @@
     service: Strong<dyn IVirtualizationService>,
     config_path: &Path,
     daemonize: bool,
+    log_path: Option<&Path>,
 ) -> Result<(), Error> {
     let config_file = File::open(config_path).context("Failed to open config file")?;
     let config =
         VmConfig::load(&config_file).context("Failed to parse config file")?.to_parcelable()?;
-    let stdout =
-        if daemonize { None } else { Some(ParcelFileDescriptor::new(duplicate_stdout()?)) };
+    let stdout = if let Some(log_path) = log_path {
+        Some(ParcelFileDescriptor::new(
+            File::create(log_path)
+                .with_context(|| format!("Failed to open log file {:?}", log_path))?,
+        ))
+    } else if daemonize {
+        None
+    } else {
+        Some(ParcelFileDescriptor::new(duplicate_stdout()?))
+    };
     let vm = service.startVm(&config, stdout.as_ref()).context("Failed to start VM")?;
 
     let cid = vm.getCid().context("Failed to get CID")?;