3 * Bacula Tape manipulation program
5 * Has various tape manipulation commands -- mostly for
6 * use in determining how tapes really work.
8 * Kern Sibbald, April MM
10 * Note, this program reads stored.conf, and will only
11 * talk to devices that are configured.
17 Copyright (C) 2000-2003 Kern Sibbald and John Walker
19 This program is free software; you can redistribute it and/or
20 modify it under the terms of the GNU General Public License as
21 published by the Free Software Foundation; either version 2 of
22 the License, or (at your option) any later version.
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
29 You should have received a copy of the GNU General Public
30 License along with this program; if not, write to the Free
31 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
40 /* External subroutines */
41 extern void free_config_resources();
43 /* Exported variables */
46 int bsize = TAPE_BSIZE;
50 DEVRES *device = NULL;
53 /* Forward referenced subroutines */
54 static void do_tape_cmds();
55 static void helpcmd();
56 static void scancmd();
57 static void rewindcmd();
58 static void clearcmd();
62 static void fillcmd();
63 static void statcmd();
64 static void unfillcmd();
65 static int flush_block(DEV_BLOCK *block, int dump);
66 static void record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec);
67 static int my_mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block);
68 static void scan_blocks();
71 /* Static variables */
72 #define CONFIG_FILE "bacula-sd.conf"
75 static BSR *bsr = NULL;
76 static char cmd[1000];
77 static int signals = TRUE;
80 static uint64_t vol_size;
81 static uint64_t VolBytes;
84 static long file_index;
85 static int end_of_tape = 0;
86 static uint32_t LastBlock = 0;
87 static uint32_t eot_block;
88 static uint32_t eot_block_len;
89 static uint32_t eot_FileIndex;
90 static int dumped = 0;
91 static DEV_BLOCK *last_block = NULL;
92 static DEV_BLOCK *this_block = NULL;
93 static uint32_t last_file = 0;
94 static uint32_t last_block_num = 0;
95 static int simple = TRUE;
97 static char *VolumeName = NULL;
99 static JCR *jcr = NULL;
103 static void terminate_btape(int sig);
104 int get_cmd(char *prompt);
107 int write_dev(DEVICE *dev, char *buf, size_t len)
109 Emsg0(M_ABORT, 0, "write_dev not implemented.\n");
113 int read_dev(DEVICE *dev, char *buf, size_t len)
115 Emsg0(M_ABORT, 0, "read_dev not implemented.\n");
120 /*********************************************************************
122 * Main Bacula Pool Creation Program
125 int main(int argc, char *argv[])
131 if (TAPE_BSIZE % DEV_BSIZE != 0 || TAPE_BSIZE / DEV_BSIZE == 0) {
132 Emsg2(M_ABORT, 0, "Tape block size (%d) not multiple of system size (%d)\n",
133 TAPE_BSIZE, DEV_BSIZE);
135 if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
136 Emsg1(M_ABORT, 0, "Tape block size (%d) is not a power of 2\n", TAPE_BSIZE);
139 printf("Tape block granularity is %d bytes.\n", TAPE_BSIZE);
141 working_directory = "/tmp";
142 my_name_is(argc, argv, "btape");
143 init_msg(NULL, NULL);
145 while ((ch = getopt(argc, argv, "b:c:d:sv?")) != -1) {
147 case 'b': /* bootstrap file */
148 bsr = parse_bsr(NULL, optarg);
152 case 'c': /* specify config file */
153 if (configfile != NULL) {
156 configfile = bstrdup(optarg);
159 case 'd': /* set debug level */
160 debug_level = atoi(optarg);
161 if (debug_level <= 0) {
187 init_signals(terminate_btape);
190 if (configfile == NULL) {
191 configfile = bstrdup(CONFIG_FILE);
194 daemon_start_time = time(NULL);
196 parse_config(configfile);
199 /* See if we can open a device */
201 Pmsg0(000, "No archive name specified.\n");
204 } else if (argc != 1) {
205 Pmsg0(000, "Improper number of arguments specified.\n");
210 jcr = setup_jcr("btape", argv[0], bsr, NULL);
211 dev = setup_to_access_device(jcr, 0); /* acquire for write */
215 block = new_block(dev);
217 if (!(dev->state & ST_OPENED)) {
218 Dmsg0(129, "Opening device.\n");
219 if (open_dev(dev, jcr->VolumeName, READ_WRITE) < 0) {
220 Emsg1(M_FATAL, 0, _("dev open failed: %s\n"), dev->errmsg);
226 Dmsg1(129, "open_dev %s OK\n", dev_name(dev));
230 Dmsg0(200, "Do tape commands\n");
238 static void terminate_btape(int stat)
241 sm_check(__FILE__, __LINE__, False);
245 free_config_resources();
251 if (debug_level > 10)
252 print_memory_pool_stats();
262 free_block(last_block);
265 free_block(this_block);
269 close_memory_pool(); /* free memory in pool */
281 * Write a label to the tape
283 static void labelcmd()
289 for (device=NULL; (device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device)); ) {
290 if (strcmp(device->device_name, dev->dev_name) == 0) {
291 jcr->device = device; /* Arggg a bit of duplication here */
293 dev->device = device;
300 Pmsg2(0, "Could not find device %s in %s\n", dev->dev_name, configfile);
305 strcpy(cmd, VolumeName);
307 if (!get_cmd("Enter Volume Name: ")) {
312 if (!(dev->state & ST_OPENED)) {
313 if (!open_device(dev)) {
314 Pmsg1(0, "Device open failed. ERR=%s\n", strerror_dev(dev));
317 write_volume_label_to_dev(jcr, device, cmd, "Default");
321 * Read the tape label
323 static void readlabelcmd()
325 int save_debug_level = debug_level;
329 block = new_block(dev);
330 stat = read_dev_volume_label(jcr, dev, block);
333 Pmsg0(0, "Volume has no label.\n");
336 Pmsg0(0, "Volume label read correctly.\n");
339 Pmsg1(0, "I/O error on device: ERR=%s", strerror_dev(dev));
342 Pmsg0(0, "Volume name error\n");
344 case VOL_CREATE_ERROR:
345 Pmsg1(0, "Error creating label. ERR=%s", strerror_dev(dev));
347 case VOL_VERSION_ERROR:
348 Pmsg0(0, "Volume version error.\n");
350 case VOL_LABEL_ERROR:
351 Pmsg0(0, "Bad Volume label type.\n");
354 Pmsg0(0, "Unknown error.\n");
359 dump_volume_label(dev);
360 debug_level = save_debug_level;
366 * Load the tape should have prevously been taken
367 * off line, otherwise this command is not necessary.
369 static void loadcmd()
372 if (!load_dev(dev)) {
373 Pmsg1(0, "Bad status from load. ERR=%s\n", strerror_dev(dev));
375 Pmsg1(0, "Loaded %s\n", dev_name(dev));
381 static void rewindcmd()
383 if (!rewind_dev(dev)) {
384 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
385 clrerror_dev(dev, -1);
387 Pmsg1(0, "Rewound %s\n", dev_name(dev));
392 * Clear any tape error
394 static void clearcmd()
396 clrerror_dev(dev, -1);
400 * Write and end of file on the tape
402 static void weofcmd()
406 if ((stat = weof_dev(dev, 1)) < 0) {
407 Pmsg2(0, "Bad status from weof %d. ERR=%s\n", stat, strerror_dev(dev));
410 Pmsg1(0, "Wrote EOF to %s\n", dev_name(dev));
415 /* Go to the end of the medium -- raw command
416 * The idea was orginally that the end of the Bacula
417 * medium would be flagged differently. This is not
418 * currently the case. So, this is identical to the
424 Pmsg1(0, _("Bad status from eod. ERR=%s\n"), strerror_dev(dev));
427 Pmsg0(0, _("Moved to end of media\n"));
432 * Go to the end of the media (either hardware determined
433 * or defined by two eofs.
447 if ((stat=bsf_dev(dev, 1)) < 0) {
448 Pmsg1(0, _("Bad status from bsf. ERR=%s\n"), strerror(errno));
450 Pmsg0(0, _("Back spaced one file.\n"));
461 if ((stat=bsr_dev(dev, 1)) < 0) {
462 Pmsg1(0, _("Bad status from bsr. ERR=%s\n"), strerror(errno));
464 Pmsg0(0, _("Back spaced one record.\n"));
469 * List device capabilities as defined in the
474 printf(_("Device capabilities:\n"));
475 printf("%sEOF ", dev->capabilities & CAP_EOF ? "" : "!");
476 printf("%sBSR ", dev->capabilities & CAP_BSR ? "" : "!");
477 printf("%sBSF ", dev->capabilities & CAP_BSF ? "" : "!");
478 printf("%sFSR ", dev->capabilities & CAP_FSR ? "" : "!");
479 printf("%sFSF ", dev->capabilities & CAP_FSF ? "" : "!");
480 printf("%sEOM ", dev->capabilities & CAP_EOM ? "" : "!");
481 printf("%sREM ", dev->capabilities & CAP_REM ? "" : "!");
482 printf("%sRACCESS ", dev->capabilities & CAP_RACCESS ? "" : "!");
483 printf("%sAUTOMOUNT ", dev->capabilities & CAP_AUTOMOUNT ? "" : "!");
484 printf("%sLABEL ", dev->capabilities & CAP_LABEL ? "" : "!");
485 printf("%sANONVOLS ", dev->capabilities & CAP_ANONVOLS ? "" : "!");
486 printf("%sALWAYSOPEN ", dev->capabilities & CAP_ALWAYSOPEN ? "" : "!");
489 printf(_("Device status:\n"));
490 printf("%sOPENED ", dev->state & ST_OPENED ? "" : "!");
491 printf("%sTAPE ", dev->state & ST_TAPE ? "" : "!");
492 printf("%sLABEL ", dev->state & ST_LABEL ? "" : "!");
493 printf("%sMALLOC ", dev->state & ST_MALLOC ? "" : "!");
494 printf("%sAPPEND ", dev->state & ST_APPEND ? "" : "!");
495 printf("%sREAD ", dev->state & ST_READ ? "" : "!");
496 printf("%sEOT ", dev->state & ST_EOT ? "" : "!");
497 printf("%sWEOT ", dev->state & ST_WEOT ? "" : "!");
498 printf("%sEOF ", dev->state & ST_EOF ? "" : "!");
499 printf("%sNEXTVOL ", dev->state & ST_NEXTVOL ? "" : "!");
500 printf("%sSHORT ", dev->state & ST_SHORT ? "" : "!");
503 printf(_("Device parameters:\n"));
504 printf("Device name: %s\n", dev->dev_name);
505 printf("File=%u block=%u\n", dev->file, dev->block_num);
506 printf("Min block=%u Max block=%u\n", dev->min_block_size, dev->max_block_size);
514 * Test writting larger and larger records.
515 * This is a torture test for records.
517 static void rectestcmd()
523 Pmsg0(0, "Test writting larger and larger records.\n"
524 "This is a torture test for records.\nI am going to write\n"
525 "larger and larger records. It will stop when the record size\n"
526 "plus the header exceeds the block size (by default about 64K)\n");
529 get_cmd("Do you want to continue? (y/n): ");
531 Pmsg0(000, "Command aborted.\n");
535 sm_check(__FILE__, __LINE__, False);
536 block = new_block(dev);
539 for (i=1; i<500000; i++) {
540 rec->data = check_pool_memory_size(rec->data, i);
541 memset(rec->data, i & 0xFF, i);
543 sm_check(__FILE__, __LINE__, False);
544 if (write_record_to_block(block, rec)) {
547 Pmsg2(0, "Block %d i=%d\n", blkno, i);
551 sm_check(__FILE__, __LINE__, False);
555 sm_check(__FILE__, __LINE__, False);
559 * This test attempts to re-read a block written by Bacula
560 * normally at the end of the tape. Bacula will then back up
561 * over the two eof marks, backup over the record and reread
562 * it to make sure it is valid. Bacula can skip this validation
563 * if you set "Backward space record = no"
565 static int re_read_block_test()
572 if (!(dev->capabilities & CAP_BSR)) {
573 Pmsg0(-1, _("Skipping read backwards test because BSR turned off.\n"));
577 Pmsg0(-1, _("\n=== Write, backup, and re-read test ===\n\n"
578 "I'm going to write three records and two eof's\n"
579 "then backup over the eof's and re-read the last record.\n"
580 "Bacula does this after writing the last block on the\n"
581 "tape to verify that the block was written correctly.\n"
582 "It is not an *essential* feature ...\n\n"));
584 block = new_block(dev);
586 rec->data = check_pool_memory_size(rec->data, block->buf_len);
587 len = rec->data_len = block->buf_len-100;
588 memset(rec->data, 1, rec->data_len);
589 if (!write_record_to_block(block, rec)) {
590 Pmsg0(0, _("Error writing record to block.\n"));
593 if (!write_block_to_dev(jcr, dev, block)) {
594 Pmsg0(0, _("Error writing block to device.\n"));
597 Pmsg1(0, _("Wrote first record of %d bytes.\n"), rec->data_len);
599 memset(rec->data, 2, rec->data_len);
600 if (!write_record_to_block(block, rec)) {
601 Pmsg0(0, _("Error writing record to block.\n"));
604 if (!write_block_to_dev(jcr, dev, block)) {
605 Pmsg0(0, _("Error writing block to device.\n"));
608 Pmsg1(0, _("Wrote second record of %d bytes.\n"), rec->data_len);
610 memset(rec->data, 3, rec->data_len);
611 if (!write_record_to_block(block, rec)) {
612 Pmsg0(0, _("Error writing record to block.\n"));
615 if (!write_block_to_dev(jcr, dev, block)) {
616 Pmsg0(0, _("Error writing block to device.\n"));
619 Pmsg1(0, _("Wrote third record of %d bytes.\n"), rec->data_len);
623 if (bsf_dev(dev, 1) != 0) {
624 Pmsg1(0, _("Back space file failed! ERR=%s\n"), strerror(dev->dev_errno));
627 if (bsf_dev(dev, 1) != 0) {
628 Pmsg1(0, _("Back space file failed! ERR=%s\n"), strerror(dev->dev_errno));
631 Pmsg0(0, "Backspaced over two EOFs OK.\n");
632 if (bsr_dev(dev, 1) != 0) {
633 Pmsg1(0, _("Back space record failed! ERR=%s\n"), strerror(dev->dev_errno));
636 Pmsg0(0, "Backspace record OK.\n");
637 if (!read_block_from_dev(dev, block)) {
638 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
641 memset(rec->data, 0, rec->data_len);
642 if (!read_record_from_block(block, rec)) {
643 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
646 for (int i=0; i<len; i++) {
647 if (rec->data[i] != 3) {
648 Pmsg0(0, _("Bad data in record. Test failed!\n"));
652 Pmsg0(0, _("\nBlock re-read correct. Test succeeded!\n"));
653 Pmsg0(-1, _("=== End Write, backup, and re-read test ===\n\n"));
661 Pmsg0(0, _("This is not terribly serious since Bacula only uses\n"
662 "this function to verify the last block written to the\n"
663 "tape. Bacula will skip the last block verification\n"
665 "Backward Space Record = No\n\n"
666 "to your Storage daemon's Device resource definition.\n"));
672 * This test writes some records, then writes an end of file,
673 * rewinds the tape, moves to the end of the data and attepts
674 * to append to the tape. This function is essential for
675 * Bacula to be able to write multiple jobs to the tape.
677 static int append_test()
679 Pmsg0(-1, _("\n\n=== Append files test ===\n\n"
680 "This test is essential to Bacula.\n\n"
681 "I'm going to write one record in file 0,\n"
682 " two records in file 1,\n"
683 " and three records in file 2\n\n"));
686 weofcmd(); /* end file 0 */
689 weofcmd(); /* end file 1 */
693 weofcmd(); /* end file 2 */
695 Pmsg0(0, _("Now moving to end of media.\n"));
697 Pmsg2(-1, _("We should be in file 3. I am at file %d. This is %s\n"),
698 dev->file, dev->file == 3 ? "correct!" : "NOT correct!!!!");
700 if (dev->file != 3) {
704 Pmsg0(-1, _("\nNow the important part, I am going to attempt to append to the tape.\n\n"));
708 Pmsg0(-1, _("Done appending, there should be no I/O errors\n\n"));
709 Pmsg0(-1, "Doing Bacula scan of blocks:\n");
711 Pmsg0(-1, _("End scanning the tape.\n"));
712 Pmsg2(-1, _("We should be in file 4. I am at file %d. This is %s\n"),
713 dev->file, dev->file == 4 ? "correct!" : "NOT correct!!!!");
715 if (dev->file != 4) {
723 * This is a general test of Bacula's functions
724 * needed to read and write the tape.
726 static void testcmd()
730 stat = append_test();
731 if (stat == 1) { /* OK get out */
734 if (stat == -1) { /* first test failed */
735 if (dev_cap(dev, CAP_EOM)) {
736 Pmsg0(-1, "\nAppend test failed. Attempting again.\n"
737 "Setting \"Hardware End of Medium = no\" and retrying append test.\n\n");
738 dev->capabilities &= ~CAP_EOM; /* turn off eom */
739 stat = append_test();
741 Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n"
742 " Hardware End of Medium = No\n\n"
743 "to your Device resource in the Storage conf file.\n");
747 Pmsg0(-1, "\n\nThat appears not to have corrected the problem.\n");
750 /* Wrong count after append */
752 Pmsg0(-1, "\n\nIt looks like the append failed. Attempting again.\n"
753 "Setting \"BSF at EOM = yes\" and retrying append test.\n");
754 dev->capabilities |= CAP_BSFATEOM; /* backspace on eom */
755 stat = append_test();
757 Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n"
758 " Hardware End of Medium = No\n"
759 " BSR at EOM = yes\n\n"
760 "to your Device resource in the Storage conf file.\n");
765 Pmsg0(-1, "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
766 "Unable to correct the problem. You MUST fix this\n"
767 "problem before Bacula can use your tape drive correctly\n");
768 Pmsg0(-1, "\nPerhaps running Bacula in fixed block mode will work.\n"
769 "Do so by setting:\n\n"
770 "Minimum Block Size = nnn\n"
771 "Maximum Block Size = nnn\n\n"
772 "in your Storage daemon's Device definition.\n"
773 "nnn must match your tape driver's block size.\n"
774 "This, however, is not really an ideal solution.\n");
779 Pmsg0(-1, _("\nThe above Bacula scan should have output identical to what follows.\n"
780 "Please double check it ...\n"
781 "=== Sample correct output ===\n"
782 "1 block of 64448 bytes in file 1\n"
783 "End of File mark.\n"
784 "2 blocks of 64448 bytes in file 2\n"
785 "End of File mark.\n"
786 "3 blocks of 64448 bytes in file 3\n"
787 "End of File mark.\n"
788 "1 block of 64448 bytes in file 4\n"
789 "End of File mark.\n"
790 "Total files=4, blocks=7, bytes = 451136\n"
791 "=== End sample correct output ===\n\n"));
793 Pmsg0(-1, _("If the above scan output is not identical to the\n"
794 "sample output, you MUST correct the problem\n"
795 "or Bacula will not be able to write multiple Jobs to \n"
799 re_read_block_test();
802 Pmsg0(-1, _("\n=== End Append files test ===\n"));
806 /* Forward space a file */
809 if (!fsf_dev(dev, 1)) {
810 Pmsg1(0, "Bad status from fsf. ERR=%s\n", strerror_dev(dev));
813 Pmsg0(0, "Forward spaced one file.\n");
816 /* Forward space a record */
821 if ((stat=fsr_dev(dev, 1)) < 0) {
822 Pmsg2(0, "Bad status from fsr %d. ERR=%s\n", stat, strerror_dev(dev));
825 Pmsg0(0, "Forward spaced one record.\n");
830 * Write a Bacula block to the tape
838 sm_check(__FILE__, __LINE__, False);
839 block = new_block(dev);
841 dump_block(block, "test");
843 i = block->buf_len - 100;
845 rec->data = check_pool_memory_size(rec->data, i);
846 memset(rec->data, i & 0xFF, i);
848 sm_check(__FILE__, __LINE__, False);
849 if (!write_record_to_block(block, rec)) {
850 Pmsg0(0, _("Error writing record to block.\n"));
853 if (!write_block_to_dev(jcr, dev, block)) {
854 Pmsg0(0, _("Error writing block to device.\n"));
857 Pmsg1(0, _("Wrote one record of %d bytes.\n"), i);
859 Pmsg0(0, _("Wrote block to device.\n"));
862 sm_check(__FILE__, __LINE__, False);
865 sm_check(__FILE__, __LINE__, False);
869 * Read a record from the tape
876 if (!get_cmd("Enter length to read: ")) {
880 if (len < 0 || len > 1000000) {
881 Pmsg0(0, _("Bad length entered, using default of 1024 bytes.\n"));
884 buf = (char *)malloc(len);
885 stat = read(dev->fd, buf, len);
886 if (stat > 0 && stat <= len) {
889 Pmsg3(0, _("Read of %d bytes gives stat=%d. ERR=%s\n"),
890 len, stat, strerror(errno));
896 * Scan tape by reading block by block. Report what is
897 * on the tape. Note, this command does raw reads, and as such
898 * will not work with fixed block size devices.
900 static void scancmd()
903 int blocks, tot_blocks, tot_files;
908 blocks = block_size = tot_blocks = 0;
910 if (dev->state & ST_EOT) {
911 Pmsg0(0, "End of tape\n");
915 tot_files = dev->file;
917 if ((stat = read(dev->fd, buf, sizeof(buf))) < 0) {
918 clrerror_dev(dev, -1);
919 Mmsg2(&dev->errmsg, "read error on %s. ERR=%s.\n",
920 dev->dev_name, strerror(dev->dev_errno));
921 Pmsg2(0, "Bad status from read %d. ERR=%s\n", stat, strerror_dev(dev));
923 printf("%d block%s of %d bytes in file %d\n",
924 blocks, blocks>1?"s":"", block_size, dev->file);
927 Dmsg1(200, "read status = %d\n", stat);
929 if (stat != block_size) {
932 printf("%d block%s of %d bytes in file %d\n",
933 blocks, blocks>1?"s":"", block_size, dev->file);
938 if (stat == 0) { /* EOF */
940 printf("End of File mark.\n");
941 /* Two reads of zero means end of tape */
942 if (dev->state & ST_EOF)
943 dev->state |= ST_EOT;
945 dev->state |= ST_EOF;
948 if (dev->state & ST_EOT) {
949 printf("End of tape\n");
952 } else { /* Got data */
953 dev->state &= ~ST_EOF;
960 tot_files = dev->file - tot_files;
961 printf("Total files=%d, blocks=%d, bytes = %" lld "\n", tot_files, tot_blocks, bytes);
966 * Scan tape by reading Bacula block by block. Report what is
967 * on the tape. This function reads Bacula blocks, so if your
968 * Device resource is correctly defined, it should work with
969 * either variable or fixed block sizes.
971 static void scan_blocks()
973 int blocks, tot_blocks, tot_files;
978 block = new_block(dev);
979 blocks = block_size = tot_blocks = 0;
983 tot_files = dev->file;
985 if (!read_block_from_device(dev, block)) {
986 Dmsg1(100, "!read_block(): ERR=%s\n", strerror_dev(dev));
987 if (dev->state & ST_EOT) {
989 printf("%d block%s of %d bytes in file %d\n",
990 blocks, blocks>1?"s":"", block_size, dev->file);
995 if (dev->state & ST_EOF) {
997 printf("%d block%s of %d bytes in file %d\n",
998 blocks, blocks>1?"s":"", block_size, dev->file);
1001 printf(_("End of File mark.\n"));
1004 if (dev->state & ST_SHORT) {
1006 printf("%d block%s of %d bytes in file %d\n",
1007 blocks, blocks>1?"s":"", block_size, dev->file);
1010 printf(_("Short block read.\n"));
1013 printf(_("Error reading block. ERR=%s\n"), strerror_dev(dev));
1016 if (block->block_len != block_size) {
1018 printf("%d block%s of %d bytes in file %d\n",
1019 blocks, blocks>1?"s":"", block_size, dev->file);
1022 block_size = block->block_len;
1026 bytes += block->block_len;
1027 Dmsg5(100, "Blk=%u blen=%u bVer=%d SessId=%u SessTim=%u\n",
1028 block->BlockNumber, block->block_len, block->BlockVer,
1029 block->VolSessionId, block->VolSessionTime);
1031 DEV_RECORD *rec = new_record();
1032 read_record_from_block(block, rec);
1033 Pmsg7(-1, "Block: %u blen=%u First rec FI=%s SessId=%u SessTim=%u Strm=%s rlen=%d\n",
1034 block->BlockNumber, block->block_len,
1035 FI_to_ascii(rec->FileIndex), rec->VolSessionId, rec->VolSessionTime,
1036 stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len);
1039 } else if (verbose > 1) {
1040 dump_block(block, "");
1046 tot_files = dev->file - tot_files;
1047 printf("Total files=%d, blocks=%d, bytes = %" lld "\n", tot_files, tot_blocks, bytes);
1051 static void statcmd()
1057 debug = debug_level;
1059 if (!status_dev(dev, &status)) {
1060 Pmsg2(0, "Bad status from status %d. ERR=%s\n", stat, strerror_dev(dev));
1063 dump_volume_label(dev);
1065 debug_level = debug;
1070 * First we label the tape, then we fill
1071 * it with data get a new tape and write a few blocks.
1073 static void fillcmd()
1084 This command simulates Bacula writing to a tape.\n\
1085 It requires either one or two blank tapes, which it\n\
1086 will label and write. It will print a status approximately\n\
1087 every 322 MB, and write an EOF every 3.2 GB. If you have\n\
1088 selected the simple test option, after writing the first tape\n\
1089 it will rewind it and re-read the last block written.\n\
1090 If you have selected the multiple tape test, when the first tape\n\
1091 fills, it will ask for a second, and after writing a few \n\
1092 blocks, it will stop. Then it will begin re-reading the\n\
1093 This may take a long time. I.e. hours! ...\n\n");
1095 get_cmd("Insert a blank tape then indicate if you want\n"
1096 "to run the simplified test (s) with one tape or\n"
1097 "the complete multiple tape (m) test: (s/m) ");
1098 if (cmd[0] == 's') {
1099 Pmsg0(-1, "Simple test (single tape) selected.\n");
1101 } else if (cmd[0] == 'm') {
1102 Pmsg0(-1, "Complete multiple tape test selected.\n");
1105 Pmsg0(000, "Command aborted.\n");
1109 VolumeName = "TestVolume1";
1114 Dmsg1(20, "Begin append device=%s\n", dev_name(dev));
1116 block = new_block(dev);
1119 * Acquire output device for writing. Note, after acquiring a
1120 * device, we MUST release it, which is done at the end of this
1123 Dmsg0(100, "just before acquire_device\n");
1124 if (!(dev=acquire_device_for_append(jcr, dev, block))) {
1125 set_jcr_job_status(jcr, JS_ErrorTerminated);
1130 Dmsg0(100, "Just after acquire_device_for_append\n");
1132 * Write Begin Session Record
1134 if (!write_session_label(jcr, block, SOS_LABEL)) {
1135 set_jcr_job_status(jcr, JS_ErrorTerminated);
1136 Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"),
1141 memset(&rec, 0, sizeof(rec));
1142 rec.data = get_memory(100000); /* max record size */
1144 * Fill write buffer with random data
1146 #define REC_SIZE 32768
1148 for (int i=0; i < REC_SIZE; ) {
1149 makeSessionKey(p, NULL, 0);
1153 rec.data_len = REC_SIZE;
1156 * Get Data from File daemon, write to device
1158 jcr->VolFirstFile = 0;
1159 time(&jcr->run_time); /* start counting time for rates */
1160 for (file_index = 0; ok && !job_cancelled(jcr); ) {
1162 rec.VolSessionId = jcr->VolSessionId;
1163 rec.VolSessionTime = jcr->VolSessionTime;
1164 rec.FileIndex = ++file_index;
1165 rec.Stream = STREAM_FILE_DATA;
1167 /* Write file_index at beginning of buffer and add file_index to each
1168 * uint64_t item to make it unique.
1170 lp = (uint64_t *)rec.data;
1171 *lp++ = (uint64_t)file_index;
1172 for (uint32_t i=0; i < (REC_SIZE-sizeof(uint64_t))/sizeof(uint64_t); i++) {
1173 *lp++ = *lp + rec.FileIndex;
1176 Dmsg4(250, "before writ_rec FI=%d SessId=%d Strm=%s len=%d\n",
1177 rec.FileIndex, rec.VolSessionId, stream_to_ascii(rec.Stream, rec.FileIndex),
1180 if (!write_record_to_block(block, &rec)) {
1181 Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec.data_len,
1184 /* Write block to tape */
1185 if (!flush_block(block, 1)) {
1189 /* Every 5000 blocks (approx 322MB) report where we are.
1191 if ((block->BlockNumber % 5000) == 0) {
1193 now -= jcr->run_time;
1197 kbs = (double)dev->VolCatInfo.VolCatBytes / (1000 * now);
1198 Pmsg3(000, "Wrote block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
1199 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), (float)kbs);
1201 /* Every 50000 blocks (approx 3.2MB) write an eof.
1203 if ((block->BlockNumber % 50000) == 0) {
1204 Pmsg0(000, "Flush block, write EOF\n");
1205 flush_block(block, 0);
1207 /* The weof resets the block number */
1210 if (block->BlockNumber > 10 && stop != 0) { /* get out */
1215 Pmsg0(000, _("Not OK\n"));
1218 jcr->JobBytes += rec.data_len; /* increment bytes this job */
1219 Dmsg4(190, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
1220 FI_to_ascii(rec.FileIndex), rec.VolSessionId,
1221 stream_to_ascii(rec.Stream, rec.FileIndex), rec.data_len);
1224 Dmsg0(000, "Write_end_session_label()\n");
1225 /* Create Job status for end of session label */
1226 if (!job_cancelled(jcr) && ok) {
1227 set_jcr_job_status(jcr, JS_Terminated);
1229 set_jcr_job_status(jcr, JS_ErrorTerminated);
1231 if (!write_session_label(jcr, block, EOS_LABEL)) {
1232 Pmsg1(000, _("Error writting end session label. ERR=%s\n"), strerror_dev(dev));
1235 /* Write out final block of this session */
1236 if (!write_block_to_device(jcr, dev, block)) {
1237 Pmsg0(000, _("Set ok=FALSE after write_block_to_device.\n"));
1242 /* Release the device */
1243 if (!release_device(jcr, dev)) {
1244 Pmsg0(000, _("Error in release_device\n"));
1249 free_memory(rec.data);
1250 Pmsg0(000, _("\n\nDone filling tape. Now beginning re-read of tape ...\n"));
1252 dump_block(last_block, _("Last block written to tape.\n"));
1258 * Read two tapes written by the "fill" command and ensure
1259 * that the data is valid. If stop==1 we simulate full read back
1260 * of two tapes. If stop==-1 we simply read the last block and
1261 * verify that it is correct.
1263 static void unfillcmd()
1270 block = new_block(dev);
1272 dev->capabilities |= CAP_ANONVOLS; /* allow reading any volume */
1273 dev->capabilities &= ~CAP_LABEL; /* don't label anything here */
1277 get_cmd(_("Mount first of two tapes. Press enter when ready: "));
1281 pm_strcpy(&jcr->VolumeName, "TestVolume1");
1283 create_vol_list(jcr);
1285 dev->state &= ~ST_READ;
1286 if (!acquire_device_for_read(jcr, dev, block)) {
1287 Pmsg0(000, dev->errmsg);
1291 time(&jcr->run_time); /* start counting time for rates */
1295 /* Read all records and then second tape */
1296 read_records(jcr, dev, record_cb, my_mount_next_read_volume);
1299 * Simplified test, we simply fsf to file, then read the
1300 * last block and make sure it is the same as the saved block.
1302 if (!rewind_dev(dev)) {
1303 Pmsg1(-1, _("Error rewinding: ERR=%s\n"), strerror_dev(dev));
1306 if (last_file > 0) {
1307 if (!fsf_dev(dev, last_file)) {
1308 Pmsg1(-1, _("Error in FSF: ERR=%s\n"), strerror_dev(dev));
1312 Pmsg1(-1, _("Forward space to file %u complete. Reading blocks ...\n"),
1314 Pmsg1(-1, _("Now reading to block %u.\n"), last_block_num);
1315 for (uint32_t i= 0; i < last_block_num; i++) {
1316 if (!read_block_from_device(dev, block)) {
1317 Pmsg1(-1, _("Error reading blocks: ERR=%s\n"), strerror_dev(dev));
1318 Pmsg2(-1, _("Wanted block %u error at block %u\n"), last_block_num, i);
1321 if (i > 0 && i % 1000 == 0) {
1322 Pmsg1(-1, _("At block %u\n"), i);
1325 dump_block(last_block, _("Last block written"));
1326 dump_block(block, _("Block read back"));
1327 Pmsg0(-1, _("Except for the buffer address, the contents of\n"
1328 "the above two block dumps should be the same.\n"
1329 "If not you have a problem ...\n"));
1335 Pmsg0(000, _("Done with reread of fill data.\n"));
1340 * We are called here from "unfill" for each record on the
1343 static void record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
1346 SESSION_LABEL label;
1347 if (stop > 1) { /* on second tape */
1348 Pmsg4(000, "Blk: FileIndex=%d: block=%u size=%d vol=%s\n",
1349 rec->FileIndex, block->BlockNumber, block->block_len, dev->VolHdr.VolName);
1350 Pmsg6(000, " Rec: VId=%d VT=%d FI=%s Strm=%s len=%d state=%x\n",
1351 rec->VolSessionId, rec->VolSessionTime,
1352 FI_to_ascii(rec->FileIndex), stream_to_ascii(rec->Stream, rec->FileIndex),
1353 rec->data_len, rec->state);
1357 dump_block(block, "Block not written to previous tape");
1360 if (rec->FileIndex < 0) {
1362 dump_label_record(dev, rec, 1);
1364 switch (rec->FileIndex) {
1366 Pmsg0(000, "Volume is prelabeled. This tape cannot be scanned.\n");
1369 unser_volume_label(dev, rec);
1370 Pmsg3(000, "VOL_LABEL: block=%u size=%d vol=%s\n", block->BlockNumber,
1371 block->block_len, dev->VolHdr.VolName);
1375 unser_session_label(&label, rec);
1376 Pmsg1(000, "SOS_LABEL: JobId=%u\n", label.JobId);
1379 unser_session_label(&label, rec);
1380 Pmsg2(000, "EOS_LABEL: block=%u JobId=%u\n", block->BlockNumber,
1384 Pmsg0(000, "EOM_LABEL:\n");
1386 case EOT_LABEL: /* end of all tapes */
1389 if (LastBlock != block->BlockNumber) {
1390 VolBytes += block->block_len;
1392 LastBlock = block->BlockNumber;
1394 now -= jcr->run_time;
1398 kbs = (double)VolBytes / (1000 * now);
1399 Pmsg3(000, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
1400 edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
1402 Pmsg0(000, "End of all tapes.\n");
1410 if (LastBlock != block->BlockNumber) {
1411 VolBytes += block->block_len;
1413 if ((block->BlockNumber != LastBlock) && (block->BlockNumber % 50000) == 0) {
1416 now -= jcr->run_time;
1420 kbs = (double)VolBytes / (1000 * now);
1421 Pmsg3(000, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
1422 edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
1424 LastBlock = block->BlockNumber;
1426 Pmsg1(000, "End of all blocks. Block=%u\n", block->BlockNumber);
1433 * Write current block to tape regardless of whether or
1434 * not it is full. If the tape fills, attempt to
1435 * acquire another tape.
1437 static int flush_block(DEV_BLOCK *block, int dump)
1442 uint32_t this_file, this_block_num;
1445 this_block = new_block(dev);
1448 memcpy(this_block, block, sizeof(DEV_BLOCK));
1449 if (this_block->buf_len < block->buf_len) {
1450 free_memory(this_block->buf);
1451 this_block->buf = get_memory(block->buf_len);
1452 this_block->buf_len = block->buf_len;
1454 memcpy(this_block->buf, block->buf, this_block->buf_len);
1455 this_file = dev->file;
1456 this_block_num = dev->block_num;
1457 if (!write_block_to_dev(jcr, dev, block)) {
1458 Pmsg0(000, strerror_dev(dev));
1459 Pmsg3(000, "Block not written: FileIndex=%u Block=%u Size=%u\n",
1460 (unsigned)file_index, block->BlockNumber, block->block_len);
1462 dump_block(block, "Block not written");
1465 eot_block = block->BlockNumber;
1466 eot_block_len = block->block_len;
1467 eot_FileIndex = file_index;
1470 now -= jcr->run_time;
1474 kbs = (double)dev->VolCatInfo.VolCatBytes / (1000 * now);
1475 vol_size = dev->VolCatInfo.VolCatBytes;
1476 Pmsg2(000, "End of tape. VolumeCapacity=%s. Write rate = %.1f KB/s\n",
1477 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), kbs);
1480 stop = -1; /* stop, but do simplified test */
1482 /* Full test in progress */
1483 if (!fixup_device_block_write_error(jcr, dev, block)) {
1484 Pmsg1(000, _("Cannot fixup device error. %s\n"), strerror_dev(dev));
1492 return 1; /* end of tape reached */
1496 * Toggle between two allocated blocks for efficiency.
1497 * Switch blocks so that the block just successfully written is
1498 * always in last_block.
1500 tblock = last_block;
1501 last_block = this_block;
1502 this_block = tblock;
1503 last_file = this_file;
1504 last_block_num = this_block_num;
1511 struct cmdstruct { char *key; void (*func)(); char *help; };
1512 static struct cmdstruct commands[] = {
1513 {"bsf", bsfcmd, "backspace file"},
1514 {"bsr", bsrcmd, "backspace record"},
1515 {"cap", capcmd, "list device capabilities"},
1516 {"clear", clearcmd, "clear tape errors"},
1517 {"eod", eodcmd, "go to end of Bacula data for append"},
1518 {"eom", eomcmd, "go to the physical end of medium"},
1519 {"fill", fillcmd, "fill tape, write onto second volume"},
1520 {"unfill", unfillcmd, "read filled tape"},
1521 {"fsf", fsfcmd, "forward space a file"},
1522 {"fsr", fsrcmd, "forward space a record"},
1523 {"help", helpcmd, "print this command"},
1524 {"label", labelcmd, "write a Bacula label to the tape"},
1525 {"load", loadcmd, "load a tape"},
1526 {"quit", quitcmd, "quit btape"},
1527 {"readlabel", readlabelcmd, "read and print the Bacula tape label"},
1528 {"rectest", rectestcmd, "test record handling functions"},
1529 {"rewind", rewindcmd, "rewind the tape"},
1530 {"scan", scancmd, "read tape block by block to EOT and report"},
1531 {"status", statcmd, "print tape status"},
1532 {"test", testcmd, "General test Bacula tape functions"},
1533 {"weof", weofcmd, "write an EOF on the tape"},
1534 {"wr", wrcmd, "write a single Bacula block"},
1535 {"rr", rrcmd, "read a single record"},
1537 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
1545 while (get_cmd("*")) {
1546 sm_check(__FILE__, __LINE__, False);
1548 for (i=0; i<comsize; i++) /* search for command */
1549 if (fstrsch(cmd, commands[i].key)) {
1550 (*commands[i].func)(); /* go execute command */
1555 Pmsg1(0, _("%s is an illegal command\n"), cmd);
1561 static void helpcmd()
1565 printf(_(" Command Description\n ======= ===========\n"));
1566 for (i=0; i<comsize; i++)
1567 printf(" %-10s %s\n", commands[i].key, commands[i].help);
1574 "\nVersion: " VERSION " (" BDATE ")\n\n"
1575 "Usage: btape [-c config_file] [-d debug_level] [device_name]\n"
1576 " -c <file> set configuration file to file\n"
1577 " -dnn set debug level to nn\n"
1578 " -s turn off signals\n"
1579 " -t open the default tape device\n"
1580 " -? print this message.\n"
1586 * Get next input command from terminal. This
1587 * routine is REALLY primitive, and should be enhanced
1588 * to have correct backspacing, etc.
1591 get_cmd(char *prompt)
1595 fprintf(stdout, prompt);
1597 /* We really should turn off echoing and pretty this
1601 while ((ch = fgetc(stdin)) != EOF) {
1603 strip_trailing_junk(cmd);
1605 } else if (ch == 4 || ch == 0xd3 || ch == 0x8) {
1618 /* Dummies to replace askdir.c */
1619 int dir_get_volume_info(JCR *jcr, int writing) { return 1;}
1620 int dir_find_next_appendable_volume(JCR *jcr) { return 1;}
1621 int dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
1622 int dir_create_jobmedia_record(JCR *jcr) { return 1; }
1623 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
1624 int dir_send_job_status(JCR *jcr) {return 1;}
1627 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
1629 Pmsg0(000, dev->errmsg); /* print reason */
1630 fprintf(stderr, "Mount Volume \"%s\" on device %s and press return when ready: ",
1631 jcr->VolumeName, dev_name(dev));
1636 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev)
1638 fprintf(stderr, "Mount next Volume on device %s and press return when ready: ",
1641 VolumeName = "TestVolume2";
1647 static int my_mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
1651 Pmsg1(000, "End of Volume \"%s\"\n", jcr->VolumeName);
1653 if (LastBlock != block->BlockNumber) {
1654 VolBytes += block->block_len;
1656 LastBlock = block->BlockNumber;
1658 now -= jcr->run_time;
1662 kbs = (double)VolBytes / (1000 * now);
1663 Pmsg3(000, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
1664 edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
1666 if (strcmp(jcr->VolumeName, "TestVolume2") == 0) {
1672 pm_strcpy(&jcr->VolumeName, "TestVolume2");
1674 create_vol_list(jcr);
1676 dev->state &= ~ST_READ;
1677 if (!acquire_device_for_read(jcr, dev, block)) {
1678 Pmsg2(0, "Cannot open Dev=%s, Vol=%s\n", dev_name(dev), jcr->VolumeName);
1681 return 1; /* next volume mounted */