Skip to content

Commit

Permalink
EncryptApfsVolume: fixups (#1333)
Browse files Browse the repository at this point in the history
* EncryptApfsVolume: retry mounting the disk more times

* EncryptApfsVolume: pass disk passphrase on stdin
  • Loading branch information
cole-h authored Dec 3, 2024
1 parent 832d7e4 commit 3de46d9
Showing 1 changed file with 54 additions and 13 deletions.
67 changes: 54 additions & 13 deletions src/action/macos/encrypt_apfs_volume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::{
path::{Path, PathBuf},
process::Stdio,
};
use tokio::process::Command;
use tokio::{io::AsyncWriteExt as _, process::Command};
use tracing::{span, Span};

use super::CreateApfsVolume;
Expand Down Expand Up @@ -173,7 +173,7 @@ impl Action for EncryptApfsVolume {

let disk_str = &self.disk.to_str().expect("Could not turn disk into string"); /* Should not reasonably ever fail */

let mut retry_tokens: usize = 10;
let mut retry_tokens: usize = 60;
loop {
let mut command = Command::new("/usr/sbin/diskutil");
command.process_group(0);
Expand Down Expand Up @@ -235,17 +235,58 @@ impl Action for EncryptApfsVolume {
execute_command(&mut cmd).await.map_err(Self::error)?;

// Encrypt the mounted volume
execute_command(Command::new("/usr/sbin/diskutil").process_group(0).args([
"apfs",
"encryptVolume",
self.name.as_str(),
"-user",
"disk",
"-passphrase",
password.as_str(),
]))
.await
.map_err(Self::error)?;
{
let mut command = Command::new("/usr/sbin/diskutil");
command.process_group(0);
command.args([
"apfs",
"encryptVolume",
self.name.as_str(),
"-user",
"disk",
"-stdinpassphrase",
]);
command.stdin(Stdio::piped());
command.stdout(Stdio::piped());
command.stderr(Stdio::piped());
tracing::trace!(command = ?command.as_std(), "Executing");
let mut child = command
.spawn()
.map_err(|e| ActionErrorKind::command(&command, e))
.map_err(Self::error)?;
let mut stdin = child
.stdin
.take()
.expect("child should have had a stdin handle");
stdin
.write_all(password.as_bytes())
.await
.map_err(|e| ActionErrorKind::Write("/dev/stdin".into(), e))
.map_err(Self::error)?;
stdin
.write(b"\n")
.await
.map_err(|e| ActionErrorKind::Write("/dev/stdin".into(), e))
.map_err(Self::error)?;
let output = child
.wait_with_output()
.await
.map_err(|e| ActionErrorKind::command(&command, e))
.map_err(Self::error)?;
match output.status.success() {
true => {
tracing::trace!(
command = ?command.as_std(),
stderr = %String::from_utf8_lossy(&output.stderr),
stdout = %String::from_utf8_lossy(&output.stdout),
"Command success"
);
},
false => Err(Self::error(ActionErrorKind::command_output(
&command, output,
)))?,
}
}

execute_command(
Command::new("/usr/sbin/diskutil")
Expand Down

0 comments on commit 3de46d9

Please sign in to comment.