3 * Bacula Director -- Tape labeling commands
5 * Kern Sibbald, April MMIII
11 Copyright (C) 2000-2003 Kern Sibbald and John Walker
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, char *cmd, int relabel);
43 static void label_from_barcodes(UAContext *ua);
44 static int 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_slot_list_from_SD(UAContext *ua);
47 static int is_cleaning_tape(UAContext *ua, MEDIA_DBR *mr, POOL_DBR *pr);
53 * label storage=xxx volume=vvv
55 int label_cmd(UAContext *ua, char *cmd)
57 return do_label(ua, cmd, 0); /* standard label */
60 int relabel_cmd(UAContext *ua, char *cmd)
62 return do_label(ua, cmd, 1); /* relabel tape */
67 * Update Slots corresponding to Volumes in autochanger
69 int update_slots(UAContext *ua)
72 vol_list_t *vl, *vol_list = NULL;
78 store = get_storage_resource(ua, 1);
82 ua->jcr->store = store;
84 vol_list = get_slot_list_from_SD(ua);
88 bsendmsg(ua, _("No Volumes found to label, or no barcodes.\n"));
92 /* Walk through the list updating the media records */
93 for (vl=vol_list; vl; vl=vl->next) {
95 memset(&mr, 0, sizeof(mr));
96 bstrncpy(mr.VolumeName, vl->VolName, sizeof(mr.VolumeName));
98 if (db_get_media_record(ua->jcr, ua->db, &mr)) {
99 if (mr.Slot != vl->Slot) {
101 if (!db_update_media_record(ua->jcr, ua->db, &mr)) {
102 bsendmsg(ua, _("%s\n"), db_strerror(ua->db));
105 "Catalog record for Volume \"%s\" updated to reference slot %d.\n"),
106 mr.VolumeName, mr.Slot);
109 bsendmsg(ua, _("Catalog record for Volume \"%s\" is up to date.\n"),
115 bsendmsg(ua, _("Record for Volume \"%s\" not found in catalog.\n"),
124 for (vl=vol_list; vl; ) {
132 if (ua->jcr->store_bsock) {
133 bnet_sig(ua->jcr->store_bsock, BNET_TERMINATE);
134 bnet_close(ua->jcr->store_bsock);
135 ua->jcr->store_bsock = NULL;
141 * Common routine for both label and relabel
143 static int do_label(UAContext *ua, char *cmd, int relabel)
147 sd = ua->jcr->store_bsock;
148 char dev_name[MAX_NAME_LENGTH];
154 bool media_record_exists = false;
155 static char *barcode_keyword[] = {
161 memset(&pr, 0, sizeof(pr));
165 store = get_storage_resource(ua, 1);
169 ua->jcr->store = store;
171 if (!relabel && find_arg_keyword(ua, barcode_keyword) >= 0) {
172 label_from_barcodes(ua);
176 /* If relabel get name of Volume to relabel */
178 /* Check for oldvolume=name */
179 i = find_arg_with_value(ua, "oldvolume");
181 memset(&omr, 0, sizeof(omr));
182 bstrncpy(omr.VolumeName, ua->argv[i], sizeof(omr.VolumeName));
183 if (db_get_media_record(ua->jcr, ua->db, &omr)) {
186 bsendmsg(ua, "%s", db_strerror(ua->db));
188 /* No keyword or Vol not found, ask user to select */
189 if (!select_media_dbr(ua, &omr)) {
193 /* Require Volume to be Purged */
195 if (strcmp(omr.VolStatus, "Purged") != 0) {
196 bsendmsg(ua, _("Volume \"%s\" has VolStatus %s. It must be purged before relabeling.\n"),
197 omr.VolumeName, omr.VolStatus);
202 /* Check for volume=NewVolume */
203 i = find_arg_with_value(ua, "volume");
205 pm_strcpy(&ua->cmd, ua->argv[i]);
209 /* Get a new Volume name */
211 media_record_exists = false;
212 if (!get_cmd(ua, _("Enter new Volume name: "))) {
216 if (!is_volume_name_legal(ua, ua->cmd)) {
220 memset(&mr, 0, sizeof(mr));
221 bstrncpy(mr.VolumeName, ua->cmd, sizeof(mr.VolumeName));
222 /* If VolBytes are zero the Volume is not labeled */
223 if (db_get_media_record(ua->jcr, ua->db, &mr)) {
224 if (mr.VolBytes != 0) {
225 bsendmsg(ua, _("Media record for new Volume \"%s\" already exists.\n"),
229 media_record_exists = true;
234 /* If autochanger, request slot */
235 if (store->autochanger) {
236 i = find_arg_with_value(ua, "slot");
238 mr.Slot = atoi(ua->argv[i]);
239 } else if (!get_pint(ua, _("Enter slot (0 for none): "))) {
242 mr.Slot = ua->pint32_val;
246 bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
248 /* Must select Pool if not already done */
249 if (pr.PoolId == 0) {
250 memset(&pr, 0, sizeof(pr));
251 if (!select_pool_dbr(ua, &pr)) {
256 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d ...\n"),
257 store->hdr.name, store->address, store->SDport);
258 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
259 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
262 sd = ua->jcr->store_bsock;
264 ok = send_label_request(ua, &mr, &omr, &pr, relabel, media_record_exists);
268 if (!db_delete_media_record(ua->jcr, ua->db, &omr)) {
269 bsendmsg(ua, _("Delete of Volume \"%s\" failed. ERR=%s"),
270 omr.VolumeName, db_strerror(ua->db));
272 bsendmsg(ua, _("Old volume \"%s\" deleted from catalog.\n"),
277 bstrncpy(dev_name, store->dev_name, sizeof(dev_name));
278 bsendmsg(ua, _("Requesting mount %s ...\n"), dev_name);
279 bash_spaces(dev_name);
280 bnet_fsend(sd, "mount %s", dev_name);
281 unbash_spaces(dev_name);
282 while (bnet_recv(sd) >= 0) {
283 bsendmsg(ua, "%s", sd->msg);
285 * 3001 OK mount. Device=xxx or
286 * 3001 Mounted Volume vvvv
288 mounted = strncmp(sd->msg, "3001 ", 5) == 0;
293 bsendmsg(ua, _("Do not forget to mount the drive!!!\n"));
295 bnet_sig(sd, BNET_TERMINATE);
297 ua->jcr->store_bsock = NULL;
303 * Request SD to send us the slot:barcodes, then wiffle
304 * through them all labeling them.
306 static void label_from_barcodes(UAContext *ua)
308 STORE *store = ua->jcr->store;
311 vol_list_t *vl, *vol_list = NULL;
312 bool media_record_exists;
314 vol_list = get_slot_list_from_SD(ua);
317 bsendmsg(ua, _("No Volumes found to label, or no barcodes.\n"));
321 /* Display list of Volumes and ask if he really wants to proceed */
322 bsendmsg(ua, _("The following Volumes will be labeled:\n"
324 "==============\n"));
325 for (vl=vol_list; vl; vl=vl->next) {
326 bsendmsg(ua, "%4d %s\n", vl->Slot, vl->VolName);
328 if (!get_cmd(ua, _("Do you want to continue? (y/n): ")) ||
329 (ua->cmd[0] != 'y' && ua->cmd[0] != 'Y')) {
333 memset(&pr, 0, sizeof(pr));
334 if (!select_pool_dbr(ua, &pr)) {
337 memset(&omr, 0, sizeof(omr));
339 /* Fire off the label requests */
340 for (vl=vol_list; vl; vl=vl->next) {
342 memset(&mr, 0, sizeof(mr));
343 bstrncpy(mr.VolumeName, vl->VolName, sizeof(mr.VolumeName));
344 media_record_exists = false;
345 if (db_get_media_record(ua->jcr, ua->db, &mr)) {
346 if (mr.VolBytes != 0) {
347 bsendmsg(ua, _("Media record for Slot %d Volume \"%s\" already exists.\n"),
348 vl->Slot, mr.VolumeName);
351 media_record_exists = true;
354 * Deal with creating cleaning tape here. Normal tapes created in
355 * send_label_request() below
357 if (is_cleaning_tape(ua, &mr, &pr)) {
358 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
359 if (db_create_media_record(ua->jcr, ua->db, &mr)) {
360 bsendmsg(ua, _("Catalog record for cleaning tape \"%s\" successfully created.\n"),
363 bsendmsg(ua, "Catalog error on cleaning tape: %s", db_strerror(ua->db));
367 bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
368 if (ua->jcr->store_bsock) {
369 bnet_sig(ua->jcr->store_bsock, BNET_TERMINATE);
370 bnet_close(ua->jcr->store_bsock);
371 ua->jcr->store_bsock = NULL;
373 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d ...\n"),
374 store->hdr.name, store->address, store->SDport);
375 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
376 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
381 send_label_request(ua, &mr, &omr, &pr, 0, media_record_exists);
387 for (vl=vol_list; vl; ) {
395 if (ua->jcr->store_bsock) {
396 bnet_sig(ua->jcr->store_bsock, BNET_TERMINATE);
397 bnet_close(ua->jcr->store_bsock);
398 ua->jcr->store_bsock = NULL;
405 * Check if the Volume name has legal characters
406 * If ua is non-NULL send the message
408 int is_volume_name_legal(UAContext *ua, char *name)
412 char *accept = ":.-_";
414 /* Restrict the characters permitted in the Volume name */
415 for (p=name; *p; p++) {
416 if (B_ISALPHA(*p) || B_ISDIGIT(*p) || strchr(accept, (int)(*p))) {
420 bsendmsg(ua, _("Illegal character \"%c\" in a volume name.\n"), *p);
425 if (len >= MAX_NAME_LENGTH) {
427 bsendmsg(ua, _("Volume name too long.\n"));
433 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
440 static int send_label_request(UAContext *ua, MEDIA_DBR *mr, MEDIA_DBR *omr,
441 POOL_DBR *pr, int relabel, bool media_record_exists)
444 char dev_name[MAX_NAME_LENGTH];
447 sd = ua->jcr->store_bsock;
448 bstrncpy(dev_name, ua->jcr->store->dev_name, sizeof(dev_name));
449 bash_spaces(dev_name);
450 bash_spaces(mr->VolumeName);
451 bash_spaces(mr->MediaType);
452 bash_spaces(pr->Name);
454 bash_spaces(omr->VolumeName);
455 bnet_fsend(sd, "relabel %s OldName=%s NewName=%s PoolName=%s MediaType=%s Slot=%d",
456 dev_name, omr->VolumeName, mr->VolumeName, pr->Name, mr->MediaType, mr->Slot);
457 bsendmsg(ua, _("Sending relabel command from \"%s\" to \"%s\" ...\n"),
458 omr->VolumeName, mr->VolumeName);
460 bnet_fsend(sd, "label %s VolumeName=%s PoolName=%s MediaType=%s Slot=%d",
461 dev_name, mr->VolumeName, pr->Name, mr->MediaType, mr->Slot);
462 bsendmsg(ua, _("Sending label command for Volume \"%s\" Slot %d ...\n"),
463 mr->VolumeName, mr->Slot);
464 Dmsg5(200, "label %s VolumeName=%s PoolName=%s MediaType=%s Slot=%d\n",
465 dev_name, mr->VolumeName, pr->Name, mr->MediaType, mr->Slot);
468 while (bnet_recv(sd) >= 0) {
469 bsendmsg(ua, "%s", sd->msg);
470 if (strncmp(sd->msg, "3000 OK label.", 14) == 0) {
474 unbash_spaces(mr->VolumeName);
475 unbash_spaces(mr->MediaType);
476 unbash_spaces(pr->Name);
477 mr->LabelDate = time(NULL);
479 if (media_record_exists) { /* we update it */
481 if (!db_update_media_record(ua->jcr, ua->db, mr)) {
482 bsendmsg(ua, "%s", db_strerror(ua->db));
485 } else { /* create the media record */
486 set_pool_dbr_defaults_in_media_dbr(mr, pr);
487 mr->VolBytes = 1; /* flag indicating Volume labeled */
488 if (db_create_media_record(ua->jcr, ua->db, mr)) {
489 bsendmsg(ua, _("Catalog record for Volume \"%s\", Slot %d successfully created.\n"),
490 mr->VolumeName, mr->Slot);
492 bsendmsg(ua, "%s", db_strerror(ua->db));
497 bsendmsg(ua, _("Label command failed.\n"));
502 static vol_list_t *get_slot_list_from_SD(UAContext *ua)
504 STORE *store = ua->jcr->store;
505 char dev_name[MAX_NAME_LENGTH];
508 vol_list_t *vol_list = NULL;
511 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d ...\n"),
512 store->hdr.name, store->address, store->SDport);
513 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
514 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
517 sd = ua->jcr->store_bsock;
519 bstrncpy(dev_name, store->dev_name, sizeof(dev_name));
520 bash_spaces(dev_name);
521 /* Ask for autochanger list of volumes */
522 bnet_fsend(sd, _("autochanger list %s \n"), dev_name);
524 /* Read and organize list of Volumes */
525 while (bnet_recv(sd) >= 0) {
528 strip_trailing_junk(sd->msg);
530 /* Check for returned SD messages */
531 if (sd->msg[0] == '3' && B_ISDIGIT(sd->msg[1]) &&
532 B_ISDIGIT(sd->msg[2]) && B_ISDIGIT(sd->msg[3]) &&
534 bsendmsg(ua, "%s\n", sd->msg); /* pass them on to user */
538 /* Validate Slot:Barcode */
539 p = strchr(sd->msg, ':');
540 if (p && strlen(p) > 1) {
542 if (!is_an_integer(sd->msg)) {
548 Slot = atoi(sd->msg);
549 if (Slot <= 0 || !is_volume_name_legal(ua, p)) {
553 /* Add Slot and VolumeName to list */
554 vl = (vol_list_t *)malloc(sizeof(vol_list_t));
556 vl->VolName = bstrdup(p);
561 /* Add new entry to end of list */
562 for (vol_list_t *tvl=vol_list; tvl; tvl=tvl->next) {
575 * Check if this is a cleaning tape by comparing the Volume name
576 * with the Cleaning Prefix. If they match, this is a cleaning
579 static int is_cleaning_tape(UAContext *ua, MEDIA_DBR *mr, POOL_DBR *pr)
581 if (!ua->jcr->pool) {
582 /* Find Pool resource */
583 ua->jcr->pool = (POOL *)GetResWithName(R_POOL, pr->Name);
584 if (!ua->jcr->pool) {
585 bsendmsg(ua, _("Pool %s resource not found!\n"), pr->Name);
589 if (ua->jcr->pool->cleaning_prefix == NULL) {
592 Dmsg4(200, "CLNprefix=%s: Vol=%s: len=%d strncmp=%d\n",
593 ua->jcr->pool->cleaning_prefix, mr->VolumeName,
594 strlen(ua->jcr->pool->cleaning_prefix),
595 strncmp(mr->VolumeName, ua->jcr->pool->cleaning_prefix,
596 strlen(ua->jcr->pool->cleaning_prefix)));
597 return strncmp(mr->VolumeName, ua->jcr->pool->cleaning_prefix,
598 strlen(ua->jcr->pool->cleaning_prefix)) == 0;