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-2003 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 DEVRES *device = NULL;
53 /* Forward referenced subroutines */
54 static void do_tape_cmds();
55 static void helpcmd();
56 static void scancmd();
57 static void rewindcmd();
58 static void clearcmd();
62 static void fillcmd();
63 static void qfillcmd();
64 static void statcmd();
65 static void unfillcmd();
66 static int flush_block(DEV_BLOCK *block, int dump);
67 static int record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec);
68 static int my_mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block);
69 static void scan_blocks();
70 static void set_volume_name(char *VolName, int volnum);
71 static void rawfill_cmd();
72 static void bfill_cmd();
73 static bool open_the_device();
74 static char *edit_device_codes(JCR *jcr, char *omsg, char *imsg, char *cmd);
75 static void autochangercmd();
78 /* Static variables */
79 #define CONFIG_FILE "bacula-sd.conf"
82 static BSR *bsr = NULL;
83 static char cmd[1000];
84 static int signals = TRUE;
87 static uint64_t vol_size;
88 static uint64_t VolBytes;
91 static int32_t file_index;
92 static int end_of_tape = 0;
93 static uint32_t LastBlock = 0;
94 static uint32_t eot_block;
95 static uint32_t eot_block_len;
96 static uint32_t eot_FileIndex;
97 static int dumped = 0;
98 static DEV_BLOCK *last_block = NULL;
99 static DEV_BLOCK *this_block = NULL;
100 static uint32_t last_file = 0;
101 static uint32_t last_block_num = 0;
102 static uint32_t BlockNumber = 0;
103 static int simple = FALSE;
105 static char *VolumeName = NULL;
108 static JCR *jcr = NULL;
112 static void terminate_btape(int sig);
113 int get_cmd(char *prompt);
116 /*********************************************************************
118 * Main Bacula Pool Creation Program
121 int main(int argc, char *argv[])
126 if (TAPE_BSIZE % DEV_BSIZE != 0 || TAPE_BSIZE / DEV_BSIZE == 0) {
127 Emsg2(M_ABORT, 0, "Tape block size (%d) not multiple of system size (%d)\n",
128 TAPE_BSIZE, DEV_BSIZE);
130 if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
131 Emsg1(M_ABORT, 0, "Tape block size (%d) is not a power of 2\n", TAPE_BSIZE);
134 printf("Tape block granularity is %d bytes.\n", TAPE_BSIZE);
136 working_directory = "/tmp";
137 my_name_is(argc, argv, "btape");
138 init_msg(NULL, NULL);
140 while ((ch = getopt(argc, argv, "b:c:d:sv?")) != -1) {
142 case 'b': /* bootstrap file */
143 bsr = parse_bsr(NULL, optarg);
144 // dump_bsr(bsr, true);
147 case 'c': /* specify config file */
148 if (configfile != NULL) {
151 configfile = bstrdup(optarg);
154 case 'd': /* set debug level */
155 debug_level = atoi(optarg);
156 if (debug_level <= 0) {
182 init_signals(terminate_btape);
185 if (configfile == NULL) {
186 configfile = bstrdup(CONFIG_FILE);
189 daemon_start_time = time(NULL);
191 parse_config(configfile);
194 /* See if we can open a device */
196 Pmsg0(000, "No archive name specified.\n");
199 } else if (argc != 1) {
200 Pmsg0(000, "Improper number of arguments specified.\n");
205 jcr = setup_jcr("btape", argv[0], bsr, NULL);
206 dev = setup_to_access_device(jcr, 0); /* acquire for write */
210 if (!open_the_device()) {
214 Dmsg0(200, "Do tape commands\n");
222 static void terminate_btape(int stat)
225 sm_check(__FILE__, __LINE__, False);
229 free_config_resources();
235 if (debug_level > 10)
236 print_memory_pool_stats();
246 free_block(last_block);
249 free_block(this_block);
253 close_memory_pool(); /* free memory in pool */
259 static bool open_the_device()
263 block = new_block(dev);
265 if (!(dev->state & ST_OPENED)) {
266 Dmsg1(200, "Opening device %s\n", jcr->VolumeName);
267 if (open_dev(dev, jcr->VolumeName, READ_WRITE) < 0) {
268 Emsg1(M_FATAL, 0, _("dev open failed: %s\n"), dev->errmsg);
274 Dmsg1(000, "open_dev %s OK\n", dev_name(dev));
287 * Write a label to the tape
289 static void labelcmd()
292 bstrncpy(cmd, VolumeName, sizeof(cmd));
294 if (!get_cmd("Enter Volume Name: ")) {
299 if (!(dev->state & ST_OPENED)) {
300 if (!open_device(dev)) {
301 Pmsg1(0, "Device open failed. ERR=%s\n", strerror_dev(dev));
304 write_volume_label_to_dev(jcr, jcr->device, cmd, "Default");
308 * Read the tape label
310 static void readlabelcmd()
312 int save_debug_level = debug_level;
316 block = new_block(dev);
317 stat = read_dev_volume_label(jcr, dev, block);
320 Pmsg0(0, "Volume has no label.\n");
323 Pmsg0(0, "Volume label read correctly.\n");
326 Pmsg1(0, "I/O error on device: ERR=%s", strerror_dev(dev));
329 Pmsg0(0, "Volume name error\n");
331 case VOL_CREATE_ERROR:
332 Pmsg1(0, "Error creating label. ERR=%s", strerror_dev(dev));
334 case VOL_VERSION_ERROR:
335 Pmsg0(0, "Volume version error.\n");
337 case VOL_LABEL_ERROR:
338 Pmsg0(0, "Bad Volume label type.\n");
341 Pmsg0(0, "Unknown error.\n");
346 dump_volume_label(dev);
347 debug_level = save_debug_level;
353 * Load the tape should have prevously been taken
354 * off line, otherwise this command is not necessary.
356 static void loadcmd()
359 if (!load_dev(dev)) {
360 Pmsg1(0, "Bad status from load. ERR=%s\n", strerror_dev(dev));
362 Pmsg1(0, "Loaded %s\n", dev_name(dev));
368 static void rewindcmd()
370 if (!rewind_dev(dev)) {
371 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
372 clrerror_dev(dev, -1);
374 Pmsg1(0, "Rewound %s\n", dev_name(dev));
379 * Clear any tape error
381 static void clearcmd()
383 clrerror_dev(dev, -1);
387 * Write and end of file on the tape
389 static void weofcmd()
393 if ((stat = weof_dev(dev, 1)) < 0) {
394 Pmsg2(0, "Bad status from weof %d. ERR=%s\n", stat, strerror_dev(dev));
397 Pmsg1(0, "Wrote EOF to %s\n", dev_name(dev));
402 /* Go to the end of the medium -- raw command
403 * The idea was orginally that the end of the Bacula
404 * medium would be flagged differently. This is not
405 * currently the case. So, this is identical to the
411 Pmsg1(0, "%s", strerror_dev(dev));
414 Pmsg0(0, _("Moved to end of medium.\n"));
419 * Go to the end of the medium (either hardware determined
420 * or defined by two eofs.
433 if (!bsf_dev(dev, 1)) {
434 Pmsg1(0, _("Bad status from bsf. ERR=%s\n"), strerror_dev(dev));
436 Pmsg0(0, _("Backspaced one file.\n"));
445 if (!bsr_dev(dev, 1)) {
446 Pmsg1(0, _("Bad status from bsr. ERR=%s\n"), strerror_dev(dev));
448 Pmsg0(0, _("Backspaced one record.\n"));
453 * List device capabilities as defined in the
458 printf(_("Configured device capabilities:\n"));
459 printf("%sEOF ", dev->capabilities & CAP_EOF ? "" : "!");
460 printf("%sBSR ", dev->capabilities & CAP_BSR ? "" : "!");
461 printf("%sBSF ", dev->capabilities & CAP_BSF ? "" : "!");
462 printf("%sFSR ", dev->capabilities & CAP_FSR ? "" : "!");
463 printf("%sFSF ", dev->capabilities & CAP_FSF ? "" : "!");
464 printf("%sEOM ", dev->capabilities & CAP_EOM ? "" : "!");
465 printf("%sREM ", dev->capabilities & CAP_REM ? "" : "!");
466 printf("%sRACCESS ", dev->capabilities & CAP_RACCESS ? "" : "!");
467 printf("%sAUTOMOUNT ", dev->capabilities & CAP_AUTOMOUNT ? "" : "!");
468 printf("%sLABEL ", dev->capabilities & CAP_LABEL ? "" : "!");
469 printf("%sANONVOLS ", dev->capabilities & CAP_ANONVOLS ? "" : "!");
470 printf("%sALWAYSOPEN ", dev->capabilities & CAP_ALWAYSOPEN ? "" : "!");
473 printf(_("Device status:\n"));
474 printf("%sOPENED ", dev->state & ST_OPENED ? "" : "!");
475 printf("%sTAPE ", dev->state & ST_TAPE ? "" : "!");
476 printf("%sLABEL ", dev->state & ST_LABEL ? "" : "!");
477 printf("%sMALLOC ", dev->state & ST_MALLOC ? "" : "!");
478 printf("%sAPPEND ", dev->state & ST_APPEND ? "" : "!");
479 printf("%sREAD ", dev->state & ST_READ ? "" : "!");
480 printf("%sEOT ", dev->state & ST_EOT ? "" : "!");
481 printf("%sWEOT ", dev->state & ST_WEOT ? "" : "!");
482 printf("%sEOF ", dev->state & ST_EOF ? "" : "!");
483 printf("%sNEXTVOL ", dev->state & ST_NEXTVOL ? "" : "!");
484 printf("%sSHORT ", dev->state & ST_SHORT ? "" : "!");
487 printf(_("Device parameters:\n"));
488 printf("Device name: %s\n", dev->dev_name);
489 printf("File=%u block=%u\n", dev->file, dev->block_num);
490 printf("Min block=%u Max block=%u\n", dev->min_block_size, dev->max_block_size);
498 * Test writting larger and larger records.
499 * This is a torture test for records.
501 static void rectestcmd()
507 Pmsg0(0, "Test writting larger and larger records.\n"
508 "This is a torture test for records.\nI am going to write\n"
509 "larger and larger records. It will stop when the record size\n"
510 "plus the header exceeds the block size (by default about 64K)\n");
513 get_cmd("Do you want to continue? (y/n): ");
515 Pmsg0(000, "Command aborted.\n");
519 sm_check(__FILE__, __LINE__, False);
520 block = new_block(dev);
523 for (i=1; i<500000; i++) {
524 rec->data = check_pool_memory_size(rec->data, i);
525 memset(rec->data, i & 0xFF, i);
527 sm_check(__FILE__, __LINE__, False);
528 if (write_record_to_block(block, rec)) {
531 Pmsg2(0, "Block %d i=%d\n", blkno, i);
535 sm_check(__FILE__, __LINE__, False);
539 sm_check(__FILE__, __LINE__, False);
543 * This test attempts to re-read a block written by Bacula
544 * normally at the end of the tape. Bacula will then back up
545 * over the two eof marks, backup over the record and reread
546 * it to make sure it is valid. Bacula can skip this validation
547 * if you set "Backward space record = no"
549 static int re_read_block_test()
556 if (!(dev->capabilities & CAP_BSR)) {
557 Pmsg0(-1, _("Skipping read backwards test because BSR turned off.\n"));
561 Pmsg0(-1, _("\n=== Write, backup, and re-read test ===\n\n"
562 "I'm going to write three records and two eof's\n"
563 "then backup over the eof's and re-read the last record.\n"
564 "Bacula does this after writing the last block on the\n"
565 "tape to verify that the block was written correctly.\n"
566 "It is not an *essential* feature ...\n\n"));
568 block = new_block(dev);
570 rec->data = check_pool_memory_size(rec->data, block->buf_len);
571 len = rec->data_len = block->buf_len-100;
572 memset(rec->data, 1, rec->data_len);
573 if (!write_record_to_block(block, rec)) {
574 Pmsg0(0, _("Error writing record to block.\n"));
577 if (!write_block_to_dev(jcr, dev, block)) {
578 Pmsg0(0, _("Error writing block to device.\n"));
581 Pmsg1(0, _("Wrote first record of %d bytes.\n"), rec->data_len);
583 memset(rec->data, 2, rec->data_len);
584 if (!write_record_to_block(block, rec)) {
585 Pmsg0(0, _("Error writing record to block.\n"));
588 if (!write_block_to_dev(jcr, dev, block)) {
589 Pmsg0(0, _("Error writing block to device.\n"));
592 Pmsg1(0, _("Wrote second record of %d bytes.\n"), rec->data_len);
594 memset(rec->data, 3, rec->data_len);
595 if (!write_record_to_block(block, rec)) {
596 Pmsg0(0, _("Error writing record to block.\n"));
599 if (!write_block_to_dev(jcr, dev, block)) {
600 Pmsg0(0, _("Error writing block to device.\n"));
603 Pmsg1(0, _("Wrote third record of %d bytes.\n"), rec->data_len);
607 if (!bsf_dev(dev, 1)) {
608 Pmsg1(0, _("Backspace file failed! ERR=%s\n"), strerror_dev(dev));
611 if (!bsf_dev(dev, 1)) {
612 Pmsg1(0, _("Backspace file failed! ERR=%s\n"), strerror_dev(dev));
615 Pmsg0(0, "Backspaced over two EOFs OK.\n");
616 if (!bsr_dev(dev, 1)) {
617 Pmsg1(0, _("Backspace record failed! ERR=%s\n"), strerror_dev(dev));
620 Pmsg0(0, "Backspace record OK.\n");
621 if (!read_block_from_dev(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
622 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
625 memset(rec->data, 0, rec->data_len);
626 if (!read_record_from_block(block, rec)) {
627 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
630 for (int i=0; i<len; i++) {
631 if (rec->data[i] != 3) {
632 Pmsg0(0, _("Bad data in record. Test failed!\n"));
636 Pmsg0(0, _("\nBlock re-read correct. Test succeeded!\n"));
637 Pmsg0(-1, _("=== End Write, backup, and re-read test ===\n\n"));
645 Pmsg0(0, _("This is not terribly serious since Bacula only uses\n"
646 "this function to verify the last block written to the\n"
647 "tape. Bacula will skip the last block verification\n"
649 "Backward Space Record = No\n\n"
650 "to your Storage daemon's Device resource definition.\n"));
656 * This test writes some records, then writes an end of file,
657 * rewinds the tape, moves to the end of the data and attepts
658 * to append to the tape. This function is essential for
659 * Bacula to be able to write multiple jobs to the tape.
661 static int append_test()
663 Pmsg0(-1, _("\n\n=== Append files test ===\n\n"
664 "This test is essential to Bacula.\n\n"
665 "I'm going to write one record in file 0,\n"
666 " two records in file 1,\n"
667 " and three records in file 2\n\n"));
670 weofcmd(); /* end file 0 */
673 weofcmd(); /* end file 1 */
677 weofcmd(); /* end file 2 */
679 Pmsg0(0, _("Now moving to end of medium.\n"));
681 Pmsg2(-1, _("We should be in file 3. I am at file %d. This is %s\n"),
682 dev->file, dev->file == 3 ? "correct!" : "NOT correct!!!!");
684 if (dev->file != 3) {
688 Pmsg0(-1, _("\nNow the important part, I am going to attempt to append to the tape.\n\n"));
692 Pmsg0(-1, _("Done appending, there should be no I/O errors\n\n"));
693 Pmsg0(-1, "Doing Bacula scan of blocks:\n");
695 Pmsg0(-1, _("End scanning the tape.\n"));
696 Pmsg2(-1, _("We should be in file 4. I am at file %d. This is %s\n"),
697 dev->file, dev->file == 4 ? "correct!" : "NOT correct!!!!");
699 if (dev->file != 4) {
708 * This test exercises the autochanger
710 static int autochanger_test()
712 POOLMEM *results, *changer;
713 int slot, status, loaded;
717 if (!dev_cap(dev, CAP_AUTOCHANGER)) {
720 if (!(jcr->device && jcr->device->changer_name && jcr->device->changer_command)) {
721 Pmsg0(-1, "\nAutochanger enabled, but no name or no command device specified.\n");
725 Pmsg0(-1, "\nTo test the autochanger you must have a blank tape in Slot 1.\n"
726 "I'm going to write on it.\n");
727 if (!get_cmd("\nDo you wish to continue with the Autochanger test? (y/n): ")) {
730 if (cmd[0] != 'y' && cmd[0] != 'Y') {
734 Pmsg0(-1, _("\n\n=== Autochanger test ===\n\n"));
736 results = get_pool_memory(PM_MESSAGE);
737 changer = get_pool_memory(PM_FNAME);
741 jcr->VolCatInfo.Slot = slot;
742 /* Find out what is loaded, zero means device is unloaded */
743 Pmsg0(-1, _("3301 Issuing autochanger \"loaded\" command.\n"));
744 changer = edit_device_codes(jcr, changer, jcr->device->changer_command,
746 status = run_program(changer, timeout, results);
747 Dmsg3(100, "run_prog: %s stat=%d result=%s\n", changer, status, results);
749 loaded = atoi(results);
751 Pmsg1(-1, _("3991 Bad autochanger \"load slot\" status=%d.\n"), status);
752 loaded = -1; /* force unload */
756 Pmsg1(-1, "Slot %d loaded. I am going to unload it.\n", loaded);
758 Pmsg0(-1, "Nothing loaded into the drive. OK.\n");
760 Dmsg1(100, "Results from loaded query=%s\n", results);
762 offline_or_rewind_dev(dev);
763 /* We are going to load a new tape, so close the device */
764 force_close_dev(dev);
765 Pmsg0(-1, _("3302 Issuing autochanger \"unload\" command.\n"));
766 changer = edit_device_codes(jcr, changer,
767 jcr->device->changer_command, "unload");
768 status = run_program(changer, timeout, NULL);
769 Pmsg2(-1, "unload status=%s %d\n", status==0?"OK":"Bad", status);
775 Pmsg1(-1, _("3303 Issuing autochanger \"load slot %d\" command.\n"), slot);
776 changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "load");
777 Dmsg1(200, "Changer=%s\n", changer);
778 status = run_program(changer, timeout, NULL);
780 Pmsg1(-1, _("3304 Autochanger \"load slot %d\" status is OK.\n"), slot);
782 Pmsg1(-1, _("3992 Bad autochanger \"load slot\" status=%d.\n"), status);
786 if (!open_the_device()) {
789 bmicrosleep(sleep_time, 0);
790 if (!rewind_dev(dev)) {
791 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
792 clrerror_dev(dev, -1);
793 Pmsg0(-1, "\nThe test failed, probably because you need to put\n"
794 "a longer sleep time in the mtx-script in the load) case.\n"
795 "Adding a 30 second sleep and trying again ...\n");
799 Pmsg1(0, "Rewound %s\n", dev_name(dev));
802 if ((status = weof_dev(dev, 1)) < 0) {
803 Pmsg2(0, "Bad status from weof %d. ERR=%s\n", status, strerror_dev(dev));
806 Pmsg1(0, "Wrote EOF to %s\n", dev_name(dev));
810 Pmsg1(-1, "\nThe test worked this time. Please add:\n\n"
812 "to your mtx-changer script in the load) case.\n\n",
815 Pmsg0(-1, "\nThe test autochanger worked!!\n\n");
818 free_pool_memory(changer);
819 free_pool_memory(results);
824 free_pool_memory(changer);
825 free_pool_memory(results);
826 Pmsg0(-1, "You must correct this error or the Autochanger will not work.\n");
830 static void autochangercmd()
837 * This test assumes that the append test has been done,
838 * then it tests the fsf function.
840 static int fsf_test()
842 bool set_off = false;
844 Pmsg0(-1, _("\n\n=== Forward space files test ===\n\n"
845 "This test is essential to Bacula.\n\n"
846 "I'm going to write five files then test forward spacing\n\n"));
849 weofcmd(); /* end file 0 */
852 weofcmd(); /* end file 1 */
856 weofcmd(); /* end file 2 */
859 weofcmd(); /* end file 3 */
861 weofcmd(); /* end file 4 */
865 Pmsg0(0, _("Now forward spacing 1 file.\n"));
866 if (!fsf_dev(dev, 1)) {
867 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
870 Pmsg2(-1, _("We should be in file 1. I am at file %d. This is %s\n"),
871 dev->file, dev->file == 1 ? "correct!" : "NOT correct!!!!");
873 if (dev->file != 1) {
877 Pmsg0(0, _("Now forward spacing 2 files.\n"));
878 if (!fsf_dev(dev, 2)) {
879 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
882 Pmsg2(-1, _("We should be in file 3. I am at file %d. This is %s\n"),
883 dev->file, dev->file == 3 ? "correct!" : "NOT correct!!!!");
885 if (dev->file != 3) {
890 Pmsg0(0, _("Now forward spacing 4 files.\n"));
891 if (!fsf_dev(dev, 4)) {
892 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
895 Pmsg2(-1, _("We should be in file 4. I am at file %d. This is %s\n"),
896 dev->file, dev->file == 4 ? "correct!" : "NOT correct!!!!");
898 if (dev->file != 4) {
902 Pmsg0(-1, "The test worked this time. Please add:\n\n"
903 " Fast Forward Space File = no\n\n"
904 "to your Device resource for this drive.\n");
909 Pmsg0(-1, _("\nThe forward space file test failed.\n"));
910 if (dev_cap(dev, CAP_FASTFSF)) {
911 Pmsg0(-1, "You have Fast Forward Space File enabled.\n"
912 "I am turning it off then retring the test.\n");
913 dev->capabilities &= ~CAP_FASTFSF;
917 Pmsg0(-1, "You must correct this error or Bacula will not work.\n");
926 * This is a general test of Bacula's functions
927 * needed to read and write the tape.
929 static void testcmd()
933 stat = append_test();
934 if (stat == 1) { /* OK get out */
937 if (stat == -1) { /* first test failed */
938 if (dev_cap(dev, CAP_EOM)) {
939 Pmsg0(-1, "\nAppend test failed. Attempting again.\n"
940 "Setting \"Hardware End of Medium = no\n"
941 " and \"Fast Forward Space File = no\n"
942 "and retrying append test.\n\n");
943 dev->capabilities &= ~CAP_EOM; /* turn off eom */
944 dev->capabilities &= ~CAP_FASTFSF; /* turn off fast fsf */
945 stat = append_test();
947 Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n"
948 " Hardware End of Medium = No\n\n"
949 " Fast Forward Space File = No\n"
950 "to your Device resource in the Storage conf file.\n");
954 Pmsg0(-1, "\n\nThat appears not to have corrected the problem.\n");
957 /* Wrong count after append */
959 Pmsg0(-1, "\n\nIt looks like the append failed. Attempting again.\n"
960 "Setting \"BSF at EOM = yes\" and retrying append test.\n");
961 dev->capabilities |= CAP_BSFATEOM; /* backspace on eom */
962 stat = append_test();
964 Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n"
965 " Hardware End of Medium = No\n"
966 " Fast Forward Space File = No\n"
967 " BSF at EOM = yes\n\n"
968 "to your Device resource in the Storage conf file.\n");
974 Pmsg0(-1, "\nAppend test failed.\n\n");
975 Pmsg0(-1, "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
976 "Unable to correct the problem. You MUST fix this\n"
977 "problem before Bacula can use your tape drive correctly\n");
978 Pmsg0(-1, "\nPerhaps running Bacula in fixed block mode will work.\n"
979 "Do so by setting:\n\n"
980 "Minimum Block Size = nnn\n"
981 "Maximum Block Size = nnn\n\n"
982 "in your Storage daemon's Device definition.\n"
983 "nnn must match your tape driver's block size.\n"
984 "This, however, is not really an ideal solution.\n");
988 Pmsg0(-1, _("\nThe above Bacula scan should have output identical to what follows.\n"
989 "Please double check it ...\n"
990 "=== Sample correct output ===\n"
991 "1 block of 64448 bytes in file 1\n"
992 "End of File mark.\n"
993 "2 blocks of 64448 bytes in file 2\n"
994 "End of File mark.\n"
995 "3 blocks of 64448 bytes in file 3\n"
996 "End of File mark.\n"
997 "1 block of 64448 bytes in file 4\n"
998 "End of File mark.\n"
999 "Total files=4, blocks=7, bytes = 451,136\n"
1000 "=== End sample correct output ===\n\n"));
1002 Pmsg0(-1, _("If the above scan output is not identical to the\n"
1003 "sample output, you MUST correct the problem\n"
1004 "or Bacula will not be able to write multiple Jobs to \n"
1008 re_read_block_test();
1011 fsf_test(); /* do fast forward space file test */
1013 autochanger_test(); /* do autochanger test */
1015 Pmsg0(-1, _("\n=== End Append files test ===\n"));
1019 /* Forward space a file */
1020 static void fsfcmd()
1022 if (!fsf_dev(dev, 1)) {
1023 Pmsg1(0, "Bad status from fsf. ERR=%s\n", strerror_dev(dev));
1026 Pmsg0(0, "Forward spaced one file.\n");
1029 /* Forward space a record */
1030 static void fsrcmd()
1032 if (!fsr_dev(dev, 1)) {
1033 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1036 Pmsg0(0, "Forward spaced one record.\n");
1041 * Write a Bacula block to the tape
1049 sm_check(__FILE__, __LINE__, False);
1050 block = new_block(dev);
1052 dump_block(block, "test");
1054 i = block->buf_len - 100;
1056 rec->data = check_pool_memory_size(rec->data, i);
1057 memset(rec->data, i & 0xFF, i);
1059 sm_check(__FILE__, __LINE__, False);
1060 if (!write_record_to_block(block, rec)) {
1061 Pmsg0(0, _("Error writing record to block.\n"));
1064 if (!write_block_to_dev(jcr, dev, block)) {
1065 Pmsg0(0, _("Error writing block to device.\n"));
1068 Pmsg1(0, _("Wrote one record of %d bytes.\n"), i);
1070 Pmsg0(0, _("Wrote block to device.\n"));
1073 sm_check(__FILE__, __LINE__, False);
1076 sm_check(__FILE__, __LINE__, False);
1080 * Read a record from the tape
1087 if (!get_cmd("Enter length to read: ")) {
1091 if (len < 0 || len > 1000000) {
1092 Pmsg0(0, _("Bad length entered, using default of 1024 bytes.\n"));
1095 buf = (char *)malloc(len);
1096 stat = read(dev->fd, buf, len);
1097 if (stat > 0 && stat <= len) {
1100 Pmsg3(0, _("Read of %d bytes gives stat=%d. ERR=%s\n"),
1101 len, stat, strerror(errno));
1107 * Scan tape by reading block by block. Report what is
1108 * on the tape. Note, this command does raw reads, and as such
1109 * will not work with fixed block size devices.
1111 static void scancmd()
1114 int blocks, tot_blocks, tot_files;
1120 blocks = block_size = tot_blocks = 0;
1122 if (dev->state & ST_EOT) {
1123 Pmsg0(0, "End of tape\n");
1126 update_pos_dev(dev);
1127 tot_files = dev->file;
1128 Pmsg1(0, _("Starting scan at file %u\n"), dev->file);
1130 if ((stat = read(dev->fd, buf, sizeof(buf))) < 0) {
1131 clrerror_dev(dev, -1);
1132 Mmsg2(&dev->errmsg, "read error on %s. ERR=%s.\n",
1133 dev->dev_name, strerror(dev->dev_errno));
1134 Pmsg2(0, "Bad status from read %d. ERR=%s\n", stat, strerror_dev(dev));
1136 printf("%d block%s of %d bytes in file %d\n",
1137 blocks, blocks>1?"s":"", block_size, dev->file);
1140 Dmsg1(200, "read status = %d\n", stat);
1142 if (stat != block_size) {
1143 update_pos_dev(dev);
1145 printf("%d block%s of %d bytes in file %d\n",
1146 blocks, blocks>1?"s":"", block_size, dev->file);
1151 if (stat == 0) { /* EOF */
1152 update_pos_dev(dev);
1153 printf("End of File mark.\n");
1154 /* Two reads of zero means end of tape */
1155 if (dev->state & ST_EOF)
1156 dev->state |= ST_EOT;
1158 dev->state |= ST_EOF;
1161 if (dev->state & ST_EOT) {
1162 printf("End of tape\n");
1165 } else { /* Got data */
1166 dev->state &= ~ST_EOF;
1172 update_pos_dev(dev);
1173 tot_files = dev->file - tot_files;
1174 printf("Total files=%d, blocks=%d, bytes = %s\n", tot_files, tot_blocks,
1175 edit_uint64_with_commas(bytes, ec1));
1180 * Scan tape by reading Bacula block by block. Report what is
1181 * on the tape. This function reads Bacula blocks, so if your
1182 * Device resource is correctly defined, it should work with
1183 * either variable or fixed block sizes.
1185 static void scan_blocks()
1187 int blocks, tot_blocks, tot_files;
1188 uint32_t block_size;
1193 block = new_block(dev);
1194 blocks = block_size = tot_blocks = 0;
1197 update_pos_dev(dev);
1198 tot_files = dev->file;
1200 if (!read_block_from_device(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
1201 Dmsg1(100, "!read_block(): ERR=%s\n", strerror_dev(dev));
1202 if (dev->state & ST_EOT) {
1204 printf("%d block%s of %d bytes in file %d\n",
1205 blocks, blocks>1?"s":"", block_size, dev->file);
1210 if (dev->state & ST_EOF) {
1212 printf("%d block%s of %d bytes in file %d\n",
1213 blocks, blocks>1?"s":"", block_size, dev->file);
1216 printf(_("End of File mark.\n"));
1219 if (dev->state & ST_SHORT) {
1221 printf("%d block%s of %d bytes in file %d\n",
1222 blocks, blocks>1?"s":"", block_size, dev->file);
1225 printf(_("Short block read.\n"));
1228 printf(_("Error reading block. ERR=%s\n"), strerror_dev(dev));
1231 if (block->block_len != block_size) {
1233 printf("%d block%s of %d bytes in file %d\n",
1234 blocks, blocks>1?"s":"", block_size, dev->file);
1237 block_size = block->block_len;
1241 bytes += block->block_len;
1242 Dmsg6(100, "Blk_blk=%u dev_blk=%u blen=%u bVer=%d SessId=%u SessTim=%u\n",
1243 block->BlockNumber, dev->block_num, block->block_len, block->BlockVer,
1244 block->VolSessionId, block->VolSessionTime);
1246 DEV_RECORD *rec = new_record();
1247 read_record_from_block(block, rec);
1248 Pmsg8(-1, "Blk_block: %u dev_blk=%u blen=%u First rec FI=%s SessId=%u SessTim=%u Strm=%s rlen=%d\n",
1249 block->BlockNumber, dev->block_num, block->block_len,
1250 FI_to_ascii(rec->FileIndex), rec->VolSessionId, rec->VolSessionTime,
1251 stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len);
1254 } else if (verbose > 1) {
1255 dump_block(block, "");
1261 tot_files = dev->file - tot_files;
1262 printf("Total files=%d, blocks=%d, bytes = %s\n", tot_files, tot_blocks,
1263 edit_uint64_with_commas(bytes, ec1));
1267 static void statcmd()
1273 debug = debug_level;
1275 if (!status_dev(dev, &status)) {
1276 Pmsg2(0, "Bad status from status %d. ERR=%s\n", stat, strerror_dev(dev));
1279 dump_volume_label(dev);
1281 debug_level = debug;
1286 * First we label the tape, then we fill
1287 * it with data get a new tape and write a few blocks.
1289 static void fillcmd()
1300 This command simulates Bacula writing to a tape.\n\
1301 It requires either one or two blank tapes, which it\n\
1302 will label and write. It will print a status approximately\n\
1303 every 322 MB, and write an EOF every 3.2 GB. If you have\n\
1304 selected the simple test option, after writing the first tape\n\
1305 it will rewind it and re-read the last block written.\n\
1306 If you have selected the multiple tape test, when the first tape\n\
1307 fills, it will ask for a second, and after writing a few more \n\
1308 blocks, it will stop. Then it will begin re-reading the\n\
1310 This may take a long time -- hours! ...\n\n");
1312 get_cmd("Insert a blank tape then indicate if you want\n"
1313 "to run the simplified test (s) with one tape or\n"
1314 "the complete multiple tape (m) test: (s/m) ");
1315 if (cmd[0] == 's') {
1316 Pmsg0(-1, "Simple test (single tape) selected.\n");
1318 } else if (cmd[0] == 'm') {
1319 Pmsg0(-1, "Complete multiple tape test selected.\n");
1322 Pmsg0(000, "Command aborted.\n");
1326 set_volume_name("TestVolume1", 1);
1331 Dmsg1(20, "Begin append device=%s\n", dev_name(dev));
1333 block = new_block(dev);
1336 * Acquire output device for writing. Note, after acquiring a
1337 * device, we MUST release it, which is done at the end of this
1340 Dmsg0(100, "just before acquire_device\n");
1341 if (!(dev=acquire_device_for_append(jcr, dev, block))) {
1342 set_jcr_job_status(jcr, JS_ErrorTerminated);
1347 Dmsg0(100, "Just after acquire_device_for_append\n");
1349 * Write Begin Session Record
1351 if (!write_session_label(jcr, block, SOS_LABEL)) {
1352 set_jcr_job_status(jcr, JS_ErrorTerminated);
1353 Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"),
1357 Pmsg0(-1, "Wrote Start Of Session label.\n");
1359 memset(&rec, 0, sizeof(rec));
1360 rec.data = get_memory(100000); /* max record size */
1362 #define REC_SIZE 32768
1363 rec.data_len = REC_SIZE;
1366 * Generate data as if from File daemon, write to device
1368 jcr->VolFirstIndex = 0;
1369 time(&jcr->run_time); /* start counting time for rates */
1370 Pmsg0(-1, "Begin writing Bacula records to first tape ...\n");
1371 Pmsg1(-1, "Block num = %d\n", dev->block_num);
1372 for (file_index = 0; ok && !job_canceled(jcr); ) {
1373 rec.VolSessionId = jcr->VolSessionId;
1374 rec.VolSessionTime = jcr->VolSessionTime;
1375 rec.FileIndex = ++file_index;
1376 rec.Stream = STREAM_FILE_DATA;
1379 * Fill the buffer with the file_index negated. Negation ensures that
1380 * more bits are turned on.
1382 uint64_t *lp = (uint64_t *)rec.data;
1383 for (uint32_t i=0; i < (rec.data_len-sizeof(uint64_t))/sizeof(uint64_t); i++) {
1384 *lp++ = ~file_index;
1387 Dmsg4(250, "before writ_rec FI=%d SessId=%d Strm=%s len=%d\n",
1388 rec.FileIndex, rec.VolSessionId, stream_to_ascii(rec.Stream, rec.FileIndex),
1391 while (!write_record_to_block(block, &rec)) {
1393 * When we get here we have just filled a block
1395 Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec.data_len,
1398 /* Write block to tape */
1399 if (!flush_block(block, 1)) {
1403 /* Every 5000 blocks (approx 322MB) report where we are.
1405 if ((block->BlockNumber % 5000) == 0) {
1407 now -= jcr->run_time;
1411 kbs = (double)dev->VolCatInfo.VolCatBytes / (1000.0 * (double)now);
1412 Pmsg4(-1, "Wrote block=%u, blk_num=%d VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
1414 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), (float)kbs);
1416 /* Every 15000 blocks (approx 1GB) write an EOF.
1418 if ((block->BlockNumber % 15000) == 0) {
1419 Pmsg0(-1, "Flush block, write EOF\n");
1420 flush_block(block, 0);
1424 /* Get out after writing 10 blocks to the second tape */
1425 if (++BlockNumber > 10 && stop != 0) { /* get out */
1430 Pmsg0(000, _("Not OK\n"));
1433 jcr->JobBytes += rec.data_len; /* increment bytes this job */
1434 Dmsg4(190, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
1435 FI_to_ascii(rec.FileIndex), rec.VolSessionId,
1436 stream_to_ascii(rec.Stream, rec.FileIndex), rec.data_len);
1438 /* Get out after writing 10 blocks to the second tape */
1439 if (BlockNumber > 10 && stop != 0) { /* get out */
1440 Pmsg0(-1, "Done writing ...\n");
1445 Dmsg0(100, "Write_end_session_label()\n");
1446 /* Create Job status for end of session label */
1447 if (!job_canceled(jcr) && ok) {
1448 set_jcr_job_status(jcr, JS_Terminated);
1450 set_jcr_job_status(jcr, JS_ErrorTerminated);
1452 if (!write_session_label(jcr, block, EOS_LABEL)) {
1453 Pmsg1(000, _("Error writting end session label. ERR=%s\n"), strerror_dev(dev));
1456 /* Write out final block of this session */
1457 if (!write_block_to_device(jcr, dev, block)) {
1458 Pmsg0(-1, _("Set ok=FALSE after write_block_to_device.\n"));
1461 Pmsg0(-1, "Wrote End Of Session label.\n");
1464 /* Release the device */
1465 if (!release_device(jcr, dev)) {
1466 Pmsg0(-1, _("Error in release_device\n"));
1471 free_memory(rec.data);
1473 dump_block(last_block, _("Last block written to tape.\n"));
1475 Pmsg0(-1, _("\n\nDone filling tape. Now beginning re-read of tape ...\n"));
1481 * Read two tapes written by the "fill" command and ensure
1482 * that the data is valid. If stop==1 we simulate full read back
1483 * of two tapes. If stop==-1 we simply read the last block and
1484 * verify that it is correct.
1486 static void unfillcmd()
1493 block = new_block(dev);
1495 dev->capabilities |= CAP_ANONVOLS; /* allow reading any volume */
1496 dev->capabilities &= ~CAP_LABEL; /* don't label anything here */
1501 /* Close device so user can use autochanger if desired */
1502 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
1505 force_close_dev(dev);
1506 get_cmd(_("Mount first tape. Press enter when ready: "));
1509 set_volume_name("TestVolume1", 1);
1511 create_vol_list(jcr);
1513 dev->state &= ~ST_READ;
1514 if (!acquire_device_for_read(jcr, dev, block)) {
1515 Pmsg1(-1, "%s", dev->errmsg);
1520 time(&jcr->run_time); /* start counting time for rates */
1524 /* Read all records and then second tape */
1525 read_records(jcr, dev, record_cb, my_mount_next_read_volume);
1528 * Simplified test, we simply fsf to file, then read the
1529 * last block and make sure it is the same as the saved block.
1531 Pmsg0(000, "Rewinding tape ...\n");
1532 if (!rewind_dev(dev)) {
1533 Pmsg1(-1, _("Error rewinding: ERR=%s\n"), strerror_dev(dev));
1536 if (last_file > 0) {
1537 Pmsg1(000, "Forward spacing to last file=%u\n", last_file);
1538 if (!fsf_dev(dev, last_file)) {
1539 Pmsg1(-1, _("Error in FSF: ERR=%s\n"), strerror_dev(dev));
1543 Pmsg1(-1, _("Forward space to file %u complete. Reading blocks ...\n"),
1545 Pmsg1(-1, _("Now reading to block %u.\n"), last_block_num);
1546 for (uint32_t i=0; i <= last_block_num; i++) {
1547 if (!read_block_from_device(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
1548 Pmsg1(-1, _("Error reading blocks: ERR=%s\n"), strerror_dev(dev));
1549 Pmsg2(-1, _("Wanted block %u error at block %u\n"), last_block_num, i);
1552 if (i > 0 && i % 1000 == 0) {
1553 Pmsg1(-1, _("At block %u\n"), i);
1557 dump_block(last_block, _("Last block written"));
1558 dump_block(block, _("Block read back"));
1559 Pmsg0(-1, _("Except for the buffer address, the contents of\n"
1560 "the above two block dumps should be the same.\n"
1561 "If not you have a problem ...\n"));
1568 Pmsg0(000, _("Done with reread of fill data.\n"));
1572 * We are called here from "unfill" for each record on the tape.
1574 static int record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
1576 SESSION_LABEL label;
1578 if (stop > 1 && !dumped) { /* on second tape */
1580 dump_block(block, "First block on second tape");
1581 Pmsg4(-1, "Blk: FileIndex=%d: block=%u size=%d vol=%s\n",
1582 rec->FileIndex, block->BlockNumber, block->block_len, dev->VolHdr.VolName);
1583 Pmsg6(-1, " Rec: VId=%d VT=%d FI=%s Strm=%s len=%d state=%x\n",
1584 rec->VolSessionId, rec->VolSessionTime,
1585 FI_to_ascii(rec->FileIndex), stream_to_ascii(rec->Stream, rec->FileIndex),
1586 rec->data_len, rec->state);
1588 if (rec->FileIndex < 0) {
1590 dump_label_record(dev, rec, 1);
1592 switch (rec->FileIndex) {
1594 Pmsg0(-1, "Volume is prelabeled. This tape cannot be scanned.\n");
1597 unser_volume_label(dev, rec);
1598 Pmsg3(-1, "VOL_LABEL: block=%u size=%d vol=%s\n", block->BlockNumber,
1599 block->block_len, dev->VolHdr.VolName);
1603 unser_session_label(&label, rec);
1604 Pmsg1(-1, "SOS_LABEL: JobId=%u\n", label.JobId);
1607 unser_session_label(&label, rec);
1608 Pmsg2(-1, "EOS_LABEL: block=%u JobId=%u\n", block->BlockNumber,
1612 Pmsg0(-1, "EOM_LABEL:\n");
1614 case EOT_LABEL: /* end of all tapes */
1617 if (LastBlock != block->BlockNumber) {
1618 VolBytes += block->block_len;
1620 LastBlock = block->BlockNumber;
1622 now -= jcr->run_time;
1626 kbs = (double)VolBytes / (1000 * now);
1627 Pmsg3(000, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
1628 edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
1630 Pmsg0(000, "End of all tapes.\n");
1638 if (++file_index != rec->FileIndex) {
1639 Pmsg3(000, "Incorrect FileIndex in Block %u. Got %d, expected %d.\n",
1640 block->BlockNumber, rec->FileIndex, file_index);
1643 * Now check that the right data is in the record.
1645 uint64_t *lp = (uint64_t *)rec->data;
1646 uint64_t val = ~file_index;
1647 for (uint32_t i=0; i < (REC_SIZE-sizeof(uint64_t))/sizeof(uint64_t); i++) {
1649 Pmsg2(000, "Record %d contains bad data in Block %u.\n",
1650 file_index, block->BlockNumber);
1655 if (LastBlock != block->BlockNumber) {
1656 VolBytes += block->block_len;
1658 if ((block->BlockNumber != LastBlock) && (block->BlockNumber % 50000) == 0) {
1661 now -= jcr->run_time;
1665 kbs = (double)VolBytes / (1000 * now);
1666 Pmsg3(000, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
1667 edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
1669 LastBlock = block->BlockNumber;
1671 Pmsg1(000, "End of all blocks. Block=%u\n", block->BlockNumber);
1679 * Write current block to tape regardless of whether or
1680 * not it is full. If the tape fills, attempt to
1681 * acquire another tape.
1683 static int flush_block(DEV_BLOCK *block, int dump)
1688 uint32_t this_file, this_block_num;
1691 this_block = new_block(dev);
1694 free_memory(this_block->buf);
1695 memcpy(this_block, block, sizeof(DEV_BLOCK));
1696 this_block->buf = get_memory(block->buf_len);
1697 memcpy(this_block->buf, block->buf, this_block->buf_len);
1698 this_file = dev->file;
1699 this_block_num = dev->block_num;
1700 if (!write_block_to_dev(jcr, dev, block)) {
1701 Pmsg3(000, "Block not written: FileIndex=%u Block=%u Size=%u\n",
1702 (unsigned)file_index, block->BlockNumber, block->block_len);
1703 Pmsg2(000, "last_block_num=%u this_block_num=%d\n", last_block_num,
1706 dump_block(block, "Block not written");
1709 eot_block = block->BlockNumber;
1710 eot_block_len = block->block_len;
1711 eot_FileIndex = file_index;
1714 now -= jcr->run_time;
1718 kbs = (double)dev->VolCatInfo.VolCatBytes / (1000 * now);
1719 vol_size = dev->VolCatInfo.VolCatBytes;
1720 Pmsg2(000, "End of tape. VolumeCapacity=%s. Write rate = %.1f KB/s\n",
1721 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), kbs);
1724 stop = -1; /* stop, but do simplified test */
1726 /* Full test in progress */
1727 if (!fixup_device_block_write_error(jcr, dev, block)) {
1728 Pmsg1(000, _("Cannot fixup device error. %s\n"), strerror_dev(dev));
1734 BlockNumber = 0; /* start counting for second tape */
1737 return 1; /* end of tape reached */
1741 * Toggle between two allocated blocks for efficiency.
1742 * Switch blocks so that the block just successfully written is
1743 * always in last_block.
1745 tblock = last_block;
1746 last_block = this_block;
1747 this_block = tblock;
1748 last_file = this_file;
1749 last_block_num = this_block_num;
1757 * First we label the tape, then we fill
1758 * it with data get a new tape and write a few blocks.
1760 static void qfillcmd()
1766 Pmsg0(0, "Test writing blocks of 64512 bytes to tape.\n");
1768 get_cmd("How many blocks do you want to write? (1000): ");
1775 sm_check(__FILE__, __LINE__, False);
1776 block = new_block(dev);
1779 i = block->buf_len - 100;
1781 rec->data = check_pool_memory_size(rec->data, i);
1782 memset(rec->data, i & 0xFF, i);
1785 Pmsg1(0, "Begin writing %d Bacula blocks to tape ...\n", count);
1786 for (i=0; i < count; i++) {
1791 if (!write_record_to_block(block, rec)) {
1792 Pmsg0(0, _("Error writing record to block.\n"));
1795 if (!write_block_to_dev(jcr, dev, block)) {
1796 Pmsg0(0, _("Error writing block to device.\n"));
1807 sm_check(__FILE__, __LINE__, False);
1810 sm_check(__FILE__, __LINE__, False);
1815 * Fill a tape using raw write() command
1817 static void rawfill_cmd()
1822 uint32_t block_num = 0;
1826 block = new_block(dev);
1827 fd = open("/dev/urandom", O_RDONLY);
1829 read(fd, block->buf, block->buf_len);
1831 Pmsg0(0, "Cannot open /dev/urandom.\n");
1835 p = (uint32_t *)block->buf;
1836 Pmsg1(0, "Begin writing raw blocks of %u bytes.\n", block->buf_len);
1839 stat = write(dev->fd, block->buf, block->buf_len);
1840 if (stat == (int)block->buf_len) {
1841 if ((block_num++ % 100) == 0) {
1851 printf("Write failed at block %u. stat=%d ERR=%s\n", block_num, stat,
1852 strerror(my_errno));
1859 * Fill a tape using raw write() command
1861 static void bfill_cmd()
1864 uint32_t block_num = 0;
1869 block = new_block(dev);
1870 fd = open("/dev/urandom", O_RDONLY);
1872 read(fd, block->buf, block->buf_len);
1874 Pmsg0(0, "Cannot open /dev/urandom.\n");
1878 p = (uint32_t *)block->buf;
1879 Pmsg1(0, "Begin writing Bacula blocks of %u bytes.\n", block->buf_len);
1882 block->binbuf = block->buf_len;
1883 block->bufp = block->buf + block->binbuf;
1884 if (!write_block_to_dev(jcr, dev, block)) {
1887 if ((block_num++ % 100) == 0) {
1894 printf("Write failed at block %u.\n", block_num);
1900 struct cmdstruct { char *key; void (*func)(); char *help; };
1901 static struct cmdstruct commands[] = {
1902 {"autochanger", autochangercmd, "test autochanger"},
1903 {"bsf", bsfcmd, "backspace file"},
1904 {"bsr", bsrcmd, "backspace record"},
1905 {"bfill", bfill_cmd, "fill tape using Bacula writes"},
1906 {"cap", capcmd, "list device capabilities"},
1907 {"clear", clearcmd, "clear tape errors"},
1908 {"eod", eodcmd, "go to end of Bacula data for append"},
1909 {"eom", eomcmd, "go to the physical end of medium"},
1910 {"fill", fillcmd, "fill tape, write onto second volume"},
1911 {"unfill", unfillcmd, "read filled tape"},
1912 {"fsf", fsfcmd, "forward space a file"},
1913 {"fsr", fsrcmd, "forward space a record"},
1914 {"help", helpcmd, "print this command"},
1915 {"label", labelcmd, "write a Bacula label to the tape"},
1916 {"load", loadcmd, "load a tape"},
1917 {"quit", quitcmd, "quit btape"},
1918 {"rawfill", rawfill_cmd, "use write() to fill tape"},
1919 {"readlabel", readlabelcmd, "read and print the Bacula tape label"},
1920 {"rectest", rectestcmd, "test record handling functions"},
1921 {"rewind", rewindcmd, "rewind the tape"},
1922 {"scan", scancmd, "read() tape block by block to EOT and report"},
1923 {"scanblocks", scan_blocks, "Bacula read block by block to EOT and report"},
1924 {"status", statcmd, "print tape status"},
1925 {"test", testcmd, "General test Bacula tape functions"},
1926 {"weof", weofcmd, "write an EOF on the tape"},
1927 {"wr", wrcmd, "write a single Bacula block"},
1928 {"rr", rrcmd, "read a single record"},
1929 {"qfill", qfillcmd, "quick fill command"},
1931 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
1939 while (get_cmd("*")) {
1940 sm_check(__FILE__, __LINE__, False);
1942 for (i=0; i<comsize; i++) /* search for command */
1943 if (fstrsch(cmd, commands[i].key)) {
1944 (*commands[i].func)(); /* go execute command */
1949 Pmsg1(0, _("%s is an illegal command\n"), cmd);
1955 static void helpcmd()
1959 printf(_("Interactive commands:\n"));
1960 printf(_(" Command Description\n ======= ===========\n"));
1961 for (i=0; i<comsize; i++)
1962 printf(" %-10s %s\n", commands[i].key, commands[i].help);
1969 "\nVersion: " VERSION " (" BDATE ")\n\n"
1970 "Usage: btape <options> <device_name>\n"
1971 " -c <file> set configuration file to file\n"
1972 " -d <nn> set debug level to nn\n"
1973 " -s turn off signals\n"
1974 " -t open the default tape device\n"
1975 " -? print this message.\n"
1981 * Get next input command from terminal. This
1982 * routine is REALLY primitive, and should be enhanced
1983 * to have correct backspacing, etc.
1986 get_cmd(char *prompt)
1990 fprintf(stdout, prompt);
1992 /* We really should turn off echoing and pretty this
1996 while ((ch = fgetc(stdin)) != EOF) {
1998 strip_trailing_junk(cmd);
2000 } else if (ch == 4 || ch == 0xd3 || ch == 0x8) {
2013 /* Dummies to replace askdir.c */
2014 int dir_get_volume_info(JCR *jcr, enum get_vol_info_rw writing) { return 1;}
2015 int dir_update_volume_info(JCR *jcr, DEVICE *dev, int relabel) { return 1; }
2016 int dir_create_jobmedia_record(JCR *jcr) { return 1; }
2017 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
2018 int dir_send_job_status(JCR *jcr) {return 1;}
2022 int dir_find_next_appendable_volume(JCR *jcr)
2027 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
2029 /* Close device so user can use autochanger if desired */
2030 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2033 force_close_dev(dev);
2034 Pmsg1(-1, "%s", dev->errmsg); /* print reason */
2035 fprintf(stderr, "Mount Volume \"%s\" on device %s and press return when ready: ",
2036 jcr->VolumeName, dev_name(dev));
2041 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev)
2043 /* Close device so user can use autochanger if desired */
2044 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2047 force_close_dev(dev);
2048 fprintf(stderr, "Mount next Volume on device %s and press return when ready: ",
2051 set_volume_name("TestVolume2", 2);
2059 static int my_mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
2063 Pmsg1(000, "End of Volume \"%s\"\n", jcr->VolumeName);
2065 if (LastBlock != block->BlockNumber) {
2066 VolBytes += block->block_len;
2068 LastBlock = block->BlockNumber;
2070 now -= jcr->run_time;
2074 kbs = (double)VolBytes / (1000.0 * (double)now);
2075 Pmsg3(-1, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
2076 edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
2078 if (strcmp(jcr->VolumeName, "TestVolume2") == 0) {
2084 set_volume_name("TestVolume2", 2);
2086 create_vol_list(jcr);
2088 dev->state &= ~ST_READ;
2089 if (!acquire_device_for_read(jcr, dev, block)) {
2090 Pmsg2(0, "Cannot open Dev=%s, Vol=%s\n", dev_name(dev), jcr->VolumeName);
2093 return 1; /* next volume mounted */
2096 static void set_volume_name(char *VolName, int volnum)
2098 VolumeName = VolName;
2100 pm_strcpy(&jcr->VolumeName, VolName);
2101 bstrncpy(dev->VolCatInfo.VolCatName, VolName, sizeof(dev->VolCatInfo.VolCatName));
2105 * Edit codes into ChangerCommand
2107 * %a = archive device name
2108 * %c = changer device name
2109 * %f = Client's name
2117 * omsg = edited output message
2118 * imsg = input string containing edit codes (%x)
2119 * cmd = command string (load, unload, ...)
2122 static char *edit_device_codes(JCR *jcr, char *omsg, char *imsg, char *cmd)
2129 Dmsg1(400, "edit_device_codes: %s\n", imsg);
2130 for (p=imsg; *p; p++) {
2137 str = dev_name(jcr->device->dev);
2140 str = NPRT(jcr->device->changer_name);
2146 sprintf(add, "%d", jcr->VolCatInfo.Slot - 1);
2150 sprintf(add, "%d", jcr->VolCatInfo.Slot);
2153 case 'j': /* Job name */
2157 str = NPRT(jcr->VolumeName);
2160 str = NPRT(jcr->client_name);
2175 Dmsg1(400, "add_str %s\n", str);
2176 pm_strcat(&omsg, (char *)str);
2177 Dmsg1(400, "omsg=%s\n", omsg);