Install AM335x mtd7/mtd8 over SSH with held backups
This commit is contained in:
+201
-16
@@ -8,6 +8,9 @@
|
||||
use alkane_artifacts::afw::{verify_afw, VerifiedAfw, VerifyOptions};
|
||||
use alkane_artifacts::{seed_manifest, verify_file};
|
||||
use alkane_core::platform;
|
||||
use alkane_flash::boards::am335x::{
|
||||
self as am335x, Am335xPlan, AmInstallOptions, AmInstallOutcome, AmRestoreOptions,
|
||||
};
|
||||
use alkane_flash::boards::cv1835::Cv1835Plan;
|
||||
use alkane_flash::install::{
|
||||
install, restore, verify_live, InstallOptions, InstallOutcome, RestoreOptions,
|
||||
@@ -313,6 +316,34 @@ fn run_plan(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
|
||||
.first()
|
||||
.ok_or("usage: alkane-cli afw plan <file> --host <ip>")?;
|
||||
let verified = load_afw(path, &opts)?;
|
||||
if verified.manifest.board == "am335x" {
|
||||
let plan = Am335xPlan::from_afw(&verified)?;
|
||||
let (ssh, _secret) = ssh_options(&opts, DEFAULT_USER)?;
|
||||
let mut transport = SshTransport::connect(ssh, true)?;
|
||||
let kernel =
|
||||
String::from_utf8_lossy(&transport.user_command("uname -r", Duration::from_secs(15))?)
|
||||
.trim()
|
||||
.to_owned();
|
||||
let uid =
|
||||
String::from_utf8_lossy(&transport.root_command("id -u", Duration::from_secs(15))?)
|
||||
.trim()
|
||||
.to_owned();
|
||||
let mtd = String::from_utf8_lossy(&transport.user_command(
|
||||
"cat /proc/mtd 2>/dev/null",
|
||||
Duration::from_secs(15),
|
||||
)?)
|
||||
.into_owned();
|
||||
println!("{}", plan.render());
|
||||
println!("current kernel {kernel} uid {uid}");
|
||||
println!("board mtd table:\n{mtd}");
|
||||
println!(
|
||||
"root channel {} (transport {}, never serial)",
|
||||
transport.root_method(),
|
||||
transport.channel()
|
||||
);
|
||||
println!("plan is read-only; no write performed");
|
||||
return Ok(());
|
||||
}
|
||||
let plan = Cv1835Plan::from_afw(&verified)?;
|
||||
let (ssh, _secret) = ssh_options(&opts, DEFAULT_USER)?;
|
||||
let mut transport = SshTransport::connect(ssh, true)?;
|
||||
@@ -357,9 +388,18 @@ fn run_preflight(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
|
||||
.first()
|
||||
.ok_or("usage: alkane-cli afw preflight <file> --host <ip>")?;
|
||||
let verified = load_afw(path, &opts)?;
|
||||
let plan = Cv1835Plan::from_afw(&verified)?;
|
||||
let (ssh, _secret) = ssh_options(&opts, DEFAULT_USER)?;
|
||||
let mut transport = SshTransport::connect(ssh, true)?;
|
||||
if verified.manifest.board == "am335x" {
|
||||
let plan = Am335xPlan::from_afw(&verified)?;
|
||||
let report = am335x::preflight(&plan, &mut transport, opts.value("host").unwrap_or(""))?;
|
||||
print!("{}", report.render());
|
||||
if !report.ok() {
|
||||
return Err("preflight failed; do not write".into());
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
let plan = Cv1835Plan::from_afw(&verified)?;
|
||||
let held_backup = opts.value("held-backup").map(PathBuf::from);
|
||||
let report = preflight(
|
||||
&plan,
|
||||
@@ -399,13 +439,57 @@ fn run_install(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
|
||||
.first()
|
||||
.ok_or("usage: alkane-cli afw install <file> --host <ip> --dry-run|--confirm FLASH")?;
|
||||
let verified = load_afw(path, &opts)?;
|
||||
let plan = Cv1835Plan::from_afw(&verified)?;
|
||||
let (ssh, _secret) = ssh_options(&opts, DEFAULT_USER)?;
|
||||
let mut transport = SshTransport::connect(ssh, true)?;
|
||||
let backup_dir = opts
|
||||
.value("backup-dir")
|
||||
.map(PathBuf::from)
|
||||
.unwrap_or_else(|| platform::config_path("backups"));
|
||||
if verified.manifest.board == "am335x" {
|
||||
let plan = Am335xPlan::from_afw(&verified)?;
|
||||
let install_options = AmInstallOptions {
|
||||
dry_run: opts.flag("dry-run"),
|
||||
confirm: opts.value("confirm").map(str::to_owned),
|
||||
no_reboot: opts.flag("no-reboot"),
|
||||
backup_dir,
|
||||
};
|
||||
println!("{}", plan.render());
|
||||
match am335x::install(
|
||||
&plan,
|
||||
&mut transport,
|
||||
&install_options,
|
||||
opts.value("host").unwrap_or(""),
|
||||
)? {
|
||||
AmInstallOutcome::DryRun(report) => {
|
||||
print!("{}", report.render());
|
||||
println!("DRY RUN: no board erase or write was performed.");
|
||||
println!("Re-run with --confirm FLASH to write mtd8, mtd6 and mtd7.");
|
||||
}
|
||||
AmInstallOutcome::Installed(receipt) => {
|
||||
for backup in &receipt.backups {
|
||||
println!(
|
||||
"held {} {} md5 {} sha256 {}",
|
||||
backup.partition,
|
||||
backup.path.display(),
|
||||
backup.md5,
|
||||
backup.sha256
|
||||
);
|
||||
}
|
||||
println!("all three mtd read-backs matched before reboot");
|
||||
if receipt.rebooted {
|
||||
println!("reboot issued; an SSH drop here is the success signature");
|
||||
println!(
|
||||
"next: alkane-cli afw verify-live --host {} --timeout 300",
|
||||
opts.value("host").unwrap_or("")
|
||||
);
|
||||
} else {
|
||||
println!("reboot skipped (--no-reboot); verify-live will fail until the board reboots");
|
||||
}
|
||||
}
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
let plan = Cv1835Plan::from_afw(&verified)?;
|
||||
let install_options = InstallOptions {
|
||||
dry_run: opts.flag("dry-run"),
|
||||
confirm: opts.value("confirm").map(str::to_owned),
|
||||
@@ -455,26 +539,52 @@ fn run_verify_live(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
|
||||
args,
|
||||
&[
|
||||
ssh_values(),
|
||||
&["marker", "kernel-version", "timeout", "expect-board"],
|
||||
&["marker", "kernel-version", "timeout", "expect-board", "board"],
|
||||
]
|
||||
.concat(),
|
||||
&["allow-unsigned"],
|
||||
)?;
|
||||
let (marker, kernel_version) = if let Some(path) = opts.positionals.first() {
|
||||
let requested_board = opts.value("board").map(str::to_owned);
|
||||
let (board, marker, kernel_version) = if let Some(path) = opts.positionals.first() {
|
||||
let verified = load_afw(path, &opts)?;
|
||||
let plan = Cv1835Plan::from_afw(&verified)?;
|
||||
(
|
||||
opts.value("marker").unwrap_or(&plan.marker).to_owned(),
|
||||
opts.value("kernel-version")
|
||||
.unwrap_or(&plan.kernel_version)
|
||||
.to_owned(),
|
||||
)
|
||||
let board = verified.manifest.board.clone();
|
||||
if board == "am335x" {
|
||||
let plan = Am335xPlan::from_afw(&verified)?;
|
||||
(
|
||||
board,
|
||||
opts.value("marker").unwrap_or(&plan.marker).to_owned(),
|
||||
opts.value("kernel-version")
|
||||
.unwrap_or(&plan.kernel_version)
|
||||
.to_owned(),
|
||||
)
|
||||
} else {
|
||||
let plan = Cv1835Plan::from_afw(&verified)?;
|
||||
(
|
||||
board,
|
||||
opts.value("marker").unwrap_or(&plan.marker).to_owned(),
|
||||
opts.value("kernel-version")
|
||||
.unwrap_or(&plan.kernel_version)
|
||||
.to_owned(),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
let board = requested_board.clone().unwrap_or_else(|| "cv1835".to_owned());
|
||||
let (marker, kernel) = if board == "am335x" {
|
||||
("Alkane".to_owned(), "6.18.48".to_owned())
|
||||
} else {
|
||||
("CVBOOT".to_owned(), String::new())
|
||||
};
|
||||
(
|
||||
opts.value("marker").unwrap_or("CVBOOT").to_owned(),
|
||||
opts.value("kernel-version").unwrap_or("").to_owned(),
|
||||
board,
|
||||
opts.value("marker").unwrap_or(&marker).to_owned(),
|
||||
opts.value("kernel-version").unwrap_or(&kernel).to_owned(),
|
||||
)
|
||||
};
|
||||
if let Some(expected) = &requested_board {
|
||||
if expected != &board {
|
||||
return Err(format!("bundle is for {board}, expected {expected}").into());
|
||||
}
|
||||
}
|
||||
let host = opts.value("host").ok_or("--host is required")?.to_owned();
|
||||
let (ssh, _secret) = ssh_options(&opts, DEFAULT_USER)?;
|
||||
let timeout = opts
|
||||
@@ -482,6 +592,20 @@ fn run_verify_live(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
|
||||
.map(str::parse::<u64>)
|
||||
.transpose()?
|
||||
.unwrap_or(DEFAULT_TIMEOUT);
|
||||
if board == "am335x" {
|
||||
let report = am335x::verify_live(
|
||||
&host,
|
||||
&marker,
|
||||
&kernel_version,
|
||||
Duration::from_secs(timeout),
|
||||
&ssh,
|
||||
);
|
||||
print!("{}", report.render());
|
||||
if !report.ok() {
|
||||
return Err("live verification failed: SSH, HTTP marker, API or DHCP disagreed".into());
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
let report = verify_live(
|
||||
&host,
|
||||
&marker,
|
||||
@@ -500,11 +624,54 @@ fn run_verify_live(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
|
||||
fn run_restore(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let opts = parse_args(
|
||||
args,
|
||||
&[ssh_values(), &["held-backup", "backup-dir", "confirm"]].concat(),
|
||||
&[
|
||||
ssh_values(),
|
||||
&["held-backup", "backup-dir", "confirm", "board", "held-kernel", "held-dtb"],
|
||||
]
|
||||
.concat(),
|
||||
&["no-reboot"],
|
||||
)?;
|
||||
let (ssh, _secret) = ssh_options(&opts, DEFAULT_USER)?;
|
||||
let mut transport = SshTransport::connect(ssh, true)?;
|
||||
if opts.value("board") == Some("am335x") {
|
||||
let dir = opts
|
||||
.value("backup-dir")
|
||||
.map(PathBuf::from)
|
||||
.unwrap_or_else(|| platform::config_path("backups"));
|
||||
let mut backups: Vec<(String, PathBuf)> = Vec::new();
|
||||
for (name, value, prefix) in [
|
||||
("mtd8", opts.value("held-backup"), "mtd8-"),
|
||||
("mtd7", opts.value("held-kernel"), "mtd7-"),
|
||||
("mtd6", opts.value("held-dtb"), "mtd6-"),
|
||||
] {
|
||||
let path = match value {
|
||||
Some(path) => PathBuf::from(path),
|
||||
None => latest_backup_for(&dir, prefix).ok_or_else(|| {
|
||||
format!(
|
||||
"no held {name} backup in {}; pass the matching --held-* option",
|
||||
dir.display()
|
||||
)
|
||||
})?,
|
||||
};
|
||||
backups.push((name.to_owned(), path));
|
||||
}
|
||||
let receipt = am335x::restore(
|
||||
&backups,
|
||||
&mut transport,
|
||||
&AmRestoreOptions {
|
||||
confirm: opts.value("confirm").map(str::to_owned),
|
||||
no_reboot: opts.flag("no-reboot"),
|
||||
},
|
||||
)?;
|
||||
for (name, path) in &receipt.restored {
|
||||
println!("restored {name} from {}", path.display());
|
||||
}
|
||||
println!("all mtd read-backs matched before reboot");
|
||||
if receipt.rebooted {
|
||||
println!("reboot issued; the board should return to its held OS");
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
let backup = if let Some(path) = opts.value("held-backup") {
|
||||
PathBuf::from(path)
|
||||
} else {
|
||||
@@ -536,6 +703,23 @@ fn run_restore(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn latest_backup_for(dir: &Path, prefix: &str) -> Option<PathBuf> {
|
||||
let mut entries: Vec<PathBuf> = fs::read_dir(dir)
|
||||
.ok()?
|
||||
.filter_map(Result::ok)
|
||||
.map(|entry| entry.path())
|
||||
.filter(|path| {
|
||||
path.extension() == Some(std::ffi::OsStr::new("img"))
|
||||
&& path
|
||||
.file_name()
|
||||
.map(|name| name.to_string_lossy().starts_with(prefix))
|
||||
.unwrap_or(false)
|
||||
})
|
||||
.collect();
|
||||
entries.sort();
|
||||
entries.pop()
|
||||
}
|
||||
|
||||
fn latest_backup(dir: &Path) -> Option<PathBuf> {
|
||||
let mut entries: Vec<PathBuf> = fs::read_dir(dir)
|
||||
.ok()?
|
||||
@@ -604,8 +788,9 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
|
||||
alkane-cli afw preflight <file> --host <ip> [ssh options] [--held-backup <p1.img>]\n \
|
||||
alkane-cli afw install <file> --host <ip> --dry-run [ssh options]\n \
|
||||
alkane-cli afw install <file> --host <ip> --confirm FLASH [ssh options]\n \
|
||||
alkane-cli afw verify-live [<file>] --host <ip> [--timeout <secs>] [ssh options]\n \
|
||||
alkane-cli afw restore --host <ip> --held-backup <p1.img> --confirm FLASH [ssh options]\n\
|
||||
alkane-cli afw verify-live [<file>] --host <ip> [--board <board>] [--timeout <secs>] [ssh options]\n \
|
||||
alkane-cli afw restore --host <ip> --held-backup <p1.img> --confirm FLASH [ssh options]\n \
|
||||
alkane-cli afw restore --board am335x --host <ip> --held-backup <mtd8.img> \\\n --held-kernel <mtd7.img> --held-dtb <mtd6.img> --confirm FLASH [ssh options]\n\
|
||||
\nssh options: --user --port --identity-file --password-file --known-hosts \
|
||||
--root-method auto|direct|update-daemon|rroot --update-daemon-port\n\
|
||||
credentials: ALKANE_BOARD_PASSWORD or ALKANE_BOARD_PASSWORD_FILE; \
|
||||
|
||||
Reference in New Issue
Block a user