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-2004 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();
42 extern char *edit_device_codes(JCR *jcr, char *omsg, const char *imsg, const char *cmd);
44 /* Exported variables */
47 int bsize = TAPE_BSIZE;
48 char VolName[MAX_NAME_LENGTH];
49 bool forge_on = false;
52 * If you change the format of the state file,
53 * increment this value
55 static uint32_t btape_state_level = 2;
59 DEVRES *device = NULL;
62 /* Forward referenced subroutines */
63 static void do_tape_cmds();
64 static void helpcmd();
65 static void scancmd();
66 static void rewindcmd();
67 static void clearcmd();
71 static void fillcmd();
72 static void qfillcmd();
73 static void statcmd();
74 static void unfillcmd();
75 static int flush_block(DEV_BLOCK *block, int dump);
76 static bool quickie_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec);
77 static bool compare_blocks(DEV_BLOCK *last_block, DEV_BLOCK *block);
78 static bool my_mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block);
79 static void scan_blocks();
80 static void set_volume_name(const char *VolName, int volnum);
81 static void rawfill_cmd();
82 static void bfill_cmd();
83 static bool open_the_device();
84 static void autochangercmd();
85 static void do_unfill();
88 /* Static variables */
89 #define CONFIG_FILE "bacula-sd.conf"
92 #define MAX_CMD_ARGS 30
95 static char *argk[MAX_CMD_ARGS];
96 static char *argv[MAX_CMD_ARGS];
99 static int quickie_count = 0;
100 static BSR *bsr = NULL;
101 static int signals = TRUE;
104 static uint64_t vol_size;
105 static uint64_t VolBytes;
108 static int32_t file_index;
109 static int end_of_tape = 0;
110 static uint32_t LastBlock = 0;
111 static uint32_t eot_block;
112 static uint32_t eot_block_len;
113 static uint32_t eot_FileIndex;
114 static int dumped = 0;
115 static DEV_BLOCK *last_block1 = NULL;
116 static DEV_BLOCK *last_block2 = NULL;
117 static DEV_BLOCK *last_block = NULL;
118 static DEV_BLOCK *this_block = NULL;
119 static DEV_BLOCK *first_block = NULL;
120 static uint32_t last_file1 = 0;
121 static uint32_t last_file2 = 0;
122 static uint32_t last_file = 0;
123 static uint32_t last_block_num1 = 0;
124 static uint32_t last_block_num2 = 0;
125 static uint32_t last_block_num = 0;
126 static uint32_t BlockNumber = 0;
127 static bool simple = true;
129 static const char *VolumeName = NULL;
130 static int vol_num = 0;
132 static JCR *jcr = NULL;
136 static void terminate_btape(int sig);
137 int get_cmd(const char *prompt);
140 /*********************************************************************
142 * Main Bacula Pool Creation Program
145 int main(int margc, char *margv[])
150 if (TAPE_BSIZE % DEV_BSIZE != 0 || TAPE_BSIZE / DEV_BSIZE == 0) {
151 Emsg2(M_ABORT, 0, "Tape block size (%d) not multiple of system size (%d)\n",
152 TAPE_BSIZE, DEV_BSIZE);
154 if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
155 Emsg1(M_ABORT, 0, "Tape block size (%d) is not a power of 2\n", TAPE_BSIZE);
158 printf("Tape block granularity is %d bytes.\n", TAPE_BSIZE);
160 working_directory = "/tmp";
161 my_name_is(margc, margv, "btape");
162 init_msg(NULL, NULL);
164 while ((ch = getopt(margc, margv, "b:c:d:psv?")) != -1) {
166 case 'b': /* bootstrap file */
167 bsr = parse_bsr(NULL, optarg);
168 // dump_bsr(bsr, true);
171 case 'c': /* specify config file */
172 if (configfile != NULL) {
175 configfile = bstrdup(optarg);
178 case 'd': /* set debug level */
179 debug_level = atoi(optarg);
180 if (debug_level <= 0) {
207 cmd = get_pool_memory(PM_FNAME);
208 args = get_pool_memory(PM_FNAME);
211 init_signals(terminate_btape);
214 if (configfile == NULL) {
215 configfile = bstrdup(CONFIG_FILE);
218 daemon_start_time = time(NULL);
220 parse_config(configfile);
223 /* See if we can open a device */
225 Pmsg0(000, "No archive name specified.\n");
228 } else if (margc != 1) {
229 Pmsg0(000, "Improper number of arguments specified.\n");
234 jcr = setup_jcr("btape", margv[0], bsr, NULL);
235 dev = setup_to_access_device(jcr, 0); /* acquire for write */
240 if (!open_the_device()) {
244 Dmsg0(200, "Do tape commands\n");
252 static void terminate_btape(int stat)
255 sm_check(__FILE__, __LINE__, false);
259 free_config_resources();
261 free_pool_memory(args);
265 free_pool_memory(cmd);
273 if (debug_level > 10)
274 print_memory_pool_stats();
284 free_block(this_block);
288 close_memory_pool(); /* free memory in pool */
294 static bool open_the_device()
298 block = new_block(dev);
300 if (!(dev->state & ST_OPENED)) {
301 Dmsg1(200, "Opening device %s\n", jcr->VolumeName);
302 if (open_dev(dev, jcr->VolumeName, OPEN_READ_WRITE) < 0) {
303 Emsg1(M_FATAL, 0, _("dev open failed: %s\n"), dev->errmsg);
309 Pmsg1(000, "open_dev %s OK\n", dev_name(dev));
322 * Write a label to the tape
324 static void labelcmd()
327 pm_strcpy(&cmd, VolumeName);
329 if (!get_cmd("Enter Volume Name: ")) {
334 if (!(dev->state & ST_OPENED)) {
335 if (!first_open_device(dev)) {
336 Pmsg1(0, "Device open failed. ERR=%s\n", strerror_dev(dev));
340 write_new_volume_label_to_dev(dcr, cmd, "Default");
341 Pmsg1(-1, "Wrote Volume label for volume \"%s\".\n", cmd);
345 * Read the tape label
347 static void readlabelcmd()
349 int save_debug_level = debug_level;
353 block = new_block(dev);
354 stat = read_dev_volume_label(dcr, block);
357 Pmsg0(0, "Volume has no label.\n");
360 Pmsg0(0, "Volume label read correctly.\n");
363 Pmsg1(0, "I/O error on device: ERR=%s", strerror_dev(dev));
366 Pmsg0(0, "Volume name error\n");
368 case VOL_CREATE_ERROR:
369 Pmsg1(0, "Error creating label. ERR=%s", strerror_dev(dev));
371 case VOL_VERSION_ERROR:
372 Pmsg0(0, "Volume version error.\n");
374 case VOL_LABEL_ERROR:
375 Pmsg0(0, "Bad Volume label type.\n");
378 Pmsg0(0, "Unknown error.\n");
383 dump_volume_label(dev);
384 debug_level = save_debug_level;
390 * Load the tape should have prevously been taken
391 * off line, otherwise this command is not necessary.
393 static void loadcmd()
396 if (!load_dev(dev)) {
397 Pmsg1(0, "Bad status from load. ERR=%s\n", strerror_dev(dev));
399 Pmsg1(0, "Loaded %s\n", dev_name(dev));
405 static void rewindcmd()
407 if (!rewind_dev(dev)) {
408 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
409 clrerror_dev(dev, -1);
411 Pmsg1(0, "Rewound %s\n", dev_name(dev));
416 * Clear any tape error
418 static void clearcmd()
420 clrerror_dev(dev, -1);
424 * Write and end of file on the tape
426 static void weofcmd()
437 if ((stat = weof_dev(dev, num)) < 0) {
438 Pmsg2(0, "Bad status from weof %d. ERR=%s\n", stat, strerror_dev(dev));
441 Pmsg3(0, "Wrote %d EOF%s to %s\n", num, num==1?"":"s", dev_name(dev));
446 /* Go to the end of the medium -- raw command
447 * The idea was orginally that the end of the Bacula
448 * medium would be flagged differently. This is not
449 * currently the case. So, this is identical to the
455 Pmsg1(0, "%s", strerror_dev(dev));
458 Pmsg0(0, _("Moved to end of medium.\n"));
463 * Go to the end of the medium (either hardware determined
464 * or defined by two eofs.
484 if (!bsf_dev(dev, num)) {
485 Pmsg1(0, _("Bad status from bsf. ERR=%s\n"), strerror_dev(dev));
487 Pmsg2(0, _("Backspaced %d file%s.\n"), num, num==1?"":"s");
503 if (!bsr_dev(dev, num)) {
504 Pmsg1(0, _("Bad status from bsr. ERR=%s\n"), strerror_dev(dev));
506 Pmsg2(0, _("Backspaced %d record%s.\n"), num, num==1?"":"s");
511 * List device capabilities as defined in the
516 printf(_("Configured device capabilities:\n"));
517 printf("%sEOF ", dev->capabilities & CAP_EOF ? "" : "!");
518 printf("%sBSR ", dev->capabilities & CAP_BSR ? "" : "!");
519 printf("%sBSF ", dev->capabilities & CAP_BSF ? "" : "!");
520 printf("%sFSR ", dev->capabilities & CAP_FSR ? "" : "!");
521 printf("%sFSF ", dev->capabilities & CAP_FSF ? "" : "!");
522 printf("%sFASTFSF ", dev->capabilities & CAP_FASTFSF ? "" : "!");
523 printf("%sEOM ", dev->capabilities & CAP_EOM ? "" : "!");
524 printf("%sREM ", dev->capabilities & CAP_REM ? "" : "!");
525 printf("%sRACCESS ", dev->capabilities & CAP_RACCESS ? "" : "!");
526 printf("%sAUTOMOUNT ", dev->capabilities & CAP_AUTOMOUNT ? "" : "!");
527 printf("%sLABEL ", dev->capabilities & CAP_LABEL ? "" : "!");
528 printf("%sANONVOLS ", dev->capabilities & CAP_ANONVOLS ? "" : "!");
529 printf("%sALWAYSOPEN ", dev->capabilities & CAP_ALWAYSOPEN ? "" : "!");
532 printf(_("Device status:\n"));
533 printf("%sOPENED ", dev->state & ST_OPENED ? "" : "!");
534 printf("%sTAPE ", dev->state & ST_TAPE ? "" : "!");
535 printf("%sLABEL ", dev->state & ST_LABEL ? "" : "!");
536 printf("%sMALLOC ", dev->state & ST_MALLOC ? "" : "!");
537 printf("%sAPPEND ", dev->state & ST_APPEND ? "" : "!");
538 printf("%sREAD ", dev->state & ST_READ ? "" : "!");
539 printf("%sEOT ", dev->state & ST_EOT ? "" : "!");
540 printf("%sWEOT ", dev->state & ST_WEOT ? "" : "!");
541 printf("%sEOF ", dev->state & ST_EOF ? "" : "!");
542 printf("%sNEXTVOL ", dev->state & ST_NEXTVOL ? "" : "!");
543 printf("%sSHORT ", dev->state & ST_SHORT ? "" : "!");
546 printf(_("Device parameters:\n"));
547 printf("Device name: %s\n", dev->dev_name);
548 printf("File=%u block=%u\n", dev->file, dev->block_num);
549 printf("Min block=%u Max block=%u\n", dev->min_block_size, dev->max_block_size);
557 * Test writting larger and larger records.
558 * This is a torture test for records.
560 static void rectestcmd()
566 Pmsg0(0, "Test writting larger and larger records.\n"
567 "This is a torture test for records.\nI am going to write\n"
568 "larger and larger records. It will stop when the record size\n"
569 "plus the header exceeds the block size (by default about 64K)\n");
572 get_cmd("Do you want to continue? (y/n): ");
574 Pmsg0(000, "Command aborted.\n");
578 sm_check(__FILE__, __LINE__, false);
579 block = new_block(dev);
582 for (i=1; i<500000; i++) {
583 rec->data = check_pool_memory_size(rec->data, i);
584 memset(rec->data, i & 0xFF, i);
586 sm_check(__FILE__, __LINE__, false);
587 if (write_record_to_block(block, rec)) {
590 Pmsg2(0, "Block %d i=%d\n", blkno, i);
594 sm_check(__FILE__, __LINE__, false);
598 sm_check(__FILE__, __LINE__, false);
602 * This test attempts to re-read a block written by Bacula
603 * normally at the end of the tape. Bacula will then back up
604 * over the two eof marks, backup over the record and reread
605 * it to make sure it is valid. Bacula can skip this validation
606 * if you set "Backward space record = no"
608 static int re_read_block_test()
615 if (!(dev->capabilities & CAP_BSR)) {
616 Pmsg0(-1, _("Skipping read backwards test because BSR turned off.\n"));
620 Pmsg0(-1, _("\n=== Write, backup, and re-read test ===\n\n"
621 "I'm going to write three records and an EOF\n"
622 "then backup over the EOF and re-read the last record.\n"
623 "Bacula does this after writing the last block on the\n"
624 "tape to verify that the block was written correctly.\n\n"
625 "This is not an *essential* feature ...\n\n"));
627 block = new_block(dev);
629 rec->data = check_pool_memory_size(rec->data, block->buf_len);
630 len = rec->data_len = block->buf_len-100;
631 memset(rec->data, 1, rec->data_len);
632 if (!write_record_to_block(block, rec)) {
633 Pmsg0(0, _("Error writing record to block.\n"));
636 if (!write_block_to_dev(dcr, block)) {
637 Pmsg0(0, _("Error writing block to device.\n"));
640 Pmsg1(0, _("Wrote first record of %d bytes.\n"), rec->data_len);
642 memset(rec->data, 2, rec->data_len);
643 if (!write_record_to_block(block, rec)) {
644 Pmsg0(0, _("Error writing record to block.\n"));
647 if (!write_block_to_dev(dcr, block)) {
648 Pmsg0(0, _("Error writing block to device.\n"));
651 Pmsg1(0, _("Wrote second record of %d bytes.\n"), rec->data_len);
653 memset(rec->data, 3, rec->data_len);
654 if (!write_record_to_block(block, rec)) {
655 Pmsg0(0, _("Error writing record to block.\n"));
658 if (!write_block_to_dev(dcr, block)) {
659 Pmsg0(0, _("Error writing block to device.\n"));
662 Pmsg1(0, _("Wrote third record of %d bytes.\n"), rec->data_len);
665 if (dev_cap(dev, CAP_TWOEOF)) {
668 if (!bsf_dev(dev, 1)) {
669 Pmsg1(0, _("Backspace file failed! ERR=%s\n"), strerror_dev(dev));
672 if (dev_cap(dev, CAP_TWOEOF)) {
673 if (!bsf_dev(dev, 1)) {
674 Pmsg1(0, _("Backspace file failed! ERR=%s\n"), strerror_dev(dev));
678 Pmsg0(0, "Backspaced over EOF OK.\n");
679 if (!bsr_dev(dev, 1)) {
680 Pmsg1(0, _("Backspace record failed! ERR=%s\n"), strerror_dev(dev));
683 Pmsg0(0, "Backspace record OK.\n");
684 if (!read_block_from_dev(dcr, block, NO_BLOCK_NUMBER_CHECK)) {
685 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
688 memset(rec->data, 0, rec->data_len);
689 if (!read_record_from_block(block, rec)) {
690 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
693 for (int i=0; i<len; i++) {
694 if (rec->data[i] != 3) {
695 Pmsg0(0, _("Bad data in record. Test failed!\n"));
699 Pmsg0(0, _("\nBlock re-read correct. Test succeeded!\n"));
700 Pmsg0(-1, _("=== End Write, backup, and re-read test ===\n\n"));
708 Pmsg0(0, _("This is not terribly serious since Bacula only uses\n"
709 "this function to verify the last block written to the\n"
710 "tape. Bacula will skip the last block verification\n"
712 "Backward Space Record = No\n\n"
713 "to your Storage daemon's Device resource definition.\n"));
720 * This test writes Bacula blocks to the tape in
721 * several files. It then rewinds the tape and attepts
722 * to read these blocks back checking the data.
724 static int write_read_test()
732 Pmsg0(-1, _("\n=== Write, rewind, and re-read test ===\n\n"
733 "I'm going to write 1000 records and an EOF\n"
734 "then write 1000 records and an EOF, then rewind,\n"
735 "and re-read the data to verify that it is correct.\n\n"
736 "This is an *essential* feature ...\n\n"));
737 block = new_block(dev);
739 if (!rewind_dev(dev)) {
740 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
743 rec->data = check_pool_memory_size(rec->data, block->buf_len);
744 rec->data_len = block->buf_len-100;
745 len = rec->data_len/sizeof(i);
746 for (i=1; i<=1000; i++) {
747 p = (int *)rec->data;
748 for (j=0; j<len; j++) {
751 if (!write_record_to_block(block, rec)) {
752 Pmsg0(0, _("Error writing record to block.\n"));
755 if (!write_block_to_dev(dcr, block)) {
756 Pmsg0(0, _("Error writing block to device.\n"));
760 Pmsg1(0, _("Wrote 1000 blocks of %d bytes.\n"), rec->data_len);
762 for (i=1001; i<=2000; i++) {
763 p = (int *)rec->data;
764 for (j=0; j<len; j++) {
767 if (!write_record_to_block(block, rec)) {
768 Pmsg0(0, _("Error writing record to block.\n"));
771 if (!write_block_to_dev(dcr, block)) {
772 Pmsg0(0, _("Error writing block to device.\n"));
776 Pmsg1(0, _("Wrote 1000 blocks of %d bytes.\n"), rec->data_len);
778 if (dev_cap(dev, CAP_TWOEOF)) {
781 if (!rewind_dev(dev)) {
782 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
785 Pmsg0(0, "Rewind OK.\n");
787 for (i=1; i<=2000; i++) {
789 if (!read_block_from_dev(dcr, block, NO_BLOCK_NUMBER_CHECK)) {
790 if (dev_state(dev, ST_EOF)) {
791 Pmsg0(-1, _("Got EOF on tape.\n"));
794 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
797 memset(rec->data, 0, rec->data_len);
798 if (!read_record_from_block(block, rec)) {
799 Pmsg1(0, _("Read record failed! ERR=%s\n"), strerror(dev->dev_errno));
802 p = (int *)rec->data;
803 for (j=0; j<len; j++) {
805 Pmsg3(0, _("Bad data in record. Expected %d, got %d at byte %d. Test failed!\n"),
811 if (i == 1000 || i == 2000) {
812 Pmsg0(-1, _("1000 blocks re-read correctly.\n"));
815 Pmsg0(-1, _("=== Test Succeeded. End Write, rewind, and re-read test ===\n\n"));
825 * This test writes Bacula blocks to the tape in
826 * several files. It then rewinds the tape and attepts
827 * to read these blocks back checking the data.
829 static int position_test()
837 int file = 0, blk = 0;
840 Pmsg0(-1, _("\n=== Write, rewind, and position test ===\n\n"
841 "I'm going to write 1000 records and an EOF\n"
842 "then write 1000 records and an EOF, then rewind,\n"
843 "and position to a few blocks and verify that it is correct.\n\n"
844 "This is an *essential* feature ...\n\n"));
845 block = new_block(dev);
847 if (!rewind_dev(dev)) {
848 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
851 rec->data = check_pool_memory_size(rec->data, block->buf_len);
852 rec->data_len = block->buf_len-100;
853 len = rec->data_len/sizeof(i);
854 for (i=1; i<=1000; i++) {
855 p = (int *)rec->data;
856 for (j=0; j<len; j++) {
859 if (!write_record_to_block(block, rec)) {
860 Pmsg0(0, _("Error writing record to block.\n"));
863 if (!write_block_to_dev(dcr, block)) {
864 Pmsg0(0, _("Error writing block to device.\n"));
868 Pmsg1(0, _("Wrote 1000 blocks of %d bytes.\n"), rec->data_len);
870 for (i=1001; i<=2000; i++) {
871 p = (int *)rec->data;
872 for (j=0; j<len; j++) {
875 if (!write_record_to_block(block, rec)) {
876 Pmsg0(0, _("Error writing record to block.\n"));
879 if (!write_block_to_dev(dcr, block)) {
880 Pmsg0(0, _("Error writing block to device.\n"));
884 Pmsg1(0, _("Wrote 1000 blocks of %d bytes.\n"), rec->data_len);
886 if (dev_cap(dev, CAP_TWOEOF)) {
889 if (!rewind_dev(dev)) {
890 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
893 Pmsg0(0, "Rewind OK.\n");
897 /* Set up next item to read based on where we are */
933 Pmsg2(-1, "Reposition to file:block %d:%d\n", file, blk);
934 if (!reposition_dev(dev, file, blk)) {
935 Pmsg0(0, "Reposition error.\n");
939 if (!read_block_from_dev(dcr, block, NO_BLOCK_NUMBER_CHECK)) {
940 if (dev_state(dev, ST_EOF)) {
941 Pmsg0(-1, _("Got EOF on tape.\n"));
944 Pmsg1(0, _("Read block failed! ERR=%s\n\n"), strerror(dev->dev_errno));
945 Pmsg0(0, _("This may be because the tape drive block size is not\n"
946 " set to variable blocking as normally used by Bacula.\n"
947 " Please see the Tape Testing chapter in the manual and \n"
948 " look for using mt with defblksize and setoptions\n"));
951 memset(rec->data, 0, rec->data_len);
952 if (!read_record_from_block(block, rec)) {
953 Pmsg1(0, _("Read record failed! ERR=%s\n"), strerror(dev->dev_errno));
956 p = (int *)rec->data;
957 for (j=0; j<len; j++) {
959 Pmsg3(0, _("Bad data in record. Expected %d, got %d at byte %d. Test failed!\n"),
964 Pmsg0(-1, _("Block re-read correctly.\n"));
966 Pmsg0(-1, _("=== Test Succeeded. End Write, rewind, and re-read test ===\n\n"));
979 * This test writes some records, then writes an end of file,
980 * rewinds the tape, moves to the end of the data and attepts
981 * to append to the tape. This function is essential for
982 * Bacula to be able to write multiple jobs to the tape.
984 static int append_test()
986 Pmsg0(-1, _("\n\n=== Append files test ===\n\n"
987 "This test is essential to Bacula.\n\n"
988 "I'm going to write one record in file 0,\n"
989 " two records in file 1,\n"
990 " and three records in file 2\n\n"));
994 weofcmd(); /* end file 0 */
997 weofcmd(); /* end file 1 */
1001 weofcmd(); /* end file 2 */
1002 if (dev_cap(dev, CAP_TWOEOF)) {
1006 Pmsg0(0, _("Now moving to end of medium.\n"));
1008 Pmsg2(-1, _("We should be in file 3. I am at file %d. This is %s\n"),
1009 dev->file, dev->file == 3 ? "correct!" : "NOT correct!!!!");
1011 if (dev->file != 3) {
1015 Pmsg0(-1, _("\nNow the important part, I am going to attempt to append to the tape.\n\n"));
1018 if (dev_cap(dev, CAP_TWOEOF)) {
1022 Pmsg0(-1, _("Done appending, there should be no I/O errors\n\n"));
1023 Pmsg0(-1, "Doing Bacula scan of blocks:\n");
1025 Pmsg0(-1, _("End scanning the tape.\n"));
1026 Pmsg2(-1, _("We should be in file 4. I am at file %d. This is %s\n"),
1027 dev->file, dev->file == 4 ? "correct!" : "NOT correct!!!!");
1029 if (dev->file != 4) {
1037 * This test exercises the autochanger
1039 static int autochanger_test()
1041 POOLMEM *results, *changer;
1042 int slot, status, loaded;
1043 int timeout = jcr->device->max_changer_wait;
1046 Dmsg1(100, "Max changer wait = %d sec\n", timeout);
1047 if (!dev_cap(dev, CAP_AUTOCHANGER)) {
1050 if (!(jcr->device && jcr->device->changer_name && jcr->device->changer_command)) {
1051 Pmsg0(-1, "\nAutochanger enabled, but no name or no command device specified.\n");
1055 Pmsg0(-1, "\nAh, I see you have an autochanger configured.\n"
1056 "To test the autochanger you must have a blank tape\n"
1057 " that I can write on in Slot 1.\n");
1058 if (!get_cmd("\nDo you wish to continue with the Autochanger test? (y/n): ")) {
1061 if (cmd[0] != 'y' && cmd[0] != 'Y') {
1065 Pmsg0(-1, _("\n\n=== Autochanger test ===\n\n"));
1067 results = get_pool_memory(PM_MESSAGE);
1068 changer = get_pool_memory(PM_FNAME);
1072 jcr->VolCatInfo.Slot = slot;
1073 /* Find out what is loaded, zero means device is unloaded */
1074 Pmsg0(-1, _("3301 Issuing autochanger \"loaded\" command.\n"));
1075 changer = edit_device_codes(jcr, changer, jcr->device->changer_command,
1077 status = run_program(changer, timeout, results);
1078 Dmsg3(100, "run_prog: %s stat=%d result=%s\n", changer, status, results);
1080 loaded = atoi(results);
1082 Pmsg1(-1, _("3991 Bad autochanger command: %s\n"), changer);
1083 Pmsg2(-1, _("3991 status=%d result=%s\n"), status, results);
1087 Pmsg1(-1, "Slot %d loaded. I am going to unload it.\n", loaded);
1089 Pmsg0(-1, "Nothing loaded in the drive. OK.\n");
1091 Dmsg1(100, "Results from loaded query=%s\n", results);
1093 jcr->VolCatInfo.Slot = loaded;
1094 offline_or_rewind_dev(dev);
1095 /* We are going to load a new tape, so close the device */
1096 force_close_dev(dev);
1097 Pmsg2(-1, _("3302 Issuing autochanger \"unload %d %d\" command.\n"),
1098 loaded, dev->drive_index);
1099 changer = edit_device_codes(jcr, changer,
1100 jcr->device->changer_command, "unload");
1101 status = run_program(changer, timeout, results);
1102 Pmsg2(-1, "unload status=%s %d\n", status==0?"OK":"Bad", status);
1104 Pmsg1(-1, _("3992 Bad autochanger command: %s\n"), changer);
1105 Pmsg2(-1, _("3992 status=%d result=%s\n"), status, results);
1114 jcr->VolCatInfo.Slot = slot;
1115 Pmsg2(-1, _("3303 Issuing autochanger \"load slot %d %d\" command.\n"),
1116 slot, dev->drive_index);
1117 changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "load");
1118 Dmsg1(100, "Changer=%s\n", changer);
1119 force_close_dev(dev);
1120 status = run_program(changer, timeout, results);
1122 Pmsg2(-1, _("3303 Autochanger \"load slot %d %d\" status is OK.\n"),
1123 slot, dev->drive_index);
1125 Pmsg1(-1, _("3993 Bad autochanger command: %s\n"), changer);
1126 Pmsg2(-1, _("3993 status=%d result=%s\n"), status, results);
1130 if (!open_the_device()) {
1133 bmicrosleep(sleep_time, 0);
1134 if (!rewind_dev(dev)) {
1135 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
1136 clrerror_dev(dev, -1);
1137 Pmsg0(-1, "\nThe test failed, probably because you need to put\n"
1138 "a longer sleep time in the mtx-script in the load) case.\n"
1139 "Adding a 30 second sleep and trying again ...\n");
1143 Pmsg1(0, "Rewound %s\n", dev_name(dev));
1146 if ((status = weof_dev(dev, 1)) < 0) {
1147 Pmsg2(0, "Bad status from weof %d. ERR=%s\n", status, strerror_dev(dev));
1150 Pmsg1(0, "Wrote EOF to %s\n", dev_name(dev));
1154 Pmsg1(-1, "\nThe test worked this time. Please add:\n\n"
1156 "to your mtx-changer script in the load) case.\n\n",
1159 Pmsg0(-1, "\nThe test autochanger worked!!\n\n");
1162 free_pool_memory(changer);
1163 free_pool_memory(results);
1168 free_pool_memory(changer);
1169 free_pool_memory(results);
1170 Pmsg0(-1, "You must correct this error or the Autochanger will not work.\n");
1174 static void autochangercmd()
1181 * This test assumes that the append test has been done,
1182 * then it tests the fsf function.
1184 static int fsf_test()
1186 bool set_off = false;
1188 Pmsg0(-1, _("\n\n=== Forward space files test ===\n\n"
1189 "This test is essential to Bacula.\n\n"
1190 "I'm going to write five files then test forward spacing\n\n"));
1194 weofcmd(); /* end file 0 */
1197 weofcmd(); /* end file 1 */
1201 weofcmd(); /* end file 2 */
1204 weofcmd(); /* end file 3 */
1206 weofcmd(); /* end file 4 */
1207 if (dev_cap(dev, CAP_TWOEOF)) {
1213 Pmsg0(0, _("Now forward spacing 1 file.\n"));
1214 if (!fsf_dev(dev, 1)) {
1215 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1218 Pmsg2(-1, _("We should be in file 1. I am at file %d. This is %s\n"),
1219 dev->file, dev->file == 1 ? "correct!" : "NOT correct!!!!");
1221 if (dev->file != 1) {
1225 Pmsg0(0, _("Now forward spacing 2 files.\n"));
1226 if (!fsf_dev(dev, 2)) {
1227 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1230 Pmsg2(-1, _("We should be in file 3. I am at file %d. This is %s\n"),
1231 dev->file, dev->file == 3 ? "correct!" : "NOT correct!!!!");
1233 if (dev->file != 3) {
1238 Pmsg0(0, _("Now forward spacing 4 files.\n"));
1239 if (!fsf_dev(dev, 4)) {
1240 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1243 Pmsg2(-1, _("We should be in file 4. I am at file %d. This is %s\n"),
1244 dev->file, dev->file == 4 ? "correct!" : "NOT correct!!!!");
1246 if (dev->file != 4) {
1250 Pmsg0(-1, "The test worked this time. Please add:\n\n"
1251 " Fast Forward Space File = no\n\n"
1252 "to your Device resource for this drive.\n");
1256 Pmsg0(0, _("Now forward spacing 1 more file.\n"));
1257 if (!fsf_dev(dev, 1)) {
1258 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1260 Pmsg2(-1, _("We should be in file 5. I am at file %d. This is %s\n"),
1261 dev->file, dev->file == 5 ? "correct!" : "NOT correct!!!!");
1262 if (dev->file != 5) {
1265 Pmsg0(-1, _("\n=== End Forward space files test ===\n\n"));
1269 Pmsg0(-1, _("\nThe forward space file test failed.\n"));
1270 if (dev_cap(dev, CAP_FASTFSF)) {
1271 Pmsg0(-1, "You have Fast Forward Space File enabled.\n"
1272 "I am turning it off then retrying the test.\n");
1273 dev->capabilities &= ~CAP_FASTFSF;
1277 Pmsg0(-1, "You must correct this error or Bacula will not work.\n");
1286 * This is a general test of Bacula's functions
1287 * needed to read and write the tape.
1289 static void testcmd()
1293 if (!write_read_test()) {
1296 if (!position_test()) {
1300 stat = append_test();
1301 if (stat == 1) { /* OK get out */
1304 if (stat == -1) { /* first test failed */
1305 if (dev_cap(dev, CAP_EOM) || dev_cap(dev, CAP_FASTFSF)) {
1306 Pmsg0(-1, "\nAppend test failed. Attempting again.\n"
1307 "Setting \"Hardware End of Medium = no\n"
1308 " and \"Fast Forward Space File = no\n"
1309 "and retrying append test.\n\n");
1310 dev->capabilities &= ~CAP_EOM; /* turn off eom */
1311 dev->capabilities &= ~CAP_FASTFSF; /* turn off fast fsf */
1312 stat = append_test();
1314 Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n"
1315 " Hardware End of Medium = No\n\n"
1316 " Fast Forward Space File = No\n"
1317 "to your Device resource in the Storage conf file.\n");
1321 Pmsg0(-1, "\n\nThat appears *NOT* to have corrected the problem.\n");
1324 /* Wrong count after append */
1326 Pmsg0(-1, "\n\nIt looks like the append failed. Attempting again.\n"
1327 "Setting \"BSF at EOM = yes\" and retrying append test.\n");
1328 dev->capabilities |= CAP_BSFATEOM; /* backspace on eom */
1329 stat = append_test();
1331 Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n"
1332 " Hardware End of Medium = No\n"
1333 " Fast Forward Space File = No\n"
1334 " BSF at EOM = yes\n\n"
1335 "to your Device resource in the Storage conf file.\n");
1342 Pmsg0(-1, "\nAppend test failed.\n\n");
1343 Pmsg0(-1, "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
1344 "Unable to correct the problem. You MUST fix this\n"
1345 "problem before Bacula can use your tape drive correctly\n");
1346 Pmsg0(-1, "\nPerhaps running Bacula in fixed block mode will work.\n"
1347 "Do so by setting:\n\n"
1348 "Minimum Block Size = nnn\n"
1349 "Maximum Block Size = nnn\n\n"
1350 "in your Storage daemon's Device definition.\n"
1351 "nnn must match your tape driver's block size, which\n"
1352 "can be determined by reading your tape manufacturers\n"
1353 "information, and the information on your kernel dirver.\n"
1354 "Fixed block sizes, however, are not normally an ideal solution.\n");
1359 Pmsg0(-1, _("\nThe above Bacula scan should have output identical to what follows.\n"
1360 "Please double check it ...\n"
1361 "=== Sample correct output ===\n"
1362 "1 block of 64448 bytes in file 1\n"
1363 "End of File mark.\n"
1364 "2 blocks of 64448 bytes in file 2\n"
1365 "End of File mark.\n"
1366 "3 blocks of 64448 bytes in file 3\n"
1367 "End of File mark.\n"
1368 "1 block of 64448 bytes in file 4\n"
1369 "End of File mark.\n"
1370 "Total files=4, blocks=7, bytes = 451,136\n"
1371 "=== End sample correct output ===\n\n"));
1373 Pmsg0(-1, _("If the above scan output is not identical to the\n"
1374 "sample output, you MUST correct the problem\n"
1375 "or Bacula will not be able to write multiple Jobs to \n"
1379 re_read_block_test();
1382 fsf_test(); /* do fast forward space file test */
1384 autochanger_test(); /* do autochanger test */
1388 /* Forward space a file */
1389 static void fsfcmd()
1393 num = atoi(argk[1]);
1398 if (!fsf_dev(dev, num)) {
1399 Pmsg1(0, "Bad status from fsf. ERR=%s\n", strerror_dev(dev));
1402 Pmsg2(0, "Forward spaced %d file%s.\n", num, num==1?"":"s");
1405 /* Forward space a record */
1406 static void fsrcmd()
1410 num = atoi(argk[1]);
1415 if (!fsr_dev(dev, num)) {
1416 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1419 Pmsg2(0, "Forward spaced %d record%s.\n", num, num==1?"":"s");
1424 * Write a Bacula block to the tape
1432 sm_check(__FILE__, __LINE__, false);
1433 block = new_block(dev);
1435 dump_block(block, "test");
1437 i = block->buf_len - 100;
1439 rec->data = check_pool_memory_size(rec->data, i);
1440 memset(rec->data, i & 0xFF, i);
1442 sm_check(__FILE__, __LINE__, false);
1443 if (!write_record_to_block(block, rec)) {
1444 Pmsg0(0, _("Error writing record to block.\n"));
1447 if (!write_block_to_dev(dcr, block)) {
1448 Pmsg0(0, _("Error writing block to device.\n"));
1451 Pmsg1(0, _("Wrote one record of %d bytes.\n"), i);
1453 Pmsg0(0, _("Wrote block to device.\n"));
1456 sm_check(__FILE__, __LINE__, false);
1459 sm_check(__FILE__, __LINE__, false);
1463 * Read a record from the tape
1470 if (!get_cmd("Enter length to read: ")) {
1474 if (len < 0 || len > 1000000) {
1475 Pmsg0(0, _("Bad length entered, using default of 1024 bytes.\n"));
1478 buf = (char *)malloc(len);
1479 stat = read(dev->fd, buf, len);
1480 if (stat > 0 && stat <= len) {
1483 Pmsg3(0, _("Read of %d bytes gives stat=%d. ERR=%s\n"),
1484 len, stat, strerror(errno));
1490 * Scan tape by reading block by block. Report what is
1491 * on the tape. Note, this command does raw reads, and as such
1492 * will not work with fixed block size devices.
1494 static void scancmd()
1497 int blocks, tot_blocks, tot_files;
1503 blocks = block_size = tot_blocks = 0;
1505 if (dev->state & ST_EOT) {
1506 Pmsg0(0, "End of tape\n");
1509 update_pos_dev(dev);
1510 tot_files = dev->file;
1511 Pmsg1(0, _("Starting scan at file %u\n"), dev->file);
1513 if ((stat = read(dev->fd, buf, sizeof(buf))) < 0) {
1514 clrerror_dev(dev, -1);
1515 Mmsg2(&dev->errmsg, "read error on %s. ERR=%s.\n",
1516 dev->dev_name, strerror(dev->dev_errno));
1517 Pmsg2(0, "Bad status from read %d. ERR=%s\n", stat, strerror_dev(dev));
1519 printf("%d block%s of %d bytes in file %d\n",
1520 blocks, blocks>1?"s":"", block_size, dev->file);
1523 Dmsg1(200, "read status = %d\n", stat);
1525 if (stat != block_size) {
1526 update_pos_dev(dev);
1528 printf("%d block%s of %d bytes in file %d\n",
1529 blocks, blocks>1?"s":"", block_size, dev->file);
1534 if (stat == 0) { /* EOF */
1535 update_pos_dev(dev);
1536 printf("End of File mark.\n");
1537 /* Two reads of zero means end of tape */
1538 if (dev->state & ST_EOF)
1539 dev->state |= ST_EOT;
1541 dev->state |= ST_EOF;
1544 if (dev->state & ST_EOT) {
1545 printf("End of tape\n");
1548 } else { /* Got data */
1549 dev->state &= ~ST_EOF;
1555 update_pos_dev(dev);
1556 tot_files = dev->file - tot_files;
1557 printf("Total files=%d, blocks=%d, bytes = %s\n", tot_files, tot_blocks,
1558 edit_uint64_with_commas(bytes, ec1));
1563 * Scan tape by reading Bacula block by block. Report what is
1564 * on the tape. This function reads Bacula blocks, so if your
1565 * Device resource is correctly defined, it should work with
1566 * either variable or fixed block sizes.
1568 static void scan_blocks()
1570 int blocks, tot_blocks, tot_files;
1571 uint32_t block_size;
1576 block = new_block(dev);
1577 blocks = block_size = tot_blocks = 0;
1580 update_pos_dev(dev);
1581 tot_files = dev->file;
1583 if (!read_block_from_device(dcr, block, NO_BLOCK_NUMBER_CHECK)) {
1584 Dmsg1(100, "!read_block(): ERR=%s\n", strerror_dev(dev));
1585 if (dev->state & ST_EOT) {
1587 printf("%d block%s of %d bytes in file %d\n",
1588 blocks, blocks>1?"s":"", block_size, dev->file);
1593 if (dev->state & ST_EOF) {
1595 printf("%d block%s of %d bytes in file %d\n",
1596 blocks, blocks>1?"s":"", block_size, dev->file);
1599 printf(_("End of File mark.\n"));
1602 if (dev->state & ST_SHORT) {
1604 printf("%d block%s of %d bytes in file %d\n",
1605 blocks, blocks>1?"s":"", block_size, dev->file);
1608 printf(_("Short block read.\n"));
1611 printf(_("Error reading block. ERR=%s\n"), strerror_dev(dev));
1614 if (block->block_len != block_size) {
1616 printf("%d block%s of %d bytes in file %d\n",
1617 blocks, blocks>1?"s":"", block_size, dev->file);
1620 block_size = block->block_len;
1624 bytes += block->block_len;
1625 Dmsg6(100, "Blk_blk=%u dev_blk=%u blen=%u bVer=%d SessId=%u SessTim=%u\n",
1626 block->BlockNumber, dev->block_num, block->block_len, block->BlockVer,
1627 block->VolSessionId, block->VolSessionTime);
1629 DEV_RECORD *rec = new_record();
1630 read_record_from_block(block, rec);
1631 Pmsg8(-1, "Blk_block: %u dev_blk=%u blen=%u First rec FI=%s SessId=%u SessTim=%u Strm=%s rlen=%d\n",
1632 block->BlockNumber, dev->block_num, block->block_len,
1633 FI_to_ascii(rec->FileIndex), rec->VolSessionId, rec->VolSessionTime,
1634 stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len);
1637 } else if (verbose > 1) {
1638 dump_block(block, "");
1644 tot_files = dev->file - tot_files;
1645 printf("Total files=%d, blocks=%d, bytes = %s\n", tot_files, tot_blocks,
1646 edit_uint64_with_commas(bytes, ec1));
1650 static void statcmd()
1652 int debug = debug_level;
1654 Pmsg2(0, "Device status: %u. ERR=%s\n", status_dev(dev), strerror_dev(dev));
1656 dump_volume_label(dev);
1658 debug_level = debug;
1663 * First we label the tape, then we fill
1664 * it with data get a new tape and write a few blocks.
1666 static void fillcmd()
1673 uint32_t min_block_size;
1683 This command simulates Bacula writing to a tape.\n\
1684 It requires either one or two blank tapes, which it\n\
1685 will label and write.\n\n\
1686 If you have an autochanger configured, it will use\n\
1687 the tapes that are in slots 1 and 2, otherwise, you will\n\
1688 be prompted to insert the tapes when necessary.\n\n\
1689 It will print a status approximately\n\
1690 every 322 MB, and write an EOF every 3.2 GB. If you have\n\
1691 selected the simple test option, after writing the first tape\n\
1692 it will rewind it and re-read the last block written.\n\n\
1693 If you have selected the multiple tape test, when the first tape\n\
1694 fills, it will ask for a second, and after writing a few more \n\
1695 blocks, it will stop. Then it will begin re-reading the\n\
1697 This may take a long time -- hours! ...\n\n");
1699 get_cmd("Do you want to run the simplified test (s) with one tape\n"
1700 "or the complete multiple tape (m) test: (s/m) ");
1701 if (cmd[0] == 's') {
1702 Pmsg0(-1, "Simple test (single tape) selected.\n");
1704 } else if (cmd[0] == 'm') {
1705 Pmsg0(-1, "Multiple tape test selected.\n");
1708 Pmsg0(000, "Command aborted.\n");
1712 Dmsg1(20, "Begin append device=%s\n", dev_name(dev));
1713 Dmsg1(20, "MaxVolSize=%s\n", edit_uint64(dev->max_volume_size, ec1));
1715 /* Use fixed block size to simplify read back */
1716 min_block_size = dev->min_block_size;
1717 dev->min_block_size = dev->max_block_size;
1720 * Acquire output device for writing. Note, after acquiring a
1721 * device, we MUST release it, which is done at the end of this
1724 Dmsg0(100, "just before acquire_device\n");
1725 if (!acquire_device_for_append(jcr)) {
1726 set_jcr_job_status(jcr, JS_ErrorTerminated);
1729 block = jcr->dcr->block;
1731 Dmsg0(100, "Just after acquire_device_for_append\n");
1733 * Write Begin Session Record
1735 if (!write_session_label(dcr, block, SOS_LABEL)) {
1736 set_jcr_job_status(jcr, JS_ErrorTerminated);
1737 Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"),
1741 Pmsg0(-1, "Wrote Start Of Session label.\n");
1743 memset(&rec, 0, sizeof(rec));
1744 rec.data = get_memory(100000); /* max record size */
1746 #define REC_SIZE 32768
1747 rec.data_len = REC_SIZE;
1750 * Put some random data in the record
1752 fd = open("/dev/urandom", O_RDONLY);
1754 read(fd, rec.data, rec.data_len);
1757 uint32_t *p = (uint32_t *)rec.data;
1758 srandom(time(NULL));
1759 for (i=0; i<rec.data_len/sizeof(uint32_t); i++) {
1765 * Generate data as if from File daemon, write to device
1767 jcr->dcr->VolFirstIndex = 0;
1768 time(&jcr->run_time); /* start counting time for rates */
1770 Pmsg0(-1, "Begin writing Bacula records to tape ...\n");
1772 Pmsg0(-1, "Begin writing Bacula records to first tape ...\n");
1774 for (file_index = 0; ok && !job_canceled(jcr); ) {
1775 rec.VolSessionId = jcr->VolSessionId;
1776 rec.VolSessionTime = jcr->VolSessionTime;
1777 rec.FileIndex = ++file_index;
1778 rec.Stream = STREAM_FILE_DATA;
1780 /* Mix up the data just a bit */
1781 uint32_t *lp = (uint32_t *)rec.data;
1783 for (i=1; i < (rec.data_len-sizeof(uint32_t))/sizeof(uint32_t)-1; i++) {
1787 Dmsg4(250, "before write_rec FI=%d SessId=%d Strm=%s len=%d\n",
1788 rec.FileIndex, rec.VolSessionId, stream_to_ascii(rec.Stream, rec.FileIndex),
1791 while (!write_record_to_block(block, &rec)) {
1793 * When we get here we have just filled a block
1795 Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec.data_len,
1798 /* Write block to tape */
1799 if (!flush_block(block, 1)) {
1803 /* Every 5000 blocks (approx 322MB) report where we are.
1805 if ((block->BlockNumber % 5000) == 0) {
1807 now -= jcr->run_time;
1809 now = 1; /* prevent divide error */
1811 kbs = (double)dev->VolCatInfo.VolCatBytes / (1000.0 * (double)now);
1812 Pmsg4(-1, "Wrote blk_block=%u, dev_blk_num=%u VolBytes=%s rate=%.1f KB/s\n",
1813 block->BlockNumber, dev->block_num,
1814 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), (float)kbs);
1816 /* Every 15000 blocks (approx 1GB) write an EOF.
1818 if ((block->BlockNumber % 15000) == 0) {
1819 Pmsg0(-1, "Flush block, write EOF\n");
1820 flush_block(block, 0);
1824 /* Get out after writing 10 blocks to the second tape */
1825 if (++BlockNumber > 10 && stop != 0) { /* get out */
1830 Pmsg0(000, _("Not OK\n"));
1833 jcr->JobBytes += rec.data_len; /* increment bytes this job */
1834 Dmsg4(190, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
1835 FI_to_ascii(rec.FileIndex), rec.VolSessionId,
1836 stream_to_ascii(rec.Stream, rec.FileIndex), rec.data_len);
1838 /* Get out after writing 10 blocks to the second tape */
1839 if (BlockNumber > 10 && stop != 0) { /* get out */
1840 Pmsg0(-1, "Done writing ...\n");
1845 Dmsg0(100, "Write_end_session_label()\n");
1846 /* Create Job status for end of session label */
1847 if (!job_canceled(jcr) && ok) {
1848 set_jcr_job_status(jcr, JS_Terminated);
1850 set_jcr_job_status(jcr, JS_ErrorTerminated);
1852 if (!write_session_label(dcr, block, EOS_LABEL)) {
1853 Pmsg1(000, _("Error writting end session label. ERR=%s\n"), strerror_dev(dev));
1856 /* Write out final block of this session */
1857 if (!write_block_to_device(dcr, block)) {
1858 Pmsg0(-1, _("Set ok=false after write_block_to_device.\n"));
1861 Pmsg0(-1, _("Wrote End Of Session label.\n"));
1863 /* Save last block info for second tape */
1864 last_block_num2 = last_block_num;
1865 last_file2 = last_file;
1867 free_block(last_block2);
1869 last_block2 = dup_block(last_block);
1872 sprintf(buf, "%s/btape.state", working_directory);
1873 fd = open(buf, O_CREAT|O_TRUNC|O_WRONLY, 0640);
1875 write(fd, &btape_state_level, sizeof(btape_state_level));
1876 write(fd, &simple, sizeof(simple));
1877 write(fd, &last_block_num1, sizeof(last_block_num1));
1878 write(fd, &last_block_num2, sizeof(last_block_num2));
1879 write(fd, &last_file1, sizeof(last_file1));
1880 write(fd, &last_file2, sizeof(last_file2));
1881 write(fd, last_block1->buf, last_block1->buf_len);
1882 write(fd, last_block2->buf, last_block2->buf_len);
1883 write(fd, first_block->buf, first_block->buf_len);
1885 Pmsg2(-1, "Wrote state file last_block_num1=%d last_block_num2=%d\n",
1886 last_block_num1, last_block_num2);
1888 Pmsg2(-1, _("Could not create state file: %s ERR=%s\n"), buf,
1892 /* Release the device if multiple tapes being used */
1893 if (!simple && !release_device(jcr)) {
1894 Pmsg0(-1, _("Error in release_device\n"));
1898 Pmsg2(-1, _("\n\nDone filling tape%s. Now beginning re-read of %stape ...\n"),
1899 simple?"":"s", simple?"":"first ");
1903 dev->min_block_size = min_block_size;
1904 free_memory(rec.data);
1908 * Read two tapes written by the "fill" command and ensure
1909 * that the data is valid. If stop==1 we simulate full read back
1910 * of two tapes. If stop==-1 we simply read the last block and
1911 * verify that it is correct.
1913 static void unfillcmd()
1917 last_block1 = new_block(dev);
1918 last_block2 = new_block(dev);
1919 first_block = new_block(dev);
1920 sprintf(buf, "%s/btape.state", working_directory);
1921 fd = open(buf, O_RDONLY);
1923 uint32_t state_level;
1924 read(fd, &state_level, sizeof(btape_state_level));
1925 read(fd, &simple, sizeof(simple));
1926 read(fd, &last_block_num1, sizeof(last_block_num1));
1927 read(fd, &last_block_num2, sizeof(last_block_num2));
1928 read(fd, &last_file1, sizeof(last_file1));
1929 read(fd, &last_file2, sizeof(last_file2));
1930 read(fd, last_block1->buf, last_block1->buf_len);
1931 read(fd, last_block2->buf, last_block2->buf_len);
1932 read(fd, first_block->buf, first_block->buf_len);
1934 if (state_level != btape_state_level) {
1935 Pmsg0(-1, "\nThe state file level has changed. You must redo\n"
1936 "the fill command.\n");
1940 Pmsg2(-1, "\nCould not find the state file: %s ERR=%s\n"
1941 "You must redo the fill command.\n", buf, strerror(errno));
1948 static void do_unfill()
1956 block = new_block(dev);
1958 Dmsg0(20, "Enter do_unfill\n");
1959 dev->capabilities |= CAP_ANONVOLS; /* allow reading any volume */
1960 dev->capabilities &= ~CAP_LABEL; /* don't label anything here */
1964 time(&jcr->run_time); /* start counting time for rates */
1968 free_block(last_block);
1970 last_block_num = last_block_num1;
1971 last_file = last_file1;
1972 last_block = last_block1;
1975 /* Multiple Volume tape */
1976 /* Close device so user can use autochanger if desired */
1977 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
1980 autochanger = autoload_device(jcr, dev, 1, NULL);
1982 force_close_dev(dev);
1983 get_cmd(_("Mount first tape. Press enter when ready: "));
1986 jcr->dcr = new_dcr(jcr, dev);
1987 set_volume_name("TestVolume1", 1);
1989 create_vol_list(jcr);
1991 dev->state &= ~ST_READ;
1992 if (!acquire_device_for_read(jcr)) {
1993 Pmsg1(-1, "%s", dev->errmsg);
1998 * We now have the first tape mounted.
1999 * Note, re-reading last block may have caused us to
2000 * loose track of where we are (block number unknown).
2002 rewind_dev(dev); /* get to a known place on tape */
2003 /* Read the first 1000 records */
2004 Pmsg0(-1, _("Reading the first 1000 records.\n"));
2005 read_records(dcr, quickie_cb, my_mount_next_read_volume);
2006 Pmsg4(-1, _("Reposition from %u:%u to %u:%u\n"), dev->file, dev->block_num,
2007 last_file, last_block_num);
2008 if (!reposition_dev(dev, last_file, last_block_num)) {
2009 Pmsg1(-1, "Reposition error. ERR=%s\n", strerror_dev(dev));
2011 Pmsg1(-1, _("Reading block %u.\n"), last_block_num);
2012 if (!read_block_from_device(dcr, block, NO_BLOCK_NUMBER_CHECK)) {
2013 Pmsg1(-1, _("Error reading block: ERR=%s\n"), strerror_dev(dev));
2016 if (compare_blocks(last_block, block)) {
2018 Pmsg0(-1, _("\nThe last block on the tape matches. Test succeeded.\n\n"));
2020 Pmsg0(-1, _("\nThe last block of the first tape matches.\n\n"));
2027 /* restore info for last block on second Volume */
2028 last_block_num = last_block_num2;
2029 last_file = last_file2;
2030 last_block = last_block2;
2032 /* Multiple Volume tape */
2033 /* Close device so user can use autochanger if desired */
2034 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2039 set_volume_name("TestVolume2", 2);
2041 create_vol_list(jcr);
2042 autochanger = autoload_device(jcr, dev, 1, NULL);
2044 force_close_dev(dev);
2045 get_cmd(_("Mount second tape. Press enter when ready: "));
2048 dev->state &= ~ST_READ;
2049 if (!acquire_device_for_read(jcr)) {
2050 Pmsg1(-1, "%s", dev->errmsg);
2054 /* Space to "first" block which is last block not written
2055 * on the previous tape.
2057 Pmsg2(-1, _("Reposition from %u:%u to 0:1\n"), dev->file, dev->block_num);
2058 if (!reposition_dev(dev, 0, 1)) {
2059 Pmsg1(-1, "Reposition error. ERR=%s\n", strerror_dev(dev));
2062 Pmsg1(-1, _("Reading block %d.\n"), dev->block_num);
2063 if (!read_block_from_device(dcr, block, NO_BLOCK_NUMBER_CHECK)) {
2064 Pmsg1(-1, _("Error reading block: ERR=%s\n"), strerror_dev(dev));
2067 if (compare_blocks(first_block, block)) {
2068 Pmsg0(-1, _("\nThe first block on the second tape matches.\n\n"));
2071 /* Now find and compare the last block */
2072 Pmsg4(-1, _("Reposition from %u:%u to %u:%u\n"), dev->file, dev->block_num,
2073 last_file, last_block_num);
2074 if (!reposition_dev(dev, last_file, last_block_num)) {
2075 Pmsg1(-1, "Reposition error. ERR=%s\n", strerror_dev(dev));
2078 Pmsg1(-1, _("Reading block %d.\n"), dev->block_num);
2079 if (!read_block_from_device(dcr, block, NO_BLOCK_NUMBER_CHECK)) {
2080 Pmsg1(-1, _("Error reading block: ERR=%s\n"), strerror_dev(dev));
2083 if (compare_blocks(last_block, block)) {
2084 Pmsg0(-1, _("\nThe last block on the second tape matches. Test succeeded.\n\n"));
2089 free_block(last_block1);
2090 free_block(last_block2);
2091 free_block(first_block);
2094 /* Read 1000 records then stop */
2095 static bool quickie_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
2098 return quickie_count <= 1000;
2101 static bool compare_blocks(DEV_BLOCK *last_block, DEV_BLOCK *block)
2104 uint32_t CheckSum, block_len;
2107 p = last_block->buf;
2109 unser_begin(q, BLKHDR2_LENGTH);
2110 unser_uint32(CheckSum);
2111 unser_uint32(block_len);
2112 while (q < (block->buf+block_len)) {
2119 dump_block(last_block, _("Last block written"));
2121 dump_block(block, _("Block read back"));
2122 Pmsg1(-1, "\n\nThe blocks differ at byte %u\n", p - last_block->buf);
2123 Pmsg0(-1, "\n\n!!!! The last block written and the block\n"
2124 "that was read back differ. The test FAILED !!!!\n"
2125 "This must be corrected before you use Bacula\n"
2126 "to write multi-tape Volumes.!!!!\n");
2130 dump_block(last_block, _("Last block written"));
2131 dump_block(block, _("Block read back"));
2141 * Write current block to tape regardless of whether or
2142 * not it is full. If the tape fills, attempt to
2143 * acquire another tape.
2145 static int flush_block(DEV_BLOCK *block, int dump)
2150 uint32_t this_file, this_block_num;
2153 this_block = new_block(dev);
2156 last_block = new_block(dev);
2159 this_file = dev->file;
2160 this_block_num = dev->block_num;
2161 if (!write_block_to_dev(dcr, block)) {
2162 Pmsg3(000, "Last block at: %u:%u this_dev_block_num=%d\n",
2163 last_file, last_block_num, this_block_num);
2166 * This is 1st tape, so save first tape info separate
2167 * from second tape info
2169 last_block_num1 = last_block_num;
2170 last_file1 = last_file;
2171 last_block1 = dup_block(last_block);
2172 last_block2 = dup_block(last_block);
2173 first_block = dup_block(block); /* first block second tape */
2176 Pmsg3(000, "Block not written: FileIndex=%u blk_block=%u Size=%u\n",
2177 (unsigned)file_index, block->BlockNumber, block->block_len);
2178 dump_block(last_block, "Last block written");
2180 dump_block(block, "Block not written");
2183 eot_block = block->BlockNumber;
2184 eot_block_len = block->block_len;
2185 eot_FileIndex = file_index;
2189 now -= jcr->run_time;
2191 now = 1; /* don't divide by zero */
2193 kbs = (double)dev->VolCatInfo.VolCatBytes / (1000 * now);
2194 vol_size = dev->VolCatInfo.VolCatBytes;
2195 Pmsg2(000, "End of tape. VolumeCapacity=%s. Write rate = %.1f KB/s\n",
2196 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), kbs);
2199 stop = -1; /* stop, but do simplified test */
2201 /* Full test in progress */
2202 if (!fixup_device_block_write_error(jcr->dcr, block)) {
2203 Pmsg1(000, _("Cannot fixup device error. %s\n"), strerror_dev(dev));
2208 BlockNumber = 0; /* start counting for second tape */
2211 return 1; /* end of tape reached */
2214 /* Save contents after write so that the header is serialized */
2215 memcpy(this_block->buf, block->buf, this_block->buf_len);
2218 * Toggle between two allocated blocks for efficiency.
2219 * Switch blocks so that the block just successfully written is
2220 * always in last_block.
2222 tblock = last_block;
2223 last_block = this_block;
2224 this_block = tblock;
2225 last_file = this_file;
2226 last_block_num = this_block_num;
2234 * First we label the tape, then we fill
2235 * it with data get a new tape and write a few blocks.
2237 static void qfillcmd()
2243 Pmsg0(0, "Test writing blocks of 64512 bytes to tape.\n");
2245 get_cmd("How many blocks do you want to write? (1000): ");
2252 sm_check(__FILE__, __LINE__, false);
2253 block = new_block(dev);
2256 i = block->buf_len - 100;
2258 rec->data = check_pool_memory_size(rec->data, i);
2259 memset(rec->data, i & 0xFF, i);
2262 Pmsg1(0, "Begin writing %d Bacula blocks to tape ...\n", count);
2263 for (i=0; i < count; i++) {
2268 if (!write_record_to_block(block, rec)) {
2269 Pmsg0(0, _("Error writing record to block.\n"));
2272 if (!write_block_to_dev(dcr, block)) {
2273 Pmsg0(0, _("Error writing block to device.\n"));
2279 if (dev_cap(dev, CAP_TWOEOF)) {
2286 sm_check(__FILE__, __LINE__, false);
2289 sm_check(__FILE__, __LINE__, false);
2294 * Fill a tape using raw write() command
2296 static void rawfill_cmd()
2301 uint32_t block_num = 0;
2306 block = new_block(dev);
2307 fd = open("/dev/urandom", O_RDONLY);
2309 read(fd, block->buf, block->buf_len);
2312 uint32_t *p = (uint32_t *)block->buf;
2313 srandom(time(NULL));
2314 for (i=0; i<block->buf_len/sizeof(uint32_t); i++) {
2318 p = (uint32_t *)block->buf;
2319 Pmsg1(0, "Begin writing raw blocks of %u bytes.\n", block->buf_len);
2322 stat = write(dev->fd, block->buf, block->buf_len);
2323 if (stat == (int)block->buf_len) {
2324 if ((block_num++ % 100) == 0) {
2329 for (i=1; i<(block->buf_len-sizeof(uint32_t))/sizeof(uint32_t)-1; i++) {
2338 printf("Write failed at block %u. stat=%d ERR=%s\n", block_num, stat,
2339 strerror(my_errno));
2346 * Fill a tape using raw write() command
2348 static void bfill_cmd()
2351 uint32_t block_num = 0;
2357 block = new_block(dev);
2358 fd = open("/dev/urandom", O_RDONLY);
2360 read(fd, block->buf, block->buf_len);
2363 uint32_t *p = (uint32_t *)block->buf;
2364 srandom(time(NULL));
2365 for (i=0; i<block->buf_len/sizeof(uint32_t); i++) {
2369 p = (uint32_t *)block->buf;
2370 Pmsg1(0, "Begin writing Bacula blocks of %u bytes.\n", block->buf_len);
2373 block->binbuf = block->buf_len;
2374 block->bufp = block->buf + block->binbuf;
2375 if (!write_block_to_dev(dcr, block)) {
2378 if ((block_num++ % 100) == 0) {
2383 for (i=1; i<(block->buf_len/sizeof(uint32_t)-1); i++) {
2389 printf("Write failed at block %u.\n", block_num);
2395 struct cmdstruct { const char *key; void (*func)(); const char *help; };
2396 static struct cmdstruct commands[] = {
2397 {"autochanger", autochangercmd, "test autochanger"},
2398 {"bsf", bsfcmd, "backspace file"},
2399 {"bsr", bsrcmd, "backspace record"},
2400 {"bfill", bfill_cmd, "fill tape using Bacula writes"},
2401 {"cap", capcmd, "list device capabilities"},
2402 {"clear", clearcmd, "clear tape errors"},
2403 {"eod", eodcmd, "go to end of Bacula data for append"},
2404 {"eom", eomcmd, "go to the physical end of medium"},
2405 {"fill", fillcmd, "fill tape, write onto second volume"},
2406 {"unfill", unfillcmd, "read filled tape"},
2407 {"fsf", fsfcmd, "forward space a file"},
2408 {"fsr", fsrcmd, "forward space a record"},
2409 {"help", helpcmd, "print this command"},
2410 {"label", labelcmd, "write a Bacula label to the tape"},
2411 {"load", loadcmd, "load a tape"},
2412 {"quit", quitcmd, "quit btape"},
2413 {"rawfill", rawfill_cmd, "use write() to fill tape"},
2414 {"readlabel", readlabelcmd, "read and print the Bacula tape label"},
2415 {"rectest", rectestcmd, "test record handling functions"},
2416 {"rewind", rewindcmd, "rewind the tape"},
2417 {"scan", scancmd, "read() tape block by block to EOT and report"},
2418 {"scanblocks", scan_blocks, "Bacula read block by block to EOT and report"},
2419 {"status", statcmd, "print tape status"},
2420 {"test", testcmd, "General test Bacula tape functions"},
2421 {"weof", weofcmd, "write an EOF on the tape"},
2422 {"wr", wrcmd, "write a single Bacula block"},
2423 {"rr", rrcmd, "read a single record"},
2424 {"qfill", qfillcmd, "quick fill command"},
2426 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
2434 while (get_cmd("*")) {
2435 sm_check(__FILE__, __LINE__, false);
2437 parse_args(cmd, &args, &argc, argk, argv, MAX_CMD_ARGS);
2438 for (i=0; i<comsize; i++) /* search for command */
2439 if (argc > 0 && fstrsch(argk[0], commands[i].key)) {
2440 (*commands[i].func)(); /* go execute command */
2445 Pmsg1(0, _("%s is an illegal command\n"), cmd);
2451 static void helpcmd()
2455 printf(_("Interactive commands:\n"));
2456 printf(_(" Command Description\n ======= ===========\n"));
2457 for (i=0; i<comsize; i++)
2458 printf(" %-10s %s\n", commands[i].key, commands[i].help);
2465 "\nVersion: " VERSION " (" BDATE ")\n\n"
2466 "Usage: btape <options> <device_name>\n"
2467 " -b <file> specify bootstrap file\n"
2468 " -c <file> set configuration file to file\n"
2469 " -d <nn> set debug level to nn\n"
2470 " -p proceed inspite of I/O errors\n"
2471 " -s turn off signals\n"
2473 " -? print this message.\n"
2479 * Get next input command from terminal. This
2480 * routine is REALLY primitive, and should be enhanced
2481 * to have correct backspacing, etc.
2484 get_cmd(const char *prompt)
2488 fprintf(stdout, prompt);
2490 /* We really should turn off echoing and pretty this
2494 while ((ch = fgetc(stdin)) != EOF) {
2496 strip_trailing_junk(cmd);
2498 } else if (ch == 4 || ch == 0xd3 || ch == 0x8) {
2511 /* Dummies to replace askdir.c */
2512 bool dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
2513 bool dir_send_job_status(JCR *jcr) {return 1;}
2515 bool dir_update_volume_info(JCR *jcr, bool relabel)
2521 bool dir_get_volume_info(JCR *jcr, enum get_vol_info_rw writing)
2523 Dmsg0(20, "Enter dir_get_volume_info\n");
2524 bstrncpy(jcr->VolCatInfo.VolCatName, jcr->VolumeName, sizeof(jcr->VolCatInfo.VolCatName));
2528 bool dir_create_jobmedia_record(JCR *jcr)
2530 jcr->dcr->WroteVol = false;
2535 bool dir_find_next_appendable_volume(JCR *jcr)
2537 Dmsg1(20, "Enter dir_find_next_appendable_volume. stop=%d\n", stop);
2538 return jcr->VolumeName[0] != 0;
2541 bool dir_ask_sysop_to_mount_volume(JCR *jcr)
2543 DEVICE *dev = jcr->dcr->dev;
2544 Dmsg0(20, "Enter dir_ask_sysop_to_mount_volume\n");
2545 if (jcr->VolumeName[0] == 0) {
2546 return dir_ask_sysop_to_create_appendable_volume(jcr);
2548 /* Close device so user can use autochanger if desired */
2549 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2552 force_close_dev(dev);
2553 Pmsg1(-1, "%s", dev->errmsg); /* print reason */
2554 if (jcr->VolumeName[0] == 0 || strcmp(jcr->VolumeName, "TestVolume2") == 0) {
2555 fprintf(stderr, "Mount second Volume on device %s and press return when ready: ",
2558 fprintf(stderr, "Mount Volume \"%s\" on device %s and press return when ready: ",
2559 jcr->VolumeName, dev_name(dev));
2565 bool dir_ask_sysop_to_create_appendable_volume(JCR *jcr)
2568 DEVICE *dev = jcr->dcr->dev;
2569 Dmsg0(20, "Enter dir_ask_sysop_to_create_appendable_volume\n");
2571 set_volume_name("TestVolume1", 1);
2573 set_volume_name("TestVolume2", 2);
2575 /* Close device so user can use autochanger if desired */
2576 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2579 autochanger = autoload_device(jcr, dev, 1, NULL);
2581 force_close_dev(dev);
2582 fprintf(stderr, "Mount blank Volume on device %s and press return when ready: ",
2586 open_device(jcr, dev);
2593 static bool my_mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
2597 Dmsg0(20, "Enter my_mount_next_read_volume\n");
2598 Pmsg1(000, "End of Volume \"%s\"\n", jcr->VolumeName);
2600 if (LastBlock != block->BlockNumber) {
2601 VolBytes += block->block_len;
2603 LastBlock = block->BlockNumber;
2605 now -= jcr->run_time;
2609 kbs = (double)VolBytes / (1000.0 * (double)now);
2610 Pmsg3(-1, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
2611 edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
2613 if (strcmp(jcr->VolumeName, "TestVolume2") == 0) {
2619 set_volume_name("TestVolume2", 2);
2621 create_vol_list(jcr);
2623 dev->state &= ~ST_READ;
2624 if (!acquire_device_for_read(jcr)) {
2625 Pmsg2(0, "Cannot open Dev=%s, Vol=%s\n", dev_name(dev), jcr->VolumeName);
2628 return true; /* next volume mounted */
2631 static void set_volume_name(const char *VolName, int volnum)
2633 DCR *dcr = jcr->dcr;
2634 VolumeName = VolName;
2636 pm_strcpy(&jcr->VolumeName, VolName);
2637 bstrncpy(dev->VolCatInfo.VolCatName, VolName, sizeof(dev->VolCatInfo.VolCatName));
2638 bstrncpy(jcr->VolCatInfo.VolCatName, VolName, sizeof(jcr->VolCatInfo.VolCatName));
2639 bstrncpy(dcr->VolCatInfo.VolCatName, VolName, sizeof(dcr->VolCatInfo.VolCatName));
2640 bstrncpy(dcr->VolumeName, VolName, sizeof(dcr->VolumeName));
2641 jcr->VolCatInfo.Slot = volnum;
2642 dcr->VolCatInfo.Slot = volnum;