1 use std
::collections
::HashSet
;
4 use anyhow
::{bail, Error}
;
6 use proxmox
::tools
::Uuid
;
15 MAX_CHUNK_ARCHIVE_SIZE
,
21 SnapshotChunkIterator
,
26 tape_write_snapshot_archive
,
27 request_and_load_media
,
28 file_formats
::MediaSetLabel
,
33 struct PoolWriterState
{
34 drive
: Box
<dyn TapeDriver
>,
35 catalog
: MediaCatalog
,
36 // tell if we already moved to EOM
38 // bytes written after the last tape fush/sync
42 impl PoolWriterState
{
44 fn commit(&mut self) -> Result
<(), Error
> {
45 self.drive
.sync()?
; // sync all data to the tape
46 self.catalog
.commit()?
; // then commit the catalog
47 self.bytes_written
= 0;
52 /// Helper to manage a backup job, writing several tapes of a pool
53 pub struct PoolWriter
{
56 status
: Option
<PoolWriterState
>,
57 media_set_catalog
: MediaSetCatalog
,
62 pub fn new(mut pool
: MediaPool
, drive_name
: &str) -> Result
<Self, Error
> {
64 let current_time
= proxmox
::tools
::time
::epoch_i64();
66 pool
.start_write_session(current_time
)?
;
68 let mut media_set_catalog
= MediaSetCatalog
::new();
70 // load all catalogs read-only at start
71 for media_uuid
in pool
.current_media_list()?
{
72 let media_catalog
= MediaCatalog
::open(
73 Path
::new(TAPE_STATUS_DIR
),
78 media_set_catalog
.append_catalog(media_catalog
)?
;
83 drive_name
: drive_name
.to_string(),
89 pub fn pool(&mut self) -> &mut MediaPool
{
93 /// Set media status to FULL (persistent - stores pool status)
94 pub fn set_media_status_full(&mut self, uuid
: &Uuid
) -> Result
<(), Error
> {
95 self.pool
.set_media_status_full(&uuid
)?
;
99 pub fn contains_snapshot(&self, snapshot
: &str) -> bool
{
100 if let Some(PoolWriterState { ref catalog, .. }
) = self.status
{
101 if catalog
.contains_snapshot(snapshot
) {
105 self.media_set_catalog
.contains_snapshot(snapshot
)
108 /// commit changes to tape and catalog
110 /// This is done automatically during a backupsession, but needs to
111 /// be called explicitly before dropping the PoolWriter
112 pub fn commit(&mut self) -> Result
<(), Error
> {
113 if let Some(ref mut status
) = self.status
{
119 /// Load a writable media into the drive
120 pub fn load_writable_media(&mut self, worker
: &WorkerTask
) -> Result
<Uuid
, Error
> {
121 let last_media_uuid
= match self.status
{
122 Some(PoolWriterState { ref catalog, .. }
) => Some(catalog
.uuid().clone()),
126 let current_time
= proxmox
::tools
::time
::epoch_i64();
127 let media_uuid
= self.pool
.alloc_writable_media(current_time
)?
;
129 let media
= self.pool
.lookup_media(&media_uuid
).unwrap();
131 let media_changed
= match last_media_uuid
{
132 Some(ref last_media_uuid
) => last_media_uuid
!= &media_uuid
,
137 return Ok(media_uuid
);
140 // remove read-only catalog (we store a writable version in status)
141 self.media_set_catalog
.remove_catalog(&media_uuid
);
143 if let Some(PoolWriterState {mut drive, catalog, .. }
) = self.status
.take() {
144 self.media_set_catalog
.append_catalog(catalog
)?
;
145 drive
.eject_media()?
;
148 let (drive_config
, _digest
) = crate::config
::drive
::config()?
;
150 let (mut drive
, old_media_id
) =
151 request_and_load_media(worker
, &drive_config
, &self.drive_name
, media
.label())?
;
153 let catalog
= update_media_set_label(
156 old_media_id
.media_set_label
,
160 self.status
= Some(PoolWriterState { drive, catalog, at_eom: false, bytes_written: 0 }
);
165 /// uuid of currently loaded BackupMedia
166 pub fn current_media_uuid(&self) -> Result
<&Uuid
, Error
> {
168 Some(PoolWriterState { ref catalog, ..}
) => Ok(catalog
.uuid()),
169 None
=> bail
!("PoolWriter - no media loaded"),
173 /// Move to EOM (if not aleady there), then creates a new snapshot
174 /// archive writing specified files (as .pxar) into it. On
175 /// success, this return 'Ok(true)' and the media catalog gets
178 /// Please note that this may fail when there is not enough space
179 /// on the media (return value 'Ok(false, _)'). In that case, the
180 /// archive is marked incomplete, and we do not use it. The caller
181 /// should mark the media as full and try again using another
183 pub fn append_snapshot_archive(
185 snapshot_reader
: &SnapshotReader
,
186 ) -> Result
<(bool
, usize), Error
> {
188 let status
= match self.status
{
189 Some(ref mut status
) => status
,
190 None
=> bail
!("PoolWriter - no media loaded"),
194 status
.drive
.move_to_eom()?
;
195 status
.at_eom
= true;
198 let current_file_number
= status
.drive
.current_file_number()?
;
199 if current_file_number
< 2 {
200 bail
!("got strange file position number from drive ({})", current_file_number
);
203 let (done
, bytes_written
) = {
204 let mut writer
: Box
<dyn TapeWrite
> = status
.drive
.write_file()?
;
206 match tape_write_snapshot_archive(writer
.as_mut(), snapshot_reader
)?
{
207 Some(content_uuid
) => {
208 status
.catalog
.register_snapshot(
211 &snapshot_reader
.snapshot().to_string(),
213 (true, writer
.bytes_written())
215 None
=> (false, writer
.bytes_written()),
219 status
.bytes_written
+= bytes_written
;
221 let request_sync
= if status
.bytes_written
>= COMMIT_BLOCK_SIZE { true }
else { false }
;
223 if !done
|| request_sync
{
227 Ok((done
, bytes_written
))
230 /// Move to EOM (if not aleady there), then creates a new chunk
231 /// archive and writes chunks from 'chunk_iter'. This stops when
232 /// it detect LEOM or when we reach max archive size
233 /// (4GB). Written chunks are registered in the media catalog.
234 pub fn append_chunk_archive(
236 datastore
: &DataStore
,
237 chunk_iter
: &mut std
::iter
::Peekable
<SnapshotChunkIterator
>,
238 ) -> Result
<(bool
, usize), Error
> {
240 let status
= match self.status
{
241 Some(ref mut status
) => status
,
242 None
=> bail
!("PoolWriter - no media loaded"),
246 status
.drive
.move_to_eom()?
;
247 status
.at_eom
= true;
250 let current_file_number
= status
.drive
.current_file_number()?
;
251 if current_file_number
< 2 {
252 bail
!("got strange file position number from drive ({})", current_file_number
);
254 let writer
= status
.drive
.write_file()?
;
256 let (saved_chunks
, content_uuid
, leom
, bytes_written
) = write_chunk_archive(
260 &self.media_set_catalog
,
262 MAX_CHUNK_ARCHIVE_SIZE
,
265 status
.bytes_written
+= bytes_written
;
267 let request_sync
= if status
.bytes_written
>= COMMIT_BLOCK_SIZE { true }
else { false }
;
269 // register chunks in media_catalog
270 status
.catalog
.start_chunk_archive(content_uuid
, current_file_number
)?
;
271 for digest
in saved_chunks
{
272 status
.catalog
.register_chunk(&digest
)?
;
274 status
.catalog
.end_chunk_archive()?
;
276 if leom
|| request_sync
{
280 Ok((leom
, bytes_written
))
284 /// write up to <max_size> of chunks
285 fn write_chunk_archive
<'a
>(
286 writer
: Box
<dyn 'a
+ TapeWrite
>,
287 datastore
: &DataStore
,
288 chunk_iter
: &mut std
::iter
::Peekable
<SnapshotChunkIterator
>,
289 media_set_catalog
: &MediaSetCatalog
,
290 media_catalog
: &MediaCatalog
,
292 ) -> Result
<(Vec
<[u8;32]>, Uuid
, bool
, usize), Error
> {
294 let (mut writer
, content_uuid
) = ChunkArchiveWriter
::new(writer
, true)?
;
296 let mut chunk_index
: HashSet
<[u8;32]> = HashSet
::new();
298 // we want to get the chunk list in correct order
299 let mut chunk_list
: Vec
<[u8;32]> = Vec
::new();
301 let mut leom
= false;
304 let digest
= match chunk_iter
.next() {
306 Some(digest
) => digest?
,
308 if media_catalog
.contains_chunk(&digest
)
309 || chunk_index
.contains(&digest
)
310 || media_set_catalog
.contains_chunk(&digest
)
315 let blob
= datastore
.load_chunk(&digest
)?
;
316 println
!("CHUNK {} size {}", proxmox
::tools
::digest_to_hex(&digest
), blob
.raw_size());
318 match writer
.try_write_chunk(&digest
, &blob
) {
320 chunk_index
.insert(digest
);
321 chunk_list
.push(digest
);
327 Err(err
) => bail
!("write chunk failed - {}", err
),
330 if writer
.bytes_written() > max_size
{
331 println
!("Chunk Archive max size reached, closing archive");
338 Ok((chunk_list
, content_uuid
, leom
, writer
.bytes_written()))
341 // Compare the media set label. If the media is empty, or the existing
342 // set label does not match the expected media set, overwrite the
344 fn update_media_set_label(
346 drive
: &mut dyn TapeDriver
,
347 old_set
: Option
<MediaSetLabel
>,
349 ) -> Result
<MediaCatalog
, Error
> {
353 let new_set
= match media_id
.media_set_label
{
354 None
=> bail
!("got media without media set - internal error"),
355 Some(ref set
) => set
,
358 let status_path
= Path
::new(TAPE_STATUS_DIR
);
362 worker
.log(format
!("wrinting new media set label"));
363 drive
.write_media_set_label(new_set
)?
;
364 media_catalog
= MediaCatalog
::overwrite(status_path
, media_id
, true)?
;
366 Some(media_set_label
) => {
367 if new_set
.uuid
== media_set_label
.uuid
{
368 if new_set
.seq_nr
!= media_set_label
.seq_nr
{
369 bail
!("got media with wrong media sequence number ({} != {}",
370 new_set
.seq_nr
,media_set_label
.seq_nr
);
372 media_catalog
= MediaCatalog
::open(status_path
, &media_id
.label
.uuid
, true, false)?
;
375 format
!("wrinting new media set label (overwrite '{}/{}')",
376 media_set_label
.uuid
.to_string(), media_set_label
.seq_nr
)
379 drive
.write_media_set_label(new_set
)?
;
380 media_catalog
= MediaCatalog
::overwrite(status_path
, media_id
, true)?
;
385 // todo: verify last content/media_catalog somehow?
386 drive
.move_to_eom()?
;