4 use anyhow
::{bail, Error}
;
10 list_subdirs_api_method
,
25 drive
::check_drive_exists
,
27 backup
::decrypt_key_config
,
33 MEDIA_POOL_NAME_SCHEMA
,
40 LinuxDriveAndMediaStatus
,
42 tape
::restore
::restore_media
,
52 linux_tape_device_list
,
55 required_media_changer
,
56 update_changer_online_status
,
59 open_linux_tape_device
,
72 schema
: DRIVE_NAME_SCHEMA
,
75 schema
: MEDIA_LABEL_SCHEMA
,
80 /// Load media with specified label
82 /// Issue a media load request to the associated changer device.
83 pub async
fn load_media(drive
: String
, label_text
: String
) -> Result
<(), Error
> {
85 let (config
, _digest
) = config
::drive
::config()?
;
87 tokio
::task
::spawn_blocking(move || {
88 let (mut changer
, _
) = required_media_changer(&config
, &drive
)?
;
89 changer
.load_media(&label_text
)
97 schema
: DRIVE_NAME_SCHEMA
,
100 description
: "Source slot number.",
106 /// Load media from the specified slot
108 /// Issue a media load request to the associated changer device.
109 pub async
fn load_slot(drive
: String
, source_slot
: u64) -> Result
<(), Error
> {
111 let (config
, _digest
) = config
::drive
::config()?
;
113 tokio
::task
::spawn_blocking(move || {
114 let (mut changer
, _
) = required_media_changer(&config
, &drive
)?
;
115 changer
.load_media_from_slot(source_slot
)
123 schema
: DRIVE_NAME_SCHEMA
,
126 schema
: MEDIA_LABEL_SCHEMA
,
131 description
: "The import-export slot number the media was transfered to.",
136 /// Export media with specified label
137 pub async
fn export_media(drive
: String
, label_text
: String
) -> Result
<u64, Error
> {
139 let (config
, _digest
) = config
::drive
::config()?
;
141 tokio
::task
::spawn_blocking(move || {
142 let (mut changer
, changer_name
) = required_media_changer(&config
, &drive
)?
;
143 match changer
.export_media(&label_text
)?
{
144 Some(slot
) => Ok(slot
),
145 None
=> bail
!("media '{}' is not online (via changer '{}')", label_text
, changer_name
),
154 schema
: DRIVE_NAME_SCHEMA
,
157 description
: "Target slot number. If omitted, defaults to the slot that the drive was loaded from.",
164 /// Unload media via changer
167 target_slot
: Option
<u64>,
169 ) -> Result
<(), Error
> {
171 let (config
, _digest
) = config
::drive
::config()?
;
173 tokio
::task
::spawn_blocking(move || {
174 let (mut changer
, _
) = required_media_changer(&config
, &drive
)?
;
175 changer
.unload_media(target_slot
)
184 description
: "The list of autodetected tape drives.",
187 type: TapeDeviceInfo
,
192 pub fn scan_drives(_param
: Value
) -> Result
<Vec
<TapeDeviceInfo
>, Error
> {
194 let list
= linux_tape_device_list();
203 schema
: DRIVE_NAME_SCHEMA
,
206 description
: "Use fast erase.",
221 rpcenv
: &mut dyn RpcEnvironment
,
222 ) -> Result
<Value
, Error
> {
224 let (config
, _digest
) = config
::drive
::config()?
;
226 check_drive_exists(&config
, &drive
)?
; // early check before starting worker
228 let auth_id
: Authid
= rpcenv
.get_auth_id().unwrap().parse()?
;
230 let to_stdout
= rpcenv
.env_type() == RpcEnvironmentType
::CLI
;
232 let upid_str
= WorkerTask
::new_thread(
238 let mut drive
= open_drive(&config
, &drive
)?
;
239 drive
.erase_media(fast
.unwrap_or(true))?
;
251 schema
: DRIVE_NAME_SCHEMA
,
262 rpcenv
: &mut dyn RpcEnvironment
,
263 ) -> Result
<Value
, Error
> {
265 let (config
, _digest
) = config
::drive
::config()?
;
267 check_drive_exists(&config
, &drive
)?
; // early check before starting worker
269 let auth_id
: Authid
= rpcenv
.get_auth_id().unwrap().parse()?
;
271 let to_stdout
= rpcenv
.env_type() == RpcEnvironmentType
::CLI
;
273 let upid_str
= WorkerTask
::new_thread(
279 let mut drive
= open_drive(&config
, &drive
)?
;
292 schema
: DRIVE_NAME_SCHEMA
,
297 /// Eject/Unload drive media
298 pub async
fn eject_media(drive
: String
) -> Result
<(), Error
> {
300 let (config
, _digest
) = config
::drive
::config()?
;
302 tokio
::task
::spawn_blocking(move || {
303 if let Some((mut changer
, _
)) = media_changer(&config
, &drive
)?
{
304 changer
.unload_media(None
)?
;
306 let mut drive
= open_drive(&config
, &drive
)?
;
307 drive
.eject_media()?
;
317 schema
: DRIVE_NAME_SCHEMA
,
320 schema
: MEDIA_LABEL_SCHEMA
,
323 schema
: MEDIA_POOL_NAME_SCHEMA
,
334 /// Write a new media label to the media in 'drive'. The media is
335 /// assigned to the specified 'pool', or else to the free media pool.
337 /// Note: The media need to be empty (you may want to erase it first).
340 pool
: Option
<String
>,
342 rpcenv
: &mut dyn RpcEnvironment
,
343 ) -> Result
<Value
, Error
> {
345 let auth_id
: Authid
= rpcenv
.get_auth_id().unwrap().parse()?
;
347 if let Some(ref pool
) = pool
{
348 let (pool_config
, _digest
) = config
::media_pool
::config()?
;
350 if pool_config
.sections
.get(pool
).is_none() {
351 bail
!("no such pool ('{}')", pool
);
355 let (config
, _digest
) = config
::drive
::config()?
;
357 let to_stdout
= rpcenv
.env_type() == RpcEnvironmentType
::CLI
;
359 let upid_str
= WorkerTask
::new_thread(
366 let mut drive
= open_drive(&config
, &drive
)?
;
370 match drive
.read_next_file() {
371 Ok(Some(_file
)) => bail
!("media is not empty (erase first)"),
372 Ok(None
) => { /* EOF mark at BOT, assume tape is empty */ }
,
374 if err
.is_errno(nix
::errno
::Errno
::ENOSPC
) || err
.is_errno(nix
::errno
::Errno
::EIO
) {
375 /* assume tape is empty */
377 bail
!("media read error - {}", err
);
382 let ctime
= proxmox
::tools
::time
::epoch_i64();
383 let label
= MediaLabel
{
384 label_text
: label_text
.to_string(),
385 uuid
: Uuid
::generate(),
389 write_media_label(worker
, &mut drive
, label
, pool
)
396 fn write_media_label(
397 worker
: Arc
<WorkerTask
>,
398 drive
: &mut Box
<dyn TapeDriver
>,
400 pool
: Option
<String
>,
401 ) -> Result
<(), Error
> {
403 drive
.label_tape(&label
)?
;
405 let mut media_set_label
= None
;
407 if let Some(ref pool
) = pool
{
408 // assign media to pool by writing special media set label
409 worker
.log(format
!("Label media '{}' for pool '{}'", label
.label_text
, pool
));
410 let set
= MediaSetLabel
::with_data(&pool
, [0u8; 16].into(), 0, label
.ctime
, None
);
412 drive
.write_media_set_label(&set
, None
)?
;
413 media_set_label
= Some(set
);
415 worker
.log(format
!("Label media '{}' (no pool assignment)", label
.label_text
));
418 let media_id
= MediaId { label, media_set_label }
;
420 let status_path
= Path
::new(TAPE_STATUS_DIR
);
422 // Create the media catalog
423 MediaCatalog
::overwrite(status_path
, &media_id
, false)?
;
425 let mut inventory
= Inventory
::load(status_path
)?
;
426 inventory
.store(media_id
.clone(), false)?
;
430 match drive
.read_label() {
431 Ok((Some(info
), _
)) => {
432 if info
.label
.uuid
!= media_id
.label
.uuid
{
433 bail
!("verify label failed - got wrong label uuid");
435 if let Some(ref pool
) = pool
{
436 match info
.media_set_label
{
438 if set
.uuid
!= [0u8; 16].into() {
439 bail
!("verify media set label failed - got wrong set uuid");
441 if &set
.pool
!= pool
{
442 bail
!("verify media set label failed - got wrong pool");
446 bail
!("verify media set label failed (missing set label)");
451 Ok((None
, _
)) => bail
!("verify label failed (got empty media)"),
452 Err(err
) => bail
!("verify label failed - {}", err
),
465 schema
: DRIVE_NAME_SCHEMA
,
468 description
: "Encryption key password.",
473 /// Try to restore a tape encryption key
474 pub async
fn restore_key(
477 ) -> Result
<(), Error
> {
479 let (config
, _digest
) = config
::drive
::config()?
;
481 tokio
::task
::spawn_blocking(move || {
482 let mut drive
= open_drive(&config
, &drive
)?
;
484 let (_media_id
, key_config
) = drive
.read_label()?
;
486 if let Some(key_config
) = key_config
{
487 let hint
= String
::from("fixme: add hint");
488 // fixme: howto show restore hint
489 let password_fn
= || { Ok(password.as_bytes().to_vec()) }
;
490 let (key
, ..) = decrypt_key_config(&key_config
, &password_fn
)?
;
491 config
::tape_encryption_keys
::insert_key(key
, key_config
, hint
)?
;
493 bail
!("media does not contain any encryption key configuration");
504 schema
: DRIVE_NAME_SCHEMA
,
507 description
: "Inventorize media",
516 /// Read media label (optionally inventorize media)
517 pub async
fn read_label(
519 inventorize
: Option
<bool
>,
520 ) -> Result
<MediaIdFlat
, Error
> {
522 let (config
, _digest
) = config
::drive
::config()?
;
524 tokio
::task
::spawn_blocking(move || {
525 let mut drive
= open_drive(&config
, &drive
)?
;
527 let (media_id
, _key_config
) = drive
.read_label()?
;
529 let media_id
= match media_id
{
531 let mut flat
= MediaIdFlat
{
532 uuid
: media_id
.label
.uuid
.to_string(),
533 label_text
: media_id
.label
.label_text
.clone(),
534 ctime
: media_id
.label
.ctime
,
535 media_set_ctime
: None
,
536 media_set_uuid
: None
,
537 encryption_key_fingerprint
: None
,
541 if let Some(ref set
) = media_id
.media_set_label
{
542 flat
.pool
= Some(set
.pool
.clone());
543 flat
.seq_nr
= Some(set
.seq_nr
);
544 flat
.media_set_uuid
= Some(set
.uuid
.to_string());
545 flat
.media_set_ctime
= Some(set
.ctime
);
546 flat
.encryption_key_fingerprint
= set
547 .encryption_key_fingerprint
549 .map(|fp
| crate::tools
::format
::as_fingerprint(fp
.bytes()));
552 if let Some(true) = inventorize
{
553 let state_path
= Path
::new(TAPE_STATUS_DIR
);
554 let mut inventory
= Inventory
::load(state_path
)?
;
555 inventory
.store(media_id
, false)?
;
561 bail
!("Media is empty (no label).");
573 schema
: DRIVE_NAME_SCHEMA
,
584 rpcenv
: &mut dyn RpcEnvironment
,
585 ) -> Result
<Value
, Error
> {
587 let (config
, _digest
) = config
::drive
::config()?
;
589 check_drive_exists(&config
, &drive
)?
; // early check before starting worker
591 let auth_id
: Authid
= rpcenv
.get_auth_id().unwrap().parse()?
;
593 let to_stdout
= rpcenv
.env_type() == RpcEnvironmentType
::CLI
;
595 let upid_str
= WorkerTask
::new_thread(
602 let (mut changer
, _changer_name
) = required_media_changer(&config
, &drive
)?
;
604 worker
.log("Starting drive clean");
606 changer
.clean_drive()?
;
608 worker
.log("Drive cleaned sucessfully");
620 schema
: DRIVE_NAME_SCHEMA
,
625 description
: "The list of media labels with associated media Uuid (if any).",
632 /// List known media labels (Changer Inventory)
634 /// Note: Only useful for drives with associated changer device.
636 /// This method queries the changer to get a list of media labels.
638 /// Note: This updates the media online status.
639 pub async
fn inventory(
641 ) -> Result
<Vec
<LabelUuidMap
>, Error
> {
643 let (config
, _digest
) = config
::drive
::config()?
;
645 tokio
::task
::spawn_blocking(move || {
646 let (mut changer
, changer_name
) = required_media_changer(&config
, &drive
)?
;
648 let label_text_list
= changer
.online_media_label_texts()?
;
650 let state_path
= Path
::new(TAPE_STATUS_DIR
);
652 let mut inventory
= Inventory
::load(state_path
)?
;
654 update_changer_online_status(
661 let mut list
= Vec
::new();
663 for label_text
in label_text_list
.iter() {
664 if label_text
.starts_with("CLN") {
665 // skip cleaning unit
669 let label_text
= label_text
.to_string();
671 if let Some(media_id
) = inventory
.find_media_by_label_text(&label_text
) {
672 list
.push(LabelUuidMap { label_text, uuid: Some(media_id.label.uuid.to_string()) }
);
674 list
.push(LabelUuidMap { label_text, uuid: None }
);
686 schema
: DRIVE_NAME_SCHEMA
,
689 description
: "Load all tapes and try read labels (even if already inventoried)",
701 /// Note: Only useful for drives with associated changer device.
703 /// This method queries the changer to get a list of media labels. It
704 /// then loads any unknown media into the drive, reads the label, and
705 /// store the result to the media database.
707 /// Note: This updates the media online status.
708 pub fn update_inventory(
710 read_all_labels
: Option
<bool
>,
711 rpcenv
: &mut dyn RpcEnvironment
,
712 ) -> Result
<Value
, Error
> {
714 let (config
, _digest
) = config
::drive
::config()?
;
716 check_drive_exists(&config
, &drive
)?
; // early check before starting worker
718 let auth_id
: Authid
= rpcenv
.get_auth_id().unwrap().parse()?
;
720 let to_stdout
= rpcenv
.env_type() == RpcEnvironmentType
::CLI
;
722 let upid_str
= WorkerTask
::new_thread(
729 let (mut changer
, changer_name
) = required_media_changer(&config
, &drive
)?
;
731 let label_text_list
= changer
.online_media_label_texts()?
;
732 if label_text_list
.is_empty() {
733 worker
.log(format
!("changer device does not list any media labels"));
736 let state_path
= Path
::new(TAPE_STATUS_DIR
);
738 let mut inventory
= Inventory
::load(state_path
)?
;
740 update_changer_online_status(&config
, &mut inventory
, &changer_name
, &label_text_list
)?
;
742 for label_text
in label_text_list
.iter() {
743 if label_text
.starts_with("CLN") {
744 worker
.log(format
!("skip cleaning unit '{}'", label_text
));
748 let label_text
= label_text
.to_string();
750 if !read_all_labels
.unwrap_or(false) && inventory
.find_media_by_label_text(&label_text
).is_some() {
751 worker
.log(format
!("media '{}' already inventoried", label_text
));
755 if let Err(err
) = changer
.load_media(&label_text
) {
756 worker
.warn(format
!("unable to load media '{}' - {}", label_text
, err
));
760 let mut drive
= open_drive(&config
, &drive
)?
;
761 match drive
.read_label() {
763 worker
.warn(format
!("unable to read label form media '{}' - {}", label_text
, err
));
766 worker
.log(format
!("media '{}' is empty", label_text
));
768 Ok((Some(media_id
), _key_config
)) => {
769 if label_text
!= media_id
.label
.label_text
{
770 worker
.warn(format
!("label text missmatch ({} != {})", label_text
, media_id
.label
.label_text
));
773 worker
.log(format
!("inventorize media '{}' with uuid '{}'", label_text
, media_id
.label
.uuid
));
774 inventory
.store(media_id
, false)?
;
777 changer
.unload_media(None
)?
;
791 schema
: DRIVE_NAME_SCHEMA
,
794 schema
: MEDIA_POOL_NAME_SCHEMA
,
803 /// Label media with barcodes from changer device
804 pub fn barcode_label_media(
806 pool
: Option
<String
>,
807 rpcenv
: &mut dyn RpcEnvironment
,
808 ) -> Result
<Value
, Error
> {
810 if let Some(ref pool
) = pool
{
811 let (pool_config
, _digest
) = config
::media_pool
::config()?
;
813 if pool_config
.sections
.get(pool
).is_none() {
814 bail
!("no such pool ('{}')", pool
);
818 let auth_id
: Authid
= rpcenv
.get_auth_id().unwrap().parse()?
;
820 let to_stdout
= rpcenv
.env_type() == RpcEnvironmentType
::CLI
;
822 let upid_str
= WorkerTask
::new_thread(
823 "barcode-label-media",
828 barcode_label_media_worker(worker
, drive
, pool
)
835 fn barcode_label_media_worker(
836 worker
: Arc
<WorkerTask
>,
838 pool
: Option
<String
>,
839 ) -> Result
<(), Error
> {
841 let (config
, _digest
) = config
::drive
::config()?
;
843 let (mut changer
, changer_name
) = required_media_changer(&config
, &drive
)?
;
845 let label_text_list
= changer
.online_media_label_texts()?
;
847 let state_path
= Path
::new(TAPE_STATUS_DIR
);
849 let mut inventory
= Inventory
::load(state_path
)?
;
851 update_changer_online_status(&config
, &mut inventory
, &changer_name
, &label_text_list
)?
;
853 if label_text_list
.is_empty() {
854 bail
!("changer device does not list any media labels");
857 for label_text
in label_text_list
{
858 if label_text
.starts_with("CLN") { continue; }
861 if inventory
.find_media_by_label_text(&label_text
).is_some() {
862 worker
.log(format
!("media '{}' already inventoried (already labeled)", label_text
));
866 worker
.log(format
!("checking/loading media '{}'", label_text
));
868 if let Err(err
) = changer
.load_media(&label_text
) {
869 worker
.warn(format
!("unable to load media '{}' - {}", label_text
, err
));
873 let mut drive
= open_drive(&config
, &drive
)?
;
876 match drive
.read_next_file() {
878 worker
.log(format
!("media '{}' is not empty (erase first)", label_text
));
881 Ok(None
) => { /* EOF mark at BOT, assume tape is empty */ }
,
883 if err
.is_errno(nix
::errno
::Errno
::ENOSPC
) || err
.is_errno(nix
::errno
::Errno
::EIO
) {
884 /* assume tape is empty */
886 worker
.warn(format
!("media '{}' read error (maybe not empty - erase first)", label_text
));
892 let ctime
= proxmox
::tools
::time
::epoch_i64();
893 let label
= MediaLabel
{
894 label_text
: label_text
.to_string(),
895 uuid
: Uuid
::generate(),
899 write_media_label(worker
.clone(), &mut drive
, label
, pool
.clone())?
909 schema
: DRIVE_NAME_SCHEMA
,
914 description
: "A List of medium auxiliary memory attributes.",
921 /// Read Cartridge Memory (Medium auxiliary memory attributes)
922 pub fn cartridge_memory(drive
: String
) -> Result
<Vec
<MamAttribute
>, Error
> {
924 let (config
, _digest
) = config
::drive
::config()?
;
926 let drive_config
: LinuxTapeDrive
= config
.lookup("linux", &drive
)?
;
927 let mut handle
= drive_config
.open()?
;
929 handle
.cartridge_memory()
936 schema
: DRIVE_NAME_SCHEMA
,
941 type: LinuxDriveAndMediaStatus
,
944 /// Get drive/media status
945 pub fn status(drive
: String
) -> Result
<LinuxDriveAndMediaStatus
, Error
> {
947 let (config
, _digest
) = config
::drive
::config()?
;
949 let drive_config
: LinuxTapeDrive
= config
.lookup("linux", &drive
)?
;
951 // Note: use open_linux_tape_device, because this also works if no medium loaded
952 let file
= open_linux_tape_device(&drive_config
.path
)?
;
954 let mut handle
= LinuxTapeHandle
::new(file
);
956 handle
.get_drive_and_media_status()
963 schema
: DRIVE_NAME_SCHEMA
,
966 description
: "Force overriding existing index.",
971 description
: "Verbose mode - log all found chunks.",
981 /// Scan media and record content
982 pub fn catalog_media(
985 verbose
: Option
<bool
>,
986 rpcenv
: &mut dyn RpcEnvironment
,
987 ) -> Result
<Value
, Error
> {
989 let verbose
= verbose
.unwrap_or(false);
990 let force
= force
.unwrap_or(false);
992 let (config
, _digest
) = config
::drive
::config()?
;
994 check_drive_exists(&config
, &drive
)?
; // early check before starting worker
996 let auth_id
: Authid
= rpcenv
.get_auth_id().unwrap().parse()?
;
998 let to_stdout
= rpcenv
.env_type() == RpcEnvironmentType
::CLI
;
1000 let upid_str
= WorkerTask
::new_thread(
1002 Some(drive
.clone()),
1007 let mut drive
= open_drive(&config
, &drive
)?
;
1011 let media_id
= match drive
.read_label()?
{
1012 (Some(media_id
), key_config
) => {
1014 "found media label: {}",
1015 serde_json
::to_string_pretty(&serde_json
::to_value(&media_id
)?
)?
1017 if key_config
.is_some() {
1019 "encryption key config: {}",
1020 serde_json
::to_string_pretty(&serde_json
::to_value(&key_config
)?
)?
1025 (None
, _
) => bail
!("media is empty (no media label found)"),
1028 let status_path
= Path
::new(TAPE_STATUS_DIR
);
1030 let mut inventory
= Inventory
::load(status_path
)?
;
1031 inventory
.store(media_id
.clone(), false)?
;
1033 let pool
= match media_id
.media_set_label
{
1035 worker
.log("media is empty");
1036 MediaCatalog
::destroy(status_path
, &media_id
.label
.uuid
)?
;
1040 if set
.uuid
.as_ref() == [0u8;16] { // media is empty
1041 worker
.log("media is empty");
1042 MediaCatalog
::destroy(status_path
, &media_id
.label
.uuid
)?
;
1045 let encrypt_fingerprint
= set
.encryption_key_fingerprint
.clone();
1046 drive
.set_encryption(encrypt_fingerprint
)?
;
1052 let _lock
= MediaPool
::lock(status_path
, &pool
)?
;
1054 if MediaCatalog
::exists(status_path
, &media_id
.label
.uuid
) {
1056 bail
!("media catalog exists (please use --force to overwrite)");
1060 restore_media(&worker
, &mut drive
, &media_id
, None
, verbose
)?
;
1071 pub const SUBDIRS
: SubdirMap
= &sorted
!([
1073 "barcode-label-media",
1075 .put(&API_METHOD_BARCODE_LABEL_MEDIA
)
1080 .put(&API_METHOD_CATALOG_MEDIA
)
1085 .put(&API_METHOD_CLEAN_DRIVE
)
1090 .put(&API_METHOD_EJECT_MEDIA
)
1095 .put(&API_METHOD_ERASE_MEDIA
)
1100 .get(&API_METHOD_INVENTORY
)
1101 .put(&API_METHOD_UPDATE_INVENTORY
)
1106 .put(&API_METHOD_LABEL_MEDIA
)
1111 .put(&API_METHOD_LOAD_SLOT
)
1116 .put(&API_METHOD_CARTRIDGE_MEMORY
)
1121 .get(&API_METHOD_READ_LABEL
)
1126 .put(&API_METHOD_REWIND
)
1131 .get(&API_METHOD_SCAN_DRIVES
)
1136 .get(&API_METHOD_STATUS
)
1141 .put(&API_METHOD_UNLOAD
)
1145 pub const ROUTER
: Router
= Router
::new()
1146 .get(&list_subdirs_api_method
!(SUBDIRS
))