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;
47 char VolName[MAX_NAME_LENGTH];
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 qfillcmd();
64 static void statcmd();
65 static void unfillcmd();
66 static int flush_block(DEV_BLOCK *block, int dump);
67 static void record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec);
68 static int my_mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block);
69 static void scan_blocks();
70 static void set_volume_name(char *VolName, int volnum);
71 static void rawfill_cmd();
74 /* Static variables */
75 #define CONFIG_FILE "bacula-sd.conf"
78 static BSR *bsr = NULL;
79 static char cmd[1000];
80 static int signals = TRUE;
83 static uint64_t vol_size;
84 static uint64_t VolBytes;
87 static int32_t file_index;
88 static int end_of_tape = 0;
89 static uint32_t LastBlock = 0;
90 static uint32_t eot_block;
91 static uint32_t eot_block_len;
92 static uint32_t eot_FileIndex;
93 static int dumped = 0;
94 static DEV_BLOCK *last_block = NULL;
95 static DEV_BLOCK *this_block = NULL;
96 static uint32_t last_file = 0;
97 static uint32_t last_block_num = 0;
98 static uint32_t BlockNumber = 0;
99 static int simple = FALSE;
101 static char *VolumeName = NULL;
104 static JCR *jcr = NULL;
108 static void terminate_btape(int sig);
109 int get_cmd(char *prompt);
112 /*********************************************************************
114 * Main Bacula Pool Creation Program
117 int main(int argc, char *argv[])
123 if (TAPE_BSIZE % DEV_BSIZE != 0 || TAPE_BSIZE / DEV_BSIZE == 0) {
124 Emsg2(M_ABORT, 0, "Tape block size (%d) not multiple of system size (%d)\n",
125 TAPE_BSIZE, DEV_BSIZE);
127 if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
128 Emsg1(M_ABORT, 0, "Tape block size (%d) is not a power of 2\n", TAPE_BSIZE);
131 printf("Tape block granularity is %d bytes.\n", TAPE_BSIZE);
133 working_directory = "/tmp";
134 my_name_is(argc, argv, "btape");
135 init_msg(NULL, NULL);
137 while ((ch = getopt(argc, argv, "b:c:d:sv?")) != -1) {
139 case 'b': /* bootstrap file */
140 bsr = parse_bsr(NULL, optarg);
144 case 'c': /* specify config file */
145 if (configfile != NULL) {
148 configfile = bstrdup(optarg);
151 case 'd': /* set debug level */
152 debug_level = atoi(optarg);
153 if (debug_level <= 0) {
179 init_signals(terminate_btape);
182 if (configfile == NULL) {
183 configfile = bstrdup(CONFIG_FILE);
186 daemon_start_time = time(NULL);
188 parse_config(configfile);
191 /* See if we can open a device */
193 Pmsg0(000, "No archive name specified.\n");
196 } else if (argc != 1) {
197 Pmsg0(000, "Improper number of arguments specified.\n");
202 jcr = setup_jcr("btape", argv[0], bsr, NULL);
203 dev = setup_to_access_device(jcr, 0); /* acquire for write */
207 block = new_block(dev);
209 if (!(dev->state & ST_OPENED)) {
210 Dmsg0(129, "Opening device.\n");
211 if (open_dev(dev, jcr->VolumeName, READ_WRITE) < 0) {
212 Emsg1(M_FATAL, 0, _("dev open failed: %s\n"), dev->errmsg);
218 Dmsg1(129, "open_dev %s OK\n", dev_name(dev));
222 Dmsg0(200, "Do tape commands\n");
230 static void terminate_btape(int stat)
233 sm_check(__FILE__, __LINE__, False);
237 free_config_resources();
243 if (debug_level > 10)
244 print_memory_pool_stats();
254 free_block(last_block);
257 free_block(this_block);
261 close_memory_pool(); /* free memory in pool */
273 * Write a label to the tape
275 static void labelcmd()
281 for (device=NULL; (device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device)); ) {
282 if (strcmp(device->device_name, dev->dev_name) == 0) {
283 jcr->device = device; /* Arggg a bit of duplication here */
285 dev->device = device;
292 Pmsg2(0, "Could not find device %s in %s\n", dev->dev_name, configfile);
297 strcpy(cmd, VolumeName);
299 if (!get_cmd("Enter Volume Name: ")) {
304 if (!(dev->state & ST_OPENED)) {
305 if (!open_device(dev)) {
306 Pmsg1(0, "Device open failed. ERR=%s\n", strerror_dev(dev));
309 write_volume_label_to_dev(jcr, device, cmd, "Default");
313 * Read the tape label
315 static void readlabelcmd()
317 int save_debug_level = debug_level;
321 block = new_block(dev);
322 stat = read_dev_volume_label(jcr, dev, block);
325 Pmsg0(0, "Volume has no label.\n");
328 Pmsg0(0, "Volume label read correctly.\n");
331 Pmsg1(0, "I/O error on device: ERR=%s", strerror_dev(dev));
334 Pmsg0(0, "Volume name error\n");
336 case VOL_CREATE_ERROR:
337 Pmsg1(0, "Error creating label. ERR=%s", strerror_dev(dev));
339 case VOL_VERSION_ERROR:
340 Pmsg0(0, "Volume version error.\n");
342 case VOL_LABEL_ERROR:
343 Pmsg0(0, "Bad Volume label type.\n");
346 Pmsg0(0, "Unknown error.\n");
351 dump_volume_label(dev);
352 debug_level = save_debug_level;
358 * Load the tape should have prevously been taken
359 * off line, otherwise this command is not necessary.
361 static void loadcmd()
364 if (!load_dev(dev)) {
365 Pmsg1(0, "Bad status from load. ERR=%s\n", strerror_dev(dev));
367 Pmsg1(0, "Loaded %s\n", dev_name(dev));
373 static void rewindcmd()
375 if (!rewind_dev(dev)) {
376 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
377 clrerror_dev(dev, -1);
379 Pmsg1(0, "Rewound %s\n", dev_name(dev));
384 * Clear any tape error
386 static void clearcmd()
388 clrerror_dev(dev, -1);
392 * Write and end of file on the tape
394 static void weofcmd()
398 if ((stat = weof_dev(dev, 1)) < 0) {
399 Pmsg2(0, "Bad status from weof %d. ERR=%s\n", stat, strerror_dev(dev));
402 Pmsg1(0, "Wrote EOF to %s\n", dev_name(dev));
407 /* Go to the end of the medium -- raw command
408 * The idea was orginally that the end of the Bacula
409 * medium would be flagged differently. This is not
410 * currently the case. So, this is identical to the
416 Pmsg1(0, _("Bad status from MTEOD. ERR=%s\n"), strerror_dev(dev));
419 Pmsg0(0, _("Moved to end of medium.\n"));
424 * Go to the end of the medium (either hardware determined
425 * or defined by two eofs.
439 if ((stat=bsf_dev(dev, 1)) < 0) {
440 Pmsg1(0, _("Bad status from bsf. ERR=%s\n"), strerror(errno));
442 Pmsg0(0, _("Back spaced one file.\n"));
453 if ((stat=bsr_dev(dev, 1)) < 0) {
454 Pmsg1(0, _("Bad status from bsr. ERR=%s\n"), strerror(errno));
456 Pmsg0(0, _("Back spaced one record.\n"));
461 * List device capabilities as defined in the
466 printf(_("Device capabilities:\n"));
467 printf("%sEOF ", dev->capabilities & CAP_EOF ? "" : "!");
468 printf("%sBSR ", dev->capabilities & CAP_BSR ? "" : "!");
469 printf("%sBSF ", dev->capabilities & CAP_BSF ? "" : "!");
470 printf("%sFSR ", dev->capabilities & CAP_FSR ? "" : "!");
471 printf("%sFSF ", dev->capabilities & CAP_FSF ? "" : "!");
472 printf("%sEOM ", dev->capabilities & CAP_EOM ? "" : "!");
473 printf("%sREM ", dev->capabilities & CAP_REM ? "" : "!");
474 printf("%sRACCESS ", dev->capabilities & CAP_RACCESS ? "" : "!");
475 printf("%sAUTOMOUNT ", dev->capabilities & CAP_AUTOMOUNT ? "" : "!");
476 printf("%sLABEL ", dev->capabilities & CAP_LABEL ? "" : "!");
477 printf("%sANONVOLS ", dev->capabilities & CAP_ANONVOLS ? "" : "!");
478 printf("%sALWAYSOPEN ", dev->capabilities & CAP_ALWAYSOPEN ? "" : "!");
481 printf(_("Device status:\n"));
482 printf("%sOPENED ", dev->state & ST_OPENED ? "" : "!");
483 printf("%sTAPE ", dev->state & ST_TAPE ? "" : "!");
484 printf("%sLABEL ", dev->state & ST_LABEL ? "" : "!");
485 printf("%sMALLOC ", dev->state & ST_MALLOC ? "" : "!");
486 printf("%sAPPEND ", dev->state & ST_APPEND ? "" : "!");
487 printf("%sREAD ", dev->state & ST_READ ? "" : "!");
488 printf("%sEOT ", dev->state & ST_EOT ? "" : "!");
489 printf("%sWEOT ", dev->state & ST_WEOT ? "" : "!");
490 printf("%sEOF ", dev->state & ST_EOF ? "" : "!");
491 printf("%sNEXTVOL ", dev->state & ST_NEXTVOL ? "" : "!");
492 printf("%sSHORT ", dev->state & ST_SHORT ? "" : "!");
495 printf(_("Device parameters:\n"));
496 printf("Device name: %s\n", dev->dev_name);
497 printf("File=%u block=%u\n", dev->file, dev->block_num);
498 printf("Min block=%u Max block=%u\n", dev->min_block_size, dev->max_block_size);
506 * Test writting larger and larger records.
507 * This is a torture test for records.
509 static void rectestcmd()
515 Pmsg0(0, "Test writting larger and larger records.\n"
516 "This is a torture test for records.\nI am going to write\n"
517 "larger and larger records. It will stop when the record size\n"
518 "plus the header exceeds the block size (by default about 64K)\n");
521 get_cmd("Do you want to continue? (y/n): ");
523 Pmsg0(000, "Command aborted.\n");
527 sm_check(__FILE__, __LINE__, False);
528 block = new_block(dev);
531 for (i=1; i<500000; i++) {
532 rec->data = check_pool_memory_size(rec->data, i);
533 memset(rec->data, i & 0xFF, i);
535 sm_check(__FILE__, __LINE__, False);
536 if (write_record_to_block(block, rec)) {
539 Pmsg2(0, "Block %d i=%d\n", blkno, i);
543 sm_check(__FILE__, __LINE__, False);
547 sm_check(__FILE__, __LINE__, False);
551 * This test attempts to re-read a block written by Bacula
552 * normally at the end of the tape. Bacula will then back up
553 * over the two eof marks, backup over the record and reread
554 * it to make sure it is valid. Bacula can skip this validation
555 * if you set "Backward space record = no"
557 static int re_read_block_test()
564 if (!(dev->capabilities & CAP_BSR)) {
565 Pmsg0(-1, _("Skipping read backwards test because BSR turned off.\n"));
569 Pmsg0(-1, _("\n=== Write, backup, and re-read test ===\n\n"
570 "I'm going to write three records and two eof's\n"
571 "then backup over the eof's and re-read the last record.\n"
572 "Bacula does this after writing the last block on the\n"
573 "tape to verify that the block was written correctly.\n"
574 "It is not an *essential* feature ...\n\n"));
576 block = new_block(dev);
578 rec->data = check_pool_memory_size(rec->data, block->buf_len);
579 len = rec->data_len = block->buf_len-100;
580 memset(rec->data, 1, rec->data_len);
581 if (!write_record_to_block(block, rec)) {
582 Pmsg0(0, _("Error writing record to block.\n"));
585 if (!write_block_to_dev(jcr, dev, block)) {
586 Pmsg0(0, _("Error writing block to device.\n"));
589 Pmsg1(0, _("Wrote first record of %d bytes.\n"), rec->data_len);
591 memset(rec->data, 2, rec->data_len);
592 if (!write_record_to_block(block, rec)) {
593 Pmsg0(0, _("Error writing record to block.\n"));
596 if (!write_block_to_dev(jcr, dev, block)) {
597 Pmsg0(0, _("Error writing block to device.\n"));
600 Pmsg1(0, _("Wrote second record of %d bytes.\n"), rec->data_len);
602 memset(rec->data, 3, rec->data_len);
603 if (!write_record_to_block(block, rec)) {
604 Pmsg0(0, _("Error writing record to block.\n"));
607 if (!write_block_to_dev(jcr, dev, block)) {
608 Pmsg0(0, _("Error writing block to device.\n"));
611 Pmsg1(0, _("Wrote third record of %d bytes.\n"), rec->data_len);
615 if (bsf_dev(dev, 1) != 0) {
616 Pmsg1(0, _("Back space file failed! ERR=%s\n"), strerror(dev->dev_errno));
619 if (bsf_dev(dev, 1) != 0) {
620 Pmsg1(0, _("Back space file failed! ERR=%s\n"), strerror(dev->dev_errno));
623 Pmsg0(0, "Backspaced over two EOFs OK.\n");
624 if (bsr_dev(dev, 1) != 0) {
625 Pmsg1(0, _("Back space record failed! ERR=%s\n"), strerror(dev->dev_errno));
628 Pmsg0(0, "Backspace record OK.\n");
629 if (!read_block_from_dev(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
630 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
633 memset(rec->data, 0, rec->data_len);
634 if (!read_record_from_block(block, rec)) {
635 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
638 for (int i=0; i<len; i++) {
639 if (rec->data[i] != 3) {
640 Pmsg0(0, _("Bad data in record. Test failed!\n"));
644 Pmsg0(0, _("\nBlock re-read correct. Test succeeded!\n"));
645 Pmsg0(-1, _("=== End Write, backup, and re-read test ===\n\n"));
653 Pmsg0(0, _("This is not terribly serious since Bacula only uses\n"
654 "this function to verify the last block written to the\n"
655 "tape. Bacula will skip the last block verification\n"
657 "Backward Space Record = No\n\n"
658 "to your Storage daemon's Device resource definition.\n"));
664 * This test writes some records, then writes an end of file,
665 * rewinds the tape, moves to the end of the data and attepts
666 * to append to the tape. This function is essential for
667 * Bacula to be able to write multiple jobs to the tape.
669 static int append_test()
671 Pmsg0(-1, _("\n\n=== Append files test ===\n\n"
672 "This test is essential to Bacula.\n\n"
673 "I'm going to write one record in file 0,\n"
674 " two records in file 1,\n"
675 " and three records in file 2\n\n"));
678 weofcmd(); /* end file 0 */
681 weofcmd(); /* end file 1 */
685 weofcmd(); /* end file 2 */
687 Pmsg0(0, _("Now moving to end of medium.\n"));
689 Pmsg2(-1, _("We should be in file 3. I am at file %d. This is %s\n"),
690 dev->file, dev->file == 3 ? "correct!" : "NOT correct!!!!");
692 if (dev->file != 3) {
696 Pmsg0(-1, _("\nNow the important part, I am going to attempt to append to the tape.\n\n"));
700 Pmsg0(-1, _("Done appending, there should be no I/O errors\n\n"));
701 Pmsg0(-1, "Doing Bacula scan of blocks:\n");
703 Pmsg0(-1, _("End scanning the tape.\n"));
704 Pmsg2(-1, _("We should be in file 4. I am at file %d. This is %s\n"),
705 dev->file, dev->file == 4 ? "correct!" : "NOT correct!!!!");
707 if (dev->file != 4) {
715 * This is a general test of Bacula's functions
716 * needed to read and write the tape.
718 static void testcmd()
722 stat = append_test();
723 if (stat == 1) { /* OK get out */
726 if (stat == -1) { /* first test failed */
727 if (dev_cap(dev, CAP_EOM)) {
728 Pmsg0(-1, "\nAppend test failed. Attempting again.\n"
729 "Setting \"Hardware End of Medium = no\" and retrying append test.\n\n");
730 dev->capabilities &= ~CAP_EOM; /* turn off eom */
731 stat = append_test();
733 Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n"
734 " Hardware End of Medium = No\n\n"
735 "to your Device resource in the Storage conf file.\n");
739 Pmsg0(-1, "\n\nThat appears not to have corrected the problem.\n");
742 /* Wrong count after append */
744 Pmsg0(-1, "\n\nIt looks like the append failed. Attempting again.\n"
745 "Setting \"BSF at EOM = yes\" and retrying append test.\n");
746 dev->capabilities |= CAP_BSFATEOM; /* backspace on eom */
747 stat = append_test();
749 Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n"
750 " Hardware End of Medium = No\n"
751 " BSR at EOM = yes\n\n"
752 "to your Device resource in the Storage conf file.\n");
757 Pmsg0(-1, "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
758 "Unable to correct the problem. You MUST fix this\n"
759 "problem before Bacula can use your tape drive correctly\n");
760 Pmsg0(-1, "\nPerhaps running Bacula in fixed block mode will work.\n"
761 "Do so by setting:\n\n"
762 "Minimum Block Size = nnn\n"
763 "Maximum Block Size = nnn\n\n"
764 "in your Storage daemon's Device definition.\n"
765 "nnn must match your tape driver's block size.\n"
766 "This, however, is not really an ideal solution.\n");
771 Pmsg0(-1, _("\nThe above Bacula scan should have output identical to what follows.\n"
772 "Please double check it ...\n"
773 "=== Sample correct output ===\n"
774 "1 block of 64448 bytes in file 1\n"
775 "End of File mark.\n"
776 "2 blocks of 64448 bytes in file 2\n"
777 "End of File mark.\n"
778 "3 blocks of 64448 bytes in file 3\n"
779 "End of File mark.\n"
780 "1 block of 64448 bytes in file 4\n"
781 "End of File mark.\n"
782 "Total files=4, blocks=7, bytes = 451136\n"
783 "=== End sample correct output ===\n\n"));
785 Pmsg0(-1, _("If the above scan output is not identical to the\n"
786 "sample output, you MUST correct the problem\n"
787 "or Bacula will not be able to write multiple Jobs to \n"
791 re_read_block_test();
794 Pmsg0(-1, _("\n=== End Append files test ===\n"));
798 /* Forward space a file */
801 if (!fsf_dev(dev, 1)) {
802 Pmsg1(0, "Bad status from fsf. ERR=%s\n", strerror_dev(dev));
805 Pmsg0(0, "Forward spaced one file.\n");
808 /* Forward space a record */
813 if ((stat=fsr_dev(dev, 1)) < 0) {
814 Pmsg2(0, "Bad status from fsr %d. ERR=%s\n", stat, strerror_dev(dev));
817 Pmsg0(0, "Forward spaced one record.\n");
822 * Write a Bacula block to the tape
830 sm_check(__FILE__, __LINE__, False);
831 block = new_block(dev);
833 dump_block(block, "test");
835 i = block->buf_len - 100;
837 rec->data = check_pool_memory_size(rec->data, i);
838 memset(rec->data, i & 0xFF, i);
840 sm_check(__FILE__, __LINE__, False);
841 if (!write_record_to_block(block, rec)) {
842 Pmsg0(0, _("Error writing record to block.\n"));
845 if (!write_block_to_dev(jcr, dev, block)) {
846 Pmsg0(0, _("Error writing block to device.\n"));
849 Pmsg1(0, _("Wrote one record of %d bytes.\n"), i);
851 Pmsg0(0, _("Wrote block to device.\n"));
854 sm_check(__FILE__, __LINE__, False);
857 sm_check(__FILE__, __LINE__, False);
861 * Read a record from the tape
868 if (!get_cmd("Enter length to read: ")) {
872 if (len < 0 || len > 1000000) {
873 Pmsg0(0, _("Bad length entered, using default of 1024 bytes.\n"));
876 buf = (char *)malloc(len);
877 stat = read(dev->fd, buf, len);
878 if (stat > 0 && stat <= len) {
881 Pmsg3(0, _("Read of %d bytes gives stat=%d. ERR=%s\n"),
882 len, stat, strerror(errno));
888 * Scan tape by reading block by block. Report what is
889 * on the tape. Note, this command does raw reads, and as such
890 * will not work with fixed block size devices.
892 static void scancmd()
895 int blocks, tot_blocks, tot_files;
901 blocks = block_size = tot_blocks = 0;
903 if (dev->state & ST_EOT) {
904 Pmsg0(0, "End of tape\n");
908 tot_files = dev->file;
909 Pmsg1(0, _("Starting scan at file %u\n"), dev->file);
911 if ((stat = read(dev->fd, buf, sizeof(buf))) < 0) {
912 clrerror_dev(dev, -1);
913 Mmsg2(&dev->errmsg, "read error on %s. ERR=%s.\n",
914 dev->dev_name, strerror(dev->dev_errno));
915 Pmsg2(0, "Bad status from read %d. ERR=%s\n", stat, strerror_dev(dev));
917 printf("%d block%s of %d bytes in file %d\n",
918 blocks, blocks>1?"s":"", block_size, dev->file);
921 Dmsg1(200, "read status = %d\n", stat);
923 if (stat != block_size) {
926 printf("%d block%s of %d bytes in file %d\n",
927 blocks, blocks>1?"s":"", block_size, dev->file);
932 if (stat == 0) { /* EOF */
934 printf("End of File mark.\n");
935 /* Two reads of zero means end of tape */
936 if (dev->state & ST_EOF)
937 dev->state |= ST_EOT;
939 dev->state |= ST_EOF;
942 if (dev->state & ST_EOT) {
943 printf("End of tape\n");
946 } else { /* Got data */
947 dev->state &= ~ST_EOF;
954 tot_files = dev->file - tot_files;
955 printf("Total files=%d, blocks=%d, bytes = %s\n", tot_files, tot_blocks,
956 edit_uint64_with_commas(bytes, ec1));
961 * Scan tape by reading Bacula block by block. Report what is
962 * on the tape. This function reads Bacula blocks, so if your
963 * Device resource is correctly defined, it should work with
964 * either variable or fixed block sizes.
966 static void scan_blocks()
968 int blocks, tot_blocks, tot_files;
974 block = new_block(dev);
975 blocks = block_size = tot_blocks = 0;
979 tot_files = dev->file;
981 if (!read_block_from_device(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
982 Dmsg1(100, "!read_block(): ERR=%s\n", strerror_dev(dev));
983 if (dev->state & ST_EOT) {
985 printf("%d block%s of %d bytes in file %d\n",
986 blocks, blocks>1?"s":"", block_size, dev->file);
991 if (dev->state & ST_EOF) {
993 printf("%d block%s of %d bytes in file %d\n",
994 blocks, blocks>1?"s":"", block_size, dev->file);
997 printf(_("End of File mark.\n"));
1000 if (dev->state & ST_SHORT) {
1002 printf("%d block%s of %d bytes in file %d\n",
1003 blocks, blocks>1?"s":"", block_size, dev->file);
1006 printf(_("Short block read.\n"));
1009 printf(_("Error reading block. ERR=%s\n"), strerror_dev(dev));
1012 if (block->block_len != block_size) {
1014 printf("%d block%s of %d bytes in file %d\n",
1015 blocks, blocks>1?"s":"", block_size, dev->file);
1018 block_size = block->block_len;
1022 bytes += block->block_len;
1023 Dmsg5(100, "Blk=%u blen=%u bVer=%d SessId=%u SessTim=%u\n",
1024 block->BlockNumber, block->block_len, block->BlockVer,
1025 block->VolSessionId, block->VolSessionTime);
1027 DEV_RECORD *rec = new_record();
1028 read_record_from_block(block, rec);
1029 Pmsg7(-1, "Block: %u blen=%u First rec FI=%s SessId=%u SessTim=%u Strm=%s rlen=%d\n",
1030 block->BlockNumber, block->block_len,
1031 FI_to_ascii(rec->FileIndex), rec->VolSessionId, rec->VolSessionTime,
1032 stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len);
1035 } else if (verbose > 1) {
1036 dump_block(block, "");
1042 tot_files = dev->file - tot_files;
1043 printf("Total files=%d, blocks=%d, bytes = %s\n", tot_files, tot_blocks,
1044 edit_uint64_with_commas(bytes, ec1));
1048 static void statcmd()
1054 debug = debug_level;
1056 if (!status_dev(dev, &status)) {
1057 Pmsg2(0, "Bad status from status %d. ERR=%s\n", stat, strerror_dev(dev));
1060 dump_volume_label(dev);
1062 debug_level = debug;
1067 * First we label the tape, then we fill
1068 * it with data get a new tape and write a few blocks.
1070 static void fillcmd()
1081 This command simulates Bacula writing to a tape.\n\
1082 It requires either one or two blank tapes, which it\n\
1083 will label and write. It will print a status approximately\n\
1084 every 322 MB, and write an EOF every 3.2 GB. If you have\n\
1085 selected the simple test option, after writing the first tape\n\
1086 it will rewind it and re-read the last block written.\n\
1087 If you have selected the multiple tape test, when the first tape\n\
1088 fills, it will ask for a second, and after writing a few more \n\
1089 blocks, it will stop. Then it will begin re-reading the\n\
1091 This may take a long time -- hours! ...\n\n");
1093 get_cmd("Insert a blank tape then indicate if you want\n"
1094 "to run the simplified test (s) with one tape or\n"
1095 "the complete multiple tape (m) test: (s/m) ");
1096 if (cmd[0] == 's') {
1097 Pmsg0(-1, "Simple test (single tape) selected.\n");
1099 } else if (cmd[0] == 'm') {
1100 Pmsg0(-1, "Complete multiple tape test selected.\n");
1103 Pmsg0(000, "Command aborted.\n");
1107 set_volume_name("TestVolume1", 1);
1112 Dmsg1(20, "Begin append device=%s\n", dev_name(dev));
1114 block = new_block(dev);
1117 * Acquire output device for writing. Note, after acquiring a
1118 * device, we MUST release it, which is done at the end of this
1121 Dmsg0(100, "just before acquire_device\n");
1122 if (!(dev=acquire_device_for_append(jcr, dev, block))) {
1123 set_jcr_job_status(jcr, JS_ErrorTerminated);
1128 Dmsg0(100, "Just after acquire_device_for_append\n");
1130 * Write Begin Session Record
1132 if (!write_session_label(jcr, block, SOS_LABEL)) {
1133 set_jcr_job_status(jcr, JS_ErrorTerminated);
1134 Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"),
1138 Pmsg0(-1, "Wrote Start Of Session label.\n");
1140 memset(&rec, 0, sizeof(rec));
1141 rec.data = get_memory(100000); /* max record size */
1143 #define REC_SIZE 32768
1144 rec.data_len = REC_SIZE;
1147 * Generate data as if from File daemon, write to device
1149 jcr->VolFirstIndex = 0;
1150 time(&jcr->run_time); /* start counting time for rates */
1151 Pmsg0(-1, "Begin writing records to first tape ...\n");
1152 for (file_index = 0; ok && !job_canceled(jcr); ) {
1153 rec.VolSessionId = jcr->VolSessionId;
1154 rec.VolSessionTime = jcr->VolSessionTime;
1155 rec.FileIndex = ++file_index;
1156 rec.Stream = STREAM_FILE_DATA;
1159 * Fill the buffer with the file_index negated. Negation ensures that
1160 * more bits are turned on.
1162 uint64_t *lp = (uint64_t *)rec.data;
1163 for (uint32_t i=0; i < (rec.data_len-sizeof(uint64_t))/sizeof(uint64_t); i++) {
1164 *lp++ = ~file_index;
1167 Dmsg4(250, "before writ_rec FI=%d SessId=%d Strm=%s len=%d\n",
1168 rec.FileIndex, rec.VolSessionId, stream_to_ascii(rec.Stream, rec.FileIndex),
1171 while (!write_record_to_block(block, &rec)) {
1173 * When we get here we have just filled a block
1175 Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec.data_len,
1178 /* Write block to tape */
1179 if (!flush_block(block, 1)) {
1183 /* Every 5000 blocks (approx 322MB) report where we are.
1185 if ((block->BlockNumber % 5000) == 0) {
1187 now -= jcr->run_time;
1191 kbs = (double)dev->VolCatInfo.VolCatBytes / (1000.0 * (double)now);
1192 Pmsg3(-1, "Wrote block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
1193 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), (float)kbs);
1195 /* Every 15000 blocks (approx 1GB) write an EOF.
1197 if ((block->BlockNumber % 15000) == 0) {
1198 Pmsg0(-1, "Flush block, write EOF\n");
1199 flush_block(block, 0);
1203 /* Get out after writing 10 blocks to the second tape */
1204 if (++BlockNumber > 10 && stop != 0) { /* get out */
1209 Pmsg0(000, _("Not OK\n"));
1212 jcr->JobBytes += rec.data_len; /* increment bytes this job */
1213 Dmsg4(190, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
1214 FI_to_ascii(rec.FileIndex), rec.VolSessionId,
1215 stream_to_ascii(rec.Stream, rec.FileIndex), rec.data_len);
1217 /* Get out after writing 10 blocks to the second tape */
1218 if (BlockNumber > 10 && stop != 0) { /* get out */
1219 Pmsg0(-1, "Done writing ...\n");
1224 Dmsg0(100, "Write_end_session_label()\n");
1225 /* Create Job status for end of session label */
1226 if (!job_canceled(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(-1, _("Set ok=FALSE after write_block_to_device.\n"));
1240 Pmsg0(-1, "Wrote End Of Session label.\n");
1243 /* Release the device */
1244 if (!release_device(jcr, dev)) {
1245 Pmsg0(-1, _("Error in release_device\n"));
1250 free_memory(rec.data);
1251 Pmsg0(-1, _("\n\nDone filling tape. Now beginning re-read of tape ...\n"));
1253 dump_block(last_block, _("Last block written to tape.\n"));
1259 * Read two tapes written by the "fill" command and ensure
1260 * that the data is valid. If stop==1 we simulate full read back
1261 * of two tapes. If stop==-1 we simply read the last block and
1262 * verify that it is correct.
1264 static void unfillcmd()
1271 block = new_block(dev);
1273 dev->capabilities |= CAP_ANONVOLS; /* allow reading any volume */
1274 dev->capabilities &= ~CAP_LABEL; /* don't label anything here */
1279 /* Close device so user can use autochanger if desired */
1280 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
1283 force_close_dev(dev);
1284 get_cmd(_("Mount first tape. Press enter when ready: "));
1287 set_volume_name("TestVolume1", 1);
1289 create_vol_list(jcr);
1291 dev->state &= ~ST_READ;
1292 if (!acquire_device_for_read(jcr, dev, block)) {
1293 Pmsg1(-1, "%s", dev->errmsg);
1298 time(&jcr->run_time); /* start counting time for rates */
1302 /* Read all records and then second tape */
1303 read_records(jcr, dev, record_cb, my_mount_next_read_volume);
1306 * Simplified test, we simply fsf to file, then read the
1307 * last block and make sure it is the same as the saved block.
1309 Pmsg0(000, "Rewinding tape ...\n");
1310 if (!rewind_dev(dev)) {
1311 Pmsg1(-1, _("Error rewinding: ERR=%s\n"), strerror_dev(dev));
1314 if (last_file > 0) {
1315 Pmsg1(000, "Forward spacing to last file=%u\n", last_file);
1316 if (!fsf_dev(dev, last_file)) {
1317 Pmsg1(-1, _("Error in FSF: ERR=%s\n"), strerror_dev(dev));
1321 Pmsg1(-1, _("Forward space to file %u complete. Reading blocks ...\n"),
1323 Pmsg1(-1, _("Now reading to block %u.\n"), last_block_num);
1324 for (uint32_t i=0; i <= last_block_num; i++) {
1325 if (!read_block_from_device(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
1326 Pmsg1(-1, _("Error reading blocks: ERR=%s\n"), strerror_dev(dev));
1327 Pmsg2(-1, _("Wanted block %u error at block %u\n"), last_block_num, i);
1330 if (i > 0 && i % 1000 == 0) {
1331 Pmsg1(-1, _("At block %u\n"), i);
1335 dump_block(last_block, _("Last block written"));
1336 dump_block(block, _("Block read back"));
1337 Pmsg0(-1, _("Except for the buffer address, the contents of\n"
1338 "the above two block dumps should be the same.\n"
1339 "If not you have a problem ...\n"));
1346 Pmsg0(000, _("Done with reread of fill data.\n"));
1350 * We are called here from "unfill" for each record on the tape.
1352 static void record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
1354 SESSION_LABEL label;
1356 if (stop > 1 && !dumped) { /* on second tape */
1358 dump_block(block, "First block on second tape");
1359 Pmsg4(-1, "Blk: FileIndex=%d: block=%u size=%d vol=%s\n",
1360 rec->FileIndex, block->BlockNumber, block->block_len, dev->VolHdr.VolName);
1361 Pmsg6(-1, " Rec: VId=%d VT=%d FI=%s Strm=%s len=%d state=%x\n",
1362 rec->VolSessionId, rec->VolSessionTime,
1363 FI_to_ascii(rec->FileIndex), stream_to_ascii(rec->Stream, rec->FileIndex),
1364 rec->data_len, rec->state);
1366 if (rec->FileIndex < 0) {
1368 dump_label_record(dev, rec, 1);
1370 switch (rec->FileIndex) {
1372 Pmsg0(-1, "Volume is prelabeled. This tape cannot be scanned.\n");
1375 unser_volume_label(dev, rec);
1376 Pmsg3(-1, "VOL_LABEL: block=%u size=%d vol=%s\n", block->BlockNumber,
1377 block->block_len, dev->VolHdr.VolName);
1381 unser_session_label(&label, rec);
1382 Pmsg1(-1, "SOS_LABEL: JobId=%u\n", label.JobId);
1385 unser_session_label(&label, rec);
1386 Pmsg2(-1, "EOS_LABEL: block=%u JobId=%u\n", block->BlockNumber,
1390 Pmsg0(-1, "EOM_LABEL:\n");
1392 case EOT_LABEL: /* end of all tapes */
1395 if (LastBlock != block->BlockNumber) {
1396 VolBytes += block->block_len;
1398 LastBlock = block->BlockNumber;
1400 now -= jcr->run_time;
1404 kbs = (double)VolBytes / (1000 * now);
1405 Pmsg3(000, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
1406 edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
1408 Pmsg0(000, "End of all tapes.\n");
1416 if (++file_index != rec->FileIndex) {
1417 Pmsg3(000, "Incorrect FileIndex in Block %u. Got %d, expected %d.\n",
1418 block->BlockNumber, rec->FileIndex, file_index);
1421 * Now check that the right data is in the record.
1423 uint64_t *lp = (uint64_t *)rec->data;
1424 uint64_t val = ~file_index;
1425 for (uint32_t i=0; i < (REC_SIZE-sizeof(uint64_t))/sizeof(uint64_t); i++) {
1427 Pmsg2(000, "Record %d contains bad data in Block %u.\n",
1428 file_index, block->BlockNumber);
1433 if (LastBlock != block->BlockNumber) {
1434 VolBytes += block->block_len;
1436 if ((block->BlockNumber != LastBlock) && (block->BlockNumber % 50000) == 0) {
1439 now -= jcr->run_time;
1443 kbs = (double)VolBytes / (1000 * now);
1444 Pmsg3(000, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
1445 edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
1447 LastBlock = block->BlockNumber;
1449 Pmsg1(000, "End of all blocks. Block=%u\n", block->BlockNumber);
1456 * Write current block to tape regardless of whether or
1457 * not it is full. If the tape fills, attempt to
1458 * acquire another tape.
1460 static int flush_block(DEV_BLOCK *block, int dump)
1465 uint32_t this_file, this_block_num;
1468 this_block = new_block(dev);
1471 free_memory(this_block->buf);
1472 memcpy(this_block, block, sizeof(DEV_BLOCK));
1473 this_block->buf = get_memory(block->buf_len);
1474 memcpy(this_block->buf, block->buf, this_block->buf_len);
1475 this_file = dev->file;
1476 this_block_num = dev->block_num;
1477 if (!write_block_to_dev(jcr, dev, block)) {
1478 Pmsg0(000, strerror_dev(dev));
1479 Pmsg3(000, "Block not written: FileIndex=%u Block=%u Size=%u\n",
1480 (unsigned)file_index, block->BlockNumber, block->block_len);
1481 Pmsg2(000, "last_block_num=%u this_block_num=%d\n", last_block_num,
1484 dump_block(block, "Block not written");
1487 eot_block = block->BlockNumber;
1488 eot_block_len = block->block_len;
1489 eot_FileIndex = file_index;
1492 now -= jcr->run_time;
1496 kbs = (double)dev->VolCatInfo.VolCatBytes / (1000 * now);
1497 vol_size = dev->VolCatInfo.VolCatBytes;
1498 Pmsg2(000, "End of tape. VolumeCapacity=%s. Write rate = %.1f KB/s\n",
1499 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), kbs);
1502 stop = -1; /* stop, but do simplified test */
1504 /* Full test in progress */
1505 if (!fixup_device_block_write_error(jcr, dev, block)) {
1506 Pmsg1(000, _("Cannot fixup device error. %s\n"), strerror_dev(dev));
1512 BlockNumber = 0; /* start counting for second tape */
1515 return 1; /* end of tape reached */
1519 * Toggle between two allocated blocks for efficiency.
1520 * Switch blocks so that the block just successfully written is
1521 * always in last_block.
1523 tblock = last_block;
1524 last_block = this_block;
1525 this_block = tblock;
1526 last_file = this_file;
1527 last_block_num = this_block_num;
1535 * First we label the tape, then we fill
1536 * it with data get a new tape and write a few blocks.
1538 static void qfillcmd()
1544 Pmsg0(0, "Test writing blocks of 64512 bytes to tape.\n");
1546 get_cmd("How many blocks do you want to write? (1000): ");
1553 sm_check(__FILE__, __LINE__, False);
1554 block = new_block(dev);
1557 i = block->buf_len - 100;
1559 rec->data = check_pool_memory_size(rec->data, i);
1560 memset(rec->data, i & 0xFF, i);
1563 Pmsg1(0, "Begin writing %d blocks to tape ...\n", count);
1564 for (i=0; i < count; i++) {
1568 if (!write_record_to_block(block, rec)) {
1569 Pmsg0(0, _("Error writing record to block.\n"));
1572 if (!write_block_to_dev(jcr, dev, block)) {
1573 Pmsg0(0, _("Error writing block to device.\n"));
1584 sm_check(__FILE__, __LINE__, False);
1587 sm_check(__FILE__, __LINE__, False);
1591 static void rawfill_cmd()
1596 uint32_t block_num = 0;
1600 block = new_block(dev);
1601 fd = open("/dev/urandom", O_RDONLY);
1603 read(fd, block->buf, block->buf_len);
1605 Pmsg0(0, "Cannot open /dev/urandom.\n");
1609 p = (uint32_t *)block->buf;
1610 Pmsg1(0, "Begin writing blocks of %u bytes.\n", block->buf_len);
1613 stat = write(dev->fd, block->buf, block->buf_len);
1614 if (stat == (int)block->buf_len) {
1615 if ((block_num++ % 100) == 0) {
1625 printf("Write failed at block %u. stat=%d ERR=%s\n", block_num, stat,
1626 strerror(my_errno));
1631 struct cmdstruct { char *key; void (*func)(); char *help; };
1632 static struct cmdstruct commands[] = {
1633 {"bsf", bsfcmd, "backspace file"},
1634 {"bsr", bsrcmd, "backspace record"},
1635 {"cap", capcmd, "list device capabilities"},
1636 {"clear", clearcmd, "clear tape errors"},
1637 {"eod", eodcmd, "go to end of Bacula data for append"},
1638 {"eom", eomcmd, "go to the physical end of medium"},
1639 {"fill", fillcmd, "fill tape, write onto second volume"},
1640 {"unfill", unfillcmd, "read filled tape"},
1641 {"fsf", fsfcmd, "forward space a file"},
1642 {"fsr", fsrcmd, "forward space a record"},
1643 {"help", helpcmd, "print this command"},
1644 {"label", labelcmd, "write a Bacula label to the tape"},
1645 {"load", loadcmd, "load a tape"},
1646 {"quit", quitcmd, "quit btape"},
1647 {"rawfill", rawfill_cmd, "use write() to fill tape"},
1648 {"readlabel", readlabelcmd, "read and print the Bacula tape label"},
1649 {"rectest", rectestcmd, "test record handling functions"},
1650 {"rewind", rewindcmd, "rewind the tape"},
1651 {"scan", scancmd, "read() tape block by block to EOT and report"},
1652 {"scanblocks", scan_blocks, "Bacula read block by block to EOT and report"},
1653 {"status", statcmd, "print tape status"},
1654 {"test", testcmd, "General test Bacula tape functions"},
1655 {"weof", weofcmd, "write an EOF on the tape"},
1656 {"wr", wrcmd, "write a single Bacula block"},
1657 {"rr", rrcmd, "read a single record"},
1658 {"qfill", qfillcmd, "quick fill command"},
1660 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
1668 while (get_cmd("*")) {
1669 sm_check(__FILE__, __LINE__, False);
1671 for (i=0; i<comsize; i++) /* search for command */
1672 if (fstrsch(cmd, commands[i].key)) {
1673 (*commands[i].func)(); /* go execute command */
1678 Pmsg1(0, _("%s is an illegal command\n"), cmd);
1684 static void helpcmd()
1688 printf(_(" Command Description\n ======= ===========\n"));
1689 for (i=0; i<comsize; i++)
1690 printf(" %-10s %s\n", commands[i].key, commands[i].help);
1697 "\nVersion: " VERSION " (" BDATE ")\n\n"
1698 "Usage: btape [-c config_file] [-d debug_level] [device_name]\n"
1699 " -c <file> set configuration file to file\n"
1700 " -dnn set debug level to nn\n"
1701 " -s turn off signals\n"
1702 " -t open the default tape device\n"
1703 " -? print this message.\n"
1709 * Get next input command from terminal. This
1710 * routine is REALLY primitive, and should be enhanced
1711 * to have correct backspacing, etc.
1714 get_cmd(char *prompt)
1718 fprintf(stdout, prompt);
1720 /* We really should turn off echoing and pretty this
1724 while ((ch = fgetc(stdin)) != EOF) {
1726 strip_trailing_junk(cmd);
1728 } else if (ch == 4 || ch == 0xd3 || ch == 0x8) {
1741 /* Dummies to replace askdir.c */
1742 int dir_get_volume_info(JCR *jcr, enum get_vol_info_rw writing) { return 1;}
1743 int dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
1744 int dir_create_jobmedia_record(JCR *jcr) { return 1; }
1745 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
1746 int dir_send_job_status(JCR *jcr) {return 1;}
1750 int dir_find_next_appendable_volume(JCR *jcr)
1755 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
1757 /* Close device so user can use autochanger if desired */
1758 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
1761 force_close_dev(dev);
1762 Pmsg1(-1, "%s", dev->errmsg); /* print reason */
1763 fprintf(stderr, "Mount Volume \"%s\" on device %s and press return when ready: ",
1764 jcr->VolumeName, dev_name(dev));
1769 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev)
1771 /* Close device so user can use autochanger if desired */
1772 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
1775 force_close_dev(dev);
1776 fprintf(stderr, "Mount next Volume on device %s and press return when ready: ",
1779 set_volume_name("TestVolume2", 2);
1787 static int my_mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
1791 Pmsg1(000, "End of Volume \"%s\"\n", jcr->VolumeName);
1793 if (LastBlock != block->BlockNumber) {
1794 VolBytes += block->block_len;
1796 LastBlock = block->BlockNumber;
1798 now -= jcr->run_time;
1802 kbs = (double)VolBytes / (1000.0 * (double)now);
1803 Pmsg3(-1, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
1804 edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
1806 if (strcmp(jcr->VolumeName, "TestVolume2") == 0) {
1812 set_volume_name("TestVolume2", 2);
1814 create_vol_list(jcr);
1816 dev->state &= ~ST_READ;
1817 if (!acquire_device_for_read(jcr, dev, block)) {
1818 Pmsg2(0, "Cannot open Dev=%s, Vol=%s\n", dev_name(dev), jcr->VolumeName);
1821 return 1; /* next volume mounted */
1824 static void set_volume_name(char *VolName, int volnum)
1826 VolumeName = VolName;
1828 pm_strcpy(&jcr->VolumeName, VolName);
1829 bstrncpy(dev->VolCatInfo.VolCatName, VolName, sizeof(dev->VolCatInfo.VolCatName));