Skip to content

Commit

Permalink
install: have --preserve-on-error write saved partitions to tempfile
Browse files Browse the repository at this point in the history
If the user asked for --preserve-on-error we shouldn't tamper with the
partition state as of the moment of failure, but we also shouldn't lose
the user's saved partitions.  Compromise by writing them to a file in
/tmp and telling the user the path.  They can use that file for recovery
if they wish.
  • Loading branch information
bgilbert committed Jul 31, 2020
1 parent 795e10b commit 795be14
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/blockdev.rs
Expand Up @@ -657,6 +657,10 @@ impl SavedPartitions {
))),
}
}

pub fn is_saved(&self) -> bool {
!self.partitions.is_empty()
}
}

fn read_sysfs_dev_block_value_u64(maj: u64, min: u64, field: &str) -> Result<u64> {
Expand Down
26 changes: 26 additions & 0 deletions src/install.rs
Expand Up @@ -95,6 +95,32 @@ pub fn install(config: &InstallConfig) -> Result<()> {
// clean up
if config.preserve_on_error {
eprintln!("Preserving partition table as requested");
if saved.is_saved() {
// The user asked to preserve the damaged partition table
// for debugging. We also have saved partitions, and those
// may or may not be in the damaged table depending where we
// failed. Preserve the saved partitions by writing them to
// a file in /tmp and telling the user about it. Hey, it's
// a debug flag.
let stash = tempfile::Builder::new()
.prefix("coreos-installer-partitions.")
.tempfile()
.chain_err(|| "creating partition stash file")?;
eprintln!(
"Storing saved partition entries to {}",
stash.path().display()
);
stash
.as_file()
.set_len(1024 * 1024)
.chain_err(|| "extending partition stash file")?;
saved
.write(stash.path())
.chain_err(|| "stashing saved partitions")?;
stash
.keep()
.chain_err(|| "retaining saved partition stash")?;
}
} else {
clear_partition_table(&mut dest, &mut *table)?;
saved
Expand Down

0 comments on commit 795be14

Please sign in to comment.