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[])
153 if (TAPE_BSIZE % DEV_BSIZE != 0 || TAPE_BSIZE / DEV_BSIZE == 0) {
154 Emsg2(M_ABORT, 0, "Tape block size (%d) not multiple of system size (%d)\n",
155 TAPE_BSIZE, DEV_BSIZE);
157 if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
158 Emsg1(M_ABORT, 0, "Tape block size (%d) is not a power of 2\n", TAPE_BSIZE);
160 if (sizeof(off_t) < 8) {
161 Pmsg1(-1, "\n\n!!!! Warning large disk addressing disabled. off_t=%d should be 8 or more !!!!!\n\n\n",
165 bsnprintf(buf, sizeof(buf), "%u", x32);
166 i = bsscanf(buf, "%u", &y32);
167 if (i != 1 || x32 != y32) {
168 Pmsg3(-1, "32 bit printf/scanf problem. i=%d x32=%u y32=%u\n", i, x32, y32);
171 x64 = 123456789012345678;
172 bsnprintf(buf, sizeof(buf), "%" llu, x64);
173 i = bsscanf(buf, "%llu", &y64);
174 if (i != 1 || x64 != y64) {
175 Pmsg3(-1, "64 bit printf/scanf problem. i=%d x64=%" llu " y64=%" llu "\n", i, x64, y64);
179 printf("Tape block granularity is %d bytes.\n", TAPE_BSIZE);
181 working_directory = "/tmp";
182 my_name_is(margc, margv, "btape");
183 init_msg(NULL, NULL);
185 while ((ch = getopt(margc, margv, "b:c:d:psv?")) != -1) {
187 case 'b': /* bootstrap file */
188 bsr = parse_bsr(NULL, optarg);
189 // dump_bsr(bsr, true);
192 case 'c': /* specify config file */
193 if (configfile != NULL) {
196 configfile = bstrdup(optarg);
199 case 'd': /* set debug level */
200 debug_level = atoi(optarg);
201 if (debug_level <= 0) {
228 cmd = get_pool_memory(PM_FNAME);
229 args = get_pool_memory(PM_FNAME);
232 init_signals(terminate_btape);
235 if (configfile == NULL) {
236 configfile = bstrdup(CONFIG_FILE);
239 daemon_start_time = time(NULL);
241 parse_config(configfile);
244 /* See if we can open a device */
246 Pmsg0(000, "No archive name specified.\n");
249 } else if (margc != 1) {
250 Pmsg0(000, "Improper number of arguments specified.\n");
255 jcr = setup_jcr("btape", margv[0], bsr, NULL);
256 dev = setup_to_access_device(jcr, 0); /* acquire for write */
261 if (!open_the_device()) {
265 Dmsg0(200, "Do tape commands\n");
273 static void terminate_btape(int stat)
276 sm_check(__FILE__, __LINE__, false);
280 free_config_resources();
282 free_pool_memory(args);
286 free_pool_memory(cmd);
301 if (debug_level > 10)
302 print_memory_pool_stats();
305 free_block(this_block);
309 close_memory_pool(); /* free memory in pool */
315 static bool open_the_device()
319 block = new_block(dev);
321 if (!(dev->state & ST_OPENED)) {
322 Dmsg1(200, "Opening device %s\n", jcr->VolumeName);
323 if (open_dev(dev, jcr->VolumeName, OPEN_READ_WRITE) < 0) {
324 Emsg1(M_FATAL, 0, _("dev open failed: %s\n"), dev->errmsg);
330 Pmsg1(000, "open_dev %s OK\n", dev_name(dev));
343 * Write a label to the tape
345 static void labelcmd()
348 pm_strcpy(&cmd, VolumeName);
350 if (!get_cmd("Enter Volume Name: ")) {
355 if (!(dev->state & ST_OPENED)) {
356 if (!first_open_device(dev)) {
357 Pmsg1(0, "Device open failed. ERR=%s\n", strerror_dev(dev));
361 write_new_volume_label_to_dev(dcr, cmd, "Default");
362 Pmsg1(-1, "Wrote Volume label for volume \"%s\".\n", cmd);
366 * Read the tape label
368 static void readlabelcmd()
370 int save_debug_level = debug_level;
374 block = new_block(dev);
375 stat = read_dev_volume_label(dcr, block);
378 Pmsg0(0, "Volume has no label.\n");
381 Pmsg0(0, "Volume label read correctly.\n");
384 Pmsg1(0, "I/O error on device: ERR=%s", strerror_dev(dev));
387 Pmsg0(0, "Volume name error\n");
389 case VOL_CREATE_ERROR:
390 Pmsg1(0, "Error creating label. ERR=%s", strerror_dev(dev));
392 case VOL_VERSION_ERROR:
393 Pmsg0(0, "Volume version error.\n");
395 case VOL_LABEL_ERROR:
396 Pmsg0(0, "Bad Volume label type.\n");
399 Pmsg0(0, "Unknown error.\n");
404 dump_volume_label(dev);
405 debug_level = save_debug_level;
411 * Load the tape should have prevously been taken
412 * off line, otherwise this command is not necessary.
414 static void loadcmd()
417 if (!load_dev(dev)) {
418 Pmsg1(0, "Bad status from load. ERR=%s\n", strerror_dev(dev));
420 Pmsg1(0, "Loaded %s\n", dev_name(dev));
426 static void rewindcmd()
428 if (!rewind_dev(dev)) {
429 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
430 clrerror_dev(dev, -1);
432 Pmsg1(0, "Rewound %s\n", dev_name(dev));
437 * Clear any tape error
439 static void clearcmd()
441 clrerror_dev(dev, -1);
445 * Write and end of file on the tape
447 static void weofcmd()
458 if ((stat = weof_dev(dev, num)) < 0) {
459 Pmsg2(0, "Bad status from weof %d. ERR=%s\n", stat, strerror_dev(dev));
462 Pmsg3(0, "Wrote %d EOF%s to %s\n", num, num==1?"":"s", dev_name(dev));
467 /* Go to the end of the medium -- raw command
468 * The idea was orginally that the end of the Bacula
469 * medium would be flagged differently. This is not
470 * currently the case. So, this is identical to the
476 Pmsg1(0, "%s", strerror_dev(dev));
479 Pmsg0(0, _("Moved to end of medium.\n"));
484 * Go to the end of the medium (either hardware determined
485 * or defined by two eofs.
505 if (!bsf_dev(dev, num)) {
506 Pmsg1(0, _("Bad status from bsf. ERR=%s\n"), strerror_dev(dev));
508 Pmsg2(0, _("Backspaced %d file%s.\n"), num, num==1?"":"s");
524 if (!bsr_dev(dev, num)) {
525 Pmsg1(0, _("Bad status from bsr. ERR=%s\n"), strerror_dev(dev));
527 Pmsg2(0, _("Backspaced %d record%s.\n"), num, num==1?"":"s");
532 * List device capabilities as defined in the
537 printf(_("Configured device capabilities:\n"));
538 printf("%sEOF ", dev->capabilities & CAP_EOF ? "" : "!");
539 printf("%sBSR ", dev->capabilities & CAP_BSR ? "" : "!");
540 printf("%sBSF ", dev->capabilities & CAP_BSF ? "" : "!");
541 printf("%sFSR ", dev->capabilities & CAP_FSR ? "" : "!");
542 printf("%sFSF ", dev->capabilities & CAP_FSF ? "" : "!");
543 printf("%sFASTFSF ", dev->capabilities & CAP_FASTFSF ? "" : "!");
544 printf("%sEOM ", dev->capabilities & CAP_EOM ? "" : "!");
545 printf("%sREM ", dev->capabilities & CAP_REM ? "" : "!");
546 printf("%sRACCESS ", dev->capabilities & CAP_RACCESS ? "" : "!");
547 printf("%sAUTOMOUNT ", dev->capabilities & CAP_AUTOMOUNT ? "" : "!");
548 printf("%sLABEL ", dev->capabilities & CAP_LABEL ? "" : "!");
549 printf("%sANONVOLS ", dev->capabilities & CAP_ANONVOLS ? "" : "!");
550 printf("%sALWAYSOPEN ", dev->capabilities & CAP_ALWAYSOPEN ? "" : "!");
553 printf(_("Device status:\n"));
554 printf("%sOPENED ", dev->state & ST_OPENED ? "" : "!");
555 printf("%sTAPE ", dev->state & ST_TAPE ? "" : "!");
556 printf("%sLABEL ", dev->state & ST_LABEL ? "" : "!");
557 printf("%sMALLOC ", dev->state & ST_MALLOC ? "" : "!");
558 printf("%sAPPEND ", dev->state & ST_APPEND ? "" : "!");
559 printf("%sREAD ", dev->state & ST_READ ? "" : "!");
560 printf("%sEOT ", dev->state & ST_EOT ? "" : "!");
561 printf("%sWEOT ", dev->state & ST_WEOT ? "" : "!");
562 printf("%sEOF ", dev->state & ST_EOF ? "" : "!");
563 printf("%sNEXTVOL ", dev->state & ST_NEXTVOL ? "" : "!");
564 printf("%sSHORT ", dev->state & ST_SHORT ? "" : "!");
567 printf(_("Device parameters:\n"));
568 printf("Device name: %s\n", dev->dev_name);
569 printf("File=%u block=%u\n", dev->file, dev->block_num);
570 printf("Min block=%u Max block=%u\n", dev->min_block_size, dev->max_block_size);
578 * Test writting larger and larger records.
579 * This is a torture test for records.
581 static void rectestcmd()
587 Pmsg0(0, "Test writting larger and larger records.\n"
588 "This is a torture test for records.\nI am going to write\n"
589 "larger and larger records. It will stop when the record size\n"
590 "plus the header exceeds the block size (by default about 64K)\n");
593 get_cmd("Do you want to continue? (y/n): ");
595 Pmsg0(000, "Command aborted.\n");
599 sm_check(__FILE__, __LINE__, false);
600 block = new_block(dev);
603 for (i=1; i<500000; i++) {
604 rec->data = check_pool_memory_size(rec->data, i);
605 memset(rec->data, i & 0xFF, i);
607 sm_check(__FILE__, __LINE__, false);
608 if (write_record_to_block(block, rec)) {
611 Pmsg2(0, "Block %d i=%d\n", blkno, i);
615 sm_check(__FILE__, __LINE__, false);
619 sm_check(__FILE__, __LINE__, false);
623 * This test attempts to re-read a block written by Bacula
624 * normally at the end of the tape. Bacula will then back up
625 * over the two eof marks, backup over the record and reread
626 * it to make sure it is valid. Bacula can skip this validation
627 * if you set "Backward space record = no"
629 static int re_read_block_test()
636 if (!(dev->capabilities & CAP_BSR)) {
637 Pmsg0(-1, _("Skipping read backwards test because BSR turned off.\n"));
641 Pmsg0(-1, _("\n=== Write, backup, and re-read test ===\n\n"
642 "I'm going to write three records and an EOF\n"
643 "then backup over the EOF and re-read the last record.\n"
644 "Bacula does this after writing the last block on the\n"
645 "tape to verify that the block was written correctly.\n\n"
646 "This is not an *essential* feature ...\n\n"));
648 block = new_block(dev);
650 rec->data = check_pool_memory_size(rec->data, block->buf_len);
651 len = rec->data_len = block->buf_len-100;
652 memset(rec->data, 1, rec->data_len);
653 if (!write_record_to_block(block, rec)) {
654 Pmsg0(0, _("Error writing record to block.\n"));
657 if (!write_block_to_dev(dcr, block)) {
658 Pmsg0(0, _("Error writing block to device.\n"));
661 Pmsg1(0, _("Wrote first record of %d bytes.\n"), rec->data_len);
663 memset(rec->data, 2, rec->data_len);
664 if (!write_record_to_block(block, rec)) {
665 Pmsg0(0, _("Error writing record to block.\n"));
668 if (!write_block_to_dev(dcr, block)) {
669 Pmsg0(0, _("Error writing block to device.\n"));
672 Pmsg1(0, _("Wrote second record of %d bytes.\n"), rec->data_len);
674 memset(rec->data, 3, rec->data_len);
675 if (!write_record_to_block(block, rec)) {
676 Pmsg0(0, _("Error writing record to block.\n"));
679 if (!write_block_to_dev(dcr, block)) {
680 Pmsg0(0, _("Error writing block to device.\n"));
683 Pmsg1(0, _("Wrote third record of %d bytes.\n"), rec->data_len);
686 if (dev_cap(dev, CAP_TWOEOF)) {
689 if (!bsf_dev(dev, 1)) {
690 Pmsg1(0, _("Backspace file failed! ERR=%s\n"), strerror_dev(dev));
693 if (dev_cap(dev, CAP_TWOEOF)) {
694 if (!bsf_dev(dev, 1)) {
695 Pmsg1(0, _("Backspace file failed! ERR=%s\n"), strerror_dev(dev));
699 Pmsg0(0, "Backspaced over EOF OK.\n");
700 if (!bsr_dev(dev, 1)) {
701 Pmsg1(0, _("Backspace record failed! ERR=%s\n"), strerror_dev(dev));
704 Pmsg0(0, "Backspace record OK.\n");
705 if (!read_block_from_dev(dcr, block, NO_BLOCK_NUMBER_CHECK)) {
706 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
709 memset(rec->data, 0, rec->data_len);
710 if (!read_record_from_block(block, rec)) {
711 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
714 for (int i=0; i<len; i++) {
715 if (rec->data[i] != 3) {
716 Pmsg0(0, _("Bad data in record. Test failed!\n"));
720 Pmsg0(0, _("\nBlock re-read correct. Test succeeded!\n"));
721 Pmsg0(-1, _("=== End Write, backup, and re-read test ===\n\n"));
729 Pmsg0(0, _("This is not terribly serious since Bacula only uses\n"
730 "this function to verify the last block written to the\n"
731 "tape. Bacula will skip the last block verification\n"
733 "Backward Space Record = No\n\n"
734 "to your Storage daemon's Device resource definition.\n"));
741 * This test writes Bacula blocks to the tape in
742 * several files. It then rewinds the tape and attepts
743 * to read these blocks back checking the data.
745 static int write_read_test()
753 Pmsg0(-1, _("\n=== Write, rewind, and re-read test ===\n\n"
754 "I'm going to write 1000 records and an EOF\n"
755 "then write 1000 records and an EOF, then rewind,\n"
756 "and re-read the data to verify that it is correct.\n\n"
757 "This is an *essential* feature ...\n\n"));
758 block = new_block(dev);
760 if (!rewind_dev(dev)) {
761 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
764 rec->data = check_pool_memory_size(rec->data, block->buf_len);
765 rec->data_len = block->buf_len-100;
766 len = rec->data_len/sizeof(i);
767 for (i=1; i<=1000; i++) {
768 p = (int *)rec->data;
769 for (j=0; j<len; j++) {
772 if (!write_record_to_block(block, rec)) {
773 Pmsg0(0, _("Error writing record to block.\n"));
776 if (!write_block_to_dev(dcr, block)) {
777 Pmsg0(0, _("Error writing block to device.\n"));
781 Pmsg1(0, _("Wrote 1000 blocks of %d bytes.\n"), rec->data_len);
783 for (i=1001; i<=2000; i++) {
784 p = (int *)rec->data;
785 for (j=0; j<len; j++) {
788 if (!write_record_to_block(block, rec)) {
789 Pmsg0(0, _("Error writing record to block.\n"));
792 if (!write_block_to_dev(dcr, block)) {
793 Pmsg0(0, _("Error writing block to device.\n"));
797 Pmsg1(0, _("Wrote 1000 blocks of %d bytes.\n"), rec->data_len);
799 if (dev_cap(dev, CAP_TWOEOF)) {
802 if (!rewind_dev(dev)) {
803 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
806 Pmsg0(0, "Rewind OK.\n");
808 for (i=1; i<=2000; i++) {
810 if (!read_block_from_dev(dcr, block, NO_BLOCK_NUMBER_CHECK)) {
811 if (dev_state(dev, ST_EOF)) {
812 Pmsg0(-1, _("Got EOF on tape.\n"));
815 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
818 memset(rec->data, 0, rec->data_len);
819 if (!read_record_from_block(block, rec)) {
820 Pmsg1(0, _("Read record failed! ERR=%s\n"), strerror(dev->dev_errno));
823 p = (int *)rec->data;
824 for (j=0; j<len; j++) {
826 Pmsg3(0, _("Bad data in record. Expected %d, got %d at byte %d. Test failed!\n"),
832 if (i == 1000 || i == 2000) {
833 Pmsg0(-1, _("1000 blocks re-read correctly.\n"));
836 Pmsg0(-1, _("=== Test Succeeded. End Write, rewind, and re-read test ===\n\n"));
846 * This test writes Bacula blocks to the tape in
847 * several files. It then rewinds the tape and attepts
848 * to read these blocks back checking the data.
850 static int position_test()
858 int file = 0, blk = 0;
861 Pmsg0(-1, _("\n=== Write, rewind, and position test ===\n\n"
862 "I'm going to write 1000 records and an EOF\n"
863 "then write 1000 records and an EOF, then rewind,\n"
864 "and position to a few blocks and verify that it is correct.\n\n"
865 "This is an *essential* feature ...\n\n"));
866 block = new_block(dev);
868 if (!rewind_dev(dev)) {
869 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
872 rec->data = check_pool_memory_size(rec->data, block->buf_len);
873 rec->data_len = block->buf_len-100;
874 len = rec->data_len/sizeof(i);
875 for (i=1; i<=1000; i++) {
876 p = (int *)rec->data;
877 for (j=0; j<len; j++) {
880 if (!write_record_to_block(block, rec)) {
881 Pmsg0(0, _("Error writing record to block.\n"));
884 if (!write_block_to_dev(dcr, block)) {
885 Pmsg0(0, _("Error writing block to device.\n"));
889 Pmsg1(0, _("Wrote 1000 blocks of %d bytes.\n"), rec->data_len);
891 for (i=1001; i<=2000; i++) {
892 p = (int *)rec->data;
893 for (j=0; j<len; j++) {
896 if (!write_record_to_block(block, rec)) {
897 Pmsg0(0, _("Error writing record to block.\n"));
900 if (!write_block_to_dev(dcr, block)) {
901 Pmsg0(0, _("Error writing block to device.\n"));
905 Pmsg1(0, _("Wrote 1000 blocks of %d bytes.\n"), rec->data_len);
907 if (dev_cap(dev, CAP_TWOEOF)) {
910 if (!rewind_dev(dev)) {
911 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
914 Pmsg0(0, "Rewind OK.\n");
918 /* Set up next item to read based on where we are */
954 Pmsg2(-1, "Reposition to file:block %d:%d\n", file, blk);
955 if (!reposition_dev(dev, file, blk)) {
956 Pmsg0(0, "Reposition error.\n");
960 if (!read_block_from_dev(dcr, block, NO_BLOCK_NUMBER_CHECK)) {
961 if (dev_state(dev, ST_EOF)) {
962 Pmsg0(-1, _("Got EOF on tape.\n"));
965 Pmsg1(0, _("Read block failed! ERR=%s\n\n"), strerror(dev->dev_errno));
966 Pmsg0(0, _("This may be because the tape drive block size is not\n"
967 " set to variable blocking as normally used by Bacula.\n"
968 " Please see the Tape Testing chapter in the manual and \n"
969 " look for using mt with defblksize and setoptions\n"));
972 memset(rec->data, 0, rec->data_len);
973 if (!read_record_from_block(block, rec)) {
974 Pmsg1(0, _("Read record failed! ERR=%s\n"), strerror(dev->dev_errno));
977 p = (int *)rec->data;
978 for (j=0; j<len; j++) {
980 Pmsg3(0, _("Bad data in record. Expected %d, got %d at byte %d. Test failed!\n"),
985 Pmsg0(-1, _("Block re-read correctly.\n"));
987 Pmsg0(-1, _("=== Test Succeeded. End Write, rewind, and re-read test ===\n\n"));
1000 * This test writes some records, then writes an end of file,
1001 * rewinds the tape, moves to the end of the data and attepts
1002 * to append to the tape. This function is essential for
1003 * Bacula to be able to write multiple jobs to the tape.
1005 static int append_test()
1007 Pmsg0(-1, _("\n\n=== Append files test ===\n\n"
1008 "This test is essential to Bacula.\n\n"
1009 "I'm going to write one record in file 0,\n"
1010 " two records in file 1,\n"
1011 " and three records in file 2\n\n"));
1015 weofcmd(); /* end file 0 */
1018 weofcmd(); /* end file 1 */
1022 weofcmd(); /* end file 2 */
1023 if (dev_cap(dev, CAP_TWOEOF)) {
1026 force_close_dev(dev); /* release device */
1027 if (!open_the_device()) {
1031 Pmsg0(0, _("Now moving to end of medium.\n"));
1033 Pmsg2(-1, _("We should be in file 3. I am at file %d. This is %s\n"),
1034 dev->file, dev->file == 3 ? "correct!" : "NOT correct!!!!");
1036 if (dev->file != 3) {
1040 Pmsg0(-1, _("\nNow the important part, I am going to attempt to append to the tape.\n\n"));
1043 if (dev_cap(dev, CAP_TWOEOF)) {
1047 Pmsg0(-1, _("Done appending, there should be no I/O errors\n\n"));
1048 Pmsg0(-1, "Doing Bacula scan of blocks:\n");
1050 Pmsg0(-1, _("End scanning the tape.\n"));
1051 Pmsg2(-1, _("We should be in file 4. I am at file %d. This is %s\n"),
1052 dev->file, dev->file == 4 ? "correct!" : "NOT correct!!!!");
1054 if (dev->file != 4) {
1062 * This test exercises the autochanger
1064 static int autochanger_test()
1066 POOLMEM *results, *changer;
1067 int slot, status, loaded;
1068 int timeout = jcr->device->max_changer_wait;
1071 Dmsg1(100, "Max changer wait = %d sec\n", timeout);
1072 if (!dev_cap(dev, CAP_AUTOCHANGER)) {
1075 if (!(jcr->device && jcr->device->changer_name && jcr->device->changer_command)) {
1076 Pmsg0(-1, "\nAutochanger enabled, but no name or no command device specified.\n");
1080 Pmsg0(-1, "\nAh, I see you have an autochanger configured.\n"
1081 "To test the autochanger you must have a blank tape\n"
1082 " that I can write on in Slot 1.\n");
1083 if (!get_cmd("\nDo you wish to continue with the Autochanger test? (y/n): ")) {
1086 if (cmd[0] != 'y' && cmd[0] != 'Y') {
1090 Pmsg0(-1, _("\n\n=== Autochanger test ===\n\n"));
1092 results = get_pool_memory(PM_MESSAGE);
1093 changer = get_pool_memory(PM_FNAME);
1097 jcr->VolCatInfo.Slot = slot;
1098 /* Find out what is loaded, zero means device is unloaded */
1099 Pmsg0(-1, _("3301 Issuing autochanger \"loaded\" command.\n"));
1100 changer = edit_device_codes(jcr, changer, jcr->device->changer_command,
1102 status = run_program(changer, timeout, results);
1103 Dmsg3(100, "run_prog: %s stat=%d result=%s\n", changer, status, results);
1105 loaded = atoi(results);
1107 Pmsg1(-1, _("3991 Bad autochanger command: %s\n"), changer);
1108 Pmsg2(-1, _("3991 status=%d result=%s\n"), status, results);
1112 Pmsg1(-1, "Slot %d loaded. I am going to unload it.\n", loaded);
1114 Pmsg0(-1, "Nothing loaded in the drive. OK.\n");
1116 Dmsg1(100, "Results from loaded query=%s\n", results);
1118 jcr->VolCatInfo.Slot = loaded;
1119 offline_or_rewind_dev(dev);
1120 /* We are going to load a new tape, so close the device */
1121 force_close_dev(dev);
1122 Pmsg2(-1, _("3302 Issuing autochanger \"unload %d %d\" command.\n"),
1123 loaded, dev->drive_index);
1124 changer = edit_device_codes(jcr, changer,
1125 jcr->device->changer_command, "unload");
1126 status = run_program(changer, timeout, results);
1127 Pmsg2(-1, "unload status=%s %d\n", status==0?"OK":"Bad", status);
1129 Pmsg1(-1, _("3992 Bad autochanger command: %s\n"), changer);
1130 Pmsg2(-1, _("3992 status=%d result=%s\n"), status, results);
1139 jcr->VolCatInfo.Slot = slot;
1140 Pmsg2(-1, _("3303 Issuing autochanger \"load slot %d %d\" command.\n"),
1141 slot, dev->drive_index);
1142 changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "load");
1143 Dmsg1(100, "Changer=%s\n", changer);
1144 force_close_dev(dev);
1145 status = run_program(changer, timeout, results);
1147 Pmsg2(-1, _("3303 Autochanger \"load slot %d %d\" status is OK.\n"),
1148 slot, dev->drive_index);
1150 Pmsg1(-1, _("3993 Bad autochanger command: %s\n"), changer);
1151 Pmsg2(-1, _("3993 status=%d result=%s\n"), status, results);
1155 if (!open_the_device()) {
1158 bmicrosleep(sleep_time, 0);
1159 if (!rewind_dev(dev)) {
1160 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
1161 clrerror_dev(dev, -1);
1162 Pmsg0(-1, "\nThe test failed, probably because you need to put\n"
1163 "a longer sleep time in the mtx-script in the load) case.\n"
1164 "Adding a 30 second sleep and trying again ...\n");
1168 Pmsg1(0, "Rewound %s\n", dev_name(dev));
1171 if ((status = weof_dev(dev, 1)) < 0) {
1172 Pmsg2(0, "Bad status from weof %d. ERR=%s\n", status, strerror_dev(dev));
1175 Pmsg1(0, "Wrote EOF to %s\n", dev_name(dev));
1179 Pmsg1(-1, "\nThe test worked this time. Please add:\n\n"
1181 "to your mtx-changer script in the load) case.\n\n",
1184 Pmsg0(-1, "\nThe test autochanger worked!!\n\n");
1187 free_pool_memory(changer);
1188 free_pool_memory(results);
1193 free_pool_memory(changer);
1194 free_pool_memory(results);
1195 Pmsg0(-1, "You must correct this error or the Autochanger will not work.\n");
1199 static void autochangercmd()
1206 * This test assumes that the append test has been done,
1207 * then it tests the fsf function.
1209 static int fsf_test()
1211 bool set_off = false;
1213 Pmsg0(-1, _("\n\n=== Forward space files test ===\n\n"
1214 "This test is essential to Bacula.\n\n"
1215 "I'm going to write five files then test forward spacing\n\n"));
1219 weofcmd(); /* end file 0 */
1222 weofcmd(); /* end file 1 */
1226 weofcmd(); /* end file 2 */
1229 weofcmd(); /* end file 3 */
1231 weofcmd(); /* end file 4 */
1232 if (dev_cap(dev, CAP_TWOEOF)) {
1238 Pmsg0(0, _("Now forward spacing 1 file.\n"));
1239 if (!fsf_dev(dev, 1)) {
1240 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1243 Pmsg2(-1, _("We should be in file 1. I am at file %d. This is %s\n"),
1244 dev->file, dev->file == 1 ? "correct!" : "NOT correct!!!!");
1246 if (dev->file != 1) {
1250 Pmsg0(0, _("Now forward spacing 2 files.\n"));
1251 if (!fsf_dev(dev, 2)) {
1252 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1255 Pmsg2(-1, _("We should be in file 3. I am at file %d. This is %s\n"),
1256 dev->file, dev->file == 3 ? "correct!" : "NOT correct!!!!");
1258 if (dev->file != 3) {
1263 Pmsg0(0, _("Now forward spacing 4 files.\n"));
1264 if (!fsf_dev(dev, 4)) {
1265 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1268 Pmsg2(-1, _("We should be in file 4. I am at file %d. This is %s\n"),
1269 dev->file, dev->file == 4 ? "correct!" : "NOT correct!!!!");
1271 if (dev->file != 4) {
1275 Pmsg0(-1, "The test worked this time. Please add:\n\n"
1276 " Fast Forward Space File = no\n\n"
1277 "to your Device resource for this drive.\n");
1281 Pmsg0(0, _("Now forward spacing 1 more file.\n"));
1282 if (!fsf_dev(dev, 1)) {
1283 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1285 Pmsg2(-1, _("We should be in file 5. I am at file %d. This is %s\n"),
1286 dev->file, dev->file == 5 ? "correct!" : "NOT correct!!!!");
1287 if (dev->file != 5) {
1290 Pmsg0(-1, _("\n=== End Forward space files test ===\n\n"));
1294 Pmsg0(-1, _("\nThe forward space file test failed.\n"));
1295 if (dev_cap(dev, CAP_FASTFSF)) {
1296 Pmsg0(-1, "You have Fast Forward Space File enabled.\n"
1297 "I am turning it off then retrying the test.\n");
1298 dev->capabilities &= ~CAP_FASTFSF;
1302 Pmsg0(-1, "You must correct this error or Bacula will not work.\n");
1311 * This is a general test of Bacula's functions
1312 * needed to read and write the tape.
1314 static void testcmd()
1318 if (!write_read_test()) {
1321 if (!position_test()) {
1325 stat = append_test();
1326 if (stat == 1) { /* OK get out */
1329 if (stat == -1) { /* first test failed */
1330 if (dev_cap(dev, CAP_EOM) || dev_cap(dev, CAP_FASTFSF)) {
1331 Pmsg0(-1, "\nAppend test failed. Attempting again.\n"
1332 "Setting \"Hardware End of Medium = no\n"
1333 " and \"Fast Forward Space File = no\n"
1334 "and retrying append test.\n\n");
1335 dev->capabilities &= ~CAP_EOM; /* turn off eom */
1336 dev->capabilities &= ~CAP_FASTFSF; /* turn off fast fsf */
1337 stat = append_test();
1339 Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n"
1340 " Hardware End of Medium = No\n\n"
1341 " Fast Forward Space File = No\n"
1342 "to your Device resource in the Storage conf file.\n");
1346 Pmsg0(-1, "\n\nThat appears *NOT* to have corrected the problem.\n");
1349 /* Wrong count after append */
1351 Pmsg0(-1, "\n\nIt looks like the append failed. Attempting again.\n"
1352 "Setting \"BSF at EOM = yes\" and retrying append test.\n");
1353 dev->capabilities |= CAP_BSFATEOM; /* backspace on eom */
1354 stat = append_test();
1356 Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n"
1357 " Hardware End of Medium = No\n"
1358 " Fast Forward Space File = No\n"
1359 " BSF at EOM = yes\n\n"
1360 "to your Device resource in the Storage conf file.\n");
1367 Pmsg0(-1, "\nAppend test failed.\n\n");
1368 Pmsg0(-1, "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
1369 "Unable to correct the problem. You MUST fix this\n"
1370 "problem before Bacula can use your tape drive correctly\n");
1371 Pmsg0(-1, "\nPerhaps running Bacula in fixed block mode will work.\n"
1372 "Do so by setting:\n\n"
1373 "Minimum Block Size = nnn\n"
1374 "Maximum Block Size = nnn\n\n"
1375 "in your Storage daemon's Device definition.\n"
1376 "nnn must match your tape driver's block size, which\n"
1377 "can be determined by reading your tape manufacturers\n"
1378 "information, and the information on your kernel dirver.\n"
1379 "Fixed block sizes, however, are not normally an ideal solution.\n");
1384 Pmsg0(-1, _("\nThe above Bacula scan should have output identical to what follows.\n"
1385 "Please double check it ...\n"
1386 "=== Sample correct output ===\n"
1387 "1 block of 64448 bytes in file 1\n"
1388 "End of File mark.\n"
1389 "2 blocks of 64448 bytes in file 2\n"
1390 "End of File mark.\n"
1391 "3 blocks of 64448 bytes in file 3\n"
1392 "End of File mark.\n"
1393 "1 block of 64448 bytes in file 4\n"
1394 "End of File mark.\n"
1395 "Total files=4, blocks=7, bytes = 451,136\n"
1396 "=== End sample correct output ===\n\n"));
1398 Pmsg0(-1, _("If the above scan output is not identical to the\n"
1399 "sample output, you MUST correct the problem\n"
1400 "or Bacula will not be able to write multiple Jobs to \n"
1404 re_read_block_test();
1407 fsf_test(); /* do fast forward space file test */
1409 autochanger_test(); /* do autochanger test */
1413 /* Forward space a file */
1414 static void fsfcmd()
1418 num = atoi(argk[1]);
1423 if (!fsf_dev(dev, num)) {
1424 Pmsg1(0, "Bad status from fsf. ERR=%s\n", strerror_dev(dev));
1427 Pmsg2(0, "Forward spaced %d file%s.\n", num, num==1?"":"s");
1430 /* Forward space a record */
1431 static void fsrcmd()
1435 num = atoi(argk[1]);
1440 if (!fsr_dev(dev, num)) {
1441 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1444 Pmsg2(0, "Forward spaced %d record%s.\n", num, num==1?"":"s");
1449 * Write a Bacula block to the tape
1457 sm_check(__FILE__, __LINE__, false);
1458 block = new_block(dev);
1460 dump_block(block, "test");
1462 i = block->buf_len - 100;
1464 rec->data = check_pool_memory_size(rec->data, i);
1465 memset(rec->data, i & 0xFF, i);
1467 sm_check(__FILE__, __LINE__, false);
1468 if (!write_record_to_block(block, rec)) {
1469 Pmsg0(0, _("Error writing record to block.\n"));
1472 if (!write_block_to_dev(dcr, block)) {
1473 Pmsg0(0, _("Error writing block to device.\n"));
1476 Pmsg1(0, _("Wrote one record of %d bytes.\n"), i);
1478 Pmsg0(0, _("Wrote block to device.\n"));
1481 sm_check(__FILE__, __LINE__, false);
1484 sm_check(__FILE__, __LINE__, false);
1488 * Read a record from the tape
1495 if (!get_cmd("Enter length to read: ")) {
1499 if (len < 0 || len > 1000000) {
1500 Pmsg0(0, _("Bad length entered, using default of 1024 bytes.\n"));
1503 buf = (char *)malloc(len);
1504 stat = read(dev->fd, buf, len);
1505 if (stat > 0 && stat <= len) {
1508 Pmsg3(0, _("Read of %d bytes gives stat=%d. ERR=%s\n"),
1509 len, stat, strerror(errno));
1515 * Scan tape by reading block by block. Report what is
1516 * on the tape. Note, this command does raw reads, and as such
1517 * will not work with fixed block size devices.
1519 static void scancmd()
1522 int blocks, tot_blocks, tot_files;
1528 blocks = block_size = tot_blocks = 0;
1530 if (dev->state & ST_EOT) {
1531 Pmsg0(0, "End of tape\n");
1534 update_pos_dev(dev);
1535 tot_files = dev->file;
1536 Pmsg1(0, _("Starting scan at file %u\n"), dev->file);
1538 if ((stat = read(dev->fd, buf, sizeof(buf))) < 0) {
1539 clrerror_dev(dev, -1);
1540 Mmsg2(&dev->errmsg, "read error on %s. ERR=%s.\n",
1541 dev->dev_name, strerror(dev->dev_errno));
1542 Pmsg2(0, "Bad status from read %d. ERR=%s\n", stat, strerror_dev(dev));
1544 printf("%d block%s of %d bytes in file %d\n",
1545 blocks, blocks>1?"s":"", block_size, dev->file);
1548 Dmsg1(200, "read status = %d\n", stat);
1550 if (stat != block_size) {
1551 update_pos_dev(dev);
1553 printf("%d block%s of %d bytes in file %d\n",
1554 blocks, blocks>1?"s":"", block_size, dev->file);
1559 if (stat == 0) { /* EOF */
1560 update_pos_dev(dev);
1561 printf("End of File mark.\n");
1562 /* Two reads of zero means end of tape */
1563 if (dev->state & ST_EOF)
1564 dev->state |= ST_EOT;
1566 dev->state |= ST_EOF;
1569 if (dev->state & ST_EOT) {
1570 printf("End of tape\n");
1573 } else { /* Got data */
1574 dev->state &= ~ST_EOF;
1580 update_pos_dev(dev);
1581 tot_files = dev->file - tot_files;
1582 printf("Total files=%d, blocks=%d, bytes = %s\n", tot_files, tot_blocks,
1583 edit_uint64_with_commas(bytes, ec1));
1588 * Scan tape by reading Bacula block by block. Report what is
1589 * on the tape. This function reads Bacula blocks, so if your
1590 * Device resource is correctly defined, it should work with
1591 * either variable or fixed block sizes.
1593 static void scan_blocks()
1595 int blocks, tot_blocks, tot_files;
1596 uint32_t block_size;
1601 block = new_block(dev);
1602 blocks = block_size = tot_blocks = 0;
1605 update_pos_dev(dev);
1606 tot_files = dev->file;
1608 if (!read_block_from_device(dcr, block, NO_BLOCK_NUMBER_CHECK)) {
1609 Dmsg1(100, "!read_block(): ERR=%s\n", strerror_dev(dev));
1610 if (dev->state & ST_EOT) {
1612 printf("%d block%s of %d bytes in file %d\n",
1613 blocks, blocks>1?"s":"", block_size, dev->file);
1618 if (dev->state & ST_EOF) {
1620 printf("%d block%s of %d bytes in file %d\n",
1621 blocks, blocks>1?"s":"", block_size, dev->file);
1624 printf(_("End of File mark.\n"));
1627 if (dev->state & ST_SHORT) {
1629 printf("%d block%s of %d bytes in file %d\n",
1630 blocks, blocks>1?"s":"", block_size, dev->file);
1633 printf(_("Short block read.\n"));
1636 printf(_("Error reading block. ERR=%s\n"), strerror_dev(dev));
1639 if (block->block_len != block_size) {
1641 printf("%d block%s of %d bytes in file %d\n",
1642 blocks, blocks>1?"s":"", block_size, dev->file);
1645 block_size = block->block_len;
1649 bytes += block->block_len;
1650 Dmsg6(100, "Blk_blk=%u dev_blk=%u blen=%u bVer=%d SessId=%u SessTim=%u\n",
1651 block->BlockNumber, dev->block_num, block->block_len, block->BlockVer,
1652 block->VolSessionId, block->VolSessionTime);
1654 DEV_RECORD *rec = new_record();
1655 read_record_from_block(block, rec);
1656 Pmsg8(-1, "Blk_block: %u dev_blk=%u blen=%u First rec FI=%s SessId=%u SessTim=%u Strm=%s rlen=%d\n",
1657 block->BlockNumber, dev->block_num, block->block_len,
1658 FI_to_ascii(rec->FileIndex), rec->VolSessionId, rec->VolSessionTime,
1659 stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len);
1662 } else if (verbose > 1) {
1663 dump_block(block, "");
1669 tot_files = dev->file - tot_files;
1670 printf("Total files=%d, blocks=%d, bytes = %s\n", tot_files, tot_blocks,
1671 edit_uint64_with_commas(bytes, ec1));
1675 static void statcmd()
1677 int debug = debug_level;
1679 Pmsg2(0, "Device status: %u. ERR=%s\n", status_dev(dev), strerror_dev(dev));
1681 dump_volume_label(dev);
1683 debug_level = debug;
1688 * First we label the tape, then we fill
1689 * it with data get a new tape and write a few blocks.
1691 static void fillcmd()
1698 uint32_t min_block_size;
1708 This command simulates Bacula writing to a tape.\n\
1709 It requires either one or two blank tapes, which it\n\
1710 will label and write.\n\n\
1711 If you have an autochanger configured, it will use\n\
1712 the tapes that are in slots 1 and 2, otherwise, you will\n\
1713 be prompted to insert the tapes when necessary.\n\n\
1714 It will print a status approximately\n\
1715 every 322 MB, and write an EOF every 3.2 GB. If you have\n\
1716 selected the simple test option, after writing the first tape\n\
1717 it will rewind it and re-read the last block written.\n\n\
1718 If you have selected the multiple tape test, when the first tape\n\
1719 fills, it will ask for a second, and after writing a few more \n\
1720 blocks, it will stop. Then it will begin re-reading the\n\
1722 This may take a long time -- hours! ...\n\n");
1724 get_cmd("Do you want to run the simplified test (s) with one tape\n"
1725 "or the complete multiple tape (m) test: (s/m) ");
1726 if (cmd[0] == 's') {
1727 Pmsg0(-1, "Simple test (single tape) selected.\n");
1729 } else if (cmd[0] == 'm') {
1730 Pmsg0(-1, "Multiple tape test selected.\n");
1733 Pmsg0(000, "Command aborted.\n");
1737 Dmsg1(20, "Begin append device=%s\n", dev_name(dev));
1738 Dmsg1(20, "MaxVolSize=%s\n", edit_uint64(dev->max_volume_size, ec1));
1740 /* Use fixed block size to simplify read back */
1741 min_block_size = dev->min_block_size;
1742 dev->min_block_size = dev->max_block_size;
1745 * Acquire output device for writing. Note, after acquiring a
1746 * device, we MUST release it, which is done at the end of this
1749 Dmsg0(100, "just before acquire_device\n");
1750 if (!acquire_device_for_append(jcr)) {
1751 set_jcr_job_status(jcr, JS_ErrorTerminated);
1754 block = jcr->dcr->block;
1756 Dmsg0(100, "Just after acquire_device_for_append\n");
1758 * Write Begin Session Record
1760 if (!write_session_label(dcr, block, SOS_LABEL)) {
1761 set_jcr_job_status(jcr, JS_ErrorTerminated);
1762 Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"),
1766 Pmsg0(-1, "Wrote Start Of Session label.\n");
1768 memset(&rec, 0, sizeof(rec));
1769 rec.data = get_memory(100000); /* max record size */
1771 #define REC_SIZE 32768
1772 rec.data_len = REC_SIZE;
1775 * Put some random data in the record
1777 fd = open("/dev/urandom", O_RDONLY);
1779 read(fd, rec.data, rec.data_len);
1782 uint32_t *p = (uint32_t *)rec.data;
1783 srandom(time(NULL));
1784 for (i=0; i<rec.data_len/sizeof(uint32_t); i++) {
1790 * Generate data as if from File daemon, write to device
1792 jcr->dcr->VolFirstIndex = 0;
1793 time(&jcr->run_time); /* start counting time for rates */
1795 Pmsg0(-1, "Begin writing Bacula records to tape ...\n");
1797 Pmsg0(-1, "Begin writing Bacula records to first tape ...\n");
1799 for (file_index = 0; ok && !job_canceled(jcr); ) {
1800 rec.VolSessionId = jcr->VolSessionId;
1801 rec.VolSessionTime = jcr->VolSessionTime;
1802 rec.FileIndex = ++file_index;
1803 rec.Stream = STREAM_FILE_DATA;
1805 /* Mix up the data just a bit */
1806 uint32_t *lp = (uint32_t *)rec.data;
1808 for (i=1; i < (rec.data_len-sizeof(uint32_t))/sizeof(uint32_t)-1; i++) {
1812 Dmsg4(250, "before write_rec FI=%d SessId=%d Strm=%s len=%d\n",
1813 rec.FileIndex, rec.VolSessionId, stream_to_ascii(rec.Stream, rec.FileIndex),
1816 while (!write_record_to_block(block, &rec)) {
1818 * When we get here we have just filled a block
1820 Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec.data_len,
1823 /* Write block to tape */
1824 if (!flush_block(block, 1)) {
1828 /* Every 5000 blocks (approx 322MB) report where we are.
1830 if ((block->BlockNumber % 5000) == 0) {
1832 now -= jcr->run_time;
1834 now = 1; /* prevent divide error */
1836 kbs = (double)dev->VolCatInfo.VolCatBytes / (1000.0 * (double)now);
1837 Pmsg4(-1, "Wrote blk_block=%u, dev_blk_num=%u VolBytes=%s rate=%.1f KB/s\n",
1838 block->BlockNumber, dev->block_num,
1839 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), (float)kbs);
1841 /* Every 15000 blocks (approx 1GB) write an EOF.
1843 if ((block->BlockNumber % 15000) == 0) {
1844 Pmsg0(-1, "Flush block, write EOF\n");
1845 flush_block(block, 0);
1849 /* Get out after writing 10 blocks to the second tape */
1850 if (++BlockNumber > 10 && stop != 0) { /* get out */
1855 Pmsg0(000, _("Not OK\n"));
1858 jcr->JobBytes += rec.data_len; /* increment bytes this job */
1859 Dmsg4(190, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
1860 FI_to_ascii(rec.FileIndex), rec.VolSessionId,
1861 stream_to_ascii(rec.Stream, rec.FileIndex), rec.data_len);
1863 /* Get out after writing 10 blocks to the second tape */
1864 if (BlockNumber > 10 && stop != 0) { /* get out */
1865 Pmsg0(-1, "Done writing ...\n");
1870 Dmsg0(100, "Write_end_session_label()\n");
1871 /* Create Job status for end of session label */
1872 if (!job_canceled(jcr) && ok) {
1873 set_jcr_job_status(jcr, JS_Terminated);
1875 set_jcr_job_status(jcr, JS_ErrorTerminated);
1877 if (!write_session_label(dcr, block, EOS_LABEL)) {
1878 Pmsg1(000, _("Error writting end session label. ERR=%s\n"), strerror_dev(dev));
1881 /* Write out final block of this session */
1882 if (!write_block_to_device(dcr, block)) {
1883 Pmsg0(-1, _("Set ok=false after write_block_to_device.\n"));
1886 Pmsg0(-1, _("Wrote End Of Session label.\n"));
1888 /* Save last block info for second tape */
1889 last_block_num2 = last_block_num;
1890 last_file2 = last_file;
1892 free_block(last_block2);
1894 last_block2 = dup_block(last_block);
1897 sprintf(buf, "%s/btape.state", working_directory);
1898 fd = open(buf, O_CREAT|O_TRUNC|O_WRONLY, 0640);
1900 write(fd, &btape_state_level, sizeof(btape_state_level));
1901 write(fd, &simple, sizeof(simple));
1902 write(fd, &last_block_num1, sizeof(last_block_num1));
1903 write(fd, &last_block_num2, sizeof(last_block_num2));
1904 write(fd, &last_file1, sizeof(last_file1));
1905 write(fd, &last_file2, sizeof(last_file2));
1906 write(fd, last_block1->buf, last_block1->buf_len);
1907 write(fd, last_block2->buf, last_block2->buf_len);
1908 write(fd, first_block->buf, first_block->buf_len);
1910 Pmsg2(-1, "Wrote state file last_block_num1=%d last_block_num2=%d\n",
1911 last_block_num1, last_block_num2);
1913 Pmsg2(-1, _("Could not create state file: %s ERR=%s\n"), buf,
1917 /* Release the device if multiple tapes being used */
1918 if (!simple && !release_device(jcr)) {
1919 Pmsg0(-1, _("Error in release_device\n"));
1923 Pmsg2(-1, _("\n\nDone filling tape%s. Now beginning re-read of %stape ...\n"),
1924 simple?"":"s", simple?"":"first ");
1928 dev->min_block_size = min_block_size;
1929 free_memory(rec.data);
1933 * Read two tapes written by the "fill" command and ensure
1934 * that the data is valid. If stop==1 we simulate full read back
1935 * of two tapes. If stop==-1 we simply read the last block and
1936 * verify that it is correct.
1938 static void unfillcmd()
1942 last_block1 = new_block(dev);
1943 last_block2 = new_block(dev);
1944 first_block = new_block(dev);
1945 sprintf(buf, "%s/btape.state", working_directory);
1946 fd = open(buf, O_RDONLY);
1948 uint32_t state_level;
1949 read(fd, &state_level, sizeof(btape_state_level));
1950 read(fd, &simple, sizeof(simple));
1951 read(fd, &last_block_num1, sizeof(last_block_num1));
1952 read(fd, &last_block_num2, sizeof(last_block_num2));
1953 read(fd, &last_file1, sizeof(last_file1));
1954 read(fd, &last_file2, sizeof(last_file2));
1955 read(fd, last_block1->buf, last_block1->buf_len);
1956 read(fd, last_block2->buf, last_block2->buf_len);
1957 read(fd, first_block->buf, first_block->buf_len);
1959 if (state_level != btape_state_level) {
1960 Pmsg0(-1, "\nThe state file level has changed. You must redo\n"
1961 "the fill command.\n");
1965 Pmsg2(-1, "\nCould not find the state file: %s ERR=%s\n"
1966 "You must redo the fill command.\n", buf, strerror(errno));
1973 static void do_unfill()
1981 block = new_block(dev);
1983 Dmsg0(20, "Enter do_unfill\n");
1984 dev->capabilities |= CAP_ANONVOLS; /* allow reading any volume */
1985 dev->capabilities &= ~CAP_LABEL; /* don't label anything here */
1989 time(&jcr->run_time); /* start counting time for rates */
1993 free_block(last_block);
1995 last_block_num = last_block_num1;
1996 last_file = last_file1;
1997 last_block = last_block1;
2000 /* Multiple Volume tape */
2001 /* Close device so user can use autochanger if desired */
2002 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2005 autochanger = autoload_device(dcr, 1, NULL);
2007 force_close_dev(dev);
2008 get_cmd(_("Mount first tape. Press enter when ready: "));
2011 jcr->dcr = new_dcr(jcr, dev);
2012 set_volume_name("TestVolume1", 1);
2014 create_vol_list(jcr);
2016 dev->state &= ~ST_READ;
2017 if (!acquire_device_for_read(jcr)) {
2018 Pmsg1(-1, "%s", dev->errmsg);
2023 * We now have the first tape mounted.
2024 * Note, re-reading last block may have caused us to
2025 * loose track of where we are (block number unknown).
2027 rewind_dev(dev); /* get to a known place on tape */
2028 /* Read the first 1000 records */
2029 Pmsg0(-1, _("Reading the first 1000 records.\n"));
2030 read_records(dcr, quickie_cb, my_mount_next_read_volume);
2031 Pmsg4(-1, _("Reposition from %u:%u to %u:%u\n"), dev->file, dev->block_num,
2032 last_file, last_block_num);
2033 if (!reposition_dev(dev, last_file, last_block_num)) {
2034 Pmsg1(-1, "Reposition error. ERR=%s\n", strerror_dev(dev));
2036 Pmsg1(-1, _("Reading block %u.\n"), last_block_num);
2037 if (!read_block_from_device(dcr, block, NO_BLOCK_NUMBER_CHECK)) {
2038 Pmsg1(-1, _("Error reading block: ERR=%s\n"), strerror_dev(dev));
2041 if (compare_blocks(last_block, block)) {
2043 Pmsg0(-1, _("\nThe last block on the tape matches. Test succeeded.\n\n"));
2045 Pmsg0(-1, _("\nThe last block of the first tape matches.\n\n"));
2052 /* restore info for last block on second Volume */
2053 last_block_num = last_block_num2;
2054 last_file = last_file2;
2055 last_block = last_block2;
2057 /* Multiple Volume tape */
2058 /* Close device so user can use autochanger if desired */
2059 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2064 set_volume_name("TestVolume2", 2);
2066 create_vol_list(jcr);
2067 autochanger = autoload_device(dcr, 1, NULL);
2069 force_close_dev(dev);
2070 get_cmd(_("Mount second tape. Press enter when ready: "));
2073 dev->state &= ~ST_READ;
2074 if (!acquire_device_for_read(jcr)) {
2075 Pmsg1(-1, "%s", dev->errmsg);
2079 /* Space to "first" block which is last block not written
2080 * on the previous tape.
2082 Pmsg2(-1, _("Reposition from %u:%u to 0:1\n"), dev->file, dev->block_num);
2083 if (!reposition_dev(dev, 0, 1)) {
2084 Pmsg1(-1, "Reposition error. ERR=%s\n", strerror_dev(dev));
2087 Pmsg1(-1, _("Reading block %d.\n"), dev->block_num);
2088 if (!read_block_from_device(dcr, block, NO_BLOCK_NUMBER_CHECK)) {
2089 Pmsg1(-1, _("Error reading block: ERR=%s\n"), strerror_dev(dev));
2092 if (compare_blocks(first_block, block)) {
2093 Pmsg0(-1, _("\nThe first block on the second tape matches.\n\n"));
2096 /* Now find and compare the last block */
2097 Pmsg4(-1, _("Reposition from %u:%u to %u:%u\n"), dev->file, dev->block_num,
2098 last_file, last_block_num);
2099 if (!reposition_dev(dev, last_file, last_block_num)) {
2100 Pmsg1(-1, "Reposition error. ERR=%s\n", strerror_dev(dev));
2103 Pmsg1(-1, _("Reading block %d.\n"), dev->block_num);
2104 if (!read_block_from_device(dcr, block, NO_BLOCK_NUMBER_CHECK)) {
2105 Pmsg1(-1, _("Error reading block: ERR=%s\n"), strerror_dev(dev));
2108 if (compare_blocks(last_block, block)) {
2109 Pmsg0(-1, _("\nThe last block on the second tape matches. Test succeeded.\n\n"));
2114 free_block(last_block1);
2115 free_block(last_block2);
2116 free_block(first_block);
2119 /* Read 1000 records then stop */
2120 static bool quickie_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
2123 return quickie_count <= 1000;
2126 static bool compare_blocks(DEV_BLOCK *last_block, DEV_BLOCK *block)
2129 uint32_t CheckSum, block_len;
2132 p = last_block->buf;
2134 unser_begin(q, BLKHDR2_LENGTH);
2135 unser_uint32(CheckSum);
2136 unser_uint32(block_len);
2137 while (q < (block->buf+block_len)) {
2144 dump_block(last_block, _("Last block written"));
2146 dump_block(block, _("Block read back"));
2147 Pmsg1(-1, "\n\nThe blocks differ at byte %u\n", p - last_block->buf);
2148 Pmsg0(-1, "\n\n!!!! The last block written and the block\n"
2149 "that was read back differ. The test FAILED !!!!\n"
2150 "This must be corrected before you use Bacula\n"
2151 "to write multi-tape Volumes.!!!!\n");
2155 dump_block(last_block, _("Last block written"));
2156 dump_block(block, _("Block read back"));
2166 * Write current block to tape regardless of whether or
2167 * not it is full. If the tape fills, attempt to
2168 * acquire another tape.
2170 static int flush_block(DEV_BLOCK *block, int dump)
2175 uint32_t this_file, this_block_num;
2178 this_block = new_block(dev);
2181 last_block = new_block(dev);
2184 this_file = dev->file;
2185 this_block_num = dev->block_num;
2186 if (!write_block_to_dev(dcr, block)) {
2187 Pmsg3(000, "Last block at: %u:%u this_dev_block_num=%d\n",
2188 last_file, last_block_num, this_block_num);
2191 * This is 1st tape, so save first tape info separate
2192 * from second tape info
2194 last_block_num1 = last_block_num;
2195 last_file1 = last_file;
2196 last_block1 = dup_block(last_block);
2197 last_block2 = dup_block(last_block);
2198 first_block = dup_block(block); /* first block second tape */
2201 Pmsg3(000, "Block not written: FileIndex=%u blk_block=%u Size=%u\n",
2202 (unsigned)file_index, block->BlockNumber, block->block_len);
2203 dump_block(last_block, "Last block written");
2205 dump_block(block, "Block not written");
2208 eot_block = block->BlockNumber;
2209 eot_block_len = block->block_len;
2210 eot_FileIndex = file_index;
2214 now -= jcr->run_time;
2216 now = 1; /* don't divide by zero */
2218 kbs = (double)dev->VolCatInfo.VolCatBytes / (1000 * now);
2219 vol_size = dev->VolCatInfo.VolCatBytes;
2220 Pmsg2(000, "End of tape. VolumeCapacity=%s. Write rate = %.1f KB/s\n",
2221 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), kbs);
2224 stop = -1; /* stop, but do simplified test */
2226 /* Full test in progress */
2227 if (!fixup_device_block_write_error(jcr->dcr, block)) {
2228 Pmsg1(000, _("Cannot fixup device error. %s\n"), strerror_dev(dev));
2233 BlockNumber = 0; /* start counting for second tape */
2236 return 1; /* end of tape reached */
2239 /* Save contents after write so that the header is serialized */
2240 memcpy(this_block->buf, block->buf, this_block->buf_len);
2243 * Toggle between two allocated blocks for efficiency.
2244 * Switch blocks so that the block just successfully written is
2245 * always in last_block.
2247 tblock = last_block;
2248 last_block = this_block;
2249 this_block = tblock;
2250 last_file = this_file;
2251 last_block_num = this_block_num;
2259 * First we label the tape, then we fill
2260 * it with data get a new tape and write a few blocks.
2262 static void qfillcmd()
2268 Pmsg0(0, "Test writing blocks of 64512 bytes to tape.\n");
2270 get_cmd("How many blocks do you want to write? (1000): ");
2277 sm_check(__FILE__, __LINE__, false);
2278 block = new_block(dev);
2281 i = block->buf_len - 100;
2283 rec->data = check_pool_memory_size(rec->data, i);
2284 memset(rec->data, i & 0xFF, i);
2287 Pmsg1(0, "Begin writing %d Bacula blocks to tape ...\n", count);
2288 for (i=0; i < count; i++) {
2293 if (!write_record_to_block(block, rec)) {
2294 Pmsg0(0, _("Error writing record to block.\n"));
2297 if (!write_block_to_dev(dcr, block)) {
2298 Pmsg0(0, _("Error writing block to device.\n"));
2304 if (dev_cap(dev, CAP_TWOEOF)) {
2311 sm_check(__FILE__, __LINE__, false);
2314 sm_check(__FILE__, __LINE__, false);
2319 * Fill a tape using raw write() command
2321 static void rawfill_cmd()
2326 uint32_t block_num = 0;
2331 block = new_block(dev);
2332 fd = open("/dev/urandom", O_RDONLY);
2334 read(fd, block->buf, block->buf_len);
2337 uint32_t *p = (uint32_t *)block->buf;
2338 srandom(time(NULL));
2339 for (i=0; i<block->buf_len/sizeof(uint32_t); i++) {
2343 p = (uint32_t *)block->buf;
2344 Pmsg1(0, "Begin writing raw blocks of %u bytes.\n", block->buf_len);
2347 stat = write(dev->fd, block->buf, block->buf_len);
2348 if (stat == (int)block->buf_len) {
2349 if ((block_num++ % 100) == 0) {
2354 for (i=1; i<(block->buf_len-sizeof(uint32_t))/sizeof(uint32_t)-1; i++) {
2363 printf("Write failed at block %u. stat=%d ERR=%s\n", block_num, stat,
2364 strerror(my_errno));
2371 * Fill a tape using raw write() command
2373 static void bfill_cmd()
2376 uint32_t block_num = 0;
2382 block = new_block(dev);
2383 fd = open("/dev/urandom", O_RDONLY);
2385 read(fd, block->buf, block->buf_len);
2388 uint32_t *p = (uint32_t *)block->buf;
2389 srandom(time(NULL));
2390 for (i=0; i<block->buf_len/sizeof(uint32_t); i++) {
2394 p = (uint32_t *)block->buf;
2395 Pmsg1(0, "Begin writing Bacula blocks of %u bytes.\n", block->buf_len);
2398 block->binbuf = block->buf_len;
2399 block->bufp = block->buf + block->binbuf;
2400 if (!write_block_to_dev(dcr, block)) {
2403 if ((block_num++ % 100) == 0) {
2408 for (i=1; i<(block->buf_len/sizeof(uint32_t)-1); i++) {
2414 printf("Write failed at block %u.\n", block_num);
2420 struct cmdstruct { const char *key; void (*func)(); const char *help; };
2421 static struct cmdstruct commands[] = {
2422 {"autochanger", autochangercmd, "test autochanger"},
2423 {"bsf", bsfcmd, "backspace file"},
2424 {"bsr", bsrcmd, "backspace record"},
2425 {"bfill", bfill_cmd, "fill tape using Bacula writes"},
2426 {"cap", capcmd, "list device capabilities"},
2427 {"clear", clearcmd, "clear tape errors"},
2428 {"eod", eodcmd, "go to end of Bacula data for append"},
2429 {"eom", eomcmd, "go to the physical end of medium"},
2430 {"fill", fillcmd, "fill tape, write onto second volume"},
2431 {"unfill", unfillcmd, "read filled tape"},
2432 {"fsf", fsfcmd, "forward space a file"},
2433 {"fsr", fsrcmd, "forward space a record"},
2434 {"help", helpcmd, "print this command"},
2435 {"label", labelcmd, "write a Bacula label to the tape"},
2436 {"load", loadcmd, "load a tape"},
2437 {"quit", quitcmd, "quit btape"},
2438 {"rawfill", rawfill_cmd, "use write() to fill tape"},
2439 {"readlabel", readlabelcmd, "read and print the Bacula tape label"},
2440 {"rectest", rectestcmd, "test record handling functions"},
2441 {"rewind", rewindcmd, "rewind the tape"},
2442 {"scan", scancmd, "read() tape block by block to EOT and report"},
2443 {"scanblocks", scan_blocks, "Bacula read block by block to EOT and report"},
2444 {"status", statcmd, "print tape status"},
2445 {"test", testcmd, "General test Bacula tape functions"},
2446 {"weof", weofcmd, "write an EOF on the tape"},
2447 {"wr", wrcmd, "write a single Bacula block"},
2448 {"rr", rrcmd, "read a single record"},
2449 {"qfill", qfillcmd, "quick fill command"},
2451 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
2459 while (get_cmd("*")) {
2460 sm_check(__FILE__, __LINE__, false);
2462 parse_args(cmd, &args, &argc, argk, argv, MAX_CMD_ARGS);
2463 for (i=0; i<comsize; i++) /* search for command */
2464 if (argc > 0 && fstrsch(argk[0], commands[i].key)) {
2465 (*commands[i].func)(); /* go execute command */
2470 Pmsg1(0, _("%s is an illegal command\n"), cmd);
2476 static void helpcmd()
2480 printf(_("Interactive commands:\n"));
2481 printf(_(" Command Description\n ======= ===========\n"));
2482 for (i=0; i<comsize; i++)
2483 printf(" %-10s %s\n", commands[i].key, commands[i].help);
2490 "\nVersion: " VERSION " (" BDATE ")\n\n"
2491 "Usage: btape <options> <device_name>\n"
2492 " -b <file> specify bootstrap file\n"
2493 " -c <file> set configuration file to file\n"
2494 " -d <nn> set debug level to nn\n"
2495 " -p proceed inspite of I/O errors\n"
2496 " -s turn off signals\n"
2498 " -? print this message.\n"
2504 * Get next input command from terminal. This
2505 * routine is REALLY primitive, and should be enhanced
2506 * to have correct backspacing, etc.
2509 get_cmd(const char *prompt)
2513 fprintf(stdout, prompt);
2515 /* We really should turn off echoing and pretty this
2519 while ((ch = fgetc(stdin)) != EOF) {
2521 strip_trailing_junk(cmd);
2523 } else if (ch == 4 || ch == 0xd3 || ch == 0x8) {
2536 /* Dummies to replace askdir.c */
2537 bool dir_update_file_attributes(DCR *dcr, DEV_RECORD *rec) { return 1;}
2538 bool dir_send_job_status(JCR *jcr) {return 1;}
2540 bool dir_update_volume_info(DCR *dcr, bool relabel)
2546 bool dir_get_volume_info(DCR *dcr, enum get_vol_info_rw writing)
2548 JCR *jcr = dcr->jcr;
2549 Dmsg0(20, "Enter dir_get_volume_info\n");
2550 bstrncpy(jcr->VolCatInfo.VolCatName, jcr->VolumeName, sizeof(jcr->VolCatInfo.VolCatName));
2554 bool dir_create_jobmedia_record(DCR *dcr)
2556 dcr->WroteVol = false;
2561 bool dir_find_next_appendable_volume(DCR *dcr)
2563 JCR *jcr = dcr->jcr;
2564 Dmsg1(20, "Enter dir_find_next_appendable_volume. stop=%d\n", stop);
2565 return jcr->VolumeName[0] != 0;
2568 bool dir_ask_sysop_to_mount_volume(DCR *dcr)
2570 DEVICE *dev = dcr->dev;
2571 JCR *jcr = dcr->jcr;
2572 Dmsg0(20, "Enter dir_ask_sysop_to_mount_volume\n");
2573 if (jcr->VolumeName[0] == 0) {
2574 return dir_ask_sysop_to_create_appendable_volume(dcr);
2576 /* Close device so user can use autochanger if desired */
2577 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2580 force_close_dev(dev);
2581 Pmsg1(-1, "%s", dev->errmsg); /* print reason */
2582 if (jcr->VolumeName[0] == 0 || strcmp(jcr->VolumeName, "TestVolume2") == 0) {
2583 fprintf(stderr, "Mount second Volume on device %s and press return when ready: ",
2586 fprintf(stderr, "Mount Volume \"%s\" on device %s and press return when ready: ",
2587 jcr->VolumeName, dev_name(dev));
2593 bool dir_ask_sysop_to_create_appendable_volume(DCR *dcr)
2596 DEVICE *dev = dcr->dev;
2597 JCR *jcr = dcr->jcr;
2598 Dmsg0(20, "Enter dir_ask_sysop_to_create_appendable_volume\n");
2600 set_volume_name("TestVolume1", 1);
2602 set_volume_name("TestVolume2", 2);
2604 /* Close device so user can use autochanger if desired */
2605 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2608 autochanger = autoload_device(dcr, 1, NULL);
2610 force_close_dev(dev);
2611 fprintf(stderr, "Mount blank Volume on device %s and press return when ready: ",
2615 open_device(jcr, dev);
2622 static bool my_mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
2626 Dmsg0(20, "Enter my_mount_next_read_volume\n");
2627 Pmsg1(000, "End of Volume \"%s\"\n", jcr->VolumeName);
2629 if (LastBlock != block->BlockNumber) {
2630 VolBytes += block->block_len;
2632 LastBlock = block->BlockNumber;
2634 now -= jcr->run_time;
2638 kbs = (double)VolBytes / (1000.0 * (double)now);
2639 Pmsg3(-1, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
2640 edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
2642 if (strcmp(jcr->VolumeName, "TestVolume2") == 0) {
2648 set_volume_name("TestVolume2", 2);
2650 create_vol_list(jcr);
2652 dev->state &= ~ST_READ;
2653 if (!acquire_device_for_read(jcr)) {
2654 Pmsg2(0, "Cannot open Dev=%s, Vol=%s\n", dev_name(dev), jcr->VolumeName);
2657 return true; /* next volume mounted */
2660 static void set_volume_name(const char *VolName, int volnum)
2662 DCR *dcr = jcr->dcr;
2663 VolumeName = VolName;
2665 pm_strcpy(&jcr->VolumeName, VolName);
2666 bstrncpy(dev->VolCatInfo.VolCatName, VolName, sizeof(dev->VolCatInfo.VolCatName));
2667 bstrncpy(jcr->VolCatInfo.VolCatName, VolName, sizeof(jcr->VolCatInfo.VolCatName));
2668 bstrncpy(dcr->VolCatInfo.VolCatName, VolName, sizeof(dcr->VolCatInfo.VolCatName));
2669 bstrncpy(dcr->VolumeName, VolName, sizeof(dcr->VolumeName));
2670 jcr->VolCatInfo.Slot = volnum;
2671 dcr->VolCatInfo.Slot = volnum;