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];
50 * If you change the format of the state file,
51 * increment this value
53 static uint32_t btape_state_level = 1;
56 DEVRES *device = NULL;
59 /* Forward referenced subroutines */
60 static void do_tape_cmds();
61 static void helpcmd();
62 static void scancmd();
63 static void rewindcmd();
64 static void clearcmd();
68 static void fillcmd();
69 static void qfillcmd();
70 static void statcmd();
71 static void unfillcmd();
72 static int flush_block(DEV_BLOCK *block, int dump);
73 static int record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec);
74 static int my_mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block);
75 static void scan_blocks();
76 static void set_volume_name(char *VolName, int volnum);
77 static void rawfill_cmd();
78 static void bfill_cmd();
79 static bool open_the_device();
80 static char *edit_device_codes(JCR *jcr, char *omsg, char *imsg, char *cmd);
81 static void autochangercmd();
82 static void do_unfill();
85 /* Static variables */
86 #define CONFIG_FILE "bacula-sd.conf"
89 #define MAX_CMD_ARGS 30
92 static char *argk[MAX_CMD_ARGS];
93 static char *argv[MAX_CMD_ARGS];
96 static BSR *bsr = NULL;
97 static int signals = TRUE;
100 static uint64_t vol_size;
101 static uint64_t VolBytes;
104 static int32_t file_index;
105 static int end_of_tape = 0;
106 static uint32_t LastBlock = 0;
107 static uint32_t eot_block;
108 static uint32_t eot_block_len;
109 static uint32_t eot_FileIndex;
110 static int dumped = 0;
111 static DEV_BLOCK *last_block = NULL;
112 static DEV_BLOCK *this_block = NULL;
113 static uint32_t last_file = 0;
114 static uint32_t last_block_num = 0;
115 static uint32_t BlockNumber = 0;
116 static int simple = FALSE;
118 static char *VolumeName = NULL;
121 static JCR *jcr = NULL;
125 static void terminate_btape(int sig);
126 int get_cmd(char *prompt);
129 /*********************************************************************
131 * Main Bacula Pool Creation Program
134 int main(int margc, char *margv[])
139 if (TAPE_BSIZE % DEV_BSIZE != 0 || TAPE_BSIZE / DEV_BSIZE == 0) {
140 Emsg2(M_ABORT, 0, "Tape block size (%d) not multiple of system size (%d)\n",
141 TAPE_BSIZE, DEV_BSIZE);
143 if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
144 Emsg1(M_ABORT, 0, "Tape block size (%d) is not a power of 2\n", TAPE_BSIZE);
147 printf("Tape block granularity is %d bytes.\n", TAPE_BSIZE);
149 working_directory = "/tmp";
150 my_name_is(margc, margv, "btape");
151 init_msg(NULL, NULL);
153 while ((ch = getopt(margc, margv, "b:c:d:sv?")) != -1) {
155 case 'b': /* bootstrap file */
156 bsr = parse_bsr(NULL, optarg);
157 // dump_bsr(bsr, true);
160 case 'c': /* specify config file */
161 if (configfile != NULL) {
164 configfile = bstrdup(optarg);
167 case 'd': /* set debug level */
168 debug_level = atoi(optarg);
169 if (debug_level <= 0) {
192 cmd = get_pool_memory(PM_FNAME);
193 args = get_pool_memory(PM_FNAME);
196 init_signals(terminate_btape);
199 if (configfile == NULL) {
200 configfile = bstrdup(CONFIG_FILE);
203 daemon_start_time = time(NULL);
205 parse_config(configfile);
208 /* See if we can open a device */
210 Pmsg0(000, "No archive name specified.\n");
213 } else if (margc != 1) {
214 Pmsg0(000, "Improper number of arguments specified.\n");
219 jcr = setup_jcr("btape", margv[0], bsr, NULL);
220 dev = setup_to_access_device(jcr, 0); /* acquire for write */
224 dev->max_volume_size = 0;
225 if (!open_the_device()) {
229 Dmsg0(200, "Do tape commands\n");
237 static void terminate_btape(int stat)
240 sm_check(__FILE__, __LINE__, False);
244 free_config_resources();
246 free_pool_memory(args);
250 free_pool_memory(cmd);
258 if (debug_level > 10)
259 print_memory_pool_stats();
269 free_block(last_block);
272 free_block(this_block);
276 close_memory_pool(); /* free memory in pool */
282 static bool open_the_device()
286 block = new_block(dev);
288 if (!(dev->state & ST_OPENED)) {
289 Dmsg1(200, "Opening device %s\n", jcr->VolumeName);
290 if (open_dev(dev, jcr->VolumeName, READ_WRITE) < 0) {
291 Emsg1(M_FATAL, 0, _("dev open failed: %s\n"), dev->errmsg);
297 Dmsg1(000, "open_dev %s OK\n", dev_name(dev));
310 * Write a label to the tape
312 static void labelcmd()
315 pm_strcpy(&cmd, VolumeName);
317 if (!get_cmd("Enter Volume Name: ")) {
322 if (!(dev->state & ST_OPENED)) {
323 if (!open_device(dev)) {
324 Pmsg1(0, "Device open failed. ERR=%s\n", strerror_dev(dev));
327 write_volume_label_to_dev(jcr, jcr->device, cmd, "Default");
331 * Read the tape label
333 static void readlabelcmd()
335 int save_debug_level = debug_level;
339 block = new_block(dev);
340 stat = read_dev_volume_label(jcr, dev, block);
343 Pmsg0(0, "Volume has no label.\n");
346 Pmsg0(0, "Volume label read correctly.\n");
349 Pmsg1(0, "I/O error on device: ERR=%s", strerror_dev(dev));
352 Pmsg0(0, "Volume name error\n");
354 case VOL_CREATE_ERROR:
355 Pmsg1(0, "Error creating label. ERR=%s", strerror_dev(dev));
357 case VOL_VERSION_ERROR:
358 Pmsg0(0, "Volume version error.\n");
360 case VOL_LABEL_ERROR:
361 Pmsg0(0, "Bad Volume label type.\n");
364 Pmsg0(0, "Unknown error.\n");
369 dump_volume_label(dev);
370 debug_level = save_debug_level;
376 * Load the tape should have prevously been taken
377 * off line, otherwise this command is not necessary.
379 static void loadcmd()
382 if (!load_dev(dev)) {
383 Pmsg1(0, "Bad status from load. ERR=%s\n", strerror_dev(dev));
385 Pmsg1(0, "Loaded %s\n", dev_name(dev));
391 static void rewindcmd()
393 if (!rewind_dev(dev)) {
394 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
395 clrerror_dev(dev, -1);
397 Pmsg1(0, "Rewound %s\n", dev_name(dev));
402 * Clear any tape error
404 static void clearcmd()
406 clrerror_dev(dev, -1);
410 * Write and end of file on the tape
412 static void weofcmd()
423 if ((stat = weof_dev(dev, num)) < 0) {
424 Pmsg2(0, "Bad status from weof %d. ERR=%s\n", stat, strerror_dev(dev));
427 Pmsg3(0, "Wrote %d EOF%s to %s\n", num, num==1?"":"s", dev_name(dev));
432 /* Go to the end of the medium -- raw command
433 * The idea was orginally that the end of the Bacula
434 * medium would be flagged differently. This is not
435 * currently the case. So, this is identical to the
441 Pmsg1(0, "%s", strerror_dev(dev));
444 Pmsg0(0, _("Moved to end of medium.\n"));
449 * Go to the end of the medium (either hardware determined
450 * or defined by two eofs.
470 if (!bsf_dev(dev, num)) {
471 Pmsg1(0, _("Bad status from bsf. ERR=%s\n"), strerror_dev(dev));
473 Pmsg2(0, _("Backspaced %d file%s.\n"), num, num==1?"":"s");
489 if (!bsr_dev(dev, num)) {
490 Pmsg1(0, _("Bad status from bsr. ERR=%s\n"), strerror_dev(dev));
492 Pmsg2(0, _("Backspaced %d record%s.\n"), num, num==1?"":"s");
497 * List device capabilities as defined in the
502 printf(_("Configured device capabilities:\n"));
503 printf("%sEOF ", dev->capabilities & CAP_EOF ? "" : "!");
504 printf("%sBSR ", dev->capabilities & CAP_BSR ? "" : "!");
505 printf("%sBSF ", dev->capabilities & CAP_BSF ? "" : "!");
506 printf("%sFSR ", dev->capabilities & CAP_FSR ? "" : "!");
507 printf("%sFSF ", dev->capabilities & CAP_FSF ? "" : "!");
508 printf("%sFASTFSF ", dev->capabilities & CAP_FASTFSF ? "" : "!");
509 printf("%sEOM ", dev->capabilities & CAP_EOM ? "" : "!");
510 printf("%sREM ", dev->capabilities & CAP_REM ? "" : "!");
511 printf("%sRACCESS ", dev->capabilities & CAP_RACCESS ? "" : "!");
512 printf("%sAUTOMOUNT ", dev->capabilities & CAP_AUTOMOUNT ? "" : "!");
513 printf("%sLABEL ", dev->capabilities & CAP_LABEL ? "" : "!");
514 printf("%sANONVOLS ", dev->capabilities & CAP_ANONVOLS ? "" : "!");
515 printf("%sALWAYSOPEN ", dev->capabilities & CAP_ALWAYSOPEN ? "" : "!");
518 printf(_("Device status:\n"));
519 printf("%sOPENED ", dev->state & ST_OPENED ? "" : "!");
520 printf("%sTAPE ", dev->state & ST_TAPE ? "" : "!");
521 printf("%sLABEL ", dev->state & ST_LABEL ? "" : "!");
522 printf("%sMALLOC ", dev->state & ST_MALLOC ? "" : "!");
523 printf("%sAPPEND ", dev->state & ST_APPEND ? "" : "!");
524 printf("%sREAD ", dev->state & ST_READ ? "" : "!");
525 printf("%sEOT ", dev->state & ST_EOT ? "" : "!");
526 printf("%sWEOT ", dev->state & ST_WEOT ? "" : "!");
527 printf("%sEOF ", dev->state & ST_EOF ? "" : "!");
528 printf("%sNEXTVOL ", dev->state & ST_NEXTVOL ? "" : "!");
529 printf("%sSHORT ", dev->state & ST_SHORT ? "" : "!");
532 printf(_("Device parameters:\n"));
533 printf("Device name: %s\n", dev->dev_name);
534 printf("File=%u block=%u\n", dev->file, dev->block_num);
535 printf("Min block=%u Max block=%u\n", dev->min_block_size, dev->max_block_size);
543 * Test writting larger and larger records.
544 * This is a torture test for records.
546 static void rectestcmd()
552 Pmsg0(0, "Test writting larger and larger records.\n"
553 "This is a torture test for records.\nI am going to write\n"
554 "larger and larger records. It will stop when the record size\n"
555 "plus the header exceeds the block size (by default about 64K)\n");
558 get_cmd("Do you want to continue? (y/n): ");
560 Pmsg0(000, "Command aborted.\n");
564 sm_check(__FILE__, __LINE__, False);
565 block = new_block(dev);
568 for (i=1; i<500000; i++) {
569 rec->data = check_pool_memory_size(rec->data, i);
570 memset(rec->data, i & 0xFF, i);
572 sm_check(__FILE__, __LINE__, False);
573 if (write_record_to_block(block, rec)) {
576 Pmsg2(0, "Block %d i=%d\n", blkno, i);
580 sm_check(__FILE__, __LINE__, False);
584 sm_check(__FILE__, __LINE__, False);
588 * This test attempts to re-read a block written by Bacula
589 * normally at the end of the tape. Bacula will then back up
590 * over the two eof marks, backup over the record and reread
591 * it to make sure it is valid. Bacula can skip this validation
592 * if you set "Backward space record = no"
594 static int re_read_block_test()
601 if (!(dev->capabilities & CAP_BSR)) {
602 Pmsg0(-1, _("Skipping read backwards test because BSR turned off.\n"));
606 Pmsg0(-1, _("\n=== Write, backup, and re-read test ===\n\n"
607 "I'm going to write three records and an EOF\n"
608 "then backup over the EOF and re-read the last record.\n"
609 "Bacula does this after writing the last block on the\n"
610 "tape to verify that the block was written correctly.\n\n"
611 "This is not an *essential* feature ...\n\n"));
613 block = new_block(dev);
615 rec->data = check_pool_memory_size(rec->data, block->buf_len);
616 len = rec->data_len = block->buf_len-100;
617 memset(rec->data, 1, rec->data_len);
618 if (!write_record_to_block(block, rec)) {
619 Pmsg0(0, _("Error writing record to block.\n"));
622 if (!write_block_to_dev(jcr, dev, block)) {
623 Pmsg0(0, _("Error writing block to device.\n"));
626 Pmsg1(0, _("Wrote first record of %d bytes.\n"), rec->data_len);
628 memset(rec->data, 2, rec->data_len);
629 if (!write_record_to_block(block, rec)) {
630 Pmsg0(0, _("Error writing record to block.\n"));
633 if (!write_block_to_dev(jcr, dev, block)) {
634 Pmsg0(0, _("Error writing block to device.\n"));
637 Pmsg1(0, _("Wrote second record of %d bytes.\n"), rec->data_len);
639 memset(rec->data, 3, rec->data_len);
640 if (!write_record_to_block(block, rec)) {
641 Pmsg0(0, _("Error writing record to block.\n"));
644 if (!write_block_to_dev(jcr, dev, block)) {
645 Pmsg0(0, _("Error writing block to device.\n"));
648 Pmsg1(0, _("Wrote third record of %d bytes.\n"), rec->data_len);
651 if (dev_cap(dev, CAP_TWOEOF)) {
654 if (!bsf_dev(dev, 1)) {
655 Pmsg1(0, _("Backspace file failed! ERR=%s\n"), strerror_dev(dev));
658 if (dev_cap(dev, CAP_TWOEOF)) {
659 if (!bsf_dev(dev, 1)) {
660 Pmsg1(0, _("Backspace file failed! ERR=%s\n"), strerror_dev(dev));
664 Pmsg0(0, "Backspaced over EOF OK.\n");
665 if (!bsr_dev(dev, 1)) {
666 Pmsg1(0, _("Backspace record failed! ERR=%s\n"), strerror_dev(dev));
669 Pmsg0(0, "Backspace record OK.\n");
670 if (!read_block_from_dev(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
671 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
674 memset(rec->data, 0, rec->data_len);
675 if (!read_record_from_block(block, rec)) {
676 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
679 for (int i=0; i<len; i++) {
680 if (rec->data[i] != 3) {
681 Pmsg0(0, _("Bad data in record. Test failed!\n"));
685 Pmsg0(0, _("\nBlock re-read correct. Test succeeded!\n"));
686 Pmsg0(-1, _("=== End Write, backup, and re-read test ===\n\n"));
694 Pmsg0(0, _("This is not terribly serious since Bacula only uses\n"
695 "this function to verify the last block written to the\n"
696 "tape. Bacula will skip the last block verification\n"
698 "Backward Space Record = No\n\n"
699 "to your Storage daemon's Device resource definition.\n"));
706 * This test writes Bacula blocks to the tape in
707 * several files. It then rewinds the tape and attepts
708 * to read these blocks back checking the data.
710 static int write_read_test()
717 Pmsg0(-1, _("\n=== Write, rewind, and re-read test ===\n\n"
718 "I'm going to write 10 records and an EOF\n"
719 "then write 10 records and an EOF, then rewind,\n"
720 "and re-read the data to verify that it is correct.\n\n"
721 "This is an *essential* feature ...\n\n"));
722 block = new_block(dev);
724 if (!rewind_dev(dev)) {
725 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
728 rec->data = check_pool_memory_size(rec->data, block->buf_len);
729 len = rec->data_len = block->buf_len-100;
730 for (i=1; i<=10; i++) {
731 memset(rec->data, i, rec->data_len);
732 if (!write_record_to_block(block, rec)) {
733 Pmsg0(0, _("Error writing record to block.\n"));
736 if (!write_block_to_dev(jcr, dev, block)) {
737 Pmsg0(0, _("Error writing block to device.\n"));
741 Pmsg1(0, _("Wrote 10 blocks of %d bytes.\n"), rec->data_len);
743 for (i=11; i<=20; i++) {
744 memset(rec->data, i, rec->data_len);
745 if (!write_record_to_block(block, rec)) {
746 Pmsg0(0, _("Error writing record to block.\n"));
749 if (!write_block_to_dev(jcr, dev, block)) {
750 Pmsg0(0, _("Error writing block to device.\n"));
754 Pmsg1(0, _("Wrote 10 blocks of %d bytes.\n"), rec->data_len);
756 if (dev_cap(dev, CAP_TWOEOF)) {
759 if (!rewind_dev(dev)) {
760 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
763 Pmsg0(0, "Rewind OK.\n");
765 for (i=1; i<=20; i++) {
767 if (!read_block_from_dev(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
768 if (dev_state(dev, ST_EOF)) {
769 Pmsg0(-1, _("Got EOF on tape.\n"));
772 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
775 memset(rec->data, 0, rec->data_len);
776 if (!read_record_from_block(block, rec)) {
777 Pmsg1(0, _("Read record failed! ERR=%s\n"), strerror(dev->dev_errno));
780 for (j=0; j<len; j++) {
781 if (rec->data[j] != i) {
782 Pmsg3(0, _("Bad data in record. Expected %d, got %d at byte %d. Test failed!\n"),
787 if (i == 10 || i == 20) {
788 Pmsg0(-1, _("10 blocks re-read correctly.\n"));
791 Pmsg0(-1, _("=== Test Succeeded. End Write, rewind, and re-read test ===\n\n"));
801 * This test writes Bacula blocks to the tape in
802 * several files. It then rewinds the tape and attepts
803 * to read these blocks back checking the data.
805 static int position_test()
812 Pmsg0(-1, _("\n=== Write, rewind, and position test ===\n\n"
813 "I'm going to write 10 records and an EOF\n"
814 "then write 10 records and an EOF, then rewind,\n"
815 "and position to a few blocks and verify that it is correct.\n\n"
816 "This is an *essential* feature ...\n\n"));
817 block = new_block(dev);
819 if (!rewind_dev(dev)) {
820 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
823 rec->data = check_pool_memory_size(rec->data, block->buf_len);
824 len = rec->data_len = block->buf_len-100;
825 for (i=1; i<=10; i++) {
826 memset(rec->data, i, rec->data_len);
827 if (!write_record_to_block(block, rec)) {
828 Pmsg0(0, _("Error writing record to block.\n"));
831 if (!write_block_to_dev(jcr, dev, block)) {
832 Pmsg0(0, _("Error writing block to device.\n"));
836 Pmsg1(0, _("Wrote 10 blocks of %d bytes.\n"), rec->data_len);
838 for (i=11; i<=20; i++) {
839 memset(rec->data, i, rec->data_len);
840 if (!write_record_to_block(block, rec)) {
841 Pmsg0(0, _("Error writing record to block.\n"));
844 if (!write_block_to_dev(jcr, dev, block)) {
845 Pmsg0(0, _("Error writing block to device.\n"));
849 Pmsg1(0, _("Wrote 10 blocks of %d bytes.\n"), rec->data_len);
851 if (dev_cap(dev, CAP_TWOEOF)) {
854 if (!rewind_dev(dev)) {
855 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
858 Pmsg0(0, "Rewind OK.\n");
861 for (i=1; i<=20; i++) {
862 if (i != 5 && i != 17) {
866 Pmsg0(-1, "Reposition to file:block 0:4\n");
867 if (!reposition_dev(dev, 0, 4)) {
868 Pmsg0(0, "Reposition error.\n");
872 Pmsg0(-1, "Reposition to file:block 1:6\n");
873 if (!reposition_dev(dev, 1, 6)) {
874 Pmsg0(0, "Reposition error.\n");
879 if (!read_block_from_dev(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
880 if (dev_state(dev, ST_EOF)) {
881 Pmsg0(-1, _("Got EOF on tape.\n"));
884 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
887 memset(rec->data, 0, rec->data_len);
888 if (!read_record_from_block(block, rec)) {
889 Pmsg1(0, _("Read record failed! ERR=%s\n"), strerror(dev->dev_errno));
892 for (j=0; j<len; j++) {
893 if (rec->data[j] != i) {
894 Pmsg3(0, _("Bad data in record. Expected %d, got %d at byte %d. Test failed!\n"),
899 Pmsg0(-1, _("Block re-read correctly.\n"));
901 Pmsg0(-1, _("=== Test Succeeded. End Write, rewind, and re-read test ===\n\n"));
914 * This test writes some records, then writes an end of file,
915 * rewinds the tape, moves to the end of the data and attepts
916 * to append to the tape. This function is essential for
917 * Bacula to be able to write multiple jobs to the tape.
919 static int append_test()
921 Pmsg0(-1, _("\n\n=== Append files test ===\n\n"
922 "This test is essential to Bacula.\n\n"
923 "I'm going to write one record in file 0,\n"
924 " two records in file 1,\n"
925 " and three records in file 2\n\n"));
929 weofcmd(); /* end file 0 */
932 weofcmd(); /* end file 1 */
936 weofcmd(); /* end file 2 */
937 if (dev_cap(dev, CAP_TWOEOF)) {
941 Pmsg0(0, _("Now moving to end of medium.\n"));
943 Pmsg2(-1, _("We should be in file 3. I am at file %d. This is %s\n"),
944 dev->file, dev->file == 3 ? "correct!" : "NOT correct!!!!");
946 if (dev->file != 3) {
950 Pmsg0(-1, _("\nNow the important part, I am going to attempt to append to the tape.\n\n"));
953 if (dev_cap(dev, CAP_TWOEOF)) {
957 Pmsg0(-1, _("Done appending, there should be no I/O errors\n\n"));
958 Pmsg0(-1, "Doing Bacula scan of blocks:\n");
960 Pmsg0(-1, _("End scanning the tape.\n"));
961 Pmsg2(-1, _("We should be in file 4. I am at file %d. This is %s\n"),
962 dev->file, dev->file == 4 ? "correct!" : "NOT correct!!!!");
964 if (dev->file != 4) {
972 * This test exercises the autochanger
974 static int autochanger_test()
976 POOLMEM *results, *changer;
977 int slot, status, loaded;
981 if (!dev_cap(dev, CAP_AUTOCHANGER)) {
984 if (!(jcr->device && jcr->device->changer_name && jcr->device->changer_command)) {
985 Pmsg0(-1, "\nAutochanger enabled, but no name or no command device specified.\n");
989 Pmsg0(-1, "\nTo test the autochanger you must have a blank tape in Slot 1.\n"
990 "I'm going to write on it.\n");
991 if (!get_cmd("\nDo you wish to continue with the Autochanger test? (y/n): ")) {
994 if (cmd[0] != 'y' && cmd[0] != 'Y') {
998 Pmsg0(-1, _("\n\n=== Autochanger test ===\n\n"));
1000 results = get_pool_memory(PM_MESSAGE);
1001 changer = get_pool_memory(PM_FNAME);
1005 jcr->VolCatInfo.Slot = slot;
1006 /* Find out what is loaded, zero means device is unloaded */
1007 Pmsg0(-1, _("3301 Issuing autochanger \"loaded\" command.\n"));
1008 changer = edit_device_codes(jcr, changer, jcr->device->changer_command,
1010 status = run_program(changer, timeout, results);
1011 Dmsg3(100, "run_prog: %s stat=%d result=%s\n", changer, status, results);
1013 loaded = atoi(results);
1015 Pmsg1(-1, _("3991 Bad autochanger \"load slot\" status=%d.\n"), status);
1016 loaded = -1; /* force unload */
1020 Pmsg1(-1, "Slot %d loaded. I am going to unload it.\n", loaded);
1022 Pmsg0(-1, "Nothing loaded into the drive. OK.\n");
1024 Dmsg1(100, "Results from loaded query=%s\n", results);
1026 offline_or_rewind_dev(dev);
1027 /* We are going to load a new tape, so close the device */
1028 force_close_dev(dev);
1029 Pmsg0(-1, _("3302 Issuing autochanger \"unload\" command.\n"));
1030 changer = edit_device_codes(jcr, changer,
1031 jcr->device->changer_command, "unload");
1032 status = run_program(changer, timeout, NULL);
1033 Pmsg2(-1, "unload status=%s %d\n", status==0?"OK":"Bad", status);
1039 Pmsg1(-1, _("3303 Issuing autochanger \"load slot %d\" command.\n"), slot);
1040 changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "load");
1041 Dmsg1(200, "Changer=%s\n", changer);
1042 status = run_program(changer, timeout, NULL);
1044 Pmsg1(-1, _("3304 Autochanger \"load slot %d\" status is OK.\n"), slot);
1046 Pmsg1(-1, _("3992 Bad autochanger \"load slot\" status=%d.\n"), status);
1050 if (!open_the_device()) {
1053 bmicrosleep(sleep_time, 0);
1054 if (!rewind_dev(dev)) {
1055 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
1056 clrerror_dev(dev, -1);
1057 Pmsg0(-1, "\nThe test failed, probably because you need to put\n"
1058 "a longer sleep time in the mtx-script in the load) case.\n"
1059 "Adding a 30 second sleep and trying again ...\n");
1063 Pmsg1(0, "Rewound %s\n", dev_name(dev));
1066 if ((status = weof_dev(dev, 1)) < 0) {
1067 Pmsg2(0, "Bad status from weof %d. ERR=%s\n", status, strerror_dev(dev));
1070 Pmsg1(0, "Wrote EOF to %s\n", dev_name(dev));
1074 Pmsg1(-1, "\nThe test worked this time. Please add:\n\n"
1076 "to your mtx-changer script in the load) case.\n\n",
1079 Pmsg0(-1, "\nThe test autochanger worked!!\n\n");
1082 free_pool_memory(changer);
1083 free_pool_memory(results);
1088 free_pool_memory(changer);
1089 free_pool_memory(results);
1090 Pmsg0(-1, "You must correct this error or the Autochanger will not work.\n");
1094 static void autochangercmd()
1101 * This test assumes that the append test has been done,
1102 * then it tests the fsf function.
1104 static int fsf_test()
1106 bool set_off = false;
1108 Pmsg0(-1, _("\n\n=== Forward space files test ===\n\n"
1109 "This test is essential to Bacula.\n\n"
1110 "I'm going to write five files then test forward spacing\n\n"));
1114 weofcmd(); /* end file 0 */
1117 weofcmd(); /* end file 1 */
1121 weofcmd(); /* end file 2 */
1124 weofcmd(); /* end file 3 */
1126 weofcmd(); /* end file 4 */
1127 if (dev_cap(dev, CAP_TWOEOF)) {
1133 Pmsg0(0, _("Now forward spacing 1 file.\n"));
1134 if (!fsf_dev(dev, 1)) {
1135 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1138 Pmsg2(-1, _("We should be in file 1. I am at file %d. This is %s\n"),
1139 dev->file, dev->file == 1 ? "correct!" : "NOT correct!!!!");
1141 if (dev->file != 1) {
1145 Pmsg0(0, _("Now forward spacing 2 files.\n"));
1146 if (!fsf_dev(dev, 2)) {
1147 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1150 Pmsg2(-1, _("We should be in file 3. I am at file %d. This is %s\n"),
1151 dev->file, dev->file == 3 ? "correct!" : "NOT correct!!!!");
1153 if (dev->file != 3) {
1158 Pmsg0(0, _("Now forward spacing 4 files.\n"));
1159 if (!fsf_dev(dev, 4)) {
1160 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1163 Pmsg2(-1, _("We should be in file 4. I am at file %d. This is %s\n"),
1164 dev->file, dev->file == 4 ? "correct!" : "NOT correct!!!!");
1166 if (dev->file != 4) {
1170 Pmsg0(-1, "The test worked this time. Please add:\n\n"
1171 " Fast Forward Space File = no\n\n"
1172 "to your Device resource for this drive.\n");
1176 Pmsg0(0, _("Now forward spacing 1 more file.\n"));
1177 if (!fsf_dev(dev, 1)) {
1178 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1180 Pmsg2(-1, _("We should be in file 5. I am at file %d. This is %s\n"),
1181 dev->file, dev->file == 5 ? "correct!" : "NOT correct!!!!");
1182 if (dev->file != 5) {
1189 Pmsg0(-1, _("\nThe forward space file test failed.\n"));
1190 if (dev_cap(dev, CAP_FASTFSF)) {
1191 Pmsg0(-1, "You have Fast Forward Space File enabled.\n"
1192 "I am turning it off then retrying the test.\n");
1193 dev->capabilities &= ~CAP_FASTFSF;
1197 Pmsg0(-1, "You must correct this error or Bacula will not work.\n");
1206 * This is a general test of Bacula's functions
1207 * needed to read and write the tape.
1209 static void testcmd()
1213 if (!write_read_test()) {
1216 if (!position_test()) {
1220 stat = append_test();
1221 if (stat == 1) { /* OK get out */
1224 if (stat == -1) { /* first test failed */
1225 if (dev_cap(dev, CAP_EOM) || dev_cap(dev, CAP_FASTFSF)) {
1226 Pmsg0(-1, "\nAppend test failed. Attempting again.\n"
1227 "Setting \"Hardware End of Medium = no\n"
1228 " and \"Fast Forward Space File = no\n"
1229 "and retrying append test.\n\n");
1230 dev->capabilities &= ~CAP_EOM; /* turn off eom */
1231 dev->capabilities &= ~CAP_FASTFSF; /* turn off fast fsf */
1232 stat = append_test();
1234 Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n"
1235 " Hardware End of Medium = No\n\n"
1236 " Fast Forward Space File = No\n"
1237 "to your Device resource in the Storage conf file.\n");
1241 Pmsg0(-1, "\n\nThat appears *NOT* to have corrected the problem.\n");
1244 /* Wrong count after append */
1246 Pmsg0(-1, "\n\nIt looks like the append failed. Attempting again.\n"
1247 "Setting \"BSF at EOM = yes\" and retrying append test.\n");
1248 dev->capabilities |= CAP_BSFATEOM; /* backspace on eom */
1249 stat = append_test();
1251 Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n"
1252 " Hardware End of Medium = No\n"
1253 " Fast Forward Space File = No\n"
1254 " BSF at EOM = yes\n\n"
1255 "to your Device resource in the Storage conf file.\n");
1262 Pmsg0(-1, "\nAppend test failed.\n\n");
1263 Pmsg0(-1, "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
1264 "Unable to correct the problem. You MUST fix this\n"
1265 "problem before Bacula can use your tape drive correctly\n");
1266 Pmsg0(-1, "\nPerhaps running Bacula in fixed block mode will work.\n"
1267 "Do so by setting:\n\n"
1268 "Minimum Block Size = nnn\n"
1269 "Maximum Block Size = nnn\n\n"
1270 "in your Storage daemon's Device definition.\n"
1271 "nnn must match your tape driver's block size, which\n"
1272 "can be determined by reading your tape manufacturers\n"
1273 "information, and the information on your kernel dirver.\n"
1274 "Fixed block sizes, however, are not normally an ideal solution.\n");
1279 Pmsg0(-1, _("\nThe above Bacula scan should have output identical to what follows.\n"
1280 "Please double check it ...\n"
1281 "=== Sample correct output ===\n"
1282 "1 block of 64448 bytes in file 1\n"
1283 "End of File mark.\n"
1284 "2 blocks of 64448 bytes in file 2\n"
1285 "End of File mark.\n"
1286 "3 blocks of 64448 bytes in file 3\n"
1287 "End of File mark.\n"
1288 "1 block of 64448 bytes in file 4\n"
1289 "End of File mark.\n"
1290 "Total files=4, blocks=7, bytes = 451,136\n"
1291 "=== End sample correct output ===\n\n"));
1293 Pmsg0(-1, _("If the above scan output is not identical to the\n"
1294 "sample output, you MUST correct the problem\n"
1295 "or Bacula will not be able to write multiple Jobs to \n"
1299 re_read_block_test();
1302 fsf_test(); /* do fast forward space file test */
1304 autochanger_test(); /* do autochanger test */
1306 Pmsg0(-1, _("\n=== End Append files test ===\n"));
1310 /* Forward space a file */
1311 static void fsfcmd()
1315 num = atoi(argk[1]);
1320 if (!fsf_dev(dev, num)) {
1321 Pmsg1(0, "Bad status from fsf. ERR=%s\n", strerror_dev(dev));
1324 Pmsg2(0, "Forward spaced %d file%s.\n", num, num==1?"":"s");
1327 /* Forward space a record */
1328 static void fsrcmd()
1332 num = atoi(argk[1]);
1337 if (!fsr_dev(dev, num)) {
1338 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1341 Pmsg2(0, "Forward spaced %d record%s.\n", num, num==1?"":"s");
1346 * Write a Bacula block to the tape
1354 sm_check(__FILE__, __LINE__, False);
1355 block = new_block(dev);
1357 dump_block(block, "test");
1359 i = block->buf_len - 100;
1361 rec->data = check_pool_memory_size(rec->data, i);
1362 memset(rec->data, i & 0xFF, i);
1364 sm_check(__FILE__, __LINE__, False);
1365 if (!write_record_to_block(block, rec)) {
1366 Pmsg0(0, _("Error writing record to block.\n"));
1369 if (!write_block_to_dev(jcr, dev, block)) {
1370 Pmsg0(0, _("Error writing block to device.\n"));
1373 Pmsg1(0, _("Wrote one record of %d bytes.\n"), i);
1375 Pmsg0(0, _("Wrote block to device.\n"));
1378 sm_check(__FILE__, __LINE__, False);
1381 sm_check(__FILE__, __LINE__, False);
1385 * Read a record from the tape
1392 if (!get_cmd("Enter length to read: ")) {
1396 if (len < 0 || len > 1000000) {
1397 Pmsg0(0, _("Bad length entered, using default of 1024 bytes.\n"));
1400 buf = (char *)malloc(len);
1401 stat = read(dev->fd, buf, len);
1402 if (stat > 0 && stat <= len) {
1405 Pmsg3(0, _("Read of %d bytes gives stat=%d. ERR=%s\n"),
1406 len, stat, strerror(errno));
1412 * Scan tape by reading block by block. Report what is
1413 * on the tape. Note, this command does raw reads, and as such
1414 * will not work with fixed block size devices.
1416 static void scancmd()
1419 int blocks, tot_blocks, tot_files;
1425 blocks = block_size = tot_blocks = 0;
1427 if (dev->state & ST_EOT) {
1428 Pmsg0(0, "End of tape\n");
1431 update_pos_dev(dev);
1432 tot_files = dev->file;
1433 Pmsg1(0, _("Starting scan at file %u\n"), dev->file);
1435 if ((stat = read(dev->fd, buf, sizeof(buf))) < 0) {
1436 clrerror_dev(dev, -1);
1437 Mmsg2(&dev->errmsg, "read error on %s. ERR=%s.\n",
1438 dev->dev_name, strerror(dev->dev_errno));
1439 Pmsg2(0, "Bad status from read %d. ERR=%s\n", stat, strerror_dev(dev));
1441 printf("%d block%s of %d bytes in file %d\n",
1442 blocks, blocks>1?"s":"", block_size, dev->file);
1445 Dmsg1(200, "read status = %d\n", stat);
1447 if (stat != block_size) {
1448 update_pos_dev(dev);
1450 printf("%d block%s of %d bytes in file %d\n",
1451 blocks, blocks>1?"s":"", block_size, dev->file);
1456 if (stat == 0) { /* EOF */
1457 update_pos_dev(dev);
1458 printf("End of File mark.\n");
1459 /* Two reads of zero means end of tape */
1460 if (dev->state & ST_EOF)
1461 dev->state |= ST_EOT;
1463 dev->state |= ST_EOF;
1466 if (dev->state & ST_EOT) {
1467 printf("End of tape\n");
1470 } else { /* Got data */
1471 dev->state &= ~ST_EOF;
1477 update_pos_dev(dev);
1478 tot_files = dev->file - tot_files;
1479 printf("Total files=%d, blocks=%d, bytes = %s\n", tot_files, tot_blocks,
1480 edit_uint64_with_commas(bytes, ec1));
1485 * Scan tape by reading Bacula block by block. Report what is
1486 * on the tape. This function reads Bacula blocks, so if your
1487 * Device resource is correctly defined, it should work with
1488 * either variable or fixed block sizes.
1490 static void scan_blocks()
1492 int blocks, tot_blocks, tot_files;
1493 uint32_t block_size;
1498 block = new_block(dev);
1499 blocks = block_size = tot_blocks = 0;
1502 update_pos_dev(dev);
1503 tot_files = dev->file;
1505 if (!read_block_from_device(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
1506 Dmsg1(100, "!read_block(): ERR=%s\n", strerror_dev(dev));
1507 if (dev->state & ST_EOT) {
1509 printf("%d block%s of %d bytes in file %d\n",
1510 blocks, blocks>1?"s":"", block_size, dev->file);
1515 if (dev->state & ST_EOF) {
1517 printf("%d block%s of %d bytes in file %d\n",
1518 blocks, blocks>1?"s":"", block_size, dev->file);
1521 printf(_("End of File mark.\n"));
1524 if (dev->state & ST_SHORT) {
1526 printf("%d block%s of %d bytes in file %d\n",
1527 blocks, blocks>1?"s":"", block_size, dev->file);
1530 printf(_("Short block read.\n"));
1533 printf(_("Error reading block. ERR=%s\n"), strerror_dev(dev));
1536 if (block->block_len != block_size) {
1538 printf("%d block%s of %d bytes in file %d\n",
1539 blocks, blocks>1?"s":"", block_size, dev->file);
1542 block_size = block->block_len;
1546 bytes += block->block_len;
1547 Dmsg6(100, "Blk_blk=%u dev_blk=%u blen=%u bVer=%d SessId=%u SessTim=%u\n",
1548 block->BlockNumber, dev->block_num, block->block_len, block->BlockVer,
1549 block->VolSessionId, block->VolSessionTime);
1551 DEV_RECORD *rec = new_record();
1552 read_record_from_block(block, rec);
1553 Pmsg8(-1, "Blk_block: %u dev_blk=%u blen=%u First rec FI=%s SessId=%u SessTim=%u Strm=%s rlen=%d\n",
1554 block->BlockNumber, dev->block_num, block->block_len,
1555 FI_to_ascii(rec->FileIndex), rec->VolSessionId, rec->VolSessionTime,
1556 stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len);
1559 } else if (verbose > 1) {
1560 dump_block(block, "");
1566 tot_files = dev->file - tot_files;
1567 printf("Total files=%d, blocks=%d, bytes = %s\n", tot_files, tot_blocks,
1568 edit_uint64_with_commas(bytes, ec1));
1572 static void statcmd()
1574 int debug = debug_level;
1576 Pmsg2(0, "Device status: %u. ERR=%s\n", status_dev(dev), strerror_dev(dev));
1578 dump_volume_label(dev);
1580 debug_level = debug;
1585 * First we label the tape, then we fill
1586 * it with data get a new tape and write a few blocks.
1588 static void fillcmd()
1595 uint32_t min_block_size;
1605 This command simulates Bacula writing to a tape.\n\
1606 It requires either one or two blank tapes, which it\n\
1607 will label and write. It will print a status approximately\n\
1608 every 322 MB, and write an EOF every 3.2 GB. If you have\n\
1609 selected the simple test option, after writing the first tape\n\
1610 it will rewind it and re-read the last block written.\n\
1611 If you have selected the multiple tape test, when the first tape\n\
1612 fills, it will ask for a second, and after writing a few more \n\
1613 blocks, it will stop. Then it will begin re-reading the\n\
1615 This may take a long time -- hours! ...\n\n");
1617 get_cmd("Insert a blank tape then indicate if you want\n"
1618 "to run the simplified test (s) with one tape or\n"
1619 "the complete multiple tape (m) test: (s/m) ");
1620 if (cmd[0] == 's') {
1621 Pmsg0(-1, "Simple test (single tape) selected.\n");
1623 } else if (cmd[0] == 'm') {
1624 Pmsg0(-1, "Complete multiple tape test selected.\n");
1627 Pmsg0(000, "Command aborted.\n");
1631 set_volume_name("TestVolume1", 1);
1636 Dmsg1(20, "Begin append device=%s\n", dev_name(dev));
1639 /* Use fixed block size to simplify read back */
1640 min_block_size = dev->min_block_size;
1641 dev->min_block_size = dev->max_block_size;
1642 block = new_block(dev);
1645 * Acquire output device for writing. Note, after acquiring a
1646 * device, we MUST release it, which is done at the end of this
1649 Dmsg0(100, "just before acquire_device\n");
1650 if (!(dev=acquire_device_for_append(jcr, dev, block))) {
1651 set_jcr_job_status(jcr, JS_ErrorTerminated);
1656 Dmsg0(100, "Just after acquire_device_for_append\n");
1658 * Write Begin Session Record
1660 if (!write_session_label(jcr, block, SOS_LABEL)) {
1661 set_jcr_job_status(jcr, JS_ErrorTerminated);
1662 Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"),
1666 Pmsg0(-1, "Wrote Start Of Session label.\n");
1668 memset(&rec, 0, sizeof(rec));
1669 rec.data = get_memory(100000); /* max record size */
1671 #define REC_SIZE 32768
1672 rec.data_len = REC_SIZE;
1675 * Put some random data in the record
1677 fd = open("/dev/urandom", O_RDONLY);
1679 read(fd, rec.data, rec.data_len);
1682 uint32_t *p = (uint32_t *)rec.data;
1683 srandom(time(NULL));
1684 for (i=0; i<rec.data_len/sizeof(uint32_t); i++) {
1690 * Generate data as if from File daemon, write to device
1692 jcr->VolFirstIndex = 0;
1693 time(&jcr->run_time); /* start counting time for rates */
1695 Pmsg0(-1, "Begin writing Bacula records to tape ...\n");
1697 Pmsg0(-1, "Begin writing Bacula records to first tape ...\n");
1699 for (file_index = 0; ok && !job_canceled(jcr); ) {
1700 rec.VolSessionId = jcr->VolSessionId;
1701 rec.VolSessionTime = jcr->VolSessionTime;
1702 rec.FileIndex = ++file_index;
1703 rec.Stream = STREAM_FILE_DATA;
1705 /* Mix up the data just a bit */
1706 uint32_t *lp = (uint32_t *)rec.data;
1708 for (i=1; i < (rec.data_len-sizeof(uint32_t))/sizeof(uint32_t)-1; i++) {
1712 Dmsg4(250, "before write_rec FI=%d SessId=%d Strm=%s len=%d\n",
1713 rec.FileIndex, rec.VolSessionId, stream_to_ascii(rec.Stream, rec.FileIndex),
1716 while (!write_record_to_block(block, &rec)) {
1718 * When we get here we have just filled a block
1720 Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec.data_len,
1723 /* Write block to tape */
1724 if (!flush_block(block, 1)) {
1728 /* Every 5000 blocks (approx 322MB) report where we are.
1730 if ((block->BlockNumber % 5000) == 0) {
1732 now -= jcr->run_time;
1734 now = 1; /* prevent divide error */
1736 kbs = (double)dev->VolCatInfo.VolCatBytes / (1000.0 * (double)now);
1737 Pmsg4(-1, "Wrote blk_block=%u, dev_blk_num=%u VolBytes=%s rate=%.1f KB/s\n",
1738 block->BlockNumber, dev->block_num,
1739 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), (float)kbs);
1741 /* Every 15000 blocks (approx 1GB) write an EOF.
1743 if ((block->BlockNumber % 15000) == 0) {
1744 Pmsg0(-1, "Flush block, write EOF\n");
1745 flush_block(block, 0);
1749 /* Get out after writing 10 blocks to the second tape */
1750 if (++BlockNumber > 10 && stop != 0) { /* get out */
1755 Pmsg0(000, _("Not OK\n"));
1758 jcr->JobBytes += rec.data_len; /* increment bytes this job */
1759 Dmsg4(190, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
1760 FI_to_ascii(rec.FileIndex), rec.VolSessionId,
1761 stream_to_ascii(rec.Stream, rec.FileIndex), rec.data_len);
1763 /* Get out after writing 10 blocks to the second tape */
1764 if (BlockNumber > 10 && stop != 0) { /* get out */
1765 Pmsg0(-1, "Done writing ...\n");
1770 Dmsg0(100, "Write_end_session_label()\n");
1771 /* Create Job status for end of session label */
1772 if (!job_canceled(jcr) && ok) {
1773 set_jcr_job_status(jcr, JS_Terminated);
1775 set_jcr_job_status(jcr, JS_ErrorTerminated);
1777 if (!write_session_label(jcr, block, EOS_LABEL)) {
1778 Pmsg1(000, _("Error writting end session label. ERR=%s\n"), strerror_dev(dev));
1781 /* Write out final block of this session */
1782 if (!write_block_to_device(jcr, dev, block)) {
1783 Pmsg0(-1, _("Set ok=FALSE after write_block_to_device.\n"));
1786 Pmsg0(-1, _("Wrote End Of Session label.\n"));
1789 sprintf(buf, "%s/btape.state", working_directory);
1790 fd = open(buf, O_CREAT|O_TRUNC|O_WRONLY, 0640);
1792 write(fd, &btape_state_level, sizeof(btape_state_level));
1793 write(fd, &last_block_num, sizeof(last_block_num));
1794 write(fd, &last_file, sizeof(last_file));
1795 write(fd, last_block->buf, last_block->buf_len);
1797 Pmsg0(-1, "Wrote state file.\n");
1799 Pmsg2(-1, _("Could not create state file: %s ERR=%s\n"), buf,
1803 /* Release the device */
1804 if (!release_device(jcr, dev)) {
1805 Pmsg0(-1, _("Error in release_device\n"));
1811 dump_block(last_block, _("Last block written to tape.\n"));
1814 Pmsg0(-1, _("\n\nDone filling tape. Now beginning re-read of tape ...\n"));
1819 /* Multiple Volume tape */
1823 block = new_block(dev);
1825 dev->capabilities |= CAP_ANONVOLS; /* allow reading any volume */
1826 dev->capabilities &= ~CAP_LABEL; /* don't label anything here */
1831 time(&jcr->run_time); /* start counting time for rates */
1834 /* Close device so user can use autochanger if desired */
1835 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
1838 force_close_dev(dev);
1839 get_cmd(_("Mount first tape. Press enter when ready: "));
1842 set_volume_name("TestVolume1", 1);
1844 create_vol_list(jcr);
1846 dev->state &= ~ST_READ;
1847 if (!acquire_device_for_read(jcr, dev, block)) {
1848 Pmsg1(-1, "%s", dev->errmsg);
1851 /* Read all records and then second tape */
1852 read_records(jcr, dev, record_cb, my_mount_next_read_volume);
1855 dev->min_block_size = min_block_size;
1857 free_memory(rec.data);
1861 * Read two tapes written by the "fill" command and ensure
1862 * that the data is valid. If stop==1 we simulate full read back
1863 * of two tapes. If stop==-1 we simply read the last block and
1864 * verify that it is correct.
1866 static void unfillcmd()
1871 last_block = new_block(dev);
1873 sprintf(buf, "%s/btape.state", working_directory);
1874 fd = open(buf, O_RDONLY);
1876 uint32_t state_level;
1877 read(fd, &state_level, sizeof(btape_state_level));
1878 read(fd, &last_block_num, sizeof(last_block_num));
1879 read(fd, &last_file, sizeof(last_file));
1880 read(fd, last_block->buf, last_block->buf_len);
1882 if (state_level != btape_state_level) {
1883 Pmsg0(-1, "\nThe state file level has changed. You must redo\n"
1884 "the fill command.\n");
1888 Pmsg2(-1, "\nCould not find the state file: %s ERR=%s\n"
1889 "You must redo the fill command.\n", buf, strerror(errno));
1896 static void do_unfill()
1903 block = new_block(dev);
1905 dev->capabilities |= CAP_ANONVOLS; /* allow reading any volume */
1906 dev->capabilities &= ~CAP_LABEL; /* don't label anything here */
1911 time(&jcr->run_time); /* start counting time for rates */
1916 * Note, re-reading last block may have caused us to
1917 * lose track of where we are (block number unknown).
1919 rewind_dev(dev); /* get to a known place on tape */
1920 Pmsg4(-1, _("Reposition from %u:%u to %u:%u\n"), dev->file, dev->block_num,
1921 last_file, last_block_num);
1922 if (!reposition_dev(dev, last_file, last_block_num)) {
1923 Pmsg1(-1, "Reposition error. ERR=%s\n", strerror_dev(dev));
1925 Pmsg1(-1, _("Reading block %u.\n"), last_block_num);
1926 if (!read_block_from_device(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
1927 Pmsg1(-1, _("Error reading block: ERR=%s\n"), strerror_dev(dev));
1932 uint32_t CheckSum, block_len;
1934 p = last_block->buf;
1936 unser_begin(q, BLKHDR2_LENGTH);
1937 unser_uint32(CheckSum);
1938 unser_uint32(block_len);
1939 while (q < (block->buf+block_len)) {
1946 dump_block(last_block, _("Last block written"));
1948 dump_block(block, _("Block read back"));
1949 Pmsg1(-1, "\n\nThe blocks differ at byte %u\n", p - last_block->buf);
1950 Pmsg0(-1, "\n\n!!!! The last block written and the block\n"
1951 "that was read back differ. The test FAILED !!!!\n"
1952 "This must be corrected before you use Bacula\n"
1953 "to write multi-tape Volumes.!!!!\n");
1956 Pmsg0(-1, _("\nThe blocks are identical. Test succeeded.\n\n"));
1958 dump_block(last_block, _("Last block written"));
1959 dump_block(block, _("Block read back"));
1968 * We are called here from "unfill" for each record on the tape.
1970 static int record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
1972 SESSION_LABEL label;
1974 if (stop > 1 && !dumped) { /* on second tape */
1977 dump_block(block, "First block on second tape");
1979 Pmsg4(-1, "Blk: FileIndex=%d: block=%u size=%d vol=%s\n",
1980 rec->FileIndex, block->BlockNumber, block->block_len, dev->VolHdr.VolName);
1981 Pmsg6(-1, " Rec: VId=%d VT=%d FI=%s Strm=%s len=%d state=%x\n",
1982 rec->VolSessionId, rec->VolSessionTime,
1983 FI_to_ascii(rec->FileIndex), stream_to_ascii(rec->Stream, rec->FileIndex),
1984 rec->data_len, rec->state);
1986 if (rec->FileIndex < 0) {
1988 dump_label_record(dev, rec, 1);
1990 switch (rec->FileIndex) {
1992 Pmsg0(-1, "Volume is prelabeled. This tape cannot be scanned.\n");
1995 unser_volume_label(dev, rec);
1996 Pmsg3(-1, "VOL_LABEL: block=%u size=%d vol=%s\n", block->BlockNumber,
1997 block->block_len, dev->VolHdr.VolName);
2001 unser_session_label(&label, rec);
2002 Pmsg1(-1, "SOS_LABEL: JobId=%u\n", label.JobId);
2005 unser_session_label(&label, rec);
2006 Pmsg2(-1, "EOS_LABEL: block=%u JobId=%u\n", block->BlockNumber,
2010 Pmsg0(-1, "EOM_LABEL:\n");
2012 case EOT_LABEL: /* end of all tapes */
2015 if (LastBlock != block->BlockNumber) {
2016 VolBytes += block->block_len;
2018 LastBlock = block->BlockNumber;
2020 now -= jcr->run_time;
2024 kbs = (double)VolBytes / (1000 * now);
2025 Pmsg3(000, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
2026 edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
2028 Pmsg0(000, "End of all tapes.\n");
2036 if (++file_index != rec->FileIndex) {
2037 Pmsg3(000, "Incorrect FileIndex in Block %u. Got %d, expected %d.\n",
2038 block->BlockNumber, rec->FileIndex, file_index);
2041 * Now check that the right data is in the record.
2043 uint64_t *lp = (uint64_t *)rec->data;
2044 uint64_t val = ~file_index;
2045 for (uint32_t i=0; i < (REC_SIZE-sizeof(uint64_t))/sizeof(uint64_t); i++) {
2047 Pmsg2(000, "Record %d contains bad data in Block %u.\n",
2048 file_index, block->BlockNumber);
2053 if (LastBlock != block->BlockNumber) {
2054 VolBytes += block->block_len;
2056 if ((block->BlockNumber != LastBlock) && (block->BlockNumber % 50000) == 0) {
2059 now -= jcr->run_time;
2063 kbs = (double)VolBytes / (1000 * now);
2064 Pmsg3(000, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
2065 edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
2067 LastBlock = block->BlockNumber;
2069 Pmsg1(000, "End of all blocks. Block=%u\n", block->BlockNumber);
2077 * Write current block to tape regardless of whether or
2078 * not it is full. If the tape fills, attempt to
2079 * acquire another tape.
2081 static int flush_block(DEV_BLOCK *block, int dump)
2086 uint32_t this_file, this_block_num;
2089 this_block = new_block(dev);
2092 free_memory(this_block->buf);
2093 memcpy(this_block, block, sizeof(DEV_BLOCK));
2094 this_block->buf = get_memory(block->buf_len);
2095 this_file = dev->file;
2096 this_block_num = dev->block_num;
2097 if (!write_block_to_dev(jcr, dev, block)) {
2098 Pmsg3(000, "Last block at: %u:%u this_dev_block_num=%d\n",
2099 last_file, last_block_num, this_block_num);
2101 Pmsg3(000, "Block not written: FileIndex=%u blk_block=%u Size=%u\n",
2102 (unsigned)file_index, block->BlockNumber, block->block_len);
2104 dump_block(block, "Block not written");
2108 eot_block = block->BlockNumber;
2109 eot_block_len = block->block_len;
2110 eot_FileIndex = file_index;
2113 now -= jcr->run_time;
2115 now = 1; /* don't divide by zero */
2117 kbs = (double)dev->VolCatInfo.VolCatBytes / (1000 * now);
2118 vol_size = dev->VolCatInfo.VolCatBytes;
2119 Pmsg2(000, "End of tape. VolumeCapacity=%s. Write rate = %.1f KB/s\n",
2120 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), kbs);
2123 stop = -1; /* stop, but do simplified test */
2125 /* Full test in progress */
2126 if (!fixup_device_block_write_error(jcr, dev, block)) {
2127 Pmsg1(000, _("Cannot fixup device error. %s\n"), strerror_dev(dev));
2133 BlockNumber = 0; /* start counting for second tape */
2136 return 1; /* end of tape reached */
2138 /* Save contents after write so that the header is serialized */
2139 memcpy(this_block->buf, block->buf, this_block->buf_len);
2142 * Toggle between two allocated blocks for efficiency.
2143 * Switch blocks so that the block just successfully written is
2144 * always in last_block.
2146 tblock = last_block;
2147 last_block = this_block;
2148 this_block = tblock;
2149 last_file = this_file;
2150 last_block_num = this_block_num;
2158 * First we label the tape, then we fill
2159 * it with data get a new tape and write a few blocks.
2161 static void qfillcmd()
2167 Pmsg0(0, "Test writing blocks of 64512 bytes to tape.\n");
2169 get_cmd("How many blocks do you want to write? (1000): ");
2176 sm_check(__FILE__, __LINE__, False);
2177 block = new_block(dev);
2180 i = block->buf_len - 100;
2182 rec->data = check_pool_memory_size(rec->data, i);
2183 memset(rec->data, i & 0xFF, i);
2186 Pmsg1(0, "Begin writing %d Bacula blocks to tape ...\n", count);
2187 for (i=0; i < count; i++) {
2192 if (!write_record_to_block(block, rec)) {
2193 Pmsg0(0, _("Error writing record to block.\n"));
2196 if (!write_block_to_dev(jcr, dev, block)) {
2197 Pmsg0(0, _("Error writing block to device.\n"));
2203 if (dev_cap(dev, CAP_TWOEOF)) {
2210 sm_check(__FILE__, __LINE__, False);
2213 sm_check(__FILE__, __LINE__, False);
2218 * Fill a tape using raw write() command
2220 static void rawfill_cmd()
2225 uint32_t block_num = 0;
2230 block = new_block(dev);
2231 fd = open("/dev/urandom", O_RDONLY);
2233 read(fd, block->buf, block->buf_len);
2236 uint32_t *p = (uint32_t *)block->buf;
2237 srandom(time(NULL));
2238 for (i=0; i<block->buf_len/sizeof(uint32_t); i++) {
2242 p = (uint32_t *)block->buf;
2243 Pmsg1(0, "Begin writing raw blocks of %u bytes.\n", block->buf_len);
2246 stat = write(dev->fd, block->buf, block->buf_len);
2247 if (stat == (int)block->buf_len) {
2248 if ((block_num++ % 100) == 0) {
2253 for (i=1; i<(block->buf_len-sizeof(uint32_t))/sizeof(uint32_t)-1; i++) {
2262 printf("Write failed at block %u. stat=%d ERR=%s\n", block_num, stat,
2263 strerror(my_errno));
2270 * Fill a tape using raw write() command
2272 static void bfill_cmd()
2275 uint32_t block_num = 0;
2281 block = new_block(dev);
2282 fd = open("/dev/urandom", O_RDONLY);
2284 read(fd, block->buf, block->buf_len);
2287 uint32_t *p = (uint32_t *)block->buf;
2288 srandom(time(NULL));
2289 for (i=0; i<block->buf_len/sizeof(uint32_t); i++) {
2293 p = (uint32_t *)block->buf;
2294 Pmsg1(0, "Begin writing Bacula blocks of %u bytes.\n", block->buf_len);
2297 block->binbuf = block->buf_len;
2298 block->bufp = block->buf + block->binbuf;
2299 if (!write_block_to_dev(jcr, dev, block)) {
2302 if ((block_num++ % 100) == 0) {
2307 for (i=1; i<(block->buf_len/sizeof(uint32_t)-1); i++) {
2313 printf("Write failed at block %u.\n", block_num);
2319 struct cmdstruct { char *key; void (*func)(); char *help; };
2320 static struct cmdstruct commands[] = {
2321 {"autochanger", autochangercmd, "test autochanger"},
2322 {"bsf", bsfcmd, "backspace file"},
2323 {"bsr", bsrcmd, "backspace record"},
2324 {"bfill", bfill_cmd, "fill tape using Bacula writes"},
2325 {"cap", capcmd, "list device capabilities"},
2326 {"clear", clearcmd, "clear tape errors"},
2327 {"eod", eodcmd, "go to end of Bacula data for append"},
2328 {"eom", eomcmd, "go to the physical end of medium"},
2329 {"fill", fillcmd, "fill tape, write onto second volume"},
2330 {"unfill", unfillcmd, "read filled tape"},
2331 {"fsf", fsfcmd, "forward space a file"},
2332 {"fsr", fsrcmd, "forward space a record"},
2333 {"help", helpcmd, "print this command"},
2334 {"label", labelcmd, "write a Bacula label to the tape"},
2335 {"load", loadcmd, "load a tape"},
2336 {"quit", quitcmd, "quit btape"},
2337 {"rawfill", rawfill_cmd, "use write() to fill tape"},
2338 {"readlabel", readlabelcmd, "read and print the Bacula tape label"},
2339 {"rectest", rectestcmd, "test record handling functions"},
2340 {"rewind", rewindcmd, "rewind the tape"},
2341 {"scan", scancmd, "read() tape block by block to EOT and report"},
2342 {"scanblocks", scan_blocks, "Bacula read block by block to EOT and report"},
2343 {"status", statcmd, "print tape status"},
2344 {"test", testcmd, "General test Bacula tape functions"},
2345 {"weof", weofcmd, "write an EOF on the tape"},
2346 {"wr", wrcmd, "write a single Bacula block"},
2347 {"rr", rrcmd, "read a single record"},
2348 {"qfill", qfillcmd, "quick fill command"},
2350 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
2358 while (get_cmd("*")) {
2359 sm_check(__FILE__, __LINE__, False);
2361 parse_args(cmd, &args, &argc, argk, argv, MAX_CMD_ARGS);
2362 for (i=0; i<comsize; i++) /* search for command */
2363 if (argc > 0 && fstrsch(argk[0], commands[i].key)) {
2364 (*commands[i].func)(); /* go execute command */
2369 Pmsg1(0, _("%s is an illegal command\n"), cmd);
2375 static void helpcmd()
2379 printf(_("Interactive commands:\n"));
2380 printf(_(" Command Description\n ======= ===========\n"));
2381 for (i=0; i<comsize; i++)
2382 printf(" %-10s %s\n", commands[i].key, commands[i].help);
2389 "\nVersion: " VERSION " (" BDATE ")\n\n"
2390 "Usage: btape <options> <device_name>\n"
2391 " -c <file> set configuration file to file\n"
2392 " -d <nn> set debug level to nn\n"
2393 " -s turn off signals\n"
2394 " -t open the default tape device\n"
2395 " -? print this message.\n"
2401 * Get next input command from terminal. This
2402 * routine is REALLY primitive, and should be enhanced
2403 * to have correct backspacing, etc.
2406 get_cmd(char *prompt)
2410 fprintf(stdout, prompt);
2412 /* We really should turn off echoing and pretty this
2416 while ((ch = fgetc(stdin)) != EOF) {
2418 strip_trailing_junk(cmd);
2420 } else if (ch == 4 || ch == 0xd3 || ch == 0x8) {
2433 /* Dummies to replace askdir.c */
2434 int dir_get_volume_info(JCR *jcr, enum get_vol_info_rw writing) { return 1;}
2435 int dir_update_volume_info(JCR *jcr, DEVICE *dev, int relabel) { return 1; }
2436 int dir_create_jobmedia_record(JCR *jcr) { return 1; }
2437 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
2438 int dir_send_job_status(JCR *jcr) {return 1;}
2442 int dir_find_next_appendable_volume(JCR *jcr)
2447 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
2449 /* Close device so user can use autochanger if desired */
2450 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2453 force_close_dev(dev);
2454 Pmsg1(-1, "%s", dev->errmsg); /* print reason */
2455 fprintf(stderr, "Mount Volume \"%s\" on device %s and press return when ready: ",
2456 jcr->VolumeName, dev_name(dev));
2461 int dir_ask_sysop_to_create_appendable_volume(JCR *jcr, DEVICE *dev)
2463 /* Close device so user can use autochanger if desired */
2464 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2467 force_close_dev(dev);
2468 fprintf(stderr, "Mount next Volume on device %s and press return when ready: ",
2471 set_volume_name("TestVolume2", 2);
2479 static int my_mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
2483 Pmsg1(000, "End of Volume \"%s\"\n", jcr->VolumeName);
2485 if (LastBlock != block->BlockNumber) {
2486 VolBytes += block->block_len;
2488 LastBlock = block->BlockNumber;
2490 now -= jcr->run_time;
2494 kbs = (double)VolBytes / (1000.0 * (double)now);
2495 Pmsg3(-1, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
2496 edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
2498 if (strcmp(jcr->VolumeName, "TestVolume2") == 0) {
2504 set_volume_name("TestVolume2", 2);
2506 create_vol_list(jcr);
2508 dev->state &= ~ST_READ;
2509 if (!acquire_device_for_read(jcr, dev, block)) {
2510 Pmsg2(0, "Cannot open Dev=%s, Vol=%s\n", dev_name(dev), jcr->VolumeName);
2513 return 1; /* next volume mounted */
2516 static void set_volume_name(char *VolName, int volnum)
2518 VolumeName = VolName;
2520 pm_strcpy(&jcr->VolumeName, VolName);
2521 bstrncpy(dev->VolCatInfo.VolCatName, VolName, sizeof(dev->VolCatInfo.VolCatName));
2525 * Edit codes into ChangerCommand
2527 * %a = archive device name
2528 * %c = changer device name
2529 * %f = Client's name
2537 * omsg = edited output message
2538 * imsg = input string containing edit codes (%x)
2539 * cmd = command string (load, unload, ...)
2542 static char *edit_device_codes(JCR *jcr, char *omsg, char *imsg, char *cmd)
2549 Dmsg1(400, "edit_device_codes: %s\n", imsg);
2550 for (p=imsg; *p; p++) {
2557 str = dev_name(jcr->device->dev);
2560 str = NPRT(jcr->device->changer_name);
2566 sprintf(add, "%d", jcr->VolCatInfo.Slot - 1);
2570 sprintf(add, "%d", jcr->VolCatInfo.Slot);
2573 case 'j': /* Job name */
2577 str = NPRT(jcr->VolumeName);
2580 str = NPRT(jcr->client_name);
2595 Dmsg1(400, "add_str %s\n", str);
2596 pm_strcat(&omsg, (char *)str);
2597 Dmsg1(400, "omsg=%s\n", omsg);