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();
43 /* Exported variables */
46 int bsize = TAPE_BSIZE;
47 char VolName[MAX_NAME_LENGTH];
48 bool forge_on = false;
51 * If you change the format of the state file,
52 * increment this value
54 static uint32_t btape_state_level = 2;
58 DEVRES *device = NULL;
61 /* Forward referenced subroutines */
62 static void do_tape_cmds();
63 static void helpcmd();
64 static void scancmd();
65 static void rewindcmd();
66 static void clearcmd();
70 static void fillcmd();
71 static void qfillcmd();
72 static void statcmd();
73 static void unfillcmd();
74 static int flush_block(DEV_BLOCK *block, int dump);
75 static bool quickie_cb(DCR *dcr, DEV_RECORD *rec);
76 static bool compare_blocks(DEV_BLOCK *last_block, DEV_BLOCK *block);
77 static bool my_mount_next_read_volume(DCR *dcr);
78 static void scan_blocks();
79 static void set_volume_name(const char *VolName, int volnum);
80 static void rawfill_cmd();
81 static void bfill_cmd();
82 static bool open_the_device();
83 static void autochangercmd();
84 static void do_unfill();
87 /* Static variables */
88 #define CONFIG_FILE "bacula-sd.conf"
91 #define MAX_CMD_ARGS 30
94 static char *argk[MAX_CMD_ARGS];
95 static char *argv[MAX_CMD_ARGS];
98 static int quickie_count = 0;
99 static BSR *bsr = NULL;
100 static int signals = TRUE;
103 static uint64_t vol_size;
104 static uint64_t VolBytes;
107 static int32_t file_index;
108 static int end_of_tape = 0;
109 static uint32_t LastBlock = 0;
110 static uint32_t eot_block;
111 static uint32_t eot_block_len;
112 static uint32_t eot_FileIndex;
113 static int dumped = 0;
114 static DEV_BLOCK *last_block1 = NULL;
115 static DEV_BLOCK *last_block2 = NULL;
116 static DEV_BLOCK *last_block = NULL;
117 static DEV_BLOCK *this_block = NULL;
118 static DEV_BLOCK *first_block = NULL;
119 static uint32_t last_file1 = 0;
120 static uint32_t last_file2 = 0;
121 static uint32_t last_file = 0;
122 static uint32_t last_block_num1 = 0;
123 static uint32_t last_block_num2 = 0;
124 static uint32_t last_block_num = 0;
125 static uint32_t BlockNumber = 0;
126 static bool simple = true;
128 static const char *VolumeName = NULL;
129 static int vol_num = 0;
131 static JCR *jcr = NULL;
135 static void terminate_btape(int sig);
136 int get_cmd(const char *prompt);
139 /*********************************************************************
141 * Main Bacula Pool Creation Program
144 int main(int margc, char *margv[])
152 if (TAPE_BSIZE % DEV_BSIZE != 0 || TAPE_BSIZE / DEV_BSIZE == 0) {
153 Emsg2(M_ABORT, 0, "Tape block size (%d) not multiple of system size (%d)\n",
154 TAPE_BSIZE, DEV_BSIZE);
156 if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
157 Emsg1(M_ABORT, 0, "Tape block size (%d) is not a power of 2\n", TAPE_BSIZE);
159 if (sizeof(off_t) < 8) {
160 Pmsg1(-1, "\n\n!!!! Warning large disk addressing disabled. off_t=%d should be 8 or more !!!!!\n\n\n",
164 bsnprintf(buf, sizeof(buf), "%u", x32);
165 i = bsscanf(buf, "%u", &y32);
166 if (i != 1 || x32 != y32) {
167 Pmsg3(-1, "32 bit printf/scanf problem. i=%d x32=%u y32=%u\n", i, x32, y32);
173 bsnprintf(buf, sizeof(buf), "%" llu, x64);
174 i = bsscanf(buf, "%llu", &y64);
175 if (i != 1 || x64 != y64) {
176 Pmsg3(-1, "64 bit printf/scanf problem. i=%d x64=%" llu " y64=%" llu "\n", i, x64, y64);
180 printf("Tape block granularity is %d bytes.\n", TAPE_BSIZE);
182 working_directory = "/tmp";
183 my_name_is(margc, margv, "btape");
184 init_msg(NULL, NULL);
186 while ((ch = getopt(margc, margv, "b:c:d:psv?")) != -1) {
188 case 'b': /* bootstrap file */
189 bsr = parse_bsr(NULL, optarg);
190 // dump_bsr(bsr, true);
193 case 'c': /* specify config file */
194 if (configfile != NULL) {
197 configfile = bstrdup(optarg);
200 case 'd': /* set debug level */
201 debug_level = atoi(optarg);
202 if (debug_level <= 0) {
229 cmd = get_pool_memory(PM_FNAME);
230 args = get_pool_memory(PM_FNAME);
233 init_signals(terminate_btape);
236 if (configfile == NULL) {
237 configfile = bstrdup(CONFIG_FILE);
240 daemon_start_time = time(NULL);
242 parse_config(configfile);
245 /* See if we can open a device */
247 Pmsg0(000, "No archive name specified.\n");
250 } else if (margc != 1) {
251 Pmsg0(000, "Improper number of arguments specified.\n");
256 jcr = setup_jcr("btape", margv[0], bsr, NULL, 0); /* write */
265 if (!open_the_device()) {
269 Dmsg0(200, "Do tape commands\n");
277 static void terminate_btape(int stat)
280 sm_check(__FILE__, __LINE__, false);
284 free_config_resources();
286 free_pool_memory(args);
290 free_pool_memory(cmd);
305 if (debug_level > 10)
306 print_memory_pool_stats();
309 free_block(this_block);
314 close_memory_pool(); /* free memory in pool */
320 static bool open_the_device()
324 block = new_block(dev);
326 if (!(dev->state & ST_OPENED)) {
327 Dmsg1(200, "Opening device %s\n", dcr->VolumeName);
328 if (open_dev(dev, dcr->VolumeName, OPEN_READ_WRITE) < 0) {
329 Emsg1(M_FATAL, 0, _("dev open failed: %s\n"), dev->errmsg);
335 Pmsg1(000, "open_dev %s OK\n", dev_name(dev));
336 dev->state |= ST_APPEND;
349 * Write a label to the tape
351 static void labelcmd()
354 pm_strcpy(cmd, VolumeName);
356 if (!get_cmd("Enter Volume Name: ")) {
361 if (!(dev->state & ST_OPENED)) {
362 if (!first_open_device(dev)) {
363 Pmsg1(0, "Device open failed. ERR=%s\n", strerror_dev(dev));
367 write_new_volume_label_to_dev(dcr, cmd, "Default");
368 Pmsg1(-1, "Wrote Volume label for volume \"%s\".\n", cmd);
372 * Read the tape label
374 static void readlabelcmd()
376 int save_debug_level = debug_level;
379 stat = read_dev_volume_label(dcr);
382 Pmsg0(0, "Volume has no label.\n");
385 Pmsg0(0, "Volume label read correctly.\n");
388 Pmsg1(0, "I/O error on device: ERR=%s", strerror_dev(dev));
391 Pmsg0(0, "Volume name error\n");
393 case VOL_CREATE_ERROR:
394 Pmsg1(0, "Error creating label. ERR=%s", strerror_dev(dev));
396 case VOL_VERSION_ERROR:
397 Pmsg0(0, "Volume version error.\n");
399 case VOL_LABEL_ERROR:
400 Pmsg0(0, "Bad Volume label type.\n");
403 Pmsg0(0, "Unknown error.\n");
408 dump_volume_label(dev);
409 debug_level = save_debug_level;
414 * Load the tape should have prevously been taken
415 * off line, otherwise this command is not necessary.
417 static void loadcmd()
420 if (!load_dev(dev)) {
421 Pmsg1(0, "Bad status from load. ERR=%s\n", strerror_dev(dev));
423 Pmsg1(0, "Loaded %s\n", dev_name(dev));
429 static void rewindcmd()
431 if (!rewind_dev(dev)) {
432 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
433 clrerror_dev(dev, -1);
435 Pmsg1(0, "Rewound %s\n", dev_name(dev));
440 * Clear any tape error
442 static void clearcmd()
444 clrerror_dev(dev, -1);
448 * Write and end of file on the tape
450 static void weofcmd()
461 if ((stat = weof_dev(dev, num)) < 0) {
462 Pmsg2(0, "Bad status from weof %d. ERR=%s\n", stat, strerror_dev(dev));
465 Pmsg3(0, "Wrote %d EOF%s to %s\n", num, num==1?"":"s", dev_name(dev));
470 /* Go to the end of the medium -- raw command
471 * The idea was orginally that the end of the Bacula
472 * medium would be flagged differently. This is not
473 * currently the case. So, this is identical to the
479 Pmsg1(0, "%s", strerror_dev(dev));
482 Pmsg0(0, _("Moved to end of medium.\n"));
487 * Go to the end of the medium (either hardware determined
488 * or defined by two eofs.
508 if (!bsf_dev(dev, num)) {
509 Pmsg1(0, _("Bad status from bsf. ERR=%s\n"), strerror_dev(dev));
511 Pmsg2(0, _("Backspaced %d file%s.\n"), num, num==1?"":"s");
527 if (!bsr_dev(dev, num)) {
528 Pmsg1(0, _("Bad status from bsr. ERR=%s\n"), strerror_dev(dev));
530 Pmsg2(0, _("Backspaced %d record%s.\n"), num, num==1?"":"s");
535 * List device capabilities as defined in the
540 printf(_("Configured device capabilities:\n"));
541 printf("%sEOF ", dev->capabilities & CAP_EOF ? "" : "!");
542 printf("%sBSR ", dev->capabilities & CAP_BSR ? "" : "!");
543 printf("%sBSF ", dev->capabilities & CAP_BSF ? "" : "!");
544 printf("%sFSR ", dev->capabilities & CAP_FSR ? "" : "!");
545 printf("%sFSF ", dev->capabilities & CAP_FSF ? "" : "!");
546 printf("%sFASTFSF ", dev->capabilities & CAP_FASTFSF ? "" : "!");
547 printf("%sEOM ", dev->capabilities & CAP_EOM ? "" : "!");
548 printf("%sREM ", dev->capabilities & CAP_REM ? "" : "!");
549 printf("%sRACCESS ", dev->capabilities & CAP_RACCESS ? "" : "!");
550 printf("%sAUTOMOUNT ", dev->capabilities & CAP_AUTOMOUNT ? "" : "!");
551 printf("%sLABEL ", dev->capabilities & CAP_LABEL ? "" : "!");
552 printf("%sANONVOLS ", dev->capabilities & CAP_ANONVOLS ? "" : "!");
553 printf("%sALWAYSOPEN ", dev->capabilities & CAP_ALWAYSOPEN ? "" : "!");
556 printf(_("Device status:\n"));
557 printf("%sOPENED ", dev->state & ST_OPENED ? "" : "!");
558 printf("%sTAPE ", dev->state & ST_TAPE ? "" : "!");
559 printf("%sLABEL ", dev->state & ST_LABEL ? "" : "!");
560 printf("%sMALLOC ", dev->state & ST_MALLOC ? "" : "!");
561 printf("%sAPPEND ", dev->state & ST_APPEND ? "" : "!");
562 printf("%sREAD ", dev->state & ST_READ ? "" : "!");
563 printf("%sEOT ", dev->state & ST_EOT ? "" : "!");
564 printf("%sWEOT ", dev->state & ST_WEOT ? "" : "!");
565 printf("%sEOF ", dev->state & ST_EOF ? "" : "!");
566 printf("%sNEXTVOL ", dev->state & ST_NEXTVOL ? "" : "!");
567 printf("%sSHORT ", dev->state & ST_SHORT ? "" : "!");
570 printf(_("Device parameters:\n"));
571 printf("Device name: %s\n", dev->dev_name);
572 printf("File=%u block=%u\n", dev->file, dev->block_num);
573 printf("Min block=%u Max block=%u\n", dev->min_block_size, dev->max_block_size);
581 * Test writting larger and larger records.
582 * This is a torture test for records.
584 static void rectestcmd()
590 Pmsg0(0, "Test writting larger and larger records.\n"
591 "This is a torture test for records.\nI am going to write\n"
592 "larger and larger records. It will stop when the record size\n"
593 "plus the header exceeds the block size (by default about 64K)\n");
596 get_cmd("Do you want to continue? (y/n): ");
598 Pmsg0(000, "Command aborted.\n");
602 sm_check(__FILE__, __LINE__, false);
603 block = new_block(dev);
606 for (i=1; i<500000; i++) {
607 rec->data = check_pool_memory_size(rec->data, i);
608 memset(rec->data, i & 0xFF, i);
610 sm_check(__FILE__, __LINE__, false);
611 if (write_record_to_block(block, rec)) {
614 Pmsg2(0, "Block %d i=%d\n", blkno, i);
618 sm_check(__FILE__, __LINE__, false);
622 sm_check(__FILE__, __LINE__, false);
626 * This test attempts to re-read a block written by Bacula
627 * normally at the end of the tape. Bacula will then back up
628 * over the two eof marks, backup over the record and reread
629 * it to make sure it is valid. Bacula can skip this validation
630 * if you set "Backward space record = no"
632 static int re_read_block_test()
634 DEV_BLOCK *block = dcr->block;
639 if (!(dev->capabilities & CAP_BSR)) {
640 Pmsg0(-1, _("Skipping read backwards test because BSR turned off.\n"));
644 Pmsg0(-1, _("\n=== Write, backup, and re-read test ===\n\n"
645 "I'm going to write three records and an EOF\n"
646 "then backup over the EOF and re-read the last record.\n"
647 "Bacula does this after writing the last block on the\n"
648 "tape to verify that the block was written correctly.\n\n"
649 "This is not an *essential* feature ...\n\n"));
653 rec->data = check_pool_memory_size(rec->data, block->buf_len);
654 len = rec->data_len = block->buf_len-100;
655 memset(rec->data, 1, rec->data_len);
656 if (!write_record_to_block(block, rec)) {
657 Pmsg0(0, _("Error writing record to block.\n"));
660 if (!write_block_to_dev(dcr)) {
661 Pmsg0(0, _("Error writing block to device.\n"));
664 Pmsg1(0, _("Wrote first record of %d bytes.\n"), rec->data_len);
666 memset(rec->data, 2, rec->data_len);
667 if (!write_record_to_block(block, rec)) {
668 Pmsg0(0, _("Error writing record to block.\n"));
671 if (!write_block_to_dev(dcr)) {
672 Pmsg0(0, _("Error writing block to device.\n"));
675 Pmsg1(0, _("Wrote second record of %d bytes.\n"), rec->data_len);
677 memset(rec->data, 3, rec->data_len);
678 if (!write_record_to_block(block, rec)) {
679 Pmsg0(0, _("Error writing record to block.\n"));
682 if (!write_block_to_dev(dcr)) {
683 Pmsg0(0, _("Error writing block to device.\n"));
686 Pmsg1(0, _("Wrote third record of %d bytes.\n"), rec->data_len);
689 if (dev_cap(dev, CAP_TWOEOF)) {
692 if (!bsf_dev(dev, 1)) {
693 Pmsg1(0, _("Backspace file failed! ERR=%s\n"), strerror_dev(dev));
696 if (dev_cap(dev, CAP_TWOEOF)) {
697 if (!bsf_dev(dev, 1)) {
698 Pmsg1(0, _("Backspace file failed! ERR=%s\n"), strerror_dev(dev));
702 Pmsg0(0, "Backspaced over EOF OK.\n");
703 if (!bsr_dev(dev, 1)) {
704 Pmsg1(0, _("Backspace record failed! ERR=%s\n"), strerror_dev(dev));
707 Pmsg0(0, "Backspace record OK.\n");
708 if (!read_block_from_dev(dcr, NO_BLOCK_NUMBER_CHECK)) {
709 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
712 memset(rec->data, 0, rec->data_len);
713 if (!read_record_from_block(block, rec)) {
714 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
717 for (int i=0; i<len; i++) {
718 if (rec->data[i] != 3) {
719 Pmsg0(0, _("Bad data in record. Test failed!\n"));
723 Pmsg0(0, _("\nBlock re-read correct. Test succeeded!\n"));
724 Pmsg0(-1, _("=== End Write, backup, and re-read test ===\n\n"));
731 Pmsg0(0, _("This is not terribly serious since Bacula only uses\n"
732 "this function to verify the last block written to the\n"
733 "tape. Bacula will skip the last block verification\n"
735 "Backward Space Record = No\n\n"
736 "to your Storage daemon's Device resource definition.\n"));
743 * This test writes Bacula blocks to the tape in
744 * several files. It then rewinds the tape and attepts
745 * to read these blocks back checking the data.
747 static int write_read_test()
755 Pmsg0(-1, _("\n=== Write, rewind, and re-read test ===\n\n"
756 "I'm going to write 1000 records and an EOF\n"
757 "then write 1000 records and an EOF, then rewind,\n"
758 "and re-read the data to verify that it is correct.\n\n"
759 "This is an *essential* feature ...\n\n"));
762 if (!rewind_dev(dev)) {
763 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
766 rec->data = check_pool_memory_size(rec->data, block->buf_len);
767 rec->data_len = block->buf_len-100;
768 len = rec->data_len/sizeof(i);
769 for (i=1; i<=1000; i++) {
770 p = (int *)rec->data;
771 for (j=0; j<len; j++) {
774 if (!write_record_to_block(block, rec)) {
775 Pmsg0(0, _("Error writing record to block.\n"));
778 if (!write_block_to_dev(dcr)) {
779 Pmsg0(0, _("Error writing block to device.\n"));
783 Pmsg1(0, _("Wrote 1000 blocks of %d bytes.\n"), rec->data_len);
785 for (i=1001; i<=2000; i++) {
786 p = (int *)rec->data;
787 for (j=0; j<len; j++) {
790 if (!write_record_to_block(block, rec)) {
791 Pmsg0(0, _("Error writing record to block.\n"));
794 if (!write_block_to_dev(dcr)) {
795 Pmsg0(0, _("Error writing block to device.\n"));
799 Pmsg1(0, _("Wrote 1000 blocks of %d bytes.\n"), rec->data_len);
801 if (dev_cap(dev, CAP_TWOEOF)) {
804 if (!rewind_dev(dev)) {
805 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
808 Pmsg0(0, "Rewind OK.\n");
810 for (i=1; i<=2000; i++) {
812 if (!read_block_from_dev(dcr, NO_BLOCK_NUMBER_CHECK)) {
813 if (dev_state(dev, ST_EOF)) {
814 Pmsg0(-1, _("Got EOF on tape.\n"));
817 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
820 memset(rec->data, 0, rec->data_len);
821 if (!read_record_from_block(block, rec)) {
822 Pmsg1(0, _("Read record failed! ERR=%s\n"), strerror(dev->dev_errno));
825 p = (int *)rec->data;
826 for (j=0; j<len; j++) {
828 Pmsg3(0, _("Bad data in record. Expected %d, got %d at byte %d. Test failed!\n"),
834 if (i == 1000 || i == 2000) {
835 Pmsg0(-1, _("1000 blocks re-read correctly.\n"));
838 Pmsg0(-1, _("=== Test Succeeded. End Write, rewind, and re-read test ===\n\n"));
847 * This test writes Bacula blocks to the tape in
848 * several files. It then rewinds the tape and attepts
849 * to read these blocks back checking the data.
851 static int position_test()
853 DEV_BLOCK *block = dcr->block;
859 int file = 0, blk = 0;
862 Pmsg0(-1, _("\n=== Write, rewind, and position test ===\n\n"
863 "I'm going to write 1000 records and an EOF\n"
864 "then write 1000 records and an EOF, then rewind,\n"
865 "and position to a few blocks and verify that it is correct.\n\n"
866 "This is an *essential* feature ...\n\n"));
869 if (!rewind_dev(dev)) {
870 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
873 rec->data = check_pool_memory_size(rec->data, block->buf_len);
874 rec->data_len = block->buf_len-100;
875 len = rec->data_len/sizeof(i);
876 for (i=1; i<=1000; i++) {
877 p = (int *)rec->data;
878 for (j=0; j<len; j++) {
881 if (!write_record_to_block(block, rec)) {
882 Pmsg0(0, _("Error writing record to block.\n"));
885 if (!write_block_to_dev(dcr)) {
886 Pmsg0(0, _("Error writing block to device.\n"));
890 Pmsg1(0, _("Wrote 1000 blocks of %d bytes.\n"), rec->data_len);
892 for (i=1001; i<=2000; i++) {
893 p = (int *)rec->data;
894 for (j=0; j<len; j++) {
897 if (!write_record_to_block(block, rec)) {
898 Pmsg0(0, _("Error writing record to block.\n"));
901 if (!write_block_to_dev(dcr)) {
902 Pmsg0(0, _("Error writing block to device.\n"));
906 Pmsg1(0, _("Wrote 1000 blocks of %d bytes.\n"), rec->data_len);
908 if (dev_cap(dev, CAP_TWOEOF)) {
911 if (!rewind_dev(dev)) {
912 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
915 Pmsg0(0, "Rewind OK.\n");
919 /* Set up next item to read based on where we are */
955 Pmsg2(-1, "Reposition to file:block %d:%d\n", file, blk);
956 if (!reposition_dev(dev, file, blk)) {
957 Pmsg0(0, "Reposition error.\n");
961 if (!read_block_from_dev(dcr, NO_BLOCK_NUMBER_CHECK)) {
962 if (dev_state(dev, ST_EOF)) {
963 Pmsg0(-1, _("Got EOF on tape.\n"));
966 Pmsg1(0, _("Read block failed! ERR=%s\n\n"), strerror(dev->dev_errno));
967 Pmsg0(0, _("This may be because the tape drive block size is not\n"
968 " set to variable blocking as normally used by Bacula.\n"
969 " Please see the Tape Testing chapter in the manual and \n"
970 " look for using mt with defblksize and setoptions\n"));
973 memset(rec->data, 0, rec->data_len);
974 if (!read_record_from_block(block, rec)) {
975 Pmsg1(0, _("Read record failed! ERR=%s\n"), strerror(dev->dev_errno));
978 p = (int *)rec->data;
979 for (j=0; j<len; j++) {
981 Pmsg3(0, _("Bad data in record. Expected %d, got %d at byte %d. Test failed!\n"),
986 Pmsg0(-1, _("Block re-read correctly.\n"));
988 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 dcr->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);
1108 Pmsg1(-1, _("3991 Bad autochanger command: %s\n"), changer);
1109 Pmsg2(-1, _("3991 result=\"%s\": ERR=%s\n"), results, be.strerror(status));
1113 Pmsg1(-1, "Slot %d loaded. I am going to unload it.\n", loaded);
1115 Pmsg0(-1, "Nothing loaded in the drive. OK.\n");
1117 Dmsg1(100, "Results from loaded query=%s\n", results);
1119 dcr->VolCatInfo.Slot = loaded;
1120 offline_or_rewind_dev(dev);
1121 /* We are going to load a new tape, so close the device */
1122 force_close_dev(dev);
1123 Pmsg2(-1, _("3302 Issuing autochanger \"unload %d %d\" command.\n"),
1124 loaded, dev->drive_index);
1125 changer = edit_device_codes(jcr, changer,
1126 jcr->device->changer_command, "unload");
1127 status = run_program(changer, timeout, results);
1128 Pmsg2(-1, "unload status=%s %d\n", status==0?"OK":"Bad", status);
1131 Pmsg1(-1, _("3992 Bad autochanger command: %s\n"), changer);
1132 Pmsg2(-1, _("3992 result=\"%s\": ERR=%s\n"), results, be.strerror(status));
1141 dcr->VolCatInfo.Slot = slot;
1142 Pmsg2(-1, _("3303 Issuing autochanger \"load slot %d %d\" command.\n"),
1143 slot, dev->drive_index);
1144 changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "load");
1145 Dmsg1(100, "Changer=%s\n", changer);
1146 force_close_dev(dev);
1147 status = run_program(changer, timeout, results);
1149 Pmsg2(-1, _("3303 Autochanger \"load slot %d %d\" status is OK.\n"),
1150 slot, dev->drive_index);
1153 Pmsg1(-1, _("3993 Bad autochanger command: %s\n"), changer);
1154 Pmsg2(-1, _("3993 result=\"%s\": ERR=%s\n"), results, be.strerror(status));
1158 if (!open_the_device()) {
1162 * Start with sleep_time 0 then increment by 30 seconds if we get
1165 bmicrosleep(sleep_time, 0);
1166 if (!rewind_dev(dev) || weof_dev(dev,1) < 0) {
1167 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
1168 clrerror_dev(dev, -1);
1169 Pmsg0(-1, "\nThe test failed, probably because you need to put\n"
1170 "a longer sleep time in the mtx-script in the load) case.\n"
1171 "Adding a 30 second sleep and trying again ...\n");
1175 Pmsg1(0, "Rewound %s\n", dev_name(dev));
1178 if ((status = weof_dev(dev, 1)) < 0) {
1179 Pmsg2(0, "Bad status from weof %d. ERR=%s\n", status, strerror_dev(dev));
1182 Pmsg1(0, "Wrote EOF to %s\n", dev_name(dev));
1186 Pmsg1(-1, "\nThe test worked this time. Please add:\n\n"
1188 "to your mtx-changer script in the load) case.\n\n",
1191 Pmsg0(-1, "\nThe test autochanger worked!!\n\n");
1194 free_pool_memory(changer);
1195 free_pool_memory(results);
1200 free_pool_memory(changer);
1201 free_pool_memory(results);
1202 Pmsg0(-1, "You must correct this error or the Autochanger will not work.\n");
1206 static void autochangercmd()
1213 * This test assumes that the append test has been done,
1214 * then it tests the fsf function.
1216 static int fsf_test()
1218 bool set_off = false;
1220 Pmsg0(-1, _("\n\n=== Forward space files test ===\n\n"
1221 "This test is essential to Bacula.\n\n"
1222 "I'm going to write five files then test forward spacing\n\n"));
1226 weofcmd(); /* end file 0 */
1229 weofcmd(); /* end file 1 */
1233 weofcmd(); /* end file 2 */
1236 weofcmd(); /* end file 3 */
1238 weofcmd(); /* end file 4 */
1239 if (dev_cap(dev, CAP_TWOEOF)) {
1245 Pmsg0(0, _("Now forward spacing 1 file.\n"));
1246 if (!fsf_dev(dev, 1)) {
1247 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1250 Pmsg2(-1, _("We should be in file 1. I am at file %d. This is %s\n"),
1251 dev->file, dev->file == 1 ? "correct!" : "NOT correct!!!!");
1253 if (dev->file != 1) {
1257 Pmsg0(0, _("Now forward spacing 2 files.\n"));
1258 if (!fsf_dev(dev, 2)) {
1259 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1262 Pmsg2(-1, _("We should be in file 3. I am at file %d. This is %s\n"),
1263 dev->file, dev->file == 3 ? "correct!" : "NOT correct!!!!");
1265 if (dev->file != 3) {
1270 Pmsg0(0, _("Now forward spacing 4 files.\n"));
1271 if (!fsf_dev(dev, 4)) {
1272 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1275 Pmsg2(-1, _("We should be in file 4. I am at file %d. This is %s\n"),
1276 dev->file, dev->file == 4 ? "correct!" : "NOT correct!!!!");
1278 if (dev->file != 4) {
1282 Pmsg0(-1, "The test worked this time. Please add:\n\n"
1283 " Fast Forward Space File = no\n\n"
1284 "to your Device resource for this drive.\n");
1288 Pmsg0(0, _("Now forward spacing 1 more file.\n"));
1289 if (!fsf_dev(dev, 1)) {
1290 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1292 Pmsg2(-1, _("We should be in file 5. I am at file %d. This is %s\n"),
1293 dev->file, dev->file == 5 ? "correct!" : "NOT correct!!!!");
1294 if (dev->file != 5) {
1297 Pmsg0(-1, _("\n=== End Forward space files test ===\n\n"));
1301 Pmsg0(-1, _("\nThe forward space file test failed.\n"));
1302 if (dev_cap(dev, CAP_FASTFSF)) {
1303 Pmsg0(-1, "You have Fast Forward Space File enabled.\n"
1304 "I am turning it off then retrying the test.\n");
1305 dev->capabilities &= ~CAP_FASTFSF;
1309 Pmsg0(-1, "You must correct this error or Bacula will not work.\n");
1318 * This is a general test of Bacula's functions
1319 * needed to read and write the tape.
1321 static void testcmd()
1325 if (!write_read_test()) {
1328 if (!position_test()) {
1332 stat = append_test();
1333 if (stat == 1) { /* OK get out */
1336 if (stat == -1) { /* first test failed */
1337 if (dev_cap(dev, CAP_EOM) || dev_cap(dev, CAP_FASTFSF)) {
1338 Pmsg0(-1, "\nAppend test failed. Attempting again.\n"
1339 "Setting \"Hardware End of Medium = no\n"
1340 " and \"Fast Forward Space File = no\n"
1341 "and retrying append test.\n\n");
1342 dev->capabilities &= ~CAP_EOM; /* turn off eom */
1343 dev->capabilities &= ~CAP_FASTFSF; /* turn off fast fsf */
1344 stat = append_test();
1346 Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n"
1347 " Hardware End of Medium = No\n\n"
1348 " Fast Forward Space File = No\n"
1349 "to your Device resource in the Storage conf file.\n");
1353 Pmsg0(-1, "\n\nThat appears *NOT* to have corrected the problem.\n");
1356 /* Wrong count after append */
1358 Pmsg0(-1, "\n\nIt looks like the append failed. Attempting again.\n"
1359 "Setting \"BSF at EOM = yes\" and retrying append test.\n");
1360 dev->capabilities |= CAP_BSFATEOM; /* backspace on eom */
1361 stat = append_test();
1363 Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n"
1364 " Hardware End of Medium = No\n"
1365 " Fast Forward Space File = No\n"
1366 " BSF at EOM = yes\n\n"
1367 "to your Device resource in the Storage conf file.\n");
1374 Pmsg0(-1, "\nAppend test failed.\n\n");
1375 Pmsg0(-1, "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
1376 "Unable to correct the problem. You MUST fix this\n"
1377 "problem before Bacula can use your tape drive correctly\n");
1378 Pmsg0(-1, "\nPerhaps running Bacula in fixed block mode will work.\n"
1379 "Do so by setting:\n\n"
1380 "Minimum Block Size = nnn\n"
1381 "Maximum Block Size = nnn\n\n"
1382 "in your Storage daemon's Device definition.\n"
1383 "nnn must match your tape driver's block size, which\n"
1384 "can be determined by reading your tape manufacturers\n"
1385 "information, and the information on your kernel dirver.\n"
1386 "Fixed block sizes, however, are not normally an ideal solution.\n");
1391 Pmsg0(-1, _("\nThe above Bacula scan should have output identical to what follows.\n"
1392 "Please double check it ...\n"
1393 "=== Sample correct output ===\n"
1394 "1 block of 64448 bytes in file 1\n"
1395 "End of File mark.\n"
1396 "2 blocks of 64448 bytes in file 2\n"
1397 "End of File mark.\n"
1398 "3 blocks of 64448 bytes in file 3\n"
1399 "End of File mark.\n"
1400 "1 block of 64448 bytes in file 4\n"
1401 "End of File mark.\n"
1402 "Total files=4, blocks=7, bytes = 451,136\n"
1403 "=== End sample correct output ===\n\n"));
1405 Pmsg0(-1, _("If the above scan output is not identical to the\n"
1406 "sample output, you MUST correct the problem\n"
1407 "or Bacula will not be able to write multiple Jobs to \n"
1411 re_read_block_test();
1414 fsf_test(); /* do fast forward space file test */
1416 autochanger_test(); /* do autochanger test */
1420 /* Forward space a file */
1421 static void fsfcmd()
1425 num = atoi(argk[1]);
1430 if (!fsf_dev(dev, num)) {
1431 Pmsg1(0, "Bad status from fsf. ERR=%s\n", strerror_dev(dev));
1434 Pmsg2(0, "Forward spaced %d file%s.\n", num, num==1?"":"s");
1437 /* Forward space a record */
1438 static void fsrcmd()
1442 num = atoi(argk[1]);
1447 if (!fsr_dev(dev, num)) {
1448 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1451 Pmsg2(0, "Forward spaced %d record%s.\n", num, num==1?"":"s");
1456 * Write a Bacula block to the tape
1460 DEV_BLOCK *block = dcr->block;
1461 DEV_RECORD *rec = dcr->rec;
1464 sm_check(__FILE__, __LINE__, false);
1467 dump_block(block, "test");
1470 i = block->buf_len - 100;
1472 rec->data = check_pool_memory_size(rec->data, i);
1473 memset(rec->data, i & 0xFF, i);
1475 sm_check(__FILE__, __LINE__, false);
1476 if (!write_record_to_block(block, rec)) {
1477 Pmsg0(0, _("Error writing record to block.\n"));
1480 if (!write_block_to_dev(dcr)) {
1481 Pmsg0(0, _("Error writing block to device.\n"));
1484 Pmsg1(0, _("Wrote one record of %d bytes.\n"), i);
1486 Pmsg0(0, _("Wrote block to device.\n"));
1489 sm_check(__FILE__, __LINE__, false);
1490 sm_check(__FILE__, __LINE__, false);
1494 * Read a record from the tape
1501 if (!get_cmd("Enter length to read: ")) {
1505 if (len < 0 || len > 1000000) {
1506 Pmsg0(0, _("Bad length entered, using default of 1024 bytes.\n"));
1509 buf = (char *)malloc(len);
1510 stat = read(dev->fd, buf, len);
1511 if (stat > 0 && stat <= len) {
1515 Pmsg3(0, _("Read of %d bytes gives stat=%d. ERR=%s\n"),
1516 len, stat, be.strerror());
1522 * Scan tape by reading block by block. Report what is
1523 * on the tape. Note, this command does raw reads, and as such
1524 * will not work with fixed block size devices.
1526 static void scancmd()
1529 int blocks, tot_blocks, tot_files;
1535 blocks = block_size = tot_blocks = 0;
1537 if (dev->state & ST_EOT) {
1538 Pmsg0(0, "End of tape\n");
1541 update_pos_dev(dev);
1542 tot_files = dev->file;
1543 Pmsg1(0, _("Starting scan at file %u\n"), dev->file);
1545 if ((stat = read(dev->fd, buf, sizeof(buf))) < 0) {
1547 clrerror_dev(dev, -1);
1548 Mmsg2(dev->errmsg, "read error on %s. ERR=%s.\n",
1549 dev->dev_name, be.strerror());
1550 Pmsg2(0, "Bad status from read %d. ERR=%s\n", stat, strerror_dev(dev));
1552 printf("%d block%s of %d bytes in file %d\n",
1553 blocks, blocks>1?"s":"", block_size, dev->file);
1556 Dmsg1(200, "read status = %d\n", stat);
1558 if (stat != block_size) {
1559 update_pos_dev(dev);
1561 printf("%d block%s of %d bytes in file %d\n",
1562 blocks, blocks>1?"s":"", block_size, dev->file);
1567 if (stat == 0) { /* EOF */
1568 update_pos_dev(dev);
1569 printf("End of File mark.\n");
1570 /* Two reads of zero means end of tape */
1571 if (dev->state & ST_EOF)
1572 dev->state |= ST_EOT;
1574 dev->state |= ST_EOF;
1577 if (dev->state & ST_EOT) {
1578 printf("End of tape\n");
1581 } else { /* Got data */
1582 dev->state &= ~ST_EOF;
1588 update_pos_dev(dev);
1589 tot_files = dev->file - tot_files;
1590 printf("Total files=%d, blocks=%d, bytes = %s\n", tot_files, tot_blocks,
1591 edit_uint64_with_commas(bytes, ec1));
1596 * Scan tape by reading Bacula block by block. Report what is
1597 * on the tape. This function reads Bacula blocks, so if your
1598 * Device resource is correctly defined, it should work with
1599 * either variable or fixed block sizes.
1601 static void scan_blocks()
1603 int blocks, tot_blocks, tot_files;
1604 uint32_t block_size;
1606 DEV_BLOCK *block = dcr->block;
1609 blocks = block_size = tot_blocks = 0;
1613 update_pos_dev(dev);
1614 tot_files = dev->file;
1616 if (!read_block_from_device(dcr, NO_BLOCK_NUMBER_CHECK)) {
1617 Dmsg1(100, "!read_block(): ERR=%s\n", strerror_dev(dev));
1618 if (dev->state & ST_EOT) {
1620 printf("%d block%s of %d bytes in file %d\n",
1621 blocks, blocks>1?"s":"", block_size, dev->file);
1626 if (dev->state & ST_EOF) {
1628 printf("%d block%s of %d bytes in file %d\n",
1629 blocks, blocks>1?"s":"", block_size, dev->file);
1632 printf(_("End of File mark.\n"));
1635 if (dev->state & ST_SHORT) {
1637 printf("%d block%s of %d bytes in file %d\n",
1638 blocks, blocks>1?"s":"", block_size, dev->file);
1641 printf(_("Short block read.\n"));
1644 printf(_("Error reading block. ERR=%s\n"), strerror_dev(dev));
1647 if (block->block_len != block_size) {
1649 printf("%d block%s of %d bytes in file %d\n",
1650 blocks, blocks>1?"s":"", block_size, dev->file);
1653 block_size = block->block_len;
1657 bytes += block->block_len;
1658 Dmsg6(100, "Blk_blk=%u dev_blk=%u blen=%u bVer=%d SessId=%u SessTim=%u\n",
1659 block->BlockNumber, dev->block_num, block->block_len, block->BlockVer,
1660 block->VolSessionId, block->VolSessionTime);
1662 DEV_RECORD *rec = new_record();
1663 read_record_from_block(block, rec);
1664 Pmsg8(-1, "Blk_block: %u dev_blk=%u blen=%u First rec FI=%s SessId=%u SessTim=%u Strm=%s rlen=%d\n",
1665 block->BlockNumber, dev->block_num, block->block_len,
1666 FI_to_ascii(rec->FileIndex), rec->VolSessionId, rec->VolSessionTime,
1667 stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len);
1670 } else if (verbose > 1) {
1671 dump_block(block, "");
1676 tot_files = dev->file - tot_files;
1677 printf("Total files=%d, blocks=%d, bytes = %s\n", tot_files, tot_blocks,
1678 edit_uint64_with_commas(bytes, ec1));
1682 static void statcmd()
1684 int debug = debug_level;
1686 Pmsg2(0, "Device status: %u. ERR=%s\n", status_dev(dev), strerror_dev(dev));
1688 dump_volume_label(dev);
1690 debug_level = debug;
1695 * First we label the tape, then we fill
1696 * it with data get a new tape and write a few blocks.
1698 static void fillcmd()
1701 DEV_BLOCK *block = dcr->block;
1705 uint32_t min_block_size;
1715 This command simulates Bacula writing to a tape.\n\
1716 It requires either one or two blank tapes, which it\n\
1717 will label and write.\n\n\
1718 If you have an autochanger configured, it will use\n\
1719 the tapes that are in slots 1 and 2, otherwise, you will\n\
1720 be prompted to insert the tapes when necessary.\n\n\
1721 It will print a status approximately\n\
1722 every 322 MB, and write an EOF every 3.2 GB. If you have\n\
1723 selected the simple test option, after writing the first tape\n\
1724 it will rewind it and re-read the last block written.\n\n\
1725 If you have selected the multiple tape test, when the first tape\n\
1726 fills, it will ask for a second, and after writing a few more \n\
1727 blocks, it will stop. Then it will begin re-reading the\n\
1729 This may take a long time -- hours! ...\n\n");
1731 get_cmd("Do you want to run the simplified test (s) with one tape\n"
1732 "or the complete multiple tape (m) test: (s/m) ");
1733 if (cmd[0] == 's') {
1734 Pmsg0(-1, "Simple test (single tape) selected.\n");
1736 } else if (cmd[0] == 'm') {
1737 Pmsg0(-1, "Multiple tape test selected.\n");
1740 Pmsg0(000, "Command aborted.\n");
1744 Dmsg1(20, "Begin append device=%s\n", dev_name(dev));
1745 Dmsg1(20, "MaxVolSize=%s\n", edit_uint64(dev->max_volume_size, ec1));
1747 /* Use fixed block size to simplify read back */
1748 min_block_size = dev->min_block_size;
1749 dev->min_block_size = dev->max_block_size;
1750 set_volume_name("TestVolume1", 1);
1754 dev->state &= ~ST_APPEND; /* force volume to be relabeled */
1757 * Acquire output device for writing. Note, after acquiring a
1758 * device, we MUST release it, which is done at the end of this
1761 Dmsg0(100, "just before acquire_device\n");
1762 if (!acquire_device_for_append(jcr)) {
1763 set_jcr_job_status(jcr, JS_ErrorTerminated);
1766 block = jcr->dcr->block;
1768 Dmsg0(100, "Just after acquire_device_for_append\n");
1770 * Write Begin Session Record
1772 if (!write_session_label(dcr, SOS_LABEL)) {
1773 set_jcr_job_status(jcr, JS_ErrorTerminated);
1774 Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"),
1778 Pmsg0(-1, "Wrote Start of Session label.\n");
1780 memset(&rec, 0, sizeof(rec));
1781 rec.data = get_memory(100000); /* max record size */
1783 #define REC_SIZE 32768
1784 rec.data_len = REC_SIZE;
1787 * Put some random data in the record
1789 fd = open("/dev/urandom", O_RDONLY);
1791 read(fd, rec.data, rec.data_len);
1794 uint32_t *p = (uint32_t *)rec.data;
1795 srandom(time(NULL));
1796 for (i=0; i<rec.data_len/sizeof(uint32_t); i++) {
1802 * Generate data as if from File daemon, write to device
1804 jcr->dcr->VolFirstIndex = 0;
1805 time(&jcr->run_time); /* start counting time for rates */
1807 Pmsg0(-1, "Begin writing Bacula records to tape ...\n");
1809 Pmsg0(-1, "Begin writing Bacula records to first tape ...\n");
1811 for (file_index = 0; ok && !job_canceled(jcr); ) {
1812 rec.VolSessionId = jcr->VolSessionId;
1813 rec.VolSessionTime = jcr->VolSessionTime;
1814 rec.FileIndex = ++file_index;
1815 rec.Stream = STREAM_FILE_DATA;
1817 /* Mix up the data just a bit */
1818 uint32_t *lp = (uint32_t *)rec.data;
1820 for (i=1; i < (rec.data_len-sizeof(uint32_t))/sizeof(uint32_t)-1; i++) {
1824 Dmsg4(250, "before write_rec FI=%d SessId=%d Strm=%s len=%d\n",
1825 rec.FileIndex, rec.VolSessionId, stream_to_ascii(rec.Stream, rec.FileIndex),
1828 while (!write_record_to_block(block, &rec)) {
1830 * When we get here we have just filled a block
1832 Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec.data_len,
1835 /* Write block to tape */
1836 if (!flush_block(block, 1)) {
1840 /* Every 5000 blocks (approx 322MB) report where we are.
1842 if ((block->BlockNumber % 5000) == 0) {
1844 now -= jcr->run_time;
1846 now = 1; /* prevent divide error */
1848 kbs = (double)dev->VolCatInfo.VolCatBytes / (1000.0 * (double)now);
1849 Pmsg4(-1, "Wrote blk_block=%u, dev_blk_num=%u VolBytes=%s rate=%.1f KB/s\n",
1850 block->BlockNumber, dev->block_num,
1851 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), (float)kbs);
1853 /* Every 15000 blocks (approx 1GB) write an EOF.
1855 if ((block->BlockNumber % 15000) == 0) {
1856 Pmsg0(-1, "Flush block, write EOF\n");
1857 flush_block(block, 0);
1861 /* Get out after writing 10 blocks to the second tape */
1862 if (++BlockNumber > 10 && stop != 0) { /* get out */
1867 Pmsg0(000, _("Not OK\n"));
1870 jcr->JobBytes += rec.data_len; /* increment bytes this job */
1871 Dmsg4(190, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
1872 FI_to_ascii(rec.FileIndex), rec.VolSessionId,
1873 stream_to_ascii(rec.Stream, rec.FileIndex), rec.data_len);
1875 /* Get out after writing 10 blocks to the second tape */
1876 if (BlockNumber > 10 && stop != 0) { /* get out */
1877 Pmsg0(-1, "Done writing ...\n");
1882 Dmsg0(100, "Write_end_session_label()\n");
1883 /* Create Job status for end of session label */
1884 if (!job_canceled(jcr) && ok) {
1885 set_jcr_job_status(jcr, JS_Terminated);
1887 set_jcr_job_status(jcr, JS_ErrorTerminated);
1889 if (!write_session_label(dcr, EOS_LABEL)) {
1890 Pmsg1(000, _("Error writting end session label. ERR=%s\n"), strerror_dev(dev));
1893 /* Write out final block of this session */
1894 if (!write_block_to_device(dcr)) {
1895 Pmsg0(-1, _("Set ok=false after write_block_to_device.\n"));
1898 Pmsg0(-1, _("Wrote End of Session label.\n"));
1900 /* Save last block info for second tape */
1901 last_block_num2 = last_block_num;
1902 last_file2 = last_file;
1904 free_block(last_block2);
1906 last_block2 = dup_block(last_block);
1909 sprintf(buf, "%s/btape.state", working_directory);
1910 fd = open(buf, O_CREAT|O_TRUNC|O_WRONLY, 0640);
1912 write(fd, &btape_state_level, sizeof(btape_state_level));
1913 write(fd, &simple, sizeof(simple));
1914 write(fd, &last_block_num1, sizeof(last_block_num1));
1915 write(fd, &last_block_num2, sizeof(last_block_num2));
1916 write(fd, &last_file1, sizeof(last_file1));
1917 write(fd, &last_file2, sizeof(last_file2));
1918 write(fd, last_block1->buf, last_block1->buf_len);
1919 write(fd, last_block2->buf, last_block2->buf_len);
1920 write(fd, first_block->buf, first_block->buf_len);
1922 Pmsg2(-1, "Wrote state file last_block_num1=%d last_block_num2=%d\n",
1923 last_block_num1, last_block_num2);
1926 Pmsg2(-1, _("Could not create state file: %s ERR=%s\n"), buf,
1930 Pmsg4(-1, _("\n\nDone filling tape%s at %d:%d. Now beginning re-read of %stape ...\n"),
1931 simple?"":"s", jcr->dcr->dev->file, jcr->dcr->dev->block_num, simple?"":"first ");
1933 jcr->dcr->block = block;
1934 /* Release the device if multiple tapes being used */
1935 // if (!simple && !release_device(jcr)) {
1936 // Pmsg0(-1, _("Error in release_device\n"));
1942 dev->min_block_size = min_block_size;
1943 free_memory(rec.data);
1947 * Read two tapes written by the "fill" command and ensure
1948 * that the data is valid. If stop==1 we simulate full read back
1949 * of two tapes. If stop==-1 we simply read the last block and
1950 * verify that it is correct.
1952 static void unfillcmd()
1956 last_block1 = new_block(dev);
1957 last_block2 = new_block(dev);
1958 first_block = new_block(dev);
1959 sprintf(buf, "%s/btape.state", working_directory);
1960 fd = open(buf, O_RDONLY);
1962 uint32_t state_level;
1963 read(fd, &state_level, sizeof(btape_state_level));
1964 read(fd, &simple, sizeof(simple));
1965 read(fd, &last_block_num1, sizeof(last_block_num1));
1966 read(fd, &last_block_num2, sizeof(last_block_num2));
1967 read(fd, &last_file1, sizeof(last_file1));
1968 read(fd, &last_file2, sizeof(last_file2));
1969 read(fd, last_block1->buf, last_block1->buf_len);
1970 read(fd, last_block2->buf, last_block2->buf_len);
1971 read(fd, first_block->buf, first_block->buf_len);
1973 if (state_level != btape_state_level) {
1974 Pmsg0(-1, "\nThe state file level has changed. You must redo\n"
1975 "the fill command.\n");
1980 Pmsg2(-1, "\nCould not find the state file: %s ERR=%s\n"
1981 "You must redo the fill command.\n", buf, be.strerror());
1988 static void do_unfill()
1990 DEV_BLOCK *block = dcr->block;
1997 Dmsg0(20, "Enter do_unfill\n");
1998 dev->capabilities |= CAP_ANONVOLS; /* allow reading any volume */
1999 dev->capabilities &= ~CAP_LABEL; /* don't label anything here */
2003 time(&jcr->run_time); /* start counting time for rates */
2007 free_block(last_block);
2009 last_block_num = last_block_num1;
2010 last_file = last_file1;
2011 last_block = last_block1;
2014 /* Multiple Volume tape */
2015 /* Close device so user can use autochanger if desired */
2016 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2019 autochanger = autoload_device(dcr, 1, NULL);
2021 force_close_dev(dev);
2022 get_cmd(_("Mount first tape. Press enter when ready: "));
2025 jcr->dcr = new_dcr(jcr, dev);
2026 set_volume_name("TestVolume1", 1);
2028 create_vol_list(jcr);
2030 dev->state &= ~(ST_READ|ST_APPEND);
2031 dev->num_writers = 0;
2032 if (!acquire_device_for_read(jcr)) {
2033 Pmsg1(-1, "%s", dev->errmsg);
2038 * We now have the first tape mounted.
2039 * Note, re-reading last block may have caused us to
2040 * loose track of where we are (block number unknown).
2042 if (!rewind_dev(dev)) { /* get to a known place on tape */
2045 /* Read the first 1000 records */
2046 Pmsg0(-1, _("Reading the first 1000 records.\n"));
2048 read_records(dcr, quickie_cb, my_mount_next_read_volume);
2049 Pmsg4(-1, _("Reposition from %u:%u to %u:%u\n"), dev->file, dev->block_num,
2050 last_file, last_block_num);
2051 if (!reposition_dev(dev, last_file, last_block_num)) {
2052 Pmsg1(-1, "Reposition error. ERR=%s\n", strerror_dev(dev));
2055 Pmsg1(-1, _("Reading block %u.\n"), last_block_num);
2056 if (!read_block_from_device(dcr, NO_BLOCK_NUMBER_CHECK)) {
2057 Pmsg1(-1, _("Error reading block: ERR=%s\n"), strerror_dev(dev));
2060 if (compare_blocks(last_block, block)) {
2062 Pmsg0(-1, _("\nThe last block on the tape matches. Test succeeded.\n\n"));
2064 Pmsg0(-1, _("\nThe last block of the first tape matches.\n\n"));
2071 /* restore info for last block on second Volume */
2072 last_block_num = last_block_num2;
2073 last_file = last_file2;
2074 last_block = last_block2;
2076 /* Multiple Volume tape */
2077 /* Close device so user can use autochanger if desired */
2078 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2083 set_volume_name("TestVolume2", 2);
2085 create_vol_list(jcr);
2086 autochanger = autoload_device(dcr, 1, NULL);
2088 force_close_dev(dev);
2089 get_cmd(_("Mount second tape. Press enter when ready: "));
2092 dev->state &= ~ST_READ;
2093 if (!acquire_device_for_read(jcr)) {
2094 Pmsg1(-1, "%s", dev->errmsg);
2098 /* Space to "first" block which is last block not written
2099 * on the previous tape.
2101 Pmsg2(-1, _("Reposition from %u:%u to 0:1\n"), dev->file, dev->block_num);
2102 if (!reposition_dev(dev, 0, 1)) {
2103 Pmsg1(-1, "Reposition error. ERR=%s\n", strerror_dev(dev));
2106 Pmsg1(-1, _("Reading block %d.\n"), dev->block_num);
2107 if (!read_block_from_device(dcr, NO_BLOCK_NUMBER_CHECK)) {
2108 Pmsg1(-1, _("Error reading block: ERR=%s\n"), strerror_dev(dev));
2111 if (compare_blocks(first_block, block)) {
2112 Pmsg0(-1, _("\nThe first block on the second tape matches.\n\n"));
2115 /* Now find and compare the last block */
2116 Pmsg4(-1, _("Reposition from %u:%u to %u:%u\n"), dev->file, dev->block_num,
2117 last_file, last_block_num);
2118 if (!reposition_dev(dev, last_file, last_block_num)) {
2119 Pmsg1(-1, "Reposition error. ERR=%s\n", strerror_dev(dev));
2122 Pmsg1(-1, _("Reading block %d.\n"), dev->block_num);
2123 if (!read_block_from_device(dcr, NO_BLOCK_NUMBER_CHECK)) {
2124 Pmsg1(-1, _("Error reading block: ERR=%s\n"), strerror_dev(dev));
2127 if (compare_blocks(last_block, block)) {
2128 Pmsg0(-1, _("\nThe last block on the second tape matches. Test succeeded.\n\n"));
2132 free_block(last_block1);
2133 free_block(last_block2);
2134 free_block(first_block);
2137 /* Read 1000 records then stop */
2138 static bool quickie_cb(DCR *dcr, DEV_RECORD *rec)
2140 DEVICE *dev = dcr->dev;
2141 if (dev->file != 0) {
2142 Pmsg3(-1, "ERROR! device at %d:%d count=%d\n", dev->file, dev->block_num,
2147 if (quickie_count == 1000) {
2148 Pmsg2(-1, "1000 records read now at %d:%d\n", dev->file, dev->block_num);
2150 return quickie_count < 1000;
2153 static bool compare_blocks(DEV_BLOCK *last_block, DEV_BLOCK *block)
2156 uint32_t CheckSum, block_len;
2159 p = last_block->buf;
2161 unser_begin(q, BLKHDR2_LENGTH);
2162 unser_uint32(CheckSum);
2163 unser_uint32(block_len);
2164 while (q < (block->buf+block_len)) {
2171 dump_block(last_block, _("Last block written"));
2173 dump_block(block, _("Block read back"));
2174 Pmsg1(-1, "\n\nThe blocks differ at byte %u\n", p - last_block->buf);
2175 Pmsg0(-1, "\n\n!!!! The last block written and the block\n"
2176 "that was read back differ. The test FAILED !!!!\n"
2177 "This must be corrected before you use Bacula\n"
2178 "to write multi-tape Volumes.!!!!\n");
2182 dump_block(last_block, _("Last block written"));
2183 dump_block(block, _("Block read back"));
2193 * Write current block to tape regardless of whether or
2194 * not it is full. If the tape fills, attempt to
2195 * acquire another tape.
2197 static int flush_block(DEV_BLOCK *block, int dump)
2201 uint32_t this_file, this_block_num;
2205 this_block = new_block(dev);
2208 last_block = new_block(dev);
2211 this_file = dev->file;
2212 this_block_num = dev->block_num;
2213 if (!write_block_to_dev(dcr)) {
2214 Pmsg3(000, "Last block at: %u:%u this_dev_block_num=%d\n",
2215 last_file, last_block_num, this_block_num);
2218 * This is 1st tape, so save first tape info separate
2219 * from second tape info
2221 last_block_num1 = last_block_num;
2222 last_file1 = last_file;
2223 last_block1 = dup_block(last_block);
2224 last_block2 = dup_block(last_block);
2225 first_block = dup_block(block); /* first block second tape */
2228 Pmsg3(000, "Block not written: FileIndex=%u blk_block=%u Size=%u\n",
2229 (unsigned)file_index, block->BlockNumber, block->block_len);
2230 dump_block(last_block, "Last block written");
2232 dump_block(block, "Block not written");
2235 eot_block = block->BlockNumber;
2236 eot_block_len = block->block_len;
2237 eot_FileIndex = file_index;
2241 now -= jcr->run_time;
2243 now = 1; /* don't divide by zero */
2245 kbs = (double)dev->VolCatInfo.VolCatBytes / (1000 * now);
2246 vol_size = dev->VolCatInfo.VolCatBytes;
2247 Pmsg4(000, "End of tape %d:%d. VolumeCapacity=%s. Write rate = %.1f KB/s\n",
2248 dev->file, dev->block_num,
2249 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), kbs);
2252 stop = -1; /* stop, but do simplified test */
2254 /* Full test in progress */
2255 if (!fixup_device_block_write_error(jcr->dcr)) {
2256 Pmsg1(000, _("Cannot fixup device error. %s\n"), strerror_dev(dev));
2261 BlockNumber = 0; /* start counting for second tape */
2264 return 1; /* end of tape reached */
2267 /* Save contents after write so that the header is serialized */
2268 memcpy(this_block->buf, block->buf, this_block->buf_len);
2271 * Note, we always read/write to block, but we toggle
2272 * copying it to one or another of two allocated blocks.
2273 * Switch blocks so that the block just successfully written is
2274 * always in last_block.
2276 tblock = last_block;
2277 last_block = this_block;
2278 this_block = tblock;
2279 last_file = this_file;
2280 last_block_num = this_block_num;
2288 * First we label the tape, then we fill
2289 * it with data get a new tape and write a few blocks.
2291 static void qfillcmd()
2293 DEV_BLOCK *block = dcr->block;
2294 DEV_RECORD *rec = dcr->rec;
2297 Pmsg0(0, "Test writing blocks of 64512 bytes to tape.\n");
2299 get_cmd("How many blocks do you want to write? (1000): ");
2306 sm_check(__FILE__, __LINE__, false);
2308 i = block->buf_len - 100;
2310 rec->data = check_pool_memory_size(rec->data, i);
2311 memset(rec->data, i & 0xFF, i);
2314 Pmsg1(0, "Begin writing %d Bacula blocks to tape ...\n", count);
2315 for (i=0; i < count; i++) {
2320 if (!write_record_to_block(block, rec)) {
2321 Pmsg0(0, _("Error writing record to block.\n"));
2324 if (!write_block_to_dev(dcr)) {
2325 Pmsg0(0, _("Error writing block to device.\n"));
2331 if (dev_cap(dev, CAP_TWOEOF)) {
2338 sm_check(__FILE__, __LINE__, false);
2342 * Fill a tape using raw write() command
2344 static void rawfill_cmd()
2346 DEV_BLOCK *block = dcr->block;
2349 uint32_t block_num = 0;
2354 fd = open("/dev/urandom", O_RDONLY);
2356 read(fd, block->buf, block->buf_len);
2359 uint32_t *p = (uint32_t *)block->buf;
2360 srandom(time(NULL));
2361 for (i=0; i<block->buf_len/sizeof(uint32_t); i++) {
2365 p = (uint32_t *)block->buf;
2366 Pmsg1(0, "Begin writing raw blocks of %u bytes.\n", block->buf_len);
2369 stat = write(dev->fd, block->buf, block->buf_len);
2370 if (stat == (int)block->buf_len) {
2371 if ((block_num++ % 100) == 0) {
2376 for (i=1; i<(block->buf_len-sizeof(uint32_t))/sizeof(uint32_t)-1; i++) {
2386 printf("Write failed at block %u. stat=%d ERR=%s\n", block_num, stat,
2387 be.strerror(my_errno));
2393 * Fill a tape using raw write() command
2395 static void bfill_cmd()
2397 DEV_BLOCK *block = dcr->block;
2398 uint32_t block_num = 0;
2404 fd = open("/dev/urandom", O_RDONLY);
2406 read(fd, block->buf, block->buf_len);
2409 uint32_t *p = (uint32_t *)block->buf;
2410 srandom(time(NULL));
2411 for (i=0; i<block->buf_len/sizeof(uint32_t); i++) {
2415 p = (uint32_t *)block->buf;
2416 Pmsg1(0, "Begin writing Bacula blocks of %u bytes.\n", block->buf_len);
2419 block->binbuf = block->buf_len;
2420 block->bufp = block->buf + block->binbuf;
2421 if (!write_block_to_dev(dcr)) {
2424 if ((block_num++ % 100) == 0) {
2429 for (i=1; i<(block->buf_len/sizeof(uint32_t)-1); i++) {
2435 printf("Write failed at block %u.\n", block_num);
2440 struct cmdstruct { const char *key; void (*func)(); const char *help; };
2441 static struct cmdstruct commands[] = {
2442 {"autochanger", autochangercmd, "test autochanger"},
2443 {"bsf", bsfcmd, "backspace file"},
2444 {"bsr", bsrcmd, "backspace record"},
2445 {"bfill", bfill_cmd, "fill tape using Bacula writes"},
2446 {"cap", capcmd, "list device capabilities"},
2447 {"clear", clearcmd, "clear tape errors"},
2448 {"eod", eodcmd, "go to end of Bacula data for append"},
2449 {"eom", eomcmd, "go to the physical end of medium"},
2450 {"fill", fillcmd, "fill tape, write onto second volume"},
2451 {"unfill", unfillcmd, "read filled tape"},
2452 {"fsf", fsfcmd, "forward space a file"},
2453 {"fsr", fsrcmd, "forward space a record"},
2454 {"help", helpcmd, "print this command"},
2455 {"label", labelcmd, "write a Bacula label to the tape"},
2456 {"load", loadcmd, "load a tape"},
2457 {"quit", quitcmd, "quit btape"},
2458 {"rawfill", rawfill_cmd, "use write() to fill tape"},
2459 {"readlabel", readlabelcmd, "read and print the Bacula tape label"},
2460 {"rectest", rectestcmd, "test record handling functions"},
2461 {"rewind", rewindcmd, "rewind the tape"},
2462 {"scan", scancmd, "read() tape block by block to EOT and report"},
2463 {"scanblocks", scan_blocks, "Bacula read block by block to EOT and report"},
2464 {"status", statcmd, "print tape status"},
2465 {"test", testcmd, "General test Bacula tape functions"},
2466 {"weof", weofcmd, "write an EOF on the tape"},
2467 {"wr", wrcmd, "write a single Bacula block"},
2468 {"rr", rrcmd, "read a single record"},
2469 {"qfill", qfillcmd, "quick fill command"},
2471 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
2479 while (get_cmd("*")) {
2480 sm_check(__FILE__, __LINE__, false);
2482 parse_args(cmd, &args, &argc, argk, argv, MAX_CMD_ARGS);
2483 for (i=0; i<comsize; i++) /* search for command */
2484 if (argc > 0 && fstrsch(argk[0], commands[i].key)) {
2485 (*commands[i].func)(); /* go execute command */
2490 Pmsg1(0, _("%s is an illegal command\n"), cmd);
2496 static void helpcmd()
2500 printf(_("Interactive commands:\n"));
2501 printf(_(" Command Description\n ======= ===========\n"));
2502 for (i=0; i<comsize; i++)
2503 printf(" %-10s %s\n", commands[i].key, commands[i].help);
2510 "Copyright (C) 2000-2004 Kern Sibbald and John Walker.\n"
2511 "\nVersion: " VERSION " (" BDATE ")\n\n"
2512 "Usage: btape <options> <device_name>\n"
2513 " -b <file> specify bootstrap file\n"
2514 " -c <file> set configuration file to file\n"
2515 " -d <nn> set debug level to nn\n"
2516 " -p proceed inspite of I/O errors\n"
2517 " -s turn off signals\n"
2519 " -? print this message.\n"
2525 * Get next input command from terminal. This
2526 * routine is REALLY primitive, and should be enhanced
2527 * to have correct backspacing, etc.
2530 get_cmd(const char *prompt)
2534 fprintf(stdout, prompt);
2536 /* We really should turn off echoing and pretty this
2540 while ((ch = fgetc(stdin)) != EOF) {
2542 strip_trailing_junk(cmd);
2544 } else if (ch == 4 || ch == 0xd3 || ch == 0x8) {
2557 /* Dummies to replace askdir.c */
2558 bool dir_update_file_attributes(DCR *dcr, DEV_RECORD *rec) { return 1;}
2559 bool dir_send_job_status(JCR *jcr) {return 1;}
2561 bool dir_update_volume_info(DCR *dcr, bool relabel)
2567 bool dir_get_volume_info(DCR *dcr, enum get_vol_info_rw writing)
2569 Dmsg0(20, "Enter dir_get_volume_info\n");
2570 bstrncpy(dcr->VolCatInfo.VolCatName, dcr->VolumeName, sizeof(dcr->VolCatInfo.VolCatName));
2574 bool dir_create_jobmedia_record(DCR *dcr)
2576 dcr->WroteVol = false;
2581 bool dir_find_next_appendable_volume(DCR *dcr)
2583 Dmsg1(20, "Enter dir_find_next_appendable_volume. stop=%d\n", stop);
2584 return dcr->VolumeName[0] != 0;
2587 bool dir_ask_sysop_to_mount_volume(DCR *dcr)
2589 DEVICE *dev = dcr->dev;
2590 Dmsg0(20, "Enter dir_ask_sysop_to_mount_volume\n");
2591 if (dcr->VolumeName[0] == 0) {
2592 return dir_ask_sysop_to_create_appendable_volume(dcr);
2594 /* Close device so user can use autochanger if desired */
2595 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2598 force_close_dev(dev);
2599 Pmsg1(-1, "%s", dev->errmsg); /* print reason */
2600 if (dcr->VolumeName[0] == 0 || strcmp(dcr->VolumeName, "TestVolume2") == 0) {
2601 fprintf(stderr, "Mount second Volume on device %s and press return when ready: ",
2604 fprintf(stderr, "Mount Volume \"%s\" on device %s and press return when ready: ",
2605 dcr->VolumeName, dev_name(dev));
2611 bool dir_ask_sysop_to_create_appendable_volume(DCR *dcr)
2614 DEVICE *dev = dcr->dev;
2615 Dmsg0(20, "Enter dir_ask_sysop_to_create_appendable_volume\n");
2617 set_volume_name("TestVolume1", 1);
2619 set_volume_name("TestVolume2", 2);
2621 /* Close device so user can use autochanger if desired */
2622 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2625 autochanger = autoload_device(dcr, 1, NULL);
2627 force_close_dev(dev);
2628 fprintf(stderr, "Mount blank Volume on device %s and press return when ready: ",
2639 static bool my_mount_next_read_volume(DCR *dcr)
2642 JCR *jcr = dcr->jcr;
2643 DEV_BLOCK *block = dcr->block;
2645 Dmsg0(20, "Enter my_mount_next_read_volume\n");
2646 Pmsg1(000, "End of Volume \"%s\"\n", dcr->VolumeName);
2648 if (LastBlock != block->BlockNumber) {
2649 VolBytes += block->block_len;
2651 LastBlock = block->BlockNumber;
2653 now -= jcr->run_time;
2657 kbs = (double)VolBytes / (1000.0 * (double)now);
2658 Pmsg3(-1, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
2659 edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
2661 if (strcmp(dcr->VolumeName, "TestVolume2") == 0) {
2667 set_volume_name("TestVolume2", 2);
2669 create_vol_list(jcr);
2671 dev->state &= ~ST_READ;
2672 if (!acquire_device_for_read(jcr)) {
2673 Pmsg2(0, "Cannot open Dev=%s, Vol=%s\n", dev_name(dev), dcr->VolumeName);
2676 return true; /* next volume mounted */
2679 static void set_volume_name(const char *VolName, int volnum)
2681 DCR *dcr = jcr->dcr;
2682 VolumeName = VolName;
2684 bstrncpy(dev->VolCatInfo.VolCatName, VolName, sizeof(dev->VolCatInfo.VolCatName));
2685 bstrncpy(dcr->VolCatInfo.VolCatName, VolName, sizeof(dcr->VolCatInfo.VolCatName));
2686 bstrncpy(dcr->VolumeName, VolName, sizeof(dcr->VolumeName));
2687 dcr->VolCatInfo.Slot = volnum;