3 * Bacula Tape manipulation program
5 * Has various tape manipulation commands -- mostly for
6 * use in determining how tapes really work.
8 * Kern Sibbald, April MM
10 * Note, this program reads stored.conf, and will only
11 * talk to devices that are configured.
17 Copyright (C) 2000-2004 Kern Sibbald and John Walker
19 This program is free software; you can redistribute it and/or
20 modify it under the terms of the GNU General Public License as
21 published by the Free Software Foundation; either version 2 of
22 the License, or (at your option) any later version.
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
29 You should have received a copy of the GNU General Public
30 License along with this program; if not, write to the Free
31 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
40 /* External subroutines */
41 extern void free_config_resources();
43 /* Exported variables */
46 int bsize = TAPE_BSIZE;
47 char VolName[MAX_NAME_LENGTH];
50 * If you change the format of the state file,
51 * increment this value
53 static uint32_t btape_state_level = 1;
56 DEVRES *device = NULL;
59 /* Forward referenced subroutines */
60 static void do_tape_cmds();
61 static void helpcmd();
62 static void scancmd();
63 static void rewindcmd();
64 static void clearcmd();
68 static void fillcmd();
69 static void qfillcmd();
70 static void statcmd();
71 static void unfillcmd();
72 static int flush_block(DEV_BLOCK *block, int dump);
73 static int record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec);
74 static int my_mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block);
75 static void scan_blocks();
76 static void set_volume_name(char *VolName, int volnum);
77 static void rawfill_cmd();
78 static void bfill_cmd();
79 static bool open_the_device();
80 static char *edit_device_codes(JCR *jcr, char *omsg, char *imsg, char *cmd);
81 static void autochangercmd();
82 static void do_unfill();
85 /* Static variables */
86 #define CONFIG_FILE "bacula-sd.conf"
89 #define MAX_CMD_ARGS 30
92 static char *argk[MAX_CMD_ARGS];
93 static char *argv[MAX_CMD_ARGS];
96 static BSR *bsr = NULL;
97 static int signals = TRUE;
100 static uint64_t vol_size;
101 static uint64_t VolBytes;
104 static int32_t file_index;
105 static int end_of_tape = 0;
106 static uint32_t LastBlock = 0;
107 static uint32_t eot_block;
108 static uint32_t eot_block_len;
109 static uint32_t eot_FileIndex;
110 static int dumped = 0;
111 static DEV_BLOCK *last_block = NULL;
112 static DEV_BLOCK *this_block = NULL;
113 static uint32_t last_file = 0;
114 static uint32_t last_block_num = 0;
115 static uint32_t BlockNumber = 0;
116 static int simple = FALSE;
118 static char *VolumeName = NULL;
121 static JCR *jcr = NULL;
125 static void terminate_btape(int sig);
126 int get_cmd(char *prompt);
129 /*********************************************************************
131 * Main Bacula Pool Creation Program
134 int main(int margc, char *margv[])
139 if (TAPE_BSIZE % DEV_BSIZE != 0 || TAPE_BSIZE / DEV_BSIZE == 0) {
140 Emsg2(M_ABORT, 0, "Tape block size (%d) not multiple of system size (%d)\n",
141 TAPE_BSIZE, DEV_BSIZE);
143 if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
144 Emsg1(M_ABORT, 0, "Tape block size (%d) is not a power of 2\n", TAPE_BSIZE);
147 printf("Tape block granularity is %d bytes.\n", TAPE_BSIZE);
149 working_directory = "/tmp";
150 my_name_is(margc, margv, "btape");
151 init_msg(NULL, NULL);
153 while ((ch = getopt(margc, margv, "b:c:d:sv?")) != -1) {
155 case 'b': /* bootstrap file */
156 bsr = parse_bsr(NULL, optarg);
157 // dump_bsr(bsr, true);
160 case 'c': /* specify config file */
161 if (configfile != NULL) {
164 configfile = bstrdup(optarg);
167 case 'd': /* set debug level */
168 debug_level = atoi(optarg);
169 if (debug_level <= 0) {
192 cmd = get_pool_memory(PM_FNAME);
193 args = get_pool_memory(PM_FNAME);
196 init_signals(terminate_btape);
199 if (configfile == NULL) {
200 configfile = bstrdup(CONFIG_FILE);
203 daemon_start_time = time(NULL);
205 parse_config(configfile);
208 /* See if we can open a device */
210 Pmsg0(000, "No archive name specified.\n");
213 } else if (margc != 1) {
214 Pmsg0(000, "Improper number of arguments specified.\n");
219 jcr = setup_jcr("btape", margv[0], bsr, NULL);
220 dev = setup_to_access_device(jcr, 0); /* acquire for write */
224 dev->max_volume_size = 0;
225 if (!open_the_device()) {
229 Dmsg0(200, "Do tape commands\n");
237 static void terminate_btape(int stat)
240 sm_check(__FILE__, __LINE__, False);
244 free_config_resources();
246 free_pool_memory(args);
250 free_pool_memory(cmd);
258 if (debug_level > 10)
259 print_memory_pool_stats();
269 free_block(last_block);
272 free_block(this_block);
276 close_memory_pool(); /* free memory in pool */
282 static bool open_the_device()
286 block = new_block(dev);
288 if (!(dev->state & ST_OPENED)) {
289 Dmsg1(200, "Opening device %s\n", jcr->VolumeName);
290 if (open_dev(dev, jcr->VolumeName, READ_WRITE) < 0) {
291 Emsg1(M_FATAL, 0, _("dev open failed: %s\n"), dev->errmsg);
297 Dmsg1(000, "open_dev %s OK\n", dev_name(dev));
310 * Write a label to the tape
312 static void labelcmd()
315 pm_strcpy(&cmd, VolumeName);
317 if (!get_cmd("Enter Volume Name: ")) {
322 if (!(dev->state & ST_OPENED)) {
323 if (!open_device(dev)) {
324 Pmsg1(0, "Device open failed. ERR=%s\n", strerror_dev(dev));
327 write_volume_label_to_dev(jcr, jcr->device, cmd, "Default");
331 * Read the tape label
333 static void readlabelcmd()
335 int save_debug_level = debug_level;
339 block = new_block(dev);
340 stat = read_dev_volume_label(jcr, dev, block);
343 Pmsg0(0, "Volume has no label.\n");
346 Pmsg0(0, "Volume label read correctly.\n");
349 Pmsg1(0, "I/O error on device: ERR=%s", strerror_dev(dev));
352 Pmsg0(0, "Volume name error\n");
354 case VOL_CREATE_ERROR:
355 Pmsg1(0, "Error creating label. ERR=%s", strerror_dev(dev));
357 case VOL_VERSION_ERROR:
358 Pmsg0(0, "Volume version error.\n");
360 case VOL_LABEL_ERROR:
361 Pmsg0(0, "Bad Volume label type.\n");
364 Pmsg0(0, "Unknown error.\n");
369 dump_volume_label(dev);
370 debug_level = save_debug_level;
376 * Load the tape should have prevously been taken
377 * off line, otherwise this command is not necessary.
379 static void loadcmd()
382 if (!load_dev(dev)) {
383 Pmsg1(0, "Bad status from load. ERR=%s\n", strerror_dev(dev));
385 Pmsg1(0, "Loaded %s\n", dev_name(dev));
391 static void rewindcmd()
393 if (!rewind_dev(dev)) {
394 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
395 clrerror_dev(dev, -1);
397 Pmsg1(0, "Rewound %s\n", dev_name(dev));
402 * Clear any tape error
404 static void clearcmd()
406 clrerror_dev(dev, -1);
410 * Write and end of file on the tape
412 static void weofcmd()
423 if ((stat = weof_dev(dev, num)) < 0) {
424 Pmsg2(0, "Bad status from weof %d. ERR=%s\n", stat, strerror_dev(dev));
427 Pmsg3(0, "Wrote %d EOF%s to %s\n", num, num==1?"":"s", dev_name(dev));
432 /* Go to the end of the medium -- raw command
433 * The idea was orginally that the end of the Bacula
434 * medium would be flagged differently. This is not
435 * currently the case. So, this is identical to the
441 Pmsg1(0, "%s", strerror_dev(dev));
444 Pmsg0(0, _("Moved to end of medium.\n"));
449 * Go to the end of the medium (either hardware determined
450 * or defined by two eofs.
470 if (!bsf_dev(dev, num)) {
471 Pmsg1(0, _("Bad status from bsf. ERR=%s\n"), strerror_dev(dev));
473 Pmsg2(0, _("Backspaced %d file%s.\n"), num, num==1?"":"s");
489 if (!bsr_dev(dev, num)) {
490 Pmsg1(0, _("Bad status from bsr. ERR=%s\n"), strerror_dev(dev));
492 Pmsg2(0, _("Backspaced %d record%s.\n"), num, num==1?"":"s");
497 * List device capabilities as defined in the
502 printf(_("Configured device capabilities:\n"));
503 printf("%sEOF ", dev->capabilities & CAP_EOF ? "" : "!");
504 printf("%sBSR ", dev->capabilities & CAP_BSR ? "" : "!");
505 printf("%sBSF ", dev->capabilities & CAP_BSF ? "" : "!");
506 printf("%sFSR ", dev->capabilities & CAP_FSR ? "" : "!");
507 printf("%sFSF ", dev->capabilities & CAP_FSF ? "" : "!");
508 printf("%sEOM ", dev->capabilities & CAP_EOM ? "" : "!");
509 printf("%sREM ", dev->capabilities & CAP_REM ? "" : "!");
510 printf("%sRACCESS ", dev->capabilities & CAP_RACCESS ? "" : "!");
511 printf("%sAUTOMOUNT ", dev->capabilities & CAP_AUTOMOUNT ? "" : "!");
512 printf("%sLABEL ", dev->capabilities & CAP_LABEL ? "" : "!");
513 printf("%sANONVOLS ", dev->capabilities & CAP_ANONVOLS ? "" : "!");
514 printf("%sALWAYSOPEN ", dev->capabilities & CAP_ALWAYSOPEN ? "" : "!");
517 printf(_("Device status:\n"));
518 printf("%sOPENED ", dev->state & ST_OPENED ? "" : "!");
519 printf("%sTAPE ", dev->state & ST_TAPE ? "" : "!");
520 printf("%sLABEL ", dev->state & ST_LABEL ? "" : "!");
521 printf("%sMALLOC ", dev->state & ST_MALLOC ? "" : "!");
522 printf("%sAPPEND ", dev->state & ST_APPEND ? "" : "!");
523 printf("%sREAD ", dev->state & ST_READ ? "" : "!");
524 printf("%sEOT ", dev->state & ST_EOT ? "" : "!");
525 printf("%sWEOT ", dev->state & ST_WEOT ? "" : "!");
526 printf("%sEOF ", dev->state & ST_EOF ? "" : "!");
527 printf("%sNEXTVOL ", dev->state & ST_NEXTVOL ? "" : "!");
528 printf("%sSHORT ", dev->state & ST_SHORT ? "" : "!");
531 printf(_("Device parameters:\n"));
532 printf("Device name: %s\n", dev->dev_name);
533 printf("File=%u block=%u\n", dev->file, dev->block_num);
534 printf("Min block=%u Max block=%u\n", dev->min_block_size, dev->max_block_size);
542 * Test writting larger and larger records.
543 * This is a torture test for records.
545 static void rectestcmd()
551 Pmsg0(0, "Test writting larger and larger records.\n"
552 "This is a torture test for records.\nI am going to write\n"
553 "larger and larger records. It will stop when the record size\n"
554 "plus the header exceeds the block size (by default about 64K)\n");
557 get_cmd("Do you want to continue? (y/n): ");
559 Pmsg0(000, "Command aborted.\n");
563 sm_check(__FILE__, __LINE__, False);
564 block = new_block(dev);
567 for (i=1; i<500000; i++) {
568 rec->data = check_pool_memory_size(rec->data, i);
569 memset(rec->data, i & 0xFF, i);
571 sm_check(__FILE__, __LINE__, False);
572 if (write_record_to_block(block, rec)) {
575 Pmsg2(0, "Block %d i=%d\n", blkno, i);
579 sm_check(__FILE__, __LINE__, False);
583 sm_check(__FILE__, __LINE__, False);
587 * This test attempts to re-read a block written by Bacula
588 * normally at the end of the tape. Bacula will then back up
589 * over the two eof marks, backup over the record and reread
590 * it to make sure it is valid. Bacula can skip this validation
591 * if you set "Backward space record = no"
593 static int re_read_block_test()
600 if (!(dev->capabilities & CAP_BSR)) {
601 Pmsg0(-1, _("Skipping read backwards test because BSR turned off.\n"));
605 Pmsg0(-1, _("\n=== Write, backup, and re-read test ===\n\n"
606 "I'm going to write three records and an eof\n"
607 "then backup over the eof and re-read the last record.\n"
608 "Bacula does this after writing the last block on the\n"
609 "tape to verify that the block was written correctly.\n"
610 "It is not an *essential* feature ...\n\n"));
612 block = new_block(dev);
614 rec->data = check_pool_memory_size(rec->data, block->buf_len);
615 len = rec->data_len = block->buf_len-100;
616 memset(rec->data, 1, rec->data_len);
617 if (!write_record_to_block(block, rec)) {
618 Pmsg0(0, _("Error writing record to block.\n"));
621 if (!write_block_to_dev(jcr, dev, block)) {
622 Pmsg0(0, _("Error writing block to device.\n"));
625 Pmsg1(0, _("Wrote first record of %d bytes.\n"), rec->data_len);
627 memset(rec->data, 2, rec->data_len);
628 if (!write_record_to_block(block, rec)) {
629 Pmsg0(0, _("Error writing record to block.\n"));
632 if (!write_block_to_dev(jcr, dev, block)) {
633 Pmsg0(0, _("Error writing block to device.\n"));
636 Pmsg1(0, _("Wrote second record of %d bytes.\n"), rec->data_len);
638 memset(rec->data, 3, rec->data_len);
639 if (!write_record_to_block(block, rec)) {
640 Pmsg0(0, _("Error writing record to block.\n"));
643 if (!write_block_to_dev(jcr, dev, block)) {
644 Pmsg0(0, _("Error writing block to device.\n"));
647 Pmsg1(0, _("Wrote third record of %d bytes.\n"), rec->data_len);
650 if (dev_cap(dev, CAP_TWOEOF)) {
653 if (!bsf_dev(dev, 1)) {
654 Pmsg1(0, _("Backspace file failed! ERR=%s\n"), strerror_dev(dev));
657 if (dev_cap(dev, CAP_TWOEOF)) {
658 if (!bsf_dev(dev, 1)) {
659 Pmsg1(0, _("Backspace file failed! ERR=%s\n"), strerror_dev(dev));
663 Pmsg0(0, "Backspaced over EOF OK.\n");
664 if (!bsr_dev(dev, 1)) {
665 Pmsg1(0, _("Backspace record failed! ERR=%s\n"), strerror_dev(dev));
668 Pmsg0(0, "Backspace record OK.\n");
669 if (!read_block_from_dev(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
670 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
673 memset(rec->data, 0, rec->data_len);
674 if (!read_record_from_block(block, rec)) {
675 Pmsg1(0, _("Read block failed! ERR=%s\n"), strerror(dev->dev_errno));
678 for (int i=0; i<len; i++) {
679 if (rec->data[i] != 3) {
680 Pmsg0(0, _("Bad data in record. Test failed!\n"));
684 Pmsg0(0, _("\nBlock re-read correct. Test succeeded!\n"));
685 Pmsg0(-1, _("=== End Write, backup, and re-read test ===\n\n"));
693 Pmsg0(0, _("This is not terribly serious since Bacula only uses\n"
694 "this function to verify the last block written to the\n"
695 "tape. Bacula will skip the last block verification\n"
697 "Backward Space Record = No\n\n"
698 "to your Storage daemon's Device resource definition.\n"));
704 * This test writes some records, then writes an end of file,
705 * rewinds the tape, moves to the end of the data and attepts
706 * to append to the tape. This function is essential for
707 * Bacula to be able to write multiple jobs to the tape.
709 static int append_test()
711 Pmsg0(-1, _("\n\n=== Append files test ===\n\n"
712 "This test is essential to Bacula.\n\n"
713 "I'm going to write one record in file 0,\n"
714 " two records in file 1,\n"
715 " and three records in file 2\n\n"));
719 weofcmd(); /* end file 0 */
722 weofcmd(); /* end file 1 */
726 weofcmd(); /* end file 2 */
727 if (dev_cap(dev, CAP_TWOEOF)) {
731 Pmsg0(0, _("Now moving to end of medium.\n"));
733 Pmsg2(-1, _("We should be in file 3. I am at file %d. This is %s\n"),
734 dev->file, dev->file == 3 ? "correct!" : "NOT correct!!!!");
736 if (dev->file != 3) {
740 Pmsg0(-1, _("\nNow the important part, I am going to attempt to append to the tape.\n\n"));
743 if (dev_cap(dev, CAP_TWOEOF)) {
747 Pmsg0(-1, _("Done appending, there should be no I/O errors\n\n"));
748 Pmsg0(-1, "Doing Bacula scan of blocks:\n");
750 Pmsg0(-1, _("End scanning the tape.\n"));
751 Pmsg2(-1, _("We should be in file 4. I am at file %d. This is %s\n"),
752 dev->file, dev->file == 4 ? "correct!" : "NOT correct!!!!");
754 if (dev->file != 4) {
763 * This test exercises the autochanger
765 static int autochanger_test()
767 POOLMEM *results, *changer;
768 int slot, status, loaded;
772 if (!dev_cap(dev, CAP_AUTOCHANGER)) {
775 if (!(jcr->device && jcr->device->changer_name && jcr->device->changer_command)) {
776 Pmsg0(-1, "\nAutochanger enabled, but no name or no command device specified.\n");
780 Pmsg0(-1, "\nTo test the autochanger you must have a blank tape in Slot 1.\n"
781 "I'm going to write on it.\n");
782 if (!get_cmd("\nDo you wish to continue with the Autochanger test? (y/n): ")) {
785 if (cmd[0] != 'y' && cmd[0] != 'Y') {
789 Pmsg0(-1, _("\n\n=== Autochanger test ===\n\n"));
791 results = get_pool_memory(PM_MESSAGE);
792 changer = get_pool_memory(PM_FNAME);
796 jcr->VolCatInfo.Slot = slot;
797 /* Find out what is loaded, zero means device is unloaded */
798 Pmsg0(-1, _("3301 Issuing autochanger \"loaded\" command.\n"));
799 changer = edit_device_codes(jcr, changer, jcr->device->changer_command,
801 status = run_program(changer, timeout, results);
802 Dmsg3(100, "run_prog: %s stat=%d result=%s\n", changer, status, results);
804 loaded = atoi(results);
806 Pmsg1(-1, _("3991 Bad autochanger \"load slot\" status=%d.\n"), status);
807 loaded = -1; /* force unload */
811 Pmsg1(-1, "Slot %d loaded. I am going to unload it.\n", loaded);
813 Pmsg0(-1, "Nothing loaded into the drive. OK.\n");
815 Dmsg1(100, "Results from loaded query=%s\n", results);
817 offline_or_rewind_dev(dev);
818 /* We are going to load a new tape, so close the device */
819 force_close_dev(dev);
820 Pmsg0(-1, _("3302 Issuing autochanger \"unload\" command.\n"));
821 changer = edit_device_codes(jcr, changer,
822 jcr->device->changer_command, "unload");
823 status = run_program(changer, timeout, NULL);
824 Pmsg2(-1, "unload status=%s %d\n", status==0?"OK":"Bad", status);
830 Pmsg1(-1, _("3303 Issuing autochanger \"load slot %d\" command.\n"), slot);
831 changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "load");
832 Dmsg1(200, "Changer=%s\n", changer);
833 status = run_program(changer, timeout, NULL);
835 Pmsg1(-1, _("3304 Autochanger \"load slot %d\" status is OK.\n"), slot);
837 Pmsg1(-1, _("3992 Bad autochanger \"load slot\" status=%d.\n"), status);
841 if (!open_the_device()) {
844 bmicrosleep(sleep_time, 0);
845 if (!rewind_dev(dev)) {
846 Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
847 clrerror_dev(dev, -1);
848 Pmsg0(-1, "\nThe test failed, probably because you need to put\n"
849 "a longer sleep time in the mtx-script in the load) case.\n"
850 "Adding a 30 second sleep and trying again ...\n");
854 Pmsg1(0, "Rewound %s\n", dev_name(dev));
857 if ((status = weof_dev(dev, 1)) < 0) {
858 Pmsg2(0, "Bad status from weof %d. ERR=%s\n", status, strerror_dev(dev));
861 Pmsg1(0, "Wrote EOF to %s\n", dev_name(dev));
865 Pmsg1(-1, "\nThe test worked this time. Please add:\n\n"
867 "to your mtx-changer script in the load) case.\n\n",
870 Pmsg0(-1, "\nThe test autochanger worked!!\n\n");
873 free_pool_memory(changer);
874 free_pool_memory(results);
879 free_pool_memory(changer);
880 free_pool_memory(results);
881 Pmsg0(-1, "You must correct this error or the Autochanger will not work.\n");
885 static void autochangercmd()
892 * This test assumes that the append test has been done,
893 * then it tests the fsf function.
895 static int fsf_test()
897 bool set_off = false;
899 Pmsg0(-1, _("\n\n=== Forward space files test ===\n\n"
900 "This test is essential to Bacula.\n\n"
901 "I'm going to write five files then test forward spacing\n\n"));
905 weofcmd(); /* end file 0 */
908 weofcmd(); /* end file 1 */
912 weofcmd(); /* end file 2 */
915 weofcmd(); /* end file 3 */
917 weofcmd(); /* end file 4 */
918 if (dev_cap(dev, CAP_TWOEOF)) {
924 Pmsg0(0, _("Now forward spacing 1 file.\n"));
925 if (!fsf_dev(dev, 1)) {
926 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
929 Pmsg2(-1, _("We should be in file 1. I am at file %d. This is %s\n"),
930 dev->file, dev->file == 1 ? "correct!" : "NOT correct!!!!");
932 if (dev->file != 1) {
936 Pmsg0(0, _("Now forward spacing 2 files.\n"));
937 if (!fsf_dev(dev, 2)) {
938 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
941 Pmsg2(-1, _("We should be in file 3. I am at file %d. This is %s\n"),
942 dev->file, dev->file == 3 ? "correct!" : "NOT correct!!!!");
944 if (dev->file != 3) {
949 Pmsg0(0, _("Now forward spacing 4 files.\n"));
950 if (!fsf_dev(dev, 4)) {
951 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
954 Pmsg2(-1, _("We should be in file 4. I am at file %d. This is %s\n"),
955 dev->file, dev->file == 4 ? "correct!" : "NOT correct!!!!");
957 if (dev->file != 4) {
961 Pmsg0(-1, "The test worked this time. Please add:\n\n"
962 " Fast Forward Space File = no\n\n"
963 "to your Device resource for this drive.\n");
967 Pmsg0(0, _("Now forward spacing 4 more files.\n"));
968 if (!fsf_dev(dev, 4)) {
969 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
972 Pmsg2(-1, _("We should be in file 5. I am at file %d. This is %s\n"),
973 dev->file, dev->file == 5 ? "correct!" : "NOT correct!!!!");
974 if (dev->file != 5) {
981 Pmsg0(-1, _("\nThe forward space file test failed.\n"));
982 if (dev_cap(dev, CAP_FASTFSF)) {
983 Pmsg0(-1, "You have Fast Forward Space File enabled.\n"
984 "I am turning it off then retrying the test.\n");
985 dev->capabilities &= ~CAP_FASTFSF;
989 Pmsg0(-1, "You must correct this error or Bacula will not work.\n");
998 * This is a general test of Bacula's functions
999 * needed to read and write the tape.
1001 static void testcmd()
1005 stat = append_test();
1006 if (stat == 1) { /* OK get out */
1009 if (stat == -1) { /* first test failed */
1010 if (dev_cap(dev, CAP_EOM) || dev_cap(dev, CAP_FASTFSF)) {
1011 Pmsg0(-1, "\nAppend test failed. Attempting again.\n"
1012 "Setting \"Hardware End of Medium = no\n"
1013 " and \"Fast Forward Space File = no\n"
1014 "and retrying append test.\n\n");
1015 dev->capabilities &= ~CAP_EOM; /* turn off eom */
1016 dev->capabilities &= ~CAP_FASTFSF; /* turn off fast fsf */
1017 stat = append_test();
1019 Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n"
1020 " Hardware End of Medium = No\n\n"
1021 " Fast Forward Space File = No\n"
1022 "to your Device resource in the Storage conf file.\n");
1026 Pmsg0(-1, "\n\nThat appears not to have corrected the problem.\n");
1029 /* Wrong count after append */
1031 Pmsg0(-1, "\n\nIt looks like the append failed. Attempting again.\n"
1032 "Setting \"BSF at EOM = yes\" and retrying append test.\n");
1033 dev->capabilities |= CAP_BSFATEOM; /* backspace on eom */
1034 stat = append_test();
1036 Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n"
1037 " Hardware End of Medium = No\n"
1038 " Fast Forward Space File = No\n"
1039 " BSF at EOM = yes\n\n"
1040 "to your Device resource in the Storage conf file.\n");
1046 Pmsg0(-1, "\nAppend test failed.\n\n");
1047 Pmsg0(-1, "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
1048 "Unable to correct the problem. You MUST fix this\n"
1049 "problem before Bacula can use your tape drive correctly\n");
1050 Pmsg0(-1, "\nPerhaps running Bacula in fixed block mode will work.\n"
1051 "Do so by setting:\n\n"
1052 "Minimum Block Size = nnn\n"
1053 "Maximum Block Size = nnn\n\n"
1054 "in your Storage daemon's Device definition.\n"
1055 "nnn must match your tape driver's block size.\n"
1056 "This, however, is not really an ideal solution.\n");
1060 Pmsg0(-1, _("\nThe above Bacula scan should have output identical to what follows.\n"
1061 "Please double check it ...\n"
1062 "=== Sample correct output ===\n"
1063 "1 block of 64448 bytes in file 1\n"
1064 "End of File mark.\n"
1065 "2 blocks of 64448 bytes in file 2\n"
1066 "End of File mark.\n"
1067 "3 blocks of 64448 bytes in file 3\n"
1068 "End of File mark.\n"
1069 "1 block of 64448 bytes in file 4\n"
1070 "End of File mark.\n"
1071 "Total files=4, blocks=7, bytes = 451,136\n"
1072 "=== End sample correct output ===\n\n"));
1074 Pmsg0(-1, _("If the above scan output is not identical to the\n"
1075 "sample output, you MUST correct the problem\n"
1076 "or Bacula will not be able to write multiple Jobs to \n"
1080 re_read_block_test();
1083 fsf_test(); /* do fast forward space file test */
1085 autochanger_test(); /* do autochanger test */
1087 Pmsg0(-1, _("\n=== End Append files test ===\n"));
1091 /* Forward space a file */
1092 static void fsfcmd()
1096 num = atoi(argk[1]);
1101 if (!fsf_dev(dev, num)) {
1102 Pmsg1(0, "Bad status from fsf. ERR=%s\n", strerror_dev(dev));
1105 Pmsg2(0, "Forward spaced %d file%s.\n", num, num==1?"":"s");
1108 /* Forward space a record */
1109 static void fsrcmd()
1113 num = atoi(argk[1]);
1118 if (!fsr_dev(dev, num)) {
1119 Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1122 Pmsg2(0, "Forward spaced %d record%s.\n", num, num==1?"":"s");
1127 * Write a Bacula block to the tape
1135 sm_check(__FILE__, __LINE__, False);
1136 block = new_block(dev);
1138 dump_block(block, "test");
1140 i = block->buf_len - 100;
1142 rec->data = check_pool_memory_size(rec->data, i);
1143 memset(rec->data, i & 0xFF, i);
1145 sm_check(__FILE__, __LINE__, False);
1146 if (!write_record_to_block(block, rec)) {
1147 Pmsg0(0, _("Error writing record to block.\n"));
1150 if (!write_block_to_dev(jcr, dev, block)) {
1151 Pmsg0(0, _("Error writing block to device.\n"));
1154 Pmsg1(0, _("Wrote one record of %d bytes.\n"), i);
1156 Pmsg0(0, _("Wrote block to device.\n"));
1159 sm_check(__FILE__, __LINE__, False);
1162 sm_check(__FILE__, __LINE__, False);
1166 * Read a record from the tape
1173 if (!get_cmd("Enter length to read: ")) {
1177 if (len < 0 || len > 1000000) {
1178 Pmsg0(0, _("Bad length entered, using default of 1024 bytes.\n"));
1181 buf = (char *)malloc(len);
1182 stat = read(dev->fd, buf, len);
1183 if (stat > 0 && stat <= len) {
1186 Pmsg3(0, _("Read of %d bytes gives stat=%d. ERR=%s\n"),
1187 len, stat, strerror(errno));
1193 * Scan tape by reading block by block. Report what is
1194 * on the tape. Note, this command does raw reads, and as such
1195 * will not work with fixed block size devices.
1197 static void scancmd()
1200 int blocks, tot_blocks, tot_files;
1206 blocks = block_size = tot_blocks = 0;
1208 if (dev->state & ST_EOT) {
1209 Pmsg0(0, "End of tape\n");
1212 update_pos_dev(dev);
1213 tot_files = dev->file;
1214 Pmsg1(0, _("Starting scan at file %u\n"), dev->file);
1216 if ((stat = read(dev->fd, buf, sizeof(buf))) < 0) {
1217 clrerror_dev(dev, -1);
1218 Mmsg2(&dev->errmsg, "read error on %s. ERR=%s.\n",
1219 dev->dev_name, strerror(dev->dev_errno));
1220 Pmsg2(0, "Bad status from read %d. ERR=%s\n", stat, strerror_dev(dev));
1222 printf("%d block%s of %d bytes in file %d\n",
1223 blocks, blocks>1?"s":"", block_size, dev->file);
1226 Dmsg1(200, "read status = %d\n", stat);
1228 if (stat != block_size) {
1229 update_pos_dev(dev);
1231 printf("%d block%s of %d bytes in file %d\n",
1232 blocks, blocks>1?"s":"", block_size, dev->file);
1237 if (stat == 0) { /* EOF */
1238 update_pos_dev(dev);
1239 printf("End of File mark.\n");
1240 /* Two reads of zero means end of tape */
1241 if (dev->state & ST_EOF)
1242 dev->state |= ST_EOT;
1244 dev->state |= ST_EOF;
1247 if (dev->state & ST_EOT) {
1248 printf("End of tape\n");
1251 } else { /* Got data */
1252 dev->state &= ~ST_EOF;
1258 update_pos_dev(dev);
1259 tot_files = dev->file - tot_files;
1260 printf("Total files=%d, blocks=%d, bytes = %s\n", tot_files, tot_blocks,
1261 edit_uint64_with_commas(bytes, ec1));
1266 * Scan tape by reading Bacula block by block. Report what is
1267 * on the tape. This function reads Bacula blocks, so if your
1268 * Device resource is correctly defined, it should work with
1269 * either variable or fixed block sizes.
1271 static void scan_blocks()
1273 int blocks, tot_blocks, tot_files;
1274 uint32_t block_size;
1279 block = new_block(dev);
1280 blocks = block_size = tot_blocks = 0;
1283 update_pos_dev(dev);
1284 tot_files = dev->file;
1286 if (!read_block_from_device(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
1287 Dmsg1(100, "!read_block(): ERR=%s\n", strerror_dev(dev));
1288 if (dev->state & ST_EOT) {
1290 printf("%d block%s of %d bytes in file %d\n",
1291 blocks, blocks>1?"s":"", block_size, dev->file);
1296 if (dev->state & ST_EOF) {
1298 printf("%d block%s of %d bytes in file %d\n",
1299 blocks, blocks>1?"s":"", block_size, dev->file);
1302 printf(_("End of File mark.\n"));
1305 if (dev->state & ST_SHORT) {
1307 printf("%d block%s of %d bytes in file %d\n",
1308 blocks, blocks>1?"s":"", block_size, dev->file);
1311 printf(_("Short block read.\n"));
1314 printf(_("Error reading block. ERR=%s\n"), strerror_dev(dev));
1317 if (block->block_len != block_size) {
1319 printf("%d block%s of %d bytes in file %d\n",
1320 blocks, blocks>1?"s":"", block_size, dev->file);
1323 block_size = block->block_len;
1327 bytes += block->block_len;
1328 Dmsg6(100, "Blk_blk=%u dev_blk=%u blen=%u bVer=%d SessId=%u SessTim=%u\n",
1329 block->BlockNumber, dev->block_num, block->block_len, block->BlockVer,
1330 block->VolSessionId, block->VolSessionTime);
1332 DEV_RECORD *rec = new_record();
1333 read_record_from_block(block, rec);
1334 Pmsg8(-1, "Blk_block: %u dev_blk=%u blen=%u First rec FI=%s SessId=%u SessTim=%u Strm=%s rlen=%d\n",
1335 block->BlockNumber, dev->block_num, block->block_len,
1336 FI_to_ascii(rec->FileIndex), rec->VolSessionId, rec->VolSessionTime,
1337 stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len);
1340 } else if (verbose > 1) {
1341 dump_block(block, "");
1347 tot_files = dev->file - tot_files;
1348 printf("Total files=%d, blocks=%d, bytes = %s\n", tot_files, tot_blocks,
1349 edit_uint64_with_commas(bytes, ec1));
1353 static void statcmd()
1355 int debug = debug_level;
1357 Pmsg2(0, "Device status: %u. ERR=%s\n", status_dev(dev), strerror_dev(dev));
1359 dump_volume_label(dev);
1361 debug_level = debug;
1366 * First we label the tape, then we fill
1367 * it with data get a new tape and write a few blocks.
1369 static void fillcmd()
1376 uint32_t min_block_size;
1386 This command simulates Bacula writing to a tape.\n\
1387 It requires either one or two blank tapes, which it\n\
1388 will label and write. It will print a status approximately\n\
1389 every 322 MB, and write an EOF every 3.2 GB. If you have\n\
1390 selected the simple test option, after writing the first tape\n\
1391 it will rewind it and re-read the last block written.\n\
1392 If you have selected the multiple tape test, when the first tape\n\
1393 fills, it will ask for a second, and after writing a few more \n\
1394 blocks, it will stop. Then it will begin re-reading the\n\
1396 This may take a long time -- hours! ...\n\n");
1398 get_cmd("Insert a blank tape then indicate if you want\n"
1399 "to run the simplified test (s) with one tape or\n"
1400 "the complete multiple tape (m) test: (s/m) ");
1401 if (cmd[0] == 's') {
1402 Pmsg0(-1, "Simple test (single tape) selected.\n");
1404 } else if (cmd[0] == 'm') {
1405 Pmsg0(-1, "Complete multiple tape test selected.\n");
1408 Pmsg0(000, "Command aborted.\n");
1412 set_volume_name("TestVolume1", 1);
1417 Dmsg1(20, "Begin append device=%s\n", dev_name(dev));
1420 /* Use fixed block size to simplify read back */
1421 min_block_size = dev->min_block_size;
1422 dev->min_block_size = dev->max_block_size;
1423 block = new_block(dev);
1426 * Acquire output device for writing. Note, after acquiring a
1427 * device, we MUST release it, which is done at the end of this
1430 Dmsg0(100, "just before acquire_device\n");
1431 if (!(dev=acquire_device_for_append(jcr, dev, block))) {
1432 set_jcr_job_status(jcr, JS_ErrorTerminated);
1437 Dmsg0(100, "Just after acquire_device_for_append\n");
1439 * Write Begin Session Record
1441 if (!write_session_label(jcr, block, SOS_LABEL)) {
1442 set_jcr_job_status(jcr, JS_ErrorTerminated);
1443 Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"),
1447 Pmsg0(-1, "Wrote Start Of Session label.\n");
1449 memset(&rec, 0, sizeof(rec));
1450 rec.data = get_memory(100000); /* max record size */
1452 #define REC_SIZE 32768
1453 rec.data_len = REC_SIZE;
1456 * Put some random data in the record
1458 fd = open("/dev/urandom", O_RDONLY);
1460 read(fd, rec.data, rec.data_len);
1463 uint32_t *p = (uint32_t *)rec.data;
1464 srandom(time(NULL));
1465 for (i=0; i<rec.data_len/sizeof(uint32_t); i++) {
1471 * Generate data as if from File daemon, write to device
1473 jcr->VolFirstIndex = 0;
1474 time(&jcr->run_time); /* start counting time for rates */
1476 Pmsg0(-1, "Begin writing Bacula records to tape ...\n");
1478 Pmsg0(-1, "Begin writing Bacula records to first tape ...\n");
1480 for (file_index = 0; ok && !job_canceled(jcr); ) {
1481 rec.VolSessionId = jcr->VolSessionId;
1482 rec.VolSessionTime = jcr->VolSessionTime;
1483 rec.FileIndex = ++file_index;
1484 rec.Stream = STREAM_FILE_DATA;
1486 /* Mix up the data just a bit */
1487 uint32_t *lp = (uint32_t *)rec.data;
1489 for (i=1; i < (rec.data_len-sizeof(uint32_t))/sizeof(uint32_t)-1; i++) {
1493 Dmsg4(250, "before write_rec FI=%d SessId=%d Strm=%s len=%d\n",
1494 rec.FileIndex, rec.VolSessionId, stream_to_ascii(rec.Stream, rec.FileIndex),
1497 while (!write_record_to_block(block, &rec)) {
1499 * When we get here we have just filled a block
1501 Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec.data_len,
1504 /* Write block to tape */
1505 if (!flush_block(block, 1)) {
1509 /* Every 5000 blocks (approx 322MB) report where we are.
1511 if ((block->BlockNumber % 5000) == 0) {
1513 now -= jcr->run_time;
1515 now = 1; /* prevent divide error */
1517 kbs = (double)dev->VolCatInfo.VolCatBytes / (1000.0 * (double)now);
1518 Pmsg4(-1, "Wrote blk_block=%u, dev_blk_num=%u VolBytes=%s rate=%.1f KB/s\n",
1519 block->BlockNumber, dev->block_num,
1520 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), (float)kbs);
1522 /* Every 15000 blocks (approx 1GB) write an EOF.
1524 if ((block->BlockNumber % 15000) == 0) {
1525 Pmsg0(-1, "Flush block, write EOF\n");
1526 flush_block(block, 0);
1530 /* Get out after writing 10 blocks to the second tape */
1531 if (++BlockNumber > 10 && stop != 0) { /* get out */
1536 Pmsg0(000, _("Not OK\n"));
1539 jcr->JobBytes += rec.data_len; /* increment bytes this job */
1540 Dmsg4(190, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
1541 FI_to_ascii(rec.FileIndex), rec.VolSessionId,
1542 stream_to_ascii(rec.Stream, rec.FileIndex), rec.data_len);
1544 /* Get out after writing 10 blocks to the second tape */
1545 if (BlockNumber > 10 && stop != 0) { /* get out */
1546 Pmsg0(-1, "Done writing ...\n");
1551 Dmsg0(100, "Write_end_session_label()\n");
1552 /* Create Job status for end of session label */
1553 if (!job_canceled(jcr) && ok) {
1554 set_jcr_job_status(jcr, JS_Terminated);
1556 set_jcr_job_status(jcr, JS_ErrorTerminated);
1558 if (!write_session_label(jcr, block, EOS_LABEL)) {
1559 Pmsg1(000, _("Error writting end session label. ERR=%s\n"), strerror_dev(dev));
1562 /* Write out final block of this session */
1563 if (!write_block_to_device(jcr, dev, block)) {
1564 Pmsg0(-1, _("Set ok=FALSE after write_block_to_device.\n"));
1567 Pmsg0(-1, _("Wrote End Of Session label.\n"));
1570 sprintf(buf, "%s/btape.state", working_directory);
1571 fd = open(buf, O_CREAT|O_TRUNC|O_WRONLY, 0640);
1573 write(fd, &btape_state_level, sizeof(btape_state_level));
1574 write(fd, &last_block_num, sizeof(last_block_num));
1575 write(fd, &last_file, sizeof(last_file));
1576 write(fd, last_block->buf, last_block->buf_len);
1578 Pmsg0(-1, "Wrote state file.\n");
1580 Pmsg2(-1, _("Could not create state file: %s ERR=%s\n"), buf,
1584 /* Release the device */
1585 if (!release_device(jcr, dev)) {
1586 Pmsg0(-1, _("Error in release_device\n"));
1592 dump_block(last_block, _("Last block written to tape.\n"));
1595 Pmsg0(-1, _("\n\nDone filling tape. Now beginning re-read of tape ...\n"));
1600 /* Multiple Volume tape */
1604 block = new_block(dev);
1606 dev->capabilities |= CAP_ANONVOLS; /* allow reading any volume */
1607 dev->capabilities &= ~CAP_LABEL; /* don't label anything here */
1612 time(&jcr->run_time); /* start counting time for rates */
1615 /* Close device so user can use autochanger if desired */
1616 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
1619 force_close_dev(dev);
1620 get_cmd(_("Mount first tape. Press enter when ready: "));
1623 set_volume_name("TestVolume1", 1);
1625 create_vol_list(jcr);
1627 dev->state &= ~ST_READ;
1628 if (!acquire_device_for_read(jcr, dev, block)) {
1629 Pmsg1(-1, "%s", dev->errmsg);
1632 /* Read all records and then second tape */
1633 read_records(jcr, dev, record_cb, my_mount_next_read_volume);
1636 dev->min_block_size = min_block_size;
1638 free_memory(rec.data);
1642 * Read two tapes written by the "fill" command and ensure
1643 * that the data is valid. If stop==1 we simulate full read back
1644 * of two tapes. If stop==-1 we simply read the last block and
1645 * verify that it is correct.
1647 static void unfillcmd()
1652 last_block = new_block(dev);
1654 sprintf(buf, "%s/btape.state", working_directory);
1655 fd = open(buf, O_RDONLY);
1657 uint32_t state_level;
1658 read(fd, &state_level, sizeof(btape_state_level));
1659 read(fd, &last_block_num, sizeof(last_block_num));
1660 read(fd, &last_file, sizeof(last_file));
1661 read(fd, last_block->buf, last_block->buf_len);
1663 if (state_level != btape_state_level) {
1664 Pmsg0(-1, "\nThe state file level has changed. You must redo\n"
1665 "the fill command.\n");
1669 Pmsg2(-1, "\nCould not find the state file: %s ERR=%s\n"
1670 "You must redo the fill command.\n", buf, strerror(errno));
1677 static void do_unfill()
1684 block = new_block(dev);
1686 dev->capabilities |= CAP_ANONVOLS; /* allow reading any volume */
1687 dev->capabilities &= ~CAP_LABEL; /* don't label anything here */
1692 time(&jcr->run_time); /* start counting time for rates */
1697 * Note, re-reading last block may have caused us to
1698 * lose track of where we are (block number unknown).
1700 rewind_dev(dev); /* get to a known place on tape */
1701 Pmsg4(-1, _("Reposition from %u:%u to %u:%u\n"), dev->file, dev->block_num,
1702 last_file, last_block_num);
1703 if (!reposition_dev(dev, last_file, last_block_num)) {
1704 Pmsg1(-1, "Reposition error. ERR=%s\n", strerror_dev(dev));
1706 Pmsg1(-1, _("Reading block %u.\n"), last_block_num);
1707 if (!read_block_from_device(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
1708 Pmsg1(-1, _("Error reading block: ERR=%s\n"), strerror_dev(dev));
1713 uint32_t CheckSum, block_len;
1715 p = last_block->buf;
1717 unser_begin(q, BLKHDR2_LENGTH);
1718 unser_uint32(CheckSum);
1719 unser_uint32(block_len);
1720 while (q < (block->buf+block_len)) {
1727 dump_block(last_block, _("Last block written"));
1729 dump_block(block, _("Block read back"));
1730 Pmsg1(-1, "\n\nThe blocks differ at byte %u\n", p - last_block->buf);
1731 Pmsg0(-1, "\n\n!!!! The last block written and the block\n"
1732 "that was read back differ. The test FAILED !!!!\n"
1733 "This must be corrected before you use Bacula\n"
1734 "to write multi-tape Volumes.!!!!\n");
1737 Pmsg0(-1, _("\nThe blocks are identical. Test succeeded.\n\n"));
1739 dump_block(last_block, _("Last block written"));
1740 dump_block(block, _("Block read back"));
1749 * We are called here from "unfill" for each record on the tape.
1751 static int record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
1753 SESSION_LABEL label;
1755 if (stop > 1 && !dumped) { /* on second tape */
1758 dump_block(block, "First block on second tape");
1760 Pmsg4(-1, "Blk: FileIndex=%d: block=%u size=%d vol=%s\n",
1761 rec->FileIndex, block->BlockNumber, block->block_len, dev->VolHdr.VolName);
1762 Pmsg6(-1, " Rec: VId=%d VT=%d FI=%s Strm=%s len=%d state=%x\n",
1763 rec->VolSessionId, rec->VolSessionTime,
1764 FI_to_ascii(rec->FileIndex), stream_to_ascii(rec->Stream, rec->FileIndex),
1765 rec->data_len, rec->state);
1767 if (rec->FileIndex < 0) {
1769 dump_label_record(dev, rec, 1);
1771 switch (rec->FileIndex) {
1773 Pmsg0(-1, "Volume is prelabeled. This tape cannot be scanned.\n");
1776 unser_volume_label(dev, rec);
1777 Pmsg3(-1, "VOL_LABEL: block=%u size=%d vol=%s\n", block->BlockNumber,
1778 block->block_len, dev->VolHdr.VolName);
1782 unser_session_label(&label, rec);
1783 Pmsg1(-1, "SOS_LABEL: JobId=%u\n", label.JobId);
1786 unser_session_label(&label, rec);
1787 Pmsg2(-1, "EOS_LABEL: block=%u JobId=%u\n", block->BlockNumber,
1791 Pmsg0(-1, "EOM_LABEL:\n");
1793 case EOT_LABEL: /* end of all tapes */
1796 if (LastBlock != block->BlockNumber) {
1797 VolBytes += block->block_len;
1799 LastBlock = block->BlockNumber;
1801 now -= jcr->run_time;
1805 kbs = (double)VolBytes / (1000 * now);
1806 Pmsg3(000, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
1807 edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
1809 Pmsg0(000, "End of all tapes.\n");
1817 if (++file_index != rec->FileIndex) {
1818 Pmsg3(000, "Incorrect FileIndex in Block %u. Got %d, expected %d.\n",
1819 block->BlockNumber, rec->FileIndex, file_index);
1822 * Now check that the right data is in the record.
1824 uint64_t *lp = (uint64_t *)rec->data;
1825 uint64_t val = ~file_index;
1826 for (uint32_t i=0; i < (REC_SIZE-sizeof(uint64_t))/sizeof(uint64_t); i++) {
1828 Pmsg2(000, "Record %d contains bad data in Block %u.\n",
1829 file_index, block->BlockNumber);
1834 if (LastBlock != block->BlockNumber) {
1835 VolBytes += block->block_len;
1837 if ((block->BlockNumber != LastBlock) && (block->BlockNumber % 50000) == 0) {
1840 now -= jcr->run_time;
1844 kbs = (double)VolBytes / (1000 * now);
1845 Pmsg3(000, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
1846 edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
1848 LastBlock = block->BlockNumber;
1850 Pmsg1(000, "End of all blocks. Block=%u\n", block->BlockNumber);
1858 * Write current block to tape regardless of whether or
1859 * not it is full. If the tape fills, attempt to
1860 * acquire another tape.
1862 static int flush_block(DEV_BLOCK *block, int dump)
1867 uint32_t this_file, this_block_num;
1870 this_block = new_block(dev);
1873 free_memory(this_block->buf);
1874 memcpy(this_block, block, sizeof(DEV_BLOCK));
1875 this_block->buf = get_memory(block->buf_len);
1876 this_file = dev->file;
1877 this_block_num = dev->block_num;
1878 if (!write_block_to_dev(jcr, dev, block)) {
1879 Pmsg3(000, "Last block at: %u:%u this_dev_block_num=%d\n",
1880 last_file, last_block_num, this_block_num);
1882 Pmsg3(000, "Block not written: FileIndex=%u blk_block=%u Size=%u\n",
1883 (unsigned)file_index, block->BlockNumber, block->block_len);
1885 dump_block(block, "Block not written");
1889 eot_block = block->BlockNumber;
1890 eot_block_len = block->block_len;
1891 eot_FileIndex = file_index;
1894 now -= jcr->run_time;
1896 now = 1; /* don't divide by zero */
1898 kbs = (double)dev->VolCatInfo.VolCatBytes / (1000 * now);
1899 vol_size = dev->VolCatInfo.VolCatBytes;
1900 Pmsg2(000, "End of tape. VolumeCapacity=%s. Write rate = %.1f KB/s\n",
1901 edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), kbs);
1904 stop = -1; /* stop, but do simplified test */
1906 /* Full test in progress */
1907 if (!fixup_device_block_write_error(jcr, dev, block)) {
1908 Pmsg1(000, _("Cannot fixup device error. %s\n"), strerror_dev(dev));
1914 BlockNumber = 0; /* start counting for second tape */
1917 return 1; /* end of tape reached */
1919 /* Save contents after write so that the header is serialized */
1920 memcpy(this_block->buf, block->buf, this_block->buf_len);
1923 * Toggle between two allocated blocks for efficiency.
1924 * Switch blocks so that the block just successfully written is
1925 * always in last_block.
1927 tblock = last_block;
1928 last_block = this_block;
1929 this_block = tblock;
1930 last_file = this_file;
1931 last_block_num = this_block_num;
1939 * First we label the tape, then we fill
1940 * it with data get a new tape and write a few blocks.
1942 static void qfillcmd()
1948 Pmsg0(0, "Test writing blocks of 64512 bytes to tape.\n");
1950 get_cmd("How many blocks do you want to write? (1000): ");
1957 sm_check(__FILE__, __LINE__, False);
1958 block = new_block(dev);
1961 i = block->buf_len - 100;
1963 rec->data = check_pool_memory_size(rec->data, i);
1964 memset(rec->data, i & 0xFF, i);
1967 Pmsg1(0, "Begin writing %d Bacula blocks to tape ...\n", count);
1968 for (i=0; i < count; i++) {
1973 if (!write_record_to_block(block, rec)) {
1974 Pmsg0(0, _("Error writing record to block.\n"));
1977 if (!write_block_to_dev(jcr, dev, block)) {
1978 Pmsg0(0, _("Error writing block to device.\n"));
1984 if (dev_cap(dev, CAP_TWOEOF)) {
1991 sm_check(__FILE__, __LINE__, False);
1994 sm_check(__FILE__, __LINE__, False);
1999 * Fill a tape using raw write() command
2001 static void rawfill_cmd()
2006 uint32_t block_num = 0;
2011 block = new_block(dev);
2012 fd = open("/dev/urandom", O_RDONLY);
2014 read(fd, block->buf, block->buf_len);
2017 uint32_t *p = (uint32_t *)block->buf;
2018 srandom(time(NULL));
2019 for (i=0; i<block->buf_len/sizeof(uint32_t); i++) {
2023 p = (uint32_t *)block->buf;
2024 Pmsg1(0, "Begin writing raw blocks of %u bytes.\n", block->buf_len);
2027 stat = write(dev->fd, block->buf, block->buf_len);
2028 if (stat == (int)block->buf_len) {
2029 if ((block_num++ % 100) == 0) {
2034 for (i=1; i<(block->buf_len-sizeof(uint32_t))/sizeof(uint32_t)-1; i++) {
2043 printf("Write failed at block %u. stat=%d ERR=%s\n", block_num, stat,
2044 strerror(my_errno));
2051 * Fill a tape using raw write() command
2053 static void bfill_cmd()
2056 uint32_t block_num = 0;
2062 block = new_block(dev);
2063 fd = open("/dev/urandom", O_RDONLY);
2065 read(fd, block->buf, block->buf_len);
2068 uint32_t *p = (uint32_t *)block->buf;
2069 srandom(time(NULL));
2070 for (i=0; i<block->buf_len/sizeof(uint32_t); i++) {
2074 p = (uint32_t *)block->buf;
2075 Pmsg1(0, "Begin writing Bacula blocks of %u bytes.\n", block->buf_len);
2078 block->binbuf = block->buf_len;
2079 block->bufp = block->buf + block->binbuf;
2080 if (!write_block_to_dev(jcr, dev, block)) {
2083 if ((block_num++ % 100) == 0) {
2088 for (i=1; i<(block->buf_len/sizeof(uint32_t)-1); i++) {
2094 printf("Write failed at block %u.\n", block_num);
2100 struct cmdstruct { char *key; void (*func)(); char *help; };
2101 static struct cmdstruct commands[] = {
2102 {"autochanger", autochangercmd, "test autochanger"},
2103 {"bsf", bsfcmd, "backspace file"},
2104 {"bsr", bsrcmd, "backspace record"},
2105 {"bfill", bfill_cmd, "fill tape using Bacula writes"},
2106 {"cap", capcmd, "list device capabilities"},
2107 {"clear", clearcmd, "clear tape errors"},
2108 {"eod", eodcmd, "go to end of Bacula data for append"},
2109 {"eom", eomcmd, "go to the physical end of medium"},
2110 {"fill", fillcmd, "fill tape, write onto second volume"},
2111 {"unfill", unfillcmd, "read filled tape"},
2112 {"fsf", fsfcmd, "forward space a file"},
2113 {"fsr", fsrcmd, "forward space a record"},
2114 {"help", helpcmd, "print this command"},
2115 {"label", labelcmd, "write a Bacula label to the tape"},
2116 {"load", loadcmd, "load a tape"},
2117 {"quit", quitcmd, "quit btape"},
2118 {"rawfill", rawfill_cmd, "use write() to fill tape"},
2119 {"readlabel", readlabelcmd, "read and print the Bacula tape label"},
2120 {"rectest", rectestcmd, "test record handling functions"},
2121 {"rewind", rewindcmd, "rewind the tape"},
2122 {"scan", scancmd, "read() tape block by block to EOT and report"},
2123 {"scanblocks", scan_blocks, "Bacula read block by block to EOT and report"},
2124 {"status", statcmd, "print tape status"},
2125 {"test", testcmd, "General test Bacula tape functions"},
2126 {"weof", weofcmd, "write an EOF on the tape"},
2127 {"wr", wrcmd, "write a single Bacula block"},
2128 {"rr", rrcmd, "read a single record"},
2129 {"qfill", qfillcmd, "quick fill command"},
2131 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
2139 while (get_cmd("*")) {
2140 sm_check(__FILE__, __LINE__, False);
2142 parse_args(cmd, &args, &argc, argk, argv, MAX_CMD_ARGS);
2143 for (i=0; i<comsize; i++) /* search for command */
2144 if (argc > 0 && fstrsch(argk[0], commands[i].key)) {
2145 (*commands[i].func)(); /* go execute command */
2150 Pmsg1(0, _("%s is an illegal command\n"), cmd);
2156 static void helpcmd()
2160 printf(_("Interactive commands:\n"));
2161 printf(_(" Command Description\n ======= ===========\n"));
2162 for (i=0; i<comsize; i++)
2163 printf(" %-10s %s\n", commands[i].key, commands[i].help);
2170 "\nVersion: " VERSION " (" BDATE ")\n\n"
2171 "Usage: btape <options> <device_name>\n"
2172 " -c <file> set configuration file to file\n"
2173 " -d <nn> set debug level to nn\n"
2174 " -s turn off signals\n"
2175 " -t open the default tape device\n"
2176 " -? print this message.\n"
2182 * Get next input command from terminal. This
2183 * routine is REALLY primitive, and should be enhanced
2184 * to have correct backspacing, etc.
2187 get_cmd(char *prompt)
2191 fprintf(stdout, prompt);
2193 /* We really should turn off echoing and pretty this
2197 while ((ch = fgetc(stdin)) != EOF) {
2199 strip_trailing_junk(cmd);
2201 } else if (ch == 4 || ch == 0xd3 || ch == 0x8) {
2214 /* Dummies to replace askdir.c */
2215 int dir_get_volume_info(JCR *jcr, enum get_vol_info_rw writing) { return 1;}
2216 int dir_update_volume_info(JCR *jcr, DEVICE *dev, int relabel) { return 1; }
2217 int dir_create_jobmedia_record(JCR *jcr) { return 1; }
2218 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
2219 int dir_send_job_status(JCR *jcr) {return 1;}
2223 int dir_find_next_appendable_volume(JCR *jcr)
2228 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
2230 /* Close device so user can use autochanger if desired */
2231 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2234 force_close_dev(dev);
2235 Pmsg1(-1, "%s", dev->errmsg); /* print reason */
2236 fprintf(stderr, "Mount Volume \"%s\" on device %s and press return when ready: ",
2237 jcr->VolumeName, dev_name(dev));
2242 int dir_ask_sysop_to_create_appendable_volume(JCR *jcr, DEVICE *dev)
2244 /* Close device so user can use autochanger if desired */
2245 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2248 force_close_dev(dev);
2249 fprintf(stderr, "Mount next Volume on device %s and press return when ready: ",
2252 set_volume_name("TestVolume2", 2);
2260 static int my_mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
2264 Pmsg1(000, "End of Volume \"%s\"\n", jcr->VolumeName);
2266 if (LastBlock != block->BlockNumber) {
2267 VolBytes += block->block_len;
2269 LastBlock = block->BlockNumber;
2271 now -= jcr->run_time;
2275 kbs = (double)VolBytes / (1000.0 * (double)now);
2276 Pmsg3(-1, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
2277 edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
2279 if (strcmp(jcr->VolumeName, "TestVolume2") == 0) {
2285 set_volume_name("TestVolume2", 2);
2287 create_vol_list(jcr);
2289 dev->state &= ~ST_READ;
2290 if (!acquire_device_for_read(jcr, dev, block)) {
2291 Pmsg2(0, "Cannot open Dev=%s, Vol=%s\n", dev_name(dev), jcr->VolumeName);
2294 return 1; /* next volume mounted */
2297 static void set_volume_name(char *VolName, int volnum)
2299 VolumeName = VolName;
2301 pm_strcpy(&jcr->VolumeName, VolName);
2302 bstrncpy(dev->VolCatInfo.VolCatName, VolName, sizeof(dev->VolCatInfo.VolCatName));
2306 * Edit codes into ChangerCommand
2308 * %a = archive device name
2309 * %c = changer device name
2310 * %f = Client's name
2318 * omsg = edited output message
2319 * imsg = input string containing edit codes (%x)
2320 * cmd = command string (load, unload, ...)
2323 static char *edit_device_codes(JCR *jcr, char *omsg, char *imsg, char *cmd)
2330 Dmsg1(400, "edit_device_codes: %s\n", imsg);
2331 for (p=imsg; *p; p++) {
2338 str = dev_name(jcr->device->dev);
2341 str = NPRT(jcr->device->changer_name);
2347 sprintf(add, "%d", jcr->VolCatInfo.Slot - 1);
2351 sprintf(add, "%d", jcr->VolCatInfo.Slot);
2354 case 'j': /* Job name */
2358 str = NPRT(jcr->VolumeName);
2361 str = NPRT(jcr->client_name);
2376 Dmsg1(400, "add_str %s\n", str);
2377 pm_strcat(&omsg, (char *)str);
2378 Dmsg1(400, "omsg=%s\n", omsg);