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);
174 bsnprintf(buf, sizeof(buf), "%" llu, x64);
175 i = bsscanf(buf, "%llu", &y64);
176 if (i != 1 || x64 != y64) {
177 Pmsg3(-1, "64 bit printf/scanf problem. i=%d x64=%" llu " y64=%" llu "\n", i, x64, y64);
181 printf("Tape block granularity is %d bytes.\n", TAPE_BSIZE);
183 working_directory = "/tmp";
184 my_name_is(margc, margv, "btape");
185 init_msg(NULL, NULL);
187 while ((ch = getopt(margc, margv, "b:c:d:psv?")) != -1) {
189 case 'b': /* bootstrap file */
190 bsr = parse_bsr(NULL, optarg);
191 // dump_bsr(bsr, true);
194 case 'c': /* specify config file */
195 if (configfile != NULL) {
198 configfile = bstrdup(optarg);
201 case 'd': /* set debug level */
202 debug_level = atoi(optarg);
203 if (debug_level <= 0) {
230 cmd = get_pool_memory(PM_FNAME);
231 args = get_pool_memory(PM_FNAME);
234 init_signals(terminate_btape);
237 if (configfile == NULL) {
238 configfile = bstrdup(CONFIG_FILE);
241 daemon_start_time = time(NULL);
243 parse_config(configfile);
246 /* See if we can open a device */
248 Pmsg0(000, "No archive name specified.\n");
251 } else if (margc != 1) {
252 Pmsg0(000, "Improper number of arguments specified.\n");
257 jcr = setup_jcr("btape", margv[0], bsr, NULL);
258 dev = setup_to_access_device(jcr, 0); /* acquire for write */
263 if (!open_the_device()) {
267 Dmsg0(200, "Do tape commands\n");
275 static void terminate_btape(int stat)
278 sm_check(__FILE__, __LINE__, false);
282 free_config_resources();
284 free_pool_memory(args);
288 free_pool_memory(cmd);
303 if (debug_level > 10)
304 print_memory_pool_stats();
307 free_block(this_block);
311 close_memory_pool(); /* free memory in pool */
317 static bool open_the_device()
321 block = new_block(dev);
323 if (!(dev->state & ST_OPENED)) {
324 Dmsg1(200, "Opening device %s\n", jcr->VolumeName);
325 if (open_dev(dev, jcr->VolumeName, OPEN_READ_WRITE) < 0) {
326 Emsg1(M_FATAL, 0, _("dev open failed: %s\n"), dev->errmsg);
332 Pmsg1(000, "open_dev %s OK\n", dev_name(dev));
345 * Write a label to the tape
347 static void labelcmd()
350 pm_strcpy(&cmd, VolumeName);
352 if (!get_cmd("Enter Volume Name: ")) {
357 if (!(dev->state & ST_OPENED)) {
358 if (!first_open_device(dev)) {
359 Pmsg1(0, "Device open failed. ERR=%s\n", strerror_dev(dev));
363 write_new_volume_label_to_dev(dcr, cmd, "Default");
364 Pmsg1(-1, "Wrote Volume label for volume \"%s\".\n", cmd);
368 * Read the tape label
370 static void readlabelcmd()
372 int save_debug_level = debug_level;
376 block = new_block(dev);
377 stat = read_dev_volume_label(dcr, block);
380 Pmsg0(0, "Volume has no label.\n");
383 Pmsg0(0, "Volume label read correctly.\n");
386 Pmsg1(0, "I/O error on device: ERR=%s", strerror_dev(dev));
389 Pmsg0(0, "Volume name error\n");
391 case VOL_CREATE_ERROR:
392 Pmsg1(0, "Error creating label. ERR=%s", strerror_dev(dev));
394 case VOL_VERSION_ERROR:
395 Pmsg0(0, "Volume version error.\n");
397 case VOL_LABEL_ERROR:
398 Pmsg0(0, "Bad Volume label type.\n");
401 Pmsg0(0, "Unknown error.\n");
406 dump_volume_label(dev);
407 debug_level = save_debug_level;
413 * Load the tape should have prevously been taken
414 * off line, otherwise this command is not necessary.
416 static void loadcmd()
419 if (!load_dev(dev)) {
420 Pmsg1(0, "Bad status from load. ERR=%s\n", strerror_dev(dev));
422 Pmsg1(0, "Loaded %s\n", dev_name(dev));
428 static void rewindcmd()
430 if (!rewind_dev(dev)) {
431 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
432 clrerror_dev(dev, -1);
434 Pmsg1(0, "Rewound %s\n", dev_name(dev));
439 * Clear any tape error
441 static void clearcmd()
443 clrerror_dev(dev, -1);
447 * Write and end of file on the tape
449 static void weofcmd()
460 if ((stat = weof_dev(dev, num)) < 0) {
461 Pmsg2(0, "Bad status from weof %d. ERR=%s\n", stat, strerror_dev(dev));
464 Pmsg3(0, "Wrote %d EOF%s to %s\n", num, num==1?"":"s", dev_name(dev));
469 /* Go to the end of the medium -- raw command
470 * The idea was orginally that the end of the Bacula
471 * medium would be flagged differently. This is not
472 * currently the case. So, this is identical to the
478 Pmsg1(0, "%s", strerror_dev(dev));
481 Pmsg0(0, _("Moved to end of medium.\n"));
486 * Go to the end of the medium (either hardware determined
487 * or defined by two eofs.
507 if (!bsf_dev(dev, num)) {
508 Pmsg1(0, _("Bad status from bsf. ERR=%s\n"), strerror_dev(dev));
510 Pmsg2(0, _("Backspaced %d file%s.\n"), num, num==1?"":"s");
526 if (!bsr_dev(dev, num)) {
527 Pmsg1(0, _("Bad status from bsr. ERR=%s\n"), strerror_dev(dev));
529 Pmsg2(0, _("Backspaced %d record%s.\n"), num, num==1?"":"s");
534 * List device capabilities as defined in the
539 printf(_("Configured device capabilities:\n"));
540 printf("%sEOF ", dev->capabilities & CAP_EOF ? "" : "!");
541 printf("%sBSR ", dev->capabilities & CAP_BSR ? "" : "!");
542 printf("%sBSF ", dev->capabilities & CAP_BSF ? "" : "!");
543 printf("%sFSR ", dev->capabilities & CAP_FSR ? "" : "!");
544 printf("%sFSF ", dev->capabilities & CAP_FSF ? "" : "!");
545 printf("%sFASTFSF ", dev->capabilities & CAP_FASTFSF ? "" : "!");
546 printf("%sEOM ", dev->capabilities & CAP_EOM ? "" : "!");
547 printf("%sREM ", dev->capabilities & CAP_REM ? "" : "!");
548 printf("%sRACCESS ", dev->capabilities & CAP_RACCESS ? "" : "!");
549 printf("%sAUTOMOUNT ", dev->capabilities & CAP_AUTOMOUNT ? "" : "!");
550 printf("%sLABEL ", dev->capabilities & CAP_LABEL ? "" : "!");
551 printf("%sANONVOLS ", dev->capabilities & CAP_ANONVOLS ? "" : "!");
552 printf("%sALWAYSOPEN ", dev->capabilities & CAP_ALWAYSOPEN ? "" : "!");
555 printf(_("Device status:\n"));
556 printf("%sOPENED ", dev->state & ST_OPENED ? "" : "!");
557 printf("%sTAPE ", dev->state & ST_TAPE ? "" : "!");
558 printf("%sLABEL ", dev->state & ST_LABEL ? "" : "!");
559 printf("%sMALLOC ", dev->state & ST_MALLOC ? "" : "!");
560 printf("%sAPPEND ", dev->state & ST_APPEND ? "" : "!");
561 printf("%sREAD ", dev->state & ST_READ ? "" : "!");
562 printf("%sEOT ", dev->state & ST_EOT ? "" : "!");
563 printf("%sWEOT ", dev->state & ST_WEOT ? "" : "!");
564 printf("%sEOF ", dev->state & ST_EOF ? "" : "!");
565 printf("%sNEXTVOL ", dev->state & ST_NEXTVOL ? "" : "!");
566 printf("%sSHORT ", dev->state & ST_SHORT ? "" : "!");
569 printf(_("Device parameters:\n"));
570 printf("Device name: %s\n", dev->dev_name);
571 printf("File=%u block=%u\n", dev->file, dev->block_num);
572 printf("Min block=%u Max block=%u\n", dev->min_block_size, dev->max_block_size);
580 * Test writting larger and larger records.
581 * This is a torture test for records.
583 static void rectestcmd()
589 Pmsg0(0, "Test writting larger and larger records.\n"
590 "This is a torture test for records.\nI am going to write\n"
591 "larger and larger records. It will stop when the record size\n"
592 "plus the header exceeds the block size (by default about 64K)\n");
595 get_cmd("Do you want to continue? (y/n): ");
597 Pmsg0(000, "Command aborted.\n");
601 sm_check(__FILE__, __LINE__, false);
602 block = new_block(dev);
605 for (i=1; i<500000; i++) {
606 rec->data = check_pool_memory_size(rec->data, i);
607 memset(rec->data, i & 0xFF, i);
609 sm_check(__FILE__, __LINE__, false);
610 if (write_record_to_block(block, rec)) {
613 Pmsg2(0, "Block %d i=%d\n", blkno, i);
617 sm_check(__FILE__, __LINE__, false);
621 sm_check(__FILE__, __LINE__, false);
625 * This test attempts to re-read a block written by Bacula
626 * normally at the end of the tape. Bacula will then back up
627 * over the two eof marks, backup over the record and reread
628 * it to make sure it is valid. Bacula can skip this validation
629 * if you set "Backward space record = no"
631 static int re_read_block_test()
638 if (!(dev->capabilities & CAP_BSR)) {
639 Pmsg0(-1, _("Skipping read backwards test because BSR turned off.\n"));
643 Pmsg0(-1, _("\n=== Write, backup, and re-read test ===\n\n"
644 "I'm going to write three records and an EOF\n"
645 "then backup over the EOF and re-read the last record.\n"
646 "Bacula does this after writing the last block on the\n"
647 "tape to verify that the block was written correctly.\n\n"
648 "This is not an *essential* feature ...\n\n"));
650 block = new_block(dev);
652 rec->data = check_pool_memory_size(rec->data, block->buf_len);
653 len = rec->data_len = block->buf_len-100;
654 memset(rec->data, 1, rec->data_len);
655 if (!write_record_to_block(block, rec)) {
656 Pmsg0(0, _("Error writing record to block.\n"));
659 if (!write_block_to_dev(dcr, block)) {
660 Pmsg0(0, _("Error writing block to device.\n"));
663 Pmsg1(0, _("Wrote first record of %d bytes.\n"), rec->data_len);
665 memset(rec->data, 2, rec->data_len);
666 if (!write_record_to_block(block, rec)) {
667 Pmsg0(0, _("Error writing record to block.\n"));
670 if (!write_block_to_dev(dcr, block)) {
671 Pmsg0(0, _("Error writing block to device.\n"));
674 Pmsg1(0, _("Wrote second record of %d bytes.\n"), rec->data_len);
676 memset(rec->data, 3, rec->data_len);
677 if (!write_record_to_block(block, rec)) {
678 Pmsg0(0, _("Error writing record to block.\n"));
681 if (!write_block_to_dev(dcr, block)) {
682 Pmsg0(0, _("Error writing block to device.\n"));
685 Pmsg1(0, _("Wrote third record of %d bytes.\n"), rec->data_len);
688 if (dev_cap(dev, CAP_TWOEOF)) {
691 if (!bsf_dev(dev, 1)) {
692 Pmsg1(0, _("Backspace file failed! ERR=%s\n"), strerror_dev(dev));
695 if (dev_cap(dev, CAP_TWOEOF)) {
696 if (!bsf_dev(dev, 1)) {
697 Pmsg1(0, _("Backspace file failed! ERR=%s\n"), strerror_dev(dev));
701 Pmsg0(0, "Backspaced over EOF OK.\n");
702 if (!bsr_dev(dev, 1)) {
703 Pmsg1(0, _("Backspace record failed! ERR=%s\n"), strerror_dev(dev));
706 Pmsg0(0, "Backspace record OK.\n");
707 if (!read_block_from_dev(dcr, block, NO_BLOCK_NUMBER_CHECK)) {
708 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
711 memset(rec->data, 0, rec->data_len);
712 if (!read_record_from_block(block, rec)) {
713 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
716 for (int i=0; i<len; i++) {
717 if (rec->data[i] != 3) {
718 Pmsg0(0, _("Bad data in record. Test failed!\n"));
722 Pmsg0(0, _("\nBlock re-read correct. Test succeeded!\n"));
723 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"));
760 block = new_block(dev);
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, block)) {
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, block)) {
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, block, 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"));
848 * This test writes Bacula blocks to the tape in
849 * several files. It then rewinds the tape and attepts
850 * to read these blocks back checking the data.
852 static int position_test()
860 int file = 0, blk = 0;
863 Pmsg0(-1, _("\n=== Write, rewind, and position test ===\n\n"
864 "I'm going to write 1000 records and an EOF\n"
865 "then write 1000 records and an EOF, then rewind,\n"
866 "and position to a few blocks and verify that it is correct.\n\n"
867 "This is an *essential* feature ...\n\n"));
868 block = new_block(dev);
870 if (!rewind_dev(dev)) {
871 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
874 rec->data = check_pool_memory_size(rec->data, block->buf_len);
875 rec->data_len = block->buf_len-100;
876 len = rec->data_len/sizeof(i);
877 for (i=1; i<=1000; i++) {
878 p = (int *)rec->data;
879 for (j=0; j<len; j++) {
882 if (!write_record_to_block(block, rec)) {
883 Pmsg0(0, _("Error writing record to block.\n"));
886 if (!write_block_to_dev(dcr, block)) {
887 Pmsg0(0, _("Error writing block to device.\n"));
891 Pmsg1(0, _("Wrote 1000 blocks of %d bytes.\n"), rec->data_len);
893 for (i=1001; i<=2000; i++) {
894 p = (int *)rec->data;
895 for (j=0; j<len; j++) {
898 if (!write_record_to_block(block, rec)) {
899 Pmsg0(0, _("Error writing record to block.\n"));
902 if (!write_block_to_dev(dcr, block)) {
903 Pmsg0(0, _("Error writing block to device.\n"));
907 Pmsg1(0, _("Wrote 1000 blocks of %d bytes.\n"), rec->data_len);
909 if (dev_cap(dev, CAP_TWOEOF)) {
912 if (!rewind_dev(dev)) {
913 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
916 Pmsg0(0, "Rewind OK.\n");
920 /* Set up next item to read based on where we are */
956 Pmsg2(-1, "Reposition to file:block %d:%d\n", file, blk);
957 if (!reposition_dev(dev, file, blk)) {
958 Pmsg0(0, "Reposition error.\n");
962 if (!read_block_from_dev(dcr, block, NO_BLOCK_NUMBER_CHECK)) {
963 if (dev_state(dev, ST_EOF)) {
964 Pmsg0(-1, _("Got EOF on tape.\n"));
967 Pmsg1(0, _("Read block failed! ERR=%s\n\n"), strerror(dev->dev_errno));
968 Pmsg0(0, _("This may be because the tape drive block size is not\n"
969 " set to variable blocking as normally used by Bacula.\n"
970 " Please see the Tape Testing chapter in the manual and \n"
971 " look for using mt with defblksize and setoptions\n"));
974 memset(rec->data, 0, rec->data_len);
975 if (!read_record_from_block(block, rec)) {
976 Pmsg1(0, _("Read record failed! ERR=%s\n"), strerror(dev->dev_errno));
979 p = (int *)rec->data;
980 for (j=0; j<len; j++) {
982 Pmsg3(0, _("Bad data in record. Expected %d, got %d at byte %d. Test failed!\n"),
987 Pmsg0(-1, _("Block re-read correctly.\n"));
989 Pmsg0(-1, _("=== Test Succeeded. End Write, rewind, and re-read test ===\n\n"));
1002 * This test writes some records, then writes an end of file,
1003 * rewinds the tape, moves to the end of the data and attepts
1004 * to append to the tape. This function is essential for
1005 * Bacula to be able to write multiple jobs to the tape.
1007 static int append_test()
1009 Pmsg0(-1, _("\n\n=== Append files test ===\n\n"
1010 "This test is essential to Bacula.\n\n"
1011 "I'm going to write one record in file 0,\n"
1012 " two records in file 1,\n"
1013 " and three records in file 2\n\n"));
1017 weofcmd(); /* end file 0 */
1020 weofcmd(); /* end file 1 */
1024 weofcmd(); /* end file 2 */
1025 if (dev_cap(dev, CAP_TWOEOF)) {
1028 force_close_dev(dev); /* release device */
1029 if (!open_the_device()) {
1033 Pmsg0(0, _("Now moving to end of medium.\n"));
1035 Pmsg2(-1, _("We should be in file 3. I am at file %d. This is %s\n"),
1036 dev->file, dev->file == 3 ? "correct!" : "NOT correct!!!!");
1038 if (dev->file != 3) {
1042 Pmsg0(-1, _("\nNow the important part, I am going to attempt to append to the tape.\n\n"));
1045 if (dev_cap(dev, CAP_TWOEOF)) {
1049 Pmsg0(-1, _("Done appending, there should be no I/O errors\n\n"));
1050 Pmsg0(-1, "Doing Bacula scan of blocks:\n");
1052 Pmsg0(-1, _("End scanning the tape.\n"));
1053 Pmsg2(-1, _("We should be in file 4. I am at file %d. This is %s\n"),
1054 dev->file, dev->file == 4 ? "correct!" : "NOT correct!!!!");
1056 if (dev->file != 4) {
1064 * This test exercises the autochanger
1066 static int autochanger_test()
1068 POOLMEM *results, *changer;
1069 int slot, status, loaded;
1070 int timeout = jcr->device->max_changer_wait;
1073 Dmsg1(100, "Max changer wait = %d sec\n", timeout);
1074 if (!dev_cap(dev, CAP_AUTOCHANGER)) {
1077 if (!(jcr->device && jcr->device->changer_name && jcr->device->changer_command)) {
1078 Pmsg0(-1, "\nAutochanger enabled, but no name or no command device specified.\n");
1082 Pmsg0(-1, "\nAh, I see you have an autochanger configured.\n"
1083 "To test the autochanger you must have a blank tape\n"
1084 " that I can write on in Slot 1.\n");
1085 if (!get_cmd("\nDo you wish to continue with the Autochanger test? (y/n): ")) {
1088 if (cmd[0] != 'y' && cmd[0] != 'Y') {
1092 Pmsg0(-1, _("\n\n=== Autochanger test ===\n\n"));
1094 results = get_pool_memory(PM_MESSAGE);
1095 changer = get_pool_memory(PM_FNAME);
1099 jcr->VolCatInfo.Slot = slot;
1100 /* Find out what is loaded, zero means device is unloaded */
1101 Pmsg0(-1, _("3301 Issuing autochanger \"loaded\" command.\n"));
1102 changer = edit_device_codes(jcr, changer, jcr->device->changer_command,
1104 status = run_program(changer, timeout, results);
1105 Dmsg3(100, "run_prog: %s stat=%d result=%s\n", changer, status, results);
1107 loaded = atoi(results);
1109 Pmsg1(-1, _("3991 Bad autochanger command: %s\n"), changer);
1110 Pmsg2(-1, _("3991 status=%d result=%s\n"), status, results);
1114 Pmsg1(-1, "Slot %d loaded. I am going to unload it.\n", loaded);
1116 Pmsg0(-1, "Nothing loaded in the drive. OK.\n");
1118 Dmsg1(100, "Results from loaded query=%s\n", results);
1120 jcr->VolCatInfo.Slot = loaded;
1121 offline_or_rewind_dev(dev);
1122 /* We are going to load a new tape, so close the device */
1123 force_close_dev(dev);
1124 Pmsg2(-1, _("3302 Issuing autochanger \"unload %d %d\" command.\n"),
1125 loaded, dev->drive_index);
1126 changer = edit_device_codes(jcr, changer,
1127 jcr->device->changer_command, "unload");
1128 status = run_program(changer, timeout, results);
1129 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 status=%d result=%s\n"), status, results);
1141 jcr->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);
1152 Pmsg1(-1, _("3993 Bad autochanger command: %s\n"), changer);
1153 Pmsg2(-1, _("3993 status=%d result=%s\n"), status, results);
1157 if (!open_the_device()) {
1160 bmicrosleep(sleep_time, 0);
1161 if (!rewind_dev(dev)) {
1162 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
1163 clrerror_dev(dev, -1);
1164 Pmsg0(-1, "\nThe test failed, probably because you need to put\n"
1165 "a longer sleep time in the mtx-script in the load) case.\n"
1166 "Adding a 30 second sleep and trying again ...\n");
1170 Pmsg1(0, "Rewound %s\n", dev_name(dev));
1173 if ((status = weof_dev(dev, 1)) < 0) {
1174 Pmsg2(0, "Bad status from weof %d. ERR=%s\n", status, strerror_dev(dev));
1177 Pmsg1(0, "Wrote EOF to %s\n", dev_name(dev));
1181 Pmsg1(-1, "\nThe test worked this time. Please add:\n\n"
1183 "to your mtx-changer script in the load) case.\n\n",
1186 Pmsg0(-1, "\nThe test autochanger worked!!\n\n");
1189 free_pool_memory(changer);
1190 free_pool_memory(results);
1195 free_pool_memory(changer);
1196 free_pool_memory(results);
1197 Pmsg0(-1, "You must correct this error or the Autochanger will not work.\n");
1201 static void autochangercmd()
1208 * This test assumes that the append test has been done,
1209 * then it tests the fsf function.
1211 static int fsf_test()
1213 bool set_off = false;
1215 Pmsg0(-1, _("\n\n=== Forward space files test ===\n\n"
1216 "This test is essential to Bacula.\n\n"
1217 "I'm going to write five files then test forward spacing\n\n"));
1221 weofcmd(); /* end file 0 */
1224 weofcmd(); /* end file 1 */
1228 weofcmd(); /* end file 2 */
1231 weofcmd(); /* end file 3 */
1233 weofcmd(); /* end file 4 */
1234 if (dev_cap(dev, CAP_TWOEOF)) {
1240 Pmsg0(0, _("Now forward spacing 1 file.\n"));
1241 if (!fsf_dev(dev, 1)) {
1242 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1245 Pmsg2(-1, _("We should be in file 1. I am at file %d. This is %s\n"),
1246 dev->file, dev->file == 1 ? "correct!" : "NOT correct!!!!");
1248 if (dev->file != 1) {
1252 Pmsg0(0, _("Now forward spacing 2 files.\n"));
1253 if (!fsf_dev(dev, 2)) {
1254 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1257 Pmsg2(-1, _("We should be in file 3. I am at file %d. This is %s\n"),
1258 dev->file, dev->file == 3 ? "correct!" : "NOT correct!!!!");
1260 if (dev->file != 3) {
1265 Pmsg0(0, _("Now forward spacing 4 files.\n"));
1266 if (!fsf_dev(dev, 4)) {
1267 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1270 Pmsg2(-1, _("We should be in file 4. I am at file %d. This is %s\n"),
1271 dev->file, dev->file == 4 ? "correct!" : "NOT correct!!!!");
1273 if (dev->file != 4) {
1277 Pmsg0(-1, "The test worked this time. Please add:\n\n"
1278 " Fast Forward Space File = no\n\n"
1279 "to your Device resource for this drive.\n");
1283 Pmsg0(0, _("Now forward spacing 1 more file.\n"));
1284 if (!fsf_dev(dev, 1)) {
1285 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1287 Pmsg2(-1, _("We should be in file 5. I am at file %d. This is %s\n"),
1288 dev->file, dev->file == 5 ? "correct!" : "NOT correct!!!!");
1289 if (dev->file != 5) {
1292 Pmsg0(-1, _("\n=== End Forward space files test ===\n\n"));
1296 Pmsg0(-1, _("\nThe forward space file test failed.\n"));
1297 if (dev_cap(dev, CAP_FASTFSF)) {
1298 Pmsg0(-1, "You have Fast Forward Space File enabled.\n"
1299 "I am turning it off then retrying the test.\n");
1300 dev->capabilities &= ~CAP_FASTFSF;
1304 Pmsg0(-1, "You must correct this error or Bacula will not work.\n");
1313 * This is a general test of Bacula's functions
1314 * needed to read and write the tape.
1316 static void testcmd()
1320 if (!write_read_test()) {
1323 if (!position_test()) {
1327 stat = append_test();
1328 if (stat == 1) { /* OK get out */
1331 if (stat == -1) { /* first test failed */
1332 if (dev_cap(dev, CAP_EOM) || dev_cap(dev, CAP_FASTFSF)) {
1333 Pmsg0(-1, "\nAppend test failed. Attempting again.\n"
1334 "Setting \"Hardware End of Medium = no\n"
1335 " and \"Fast Forward Space File = no\n"
1336 "and retrying append test.\n\n");
1337 dev->capabilities &= ~CAP_EOM; /* turn off eom */
1338 dev->capabilities &= ~CAP_FASTFSF; /* turn off fast fsf */
1339 stat = append_test();
1341 Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n"
1342 " Hardware End of Medium = No\n\n"
1343 " Fast Forward Space File = No\n"
1344 "to your Device resource in the Storage conf file.\n");
1348 Pmsg0(-1, "\n\nThat appears *NOT* to have corrected the problem.\n");
1351 /* Wrong count after append */
1353 Pmsg0(-1, "\n\nIt looks like the append failed. Attempting again.\n"
1354 "Setting \"BSF at EOM = yes\" and retrying append test.\n");
1355 dev->capabilities |= CAP_BSFATEOM; /* backspace on eom */
1356 stat = append_test();
1358 Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n"
1359 " Hardware End of Medium = No\n"
1360 " Fast Forward Space File = No\n"
1361 " BSF at EOM = yes\n\n"
1362 "to your Device resource in the Storage conf file.\n");
1369 Pmsg0(-1, "\nAppend test failed.\n\n");
1370 Pmsg0(-1, "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
1371 "Unable to correct the problem. You MUST fix this\n"
1372 "problem before Bacula can use your tape drive correctly\n");
1373 Pmsg0(-1, "\nPerhaps running Bacula in fixed block mode will work.\n"
1374 "Do so by setting:\n\n"
1375 "Minimum Block Size = nnn\n"
1376 "Maximum Block Size = nnn\n\n"
1377 "in your Storage daemon's Device definition.\n"
1378 "nnn must match your tape driver's block size, which\n"
1379 "can be determined by reading your tape manufacturers\n"
1380 "information, and the information on your kernel dirver.\n"
1381 "Fixed block sizes, however, are not normally an ideal solution.\n");
1386 Pmsg0(-1, _("\nThe above Bacula scan should have output identical to what follows.\n"
1387 "Please double check it ...\n"
1388 "=== Sample correct output ===\n"
1389 "1 block of 64448 bytes in file 1\n"
1390 "End of File mark.\n"
1391 "2 blocks of 64448 bytes in file 2\n"
1392 "End of File mark.\n"
1393 "3 blocks of 64448 bytes in file 3\n"
1394 "End of File mark.\n"
1395 "1 block of 64448 bytes in file 4\n"
1396 "End of File mark.\n"
1397 "Total files=4, blocks=7, bytes = 451,136\n"
1398 "=== End sample correct output ===\n\n"));
1400 Pmsg0(-1, _("If the above scan output is not identical to the\n"
1401 "sample output, you MUST correct the problem\n"
1402 "or Bacula will not be able to write multiple Jobs to \n"
1406 re_read_block_test();
1409 fsf_test(); /* do fast forward space file test */
1411 autochanger_test(); /* do autochanger test */
1415 /* Forward space a file */
1416 static void fsfcmd()
1420 num = atoi(argk[1]);
1425 if (!fsf_dev(dev, num)) {
1426 Pmsg1(0, "Bad status from fsf. ERR=%s\n", strerror_dev(dev));
1429 Pmsg2(0, "Forward spaced %d file%s.\n", num, num==1?"":"s");
1432 /* Forward space a record */
1433 static void fsrcmd()
1437 num = atoi(argk[1]);
1442 if (!fsr_dev(dev, num)) {
1443 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1446 Pmsg2(0, "Forward spaced %d record%s.\n", num, num==1?"":"s");
1451 * Write a Bacula block to the tape
1459 sm_check(__FILE__, __LINE__, false);
1460 block = new_block(dev);
1462 dump_block(block, "test");
1464 i = block->buf_len - 100;
1466 rec->data = check_pool_memory_size(rec->data, i);
1467 memset(rec->data, i & 0xFF, i);
1469 sm_check(__FILE__, __LINE__, false);
1470 if (!write_record_to_block(block, rec)) {
1471 Pmsg0(0, _("Error writing record to block.\n"));
1474 if (!write_block_to_dev(dcr, block)) {
1475 Pmsg0(0, _("Error writing block to device.\n"));
1478 Pmsg1(0, _("Wrote one record of %d bytes.\n"), i);
1480 Pmsg0(0, _("Wrote block to device.\n"));
1483 sm_check(__FILE__, __LINE__, false);
1486 sm_check(__FILE__, __LINE__, false);
1490 * Read a record from the tape
1497 if (!get_cmd("Enter length to read: ")) {
1501 if (len < 0 || len > 1000000) {
1502 Pmsg0(0, _("Bad length entered, using default of 1024 bytes.\n"));
1505 buf = (char *)malloc(len);
1506 stat = read(dev->fd, buf, len);
1507 if (stat > 0 && stat <= len) {
1510 Pmsg3(0, _("Read of %d bytes gives stat=%d. ERR=%s\n"),
1511 len, stat, strerror(errno));
1517 * Scan tape by reading block by block. Report what is
1518 * on the tape. Note, this command does raw reads, and as such
1519 * will not work with fixed block size devices.
1521 static void scancmd()
1524 int blocks, tot_blocks, tot_files;
1530 blocks = block_size = tot_blocks = 0;
1532 if (dev->state & ST_EOT) {
1533 Pmsg0(0, "End of tape\n");
1536 update_pos_dev(dev);
1537 tot_files = dev->file;
1538 Pmsg1(0, _("Starting scan at file %u\n"), dev->file);
1540 if ((stat = read(dev->fd, buf, sizeof(buf))) < 0) {
1541 clrerror_dev(dev, -1);
1542 Mmsg2(&dev->errmsg, "read error on %s. ERR=%s.\n",
1543 dev->dev_name, strerror(dev->dev_errno));
1544 Pmsg2(0, "Bad status from read %d. ERR=%s\n", stat, strerror_dev(dev));
1546 printf("%d block%s of %d bytes in file %d\n",
1547 blocks, blocks>1?"s":"", block_size, dev->file);
1550 Dmsg1(200, "read status = %d\n", stat);
1552 if (stat != block_size) {
1553 update_pos_dev(dev);
1555 printf("%d block%s of %d bytes in file %d\n",
1556 blocks, blocks>1?"s":"", block_size, dev->file);
1561 if (stat == 0) { /* EOF */
1562 update_pos_dev(dev);
1563 printf("End of File mark.\n");
1564 /* Two reads of zero means end of tape */
1565 if (dev->state & ST_EOF)
1566 dev->state |= ST_EOT;
1568 dev->state |= ST_EOF;
1571 if (dev->state & ST_EOT) {
1572 printf("End of tape\n");
1575 } else { /* Got data */
1576 dev->state &= ~ST_EOF;
1582 update_pos_dev(dev);
1583 tot_files = dev->file - tot_files;
1584 printf("Total files=%d, blocks=%d, bytes = %s\n", tot_files, tot_blocks,
1585 edit_uint64_with_commas(bytes, ec1));
1590 * Scan tape by reading Bacula block by block. Report what is
1591 * on the tape. This function reads Bacula blocks, so if your
1592 * Device resource is correctly defined, it should work with
1593 * either variable or fixed block sizes.
1595 static void scan_blocks()
1597 int blocks, tot_blocks, tot_files;
1598 uint32_t block_size;
1603 block = new_block(dev);
1604 blocks = block_size = tot_blocks = 0;
1607 update_pos_dev(dev);
1608 tot_files = dev->file;
1610 if (!read_block_from_device(dcr, block, NO_BLOCK_NUMBER_CHECK)) {
1611 Dmsg1(100, "!read_block(): ERR=%s\n", strerror_dev(dev));
1612 if (dev->state & ST_EOT) {
1614 printf("%d block%s of %d bytes in file %d\n",
1615 blocks, blocks>1?"s":"", block_size, dev->file);
1620 if (dev->state & ST_EOF) {
1622 printf("%d block%s of %d bytes in file %d\n",
1623 blocks, blocks>1?"s":"", block_size, dev->file);
1626 printf(_("End of File mark.\n"));
1629 if (dev->state & ST_SHORT) {
1631 printf("%d block%s of %d bytes in file %d\n",
1632 blocks, blocks>1?"s":"", block_size, dev->file);
1635 printf(_("Short block read.\n"));
1638 printf(_("Error reading block. ERR=%s\n"), strerror_dev(dev));
1641 if (block->block_len != block_size) {
1643 printf("%d block%s of %d bytes in file %d\n",
1644 blocks, blocks>1?"s":"", block_size, dev->file);
1647 block_size = block->block_len;
1651 bytes += block->block_len;
1652 Dmsg6(100, "Blk_blk=%u dev_blk=%u blen=%u bVer=%d SessId=%u SessTim=%u\n",
1653 block->BlockNumber, dev->block_num, block->block_len, block->BlockVer,
1654 block->VolSessionId, block->VolSessionTime);
1656 DEV_RECORD *rec = new_record();
1657 read_record_from_block(block, rec);
1658 Pmsg8(-1, "Blk_block: %u dev_blk=%u blen=%u First rec FI=%s SessId=%u SessTim=%u Strm=%s rlen=%d\n",
1659 block->BlockNumber, dev->block_num, block->block_len,
1660 FI_to_ascii(rec->FileIndex), rec->VolSessionId, rec->VolSessionTime,
1661 stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len);
1664 } else if (verbose > 1) {
1665 dump_block(block, "");
1671 tot_files = dev->file - tot_files;
1672 printf("Total files=%d, blocks=%d, bytes = %s\n", tot_files, tot_blocks,
1673 edit_uint64_with_commas(bytes, ec1));
1677 static void statcmd()
1679 int debug = debug_level;
1681 Pmsg2(0, "Device status: %u. ERR=%s\n", status_dev(dev), strerror_dev(dev));
1683 dump_volume_label(dev);
1685 debug_level = debug;
1690 * First we label the tape, then we fill
1691 * it with data get a new tape and write a few blocks.
1693 static void fillcmd()
1700 uint32_t min_block_size;
1710 This command simulates Bacula writing to a tape.\n\
1711 It requires either one or two blank tapes, which it\n\
1712 will label and write.\n\n\
1713 If you have an autochanger configured, it will use\n\
1714 the tapes that are in slots 1 and 2, otherwise, you will\n\
1715 be prompted to insert the tapes when necessary.\n\n\
1716 It will print a status approximately\n\
1717 every 322 MB, and write an EOF every 3.2 GB. If you have\n\
1718 selected the simple test option, after writing the first tape\n\
1719 it will rewind it and re-read the last block written.\n\n\
1720 If you have selected the multiple tape test, when the first tape\n\
1721 fills, it will ask for a second, and after writing a few more \n\
1722 blocks, it will stop. Then it will begin re-reading the\n\
1724 This may take a long time -- hours! ...\n\n");
1726 get_cmd("Do you want to run the simplified test (s) with one tape\n"
1727 "or the complete multiple tape (m) test: (s/m) ");
1728 if (cmd[0] == 's') {
1729 Pmsg0(-1, "Simple test (single tape) selected.\n");
1731 } else if (cmd[0] == 'm') {
1732 Pmsg0(-1, "Multiple tape test selected.\n");
1735 Pmsg0(000, "Command aborted.\n");
1739 Dmsg1(20, "Begin append device=%s\n", dev_name(dev));
1740 Dmsg1(20, "MaxVolSize=%s\n", edit_uint64(dev->max_volume_size, ec1));
1742 /* Use fixed block size to simplify read back */
1743 min_block_size = dev->min_block_size;
1744 dev->min_block_size = dev->max_block_size;
1747 * Acquire output device for writing. Note, after acquiring a
1748 * device, we MUST release it, which is done at the end of this
1751 Dmsg0(100, "just before acquire_device\n");
1752 if (!acquire_device_for_append(jcr)) {
1753 set_jcr_job_status(jcr, JS_ErrorTerminated);
1756 block = jcr->dcr->block;
1758 Dmsg0(100, "Just after acquire_device_for_append\n");
1760 * Write Begin Session Record
1762 if (!write_session_label(dcr, block, SOS_LABEL)) {
1763 set_jcr_job_status(jcr, JS_ErrorTerminated);
1764 Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"),
1768 Pmsg0(-1, "Wrote Start Of Session label.\n");
1770 memset(&rec, 0, sizeof(rec));
1771 rec.data = get_memory(100000); /* max record size */
1773 #define REC_SIZE 32768
1774 rec.data_len = REC_SIZE;
1777 * Put some random data in the record
1779 fd = open("/dev/urandom", O_RDONLY);
1781 read(fd, rec.data, rec.data_len);
1784 uint32_t *p = (uint32_t *)rec.data;
1785 srandom(time(NULL));
1786 for (i=0; i<rec.data_len/sizeof(uint32_t); i++) {
1792 * Generate data as if from File daemon, write to device
1794 jcr->dcr->VolFirstIndex = 0;
1795 time(&jcr->run_time); /* start counting time for rates */
1797 Pmsg0(-1, "Begin writing Bacula records to tape ...\n");
1799 Pmsg0(-1, "Begin writing Bacula records to first tape ...\n");
1801 for (file_index = 0; ok && !job_canceled(jcr); ) {
1802 rec.VolSessionId = jcr->VolSessionId;
1803 rec.VolSessionTime = jcr->VolSessionTime;
1804 rec.FileIndex = ++file_index;
1805 rec.Stream = STREAM_FILE_DATA;
1807 /* Mix up the data just a bit */
1808 uint32_t *lp = (uint32_t *)rec.data;
1810 for (i=1; i < (rec.data_len-sizeof(uint32_t))/sizeof(uint32_t)-1; i++) {
1814 Dmsg4(250, "before write_rec FI=%d SessId=%d Strm=%s len=%d\n",
1815 rec.FileIndex, rec.VolSessionId, stream_to_ascii(rec.Stream, rec.FileIndex),
1818 while (!write_record_to_block(block, &rec)) {
1820 * When we get here we have just filled a block
1822 Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec.data_len,
1825 /* Write block to tape */
1826 if (!flush_block(block, 1)) {
1830 /* Every 5000 blocks (approx 322MB) report where we are.
1832 if ((block->BlockNumber % 5000) == 0) {
1834 now -= jcr->run_time;
1836 now = 1; /* prevent divide error */
1838 kbs = (double)dev->VolCatInfo.VolCatBytes / (1000.0 * (double)now);
1839 Pmsg4(-1, "Wrote blk_block=%u, dev_blk_num=%u VolBytes=%s rate=%.1f KB/s\n",
1840 block->BlockNumber, dev->block_num,
1841 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), (float)kbs);
1843 /* Every 15000 blocks (approx 1GB) write an EOF.
1845 if ((block->BlockNumber % 15000) == 0) {
1846 Pmsg0(-1, "Flush block, write EOF\n");
1847 flush_block(block, 0);
1851 /* Get out after writing 10 blocks to the second tape */
1852 if (++BlockNumber > 10 && stop != 0) { /* get out */
1857 Pmsg0(000, _("Not OK\n"));
1860 jcr->JobBytes += rec.data_len; /* increment bytes this job */
1861 Dmsg4(190, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
1862 FI_to_ascii(rec.FileIndex), rec.VolSessionId,
1863 stream_to_ascii(rec.Stream, rec.FileIndex), rec.data_len);
1865 /* Get out after writing 10 blocks to the second tape */
1866 if (BlockNumber > 10 && stop != 0) { /* get out */
1867 Pmsg0(-1, "Done writing ...\n");
1872 Dmsg0(100, "Write_end_session_label()\n");
1873 /* Create Job status for end of session label */
1874 if (!job_canceled(jcr) && ok) {
1875 set_jcr_job_status(jcr, JS_Terminated);
1877 set_jcr_job_status(jcr, JS_ErrorTerminated);
1879 if (!write_session_label(dcr, block, EOS_LABEL)) {
1880 Pmsg1(000, _("Error writting end session label. ERR=%s\n"), strerror_dev(dev));
1883 /* Write out final block of this session */
1884 if (!write_block_to_device(dcr, block)) {
1885 Pmsg0(-1, _("Set ok=false after write_block_to_device.\n"));
1888 Pmsg0(-1, _("Wrote End Of Session label.\n"));
1890 /* Save last block info for second tape */
1891 last_block_num2 = last_block_num;
1892 last_file2 = last_file;
1894 free_block(last_block2);
1896 last_block2 = dup_block(last_block);
1899 sprintf(buf, "%s/btape.state", working_directory);
1900 fd = open(buf, O_CREAT|O_TRUNC|O_WRONLY, 0640);
1902 write(fd, &btape_state_level, sizeof(btape_state_level));
1903 write(fd, &simple, sizeof(simple));
1904 write(fd, &last_block_num1, sizeof(last_block_num1));
1905 write(fd, &last_block_num2, sizeof(last_block_num2));
1906 write(fd, &last_file1, sizeof(last_file1));
1907 write(fd, &last_file2, sizeof(last_file2));
1908 write(fd, last_block1->buf, last_block1->buf_len);
1909 write(fd, last_block2->buf, last_block2->buf_len);
1910 write(fd, first_block->buf, first_block->buf_len);
1912 Pmsg2(-1, "Wrote state file last_block_num1=%d last_block_num2=%d\n",
1913 last_block_num1, last_block_num2);
1915 Pmsg2(-1, _("Could not create state file: %s ERR=%s\n"), buf,
1919 /* Release the device if multiple tapes being used */
1920 if (!simple && !release_device(jcr)) {
1921 Pmsg0(-1, _("Error in release_device\n"));
1925 Pmsg2(-1, _("\n\nDone filling tape%s. Now beginning re-read of %stape ...\n"),
1926 simple?"":"s", simple?"":"first ");
1930 dev->min_block_size = min_block_size;
1931 free_memory(rec.data);
1935 * Read two tapes written by the "fill" command and ensure
1936 * that the data is valid. If stop==1 we simulate full read back
1937 * of two tapes. If stop==-1 we simply read the last block and
1938 * verify that it is correct.
1940 static void unfillcmd()
1944 last_block1 = new_block(dev);
1945 last_block2 = new_block(dev);
1946 first_block = new_block(dev);
1947 sprintf(buf, "%s/btape.state", working_directory);
1948 fd = open(buf, O_RDONLY);
1950 uint32_t state_level;
1951 read(fd, &state_level, sizeof(btape_state_level));
1952 read(fd, &simple, sizeof(simple));
1953 read(fd, &last_block_num1, sizeof(last_block_num1));
1954 read(fd, &last_block_num2, sizeof(last_block_num2));
1955 read(fd, &last_file1, sizeof(last_file1));
1956 read(fd, &last_file2, sizeof(last_file2));
1957 read(fd, last_block1->buf, last_block1->buf_len);
1958 read(fd, last_block2->buf, last_block2->buf_len);
1959 read(fd, first_block->buf, first_block->buf_len);
1961 if (state_level != btape_state_level) {
1962 Pmsg0(-1, "\nThe state file level has changed. You must redo\n"
1963 "the fill command.\n");
1967 Pmsg2(-1, "\nCould not find the state file: %s ERR=%s\n"
1968 "You must redo the fill command.\n", buf, strerror(errno));
1975 static void do_unfill()
1983 block = new_block(dev);
1985 Dmsg0(20, "Enter do_unfill\n");
1986 dev->capabilities |= CAP_ANONVOLS; /* allow reading any volume */
1987 dev->capabilities &= ~CAP_LABEL; /* don't label anything here */
1991 time(&jcr->run_time); /* start counting time for rates */
1995 free_block(last_block);
1997 last_block_num = last_block_num1;
1998 last_file = last_file1;
1999 last_block = last_block1;
2002 /* Multiple Volume tape */
2003 /* Close device so user can use autochanger if desired */
2004 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2007 autochanger = autoload_device(dcr, 1, NULL);
2009 force_close_dev(dev);
2010 get_cmd(_("Mount first tape. Press enter when ready: "));
2013 jcr->dcr = new_dcr(jcr, dev);
2014 set_volume_name("TestVolume1", 1);
2016 create_vol_list(jcr);
2018 dev->state &= ~ST_READ;
2019 if (!acquire_device_for_read(jcr)) {
2020 Pmsg1(-1, "%s", dev->errmsg);
2025 * We now have the first tape mounted.
2026 * Note, re-reading last block may have caused us to
2027 * loose track of where we are (block number unknown).
2029 rewind_dev(dev); /* get to a known place on tape */
2030 /* Read the first 1000 records */
2031 Pmsg0(-1, _("Reading the first 1000 records.\n"));
2032 read_records(dcr, quickie_cb, my_mount_next_read_volume);
2033 Pmsg4(-1, _("Reposition from %u:%u to %u:%u\n"), dev->file, dev->block_num,
2034 last_file, last_block_num);
2035 if (!reposition_dev(dev, last_file, last_block_num)) {
2036 Pmsg1(-1, "Reposition error. ERR=%s\n", strerror_dev(dev));
2038 Pmsg1(-1, _("Reading block %u.\n"), last_block_num);
2039 if (!read_block_from_device(dcr, block, NO_BLOCK_NUMBER_CHECK)) {
2040 Pmsg1(-1, _("Error reading block: ERR=%s\n"), strerror_dev(dev));
2043 if (compare_blocks(last_block, block)) {
2045 Pmsg0(-1, _("\nThe last block on the tape matches. Test succeeded.\n\n"));
2047 Pmsg0(-1, _("\nThe last block of the first tape matches.\n\n"));
2054 /* restore info for last block on second Volume */
2055 last_block_num = last_block_num2;
2056 last_file = last_file2;
2057 last_block = last_block2;
2059 /* Multiple Volume tape */
2060 /* Close device so user can use autochanger if desired */
2061 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2066 set_volume_name("TestVolume2", 2);
2068 create_vol_list(jcr);
2069 autochanger = autoload_device(dcr, 1, NULL);
2071 force_close_dev(dev);
2072 get_cmd(_("Mount second tape. Press enter when ready: "));
2075 dev->state &= ~ST_READ;
2076 if (!acquire_device_for_read(jcr)) {
2077 Pmsg1(-1, "%s", dev->errmsg);
2081 /* Space to "first" block which is last block not written
2082 * on the previous tape.
2084 Pmsg2(-1, _("Reposition from %u:%u to 0:1\n"), dev->file, dev->block_num);
2085 if (!reposition_dev(dev, 0, 1)) {
2086 Pmsg1(-1, "Reposition error. ERR=%s\n", strerror_dev(dev));
2089 Pmsg1(-1, _("Reading block %d.\n"), dev->block_num);
2090 if (!read_block_from_device(dcr, block, NO_BLOCK_NUMBER_CHECK)) {
2091 Pmsg1(-1, _("Error reading block: ERR=%s\n"), strerror_dev(dev));
2094 if (compare_blocks(first_block, block)) {
2095 Pmsg0(-1, _("\nThe first block on the second tape matches.\n\n"));
2098 /* Now find and compare the last block */
2099 Pmsg4(-1, _("Reposition from %u:%u to %u:%u\n"), dev->file, dev->block_num,
2100 last_file, last_block_num);
2101 if (!reposition_dev(dev, last_file, last_block_num)) {
2102 Pmsg1(-1, "Reposition error. ERR=%s\n", strerror_dev(dev));
2105 Pmsg1(-1, _("Reading block %d.\n"), dev->block_num);
2106 if (!read_block_from_device(dcr, block, NO_BLOCK_NUMBER_CHECK)) {
2107 Pmsg1(-1, _("Error reading block: ERR=%s\n"), strerror_dev(dev));
2110 if (compare_blocks(last_block, block)) {
2111 Pmsg0(-1, _("\nThe last block on the second tape matches. Test succeeded.\n\n"));
2116 free_block(last_block1);
2117 free_block(last_block2);
2118 free_block(first_block);
2121 /* Read 1000 records then stop */
2122 static bool quickie_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
2125 return quickie_count <= 1000;
2128 static bool compare_blocks(DEV_BLOCK *last_block, DEV_BLOCK *block)
2131 uint32_t CheckSum, block_len;
2134 p = last_block->buf;
2136 unser_begin(q, BLKHDR2_LENGTH);
2137 unser_uint32(CheckSum);
2138 unser_uint32(block_len);
2139 while (q < (block->buf+block_len)) {
2146 dump_block(last_block, _("Last block written"));
2148 dump_block(block, _("Block read back"));
2149 Pmsg1(-1, "\n\nThe blocks differ at byte %u\n", p - last_block->buf);
2150 Pmsg0(-1, "\n\n!!!! The last block written and the block\n"
2151 "that was read back differ. The test FAILED !!!!\n"
2152 "This must be corrected before you use Bacula\n"
2153 "to write multi-tape Volumes.!!!!\n");
2157 dump_block(last_block, _("Last block written"));
2158 dump_block(block, _("Block read back"));
2168 * Write current block to tape regardless of whether or
2169 * not it is full. If the tape fills, attempt to
2170 * acquire another tape.
2172 static int flush_block(DEV_BLOCK *block, int dump)
2177 uint32_t this_file, this_block_num;
2180 this_block = new_block(dev);
2183 last_block = new_block(dev);
2186 this_file = dev->file;
2187 this_block_num = dev->block_num;
2188 if (!write_block_to_dev(dcr, block)) {
2189 Pmsg3(000, "Last block at: %u:%u this_dev_block_num=%d\n",
2190 last_file, last_block_num, this_block_num);
2193 * This is 1st tape, so save first tape info separate
2194 * from second tape info
2196 last_block_num1 = last_block_num;
2197 last_file1 = last_file;
2198 last_block1 = dup_block(last_block);
2199 last_block2 = dup_block(last_block);
2200 first_block = dup_block(block); /* first block second tape */
2203 Pmsg3(000, "Block not written: FileIndex=%u blk_block=%u Size=%u\n",
2204 (unsigned)file_index, block->BlockNumber, block->block_len);
2205 dump_block(last_block, "Last block written");
2207 dump_block(block, "Block not written");
2210 eot_block = block->BlockNumber;
2211 eot_block_len = block->block_len;
2212 eot_FileIndex = file_index;
2216 now -= jcr->run_time;
2218 now = 1; /* don't divide by zero */
2220 kbs = (double)dev->VolCatInfo.VolCatBytes / (1000 * now);
2221 vol_size = dev->VolCatInfo.VolCatBytes;
2222 Pmsg2(000, "End of tape. VolumeCapacity=%s. Write rate = %.1f KB/s\n",
2223 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), kbs);
2226 stop = -1; /* stop, but do simplified test */
2228 /* Full test in progress */
2229 if (!fixup_device_block_write_error(jcr->dcr, block)) {
2230 Pmsg1(000, _("Cannot fixup device error. %s\n"), strerror_dev(dev));
2235 BlockNumber = 0; /* start counting for second tape */
2238 return 1; /* end of tape reached */
2241 /* Save contents after write so that the header is serialized */
2242 memcpy(this_block->buf, block->buf, this_block->buf_len);
2245 * Toggle between two allocated blocks for efficiency.
2246 * Switch blocks so that the block just successfully written is
2247 * always in last_block.
2249 tblock = last_block;
2250 last_block = this_block;
2251 this_block = tblock;
2252 last_file = this_file;
2253 last_block_num = this_block_num;
2261 * First we label the tape, then we fill
2262 * it with data get a new tape and write a few blocks.
2264 static void qfillcmd()
2270 Pmsg0(0, "Test writing blocks of 64512 bytes to tape.\n");
2272 get_cmd("How many blocks do you want to write? (1000): ");
2279 sm_check(__FILE__, __LINE__, false);
2280 block = new_block(dev);
2283 i = block->buf_len - 100;
2285 rec->data = check_pool_memory_size(rec->data, i);
2286 memset(rec->data, i & 0xFF, i);
2289 Pmsg1(0, "Begin writing %d Bacula blocks to tape ...\n", count);
2290 for (i=0; i < count; i++) {
2295 if (!write_record_to_block(block, rec)) {
2296 Pmsg0(0, _("Error writing record to block.\n"));
2299 if (!write_block_to_dev(dcr, block)) {
2300 Pmsg0(0, _("Error writing block to device.\n"));
2306 if (dev_cap(dev, CAP_TWOEOF)) {
2313 sm_check(__FILE__, __LINE__, false);
2316 sm_check(__FILE__, __LINE__, false);
2321 * Fill a tape using raw write() command
2323 static void rawfill_cmd()
2328 uint32_t block_num = 0;
2333 block = new_block(dev);
2334 fd = open("/dev/urandom", O_RDONLY);
2336 read(fd, block->buf, block->buf_len);
2339 uint32_t *p = (uint32_t *)block->buf;
2340 srandom(time(NULL));
2341 for (i=0; i<block->buf_len/sizeof(uint32_t); i++) {
2345 p = (uint32_t *)block->buf;
2346 Pmsg1(0, "Begin writing raw blocks of %u bytes.\n", block->buf_len);
2349 stat = write(dev->fd, block->buf, block->buf_len);
2350 if (stat == (int)block->buf_len) {
2351 if ((block_num++ % 100) == 0) {
2356 for (i=1; i<(block->buf_len-sizeof(uint32_t))/sizeof(uint32_t)-1; i++) {
2365 printf("Write failed at block %u. stat=%d ERR=%s\n", block_num, stat,
2366 strerror(my_errno));
2373 * Fill a tape using raw write() command
2375 static void bfill_cmd()
2378 uint32_t block_num = 0;
2384 block = new_block(dev);
2385 fd = open("/dev/urandom", O_RDONLY);
2387 read(fd, block->buf, block->buf_len);
2390 uint32_t *p = (uint32_t *)block->buf;
2391 srandom(time(NULL));
2392 for (i=0; i<block->buf_len/sizeof(uint32_t); i++) {
2396 p = (uint32_t *)block->buf;
2397 Pmsg1(0, "Begin writing Bacula blocks of %u bytes.\n", block->buf_len);
2400 block->binbuf = block->buf_len;
2401 block->bufp = block->buf + block->binbuf;
2402 if (!write_block_to_dev(dcr, block)) {
2405 if ((block_num++ % 100) == 0) {
2410 for (i=1; i<(block->buf_len/sizeof(uint32_t)-1); i++) {
2416 printf("Write failed at block %u.\n", block_num);
2422 struct cmdstruct { const char *key; void (*func)(); const char *help; };
2423 static struct cmdstruct commands[] = {
2424 {"autochanger", autochangercmd, "test autochanger"},
2425 {"bsf", bsfcmd, "backspace file"},
2426 {"bsr", bsrcmd, "backspace record"},
2427 {"bfill", bfill_cmd, "fill tape using Bacula writes"},
2428 {"cap", capcmd, "list device capabilities"},
2429 {"clear", clearcmd, "clear tape errors"},
2430 {"eod", eodcmd, "go to end of Bacula data for append"},
2431 {"eom", eomcmd, "go to the physical end of medium"},
2432 {"fill", fillcmd, "fill tape, write onto second volume"},
2433 {"unfill", unfillcmd, "read filled tape"},
2434 {"fsf", fsfcmd, "forward space a file"},
2435 {"fsr", fsrcmd, "forward space a record"},
2436 {"help", helpcmd, "print this command"},
2437 {"label", labelcmd, "write a Bacula label to the tape"},
2438 {"load", loadcmd, "load a tape"},
2439 {"quit", quitcmd, "quit btape"},
2440 {"rawfill", rawfill_cmd, "use write() to fill tape"},
2441 {"readlabel", readlabelcmd, "read and print the Bacula tape label"},
2442 {"rectest", rectestcmd, "test record handling functions"},
2443 {"rewind", rewindcmd, "rewind the tape"},
2444 {"scan", scancmd, "read() tape block by block to EOT and report"},
2445 {"scanblocks", scan_blocks, "Bacula read block by block to EOT and report"},
2446 {"status", statcmd, "print tape status"},
2447 {"test", testcmd, "General test Bacula tape functions"},
2448 {"weof", weofcmd, "write an EOF on the tape"},
2449 {"wr", wrcmd, "write a single Bacula block"},
2450 {"rr", rrcmd, "read a single record"},
2451 {"qfill", qfillcmd, "quick fill command"},
2453 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
2461 while (get_cmd("*")) {
2462 sm_check(__FILE__, __LINE__, false);
2464 parse_args(cmd, &args, &argc, argk, argv, MAX_CMD_ARGS);
2465 for (i=0; i<comsize; i++) /* search for command */
2466 if (argc > 0 && fstrsch(argk[0], commands[i].key)) {
2467 (*commands[i].func)(); /* go execute command */
2472 Pmsg1(0, _("%s is an illegal command\n"), cmd);
2478 static void helpcmd()
2482 printf(_("Interactive commands:\n"));
2483 printf(_(" Command Description\n ======= ===========\n"));
2484 for (i=0; i<comsize; i++)
2485 printf(" %-10s %s\n", commands[i].key, commands[i].help);
2492 "\nVersion: " VERSION " (" BDATE ")\n\n"
2493 "Usage: btape <options> <device_name>\n"
2494 " -b <file> specify bootstrap file\n"
2495 " -c <file> set configuration file to file\n"
2496 " -d <nn> set debug level to nn\n"
2497 " -p proceed inspite of I/O errors\n"
2498 " -s turn off signals\n"
2500 " -? print this message.\n"
2506 * Get next input command from terminal. This
2507 * routine is REALLY primitive, and should be enhanced
2508 * to have correct backspacing, etc.
2511 get_cmd(const char *prompt)
2515 fprintf(stdout, prompt);
2517 /* We really should turn off echoing and pretty this
2521 while ((ch = fgetc(stdin)) != EOF) {
2523 strip_trailing_junk(cmd);
2525 } else if (ch == 4 || ch == 0xd3 || ch == 0x8) {
2538 /* Dummies to replace askdir.c */
2539 bool dir_update_file_attributes(DCR *dcr, DEV_RECORD *rec) { return 1;}
2540 bool dir_send_job_status(JCR *jcr) {return 1;}
2542 bool dir_update_volume_info(DCR *dcr, bool relabel)
2548 bool dir_get_volume_info(DCR *dcr, enum get_vol_info_rw writing)
2550 JCR *jcr = dcr->jcr;
2551 Dmsg0(20, "Enter dir_get_volume_info\n");
2552 bstrncpy(jcr->VolCatInfo.VolCatName, jcr->VolumeName, sizeof(jcr->VolCatInfo.VolCatName));
2556 bool dir_create_jobmedia_record(DCR *dcr)
2558 dcr->WroteVol = false;
2563 bool dir_find_next_appendable_volume(DCR *dcr)
2565 JCR *jcr = dcr->jcr;
2566 Dmsg1(20, "Enter dir_find_next_appendable_volume. stop=%d\n", stop);
2567 return jcr->VolumeName[0] != 0;
2570 bool dir_ask_sysop_to_mount_volume(DCR *dcr)
2572 DEVICE *dev = dcr->dev;
2573 JCR *jcr = dcr->jcr;
2574 Dmsg0(20, "Enter dir_ask_sysop_to_mount_volume\n");
2575 if (jcr->VolumeName[0] == 0) {
2576 return dir_ask_sysop_to_create_appendable_volume(dcr);
2578 /* Close device so user can use autochanger if desired */
2579 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2582 force_close_dev(dev);
2583 Pmsg1(-1, "%s", dev->errmsg); /* print reason */
2584 if (jcr->VolumeName[0] == 0 || strcmp(jcr->VolumeName, "TestVolume2") == 0) {
2585 fprintf(stderr, "Mount second Volume on device %s and press return when ready: ",
2588 fprintf(stderr, "Mount Volume \"%s\" on device %s and press return when ready: ",
2589 jcr->VolumeName, dev_name(dev));
2595 bool dir_ask_sysop_to_create_appendable_volume(DCR *dcr)
2598 DEVICE *dev = dcr->dev;
2599 JCR *jcr = dcr->jcr;
2600 Dmsg0(20, "Enter dir_ask_sysop_to_create_appendable_volume\n");
2602 set_volume_name("TestVolume1", 1);
2604 set_volume_name("TestVolume2", 2);
2606 /* Close device so user can use autochanger if desired */
2607 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2610 autochanger = autoload_device(dcr, 1, NULL);
2612 force_close_dev(dev);
2613 fprintf(stderr, "Mount blank Volume on device %s and press return when ready: ",
2617 open_device(jcr, dev);
2624 static bool my_mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
2628 Dmsg0(20, "Enter my_mount_next_read_volume\n");
2629 Pmsg1(000, "End of Volume \"%s\"\n", jcr->VolumeName);
2631 if (LastBlock != block->BlockNumber) {
2632 VolBytes += block->block_len;
2634 LastBlock = block->BlockNumber;
2636 now -= jcr->run_time;
2640 kbs = (double)VolBytes / (1000.0 * (double)now);
2641 Pmsg3(-1, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
2642 edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
2644 if (strcmp(jcr->VolumeName, "TestVolume2") == 0) {
2650 set_volume_name("TestVolume2", 2);
2652 create_vol_list(jcr);
2654 dev->state &= ~ST_READ;
2655 if (!acquire_device_for_read(jcr)) {
2656 Pmsg2(0, "Cannot open Dev=%s, Vol=%s\n", dev_name(dev), jcr->VolumeName);
2659 return true; /* next volume mounted */
2662 static void set_volume_name(const char *VolName, int volnum)
2664 DCR *dcr = jcr->dcr;
2665 VolumeName = VolName;
2667 pm_strcpy(&jcr->VolumeName, VolName);
2668 bstrncpy(dev->VolCatInfo.VolCatName, VolName, sizeof(dev->VolCatInfo.VolCatName));
2669 bstrncpy(jcr->VolCatInfo.VolCatName, VolName, sizeof(jcr->VolCatInfo.VolCatName));
2670 bstrncpy(dcr->VolCatInfo.VolCatName, VolName, sizeof(dcr->VolCatInfo.VolCatName));
2671 bstrncpy(dcr->VolumeName, VolName, sizeof(dcr->VolumeName));
2672 jcr->VolCatInfo.Slot = volnum;
2673 dcr->VolCatInfo.Slot = volnum;