3 * Bacula Director -- Tape labeling commands
5 * Kern Sibbald, April MMIII
11 Copyright (C) 2000-2005 Kern Sibbald
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of
16 the License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public
24 License along with this program; if not, write to the Free
25 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
33 /* Slot list definition */
34 typedef struct s_vol_list {
35 struct s_vol_list *next;
41 /* Forward referenced functions */
42 static int do_label(UAContext *ua, const char *cmd, int relabel);
43 static void label_from_barcodes(UAContext *ua);
44 static bool send_label_request(UAContext *ua, MEDIA_DBR *mr, MEDIA_DBR *omr,
45 POOL_DBR *pr, int relabel, bool media_record_exits);
46 static vol_list_t *get_vol_list_from_SD(UAContext *ua, bool scan);
47 static void free_vol_list(vol_list_t *vol_list);
48 static bool is_cleaning_tape(UAContext *ua, MEDIA_DBR *mr, POOL_DBR *pr);
49 static BSOCK *open_sd_bsock(UAContext *ua);
50 static void close_sd_bsock(UAContext *ua);
51 static char *get_volume_name_from_SD(UAContext *ua, int Slot);
57 * label storage=xxx volume=vvv
59 int label_cmd(UAContext *ua, const char *cmd)
61 return do_label(ua, cmd, 0); /* standard label */
64 int relabel_cmd(UAContext *ua, const char *cmd)
66 return do_label(ua, cmd, 1); /* relabel tape */
69 static int const max_slots = 5000;
71 static bool get_user_slot_list(UAContext *ua, char *slot_list, int num_slots)
76 for (int i=0; i<num_slots; i++) {
79 i = find_arg_with_value(ua, "slots");
81 /* scan slot list in ua->argv[i] */
85 strip_trailing_junk(ua->argv[i]);
86 for (p=ua->argv[i]; p && *p; p=e) {
93 h = strchr(p, '-'); /* range? */
95 msg = _("Negative numbers not permitted\n");
100 if (!is_an_integer(h)) {
101 msg = _("Range end is not integer.\n");
105 if (!is_an_integer(p)) {
106 msg = _("Range start is not an integer.\n");
112 msg = _("Range end not bigger than start.\n");
117 if (!is_an_integer(p)) {
118 msg = _("Input value is not an integer.\n");
123 if (beg <= 0 || end <= 0) {
124 msg = _("Values must be be greater than zero.\n");
127 if (end >= num_slots) {
128 msg = _("Slot too large.\n");
131 for (i=beg; i<=end; i++) {
132 slot_list[i] = 1; /* Turn on specified range */
136 /* Turn everything on */
137 for (i=0; i<num_slots; i++) {
142 printf("Slots turned on:\n");
143 for (i=1; i<num_slots; i++) {
156 * Update Slots corresponding to Volumes in autochanger
158 int update_slots(UAContext *ua)
161 vol_list_t *vl, *vol_list = NULL;
169 store = get_storage_resource(ua, 1);
173 set_storage(ua->jcr, store);
175 scan = find_arg(ua, _("scan")) >= 0;
177 slot_list = (char *)malloc(max_slots);
178 if (!get_user_slot_list(ua, slot_list, max_slots)) {
183 vol_list = get_vol_list_from_SD(ua, scan);
186 bsendmsg(ua, _("No Volumes found to label, or no barcodes.\n"));
190 /* Walk through the list updating the media records */
191 for (vl=vol_list; vl; vl=vl->next) {
192 if (vl->Slot >= max_slots) {
193 bsendmsg(ua, _("Slot %d larger than max %d ignored.\n"));
196 /* Check if user wants us to look at this slot */
197 if (!slot_list[vl->Slot]) {
198 Dmsg1(100, "Skipping slot=%d\n", vl->Slot);
201 /* If scanning, we read the label rather than the barcode */
207 vl->VolName = get_volume_name_from_SD(ua, vl->Slot);
208 Dmsg2(100, "Got Vol=%s from SD for Slot=%d\n", vl->VolName, vl->Slot);
210 slot_list[vl->Slot] = 0; /* clear Slot */
212 Dmsg1(100, "No VolName for Slot=%d setting InChanger to zero.\n", vl->Slot);
213 memset(&mr, 0, sizeof(mr));
216 mr.StorageId = store->StorageId;
217 /* Set InChanger to zero for this Slot */
219 db_make_inchanger_unique(ua->jcr, ua->db, &mr);
221 bsendmsg(ua, _("No VolName for Slot=%d set InChanger to zero.\n"), vl->Slot);
224 memset(&mr, 0, sizeof(mr));
225 bstrncpy(mr.VolumeName, vl->VolName, sizeof(mr.VolumeName));
227 if (db_get_media_record(ua->jcr, ua->db, &mr)) {
228 if (mr.Slot != vl->Slot || !mr.InChanger || mr.StorageId != store->StorageId) {
231 mr.StorageId = store->StorageId;
232 if (!db_update_media_record(ua->jcr, ua->db, &mr)) {
233 bsendmsg(ua, "%s", db_strerror(ua->db));
236 "Catalog record for Volume \"%s\" updated to reference slot %d.\n"),
237 mr.VolumeName, mr.Slot);
240 bsendmsg(ua, _("Catalog record for Volume \"%s\" is up to date.\n"),
246 bsendmsg(ua, _("Record for Volume \"%s\" not found in catalog.\n"),
251 memset(&mr, 0, sizeof(mr));
253 mr.StorageId = store->StorageId;
255 for (int i=1; i<max_slots; i++) {
258 /* Set InChanger to zero for this Slot */
259 db_make_inchanger_unique(ua->jcr, ua->db, &mr);
266 free_vol_list(vol_list);
275 * Common routine for both label and relabel
277 static int do_label(UAContext *ua, const char *cmd, int relabel)
281 char dev_name[MAX_NAME_LENGTH];
284 bool print_reminder = true;
287 bool media_record_exists = false;
288 static const char *barcode_keyword[] = {
294 memset(&pr, 0, sizeof(pr));
298 store = get_storage_resource(ua, 1);
302 set_storage(ua->jcr, store);
304 if (!relabel && find_arg_keyword(ua, barcode_keyword) >= 0) {
305 label_from_barcodes(ua);
309 /* If relabel get name of Volume to relabel */
311 /* Check for oldvolume=name */
312 i = find_arg_with_value(ua, "oldvolume");
314 memset(&omr, 0, sizeof(omr));
315 bstrncpy(omr.VolumeName, ua->argv[i], sizeof(omr.VolumeName));
316 if (db_get_media_record(ua->jcr, ua->db, &omr)) {
319 bsendmsg(ua, "%s", db_strerror(ua->db));
321 /* No keyword or Vol not found, ask user to select */
322 if (!select_media_dbr(ua, &omr)) {
326 /* Require Volume to be Purged or Recycled */
328 if (strcmp(omr.VolStatus, "Purged") != 0 && strcmp(omr.VolStatus, "Recycle") != 0) {
329 bsendmsg(ua, _("Volume \"%s\" has VolStatus %s. It must be Purged or Recycled before relabeling.\n"),
330 omr.VolumeName, omr.VolStatus);
335 /* Check for volume=NewVolume */
336 i = find_arg_with_value(ua, "volume");
338 pm_strcpy(ua->cmd, ua->argv[i]);
342 /* Get a new Volume name */
344 media_record_exists = false;
345 if (!get_cmd(ua, _("Enter new Volume name: "))) {
349 if (!is_volume_name_legal(ua, ua->cmd)) {
353 memset(&mr, 0, sizeof(mr));
354 bstrncpy(mr.VolumeName, ua->cmd, sizeof(mr.VolumeName));
355 /* If VolBytes are zero the Volume is not labeled */
356 if (db_get_media_record(ua->jcr, ua->db, &mr)) {
357 if (mr.VolBytes != 0) {
358 bsendmsg(ua, _("Media record for new Volume \"%s\" already exists.\n"),
362 media_record_exists = true;
367 /* If autochanger, request slot */
368 if (store->autochanger) {
369 i = find_arg_with_value(ua, "slot");
371 mr.Slot = atoi(ua->argv[i]);
372 } else if (!get_pint(ua, _("Enter slot (0 or Enter for none): "))) {
375 mr.Slot = ua->pint32_val;
377 mr.InChanger = 1; /* assumed if we are labeling it */
380 bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
382 /* Must select Pool if not already done */
383 if (pr.PoolId == 0) {
384 memset(&pr, 0, sizeof(pr));
385 if (!select_pool_dbr(ua, &pr)) {
391 ok = send_label_request(ua, &mr, &omr, &pr, relabel, media_record_exists);
394 sd = ua->jcr->store_bsock;
396 /* Delete the old media record */
397 if (!db_delete_media_record(ua->jcr, ua->db, &omr)) {
398 bsendmsg(ua, _("Delete of Volume \"%s\" failed. ERR=%s"),
399 omr.VolumeName, db_strerror(ua->db));
401 bsendmsg(ua, _("Old volume \"%s\" deleted from catalog.\n"),
403 /* Update the number of Volumes in the pool */
405 if (!db_update_pool_record(ua->jcr, ua->db, &pr)) {
406 bsendmsg(ua, "%s", db_strerror(ua->db));
411 bstrncpy(dev_name, store->dev_name(), sizeof(dev_name));
412 bsendmsg(ua, _("Requesting to mount %s ...\n"), dev_name);
413 bash_spaces(dev_name);
414 bnet_fsend(sd, "mount %s", dev_name);
415 unbash_spaces(dev_name);
416 while (bnet_recv(sd) >= 0) {
417 bsendmsg(ua, "%s", sd->msg);
419 * 3001 OK mount. Device=xxx or
420 * 3001 Mounted Volume vvvv
421 * 3906 is cannot mount non-tape
422 * So for those, no need to print a reminder
424 if (strncmp(sd->msg, "3001 ", 5) == 0 ||
425 strncmp(sd->msg, "3906 ", 5) == 0) {
426 print_reminder = false;
431 if (print_reminder) {
432 bsendmsg(ua, _("Do not forget to mount the drive!!!\n"));
440 * Request SD to send us the slot:barcodes, then wiffle
441 * through them all labeling them.
443 static void label_from_barcodes(UAContext *ua)
445 STORE *store = ua->jcr->store;
448 vol_list_t *vl, *vol_list = NULL;
449 bool media_record_exists;
452 slot_list = (char *)malloc(max_slots);
453 if (!get_user_slot_list(ua, slot_list, max_slots)) {
457 vol_list = get_vol_list_from_SD(ua, false /*no scan*/);
460 bsendmsg(ua, _("No Volumes found to label, or no barcodes.\n"));
464 /* Display list of Volumes and ask if he really wants to proceed */
465 bsendmsg(ua, _("The following Volumes will be labeled:\n"
467 "==============\n"));
468 for (vl=vol_list; vl; vl=vl->next) {
469 if (!vl->VolName || !slot_list[vl->Slot]) {
472 bsendmsg(ua, "%4d %s\n", vl->Slot, vl->VolName);
474 if (!get_cmd(ua, _("Do you want to continue? (y/n): ")) ||
475 (ua->cmd[0] != 'y' && ua->cmd[0] != 'Y')) {
479 memset(&pr, 0, sizeof(pr));
480 if (!select_pool_dbr(ua, &pr)) {
483 memset(&omr, 0, sizeof(omr));
485 /* Fire off the label requests */
486 for (vl=vol_list; vl; vl=vl->next) {
487 if (!vl->VolName || !slot_list[vl->Slot]) {
490 memset(&mr, 0, sizeof(mr));
491 bstrncpy(mr.VolumeName, vl->VolName, sizeof(mr.VolumeName));
492 media_record_exists = false;
493 if (db_get_media_record(ua->jcr, ua->db, &mr)) {
494 if (mr.VolBytes != 0) {
495 bsendmsg(ua, _("Media record for Slot %d Volume \"%s\" already exists.\n"),
496 vl->Slot, mr.VolumeName);
499 mr.StorageId = store->StorageId;
500 if (!db_update_media_record(ua->jcr, ua->db, &mr)) {
501 bsendmsg(ua, "Error setting InChanger: ERR=%s", db_strerror(ua->db));
505 media_record_exists = true;
509 * Deal with creating cleaning tape here. Normal tapes created in
510 * send_label_request() below
512 if (is_cleaning_tape(ua, &mr, &pr)) {
513 if (media_record_exists) { /* we update it */
515 if (!db_update_media_record(ua->jcr, ua->db, &mr)) {
516 bsendmsg(ua, "%s", db_strerror(ua->db));
518 } else { /* create the media record */
519 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
520 if (db_create_media_record(ua->jcr, ua->db, &mr)) {
521 bsendmsg(ua, _("Catalog record for cleaning tape \"%s\" successfully created.\n"),
523 pr.NumVols++; /* this is a bit suspect */
524 if (!db_update_pool_record(ua->jcr, ua->db, &pr)) {
525 bsendmsg(ua, "%s", db_strerror(ua->db));
528 bsendmsg(ua, "Catalog error on cleaning tape: %s", db_strerror(ua->db));
531 continue; /* done, go handle next volume */
533 bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
536 send_label_request(ua, &mr, &omr, &pr, 0, media_record_exists);
542 free_vol_list(vol_list);
549 * Check if the Volume name has legal characters
550 * If ua is non-NULL send the message
552 bool is_volume_name_legal(UAContext *ua, const char *name)
556 const char *accept = ":.-_";
558 /* Restrict the characters permitted in the Volume name */
559 for (p=name; *p; p++) {
560 if (B_ISALPHA(*p) || B_ISDIGIT(*p) || strchr(accept, (int)(*p))) {
564 bsendmsg(ua, _("Illegal character \"%c\" in a volume name.\n"), *p);
569 if (len >= MAX_NAME_LENGTH) {
571 bsendmsg(ua, _("Volume name too long.\n"));
577 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
585 * NOTE! This routine opens the SD socket but leaves it open
587 static bool send_label_request(UAContext *ua, MEDIA_DBR *mr, MEDIA_DBR *omr,
588 POOL_DBR *pr, int relabel, bool media_record_exists)
591 char dev_name[MAX_NAME_LENGTH];
594 if (!(sd=open_sd_bsock(ua))) {
597 bstrncpy(dev_name, ua->jcr->store->dev_name(), sizeof(dev_name));
598 bash_spaces(dev_name);
599 bash_spaces(mr->VolumeName);
600 bash_spaces(mr->MediaType);
601 bash_spaces(pr->Name);
603 bash_spaces(omr->VolumeName);
604 bnet_fsend(sd, "relabel %s OldName=%s NewName=%s PoolName=%s MediaType=%s Slot=%d",
605 dev_name, omr->VolumeName, mr->VolumeName, pr->Name, mr->MediaType, mr->Slot);
606 bsendmsg(ua, _("Sending relabel command from \"%s\" to \"%s\" ...\n"),
607 omr->VolumeName, mr->VolumeName);
609 bnet_fsend(sd, "label %s VolumeName=%s PoolName=%s MediaType=%s Slot=%d",
610 dev_name, mr->VolumeName, pr->Name, mr->MediaType, mr->Slot);
611 bsendmsg(ua, _("Sending label command for Volume \"%s\" Slot %d ...\n"),
612 mr->VolumeName, mr->Slot);
613 Dmsg5(200, "label %s VolumeName=%s PoolName=%s MediaType=%s Slot=%d\n",
614 dev_name, mr->VolumeName, pr->Name, mr->MediaType, mr->Slot);
617 while (bnet_recv(sd) >= 0) {
618 bsendmsg(ua, "%s", sd->msg);
619 if (strncmp(sd->msg, "3000 OK label.", 14) == 0) {
623 unbash_spaces(mr->VolumeName);
624 unbash_spaces(mr->MediaType);
625 unbash_spaces(pr->Name);
626 mr->LabelDate = time(NULL);
627 mr->set_label_date = true;
629 if (media_record_exists) { /* we update it */
632 if (!db_update_media_record(ua->jcr, ua->db, mr)) {
633 bsendmsg(ua, "%s", db_strerror(ua->db));
636 } else { /* create the media record */
637 set_pool_dbr_defaults_in_media_dbr(mr, pr);
638 mr->VolBytes = 1; /* flag indicating Volume labeled */
640 if (db_create_media_record(ua->jcr, ua->db, mr)) {
641 bsendmsg(ua, _("Catalog record for Volume \"%s\", Slot %d successfully created.\n"),
642 mr->VolumeName, mr->Slot);
643 /* Update number of volumes in pool */
645 if (!db_update_pool_record(ua->jcr, ua->db, pr)) {
646 bsendmsg(ua, "%s", db_strerror(ua->db));
649 bsendmsg(ua, "%s", db_strerror(ua->db));
654 bsendmsg(ua, _("Label command failed for Volume %s.\n"), mr->VolumeName);
659 static BSOCK *open_sd_bsock(UAContext *ua)
661 STORE *store = ua->jcr->store;
663 if (!ua->jcr->store_bsock) {
664 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d ...\n"),
665 store->hdr.name, store->address, store->SDport);
666 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
667 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
671 return ua->jcr->store_bsock;
674 static void close_sd_bsock(UAContext *ua)
676 if (ua->jcr->store_bsock) {
677 bnet_sig(ua->jcr->store_bsock, BNET_TERMINATE);
678 bnet_close(ua->jcr->store_bsock);
679 ua->jcr->store_bsock = NULL;
683 static char *get_volume_name_from_SD(UAContext *ua, int Slot)
685 STORE *store = ua->jcr->store;
687 char dev_name[MAX_NAME_LENGTH];
688 char *VolName = NULL;
691 if (!(sd=open_sd_bsock(ua))) {
692 bsendmsg(ua, _("Could not open SD socket.\n"));
695 bstrncpy(dev_name, store->dev_name(), sizeof(dev_name));
696 bash_spaces(dev_name);
697 /* Ask for autochanger list of volumes */
698 bnet_fsend(sd, _("readlabel %s Slot=%d\n"), dev_name, Slot);
699 Dmsg1(100, "Sent: %s", sd->msg);
701 /* Get Volume name in this Slot */
702 while (bnet_recv(sd) >= 0) {
703 bsendmsg(ua, "%s", sd->msg);
704 Dmsg1(100, "Got: %s", sd->msg);
705 if (strncmp(sd->msg, "3001 Volume=", 12) == 0) {
706 VolName = (char *)malloc(sd->msglen);
707 if (sscanf(sd->msg, "3001 Volume=%s Slot=%d", VolName, &rtn_slot) == 2) {
715 Dmsg1(200, "get_vol_name=%s\n", NPRT(VolName));
720 * We get the slot list from the Storage daemon.
721 * If scan is set, we return all slots found,
722 * otherwise, we return only slots with valid barcodes (Volume names)
724 static vol_list_t *get_vol_list_from_SD(UAContext *ua, bool scan)
726 STORE *store = ua->jcr->store;
727 char dev_name[MAX_NAME_LENGTH];
730 vol_list_t *vol_list = NULL;
733 if (!(sd=open_sd_bsock(ua))) {
737 bstrncpy(dev_name, store->dev_name(), sizeof(dev_name));
738 bash_spaces(dev_name);
739 /* Ask for autochanger list of volumes */
740 bnet_fsend(sd, _("autochanger list %s \n"), dev_name);
742 /* Read and organize list of Volumes */
743 while (bnet_recv(sd) >= 0) {
746 strip_trailing_junk(sd->msg);
748 /* Check for returned SD messages */
749 if (sd->msg[0] == '3' && B_ISDIGIT(sd->msg[1]) &&
750 B_ISDIGIT(sd->msg[2]) && B_ISDIGIT(sd->msg[3]) &&
752 bsendmsg(ua, "%s\n", sd->msg); /* pass them on to user */
756 /* Validate Slot: if scanning, otherwise Slot:Barcode */
757 p = strchr(sd->msg, ':');
759 /* Scanning -- require only valid slot */
760 Slot = atoi(sd->msg);
764 bsendmsg(ua, _("Invalid Slot number: %s\n"), sd->msg);
769 if (p && strlen(p) > 1) {
771 if (!is_an_integer(sd->msg) || (Slot=atoi(sd->msg)) <= 0) {
774 bsendmsg(ua, _("Invalid Slot number: %s\n"), sd->msg);
780 if (!is_volume_name_legal(ua, p)) {
783 bsendmsg(ua, _("Invalid Volume name: %s\n"), sd->msg);
788 /* Add Slot and VolumeName to list */
789 vl = (vol_list_t *)malloc(sizeof(vol_list_t));
793 p++; /* skip separator */
795 vl->VolName = bstrdup(p);
799 Dmsg2(100, "Add slot=%d Vol=%s to SD list.\n", vl->Slot, NPRT(vl->VolName));
804 /* Add new entry to end of list */
805 for (vol_list_t *tvl=vol_list; tvl; tvl=tvl->next) {
818 static void free_vol_list(vol_list_t *vol_list)
823 for (vl=vol_list; vl; ) {
836 * Check if this is a cleaning tape by comparing the Volume name
837 * with the Cleaning Prefix. If they match, this is a cleaning
840 static bool is_cleaning_tape(UAContext *ua, MEDIA_DBR *mr, POOL_DBR *pr)
842 /* Find Pool resource */
843 ua->jcr->pool = (POOL *)GetResWithName(R_POOL, pr->Name);
844 if (!ua->jcr->pool) {
845 bsendmsg(ua, _("Pool \"%s\" resource not found!\n"), pr->Name);
848 if (ua->jcr->pool->cleaning_prefix == NULL) {
851 Dmsg4(100, "CLNprefix=%s: Vol=%s: len=%d strncmp=%d\n",
852 ua->jcr->pool->cleaning_prefix, mr->VolumeName,
853 strlen(ua->jcr->pool->cleaning_prefix),
854 strncmp(mr->VolumeName, ua->jcr->pool->cleaning_prefix,
855 strlen(ua->jcr->pool->cleaning_prefix)));
856 return strncmp(mr->VolumeName, ua->jcr->pool->cleaning_prefix,
857 strlen(ua->jcr->pool->cleaning_prefix)) == 0;