[pbs-devel] [PATCH proxmox-backup 4/5] tape/drive: improve error and email handling for requesting a tape load

Dominik Csapak d.csapak at proxmox.com
Wed Mar 3 15:00:52 CET 2021


Try once first to load the correct tape before sending an email to
insert the correct one. This way, the admin does not get a mail
if the correct tape is already inserted.

Also include the error we got that prompted the email to insert the
tape. This means that if the admin gets prompted to insert e.g.
"FOO" but inserts "BAR", he'll get an email that the wrong
tape is inserted.

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 src/tape/changer/email.rs |  5 +++++
 src/tape/drive/mod.rs     | 43 ++++++++++++++++++++++++---------------
 2 files changed, 32 insertions(+), 16 deletions(-)

diff --git a/src/tape/changer/email.rs b/src/tape/changer/email.rs
index 9be06196..abd7ead8 100644
--- a/src/tape/changer/email.rs
+++ b/src/tape/changer/email.rs
@@ -7,12 +7,17 @@ pub fn send_load_media_email(
     drive: &str,
     label_text: &str,
     to: &str,
+    reason: Option<String>,
 ) -> Result<(), Error> {
 
     let subject = format!("Load Media '{}' request for drive '{}'", label_text, drive);
 
     let mut text = String::new();
 
+    if let Some(reason) = reason {
+        text.push_str(&format!("The drive has the wrong or no tape inserted. Error:\n{}\n\n", reason));
+    }
+
     text.push_str("Please insert the requested media into the backup drive.\n\n");
 
     text.push_str(&format!("Drive: {}\n", drive));
diff --git a/src/tape/drive/mod.rs b/src/tape/drive/mod.rs
index ba63f4d4..8bbca98c 100644
--- a/src/tape/drive/mod.rs
+++ b/src/tape/drive/mod.rs
@@ -375,18 +375,34 @@ pub fn request_and_load_media(
                         return Ok((handle, media_id));
                     }
 
-                    task_log!(worker, "Please insert media '{}' into drive '{}'", label_text, drive);
 
                     let to = "root at localhost"; // fixme
 
-                    send_load_media_email(drive, &label_text, to)?;
-
                     let mut last_media_uuid = None;
                     let mut last_error = None;
 
+                    let mut tried = false;
+                    let mut failure_reason = None;
+
                     loop {
                         worker.check_abort()?;
 
+                        if tried {
+                            if let Some(reason) = failure_reason {
+                                task_log!(worker, "Please insert media '{}' into drive '{}'", label_text, drive);
+                                send_load_media_email(drive, &label_text, to, Some(reason))?;
+                            }
+
+                            failure_reason = None;
+
+                            for _ in 0..50 { // delay 5 seconds
+                                worker.check_abort()?;
+                                std::thread::sleep(std::time::Duration::from_millis(100));
+                            }
+                        }
+
+                        tried = true;
+
                         let mut handle = match drive_config.open() {
                             Ok(handle) => handle,
                             Err(err) => {
@@ -394,10 +410,7 @@ pub fn request_and_load_media(
                                 if Some(err.clone()) != last_error {
                                     task_log!(worker, "tape open failed - {}", err);
                                     last_error = Some(err);
-                                }
-                                for _ in 0..50 { // delay 5 seconds
-                                    worker.check_abort()?;
-                                    std::thread::sleep(std::time::Duration::from_millis(100));
+                                    failure_reason = last_error.clone();
                                 }
                                 continue;
                             }
@@ -414,19 +427,22 @@ pub fn request_and_load_media(
                                     );
                                     return Ok((Box::new(handle), media_id));
                                 } else if Some(media_id.label.uuid.clone()) != last_media_uuid {
-                                    task_log!(
-                                        worker,
+                                    let err = format!(
                                         "wrong media label {} ({})",
                                         media_id.label.label_text,
                                         media_id.label.uuid.to_string(),
                                     );
+                                    task_log!(worker, "{}", err);
                                     last_media_uuid = Some(media_id.label.uuid);
+                                    failure_reason = Some(err);
                                 }
                             }
                             Ok((None, _)) => {
                                 if last_media_uuid.is_some() {
-                                    task_log!(worker, "found empty media without label (please label all tapes first)");
+                                    let err = "found empty media without label (please label all tapes first)";
+                                    task_log!(worker, "{}", err);
                                     last_media_uuid = None;
+                                    failure_reason = Some(err.to_string());
                                 }
                             }
                             Err(err) => {
@@ -434,15 +450,10 @@ pub fn request_and_load_media(
                                 if Some(err.clone()) != last_error {
                                     task_log!(worker, "tape open failed - {}", err);
                                     last_error = Some(err);
+                                    failure_reason = last_error.clone();
                                 }
                             }
                         }
-
-                        // eprintln!("read label failed -  test again in 5 secs");
-                        for _ in 0..50 { // delay 5 seconds
-                            worker.check_abort()?;
-                            std::thread::sleep(std::time::Duration::from_millis(100));
-                        }
                     }
                 }
                 _ => bail!("drive type '{}' not implemented!"),
-- 
2.20.1






More information about the pbs-devel mailing list