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 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();
69 static void set_volume_name(char *VolName, int volnum);
72 /* Static variables */
73 #define CONFIG_FILE "bacula-sd.conf"
76 static BSR *bsr = NULL;
77 static char cmd[1000];
78 static int signals = TRUE;
81 static uint64_t vol_size;
82 static uint64_t VolBytes;
85 static long file_index;
86 static int end_of_tape = 0;
87 static uint32_t LastBlock = 0;
88 static uint32_t eot_block;
89 static uint32_t eot_block_len;
90 static uint32_t eot_FileIndex;
91 static int dumped = 0;
92 static DEV_BLOCK *last_block = NULL;
93 static DEV_BLOCK *this_block = NULL;
94 static uint32_t last_file = 0;
95 static uint32_t last_block_num = 0;
96 static uint32_t BlockNumber = 0;
97 static int simple = FALSE;
99 static char *VolumeName = NULL;
102 static JCR *jcr = NULL;
106 static void terminate_btape(int sig);
107 int get_cmd(char *prompt);
110 int write_dev(DEVICE *dev, char *buf, size_t len)
112 Emsg0(M_ABORT, 0, "write_dev not implemented.\n");
116 int read_dev(DEVICE *dev, char *buf, size_t len)
118 Emsg0(M_ABORT, 0, "read_dev not implemented.\n");
123 /*********************************************************************
125 * Main Bacula Pool Creation Program
128 int main(int argc, char *argv[])
134 if (TAPE_BSIZE % DEV_BSIZE != 0 || TAPE_BSIZE / DEV_BSIZE == 0) {
135 Emsg2(M_ABORT, 0, "Tape block size (%d) not multiple of system size (%d)\n",
136 TAPE_BSIZE, DEV_BSIZE);
138 if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
139 Emsg1(M_ABORT, 0, "Tape block size (%d) is not a power of 2\n", TAPE_BSIZE);
142 printf("Tape block granularity is %d bytes.\n", TAPE_BSIZE);
144 working_directory = "/tmp";
145 my_name_is(argc, argv, "btape");
146 init_msg(NULL, NULL);
148 while ((ch = getopt(argc, argv, "b:c:d:sv?")) != -1) {
150 case 'b': /* bootstrap file */
151 bsr = parse_bsr(NULL, optarg);
155 case 'c': /* specify config file */
156 if (configfile != NULL) {
159 configfile = bstrdup(optarg);
162 case 'd': /* set debug level */
163 debug_level = atoi(optarg);
164 if (debug_level <= 0) {
190 init_signals(terminate_btape);
193 if (configfile == NULL) {
194 configfile = bstrdup(CONFIG_FILE);
197 daemon_start_time = time(NULL);
199 parse_config(configfile);
202 /* See if we can open a device */
204 Pmsg0(000, "No archive name specified.\n");
207 } else if (argc != 1) {
208 Pmsg0(000, "Improper number of arguments specified.\n");
213 jcr = setup_jcr("btape", argv[0], bsr, NULL);
214 dev = setup_to_access_device(jcr, 0); /* acquire for write */
218 block = new_block(dev);
220 if (!(dev->state & ST_OPENED)) {
221 Dmsg0(129, "Opening device.\n");
222 if (open_dev(dev, jcr->VolumeName, READ_WRITE) < 0) {
223 Emsg1(M_FATAL, 0, _("dev open failed: %s\n"), dev->errmsg);
229 Dmsg1(129, "open_dev %s OK\n", dev_name(dev));
233 Dmsg0(200, "Do tape commands\n");
241 static void terminate_btape(int stat)
244 sm_check(__FILE__, __LINE__, False);
248 free_config_resources();
254 if (debug_level > 10)
255 print_memory_pool_stats();
265 free_block(last_block);
268 free_block(this_block);
272 close_memory_pool(); /* free memory in pool */
284 * Write a label to the tape
286 static void labelcmd()
292 for (device=NULL; (device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device)); ) {
293 if (strcmp(device->device_name, dev->dev_name) == 0) {
294 jcr->device = device; /* Arggg a bit of duplication here */
296 dev->device = device;
303 Pmsg2(0, "Could not find device %s in %s\n", dev->dev_name, configfile);
308 strcpy(cmd, VolumeName);
310 if (!get_cmd("Enter Volume Name: ")) {
315 if (!(dev->state & ST_OPENED)) {
316 if (!open_device(dev)) {
317 Pmsg1(0, "Device open failed. ERR=%s\n", strerror_dev(dev));
320 write_volume_label_to_dev(jcr, device, cmd, "Default");
324 * Read the tape label
326 static void readlabelcmd()
328 int save_debug_level = debug_level;
332 block = new_block(dev);
333 stat = read_dev_volume_label(jcr, dev, block);
336 Pmsg0(0, "Volume has no label.\n");
339 Pmsg0(0, "Volume label read correctly.\n");
342 Pmsg1(0, "I/O error on device: ERR=%s", strerror_dev(dev));
345 Pmsg0(0, "Volume name error\n");
347 case VOL_CREATE_ERROR:
348 Pmsg1(0, "Error creating label. ERR=%s", strerror_dev(dev));
350 case VOL_VERSION_ERROR:
351 Pmsg0(0, "Volume version error.\n");
353 case VOL_LABEL_ERROR:
354 Pmsg0(0, "Bad Volume label type.\n");
357 Pmsg0(0, "Unknown error.\n");
362 dump_volume_label(dev);
363 debug_level = save_debug_level;
369 * Load the tape should have prevously been taken
370 * off line, otherwise this command is not necessary.
372 static void loadcmd()
375 if (!load_dev(dev)) {
376 Pmsg1(0, "Bad status from load. ERR=%s\n", strerror_dev(dev));
378 Pmsg1(0, "Loaded %s\n", dev_name(dev));
384 static void rewindcmd()
386 if (!rewind_dev(dev)) {
387 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
388 clrerror_dev(dev, -1);
390 Pmsg1(0, "Rewound %s\n", dev_name(dev));
395 * Clear any tape error
397 static void clearcmd()
399 clrerror_dev(dev, -1);
403 * Write and end of file on the tape
405 static void weofcmd()
409 if ((stat = weof_dev(dev, 1)) < 0) {
410 Pmsg2(0, "Bad status from weof %d. ERR=%s\n", stat, strerror_dev(dev));
413 Pmsg1(0, "Wrote EOF to %s\n", dev_name(dev));
418 /* Go to the end of the medium -- raw command
419 * The idea was orginally that the end of the Bacula
420 * medium would be flagged differently. This is not
421 * currently the case. So, this is identical to the
427 Pmsg1(0, _("Bad status from eod. ERR=%s\n"), strerror_dev(dev));
430 Pmsg0(0, _("Moved to end of media\n"));
435 * Go to the end of the media (either hardware determined
436 * or defined by two eofs.
450 if ((stat=bsf_dev(dev, 1)) < 0) {
451 Pmsg1(0, _("Bad status from bsf. ERR=%s\n"), strerror(errno));
453 Pmsg0(0, _("Back spaced one file.\n"));
464 if ((stat=bsr_dev(dev, 1)) < 0) {
465 Pmsg1(0, _("Bad status from bsr. ERR=%s\n"), strerror(errno));
467 Pmsg0(0, _("Back spaced one record.\n"));
472 * List device capabilities as defined in the
477 printf(_("Device capabilities:\n"));
478 printf("%sEOF ", dev->capabilities & CAP_EOF ? "" : "!");
479 printf("%sBSR ", dev->capabilities & CAP_BSR ? "" : "!");
480 printf("%sBSF ", dev->capabilities & CAP_BSF ? "" : "!");
481 printf("%sFSR ", dev->capabilities & CAP_FSR ? "" : "!");
482 printf("%sFSF ", dev->capabilities & CAP_FSF ? "" : "!");
483 printf("%sEOM ", dev->capabilities & CAP_EOM ? "" : "!");
484 printf("%sREM ", dev->capabilities & CAP_REM ? "" : "!");
485 printf("%sRACCESS ", dev->capabilities & CAP_RACCESS ? "" : "!");
486 printf("%sAUTOMOUNT ", dev->capabilities & CAP_AUTOMOUNT ? "" : "!");
487 printf("%sLABEL ", dev->capabilities & CAP_LABEL ? "" : "!");
488 printf("%sANONVOLS ", dev->capabilities & CAP_ANONVOLS ? "" : "!");
489 printf("%sALWAYSOPEN ", dev->capabilities & CAP_ALWAYSOPEN ? "" : "!");
492 printf(_("Device status:\n"));
493 printf("%sOPENED ", dev->state & ST_OPENED ? "" : "!");
494 printf("%sTAPE ", dev->state & ST_TAPE ? "" : "!");
495 printf("%sLABEL ", dev->state & ST_LABEL ? "" : "!");
496 printf("%sMALLOC ", dev->state & ST_MALLOC ? "" : "!");
497 printf("%sAPPEND ", dev->state & ST_APPEND ? "" : "!");
498 printf("%sREAD ", dev->state & ST_READ ? "" : "!");
499 printf("%sEOT ", dev->state & ST_EOT ? "" : "!");
500 printf("%sWEOT ", dev->state & ST_WEOT ? "" : "!");
501 printf("%sEOF ", dev->state & ST_EOF ? "" : "!");
502 printf("%sNEXTVOL ", dev->state & ST_NEXTVOL ? "" : "!");
503 printf("%sSHORT ", dev->state & ST_SHORT ? "" : "!");
506 printf(_("Device parameters:\n"));
507 printf("Device name: %s\n", dev->dev_name);
508 printf("File=%u block=%u\n", dev->file, dev->block_num);
509 printf("Min block=%u Max block=%u\n", dev->min_block_size, dev->max_block_size);
517 * Test writting larger and larger records.
518 * This is a torture test for records.
520 static void rectestcmd()
526 Pmsg0(0, "Test writting larger and larger records.\n"
527 "This is a torture test for records.\nI am going to write\n"
528 "larger and larger records. It will stop when the record size\n"
529 "plus the header exceeds the block size (by default about 64K)\n");
532 get_cmd("Do you want to continue? (y/n): ");
534 Pmsg0(000, "Command aborted.\n");
538 sm_check(__FILE__, __LINE__, False);
539 block = new_block(dev);
542 for (i=1; i<500000; i++) {
543 rec->data = check_pool_memory_size(rec->data, i);
544 memset(rec->data, i & 0xFF, i);
546 sm_check(__FILE__, __LINE__, False);
547 if (write_record_to_block(block, rec)) {
550 Pmsg2(0, "Block %d i=%d\n", blkno, i);
554 sm_check(__FILE__, __LINE__, False);
558 sm_check(__FILE__, __LINE__, False);
562 * This test attempts to re-read a block written by Bacula
563 * normally at the end of the tape. Bacula will then back up
564 * over the two eof marks, backup over the record and reread
565 * it to make sure it is valid. Bacula can skip this validation
566 * if you set "Backward space record = no"
568 static int re_read_block_test()
575 if (!(dev->capabilities & CAP_BSR)) {
576 Pmsg0(-1, _("Skipping read backwards test because BSR turned off.\n"));
580 Pmsg0(-1, _("\n=== Write, backup, and re-read test ===\n\n"
581 "I'm going to write three records and two eof's\n"
582 "then backup over the eof's and re-read the last record.\n"
583 "Bacula does this after writing the last block on the\n"
584 "tape to verify that the block was written correctly.\n"
585 "It is not an *essential* feature ...\n\n"));
587 block = new_block(dev);
589 rec->data = check_pool_memory_size(rec->data, block->buf_len);
590 len = rec->data_len = block->buf_len-100;
591 memset(rec->data, 1, 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 first record of %d bytes.\n"), rec->data_len);
602 memset(rec->data, 2, 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 second record of %d bytes.\n"), rec->data_len);
613 memset(rec->data, 3, rec->data_len);
614 if (!write_record_to_block(block, rec)) {
615 Pmsg0(0, _("Error writing record to block.\n"));
618 if (!write_block_to_dev(jcr, dev, block)) {
619 Pmsg0(0, _("Error writing block to device.\n"));
622 Pmsg1(0, _("Wrote third record of %d bytes.\n"), rec->data_len);
626 if (bsf_dev(dev, 1) != 0) {
627 Pmsg1(0, _("Back space file failed! ERR=%s\n"), strerror(dev->dev_errno));
630 if (bsf_dev(dev, 1) != 0) {
631 Pmsg1(0, _("Back space file failed! ERR=%s\n"), strerror(dev->dev_errno));
634 Pmsg0(0, "Backspaced over two EOFs OK.\n");
635 if (bsr_dev(dev, 1) != 0) {
636 Pmsg1(0, _("Back space record failed! ERR=%s\n"), strerror(dev->dev_errno));
639 Pmsg0(0, "Backspace record OK.\n");
640 if (!read_block_from_dev(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
641 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
644 memset(rec->data, 0, rec->data_len);
645 if (!read_record_from_block(block, rec)) {
646 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
649 for (int i=0; i<len; i++) {
650 if (rec->data[i] != 3) {
651 Pmsg0(0, _("Bad data in record. Test failed!\n"));
655 Pmsg0(0, _("\nBlock re-read correct. Test succeeded!\n"));
656 Pmsg0(-1, _("=== End Write, backup, and re-read test ===\n\n"));
664 Pmsg0(0, _("This is not terribly serious since Bacula only uses\n"
665 "this function to verify the last block written to the\n"
666 "tape. Bacula will skip the last block verification\n"
668 "Backward Space Record = No\n\n"
669 "to your Storage daemon's Device resource definition.\n"));
675 * This test writes some records, then writes an end of file,
676 * rewinds the tape, moves to the end of the data and attepts
677 * to append to the tape. This function is essential for
678 * Bacula to be able to write multiple jobs to the tape.
680 static int append_test()
682 Pmsg0(-1, _("\n\n=== Append files test ===\n\n"
683 "This test is essential to Bacula.\n\n"
684 "I'm going to write one record in file 0,\n"
685 " two records in file 1,\n"
686 " and three records in file 2\n\n"));
689 weofcmd(); /* end file 0 */
692 weofcmd(); /* end file 1 */
696 weofcmd(); /* end file 2 */
698 Pmsg0(0, _("Now moving to end of media.\n"));
700 Pmsg2(-1, _("We should be in file 3. I am at file %d. This is %s\n"),
701 dev->file, dev->file == 3 ? "correct!" : "NOT correct!!!!");
703 if (dev->file != 3) {
707 Pmsg0(-1, _("\nNow the important part, I am going to attempt to append to the tape.\n\n"));
711 Pmsg0(-1, _("Done appending, there should be no I/O errors\n\n"));
712 Pmsg0(-1, "Doing Bacula scan of blocks:\n");
714 Pmsg0(-1, _("End scanning the tape.\n"));
715 Pmsg2(-1, _("We should be in file 4. I am at file %d. This is %s\n"),
716 dev->file, dev->file == 4 ? "correct!" : "NOT correct!!!!");
718 if (dev->file != 4) {
726 * This is a general test of Bacula's functions
727 * needed to read and write the tape.
729 static void testcmd()
733 stat = append_test();
734 if (stat == 1) { /* OK get out */
737 if (stat == -1) { /* first test failed */
738 if (dev_cap(dev, CAP_EOM)) {
739 Pmsg0(-1, "\nAppend test failed. Attempting again.\n"
740 "Setting \"Hardware End of Medium = no\" and retrying append test.\n\n");
741 dev->capabilities &= ~CAP_EOM; /* turn off eom */
742 stat = append_test();
744 Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n"
745 " Hardware End of Medium = No\n\n"
746 "to your Device resource in the Storage conf file.\n");
750 Pmsg0(-1, "\n\nThat appears not to have corrected the problem.\n");
753 /* Wrong count after append */
755 Pmsg0(-1, "\n\nIt looks like the append failed. Attempting again.\n"
756 "Setting \"BSF at EOM = yes\" and retrying append test.\n");
757 dev->capabilities |= CAP_BSFATEOM; /* backspace on eom */
758 stat = append_test();
760 Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n"
761 " Hardware End of Medium = No\n"
762 " BSR at EOM = yes\n\n"
763 "to your Device resource in the Storage conf file.\n");
768 Pmsg0(-1, "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
769 "Unable to correct the problem. You MUST fix this\n"
770 "problem before Bacula can use your tape drive correctly\n");
771 Pmsg0(-1, "\nPerhaps running Bacula in fixed block mode will work.\n"
772 "Do so by setting:\n\n"
773 "Minimum Block Size = nnn\n"
774 "Maximum Block Size = nnn\n\n"
775 "in your Storage daemon's Device definition.\n"
776 "nnn must match your tape driver's block size.\n"
777 "This, however, is not really an ideal solution.\n");
782 Pmsg0(-1, _("\nThe above Bacula scan should have output identical to what follows.\n"
783 "Please double check it ...\n"
784 "=== Sample correct output ===\n"
785 "1 block of 64448 bytes in file 1\n"
786 "End of File mark.\n"
787 "2 blocks of 64448 bytes in file 2\n"
788 "End of File mark.\n"
789 "3 blocks of 64448 bytes in file 3\n"
790 "End of File mark.\n"
791 "1 block of 64448 bytes in file 4\n"
792 "End of File mark.\n"
793 "Total files=4, blocks=7, bytes = 451136\n"
794 "=== End sample correct output ===\n\n"));
796 Pmsg0(-1, _("If the above scan output is not identical to the\n"
797 "sample output, you MUST correct the problem\n"
798 "or Bacula will not be able to write multiple Jobs to \n"
802 re_read_block_test();
805 Pmsg0(-1, _("\n=== End Append files test ===\n"));
809 /* Forward space a file */
812 if (!fsf_dev(dev, 1)) {
813 Pmsg1(0, "Bad status from fsf. ERR=%s\n", strerror_dev(dev));
816 Pmsg0(0, "Forward spaced one file.\n");
819 /* Forward space a record */
824 if ((stat=fsr_dev(dev, 1)) < 0) {
825 Pmsg2(0, "Bad status from fsr %d. ERR=%s\n", stat, strerror_dev(dev));
828 Pmsg0(0, "Forward spaced one record.\n");
833 * Write a Bacula block to the tape
841 sm_check(__FILE__, __LINE__, False);
842 block = new_block(dev);
844 dump_block(block, "test");
846 i = block->buf_len - 100;
848 rec->data = check_pool_memory_size(rec->data, i);
849 memset(rec->data, i & 0xFF, i);
851 sm_check(__FILE__, __LINE__, False);
852 if (!write_record_to_block(block, rec)) {
853 Pmsg0(0, _("Error writing record to block.\n"));
856 if (!write_block_to_dev(jcr, dev, block)) {
857 Pmsg0(0, _("Error writing block to device.\n"));
860 Pmsg1(0, _("Wrote one record of %d bytes.\n"), i);
862 Pmsg0(0, _("Wrote block to device.\n"));
865 sm_check(__FILE__, __LINE__, False);
868 sm_check(__FILE__, __LINE__, False);
872 * Read a record from the tape
879 if (!get_cmd("Enter length to read: ")) {
883 if (len < 0 || len > 1000000) {
884 Pmsg0(0, _("Bad length entered, using default of 1024 bytes.\n"));
887 buf = (char *)malloc(len);
888 stat = read(dev->fd, buf, len);
889 if (stat > 0 && stat <= len) {
892 Pmsg3(0, _("Read of %d bytes gives stat=%d. ERR=%s\n"),
893 len, stat, strerror(errno));
899 * Scan tape by reading block by block. Report what is
900 * on the tape. Note, this command does raw reads, and as such
901 * will not work with fixed block size devices.
903 static void scancmd()
906 int blocks, tot_blocks, tot_files;
911 blocks = block_size = tot_blocks = 0;
913 if (dev->state & ST_EOT) {
914 Pmsg0(0, "End of tape\n");
918 tot_files = dev->file;
920 if ((stat = read(dev->fd, buf, sizeof(buf))) < 0) {
921 clrerror_dev(dev, -1);
922 Mmsg2(&dev->errmsg, "read error on %s. ERR=%s.\n",
923 dev->dev_name, strerror(dev->dev_errno));
924 Pmsg2(0, "Bad status from read %d. ERR=%s\n", stat, strerror_dev(dev));
926 printf("%d block%s of %d bytes in file %d\n",
927 blocks, blocks>1?"s":"", block_size, dev->file);
930 Dmsg1(200, "read status = %d\n", stat);
932 if (stat != block_size) {
935 printf("%d block%s of %d bytes in file %d\n",
936 blocks, blocks>1?"s":"", block_size, dev->file);
941 if (stat == 0) { /* EOF */
943 printf("End of File mark.\n");
944 /* Two reads of zero means end of tape */
945 if (dev->state & ST_EOF)
946 dev->state |= ST_EOT;
948 dev->state |= ST_EOF;
951 if (dev->state & ST_EOT) {
952 printf("End of tape\n");
955 } else { /* Got data */
956 dev->state &= ~ST_EOF;
963 tot_files = dev->file - tot_files;
964 printf("Total files=%d, blocks=%d, bytes = %" lld "\n", tot_files, tot_blocks, bytes);
969 * Scan tape by reading Bacula block by block. Report what is
970 * on the tape. This function reads Bacula blocks, so if your
971 * Device resource is correctly defined, it should work with
972 * either variable or fixed block sizes.
974 static void scan_blocks()
976 int blocks, tot_blocks, tot_files;
981 block = new_block(dev);
982 blocks = block_size = tot_blocks = 0;
986 tot_files = dev->file;
988 if (!read_block_from_device(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
989 Dmsg1(100, "!read_block(): ERR=%s\n", strerror_dev(dev));
990 if (dev->state & ST_EOT) {
992 printf("%d block%s of %d bytes in file %d\n",
993 blocks, blocks>1?"s":"", block_size, dev->file);
998 if (dev->state & ST_EOF) {
1000 printf("%d block%s of %d bytes in file %d\n",
1001 blocks, blocks>1?"s":"", block_size, dev->file);
1004 printf(_("End of File mark.\n"));
1007 if (dev->state & ST_SHORT) {
1009 printf("%d block%s of %d bytes in file %d\n",
1010 blocks, blocks>1?"s":"", block_size, dev->file);
1013 printf(_("Short block read.\n"));
1016 printf(_("Error reading block. ERR=%s\n"), strerror_dev(dev));
1019 if (block->block_len != block_size) {
1021 printf("%d block%s of %d bytes in file %d\n",
1022 blocks, blocks>1?"s":"", block_size, dev->file);
1025 block_size = block->block_len;
1029 bytes += block->block_len;
1030 Dmsg5(100, "Blk=%u blen=%u bVer=%d SessId=%u SessTim=%u\n",
1031 block->BlockNumber, block->block_len, block->BlockVer,
1032 block->VolSessionId, block->VolSessionTime);
1034 DEV_RECORD *rec = new_record();
1035 read_record_from_block(block, rec);
1036 Pmsg7(-1, "Block: %u blen=%u First rec FI=%s SessId=%u SessTim=%u Strm=%s rlen=%d\n",
1037 block->BlockNumber, block->block_len,
1038 FI_to_ascii(rec->FileIndex), rec->VolSessionId, rec->VolSessionTime,
1039 stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len);
1042 } else if (verbose > 1) {
1043 dump_block(block, "");
1049 tot_files = dev->file - tot_files;
1050 printf("Total files=%d, blocks=%d, bytes = %" lld "\n", tot_files, tot_blocks, bytes);
1054 static void statcmd()
1060 debug = debug_level;
1062 if (!status_dev(dev, &status)) {
1063 Pmsg2(0, "Bad status from status %d. ERR=%s\n", stat, strerror_dev(dev));
1066 dump_volume_label(dev);
1068 debug_level = debug;
1073 * First we label the tape, then we fill
1074 * it with data get a new tape and write a few blocks.
1076 static void fillcmd()
1087 This command simulates Bacula writing to a tape.\n\
1088 It requires either one or two blank tapes, which it\n\
1089 will label and write. It will print a status approximately\n\
1090 every 322 MB, and write an EOF every 3.2 GB. If you have\n\
1091 selected the simple test option, after writing the first tape\n\
1092 it will rewind it and re-read the last block written.\n\
1093 If you have selected the multiple tape test, when the first tape\n\
1094 fills, it will ask for a second, and after writing a few more \n\
1095 blocks, it will stop. Then it will begin re-reading the\n\
1097 This may take a long time -- hours! ...\n\n");
1099 get_cmd("Insert a blank tape then indicate if you want\n"
1100 "to run the simplified test (s) with one tape or\n"
1101 "the complete multiple tape (m) test: (s/m) ");
1102 if (cmd[0] == 's') {
1103 Pmsg0(-1, "Simple test (single tape) selected.\n");
1105 } else if (cmd[0] == 'm') {
1106 Pmsg0(-1, "Complete multiple tape test selected.\n");
1109 Pmsg0(000, "Command aborted.\n");
1113 set_volume_name("TestVolume1", 1);
1118 Dmsg1(20, "Begin append device=%s\n", dev_name(dev));
1120 block = new_block(dev);
1123 * Acquire output device for writing. Note, after acquiring a
1124 * device, we MUST release it, which is done at the end of this
1127 Dmsg0(100, "just before acquire_device\n");
1128 if (!(dev=acquire_device_for_append(jcr, dev, block))) {
1129 set_jcr_job_status(jcr, JS_ErrorTerminated);
1134 Dmsg0(100, "Just after acquire_device_for_append\n");
1136 * Write Begin Session Record
1138 if (!write_session_label(jcr, block, SOS_LABEL)) {
1139 set_jcr_job_status(jcr, JS_ErrorTerminated);
1140 Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"),
1144 Pmsg0(-1, "Wrote Start Of Session label.\n");
1146 memset(&rec, 0, sizeof(rec));
1147 rec.data = get_memory(100000); /* max record size */
1149 #define REC_SIZE 32768
1150 rec.data_len = REC_SIZE;
1153 * Generate data as if from File daemon, write to device
1155 jcr->VolFirstIndex = 0;
1156 time(&jcr->run_time); /* start counting time for rates */
1157 Pmsg0(-1, "Begin writing records to first tape ...\n");
1158 for (file_index = 0; ok && !job_canceled(jcr); ) {
1159 rec.VolSessionId = jcr->VolSessionId;
1160 rec.VolSessionTime = jcr->VolSessionTime;
1161 rec.FileIndex = ++file_index;
1162 rec.Stream = STREAM_FILE_DATA;
1165 * Fill the buffer with the file_index negated. Negation ensures that
1166 * more bits are turned on.
1168 uint64_t *lp = (uint64_t *)rec.data;
1169 for (uint32_t i=0; i < (rec.data_len-sizeof(uint64_t))/sizeof(uint64_t); i++) {
1170 *lp++ = ~file_index;
1173 Dmsg4(250, "before writ_rec FI=%d SessId=%d Strm=%s len=%d\n",
1174 rec.FileIndex, rec.VolSessionId, stream_to_ascii(rec.Stream, rec.FileIndex),
1177 while (!write_record_to_block(block, &rec)) {
1179 * When we get here we have just filled a block
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.0 * (double)now);
1198 Pmsg3(-1, "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 15000 blocks (approx 1GB) write an EOF.
1203 if ((block->BlockNumber % 15000) == 0) {
1204 Pmsg0(-1, "Flush block, write EOF\n");
1205 flush_block(block, 0);
1209 /* Get out after writing 10 blocks to the second tape */
1210 if (++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);
1223 /* Get out after writing 10 blocks to the second tape */
1224 if (BlockNumber > 10 && stop != 0) { /* get out */
1225 Pmsg0(-1, "Done writing 10 blocks to second tape.\n");
1230 Dmsg0(100, "Write_end_session_label()\n");
1231 /* Create Job status for end of session label */
1232 if (!job_canceled(jcr) && ok) {
1233 set_jcr_job_status(jcr, JS_Terminated);
1235 set_jcr_job_status(jcr, JS_ErrorTerminated);
1237 if (!write_session_label(jcr, block, EOS_LABEL)) {
1238 Pmsg1(000, _("Error writting end session label. ERR=%s\n"), strerror_dev(dev));
1241 /* Write out final block of this session */
1242 if (!write_block_to_device(jcr, dev, block)) {
1243 Pmsg0(-1, _("Set ok=FALSE after write_block_to_device.\n"));
1246 Pmsg0(-1, "Wrote End Of Session label.\n");
1249 /* Release the device */
1250 if (!release_device(jcr, dev)) {
1251 Pmsg0(-1, _("Error in release_device\n"));
1256 free_memory(rec.data);
1257 Pmsg0(-1, _("\n\nDone filling tape. Now beginning re-read of tape ...\n"));
1259 dump_block(last_block, _("Last block written to tape.\n"));
1265 * Read two tapes written by the "fill" command and ensure
1266 * that the data is valid. If stop==1 we simulate full read back
1267 * of two tapes. If stop==-1 we simply read the last block and
1268 * verify that it is correct.
1270 static void unfillcmd()
1277 block = new_block(dev);
1279 dev->capabilities |= CAP_ANONVOLS; /* allow reading any volume */
1280 dev->capabilities &= ~CAP_LABEL; /* don't label anything here */
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);
1297 time(&jcr->run_time); /* start counting time for rates */
1301 /* Read all records and then second tape */
1302 read_records(jcr, dev, record_cb, my_mount_next_read_volume);
1305 * Simplified test, we simply fsf to file, then read the
1306 * last block and make sure it is the same as the saved block.
1308 Pmsg0(000, "Rewinding tape ...\n");
1309 if (!rewind_dev(dev)) {
1310 Pmsg1(-1, _("Error rewinding: ERR=%s\n"), strerror_dev(dev));
1313 if (last_file > 0) {
1314 Pmsg1(000, "Forward spacing to last file=%u\n", last_file);
1315 if (!fsf_dev(dev, last_file)) {
1316 Pmsg1(-1, _("Error in FSF: ERR=%s\n"), strerror_dev(dev));
1320 Pmsg1(-1, _("Forward space to file %u complete. Reading blocks ...\n"),
1322 Pmsg1(-1, _("Now reading to block %u.\n"), last_block_num);
1323 for (uint32_t i= 0; i < last_block_num; i++) {
1324 if (!read_block_from_device(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
1325 Pmsg1(-1, _("Error reading blocks: ERR=%s\n"), strerror_dev(dev));
1326 Pmsg2(-1, _("Wanted block %u error at block %u\n"), last_block_num, i);
1329 if (i > 0 && i % 1000 == 0) {
1330 Pmsg1(-1, _("At block %u\n"), i);
1334 dump_block(last_block, _("Last block written"));
1335 dump_block(block, _("Block read back"));
1336 Pmsg0(-1, _("Except for the buffer address, the contents of\n"
1337 "the above two block dumps should be the same.\n"
1338 "If not you have a problem ...\n"));
1345 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);
1482 dump_block(block, "Block not written");
1485 eot_block = block->BlockNumber;
1486 eot_block_len = block->block_len;
1487 eot_FileIndex = file_index;
1490 now -= jcr->run_time;
1494 kbs = (double)dev->VolCatInfo.VolCatBytes / (1000 * now);
1495 vol_size = dev->VolCatInfo.VolCatBytes;
1496 Pmsg2(000, "End of tape. VolumeCapacity=%s. Write rate = %.1f KB/s\n",
1497 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), kbs);
1500 stop = -1; /* stop, but do simplified test */
1502 /* Full test in progress */
1503 if (!fixup_device_block_write_error(jcr, dev, block)) {
1504 Pmsg1(000, _("Cannot fixup device error. %s\n"), strerror_dev(dev));
1510 BlockNumber = 0; /* start counting for second tape */
1513 return 1; /* end of tape reached */
1517 * Toggle between two allocated blocks for efficiency.
1518 * Switch blocks so that the block just successfully written is
1519 * always in last_block.
1521 tblock = last_block;
1522 last_block = this_block;
1523 this_block = tblock;
1524 last_file = this_file;
1525 last_block_num = this_block_num;
1532 struct cmdstruct { char *key; void (*func)(); char *help; };
1533 static struct cmdstruct commands[] = {
1534 {"bsf", bsfcmd, "backspace file"},
1535 {"bsr", bsrcmd, "backspace record"},
1536 {"cap", capcmd, "list device capabilities"},
1537 {"clear", clearcmd, "clear tape errors"},
1538 {"eod", eodcmd, "go to end of Bacula data for append"},
1539 {"eom", eomcmd, "go to the physical end of medium"},
1540 {"fill", fillcmd, "fill tape, write onto second volume"},
1541 {"unfill", unfillcmd, "read filled tape"},
1542 {"fsf", fsfcmd, "forward space a file"},
1543 {"fsr", fsrcmd, "forward space a record"},
1544 {"help", helpcmd, "print this command"},
1545 {"label", labelcmd, "write a Bacula label to the tape"},
1546 {"load", loadcmd, "load a tape"},
1547 {"quit", quitcmd, "quit btape"},
1548 {"readlabel", readlabelcmd, "read and print the Bacula tape label"},
1549 {"rectest", rectestcmd, "test record handling functions"},
1550 {"rewind", rewindcmd, "rewind the tape"},
1551 {"scan", scancmd, "read tape block by block to EOT and report"},
1552 {"status", statcmd, "print tape status"},
1553 {"test", testcmd, "General test Bacula tape functions"},
1554 {"weof", weofcmd, "write an EOF on the tape"},
1555 {"wr", wrcmd, "write a single Bacula block"},
1556 {"rr", rrcmd, "read a single record"},
1558 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
1566 while (get_cmd("*")) {
1567 sm_check(__FILE__, __LINE__, False);
1569 for (i=0; i<comsize; i++) /* search for command */
1570 if (fstrsch(cmd, commands[i].key)) {
1571 (*commands[i].func)(); /* go execute command */
1576 Pmsg1(0, _("%s is an illegal command\n"), cmd);
1582 static void helpcmd()
1586 printf(_(" Command Description\n ======= ===========\n"));
1587 for (i=0; i<comsize; i++)
1588 printf(" %-10s %s\n", commands[i].key, commands[i].help);
1595 "\nVersion: " VERSION " (" BDATE ")\n\n"
1596 "Usage: btape [-c config_file] [-d debug_level] [device_name]\n"
1597 " -c <file> set configuration file to file\n"
1598 " -dnn set debug level to nn\n"
1599 " -s turn off signals\n"
1600 " -t open the default tape device\n"
1601 " -? print this message.\n"
1607 * Get next input command from terminal. This
1608 * routine is REALLY primitive, and should be enhanced
1609 * to have correct backspacing, etc.
1612 get_cmd(char *prompt)
1616 fprintf(stdout, prompt);
1618 /* We really should turn off echoing and pretty this
1622 while ((ch = fgetc(stdin)) != EOF) {
1624 strip_trailing_junk(cmd);
1626 } else if (ch == 4 || ch == 0xd3 || ch == 0x8) {
1639 /* Dummies to replace askdir.c */
1640 int dir_get_volume_info(JCR *jcr, int writing) { return 1;}
1641 int dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
1642 int dir_create_jobmedia_record(JCR *jcr) { return 1; }
1643 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
1644 int dir_send_job_status(JCR *jcr) {return 1;}
1648 int dir_find_next_appendable_volume(JCR *jcr)
1653 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
1655 Pmsg1(-1, "%s", dev->errmsg); /* print reason */
1656 fprintf(stderr, "Mount Volume \"%s\" on device %s and press return when ready: ",
1657 jcr->VolumeName, dev_name(dev));
1662 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev)
1664 fprintf(stderr, "Mount next Volume on device %s and press return when ready: ",
1667 set_volume_name("TestVolume2", 2);
1675 static int my_mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
1679 Pmsg1(000, "End of Volume \"%s\"\n", jcr->VolumeName);
1681 if (LastBlock != block->BlockNumber) {
1682 VolBytes += block->block_len;
1684 LastBlock = block->BlockNumber;
1686 now -= jcr->run_time;
1690 kbs = (double)VolBytes / (1000.0 * (double)now);
1691 Pmsg3(-1, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
1692 edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
1694 if (strcmp(jcr->VolumeName, "TestVolume2") == 0) {
1700 set_volume_name("TestVolume2", 2);
1702 create_vol_list(jcr);
1704 dev->state &= ~ST_READ;
1705 if (!acquire_device_for_read(jcr, dev, block)) {
1706 Pmsg2(0, "Cannot open Dev=%s, Vol=%s\n", dev_name(dev), jcr->VolumeName);
1709 return 1; /* next volume mounted */
1712 static void set_volume_name(char *VolName, int volnum)
1714 VolumeName = VolName;
1716 pm_strcpy(&jcr->VolumeName, VolName);
1717 bstrncpy(dev->VolCatInfo.VolCatName, VolName, sizeof(dev->VolCatInfo.VolCatName));