From 1a76b7800f4a70f1f229661db934ddf991e31d12 Mon Sep 17 00:00:00 2001 From: Aiden McClelland Date: Fri, 8 Oct 2021 17:58:44 -0600 Subject: [PATCH] write directly to blockdev; swap boot label --- appmgr/src/update/mod.rs | 68 ++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/appmgr/src/update/mod.rs b/appmgr/src/update/mod.rs index a71faaba0..2a20afafb 100644 --- a/appmgr/src/update/mod.rs +++ b/appmgr/src/update/mod.rs @@ -15,6 +15,7 @@ use rpc_toolkit::command; use sha2::Sha256; use tokio::io::AsyncWriteExt; use tokio::pin; +use tokio::process::Command; use tokio_stream::StreamExt; use crate::context::RpcContext; @@ -111,7 +112,7 @@ impl MountedResource { } async fn unmount(value: X) -> Result<(), Error> { let folder = value.mount_folder(); - tokio::process::Command::new("umount") + Command::new("umount") .arg(&folder) .invoke(crate::ErrorKind::Filesystem) .await?; @@ -191,7 +192,6 @@ async fn maybe_do_update(ctx: RpcContext) -> Result>, Error base: info.eos_marketplace.clone(), version: latest_version, }, - ctx.datadir.join("updates/eos.img"), new_label, ) .await?; @@ -204,19 +204,19 @@ async fn maybe_do_update(ctx: RpcContext) -> Result>, Error let rev = tx.commit(None).await?; tokio::spawn(async move { - match do_update(download, new_label, mounted_boot).await { + let res = do_update(download, new_label, mounted_boot).await; + let mut db = ctx.db.handle(); + let mut info = crate::db::DatabaseModel::new() + .server_info() + .get_mut(&mut db) + .await + .expect("could not access status"); + info.status = ServerStatus::Running; + info.update_progress = None; + info.save(&mut db).await.expect("could not save status"); + match res { Ok(()) => todo!("issue notification"), Err(e) => { - let mut db = ctx.db.handle(); - let mut info = crate::db::DatabaseModel::new() - .server_info() - .get_mut(&mut db) - .await - .expect("could not access status"); - info.status = ServerStatus::Running; - info.update_progress = None; - info.save(&mut db).await.expect("could not save status"); - todo!("{}, issue notification", e) } } @@ -278,7 +278,6 @@ impl std::fmt::Display for EosUrl { async fn download_file<'a>( eos_url: &EosUrl, - tmp_img_path: impl AsRef + 'a, new_label: NewLabel, ) -> Result<(Option, impl Future> + 'a), Error> { let download_request = reqwest::get(eos_url.to_string()) @@ -300,7 +299,7 @@ async fn download_file<'a>( // .with_kind(ErrorKind::InvalidRequest)? // .to_owned(); let stream_download = download_request.bytes_stream(); - let file_sum = write_stream_to_label(stream_download, new_label, tmp_img_path).await?; + let file_sum = write_stream_to_label(stream_download, new_label).await?; check_download(&hash_from_header, file_sum).await?; Ok(()) }, @@ -310,11 +309,11 @@ async fn download_file<'a>( async fn write_stream_to_label( stream_download: impl Stream>, file: NewLabel, - temp_img_path: impl AsRef, ) -> Result, Error> { let block_dev = file.0.block_dev(); - let file_path = temp_img_path.as_ref(); - let mut file = tokio::fs::File::create(&file_path) + let mut file = tokio::fs::OpenOptions::new() + .write(true) + .open(&block_dev) .await .with_kind(ErrorKind::Filesystem)?; let mut hasher = Sha256::new(); @@ -328,11 +327,6 @@ async fn write_stream_to_label( file.flush().await.with_kind(ErrorKind::Filesystem)?; file.shutdown().await.with_kind(ErrorKind::Filesystem)?; drop(file); - tokio::process::Command::new("dd") - .arg(format!("if={}", file_path.display())) - .arg(format!("of={}", block_dev.display())) - .output() - .await?; Ok(hasher.finalize().to_vec()) } @@ -349,13 +343,25 @@ async fn swap_boot_label( new_label: NewLabel, mounted_boot: &MountedResource, ) -> Result<(), Error> { - // disk/util add setLabel - tokio::process::Command::new("sed") - .arg(format!( - r#""r/LABEL=(blue|green)/LABEL={}/g""#, - new_label.0.label() - )) - .arg(mounted_boot.value.mount_folder().join("etc/fstab")) + let block_dev = new_label.0.block_dev(); + Command::new("e2label") + .arg(block_dev) + .arg(new_label.0.label()) + .invoke(crate::ErrorKind::BlockDevice) + .await?; + let mut mounted = mount_label(new_label.0).await?; + let sedcmd = format!("s/LABEL=\\(blue\\|green\\)/LABEL={}/g", new_label.0.label()); + Command::new("sed") + .arg("-i") + .arg(&sedcmd) + .arg(mounted.value.mount_folder().join("etc/fstab")) + .output() + .await?; + mounted.unmount_label().await?; + Command::new("sed") + .arg("-i") + .arg(&sedcmd) + .arg(mounted_boot.value.mount_folder().join("cmdline.txt")) .output() .await?; Ok(()) @@ -370,7 +376,7 @@ where tokio::fs::create_dir_all(&folder) .await .with_ctx(|_| (crate::ErrorKind::Filesystem, folder.display().to_string()))?; - tokio::process::Command::new("mount") + Command::new("mount") .arg("-L") .arg(label) .arg(folder)