2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2009 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
30 * Bacula Tape manipulation program
32 * Has various tape manipulation commands -- mostly for
33 * use in determining how tapes really work.
35 * Kern Sibbald, April MM
37 * Note, this program reads stored.conf, and will only
38 * talk to devices that are configured.
52 int generate_daemon_event(JCR *jcr, const char *event) { return 1; }
53 extern bool parse_sd_config(CONFIG *config, const char *configfile, int exit_code);
55 /* External subroutines */
56 extern void free_config_resources();
58 /* Exported variables */
61 int bsize = TAPE_BSIZE;
62 char VolName[MAX_NAME_LENGTH];
63 STORES *me = NULL; /* our Global resource */
64 bool forge_on = false; /* proceed inspite of I/O errors */
65 pthread_mutex_t device_release_mutex = PTHREAD_MUTEX_INITIALIZER;
66 pthread_cond_t wait_device_release = PTHREAD_COND_INITIALIZER;
69 * If you change the format of the state file,
70 * increment this value
72 static uint32_t btape_state_level = 2;
76 DEVRES *device = NULL;
79 #define REC_SIZE 32768
81 /* Forward referenced subroutines */
82 static void do_tape_cmds();
83 static void helpcmd();
84 static void scancmd();
85 static void rewindcmd();
86 static void clearcmd();
91 static void fillcmd();
92 static void qfillcmd();
93 static void statcmd();
94 static void unfillcmd();
95 static int flush_block(DEV_BLOCK *block, int dump);
96 static bool quickie_cb(DCR *dcr, DEV_RECORD *rec);
97 static bool compare_blocks(DEV_BLOCK *last_block, DEV_BLOCK *block);
98 static bool my_mount_next_read_volume(DCR *dcr);
99 static void scan_blocks();
100 static void set_volume_name(const char *VolName, int volnum);
101 static void rawfill_cmd();
102 static bool open_the_device();
103 static void autochangercmd();
104 static bool do_unfill();
107 /* Static variables */
108 static CONFIG *config;
109 #define CONFIG_FILE "bacula-sd.conf"
110 char *configfile = NULL;
112 #define MAX_CMD_ARGS 30
114 static POOLMEM *args;
115 static char *argk[MAX_CMD_ARGS];
116 static char *argv[MAX_CMD_ARGS];
119 static int quickie_count = 0;
120 static uint64_t write_count = 0;
121 static BSR *bsr = NULL;
122 static int signals = TRUE;
125 static uint64_t vol_size;
126 static uint64_t VolBytes;
129 static int32_t file_index;
130 static int end_of_tape = 0;
131 static uint32_t LastBlock = 0;
132 static uint32_t eot_block;
133 static uint32_t eot_block_len;
134 static uint32_t eot_FileIndex;
135 static int dumped = 0;
136 static DEV_BLOCK *last_block1 = NULL;
137 static DEV_BLOCK *last_block2 = NULL;
138 static DEV_BLOCK *last_block = NULL;
139 static DEV_BLOCK *this_block = NULL;
140 static DEV_BLOCK *first_block = NULL;
141 static uint32_t last_file1 = 0;
142 static uint32_t last_file2 = 0;
143 static uint32_t last_file = 0;
144 static uint32_t last_block_num1 = 0;
145 static uint32_t last_block_num2 = 0;
146 static uint32_t last_block_num = 0;
147 static uint32_t BlockNumber = 0;
148 static bool simple = true;
150 static const char *VolumeName = NULL;
151 static int vol_num = 0;
153 static JCR *jcr = NULL;
157 static void terminate_btape(int sig);
158 int get_cmd(const char *prompt);
161 /*********************************************************************
163 * Bacula tape testing program
166 int main(int margc, char *margv[])
173 setlocale(LC_ALL, "");
174 bindtextdomain("bacula", LOCALEDIR);
175 textdomain("bacula");
179 if (TAPE_BSIZE % B_DEV_BSIZE != 0 || TAPE_BSIZE / B_DEV_BSIZE == 0) {
180 Emsg2(M_ABORT, 0, _("Tape block size (%d) not multiple of system size (%d)\n"),
181 TAPE_BSIZE, B_DEV_BSIZE);
183 if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
184 Emsg1(M_ABORT, 0, _("Tape block size (%d) is not a power of 2\n"), TAPE_BSIZE);
186 if (sizeof(boffset_t) < 8) {
187 Pmsg1(-1, _("\n\n!!!! Warning large disk addressing disabled. boffset_t=%d should be 8 or more !!!!!\n\n\n"),
191 bsnprintf(buf, sizeof(buf), "%u", x32);
192 i = bsscanf(buf, "%lu", &y32);
193 if (i != 1 || x32 != y32) {
194 Pmsg3(-1, _("32 bit printf/scanf problem. i=%d x32=%u y32=%u\n"), i, x32, y32);
200 bsnprintf(buf, sizeof(buf), "%" llu, x64);
201 i = bsscanf(buf, "%llu", &y64);
202 if (i != 1 || x64 != y64) {
203 Pmsg3(-1, _("64 bit printf/scanf problem. i=%d x64=%" llu " y64=%" llu "\n"),
208 printf(_("Tape block granularity is %d bytes.\n"), TAPE_BSIZE);
210 working_directory = "/tmp";
211 my_name_is(margc, margv, "btape");
212 init_msg(NULL, NULL);
216 while ((ch = getopt(margc, margv, "b:c:d:psv?")) != -1) {
218 case 'b': /* bootstrap file */
219 bsr = parse_bsr(NULL, optarg);
220 // dump_bsr(bsr, true);
223 case 'c': /* specify config file */
224 if (configfile != NULL) {
227 configfile = bstrdup(optarg);
230 case 'd': /* set debug level */
231 if (*optarg == 't') {
232 dbg_timestamp = true;
234 debug_level = atoi(optarg);
235 if (debug_level <= 0) {
263 cmd = get_pool_memory(PM_FNAME);
264 args = get_pool_memory(PM_FNAME);
267 init_signals(terminate_btape);
270 if (configfile == NULL) {
271 configfile = bstrdup(CONFIG_FILE);
274 daemon_start_time = time(NULL);
276 config = new_config_parser();
277 parse_sd_config(config, configfile, M_ERROR_TERM);
279 /* See if we can open a device */
281 Pmsg0(000, _("No archive name specified.\n"));
284 } else if (margc != 1) {
285 Pmsg0(000, _("Improper number of arguments specified.\n"));
290 jcr = setup_jcr("btape", margv[0], bsr, NULL, 0); /* write */
299 Pmsg0(000, _("btape does not work with DVD storage.\n"));
303 if (!dev->is_tape()) {
304 Pmsg0(000, _("btape only works with tape storage.\n"));
309 if (!open_the_device()) {
313 Dmsg0(200, "Do tape commands\n");
316 terminate_btape(exit_code);
319 static void terminate_btape(int stat)
322 sm_check(__FILE__, __LINE__, false);
327 config->free_resources();
332 free_pool_memory(args);
336 free_pool_memory(cmd);
353 if (debug_level > 10)
354 print_memory_pool_stats();
357 free_block(this_block);
362 term_last_jobs_list();
363 close_memory_pool(); /* free memory in pool */
371 btime_t total_time=0;
372 uint64_t total_size=0;
374 static void init_total_speed()
380 static void print_total_speed()
383 kbs = (double)total_size / (1000 * total_time);
384 Pmsg2(000, _("TotalVolumeCapacity=%sB. Total Write rate = %.1f KB/s\n"),
385 edit_uint64_with_suffix(total_size, ec1), kbs);
388 static void init_speed()
390 time(&jcr->run_time); /* start counting time for rates */
394 static void print_speed(uint64_t bytes)
398 now -= jcr->run_time;
400 now = 1; /* don't divide by zero */
406 kbs = (double)bytes / (1000 * now);
407 Pmsg2(000, _("VolumeCapacity=%sB. Write rate = %.1f KB/s\n"),
408 edit_uint64_with_suffix(bytes, ec1), kbs);
416 static void fill_buffer(fill_mode_t mode, char *buf, uint32_t len)
421 fd = open("/dev/urandom", O_RDONLY);
426 uint32_t *p = (uint32_t *)buf;
428 for (uint32_t i=0; i<len/sizeof(uint32_t); i++) {
435 memset(buf, 0xFF, len);
443 static bool open_the_device()
448 block = new_block(dev);
450 Dmsg1(200, "Opening device %s\n", dcr->VolumeName);
451 if (dev->open(dcr, OPEN_READ_WRITE) < 0) {
452 Emsg1(M_FATAL, 0, _("dev open failed: %s\n"), dev->errmsg);
456 Pmsg1(000, _("open device %s: OK\n"), dev->print_name());
457 dev->set_append(); /* put volume in append mode */
472 * Write a label to the tape
474 static void labelcmd()
477 pm_strcpy(cmd, VolumeName);
479 if (!get_cmd(_("Enter Volume Name: "))) {
484 if (!dev->is_open()) {
485 if (!first_open_device(dcr)) {
486 Pmsg1(0, _("Device open failed. ERR=%s\n"), dev->bstrerror());
490 write_new_volume_label_to_dev(dcr, cmd, "Default", false,/*no relabel*/ true /* label dvd now */);
491 Pmsg1(-1, _("Wrote Volume label for volume \"%s\".\n"), cmd);
495 * Read the tape label
497 static void readlabelcmd()
499 int save_debug_level = debug_level;
502 stat = read_dev_volume_label(dcr);
505 Pmsg0(0, _("Volume has no label.\n"));
508 Pmsg0(0, _("Volume label read correctly.\n"));
511 Pmsg1(0, _("I/O error on device: ERR=%s"), dev->bstrerror());
514 Pmsg0(0, _("Volume name error\n"));
516 case VOL_CREATE_ERROR:
517 Pmsg1(0, _("Error creating label. ERR=%s"), dev->bstrerror());
519 case VOL_VERSION_ERROR:
520 Pmsg0(0, _("Volume version error.\n"));
522 case VOL_LABEL_ERROR:
523 Pmsg0(0, _("Bad Volume label type.\n"));
526 Pmsg0(0, _("Unknown error.\n"));
531 dump_volume_label(dev);
532 debug_level = save_debug_level;
537 * Load the tape should have prevously been taken
538 * off line, otherwise this command is not necessary.
540 static void loadcmd()
543 if (!load_dev(dev)) {
544 Pmsg1(0, _("Bad status from load. ERR=%s\n"), dev->bstrerror());
546 Pmsg1(0, _("Loaded %s\n"), dev->print_name());
552 static void rewindcmd()
554 if (!dev->rewind(dcr)) {
555 Pmsg1(0, _("Bad status from rewind. ERR=%s\n"), dev->bstrerror());
558 Pmsg1(0, _("Rewound %s\n"), dev->print_name());
563 * Clear any tape error
565 static void clearcmd()
571 * Write and end of file on the tape
573 static void weofcmd()
583 if (!dev->weof(num)) {
584 Pmsg1(0, _("Bad status from weof. ERR=%s\n"), dev->bstrerror());
588 Pmsg1(0, _("Wrote 1 EOF to %s\n"), dev->print_name());
591 Pmsg2(0, _("Wrote %d EOFs to %s\n"), num, dev->print_name());
597 /* Go to the end of the medium -- raw command
598 * The idea was orginally that the end of the Bacula
599 * medium would be flagged differently. This is not
600 * currently the case. So, this is identical to the
605 if (!dev->eod(dcr)) {
606 Pmsg1(0, "%s", dev->bstrerror());
609 Pmsg0(0, _("Moved to end of medium.\n"));
614 * Go to the end of the medium (either hardware determined
615 * or defined by two eofs.
635 if (!dev->bsf(num)) {
636 Pmsg1(0, _("Bad status from bsf. ERR=%s\n"), dev->bstrerror());
638 Pmsg2(0, _("Backspaced %d file%s.\n"), num, num==1?"":"s");
654 if (!dev->bsr(num)) {
655 Pmsg1(0, _("Bad status from bsr. ERR=%s\n"), dev->bstrerror());
657 Pmsg2(0, _("Backspaced %d record%s.\n"), num, num==1?"":"s");
662 * List device capabilities as defined in the
667 printf(_("Configured device capabilities:\n"));
668 printf("%sEOF ", dev->capabilities & CAP_EOF ? "" : "!");
669 printf("%sBSR ", dev->capabilities & CAP_BSR ? "" : "!");
670 printf("%sBSF ", dev->capabilities & CAP_BSF ? "" : "!");
671 printf("%sFSR ", dev->capabilities & CAP_FSR ? "" : "!");
672 printf("%sFSF ", dev->capabilities & CAP_FSF ? "" : "!");
673 printf("%sFASTFSF ", dev->capabilities & CAP_FASTFSF ? "" : "!");
674 printf("%sBSFATEOM ", dev->capabilities & CAP_BSFATEOM ? "" : "!");
675 printf("%sEOM ", dev->capabilities & CAP_EOM ? "" : "!");
676 printf("%sREM ", dev->capabilities & CAP_REM ? "" : "!");
677 printf("%sRACCESS ", dev->capabilities & CAP_RACCESS ? "" : "!");
678 printf("%sAUTOMOUNT ", dev->capabilities & CAP_AUTOMOUNT ? "" : "!");
679 printf("%sLABEL ", dev->capabilities & CAP_LABEL ? "" : "!");
680 printf("%sANONVOLS ", dev->capabilities & CAP_ANONVOLS ? "" : "!");
681 printf("%sALWAYSOPEN ", dev->capabilities & CAP_ALWAYSOPEN ? "" : "!");
682 printf("%sMTIOCGET ", dev->capabilities & CAP_MTIOCGET ? "" : "!");
685 printf(_("Device status:\n"));
686 printf("%sOPENED ", dev->is_open() ? "" : "!");
687 printf("%sTAPE ", dev->is_tape() ? "" : "!");
688 printf("%sLABEL ", dev->is_labeled() ? "" : "!");
689 printf("%sMALLOC ", dev->state & ST_MALLOC ? "" : "!");
690 printf("%sAPPEND ", dev->can_append() ? "" : "!");
691 printf("%sREAD ", dev->can_read() ? "" : "!");
692 printf("%sEOT ", dev->at_eot() ? "" : "!");
693 printf("%sWEOT ", dev->state & ST_WEOT ? "" : "!");
694 printf("%sEOF ", dev->at_eof() ? "" : "!");
695 printf("%sNEXTVOL ", dev->state & ST_NEXTVOL ? "" : "!");
696 printf("%sSHORT ", dev->state & ST_SHORT ? "" : "!");
699 printf(_("Device parameters:\n"));
700 printf("Device name: %s\n", dev->dev_name);
701 printf("File=%u block=%u\n", dev->file, dev->block_num);
702 printf("Min block=%u Max block=%u\n", dev->min_block_size, dev->max_block_size);
704 printf(_("Status:\n"));
710 * Test writing larger and larger records.
711 * This is a torture test for records.
713 static void rectestcmd()
719 Pmsg0(0, _("Test writing larger and larger records.\n"
720 "This is a torture test for records.\nI am going to write\n"
721 "larger and larger records. It will stop when the record size\n"
722 "plus the header exceeds the block size (by default about 64K)\n"));
725 get_cmd(_("Do you want to continue? (y/n): "));
727 Pmsg0(000, _("Command aborted.\n"));
731 sm_check(__FILE__, __LINE__, false);
732 block = new_block(dev);
735 for (i=1; i<500000; i++) {
736 rec->data = check_pool_memory_size(rec->data, i);
737 memset(rec->data, i & 0xFF, i);
739 sm_check(__FILE__, __LINE__, false);
740 if (write_record_to_block(block, rec)) {
743 Pmsg2(0, _("Block %d i=%d\n"), blkno, i);
747 sm_check(__FILE__, __LINE__, false);
751 sm_check(__FILE__, __LINE__, false);
755 * This test attempts to re-read a block written by Bacula
756 * normally at the end of the tape. Bacula will then back up
757 * over the two eof marks, backup over the record and reread
758 * it to make sure it is valid. Bacula can skip this validation
759 * if you set "Backward space record = no"
761 static bool re_read_block_test()
763 DEV_BLOCK *block = dcr->block;
768 if (!(dev->capabilities & CAP_BSR)) {
769 Pmsg0(-1, _("Skipping read backwards test because BSR turned off.\n"));
773 Pmsg0(-1, _("\n=== Write, backup, and re-read test ===\n\n"
774 "I'm going to write three records and an EOF\n"
775 "then backup over the EOF and re-read the last record.\n"
776 "Bacula does this after writing the last block on the\n"
777 "tape to verify that the block was written correctly.\n\n"
778 "This is not an *essential* feature ...\n\n"));
782 rec->data = check_pool_memory_size(rec->data, block->buf_len);
783 len = rec->data_len = block->buf_len-100;
784 memset(rec->data, 1, rec->data_len);
785 if (!write_record_to_block(block, rec)) {
786 Pmsg0(0, _("Error writing record to block.\n"));
789 if (!write_block_to_dev(dcr)) {
790 Pmsg0(0, _("Error writing block to device.\n"));
793 Pmsg1(0, _("Wrote first record of %d bytes.\n"), rec->data_len);
795 memset(rec->data, 2, rec->data_len);
796 if (!write_record_to_block(block, rec)) {
797 Pmsg0(0, _("Error writing record to block.\n"));
800 if (!write_block_to_dev(dcr)) {
801 Pmsg0(0, _("Error writing block to device.\n"));
804 Pmsg1(0, _("Wrote second record of %d bytes.\n"), rec->data_len);
806 memset(rec->data, 3, rec->data_len);
807 if (!write_record_to_block(block, rec)) {
808 Pmsg0(0, _("Error writing record to block.\n"));
811 if (!write_block_to_dev(dcr)) {
812 Pmsg0(0, _("Error writing block to device.\n"));
815 Pmsg1(0, _("Wrote third record of %d bytes.\n"), rec->data_len);
818 if (dev->has_cap(CAP_TWOEOF)) {
822 Pmsg1(0, _("Backspace file failed! ERR=%s\n"), dev->bstrerror());
825 if (dev->has_cap(CAP_TWOEOF)) {
827 Pmsg1(0, _("Backspace file failed! ERR=%s\n"), dev->bstrerror());
831 Pmsg0(0, _("Backspaced over EOF OK.\n"));
833 Pmsg1(0, _("Backspace record failed! ERR=%s\n"), dev->bstrerror());
836 Pmsg0(0, _("Backspace record OK.\n"));
837 if (!read_block_from_dev(dcr, NO_BLOCK_NUMBER_CHECK)) {
839 Pmsg1(0, _("Read block failed! ERR=%s\n"), be.bstrerror(dev->dev_errno));
842 memset(rec->data, 0, rec->data_len);
843 if (!read_record_from_block(dcr, block, rec)) {
845 Pmsg1(0, _("Read block failed! ERR=%s\n"), be.bstrerror(dev->dev_errno));
848 for (int i=0; i<len; i++) {
849 if (rec->data[i] != 3) {
850 Pmsg0(0, _("Bad data in record. Test failed!\n"));
854 Pmsg0(0, _("\nBlock re-read correct. Test succeeded!\n"));
855 Pmsg0(-1, _("=== End Write, backup, and re-read test ===\n\n"));
862 Pmsg0(0, _("This is not terribly serious since Bacula only uses\n"
863 "this function to verify the last block written to the\n"
864 "tape. Bacula will skip the last block verification\n"
866 "Backward Space Record = No\n\n"
867 "to your Storage daemon's Device resource definition.\n"));
872 static bool speed_test_raw(fill_mode_t mode, uint64_t nb_gb, uint32_t nb)
874 DEV_BLOCK *block = dcr->block;
876 uint32_t block_num = 0;
881 nb_gb *= 1024*1024*1024; /* convert size from nb to GB */
884 fill_buffer(mode, block->buf, block->buf_len);
886 p = (uint32_t *)block->buf;
887 Pmsg3(0, _("Begin writing %i files of %sB with raw blocks of %u bytes.\n"),
888 nb, edit_uint64_with_suffix(nb_gb, ed1), block->buf_len);
890 for (uint32_t j=0; j<nb; j++) {
892 for ( ;jcr->JobBytes < nb_gb; ) {
893 stat = dev->d_write(dev->fd(), block->buf, block->buf_len);
894 if (stat == (int)block->buf_len) {
895 if ((block_num++ % 1000) == 0) {
899 if (mode == FILL_RANDOM) {
902 i<(block->buf_len-sizeof(uint32_t))/sizeof(uint32_t)-1;
908 jcr->JobBytes += stat;
914 printf(_("Write failed at block %u. stat=%d ERR=%s\n"), block_num,
915 stat, be.bstrerror(my_errno));
921 print_speed(jcr->JobBytes);
929 #define ok(a) if (!(a)) return
932 * For file (/dev/zero, /dev/urandom, normal?)
933 * use raw mode to write a suite of 3 files of 1, 2, 4, 8 GB
934 * use qfill mode to write the same
937 static void speed_test()
940 Pmsg0(0, _("Test with zero data, should give the maximum throughput.\n"));
941 ok(speed_test_raw(FILL_ZERO, 1, 3));
942 ok(speed_test_raw(FILL_ZERO, 2, 3));
943 ok(speed_test_raw(FILL_ZERO, 4, 3));
945 Pmsg0(0, _("Test with random data, should give the minimum throughput.\n"));
946 ok(speed_test_raw(FILL_RANDOM, 1, 3));
947 ok(speed_test_raw(FILL_RANDOM, 2, 3));
948 ok(speed_test_raw(FILL_RANDOM, 4, 3));
951 const int num_recs = 10000;
953 static bool write_two_files()
959 bool rc = false; /* bad return code */
961 Pmsg2(-1, _("\n=== Write, rewind, and re-read test ===\n\n"
962 "I'm going to write %d records and an EOF\n"
963 "then write %d records and an EOF, then rewind,\n"
964 "and re-read the data to verify that it is correct.\n\n"
965 "This is an *essential* feature ...\n\n"), num_recs, num_recs);
970 rec->data = check_pool_memory_size(rec->data, block->buf_len);
971 rec->data_len = block->buf_len-100;
972 len = rec->data_len/sizeof(i);
974 if (!dev->rewind(dcr)) {
975 Pmsg1(0, _("Bad status from rewind. ERR=%s\n"), dev->bstrerror());
979 for (i=1; i<=num_recs; i++) {
980 p = (int *)rec->data;
981 for (j=0; j<len; j++) {
984 if (!write_record_to_block(block, rec)) {
985 Pmsg0(0, _("Error writing record to block.\n"));
988 if (!write_block_to_dev(dcr)) {
989 Pmsg0(0, _("Error writing block to device.\n"));
993 Pmsg2(0, _("Wrote %d blocks of %d bytes.\n"), num_recs, rec->data_len);
995 for (i=num_recs+1; i<=2*num_recs; i++) {
996 p = (int *)rec->data;
997 for (j=0; j<len; j++) {
1000 if (!write_record_to_block(block, rec)) {
1001 Pmsg0(0, _("Error writing record to block.\n"));
1004 if (!write_block_to_dev(dcr)) {
1005 Pmsg0(0, _("Error writing block to device.\n"));
1009 Pmsg2(0, _("Wrote %d blocks of %d bytes.\n"), num_recs, rec->data_len);
1011 if (dev->has_cap(CAP_TWOEOF)) {
1026 * This test writes Bacula blocks to the tape in
1027 * several files. It then rewinds the tape and attepts
1028 * to read these blocks back checking the data.
1030 static bool write_read_test()
1040 if (!write_two_files()) {
1047 if (!dev->rewind(dcr)) {
1048 Pmsg1(0, _("Bad status from rewind. ERR=%s\n"), dev->bstrerror());
1051 Pmsg0(0, _("Rewind OK.\n"));
1054 rec->data = check_pool_memory_size(rec->data, block->buf_len);
1055 rec->data_len = block->buf_len-100;
1056 len = rec->data_len/sizeof(i);
1058 /* Now read it back */
1059 for (i=1; i<=2*num_recs; i++) {
1061 if (!read_block_from_dev(dcr, NO_BLOCK_NUMBER_CHECK)) {
1063 if (dev_state(dev, ST_EOF)) {
1064 Pmsg0(-1, _("Got EOF on tape.\n"));
1065 if (i == num_recs+1) {
1069 Pmsg2(0, _("Read block %d failed! ERR=%s\n"), i, be.bstrerror(dev->dev_errno));
1072 memset(rec->data, 0, rec->data_len);
1073 if (!read_record_from_block(dcr, block, rec)) {
1075 Pmsg2(0, _("Read record failed. Block %d! ERR=%s\n"), i, be.bstrerror(dev->dev_errno));
1078 p = (int *)rec->data;
1079 for (j=0; j<len; j++) {
1081 Pmsg3(0, _("Bad data in record. Expected %d, got %d at byte %d. Test failed!\n"),
1087 if (i == num_recs || i == 2*num_recs) {
1088 Pmsg1(-1, _("%d blocks re-read correctly.\n"), num_recs);
1091 Pmsg0(-1, _("=== Test Succeeded. End Write, rewind, and re-read test ===\n\n"));
1103 * This test writes Bacula blocks to the tape in
1104 * several files. It then rewinds the tape and attepts
1105 * to read these blocks back checking the data.
1107 static bool position_test()
1109 DEV_BLOCK *block = dcr->block;
1115 int file = 0, blk = 0;
1117 bool got_eof = false;
1119 Pmsg0(0, _("Block position test\n"));
1123 rec->data = check_pool_memory_size(rec->data, block->buf_len);
1124 rec->data_len = block->buf_len-100;
1125 len = rec->data_len/sizeof(j);
1127 if (!dev->rewind(dcr)) {
1128 Pmsg1(0, _("Bad status from rewind. ERR=%s\n"), dev->bstrerror());
1131 Pmsg0(0, _("Rewind OK.\n"));
1135 /* Set up next item to read based on where we are */
1136 /* At each step, recno is what we print for the "block number"
1137 * and file, blk are the real positions to go to.
1161 recno = num_recs+601;
1174 Pmsg2(-1, _("Reposition to file:block %d:%d\n"), file, blk);
1175 if (!dev->reposition(dcr, file, blk)) {
1176 Pmsg0(0, _("Reposition error.\n"));
1180 if (!read_block_from_dev(dcr, NO_BLOCK_NUMBER_CHECK)) {
1182 if (dev_state(dev, ST_EOF)) {
1183 Pmsg0(-1, _("Got EOF on tape.\n"));
1189 Pmsg4(0, _("Read block %d failed! file=%d blk=%d. ERR=%s\n\n"),
1190 recno, file, blk, be.bstrerror(dev->dev_errno));
1191 Pmsg0(0, _("This may be because the tape drive block size is not\n"
1192 " set to variable blocking as normally used by Bacula.\n"
1193 " Please see the Tape Testing chapter in the manual and \n"
1194 " look for using mt with defblksize and setoptions\n"
1195 "If your tape drive block size is correct, then perhaps\n"
1196 " your SCSI driver is *really* stupid and does not\n"
1197 " correctly report the file:block after a FSF. In this\n"
1198 " case try setting:\n"
1199 " Fast Forward Space File = no\n"
1200 " in your Device resource.\n"));
1204 memset(rec->data, 0, rec->data_len);
1205 if (!read_record_from_block(dcr, block, rec)) {
1207 Pmsg1(0, _("Read record failed! ERR=%s\n"), be.bstrerror(dev->dev_errno));
1210 p = (int *)rec->data;
1211 for (j=0; j<len; j++) {
1212 if (p[j] != recno) {
1213 Pmsg3(0, _("Bad data in record. Expected %d, got %d at byte %d. Test failed!\n"),
1218 Pmsg1(-1, _("Block %d re-read correctly.\n"), recno);
1220 Pmsg0(-1, _("=== Test Succeeded. End Write, rewind, and re-read test ===\n\n"));
1232 * This test writes some records, then writes an end of file,
1233 * rewinds the tape, moves to the end of the data and attepts
1234 * to append to the tape. This function is essential for
1235 * Bacula to be able to write multiple jobs to the tape.
1237 static int append_test()
1239 Pmsg0(-1, _("\n\n=== Append files test ===\n\n"
1240 "This test is essential to Bacula.\n\n"
1241 "I'm going to write one record in file 0,\n"
1242 " two records in file 1,\n"
1243 " and three records in file 2\n\n"));
1247 weofcmd(); /* end file 0 */
1250 weofcmd(); /* end file 1 */
1254 weofcmd(); /* end file 2 */
1255 if (dev->has_cap(CAP_TWOEOF)) {
1258 dev->close(); /* release device */
1259 if (!open_the_device()) {
1263 Pmsg0(0, _("Now moving to end of medium.\n"));
1265 Pmsg2(-1, _("We should be in file 3. I am at file %d. %s\n"),
1266 dev->file, dev->file == 3 ? _("This is correct!") : _("This is NOT correct!!!!"));
1268 if (dev->file != 3) {
1272 Pmsg0(-1, _("\nNow the important part, I am going to attempt to append to the tape.\n\n"));
1275 if (dev->has_cap(CAP_TWOEOF)) {
1279 Pmsg0(-1, _("Done appending, there should be no I/O errors\n\n"));
1280 Pmsg0(-1, _("Doing Bacula scan of blocks:\n"));
1282 Pmsg0(-1, _("End scanning the tape.\n"));
1283 Pmsg2(-1, _("We should be in file 4. I am at file %d. %s\n"),
1284 dev->file, dev->file == 4 ? _("This is correct!") : _("This is NOT correct!!!!"));
1286 if (dev->file != 4) {
1294 * This test exercises the autochanger
1296 static int autochanger_test()
1298 POOLMEM *results, *changer;
1299 int slot, status, loaded;
1300 int timeout = dcr->device->max_changer_wait;
1303 Dmsg1(100, "Max changer wait = %d sec\n", timeout);
1304 if (!dev->has_cap(CAP_AUTOCHANGER)) {
1307 if (!(dcr->device && dcr->device->changer_name && dcr->device->changer_command)) {
1308 Pmsg0(-1, _("\nAutochanger enabled, but no name or no command device specified.\n"));
1312 Pmsg0(-1, _("\nAh, I see you have an autochanger configured.\n"
1313 "To test the autochanger you must have a blank tape\n"
1314 " that I can write on in Slot 1.\n"));
1315 if (!get_cmd(_("\nDo you wish to continue with the Autochanger test? (y/n): "))) {
1318 if (cmd[0] != 'y' && cmd[0] != 'Y') {
1322 Pmsg0(-1, _("\n\n=== Autochanger test ===\n\n"));
1324 results = get_pool_memory(PM_MESSAGE);
1325 changer = get_pool_memory(PM_FNAME);
1329 dcr->VolCatInfo.Slot = slot;
1330 /* Find out what is loaded, zero means device is unloaded */
1331 Pmsg0(-1, _("3301 Issuing autochanger \"loaded\" command.\n"));
1332 changer = edit_device_codes(dcr, changer,
1333 dcr->device->changer_command, "loaded");
1334 status = run_program(changer, timeout, results);
1335 Dmsg3(100, "run_prog: %s stat=%d result=\"%s\"\n", changer, status, results);
1337 loaded = atoi(results);
1340 Pmsg1(-1, _("3991 Bad autochanger command: %s\n"), changer);
1341 Pmsg2(-1, _("3991 result=\"%s\": ERR=%s\n"), results, be.bstrerror(status));
1345 Pmsg1(-1, _("Slot %d loaded. I am going to unload it.\n"), loaded);
1347 Pmsg0(-1, _("Nothing loaded in the drive. OK.\n"));
1349 Dmsg1(100, "Results from loaded query=%s\n", results);
1351 dcr->VolCatInfo.Slot = loaded;
1352 /* We are going to load a new tape, so close the device */
1354 Pmsg2(-1, _("3302 Issuing autochanger \"unload %d %d\" command.\n"),
1355 loaded, dev->drive_index);
1356 changer = edit_device_codes(dcr, changer,
1357 dcr->device->changer_command, "unload");
1358 status = run_program(changer, timeout, results);
1359 Pmsg2(-1, _("unload status=%s %d\n"), status==0?_("OK"):_("Bad"), status);
1362 Pmsg1(-1, _("3992 Bad autochanger command: %s\n"), changer);
1363 Pmsg2(-1, _("3992 result=\"%s\": ERR=%s\n"), results, be.bstrerror(status));
1372 dcr->VolCatInfo.Slot = slot;
1373 Pmsg2(-1, _("3303 Issuing autochanger \"load %d %d\" command.\n"),
1374 slot, dev->drive_index);
1375 changer = edit_device_codes(dcr, changer,
1376 dcr->device->changer_command, "load");
1377 Dmsg1(100, "Changer=%s\n", changer);
1379 status = run_program(changer, timeout, results);
1381 Pmsg2(-1, _("3303 Autochanger \"load %d %d\" status is OK.\n"),
1382 slot, dev->drive_index);
1385 Pmsg1(-1, _("3993 Bad autochanger command: %s\n"), changer);
1386 Pmsg2(-1, _("3993 result=\"%s\": ERR=%s\n"), results, be.bstrerror(status));
1390 if (!open_the_device()) {
1394 * Start with sleep_time 0 then increment by 30 seconds if we get
1397 bmicrosleep(sleep_time, 0);
1398 if (!dev->rewind(dcr) || !dev->weof(1)) {
1399 Pmsg1(0, _("Bad status from rewind. ERR=%s\n"), dev->bstrerror());
1401 Pmsg0(-1, _("\nThe test failed, probably because you need to put\n"
1402 "a longer sleep time in the mtx-script in the load) case.\n"
1403 "Adding a 30 second sleep and trying again ...\n"));
1407 Pmsg1(0, _("Rewound %s\n"), dev->print_name());
1410 if (!dev->weof(1)) {
1411 Pmsg1(0, _("Bad status from weof. ERR=%s\n"), dev->bstrerror());
1414 Pmsg1(0, _("Wrote EOF to %s\n"), dev->print_name());
1418 Pmsg1(-1, _("\nThe test worked this time. Please add:\n\n"
1420 "to your mtx-changer script in the load) case.\n\n"),
1423 Pmsg0(-1, _("\nThe test autochanger worked!!\n\n"));
1426 free_pool_memory(changer);
1427 free_pool_memory(results);
1432 free_pool_memory(changer);
1433 free_pool_memory(results);
1434 Pmsg0(-1, _("You must correct this error or the Autochanger will not work.\n"));
1438 static void autochangercmd()
1445 * This test assumes that the append test has been done,
1446 * then it tests the fsf function.
1448 static bool fsf_test()
1450 bool set_off = false;
1452 Pmsg0(-1, _("\n\n=== Forward space files test ===\n\n"
1453 "This test is essential to Bacula.\n\n"
1454 "I'm going to write five files then test forward spacing\n\n"));
1458 weofcmd(); /* end file 0 */
1461 weofcmd(); /* end file 1 */
1465 weofcmd(); /* end file 2 */
1468 weofcmd(); /* end file 3 */
1470 weofcmd(); /* end file 4 */
1471 if (dev->has_cap(CAP_TWOEOF)) {
1477 Pmsg0(0, _("Now forward spacing 1 file.\n"));
1479 Pmsg1(0, _("Bad status from fsr. ERR=%s\n"), dev->bstrerror());
1482 Pmsg2(-1, _("We should be in file 1. I am at file %d. %s\n"),
1483 dev->file, dev->file == 1 ? _("This is correct!") : _("This is NOT correct!!!!"));
1485 if (dev->file != 1) {
1489 Pmsg0(0, _("Now forward spacing 2 files.\n"));
1491 Pmsg1(0, _("Bad status from fsr. ERR=%s\n"), dev->bstrerror());
1494 Pmsg2(-1, _("We should be in file 3. I am at file %d. %s\n"),
1495 dev->file, dev->file == 3 ? _("This is correct!") : _("This is NOT correct!!!!"));
1497 if (dev->file != 3) {
1502 Pmsg0(0, _("Now forward spacing 4 files.\n"));
1504 Pmsg1(0, _("Bad status from fsr. ERR=%s\n"), dev->bstrerror());
1507 Pmsg2(-1, _("We should be in file 4. I am at file %d. %s\n"),
1508 dev->file, dev->file == 4 ? _("This is correct!") : _("This is NOT correct!!!!"));
1510 if (dev->file != 4) {
1514 Pmsg0(-1, _("The test worked this time. Please add:\n\n"
1515 " Fast Forward Space File = no\n\n"
1516 "to your Device resource for this drive.\n"));
1520 Pmsg0(0, _("Now forward spacing 1 more file.\n"));
1522 Pmsg1(0, _("Bad status from fsr. ERR=%s\n"), dev->bstrerror());
1524 Pmsg2(-1, _("We should be in file 5. I am at file %d. %s\n"),
1525 dev->file, dev->file == 5 ? _("This is correct!") : _("This is NOT correct!!!!"));
1526 if (dev->file != 5) {
1529 Pmsg0(-1, _("\n=== End Forward space files test ===\n\n"));
1533 Pmsg0(-1, _("\nThe forward space file test failed.\n"));
1534 if (dev->has_cap(CAP_FASTFSF)) {
1535 Pmsg0(-1, _("You have Fast Forward Space File enabled.\n"
1536 "I am turning it off then retrying the test.\n"));
1537 dev->clear_cap(CAP_FASTFSF);
1541 Pmsg0(-1, _("You must correct this error or Bacula will not work.\n"
1542 "Some systems, e.g. OpenBSD, require you to set\n"
1543 " Use MTIOCGET= no\n"
1544 "in your device resource. Use with caution.\n"));
1553 * This is a general test of Bacula's functions
1554 * needed to read and write the tape.
1556 static void testcmd()
1560 if (!write_read_test()) {
1564 if (!position_test()) {
1569 stat = append_test();
1570 if (stat == 1) { /* OK get out */
1573 if (stat == -1) { /* first test failed */
1574 if (dev->has_cap(CAP_EOM) || dev->has_cap(CAP_FASTFSF)) {
1575 Pmsg0(-1, _("\nAppend test failed. Attempting again.\n"
1576 "Setting \"Hardware End of Medium = no\n"
1577 " and \"Fast Forward Space File = no\n"
1578 "and retrying append test.\n\n"));
1579 dev->clear_cap(CAP_EOM); /* turn off eom */
1580 dev->clear_cap(CAP_FASTFSF); /* turn off fast fsf */
1581 stat = append_test();
1583 Pmsg0(-1, _("\n\nIt looks like the test worked this time, please add:\n\n"
1584 " Hardware End of Medium = No\n\n"
1585 " Fast Forward Space File = No\n"
1586 "to your Device resource in the Storage conf file.\n"));
1590 Pmsg0(-1, _("\n\nThat appears *NOT* to have corrected the problem.\n"));
1593 /* Wrong count after append */
1595 Pmsg0(-1, _("\n\nIt looks like the append failed. Attempting again.\n"
1596 "Setting \"BSF at EOM = yes\" and retrying append test.\n"));
1597 dev->capabilities |= CAP_BSFATEOM; /* backspace on eom */
1598 stat = append_test();
1600 Pmsg0(-1, _("\n\nIt looks like the test worked this time, please add:\n\n"
1601 " Hardware End of Medium = No\n"
1602 " Fast Forward Space File = No\n"
1603 " BSF at EOM = yes\n\n"
1604 "to your Device resource in the Storage conf file.\n"));
1611 Pmsg0(-1, _("\nAppend test failed.\n\n"
1612 "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
1613 "Unable to correct the problem. You MUST fix this\n"
1614 "problem before Bacula can use your tape drive correctly\n"
1615 "\nPerhaps running Bacula in fixed block mode will work.\n"
1616 "Do so by setting:\n\n"
1617 "Minimum Block Size = nnn\n"
1618 "Maximum Block Size = nnn\n\n"
1619 "in your Storage daemon's Device definition.\n"
1620 "nnn must match your tape driver's block size, which\n"
1621 "can be determined by reading your tape manufacturers\n"
1622 "information, and the information on your kernel dirver.\n"
1623 "Fixed block sizes, however, are not normally an ideal solution.\n"
1625 "Some systems, e.g. OpenBSD, require you to set\n"
1626 " Use MTIOCGET= no\n"
1627 "in your device resource. Use with caution.\n"));
1633 Pmsg0(-1, _("\nThe above Bacula scan should have output identical to what follows.\n"
1634 "Please double check it ...\n"
1635 "=== Sample correct output ===\n"
1636 "1 block of 64448 bytes in file 1\n"
1637 "End of File mark.\n"
1638 "2 blocks of 64448 bytes in file 2\n"
1639 "End of File mark.\n"
1640 "3 blocks of 64448 bytes in file 3\n"
1641 "End of File mark.\n"
1642 "1 block of 64448 bytes in file 4\n"
1643 "End of File mark.\n"
1644 "Total files=4, blocks=7, bytes = 451,136\n"
1645 "=== End sample correct output ===\n\n"
1646 "If the above scan output is not identical to the\n"
1647 "sample output, you MUST correct the problem\n"
1648 "or Bacula will not be able to write multiple Jobs to \n"
1652 if (!re_read_block_test()) {
1657 if (!fsf_test()) { /* do fast forward space file test */
1661 autochanger_test(); /* do autochanger test */
1665 /* Forward space a file */
1666 static void fsfcmd()
1670 num = atoi(argk[1]);
1675 if (!dev->fsf(num)) {
1676 Pmsg1(0, _("Bad status from fsf. ERR=%s\n"), dev->bstrerror());
1680 Pmsg0(0, _("Forward spaced 1 file.\n"));
1683 Pmsg1(0, _("Forward spaced %d files.\n"), num);
1687 /* Forward space a record */
1688 static void fsrcmd()
1692 num = atoi(argk[1]);
1697 if (!dev->fsr(num)) {
1698 Pmsg1(0, _("Bad status from fsr. ERR=%s\n"), dev->bstrerror());
1702 Pmsg0(0, _("Forward spaced 1 record.\n"));
1705 Pmsg1(0, _("Forward spaced %d records.\n"), num);
1710 * Read a Bacula block from the tape
1714 dev->open(dcr, OPEN_READ_ONLY);
1715 read_block_from_dev(dcr, NO_BLOCK_NUMBER_CHECK);
1719 * Write a Bacula block to the tape
1723 DEV_BLOCK *block = dcr->block;
1724 DEV_RECORD *rec = dcr->rec;
1727 if (!dev->is_open()) {
1730 sm_check(__FILE__, __LINE__, false);
1733 dump_block(block, "test");
1736 i = block->buf_len - 100;
1738 rec->data = check_pool_memory_size(rec->data, i);
1739 memset(rec->data, i & 0xFF, i);
1741 sm_check(__FILE__, __LINE__, false);
1742 if (!write_record_to_block(block, rec)) {
1743 Pmsg0(0, _("Error writing record to block.\n"));
1746 if (!write_block_to_dev(dcr)) {
1747 Pmsg0(0, _("Error writing block to device.\n"));
1750 Pmsg1(0, _("Wrote one record of %d bytes.\n"), i);
1752 Pmsg0(0, _("Wrote block to device.\n"));
1755 sm_check(__FILE__, __LINE__, false);
1756 sm_check(__FILE__, __LINE__, false);
1760 * Read a record from the tape
1767 if (!get_cmd(_("Enter length to read: "))) {
1771 if (len < 0 || len > 1000000) {
1772 Pmsg0(0, _("Bad length entered, using default of 1024 bytes.\n"));
1775 buf = (char *)malloc(len);
1776 stat = read(dev->fd(), buf, len);
1777 if (stat > 0 && stat <= len) {
1781 Pmsg3(0, _("Read of %d bytes gives stat=%d. ERR=%s\n"),
1782 len, stat, be.bstrerror());
1788 * Scan tape by reading block by block. Report what is
1789 * on the tape. Note, this command does raw reads, and as such
1790 * will not work with fixed block size devices.
1792 static void scancmd()
1795 int blocks, tot_blocks, tot_files;
1801 blocks = block_size = tot_blocks = 0;
1803 if (dev->state & ST_EOT) {
1804 Pmsg0(0, _("End of tape\n"));
1807 dev->update_pos(dcr);
1808 tot_files = dev->file;
1809 Pmsg1(0, _("Starting scan at file %u\n"), dev->file);
1811 if ((stat = read(dev->fd(), buf, sizeof(buf))) < 0) {
1814 Mmsg2(dev->errmsg, _("read error on %s. ERR=%s.\n"),
1815 dev->dev_name, be.bstrerror());
1816 Pmsg2(0, _("Bad status from read %d. ERR=%s\n"), stat, dev->bstrerror());
1819 printf(_("1 block of %d bytes in file %d\n"), block_size, dev->file);
1822 printf(_("%d blocks of %d bytes in file %d\n"), blocks, block_size, dev->file);
1827 Dmsg1(200, "read status = %d\n", stat);
1829 if (stat != block_size) {
1830 dev->update_pos(dcr);
1833 printf(_("1 block of %d bytes in file %d\n"), block_size, dev->file);
1836 printf(_("%d blocks of %d bytes in file %d\n"), blocks, block_size, dev->file);
1842 if (stat == 0) { /* EOF */
1843 dev->update_pos(dcr);
1844 printf(_("End of File mark.\n"));
1845 /* Two reads of zero means end of tape */
1846 if (dev->state & ST_EOF)
1847 dev->state |= ST_EOT;
1849 dev->state |= ST_EOF;
1852 if (dev->state & ST_EOT) {
1853 printf(_("End of tape\n"));
1856 } else { /* Got data */
1857 dev->state &= ~ST_EOF;
1863 dev->update_pos(dcr);
1864 tot_files = dev->file - tot_files;
1865 printf(_("Total files=%d, blocks=%d, bytes = %s\n"), tot_files, tot_blocks,
1866 edit_uint64_with_commas(bytes, ec1));
1871 * Scan tape by reading Bacula block by block. Report what is
1872 * on the tape. This function reads Bacula blocks, so if your
1873 * Device resource is correctly defined, it should work with
1874 * either variable or fixed block sizes.
1876 static void scan_blocks()
1878 int blocks, tot_blocks, tot_files;
1879 uint32_t block_size;
1881 DEV_BLOCK *block = dcr->block;
1883 char buf1[100], buf2[100];
1885 blocks = block_size = tot_blocks = 0;
1889 dev->update_pos(dcr);
1890 tot_files = dev->file;
1892 if (!read_block_from_device(dcr, NO_BLOCK_NUMBER_CHECK)) {
1893 Dmsg1(100, "!read_block(): ERR=%s\n", dev->bstrerror());
1894 if (dev->state & ST_EOT) {
1897 printf(_("1 block of %d bytes in file %d\n"), block_size, dev->file);
1900 printf(_("%d blocks of %d bytes in file %d\n"), blocks, block_size, dev->file);
1906 if (dev->state & ST_EOF) {
1909 printf(_("1 block of %d bytes in file %d\n"), block_size, dev->file);
1912 printf(_("%d blocks of %d bytes in file %d\n"), blocks, block_size, dev->file);
1916 printf(_("End of File mark.\n"));
1919 if (dev->state & ST_SHORT) {
1922 printf(_("1 block of %d bytes in file %d\n"), block_size, dev->file);
1925 printf(_("%d blocks of %d bytes in file %d\n"), blocks, block_size, dev->file);
1929 printf(_("Short block read.\n"));
1932 printf(_("Error reading block. ERR=%s\n"), dev->bstrerror());
1935 if (block->block_len != block_size) {
1938 printf(_("1 block of %d bytes in file %d\n"), block_size, dev->file);
1941 printf(_("%d blocks of %d bytes in file %d\n"), blocks, block_size, dev->file);
1945 block_size = block->block_len;
1949 bytes += block->block_len;
1950 Dmsg7(100, "Blk_blk=%u file,blk=%u,%u blen=%u bVer=%d SessId=%u SessTim=%u\n",
1951 block->BlockNumber, dev->file, dev->block_num, block->block_len, block->BlockVer,
1952 block->VolSessionId, block->VolSessionTime);
1954 DEV_RECORD *rec = new_record();
1955 read_record_from_block(dcr, block, rec);
1956 Pmsg9(-1, _("Block=%u file,blk=%u,%u blen=%u First rec FI=%s SessId=%u SessTim=%u Strm=%s rlen=%d\n"),
1957 block->BlockNumber, dev->file, dev->block_num, block->block_len,
1958 FI_to_ascii(buf1, rec->FileIndex), rec->VolSessionId, rec->VolSessionTime,
1959 stream_to_ascii(buf2, rec->Stream, rec->FileIndex), rec->data_len);
1962 } else if (verbose > 1) {
1963 dump_block(block, "");
1968 tot_files = dev->file - tot_files;
1969 printf(_("Total files=%d, blocks=%d, bytes = %s\n"), tot_files, tot_blocks,
1970 edit_uint64_with_commas(bytes, ec1));
1974 static void statcmd()
1976 int debug = debug_level;
1978 Pmsg2(0, _("Device status: %u. ERR=%s\n"), status_dev(dev), dev->bstrerror());
1980 dump_volume_label(dev);
1982 debug_level = debug;
1987 * First we label the tape, then we fill
1988 * it with data get a new tape and write a few blocks.
1990 static void fillcmd()
1993 DEV_BLOCK *block = dcr->block;
1995 char buf1[100], buf2[100];
1998 uint32_t min_block_size;
2011 "This command simulates Bacula writing to a tape.\n"
2012 "It requires either one or two blank tapes, which it\n"
2013 "will label and write.\n\n"
2014 "If you have an autochanger configured, it will use\n"
2015 "the tapes that are in slots 1 and 2, otherwise, you will\n"
2016 "be prompted to insert the tapes when necessary.\n\n"
2017 "It will print a status approximately\n"
2018 "every 322 MB, and write an EOF every %s. If you have\n"
2019 "selected the simple test option, after writing the first tape\n"
2020 "it will rewind it and re-read the last block written.\n\n"
2021 "If you have selected the multiple tape test, when the first tape\n"
2022 "fills, it will ask for a second, and after writing a few more \n"
2023 "blocks, it will stop. Then it will begin re-reading the\n"
2025 "This may take a long time -- hours! ...\n\n"),
2026 edit_uint64_with_suffix(dev->max_file_size, buf1));
2028 get_cmd(_("Do you want to run the simplified test (s) with one tape\n"
2029 "or the complete multiple tape (m) test: (s/m) "));
2030 if (cmd[0] == 's') {
2031 Pmsg0(-1, _("Simple test (single tape) selected.\n"));
2033 } else if (cmd[0] == 'm') {
2034 Pmsg0(-1, _("Multiple tape test selected.\n"));
2037 Pmsg0(000, _("Command aborted.\n"));
2042 Dmsg1(20, "Begin append device=%s\n", dev->print_name());
2043 Dmsg1(20, "MaxVolSize=%s\n", edit_uint64(dev->max_volume_size, ec1));
2045 /* Use fixed block size to simplify read back */
2046 min_block_size = dev->min_block_size;
2047 dev->min_block_size = dev->max_block_size;
2048 write_eof = dev->max_file_size / REC_SIZE; /*compute when we add EOF*/
2049 set_volume_name("TestVolume1", 1);
2050 dir_ask_sysop_to_create_appendable_volume(dcr);
2051 dev->set_append(); /* force volume to be relabeled */
2054 * Acquire output device for writing. Note, after acquiring a
2055 * device, we MUST release it, which is done at the end of this
2058 Dmsg0(100, "just before acquire_device\n");
2059 if (!acquire_device_for_append(dcr)) {
2060 set_jcr_job_status(jcr, JS_ErrorTerminated);
2064 block = jcr->dcr->block;
2066 Dmsg0(100, "Just after acquire_device_for_append\n");
2068 * Write Begin Session Record
2070 if (!write_session_label(dcr, SOS_LABEL)) {
2071 set_jcr_job_status(jcr, JS_ErrorTerminated);
2072 Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"),
2076 Pmsg0(-1, _("Wrote Start of Session label.\n"));
2078 memset(&rec, 0, sizeof(rec));
2079 rec.data = get_memory(100000); /* max record size */
2080 rec.data_len = REC_SIZE;
2083 * Put some random data in the record
2085 fill_buffer(FILL_RANDOM, rec.data, rec.data_len);
2088 * Generate data as if from File daemon, write to device
2090 jcr->dcr->VolFirstIndex = 0;
2091 time(&jcr->run_time); /* start counting time for rates */
2092 (void)localtime_r(&jcr->run_time, &tm);
2093 strftime(buf1, sizeof(buf1), "%H:%M:%S", &tm);
2095 Pmsg1(-1, _("%s Begin writing Bacula records to tape ...\n"), buf1);
2097 Pmsg1(-1, _("%s Begin writing Bacula records to first tape ...\n"), buf1);
2099 for (file_index = 0; ok && !job_canceled(jcr); ) {
2100 rec.VolSessionId = jcr->VolSessionId;
2101 rec.VolSessionTime = jcr->VolSessionTime;
2102 rec.FileIndex = ++file_index;
2103 rec.Stream = STREAM_FILE_DATA;
2105 /* Mix up the data just a bit */
2106 uint32_t *lp = (uint32_t *)rec.data;
2108 for (i=1; i < (rec.data_len-sizeof(uint32_t))/sizeof(uint32_t)-1; i+=100) {
2112 Dmsg4(250, "before write_rec FI=%d SessId=%d Strm=%s len=%d\n",
2113 rec.FileIndex, rec.VolSessionId,
2114 stream_to_ascii(buf1, rec.Stream, rec.FileIndex),
2117 while (!write_record_to_block(block, &rec)) {
2119 * When we get here we have just filled a block
2121 Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec.data_len,
2124 /* Write block to tape */
2125 if (!flush_block(block, 1)) {
2130 /* Every 5000 blocks (approx 322MB) report where we are.
2132 if ((block->BlockNumber % 5000) == 0) {
2134 now -= jcr->run_time;
2136 now = 1; /* prevent divide error */
2138 kbs = (double)dev->VolCatInfo.VolCatBytes / (1000.0 * (double)now);
2139 Pmsg5(-1, _("Wrote block=%u, file,blk=%u,%u VolBytes=%s rate=%.1f KB/s\n"),
2140 block->BlockNumber, dev->file, dev->block_num,
2141 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), (float)kbs);
2143 /* Every X blocks (dev->max_file_size) write an EOF.
2145 if ((block->BlockNumber % write_eof) == 0) {
2147 (void)localtime_r(&now, &tm);
2148 strftime(buf1, sizeof(buf1), "%H:%M:%S", &tm);
2149 Pmsg1(-1, _("%s\n"), buf1);
2151 Pmsg1(-1, _("%s Flush block, write EOF\n"), buf1);
2152 flush_block(block, 0);
2157 /* Get out after writing 10 blocks to the second tape */
2158 if (++BlockNumber > 10 && stop != 0) { /* get out */
2163 Pmsg0(000, _("Not OK\n"));
2167 jcr->JobBytes += rec.data_len; /* increment bytes this job */
2168 Dmsg4(190, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
2169 FI_to_ascii(buf1, rec.FileIndex), rec.VolSessionId,
2170 stream_to_ascii(buf2, rec.Stream, rec.FileIndex), rec.data_len);
2172 /* Get out after writing 10 blocks to the second tape */
2173 if (BlockNumber > 10 && stop != 0) { /* get out */
2175 Pmsg1(-1, "Done writing %s records ...\n",
2176 edit_uint64_with_commas(write_count, ed1));
2181 Dmsg0(100, "Write_end_session_label()\n");
2182 /* Create Job status for end of session label */
2183 if (!job_canceled(jcr) && ok) {
2184 set_jcr_job_status(jcr, JS_Terminated);
2186 set_jcr_job_status(jcr, JS_ErrorTerminated);
2189 if (!write_session_label(dcr, EOS_LABEL)) {
2190 Pmsg1(000, _("Error writing end session label. ERR=%s\n"), dev->bstrerror());
2194 /* Write out final block of this session */
2195 if (!write_block_to_device(dcr)) {
2196 Pmsg0(-1, _("Set ok=false after write_block_to_device.\n"));
2200 Pmsg0(-1, _("Wrote End of Session label.\n"));
2202 /* Save last block info for second tape */
2203 last_block_num2 = last_block_num;
2204 last_file2 = last_file;
2206 free_block(last_block2);
2208 last_block2 = dup_block(last_block);
2211 sprintf(buf, "%s/btape.state", working_directory);
2212 fd = open(buf, O_CREAT|O_TRUNC|O_WRONLY, 0640);
2214 write(fd, &btape_state_level, sizeof(btape_state_level));
2215 write(fd, &simple, sizeof(simple));
2216 write(fd, &last_block_num1, sizeof(last_block_num1));
2217 write(fd, &last_block_num2, sizeof(last_block_num2));
2218 write(fd, &last_file1, sizeof(last_file1));
2219 write(fd, &last_file2, sizeof(last_file2));
2220 write(fd, last_block1->buf, last_block1->buf_len);
2221 write(fd, last_block2->buf, last_block2->buf_len);
2222 write(fd, first_block->buf, first_block->buf_len);
2224 Pmsg2(-1, _("Wrote state file last_block_num1=%d last_block_num2=%d\n"),
2225 last_block_num1, last_block_num2);
2228 Pmsg2(-1, _("Could not create state file: %s ERR=%s\n"), buf,
2234 (void)localtime_r(&now, &tm);
2235 strftime(buf1, sizeof(buf1), "%H:%M:%S", &tm);
2237 Pmsg3(-1, _("\n\n%s Done filling tape at %d:%d. Now beginning re-read of tape ...\n"),
2238 buf1, jcr->dcr->dev->file, jcr->dcr->dev->block_num);
2241 Pmsg3(-1, _("\n\n%s Done filling tapes at %d:%d. Now beginning re-read of first tape ...\n"),
2242 buf1, jcr->dcr->dev->file, jcr->dcr->dev->block_num);
2245 jcr->dcr->block = block;
2250 dev->min_block_size = min_block_size;
2251 free_memory(rec.data);
2255 * Read two tapes written by the "fill" command and ensure
2256 * that the data is valid. If stop==1 we simulate full read back
2257 * of two tapes. If stop==-1 we simply read the last block and
2258 * verify that it is correct.
2260 static void unfillcmd()
2265 last_block1 = new_block(dev);
2266 last_block2 = new_block(dev);
2267 first_block = new_block(dev);
2268 sprintf(buf, "%s/btape.state", working_directory);
2269 fd = open(buf, O_RDONLY);
2271 uint32_t state_level;
2272 read(fd, &state_level, sizeof(btape_state_level));
2273 read(fd, &simple, sizeof(simple));
2274 read(fd, &last_block_num1, sizeof(last_block_num1));
2275 read(fd, &last_block_num2, sizeof(last_block_num2));
2276 read(fd, &last_file1, sizeof(last_file1));
2277 read(fd, &last_file2, sizeof(last_file2));
2278 read(fd, last_block1->buf, last_block1->buf_len);
2279 read(fd, last_block2->buf, last_block2->buf_len);
2280 read(fd, first_block->buf, first_block->buf_len);
2282 if (state_level != btape_state_level) {
2283 Pmsg0(-1, _("\nThe state file level has changed. You must redo\n"
2284 "the fill command.\n"));
2290 Pmsg2(-1, _("\nCould not find the state file: %s ERR=%s\n"
2291 "You must redo the fill command.\n"), buf, be.bstrerror());
2301 static bool do_unfill()
2303 DEV_BLOCK *block = dcr->block;
2311 Dmsg0(20, "Enter do_unfill\n");
2312 dev->set_cap(CAP_ANONVOLS); /* allow reading any volume */
2313 dev->clear_cap(CAP_LABEL); /* don't label anything here */
2317 time(&jcr->run_time); /* start counting time for rates */
2321 free_block(last_block);
2324 last_block_num = last_block_num1;
2325 last_file = last_file1;
2326 last_block = last_block1;
2328 free_restore_volume_list(jcr);
2330 bstrncpy(dcr->VolumeName, "TestVolume1|TestVolume2", sizeof(dcr->VolumeName));
2331 create_restore_volume_list(jcr);
2332 if (jcr->VolList != NULL) {
2333 jcr->VolList->Slot = 1;
2334 if (jcr->VolList->next != NULL) {
2335 jcr->VolList->next->Slot = 2;
2339 set_volume_name("TestVolume1", 1);
2342 /* Multiple Volume tape */
2343 /* Close device so user can use autochanger if desired */
2344 if (dev->has_cap(CAP_OFFLINEUNMOUNT)) {
2347 autochanger = autoload_device(dcr, 1, NULL);
2348 if (autochanger != 1) {
2350 get_cmd(_("Mount first tape. Press enter when ready: "));
2355 dev->num_writers = 0;
2356 if (!acquire_device_for_read(dcr)) {
2357 Pmsg1(-1, "%s", dev->errmsg);
2361 * We now have the first tape mounted.
2362 * Note, re-reading last block may have caused us to
2363 * loose track of where we are (block number unknown).
2365 Pmsg0(-1, _("Rewinding.\n"));
2366 if (!dev->rewind(dcr)) { /* get to a known place on tape */
2369 /* Read the first 10000 records */
2370 Pmsg2(-1, _("Reading the first 10000 records from %u:%u.\n"),
2371 dev->file, dev->block_num);
2373 read_records(dcr, quickie_cb, my_mount_next_read_volume);
2374 Pmsg4(-1, _("Reposition from %u:%u to %u:%u\n"), dev->file, dev->block_num,
2375 last_file, last_block_num);
2376 if (!dev->reposition(dcr, last_file, last_block_num)) {
2377 Pmsg1(-1, _("Reposition error. ERR=%s\n"), dev->bstrerror());
2380 Pmsg1(-1, _("Reading block %u.\n"), last_block_num);
2381 if (!read_block_from_device(dcr, NO_BLOCK_NUMBER_CHECK)) {
2382 Pmsg1(-1, _("Error reading block: ERR=%s\n"), dev->bstrerror());
2385 if (compare_blocks(last_block, block)) {
2387 Pmsg0(-1, _("\nThe last block on the tape matches. Test succeeded.\n\n"));
2389 Pmsg0(-1, _("\nThe last block of the first tape matches.\n\n"));
2396 /* restore info for last block on second Volume */
2397 last_block_num = last_block_num2;
2398 last_file = last_file2;
2399 last_block = last_block2;
2401 /* Multiple Volume tape */
2402 /* Close device so user can use autochanger if desired */
2403 if (dev->has_cap(CAP_OFFLINEUNMOUNT)) {
2407 set_volume_name("TestVolume2", 2);
2409 autochanger = autoload_device(dcr, 1, NULL);
2410 if (autochanger != 1) {
2412 get_cmd(_("Mount second tape. Press enter when ready: "));
2416 if (!acquire_device_for_read(dcr)) {
2417 Pmsg1(-1, "%s", dev->errmsg);
2421 /* Space to "first" block which is last block not written
2422 * on the previous tape.
2424 Pmsg2(-1, _("Reposition from %u:%u to 0:1\n"), dev->file, dev->block_num);
2425 if (!dev->reposition(dcr, 0, 1)) {
2426 Pmsg1(-1, _("Reposition error. ERR=%s\n"), dev->bstrerror());
2429 Pmsg1(-1, _("Reading block %d.\n"), dev->block_num);
2430 if (!read_block_from_device(dcr, NO_BLOCK_NUMBER_CHECK)) {
2431 Pmsg1(-1, _("Error reading block: ERR=%s\n"), dev->bstrerror());
2434 if (compare_blocks(first_block, block)) {
2435 Pmsg0(-1, _("\nThe first block on the second tape matches.\n\n"));
2438 /* Now find and compare the last block */
2439 Pmsg4(-1, _("Reposition from %u:%u to %u:%u\n"), dev->file, dev->block_num,
2440 last_file, last_block_num);
2441 if (!dev->reposition(dcr, last_file, last_block_num)) {
2442 Pmsg1(-1, _("Reposition error. ERR=%s\n"), dev->bstrerror());
2445 Pmsg1(-1, _("Reading block %d.\n"), dev->block_num);
2446 if (!read_block_from_device(dcr, NO_BLOCK_NUMBER_CHECK)) {
2447 Pmsg1(-1, _("Error reading block: ERR=%s\n"), dev->bstrerror());
2450 if (compare_blocks(last_block, block)) {
2451 Pmsg0(-1, _("\nThe last block on the second tape matches. Test succeeded.\n\n"));
2456 free_block(last_block1);
2457 free_block(last_block2);
2458 free_block(first_block);
2462 /* Read 10000 records then stop */
2463 static bool quickie_cb(DCR *dcr, DEV_RECORD *rec)
2465 DEVICE *dev = dcr->dev;
2467 if (quickie_count == 10000) {
2468 Pmsg2(-1, _("10000 records read now at %d:%d\n"), dev->file, dev->block_num);
2470 return quickie_count < 10000;
2473 static bool compare_blocks(DEV_BLOCK *last_block, DEV_BLOCK *block)
2476 uint32_t CheckSum, block_len;
2479 p = last_block->buf;
2481 unser_begin(q, BLKHDR2_LENGTH);
2482 unser_uint32(CheckSum);
2483 unser_uint32(block_len);
2484 while (q < (block->buf+block_len)) {
2491 dump_block(last_block, _("Last block written"));
2493 dump_block(block, _("Block read back"));
2494 Pmsg1(-1, _("\n\nThe blocks differ at byte %u\n"), p - last_block->buf);
2495 Pmsg0(-1, _("\n\n!!!! The last block written and the block\n"
2496 "that was read back differ. The test FAILED !!!!\n"
2497 "This must be corrected before you use Bacula\n"
2498 "to write multi-tape Volumes.!!!!\n"));
2502 dump_block(last_block, _("Last block written"));
2503 dump_block(block, _("Block read back"));
2509 * Write current block to tape regardless of whether or
2510 * not it is full. If the tape fills, attempt to
2511 * acquire another tape.
2513 static int flush_block(DEV_BLOCK *block, int dump)
2517 uint32_t this_file, this_block_num;
2521 this_block = new_block(dev);
2524 last_block = new_block(dev);
2527 this_file = dev->file;
2528 this_block_num = dev->block_num;
2529 if (!write_block_to_dev(dcr)) {
2530 Pmsg3(000, _("Last block at: %u:%u this_dev_block_num=%d\n"),
2531 last_file, last_block_num, this_block_num);
2534 * This is 1st tape, so save first tape info separate
2535 * from second tape info
2537 last_block_num1 = last_block_num;
2538 last_file1 = last_file;
2539 last_block1 = dup_block(last_block);
2540 last_block2 = dup_block(last_block);
2541 first_block = dup_block(block); /* first block second tape */
2544 Pmsg3(000, _("Block not written: FileIndex=%u blk_block=%u Size=%u\n"),
2545 (unsigned)file_index, block->BlockNumber, block->block_len);
2546 dump_block(last_block, _("Last block written"));
2548 dump_block(block, _("Block not written"));
2551 eot_block = block->BlockNumber;
2552 eot_block_len = block->block_len;
2553 eot_FileIndex = file_index;
2557 now -= jcr->run_time;
2559 now = 1; /* don't divide by zero */
2561 kbs = (double)dev->VolCatInfo.VolCatBytes / (1000 * now);
2562 vol_size = dev->VolCatInfo.VolCatBytes;
2563 Pmsg4(000, _("End of tape %d:%d. VolumeCapacity=%s. Write rate = %.1f KB/s\n"),
2564 dev->file, dev->block_num,
2565 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), kbs);
2568 stop = -1; /* stop, but do simplified test */
2570 /* Full test in progress */
2571 if (!fixup_device_block_write_error(jcr->dcr)) {
2572 Pmsg1(000, _("Cannot fixup device error. %s\n"), dev->bstrerror());
2577 BlockNumber = 0; /* start counting for second tape */
2580 return 1; /* end of tape reached */
2583 /* Save contents after write so that the header is serialized */
2584 memcpy(this_block->buf, block->buf, this_block->buf_len);
2587 * Note, we always read/write to block, but we toggle
2588 * copying it to one or another of two allocated blocks.
2589 * Switch blocks so that the block just successfully written is
2590 * always in last_block.
2592 tblock = last_block;
2593 last_block = this_block;
2594 this_block = tblock;
2595 last_file = this_file;
2596 last_block_num = this_block_num;
2604 * First we label the tape, then we fill
2605 * it with data get a new tape and write a few blocks.
2607 static void qfillcmd()
2609 DEV_BLOCK *block = dcr->block;
2610 DEV_RECORD *rec = dcr->rec;
2613 Pmsg0(0, _("Test writing blocks of 64512 bytes to tape.\n"));
2615 get_cmd(_("How many blocks do you want to write? (1000): "));
2622 sm_check(__FILE__, __LINE__, false);
2624 i = block->buf_len - 100;
2626 rec->data = check_pool_memory_size(rec->data, i);
2627 memset(rec->data, i & 0xFF, i);
2632 Pmsg1(0, _("Begin writing %d Bacula blocks to tape ...\n"), count);
2633 for (i=0; i < count; i++) {
2638 if (!write_record_to_block(block, rec)) {
2639 Pmsg0(0, _("Error writing record to block.\n"));
2642 if (!write_block_to_dev(dcr)) {
2643 Pmsg0(0, _("Error writing block to device.\n"));
2648 print_speed(dev->VolCatInfo.VolCatBytes);
2650 if (dev->has_cap(CAP_TWOEOF)) {
2657 sm_check(__FILE__, __LINE__, false);
2661 * Fill a tape using raw write() command
2663 static void rawfill_cmd()
2665 DEV_BLOCK *block = dcr->block;
2667 uint32_t block_num = 0;
2672 fill_buffer(FILL_RANDOM, block->buf, block->buf_len);
2675 p = (uint32_t *)block->buf;
2676 Pmsg1(0, _("Begin writing raw blocks of %u bytes.\n"), block->buf_len);
2679 stat = dev->d_write(dev->fd(), block->buf, block->buf_len);
2680 if (stat == (int)block->buf_len) {
2681 if ((block_num++ % 100) == 0) {
2686 for (i=1; i<(block->buf_len-sizeof(uint32_t))/sizeof(uint32_t)-1; i+=100) {
2689 jcr->JobBytes += stat;
2697 printf(_("Write failed at block %u. stat=%d ERR=%s\n"), block_num, stat,
2698 be.bstrerror(my_errno));
2700 print_speed(jcr->JobBytes);
2706 struct cmdstruct { const char *key; void (*func)(); const char *help; };
2707 static struct cmdstruct commands[] = {
2708 {NT_("autochanger"),autochangercmd, _("test autochanger")},
2709 {NT_("bsf"), bsfcmd, _("backspace file")},
2710 {NT_("bsr"), bsrcmd, _("backspace record")},
2711 {NT_("cap"), capcmd, _("list device capabilities")},
2712 {NT_("clear"), clearcmd, _("clear tape errors")},
2713 {NT_("eod"), eodcmd, _("go to end of Bacula data for append")},
2714 {NT_("eom"), eomcmd, _("go to the physical end of medium")},
2715 {NT_("fill"), fillcmd, _("fill tape, write onto second volume")},
2716 {NT_("unfill"), unfillcmd, _("read filled tape")},
2717 {NT_("fsf"), fsfcmd, _("forward space a file")},
2718 {NT_("fsr"), fsrcmd, _("forward space a record")},
2719 {NT_("help"), helpcmd, _("print this command")},
2720 {NT_("label"), labelcmd, _("write a Bacula label to the tape")},
2721 {NT_("load"), loadcmd, _("load a tape")},
2722 {NT_("quit"), quitcmd, _("quit btape")},
2723 {NT_("rawfill"), rawfill_cmd, _("use write() to fill tape")},
2724 {NT_("readlabel"), readlabelcmd, _("read and print the Bacula tape label")},
2725 {NT_("rectest"), rectestcmd, _("test record handling functions")},
2726 {NT_("rewind"), rewindcmd, _("rewind the tape")},
2727 {NT_("scan"), scancmd, _("read() tape block by block to EOT and report")},
2728 {NT_("scanblocks"),scan_blocks, _("Bacula read block by block to EOT and report")},
2729 {NT_("speed"), speed_test, _("try different configuration and report drive speed")},
2730 {NT_("status"), statcmd, _("print tape status")},
2731 {NT_("test"), testcmd, _("General test Bacula tape functions")},
2732 {NT_("weof"), weofcmd, _("write an EOF on the tape")},
2733 {NT_("wr"), wrcmd, _("write a single Bacula block")},
2734 {NT_("rr"), rrcmd, _("read a single record")},
2735 {NT_("rb"), rbcmd, _("read a single Bacula block")},
2736 {NT_("qfill"), qfillcmd, _("quick fill command")}
2738 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
2746 while (!quit && get_cmd("*")) {
2747 sm_check(__FILE__, __LINE__, false);
2749 parse_args(cmd, &args, &argc, argk, argv, MAX_CMD_ARGS);
2750 for (i=0; i<comsize; i++) /* search for command */
2751 if (argc > 0 && fstrsch(argk[0], commands[i].key)) {
2752 (*commands[i].func)(); /* go execute command */
2756 if (*cmd && !found) {
2757 Pmsg1(0, _("\"%s\" is an invalid command\n"), cmd);
2762 static void helpcmd()
2766 printf(_("Interactive commands:\n"));
2767 printf(_(" Command Description\n ======= ===========\n"));
2768 for (i=0; i<comsize; i++)
2769 printf(" %-10s %s\n", commands[i].key, commands[i].help);
2777 "\nVersion: %s (%s)\n\n"
2778 "Usage: btape <options> <device_name>\n"
2779 " -b <file> specify bootstrap file\n"
2780 " -c <file> set configuration file to file\n"
2781 " -d <nn> set debug level to <nn>\n"
2782 " -dt print timestamp in debug output\n"
2783 " -p proceed inspite of I/O errors\n"
2784 " -s turn off signals\n"
2786 " -? print this message.\n"
2787 "\n"), 2000, VERSION, BDATE);
2792 * Get next input command from terminal. This
2793 * routine is REALLY primitive, and should be enhanced
2794 * to have correct backspacing, etc.
2797 get_cmd(const char *prompt)
2802 fprintf(stdout, "%s", prompt);
2804 /* We really should turn off echoing and pretty this
2808 while ((ch = fgetc(stdin)) != EOF) {
2810 strip_trailing_junk(cmd);
2812 } else if (ch == 4 || ch == 0xd3 || ch == 0x8) {
2826 /* Dummies to replace askdir.c */
2827 bool dir_update_file_attributes(DCR *dcr, DEV_RECORD *rec) { return 1;}
2828 bool dir_send_job_status(JCR *jcr) {return 1;}
2830 bool dir_update_volume_info(DCR *dcr, bool relabel, bool update_LastWritten)
2836 bool dir_get_volume_info(DCR *dcr, enum get_vol_info_rw writing)
2838 Dmsg0(20, "Enter dir_get_volume_info\n");
2839 bstrncpy(dcr->VolCatInfo.VolCatName, dcr->VolumeName, sizeof(dcr->VolCatInfo.VolCatName));
2843 bool dir_create_jobmedia_record(DCR *dcr, bool zero)
2845 dcr->WroteVol = false;
2850 bool dir_find_next_appendable_volume(DCR *dcr)
2852 Dmsg1(20, "Enter dir_find_next_appendable_volume. stop=%d\n", stop);
2853 return dcr->VolumeName[0] != 0;
2856 bool dir_ask_sysop_to_mount_volume(DCR *dcr, int /* mode */)
2858 DEVICE *dev = dcr->dev;
2859 Dmsg0(20, "Enter dir_ask_sysop_to_mount_volume\n");
2860 if (dcr->VolumeName[0] == 0) {
2861 return dir_ask_sysop_to_create_appendable_volume(dcr);
2863 Pmsg1(-1, "%s", dev->errmsg); /* print reason */
2864 if (dcr->VolumeName[0] == 0 || strcmp(dcr->VolumeName, "TestVolume2") == 0) {
2865 fprintf(stderr, _("Mount second Volume on device %s and press return when ready: "),
2868 fprintf(stderr, _("Mount Volume \"%s\" on device %s and press return when ready: "),
2869 dcr->VolumeName, dev->print_name());
2876 bool dir_ask_sysop_to_create_appendable_volume(DCR *dcr)
2879 DEVICE *dev = dcr->dev;
2880 Dmsg0(20, "Enter dir_ask_sysop_to_create_appendable_volume\n");
2882 set_volume_name("TestVolume1", 1);
2884 set_volume_name("TestVolume2", 2);
2886 /* Close device so user can use autochanger if desired */
2887 if (dev->has_cap(CAP_OFFLINEUNMOUNT)) {
2890 autochanger = autoload_device(dcr, 1, NULL);
2891 if (autochanger != 1) {
2892 fprintf(stderr, _("Mount blank Volume on device %s and press return when ready: "),
2903 static bool my_mount_next_read_volume(DCR *dcr)
2906 JCR *jcr = dcr->jcr;
2907 DEV_BLOCK *block = dcr->block;
2909 Dmsg0(20, "Enter my_mount_next_read_volume\n");
2910 Pmsg2(000, _("End of Volume \"%s\" %d records.\n"), dcr->VolumeName,
2913 volume_unused(dcr); /* release current volume */
2914 if (LastBlock != block->BlockNumber) {
2915 VolBytes += block->block_len;
2917 LastBlock = block->BlockNumber;
2919 now -= jcr->run_time;
2923 kbs = (double)VolBytes / (1000.0 * (double)now);
2924 Pmsg3(-1, _("Read block=%u, VolBytes=%s rate=%.1f KB/s\n"), block->BlockNumber,
2925 edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
2927 if (strcmp(dcr->VolumeName, "TestVolume2") == 0) {
2932 set_volume_name("TestVolume2", 2);
2935 if (!acquire_device_for_read(dcr)) {
2936 Pmsg2(0, _("Cannot open Dev=%s, Vol=%s\n"), dev->print_name(), dcr->VolumeName);
2939 return true; /* next volume mounted */
2942 static void set_volume_name(const char *VolName, int volnum)
2944 DCR *dcr = jcr->dcr;
2945 VolumeName = VolName;
2947 bstrncpy(dev->VolCatInfo.VolCatName, VolName, sizeof(dev->VolCatInfo.VolCatName));
2948 bstrncpy(dcr->VolCatInfo.VolCatName, VolName, sizeof(dcr->VolCatInfo.VolCatName));
2949 bstrncpy(dcr->VolumeName, VolName, sizeof(dcr->VolumeName));
2950 dcr->VolCatInfo.Slot = volnum;
2951 dcr->VolCatInfo.InChanger = true;