1 use std
::collections
::HashSet
;
3 use std
::time
::SystemTime
;
5 use anyhow
::{bail, Error}
;
7 use proxmox
::tools
::Uuid
;
16 MAX_CHUNK_ARCHIVE_SIZE
,
22 SnapshotChunkIterator
,
27 tape_write_snapshot_archive
,
28 request_and_load_media
,
29 tape_alert_flags_critical
,
31 file_formats
::MediaSetLabel
,
33 config
::tape_encryption_keys
::load_key_configs
,
37 struct PoolWriterState
{
38 drive
: Box
<dyn TapeDriver
>,
39 catalog
: MediaCatalog
,
40 // tell if we already moved to EOM
42 // bytes written after the last tape fush/sync
46 impl PoolWriterState
{
48 fn commit(&mut self) -> Result
<(), Error
> {
49 self.drive
.sync()?
; // sync all data to the tape
50 self.catalog
.commit()?
; // then commit the catalog
51 self.bytes_written
= 0;
56 /// Helper to manage a backup job, writing several tapes of a pool
57 pub struct PoolWriter
{
60 status
: Option
<PoolWriterState
>,
61 media_set_catalog
: MediaSetCatalog
,
66 pub fn new(mut pool
: MediaPool
, drive_name
: &str) -> Result
<Self, Error
> {
68 let current_time
= proxmox
::tools
::time
::epoch_i64();
70 pool
.start_write_session(current_time
)?
;
72 let mut media_set_catalog
= MediaSetCatalog
::new();
74 // load all catalogs read-only at start
75 for media_uuid
in pool
.current_media_list()?
{
76 let media_catalog
= MediaCatalog
::open(
77 Path
::new(TAPE_STATUS_DIR
),
82 media_set_catalog
.append_catalog(media_catalog
)?
;
87 drive_name
: drive_name
.to_string(),
93 pub fn pool(&mut self) -> &mut MediaPool
{
97 /// Set media status to FULL (persistent - stores pool status)
98 pub fn set_media_status_full(&mut self, uuid
: &Uuid
) -> Result
<(), Error
> {
99 self.pool
.set_media_status_full(&uuid
)?
;
103 pub fn contains_snapshot(&self, snapshot
: &str) -> bool
{
104 if let Some(PoolWriterState { ref catalog, .. }
) = self.status
{
105 if catalog
.contains_snapshot(snapshot
) {
109 self.media_set_catalog
.contains_snapshot(snapshot
)
112 /// Eject media and drop PoolWriterState (close drive)
113 pub fn eject_media(&mut self, worker
: &WorkerTask
) -> Result
<(), Error
> {
114 let mut status
= match self.status
.take() {
115 Some(status
) => status
,
116 None
=> return Ok(()), // no media loaded
119 let (drive_config
, _digest
) = crate::config
::drive
::config()?
;
121 if let Some((mut changer
, _
)) = media_changer(&drive_config
, &self.drive_name
)?
{
122 worker
.log("eject media");
123 status
.drive
.eject_media()?
; // rewind and eject early, so that unload_media is faster
124 drop(status
); // close drive
125 worker
.log("unload media");
126 changer
.unload_media(None
)?
; //eject and unload
128 worker
.log("standalone drive - ejecting media");
129 status
.drive
.eject_media()?
;
135 /// Export current media set and drop PoolWriterState (close drive)
136 pub fn export_media_set(&mut self, worker
: &WorkerTask
) -> Result
<(), Error
> {
137 let mut status
= self.status
.take();
139 let (drive_config
, _digest
) = crate::config
::drive
::config()?
;
141 if let Some((mut changer
, _
)) = media_changer(&drive_config
, &self.drive_name
)?
{
143 if let Some(ref mut status
) = status
{
144 worker
.log("eject media");
145 status
.drive
.eject_media()?
; // rewind and eject early, so that unload_media is faster
147 drop(status
); // close drive
149 worker
.log("unload media");
150 changer
.unload_media(None
)?
;
152 for media_uuid
in self.pool
.current_media_list()?
{
153 let media
= self.pool
.lookup_media(media_uuid
)?
;
154 let label_text
= media
.label_text();
155 if let Some(slot
) = changer
.export_media(label_text
)?
{
156 worker
.log(format
!("exported media '{}' to import/export slot {}", label_text
, slot
));
158 worker
.warn(format
!("export failed - media '{}' is not online", label_text
));
162 } else if let Some(mut status
) = status
{
163 worker
.log("standalone drive - ejecting media instead of export");
164 status
.drive
.eject_media()?
;
170 /// commit changes to tape and catalog
172 /// This is done automatically during a backupsession, but needs to
173 /// be called explicitly before dropping the PoolWriter
174 pub fn commit(&mut self) -> Result
<(), Error
> {
175 if let Some(ref mut status
) = self.status
{
181 /// Load a writable media into the drive
182 pub fn load_writable_media(&mut self, worker
: &WorkerTask
) -> Result
<Uuid
, Error
> {
183 let last_media_uuid
= match self.status
{
184 Some(PoolWriterState { ref catalog, .. }
) => Some(catalog
.uuid().clone()),
188 let current_time
= proxmox
::tools
::time
::epoch_i64();
189 let media_uuid
= self.pool
.alloc_writable_media(current_time
)?
;
191 let media
= self.pool
.lookup_media(&media_uuid
).unwrap();
193 let media_changed
= match last_media_uuid
{
194 Some(ref last_media_uuid
) => last_media_uuid
!= &media_uuid
,
199 return Ok(media_uuid
);
202 // remove read-only catalog (we store a writable version in status)
203 self.media_set_catalog
.remove_catalog(&media_uuid
);
205 if let Some(PoolWriterState {mut drive, catalog, .. }
) = self.status
.take() {
206 self.media_set_catalog
.append_catalog(catalog
)?
;
207 drive
.eject_media()?
;
210 let (drive_config
, _digest
) = crate::config
::drive
::config()?
;
212 let (mut drive
, old_media_id
) =
213 request_and_load_media(worker
, &drive_config
, &self.drive_name
, media
.label())?
;
215 // test for critical tape alert flags
216 if let Ok(alert_flags
) = drive
.tape_alert_flags() {
217 if !alert_flags
.is_empty() {
218 worker
.log(format
!("TapeAlertFlags: {:?}", alert_flags
));
219 if tape_alert_flags_critical(alert_flags
) {
220 bail
!("aborting due to critical tape alert flags: {:?}", alert_flags
);
225 let catalog
= update_media_set_label(
228 old_media_id
.media_set_label
,
232 let encrypt_fingerprint
= media
236 .encryption_key_fingerprint
239 drive
.set_encryption(encrypt_fingerprint
)?
;
241 self.status
= Some(PoolWriterState { drive, catalog, at_eom: false, bytes_written: 0 }
);
246 /// uuid of currently loaded BackupMedia
247 pub fn current_media_uuid(&self) -> Result
<&Uuid
, Error
> {
249 Some(PoolWriterState { ref catalog, ..}
) => Ok(catalog
.uuid()),
250 None
=> bail
!("PoolWriter - no media loaded"),
254 /// Move to EOM (if not aleady there), then creates a new snapshot
255 /// archive writing specified files (as .pxar) into it. On
256 /// success, this return 'Ok(true)' and the media catalog gets
259 /// Please note that this may fail when there is not enough space
260 /// on the media (return value 'Ok(false, _)'). In that case, the
261 /// archive is marked incomplete, and we do not use it. The caller
262 /// should mark the media as full and try again using another
264 pub fn append_snapshot_archive(
267 snapshot_reader
: &SnapshotReader
,
268 ) -> Result
<(bool
, usize), Error
> {
270 let status
= match self.status
{
271 Some(ref mut status
) => status
,
272 None
=> bail
!("PoolWriter - no media loaded"),
276 worker
.log(String
::from("moving to end of media"));
277 status
.drive
.move_to_eom()?
;
278 status
.at_eom
= true;
281 let current_file_number
= status
.drive
.current_file_number()?
;
282 if current_file_number
< 2 {
283 bail
!("got strange file position number from drive ({})", current_file_number
);
286 let (done
, bytes_written
) = {
287 let mut writer
: Box
<dyn TapeWrite
> = status
.drive
.write_file()?
;
289 match tape_write_snapshot_archive(writer
.as_mut(), snapshot_reader
)?
{
290 Some(content_uuid
) => {
291 status
.catalog
.register_snapshot(
294 &snapshot_reader
.snapshot().to_string(),
296 (true, writer
.bytes_written())
298 None
=> (false, writer
.bytes_written()),
302 status
.bytes_written
+= bytes_written
;
304 let request_sync
= status
.bytes_written
>= COMMIT_BLOCK_SIZE
;
306 if !done
|| request_sync
{
310 Ok((done
, bytes_written
))
313 /// Move to EOM (if not aleady there), then creates a new chunk
314 /// archive and writes chunks from 'chunk_iter'. This stops when
315 /// it detect LEOM or when we reach max archive size
316 /// (4GB). Written chunks are registered in the media catalog.
317 pub fn append_chunk_archive(
320 datastore
: &DataStore
,
321 chunk_iter
: &mut std
::iter
::Peekable
<SnapshotChunkIterator
>,
322 ) -> Result
<(bool
, usize), Error
> {
324 let status
= match self.status
{
325 Some(ref mut status
) => status
,
326 None
=> bail
!("PoolWriter - no media loaded"),
330 worker
.log(String
::from("moving to end of media"));
331 status
.drive
.move_to_eom()?
;
332 status
.at_eom
= true;
335 let current_file_number
= status
.drive
.current_file_number()?
;
336 if current_file_number
< 2 {
337 bail
!("got strange file position number from drive ({})", current_file_number
);
339 let writer
= status
.drive
.write_file()?
;
341 let start_time
= SystemTime
::now();
343 let (saved_chunks
, content_uuid
, leom
, bytes_written
) = write_chunk_archive(
348 &self.media_set_catalog
,
350 MAX_CHUNK_ARCHIVE_SIZE
,
353 status
.bytes_written
+= bytes_written
;
355 let elapsed
= start_time
.elapsed()?
.as_secs_f64();
357 "wrote {:.2} MB ({} MB/s)",
358 bytes_written
as f64 / (1024.0*1024.0),
359 (bytes_written
as f64)/(1024.0*1024.0*elapsed
),
362 let request_sync
= status
.bytes_written
>= COMMIT_BLOCK_SIZE
;
364 // register chunks in media_catalog
365 status
.catalog
.start_chunk_archive(content_uuid
, current_file_number
)?
;
366 for digest
in saved_chunks
{
367 status
.catalog
.register_chunk(&digest
)?
;
369 status
.catalog
.end_chunk_archive()?
;
371 if leom
|| request_sync
{
375 Ok((leom
, bytes_written
))
379 /// write up to <max_size> of chunks
380 fn write_chunk_archive
<'a
>(
382 writer
: Box
<dyn 'a
+ TapeWrite
>,
383 datastore
: &DataStore
,
384 chunk_iter
: &mut std
::iter
::Peekable
<SnapshotChunkIterator
>,
385 media_set_catalog
: &MediaSetCatalog
,
386 media_catalog
: &MediaCatalog
,
388 ) -> Result
<(Vec
<[u8;32]>, Uuid
, bool
, usize), Error
> {
390 let (mut writer
, content_uuid
) = ChunkArchiveWriter
::new(writer
, true)?
;
392 let mut chunk_index
: HashSet
<[u8;32]> = HashSet
::new();
394 // we want to get the chunk list in correct order
395 let mut chunk_list
: Vec
<[u8;32]> = Vec
::new();
397 let mut leom
= false;
400 let digest
= match chunk_iter
.next() {
402 Some(digest
) => digest?
,
404 if media_catalog
.contains_chunk(&digest
)
405 || chunk_index
.contains(&digest
)
406 || media_set_catalog
.contains_chunk(&digest
)
411 let blob
= datastore
.load_chunk(&digest
)?
;
412 //println!("CHUNK {} size {}", proxmox::tools::digest_to_hex(&digest), blob.raw_size());
414 match writer
.try_write_chunk(&digest
, &blob
) {
416 chunk_index
.insert(digest
);
417 chunk_list
.push(digest
);
423 Err(err
) => bail
!("write chunk failed - {}", err
),
426 if writer
.bytes_written() > max_size
{
427 worker
.log("Chunk Archive max size reached, closing archive".to_string());
434 Ok((chunk_list
, content_uuid
, leom
, writer
.bytes_written()))
437 // Compare the media set label. If the media is empty, or the existing
438 // set label does not match the expected media set, overwrite the
440 fn update_media_set_label(
442 drive
: &mut dyn TapeDriver
,
443 old_set
: Option
<MediaSetLabel
>,
445 ) -> Result
<MediaCatalog
, Error
> {
449 let new_set
= match media_id
.media_set_label
{
450 None
=> bail
!("got media without media set - internal error"),
451 Some(ref set
) => set
,
454 let key_config
= if let Some(ref fingerprint
) = new_set
.encryption_key_fingerprint
{
455 let (config_map
, _digest
) = load_key_configs()?
;
456 match config_map
.get(fingerprint
) {
458 // fixme: store item.hint??? should be in key-config instead
459 Some(item
.key_config
.clone())
462 bail
!("unable to find tape encryption key config '{}'", fingerprint
);
469 let status_path
= Path
::new(TAPE_STATUS_DIR
);
473 worker
.log("wrinting new media set label".to_string());
474 drive
.write_media_set_label(new_set
, key_config
.as_ref())?
;
475 media_catalog
= MediaCatalog
::overwrite(status_path
, media_id
, false)?
;
477 Some(media_set_label
) => {
478 if new_set
.uuid
== media_set_label
.uuid
{
479 if new_set
.seq_nr
!= media_set_label
.seq_nr
{
480 bail
!("got media with wrong media sequence number ({} != {}",
481 new_set
.seq_nr
,media_set_label
.seq_nr
);
483 if new_set
.encryption_key_fingerprint
!= media_set_label
.encryption_key_fingerprint
{
484 bail
!("detected changed encryption fingerprint - internal error");
486 media_catalog
= MediaCatalog
::open(status_path
, &media_id
.label
.uuid
, true, false)?
;
489 format
!("wrinting new media set label (overwrite '{}/{}')",
490 media_set_label
.uuid
.to_string(), media_set_label
.seq_nr
)
493 drive
.write_media_set_label(new_set
, key_config
.as_ref())?
;
494 media_catalog
= MediaCatalog
::overwrite(status_path
, media_id
, false)?
;
499 // todo: verify last content/media_catalog somehow?
500 drive
.move_to_eom()?
; // just to be sure