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 verbose = 0;
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 int simple = TRUE;
98 static char *VolumeName = NULL;
100 static JCR *jcr = NULL;
104 static void terminate_btape(int sig);
105 int get_cmd(char *prompt);
108 int write_dev(DEVICE *dev, char *buf, size_t len)
110 Emsg0(M_ABORT, 0, "write_dev not implemented.\n");
114 int read_dev(DEVICE *dev, char *buf, size_t len)
116 Emsg0(M_ABORT, 0, "read_dev not implemented.\n");
121 /*********************************************************************
123 * Main Bacula Pool Creation Program
126 int main(int argc, char *argv[])
132 if (TAPE_BSIZE % DEV_BSIZE != 0 || TAPE_BSIZE / DEV_BSIZE == 0) {
133 Emsg2(M_ABORT, 0, "Tape block size (%d) not multiple of system size (%d)\n",
134 TAPE_BSIZE, DEV_BSIZE);
136 if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
137 Emsg1(M_ABORT, 0, "Tape block size (%d) is not a power of 2\n", TAPE_BSIZE);
140 printf("Tape block granularity is %d bytes.\n", TAPE_BSIZE);
142 working_directory = "/tmp";
143 my_name_is(argc, argv, "btape");
144 init_msg(NULL, NULL);
146 while ((ch = getopt(argc, argv, "b:c:d:sv?")) != -1) {
148 case 'b': /* bootstrap file */
149 bsr = parse_bsr(NULL, optarg);
153 case 'c': /* specify config file */
154 if (configfile != NULL) {
157 configfile = bstrdup(optarg);
160 case 'd': /* set debug level */
161 debug_level = atoi(optarg);
162 if (debug_level <= 0) {
188 init_signals(terminate_btape);
191 if (configfile == NULL) {
192 configfile = bstrdup(CONFIG_FILE);
195 daemon_start_time = time(NULL);
197 parse_config(configfile);
200 /* See if we can open a device */
202 Pmsg0(000, "No archive name specified.\n");
205 } else if (argc != 1) {
206 Pmsg0(000, "Improper number of arguments specified.\n");
211 jcr = setup_jcr("btape", argv[0], bsr, NULL);
212 dev = setup_to_access_device(jcr, 0); /* acquire for write */
216 block = new_block(dev);
218 if (!(dev->state & ST_OPENED)) {
219 Dmsg0(129, "Opening device.\n");
220 if (open_dev(dev, jcr->VolumeName, READ_WRITE) < 0) {
221 Emsg1(M_FATAL, 0, _("dev open failed: %s\n"), dev->errmsg);
227 Dmsg1(129, "open_dev %s OK\n", dev_name(dev));
231 Dmsg0(200, "Do tape commands\n");
239 static void terminate_btape(int stat)
242 sm_check(__FILE__, __LINE__, False);
246 free_config_resources();
252 if (debug_level > 10)
253 print_memory_pool_stats();
263 free_block(last_block);
266 free_block(this_block);
270 close_memory_pool(); /* free memory in pool */
282 * Write a label to the tape
284 static void labelcmd()
290 for (device=NULL; (device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device)); ) {
291 if (strcmp(device->device_name, dev->dev_name) == 0) {
292 jcr->device = device; /* Arggg a bit of duplication here */
294 dev->device = device;
301 Pmsg2(0, "Could not find device %s in %s\n", dev->dev_name, configfile);
306 strcpy(cmd, VolumeName);
308 if (!get_cmd("Enter Volume Name: ")) {
313 if (!(dev->state & ST_OPENED)) {
314 if (!open_device(dev)) {
315 Pmsg1(0, "Device open failed. ERR=%s\n", strerror_dev(dev));
318 write_volume_label_to_dev(jcr, device, cmd, "Default");
322 * Read the tape label
324 static void readlabelcmd()
326 int save_debug_level = debug_level;
330 block = new_block(dev);
331 stat = read_dev_volume_label(jcr, dev, block);
334 Pmsg0(0, "Volume has no label.\n");
337 Pmsg0(0, "Volume label read correctly.\n");
340 Pmsg1(0, "I/O error on device: ERR=%s", strerror_dev(dev));
343 Pmsg0(0, "Volume name error\n");
345 case VOL_CREATE_ERROR:
346 Pmsg1(0, "Error creating label. ERR=%s", strerror_dev(dev));
348 case VOL_VERSION_ERROR:
349 Pmsg0(0, "Volume version error.\n");
351 case VOL_LABEL_ERROR:
352 Pmsg0(0, "Bad Volume label type.\n");
355 Pmsg0(0, "Unknown error.\n");
360 dump_volume_label(dev);
361 debug_level = save_debug_level;
367 * Load the tape should have prevously been taken
368 * off line, otherwise this command is not necessary.
370 static void loadcmd()
373 if (!load_dev(dev)) {
374 Pmsg1(0, "Bad status from load. ERR=%s\n", strerror_dev(dev));
376 Pmsg1(0, "Loaded %s\n", dev_name(dev));
382 static void rewindcmd()
384 if (!rewind_dev(dev)) {
385 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
386 clrerror_dev(dev, -1);
388 Pmsg1(0, "Rewound %s\n", dev_name(dev));
393 * Clear any tape error
395 static void clearcmd()
397 clrerror_dev(dev, -1);
401 * Write and end of file on the tape
403 static void weofcmd()
407 if ((stat = weof_dev(dev, 1)) < 0) {
408 Pmsg2(0, "Bad status from weof %d. ERR=%s\n", stat, strerror_dev(dev));
411 Pmsg1(0, "Wrote EOF to %s\n", dev_name(dev));
416 /* Go to the end of the medium -- raw command
417 * The idea was orginally that the end of the Bacula
418 * medium would be flagged differently. This is not
419 * currently the case. So, this is identical to the
425 Pmsg1(0, _("Bad status from eod. ERR=%s\n"), strerror_dev(dev));
428 Pmsg0(0, _("Moved to end of media\n"));
433 * Go to the end of the media (either hardware determined
434 * or defined by two eofs.
448 if ((stat=bsf_dev(dev, 1)) < 0) {
449 Pmsg1(0, _("Bad status from bsf. ERR=%s\n"), strerror(errno));
451 Pmsg0(0, _("Back spaced one file.\n"));
462 if ((stat=bsr_dev(dev, 1)) < 0) {
463 Pmsg1(0, _("Bad status from bsr. ERR=%s\n"), strerror(errno));
465 Pmsg0(0, _("Back spaced one record.\n"));
470 * List device capabilities as defined in the
475 printf(_("Device capabilities:\n"));
476 printf("%sEOF ", dev->capabilities & CAP_EOF ? "" : "!");
477 printf("%sBSR ", dev->capabilities & CAP_BSR ? "" : "!");
478 printf("%sBSF ", dev->capabilities & CAP_BSF ? "" : "!");
479 printf("%sFSR ", dev->capabilities & CAP_FSR ? "" : "!");
480 printf("%sFSF ", dev->capabilities & CAP_FSF ? "" : "!");
481 printf("%sEOM ", dev->capabilities & CAP_EOM ? "" : "!");
482 printf("%sREM ", dev->capabilities & CAP_REM ? "" : "!");
483 printf("%sRACCESS ", dev->capabilities & CAP_RACCESS ? "" : "!");
484 printf("%sAUTOMOUNT ", dev->capabilities & CAP_AUTOMOUNT ? "" : "!");
485 printf("%sLABEL ", dev->capabilities & CAP_LABEL ? "" : "!");
486 printf("%sANONVOLS ", dev->capabilities & CAP_ANONVOLS ? "" : "!");
487 printf("%sALWAYSOPEN ", dev->capabilities & CAP_ALWAYSOPEN ? "" : "!");
490 printf(_("Device status:\n"));
491 printf("%sOPENED ", dev->state & ST_OPENED ? "" : "!");
492 printf("%sTAPE ", dev->state & ST_TAPE ? "" : "!");
493 printf("%sLABEL ", dev->state & ST_LABEL ? "" : "!");
494 printf("%sMALLOC ", dev->state & ST_MALLOC ? "" : "!");
495 printf("%sAPPEND ", dev->state & ST_APPEND ? "" : "!");
496 printf("%sREAD ", dev->state & ST_READ ? "" : "!");
497 printf("%sEOT ", dev->state & ST_EOT ? "" : "!");
498 printf("%sWEOT ", dev->state & ST_WEOT ? "" : "!");
499 printf("%sEOF ", dev->state & ST_EOF ? "" : "!");
500 printf("%sNEXTVOL ", dev->state & ST_NEXTVOL ? "" : "!");
501 printf("%sSHORT ", dev->state & ST_SHORT ? "" : "!");
504 printf(_("Device parameters:\n"));
505 printf("Device name: %s\n", dev->dev_name);
506 printf("File=%u block=%u\n", dev->file, dev->block_num);
507 printf("Min block=%u Max block=%u\n", dev->min_block_size, dev->max_block_size);
515 * Test writting larger and larger records.
516 * This is a torture test for records.
518 static void rectestcmd()
524 Pmsg0(0, "Test writting larger and larger records.\n"
525 "This is a torture test for records.\nI am going to write\n"
526 "larger and larger records. It will stop when the record size\n"
527 "plus the header exceeds the block size (by default about 64K)\n");
530 get_cmd("Do you want to continue? (y/n): ");
532 Pmsg0(000, "Command aborted.\n");
536 sm_check(__FILE__, __LINE__, False);
537 block = new_block(dev);
540 for (i=1; i<500000; i++) {
541 rec->data = check_pool_memory_size(rec->data, i);
542 memset(rec->data, i & 0xFF, i);
544 sm_check(__FILE__, __LINE__, False);
545 if (write_record_to_block(block, rec)) {
548 Pmsg2(0, "Block %d i=%d\n", blkno, i);
552 sm_check(__FILE__, __LINE__, False);
556 sm_check(__FILE__, __LINE__, False);
560 * This test attempts to re-read a block written by Bacula
561 * normally at the end of the tape. Bacula will then back up
562 * over the two eof marks, backup over the record and reread
563 * it to make sure it is valid. Bacula can skip this validation
564 * if you set "Backward space record = no"
566 static int re_read_block_test()
573 if (!(dev->capabilities & CAP_BSR)) {
574 Pmsg0(-1, _("Skipping read backwards test because BSR turned off.\n"));
578 Pmsg0(-1, _("\n=== Write, backup, and re-read test ===\n\n"
579 "I'm going to write three records and two eof's\n"
580 "then backup over the eof's and re-read the last record.\n"
581 "Bacula does this after writing the last block on the\n"
582 "tape to verify that the block was written correctly.\n"
583 "It is not an *essential* feature ...\n\n"));
585 block = new_block(dev);
587 rec->data = check_pool_memory_size(rec->data, block->buf_len);
588 len = rec->data_len = block->buf_len-100;
589 memset(rec->data, 1, rec->data_len);
590 if (!write_record_to_block(block, rec)) {
591 Pmsg0(0, _("Error writing record to block.\n"));
594 if (!write_block_to_dev(jcr, dev, block)) {
595 Pmsg0(0, _("Error writing block to device.\n"));
598 Pmsg1(0, _("Wrote first record of %d bytes.\n"), rec->data_len);
600 memset(rec->data, 2, rec->data_len);
601 if (!write_record_to_block(block, rec)) {
602 Pmsg0(0, _("Error writing record to block.\n"));
605 if (!write_block_to_dev(jcr, dev, block)) {
606 Pmsg0(0, _("Error writing block to device.\n"));
609 Pmsg1(0, _("Wrote second record of %d bytes.\n"), rec->data_len);
611 memset(rec->data, 3, rec->data_len);
612 if (!write_record_to_block(block, rec)) {
613 Pmsg0(0, _("Error writing record to block.\n"));
616 if (!write_block_to_dev(jcr, dev, block)) {
617 Pmsg0(0, _("Error writing block to device.\n"));
620 Pmsg1(0, _("Wrote third record of %d bytes.\n"), rec->data_len);
624 if (bsf_dev(dev, 1) != 0) {
625 Pmsg1(0, _("Back space file failed! ERR=%s\n"), strerror(dev->dev_errno));
628 if (bsf_dev(dev, 1) != 0) {
629 Pmsg1(0, _("Back space file failed! ERR=%s\n"), strerror(dev->dev_errno));
632 Pmsg0(0, "Backspaced over two EOFs OK.\n");
633 if (bsr_dev(dev, 1) != 0) {
634 Pmsg1(0, _("Back space record failed! ERR=%s\n"), strerror(dev->dev_errno));
637 Pmsg0(0, "Backspace record OK.\n");
638 if (!read_block_from_dev(dev, block)) {
639 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
642 memset(rec->data, 0, rec->data_len);
643 if (!read_record_from_block(block, rec)) {
644 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
647 for (int i=0; i<len; i++) {
648 if (rec->data[i] != 3) {
649 Pmsg0(0, _("Bad data in record. Test failed!\n"
650 "This is not terribly serious since Bacula only uses\n"
651 "This function to verify the last block written to the\n"
652 "tape. Bacula will skip the last block verification\n"
654 "Backward Space Record = No\n\n"
655 "to your Storage daemon's Device resource definition.\n"));
659 Pmsg0(0, _("\nBlock re-read correct. Test succeeded!\n"));
660 Pmsg0(-1, _("=== End Write, backup, and re-read test ===\n\n"));
671 * This test writes some records, then writes an end of file,
672 * rewinds the tape, moves to the end of the data and attepts
673 * to append to the tape. This function is essential for
674 * Bacula to be able to write multiple jobs to the tape.
676 static int append_test()
678 Pmsg0(-1, _("\n\n=== Append files test ===\n\n"
679 "This test is essential to Bacula.\n\n"
680 "I'm going to write one record in file 0,\n"
681 " two records in file 1,\n"
682 " and three records in file 2\n\n"));
685 weofcmd(); /* end file 0 */
688 weofcmd(); /* end file 1 */
692 weofcmd(); /* end file 2 */
694 Pmsg0(0, _("Now moving to end of media.\n"));
696 Pmsg2(-1, _("We should be in file 3. I am at file %d. This is %s\n"),
697 dev->file, dev->file == 3 ? "correct!" : "NOT correct!!!!");
699 if (dev->file != 3) {
703 Pmsg0(-1, _("\nNow the important part, I am going to attempt to append to the tape.\n\n"));
707 Pmsg0(-1, _("Done appending, there should be no I/O errors\n\n"));
708 Pmsg0(-1, "Doing Bacula scan of blocks:\n");
710 Pmsg0(-1, _("End scanning the tape.\n"));
711 Pmsg2(-1, _("We should be in file 4. I am at file %d. This is %s\n"),
712 dev->file, dev->file == 4 ? "correct!" : "NOT correct!!!!");
714 if (dev->file != 4) {
722 * This is a general test of Bacula's functions
723 * needed to read and write the tape.
725 static void testcmd()
728 re_read_block_test();
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"
798 Pmsg0(-1, _("\n=== End Append files test ===\n"));
802 /* Forward space a file */
805 if (!fsf_dev(dev, 1)) {
806 Pmsg1(0, "Bad status from fsf. ERR=%s\n", strerror_dev(dev));
809 Pmsg0(0, "Forward spaced one file.\n");
812 /* Forward space a record */
817 if ((stat=fsr_dev(dev, 1)) < 0) {
818 Pmsg2(0, "Bad status from fsr %d. ERR=%s\n", stat, strerror_dev(dev));
821 Pmsg0(0, "Forward spaced one record.\n");
826 * Write a Bacula block to the tape
834 sm_check(__FILE__, __LINE__, False);
835 block = new_block(dev);
837 dump_block(block, "test");
839 i = block->buf_len - 100;
841 rec->data = check_pool_memory_size(rec->data, i);
842 memset(rec->data, i & 0xFF, i);
844 sm_check(__FILE__, __LINE__, False);
845 if (!write_record_to_block(block, rec)) {
846 Pmsg0(0, _("Error writing record to block.\n"));
849 if (!write_block_to_dev(jcr, dev, block)) {
850 Pmsg0(0, _("Error writing block to device.\n"));
853 Pmsg1(0, _("Wrote one record of %d bytes.\n"), i);
855 Pmsg0(0, _("Wrote block to device.\n"));
858 sm_check(__FILE__, __LINE__, False);
861 sm_check(__FILE__, __LINE__, False);
865 * Read a record from the tape
872 if (!get_cmd("Enter length to read: ")) {
876 if (len < 0 || len > 1000000) {
877 Pmsg0(0, _("Bad length entered, using default of 1024 bytes.\n"));
880 buf = (char *)malloc(len);
881 stat = read(dev->fd, buf, len);
882 if (stat > 0 && stat <= len) {
885 Pmsg3(0, _("Read of %d bytes gives stat=%d. ERR=%s\n"),
886 len, stat, strerror(errno));
892 * Scan tape by reading block by block. Report what is
893 * on the tape. Note, this command does raw reads, and as such
894 * will not work with fixed block size devices.
896 static void scancmd()
899 int blocks, tot_blocks, tot_files;
904 blocks = block_size = tot_blocks = 0;
906 if (dev->state & ST_EOT) {
907 Pmsg0(0, "End of tape\n");
911 tot_files = dev->file;
913 if ((stat = read(dev->fd, buf, sizeof(buf))) < 0) {
914 clrerror_dev(dev, -1);
915 Mmsg2(&dev->errmsg, "read error on %s. ERR=%s.\n",
916 dev->dev_name, strerror(dev->dev_errno));
917 Pmsg2(0, "Bad status from read %d. ERR=%s\n", stat, strerror_dev(dev));
919 printf("%d block%s of %d bytes in file %d\n",
920 blocks, blocks>1?"s":"", block_size, dev->file);
923 Dmsg1(200, "read status = %d\n", stat);
925 if (stat != block_size) {
928 printf("%d block%s of %d bytes in file %d\n",
929 blocks, blocks>1?"s":"", block_size, dev->file);
934 if (stat == 0) { /* EOF */
936 printf("End of File mark.\n");
937 /* Two reads of zero means end of tape */
938 if (dev->state & ST_EOF)
939 dev->state |= ST_EOT;
941 dev->state |= ST_EOF;
944 if (dev->state & ST_EOT) {
945 printf("End of tape\n");
948 } else { /* Got data */
949 dev->state &= ~ST_EOF;
956 tot_files = dev->file - tot_files;
957 printf("Total files=%d, blocks=%d, bytes = %" lld "\n", tot_files, tot_blocks, bytes);
962 * Scan tape by reading Bacula block by block. Report what is
963 * on the tape. This function reads Bacula blocks, so if your
964 * Device resource is correctly defined, it should work with
965 * either variable or fixed block sizes.
967 static void scan_blocks()
969 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(dev, block)) {
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 = %" lld "\n", tot_files, tot_blocks, bytes);
1047 static void statcmd()
1053 debug = debug_level;
1055 if (!status_dev(dev, &status)) {
1056 Pmsg2(0, "Bad status from status %d. ERR=%s\n", stat, strerror_dev(dev));
1059 dump_volume_label(dev);
1061 debug_level = debug;
1066 * First we label the tape, then we fill
1067 * it with data get a new tape and write a few blocks.
1069 static void fillcmd()
1080 This command simulates Bacula writing to a tape.\n\
1081 It requires either one or two blank tapes, which it\n\
1082 will label and write. It will print a status approximately\n\
1083 every 322 MB, and write an EOF every 3.2 GB. If you have\n\
1084 selected the simple test option, after writing the first tape\n\
1085 it will rewind it and re-read the last block written.\n\
1086 If you have selected the multiple tape test, when the first tape\n\
1087 fills, it will ask for a second, and after writing a few \n\
1088 blocks, it will stop. Then it will begin re-reading the\n\
1089 This may take a long time. I.e. hours! ...\n\n");
1091 get_cmd("Insert a blank tape then indicate if you want\n"
1092 "to run the simplified test (s) with one tape or\n"
1093 "the complete multiple tape (m) test: (s/m) ");
1094 if (cmd[0] == 's') {
1095 Pmsg0(-1, "Simple test (single tape) selected.\n");
1097 } else if (cmd[0] == 'm') {
1098 Pmsg0(-1, "Complete multiple tape test selected.\n");
1101 Pmsg0(000, "Command aborted.\n");
1105 VolumeName = "TestVolume1";
1110 Dmsg1(20, "Begin append device=%s\n", dev_name(dev));
1112 block = new_block(dev);
1115 * Acquire output device for writing. Note, after acquiring a
1116 * device, we MUST release it, which is done at the end of this
1119 Dmsg0(100, "just before acquire_device\n");
1120 if (!(dev=acquire_device_for_append(jcr, dev, block))) {
1121 set_jcr_job_status(jcr, JS_ErrorTerminated);
1126 Dmsg0(100, "Just after acquire_device_for_append\n");
1128 * Write Begin Session Record
1130 if (!write_session_label(jcr, block, SOS_LABEL)) {
1131 set_jcr_job_status(jcr, JS_ErrorTerminated);
1132 Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"),
1137 memset(&rec, 0, sizeof(rec));
1138 rec.data = get_memory(100000); /* max record size */
1140 * Fill write buffer with random data
1142 #define REC_SIZE 32768
1144 for (int i=0; i < REC_SIZE; ) {
1145 makeSessionKey(p, NULL, 0);
1149 rec.data_len = REC_SIZE;
1152 * Get Data from File daemon, write to device
1154 jcr->VolFirstFile = 0;
1155 time(&jcr->run_time); /* start counting time for rates */
1156 for (file_index = 0; ok && !job_cancelled(jcr); ) {
1158 rec.VolSessionId = jcr->VolSessionId;
1159 rec.VolSessionTime = jcr->VolSessionTime;
1160 rec.FileIndex = ++file_index;
1161 rec.Stream = STREAM_FILE_DATA;
1163 /* Write file_index at beginning of buffer and add file_index to each
1164 * uint64_t item to make it unique.
1166 lp = (uint64_t *)rec.data;
1167 *lp++ = (uint64_t)file_index;
1168 for (uint32_t i=0; i < (REC_SIZE-sizeof(uint64_t))/sizeof(uint64_t); i++) {
1169 *lp++ = *lp + rec.FileIndex;
1172 Dmsg4(250, "before writ_rec FI=%d SessId=%d Strm=%s len=%d\n",
1173 rec.FileIndex, rec.VolSessionId, stream_to_ascii(rec.Stream, rec.FileIndex),
1176 if (!write_record_to_block(block, &rec)) {
1177 Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec.data_len,
1180 /* Write block to tape */
1181 if (!flush_block(block, 1)) {
1185 /* Every 5000 blocks (approx 322MB) report where we are.
1187 if ((block->BlockNumber % 5000) == 0) {
1189 now -= jcr->run_time;
1193 kbs = (double)dev->VolCatInfo.VolCatBytes / (1000 * now);
1194 Pmsg3(000, "Wrote block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
1195 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), (float)kbs);
1197 /* Every 50000 blocks (approx 3.2MB) write an eof.
1199 if ((block->BlockNumber % 50000) == 0) {
1200 Pmsg0(000, "Flush block, write EOF\n");
1201 flush_block(block, 0);
1203 /* The weof resets the block number */
1206 if (block->BlockNumber > 10 && stop != 0) { /* get out */
1211 Pmsg0(000, _("Not OK\n"));
1214 jcr->JobBytes += rec.data_len; /* increment bytes this job */
1215 Dmsg4(190, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
1216 FI_to_ascii(rec.FileIndex), rec.VolSessionId,
1217 stream_to_ascii(rec.Stream, rec.FileIndex), rec.data_len);
1220 Dmsg0(000, "Write_end_session_label()\n");
1221 /* Create Job status for end of session label */
1222 if (!job_cancelled(jcr) && ok) {
1223 set_jcr_job_status(jcr, JS_Terminated);
1225 set_jcr_job_status(jcr, JS_ErrorTerminated);
1227 if (!write_session_label(jcr, block, EOS_LABEL)) {
1228 Pmsg1(000, _("Error writting end session label. ERR=%s\n"), strerror_dev(dev));
1231 /* Write out final block of this session */
1232 if (!write_block_to_device(jcr, dev, block)) {
1233 Pmsg0(000, _("Set ok=FALSE after write_block_to_device.\n"));
1238 /* Release the device */
1239 if (!release_device(jcr, dev)) {
1240 Pmsg0(000, _("Error in release_device\n"));
1245 free_memory(rec.data);
1246 Pmsg0(000, _("\n\nDone filling tape. Now beginning re-read of tape ...\n"));
1248 dump_block(last_block, _("Last block written to tape.\n"));
1254 * Read two tapes written by the "fill" command and ensure
1255 * that the data is valid. If stop==1 we simulate full read back
1256 * of two tapes. If stop==-1 we simply read the last block and
1257 * verify that it is correct.
1259 static void unfillcmd()
1266 block = new_block(dev);
1268 dev->capabilities |= CAP_ANONVOLS; /* allow reading any volume */
1269 dev->capabilities &= ~CAP_LABEL; /* don't label anything here */
1273 get_cmd(_("Mount first of two tapes. Press enter when ready: "));
1277 pm_strcpy(&jcr->VolumeName, "TestVolume1");
1279 create_vol_list(jcr);
1281 dev->state &= ~ST_READ;
1282 if (!acquire_device_for_read(jcr, dev, block)) {
1283 Pmsg0(000, dev->errmsg);
1287 time(&jcr->run_time); /* start counting time for rates */
1291 /* Read all records and then second tape */
1292 read_records(jcr, dev, record_cb, my_mount_next_read_volume);
1295 * Simplified test, we simply fsf to file, then read the
1296 * last block and make sure it is the same as the saved block.
1298 if (!rewind_dev(dev)) {
1299 Pmsg1(-1, _("Error rewinding: ERR=%s\n"), strerror_dev(dev));
1302 if (last_file > 0) {
1303 if (!fsf_dev(dev, last_file)) {
1304 Pmsg1(-1, _("Error in FSF: ERR=%s\n"), strerror_dev(dev));
1308 Pmsg1(-1, _("Forward space to file %u complete. Reading blocks ...\n"),
1310 Pmsg1(-1, _("Now reading to block %u.\n"), last_block_num);
1311 for (uint32_t i= 0; i < last_block_num; i++) {
1312 if (!read_block_from_device(dev, block)) {
1313 Pmsg1(-1, _("Error reading blocks: ERR=%s\n"), strerror_dev(dev));
1314 Pmsg2(-1, _("Wanted block %u error at block %u\n"), last_block_num, i);
1317 if (i > 0 && i % 1000 == 0) {
1318 Pmsg1(-1, _("At block %u\n"), i);
1321 dump_block(last_block, _("Last block written"));
1322 dump_block(block, _("Block read back"));
1323 Pmsg0(-1, _("Except for the buffer address, the contents of\n"
1324 "the above two block dumps should be the same.\n"
1325 "If not you have a problem ...\n"));
1331 Pmsg0(000, _("Done with reread of fill data.\n"));
1336 * We are called here from "unfill" for each record on the
1339 static void record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
1342 SESSION_LABEL label;
1343 if (stop > 1) { /* on second tape */
1344 Pmsg4(000, "Blk: FileIndex=%d: block=%u size=%d vol=%s\n",
1345 rec->FileIndex, block->BlockNumber, block->block_len, dev->VolHdr.VolName);
1346 Pmsg6(000, " Rec: VId=%d VT=%d FI=%s Strm=%s len=%d state=%x\n",
1347 rec->VolSessionId, rec->VolSessionTime,
1348 FI_to_ascii(rec->FileIndex), stream_to_ascii(rec->Stream, rec->FileIndex),
1349 rec->data_len, rec->state);
1353 dump_block(block, "Block not written to previous tape");
1356 if (rec->FileIndex < 0) {
1358 dump_label_record(dev, rec, 1);
1360 switch (rec->FileIndex) {
1362 Pmsg0(000, "Volume is prelabeled. This tape cannot be scanned.\n");
1365 unser_volume_label(dev, rec);
1366 Pmsg3(000, "VOL_LABEL: block=%u size=%d vol=%s\n", block->BlockNumber,
1367 block->block_len, dev->VolHdr.VolName);
1371 unser_session_label(&label, rec);
1372 Pmsg1(000, "SOS_LABEL: JobId=%u\n", label.JobId);
1375 unser_session_label(&label, rec);
1376 Pmsg2(000, "EOS_LABEL: block=%u JobId=%u\n", block->BlockNumber,
1380 Pmsg0(000, "EOM_LABEL:\n");
1382 case EOT_LABEL: /* end of all tapes */
1385 if (LastBlock != block->BlockNumber) {
1386 VolBytes += block->block_len;
1388 LastBlock = block->BlockNumber;
1390 now -= jcr->run_time;
1394 kbs = (double)VolBytes / (1000 * now);
1395 Pmsg3(000, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
1396 edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
1398 Pmsg0(000, "End of all tapes.\n");
1406 if (LastBlock != block->BlockNumber) {
1407 VolBytes += block->block_len;
1409 if ((block->BlockNumber != LastBlock) && (block->BlockNumber % 50000) == 0) {
1412 now -= jcr->run_time;
1416 kbs = (double)VolBytes / (1000 * now);
1417 Pmsg3(000, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
1418 edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
1420 LastBlock = block->BlockNumber;
1422 Pmsg1(000, "End of all blocks. Block=%u\n", block->BlockNumber);
1429 * Write current block to tape regardless of whether or
1430 * not it is full. If the tape fills, attempt to
1431 * acquire another tape.
1433 static int flush_block(DEV_BLOCK *block, int dump)
1438 uint32_t this_file, this_block_num;
1441 this_block = new_block(dev);
1444 memcpy(this_block, block, sizeof(DEV_BLOCK));
1445 if (this_block->buf_len < block->buf_len) {
1446 free_memory(this_block->buf);
1447 this_block->buf = get_memory(block->buf_len);
1448 this_block->buf_len = block->buf_len;
1450 memcpy(this_block->buf, block->buf, this_block->buf_len);
1451 this_file = dev->file;
1452 this_block_num = dev->block_num;
1453 if (!write_block_to_dev(jcr, dev, block)) {
1454 Pmsg0(000, strerror_dev(dev));
1455 Pmsg3(000, "Block not written: FileIndex=%u Block=%u Size=%u\n",
1456 (unsigned)file_index, block->BlockNumber, block->block_len);
1458 dump_block(block, "Block not written");
1461 eot_block = block->BlockNumber;
1462 eot_block_len = block->block_len;
1463 eot_FileIndex = file_index;
1466 now -= jcr->run_time;
1470 kbs = (double)dev->VolCatInfo.VolCatBytes / (1000 * now);
1471 vol_size = dev->VolCatInfo.VolCatBytes;
1472 Pmsg2(000, "End of tape. VolumeCapacity=%s. Write rate = %.1f KB/s\n",
1473 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), kbs);
1476 stop = -1; /* stop, but do simplified test */
1478 /* Full test in progress */
1479 if (!fixup_device_block_write_error(jcr, dev, block)) {
1480 Pmsg1(000, _("Cannot fixup device error. %s\n"), strerror_dev(dev));
1488 return 1; /* end of tape reached */
1492 * Toggle between two allocated blocks for efficiency.
1493 * Switch blocks so that the block just successfully written is
1494 * always in last_block.
1496 tblock = last_block;
1497 last_block = this_block;
1498 this_block = tblock;
1499 last_file = this_file;
1500 last_block_num = this_block_num;
1507 struct cmdstruct { char *key; void (*func)(); char *help; };
1508 static struct cmdstruct commands[] = {
1509 {"bsf", bsfcmd, "backspace file"},
1510 {"bsr", bsrcmd, "backspace record"},
1511 {"cap", capcmd, "list device capabilities"},
1512 {"clear", clearcmd, "clear tape errors"},
1513 {"eod", eodcmd, "go to end of Bacula data for append"},
1514 {"eom", eomcmd, "go to the physical end of medium"},
1515 {"fill", fillcmd, "fill tape, write onto second volume"},
1516 {"unfill", unfillcmd, "read filled tape"},
1517 {"fsf", fsfcmd, "forward space a file"},
1518 {"fsr", fsrcmd, "forward space a record"},
1519 {"help", helpcmd, "print this command"},
1520 {"label", labelcmd, "write a Bacula label to the tape"},
1521 {"load", loadcmd, "load a tape"},
1522 {"quit", quitcmd, "quit btape"},
1523 {"readlabel", readlabelcmd, "read and print the Bacula tape label"},
1524 {"rectest", rectestcmd, "test record handling functions"},
1525 {"rewind", rewindcmd, "rewind the tape"},
1526 {"scan", scancmd, "read tape block by block to EOT and report"},
1527 {"status", statcmd, "print tape status"},
1528 {"test", testcmd, "General test Bacula tape functions"},
1529 {"weof", weofcmd, "write an EOF on the tape"},
1530 {"wr", wrcmd, "write a single Bacula block"},
1531 {"rr", rrcmd, "read a single record"},
1533 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
1541 while (get_cmd("*")) {
1542 sm_check(__FILE__, __LINE__, False);
1544 for (i=0; i<comsize; i++) /* search for command */
1545 if (fstrsch(cmd, commands[i].key)) {
1546 (*commands[i].func)(); /* go execute command */
1551 Pmsg1(0, _("%s is an illegal command\n"), cmd);
1557 static void helpcmd()
1561 printf(_(" Command Description\n ======= ===========\n"));
1562 for (i=0; i<comsize; i++)
1563 printf(" %-10s %s\n", commands[i].key, commands[i].help);
1570 "\nVersion: " VERSION " (" BDATE ")\n\n"
1571 "Usage: btape [-c config_file] [-d debug_level] [device_name]\n"
1572 " -c <file> set configuration file to file\n"
1573 " -dnn set debug level to nn\n"
1574 " -s turn off signals\n"
1575 " -t open the default tape device\n"
1576 " -? print this message.\n"
1582 * Get next input command from terminal. This
1583 * routine is REALLY primitive, and should be enhanced
1584 * to have correct backspacing, etc.
1587 get_cmd(char *prompt)
1591 fprintf(stdout, prompt);
1593 /* We really should turn off echoing and pretty this
1597 while ((ch = fgetc(stdin)) != EOF) {
1599 strip_trailing_junk(cmd);
1601 } else if (ch == 4 || ch == 0xd3 || ch == 0x8) {
1614 /* Dummies to replace askdir.c */
1615 int dir_get_volume_info(JCR *jcr, int writing) { return 1;}
1616 int dir_find_next_appendable_volume(JCR *jcr) { return 1;}
1617 int dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
1618 int dir_create_jobmedia_record(JCR *jcr) { return 1; }
1619 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
1620 int dir_send_job_status(JCR *jcr) {return 1;}
1623 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
1625 Pmsg0(000, dev->errmsg); /* print reason */
1626 fprintf(stderr, "Mount Volume \"%s\" on device %s and press return when ready: ",
1627 jcr->VolumeName, dev_name(dev));
1632 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev)
1634 fprintf(stderr, "Mount next Volume on device %s and press return when ready: ",
1637 VolumeName = "TestVolume2";
1643 static int my_mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
1647 Pmsg1(000, "End of Volume \"%s\"\n", jcr->VolumeName);
1649 if (LastBlock != block->BlockNumber) {
1650 VolBytes += block->block_len;
1652 LastBlock = block->BlockNumber;
1654 now -= jcr->run_time;
1658 kbs = (double)VolBytes / (1000 * now);
1659 Pmsg3(000, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
1660 edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
1662 if (strcmp(jcr->VolumeName, "TestVolume2") == 0) {
1668 pm_strcpy(&jcr->VolumeName, "TestVolume2");
1670 create_vol_list(jcr);
1672 dev->state &= ~ST_READ;
1673 if (!acquire_device_for_read(jcr, dev, block)) {
1674 Pmsg2(0, "Cannot open Dev=%s, Vol=%s\n", dev_name(dev), jcr->VolumeName);
1677 return 1; /* next volume mounted */