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);
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 labelcmd(UAContext *ua, char *cmd)
57 return do_label(ua, cmd, 0); /* standard label */
60 int relabelcmd(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));
97 if (db_get_media_record(ua->jcr, ua->db, &mr)) {
98 if (mr.Slot != vl->Slot) {
100 if (!db_update_media_record(ua->jcr, ua->db, &mr)) {
101 bsendmsg(ua, _("%s\n"), db_strerror(ua->db));
104 "Catalog record for Volume \"%s\" updated to reference slot %d.\n"),
105 mr.VolumeName, mr.Slot);
108 bsendmsg(ua, _("Catalog record for Volume \"%s\" is up to date.\n"),
113 bsendmsg(ua, _("Record for Volume \"%s\" not found in catalog.\n"),
121 for (vl=vol_list; vl; ) {
129 if (ua->jcr->store_bsock) {
130 bnet_sig(ua->jcr->store_bsock, BNET_TERMINATE);
131 bnet_close(ua->jcr->store_bsock);
132 ua->jcr->store_bsock = NULL;
138 * Common routine for both label and relabel
140 static int do_label(UAContext *ua, char *cmd, int relabel)
144 sd = ua->jcr->store_bsock;
145 char dev_name[MAX_NAME_LENGTH];
151 static char *barcode_keyword[] = {
157 memset(&pr, 0, sizeof(pr));
161 store = get_storage_resource(ua, 1);
165 ua->jcr->store = store;
167 if (!relabel && find_arg_keyword(ua, barcode_keyword) >= 0) {
168 label_from_barcodes(ua);
172 /* If relabel get name of Volume to relabel */
174 /* Check for volume=OldVolume */
175 i = find_arg_with_value(ua, "volume");
177 memset(&omr, 0, sizeof(omr));
178 bstrncpy(omr.VolumeName, ua->argv[i], sizeof(omr.VolumeName));
179 if (db_get_media_record(ua->jcr, ua->db, &omr)) {
182 bsendmsg(ua, "%s", db_strerror(ua->db));
184 /* No keyword or Vol not found, ask user to select */
185 if (!select_media_dbr(ua, &omr)) {
189 /* Require Volume to be Purged */
191 if (strcmp(omr.VolStatus, "Purged") != 0) {
192 bsendmsg(ua, _("Volume \"%s\" has VolStatus %s. It must be purged before relabeling.\n"),
193 omr.VolumeName, omr.VolStatus);
198 /* Check for name=NewVolume */
199 i = find_arg_with_value(ua, "name");
201 pm_strcpy(&ua->cmd, ua->argv[i]);
205 /* Get a new Volume name */
207 if (!get_cmd(ua, _("Enter new Volume name: "))) {
211 if (!is_volume_name_legal(ua, ua->cmd)) {
215 memset(&mr, 0, sizeof(mr));
216 bstrncpy(mr.VolumeName, ua->cmd, sizeof(mr.VolumeName));
217 if (db_get_media_record(ua->jcr, ua->db, &mr)) {
218 bsendmsg(ua, _("Media record for new Volume \"%s\" already exists.\n"),
225 /* If autochanger, request slot */
226 if (store->autochanger) {
227 i = find_arg_with_value(ua, "slot");
229 mr.Slot = atoi(ua->argv[i]);
230 } else if (!get_pint(ua, _("Enter slot (0 for none): "))) {
233 mr.Slot = ua->pint32_val;
237 bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
239 /* Must select Pool if not already done */
240 if (pr.PoolId == 0) {
241 memset(&pr, 0, sizeof(pr));
242 if (!select_pool_dbr(ua, &pr)) {
247 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d ...\n"),
248 store->hdr.name, store->address, store->SDport);
249 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
250 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
253 sd = ua->jcr->store_bsock;
255 ok = send_label_request(ua, &mr, &omr, &pr, relabel);
259 if (!db_delete_media_record(ua->jcr, ua->db, &omr)) {
260 bsendmsg(ua, _("Delete of Volume \"%s\" failed. ERR=%s"),
261 omr.VolumeName, db_strerror(ua->db));
263 bsendmsg(ua, _("Old volume \"%s\" deleted from catalog.\n"),
268 strcpy(dev_name, store->dev_name);
269 bsendmsg(ua, _("Requesting mount %s ...\n"), dev_name);
270 bash_spaces(dev_name);
271 bnet_fsend(sd, "mount %s", dev_name);
272 unbash_spaces(dev_name);
273 while (bnet_recv(sd) >= 0) {
274 bsendmsg(ua, "%s", sd->msg);
276 * 3001 OK mount. Device=xxx or
277 * 3001 Mounted Volume vvvv
279 mounted = strncmp(sd->msg, "3001 ", 5) == 0;
284 bsendmsg(ua, _("Do not forget to mount the drive!!!\n"));
286 bnet_sig(sd, BNET_TERMINATE);
288 ua->jcr->store_bsock = NULL;
294 * Request SD to send us the slot:barcodes, then wiffle
295 * through them all labeling them.
297 static void label_from_barcodes(UAContext *ua)
299 STORE *store = ua->jcr->store;
302 vol_list_t *vl, *vol_list = NULL;
304 vol_list = get_slot_list_from_SD(ua);
307 bsendmsg(ua, _("No Volumes found to label, or no barcodes.\n"));
311 /* Display list of Volumes and ask if he really wants to proceed */
312 bsendmsg(ua, _("The following Volumes will be labeled:\n"
314 "==============\n"));
315 for (vl=vol_list; vl; vl=vl->next) {
316 bsendmsg(ua, "%4d %s\n", vl->Slot, vl->VolName);
318 if (!get_cmd(ua, _("Do you want to continue? (y/n): ")) ||
319 (ua->cmd[0] != 'y' && ua->cmd[0] != 'Y')) {
323 memset(&pr, 0, sizeof(pr));
324 if (!select_pool_dbr(ua, &pr)) {
327 memset(&omr, 0, sizeof(omr));
329 /* Fire off the label requests */
330 for (vl=vol_list; vl; vl=vl->next) {
332 memset(&mr, 0, sizeof(mr));
333 bstrncpy(mr.VolumeName, vl->VolName, sizeof(mr.VolumeName));
334 if (db_get_media_record(ua->jcr, ua->db, &mr)) {
335 bsendmsg(ua, _("Media record for Slot %d Volume \"%s\" already exists.\n"),
336 vl->Slot, mr.VolumeName);
339 if (is_cleaning_tape(ua, &mr, &pr)) {
340 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
341 if (db_create_media_record(ua->jcr, ua->db, &mr)) {
342 bsendmsg(ua, _("Catalog record for cleaning tape \"%s\" successfully created.\n"),
345 bsendmsg(ua, "Catalog error on cleaning tape: %s", db_strerror(ua->db));
349 bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
350 if (ua->jcr->store_bsock) {
351 bnet_sig(ua->jcr->store_bsock, BNET_TERMINATE);
352 bnet_close(ua->jcr->store_bsock);
353 ua->jcr->store_bsock = NULL;
355 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d ...\n"),
356 store->hdr.name, store->address, store->SDport);
357 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
358 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
363 send_label_request(ua, &mr, &omr, &pr, 0);
369 for (vl=vol_list; vl; ) {
377 if (ua->jcr->store_bsock) {
378 bnet_sig(ua->jcr->store_bsock, BNET_TERMINATE);
379 bnet_close(ua->jcr->store_bsock);
380 ua->jcr->store_bsock = NULL;
387 * Check if the Volume name has legal characters
388 * If ua is non-NULL send the message
390 int is_volume_name_legal(UAContext *ua, char *name)
394 char *accept = ":.-_";
396 /* Restrict the characters permitted in the Volume name */
397 for (p=name; *p; p++) {
398 if (B_ISALPHA(*p) || B_ISDIGIT(*p) || strchr(accept, (int)(*p))) {
402 bsendmsg(ua, _("Illegal character \"%c\" in a volume name.\n"), *p);
407 if (len >= MAX_NAME_LENGTH) {
409 bsendmsg(ua, _("Volume name too long.\n"));
415 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
422 static int send_label_request(UAContext *ua, MEDIA_DBR *mr, MEDIA_DBR *omr,
423 POOL_DBR *pr, int relabel)
426 char dev_name[MAX_NAME_LENGTH];
429 sd = ua->jcr->store_bsock;
430 bstrncpy(dev_name, ua->jcr->store->dev_name, sizeof(dev_name));
431 bash_spaces(dev_name);
432 bash_spaces(mr->VolumeName);
433 bash_spaces(mr->MediaType);
434 bash_spaces(pr->Name);
436 bash_spaces(omr->VolumeName);
437 bnet_fsend(sd, _("relabel %s OldName=%s NewName=%s PoolName=%s MediaType=%s Slot=%d"),
438 dev_name, omr->VolumeName, mr->VolumeName, pr->Name, mr->MediaType, mr->Slot);
439 bsendmsg(ua, _("Sending relabel command from \"%s\" to \"%s\" ...\n"),
440 omr->VolumeName, mr->VolumeName);
442 bnet_fsend(sd, _("label %s VolumeName=%s PoolName=%s MediaType=%s Slot=%d"),
443 dev_name, mr->VolumeName, pr->Name, mr->MediaType, mr->Slot);
444 bsendmsg(ua, _("Sending label command for Volume \"%s\" Slot %d ...\n"),
445 mr->VolumeName, mr->Slot);
446 Dmsg5(200, "label %s VolumeName=%s PoolName=%s MediaType=%s Slot=%d\n",
447 dev_name, mr->VolumeName, pr->Name, mr->MediaType, mr->Slot);
450 while (bnet_recv(sd) >= 0) {
451 bsendmsg(ua, "%s", sd->msg);
452 if (strncmp(sd->msg, "3000 OK label.", 14) == 0) {
456 unbash_spaces(mr->VolumeName);
457 unbash_spaces(mr->MediaType);
458 unbash_spaces(pr->Name);
459 mr->LabelDate = time(NULL);
461 set_pool_dbr_defaults_in_media_dbr(mr, pr);
462 if (db_create_media_record(ua->jcr, ua->db, mr)) {
463 bsendmsg(ua, _("Catalog record for Volume \"%s\", Slot %d successfully created.\n"),
464 mr->VolumeName, mr->Slot);
466 bsendmsg(ua, "%s", db_strerror(ua->db));
470 bsendmsg(ua, _("Label command failed.\n"));
475 static vol_list_t *get_slot_list_from_SD(UAContext *ua)
477 STORE *store = ua->jcr->store;
478 char dev_name[MAX_NAME_LENGTH];
481 vol_list_t *vol_list = NULL;
484 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d ...\n"),
485 store->hdr.name, store->address, store->SDport);
486 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
487 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
490 sd = ua->jcr->store_bsock;
492 bstrncpy(dev_name, store->dev_name, sizeof(dev_name));
493 bash_spaces(dev_name);
494 /* Ask for autochanger list of volumes */
495 bnet_fsend(sd, _("autochanger list %s \n"), dev_name);
497 /* Read and organize list of Volumes */
498 while (bnet_recv(sd) >= 0) {
501 strip_trailing_junk(sd->msg);
503 /* Check for returned SD messages */
504 if (sd->msg[0] == '3' && B_ISDIGIT(sd->msg[1]) &&
505 B_ISDIGIT(sd->msg[2]) && B_ISDIGIT(sd->msg[3]) &&
507 bsendmsg(ua, "%s\n", sd->msg); /* pass them on to user */
511 /* Validate Slot:Barcode */
512 p = strchr(sd->msg, ':');
513 if (p && strlen(p) > 1) {
515 if (!is_an_integer(sd->msg)) {
521 Slot = atoi(sd->msg);
522 if (Slot <= 0 || !is_volume_name_legal(ua, p)) {
526 /* Add Slot and VolumeName to list */
527 vl = (vol_list_t *)malloc(sizeof(vol_list_t));
529 vl->VolName = bstrdup(p);
534 /* Add new entry to end of list */
535 for (vol_list_t *tvl=vol_list; tvl; tvl=tvl->next) {
548 * Check if this is a cleaning tape by comparing the Volume name
549 * with the Cleaning Prefix. If they match, this is a cleaning
552 static int is_cleaning_tape(UAContext *ua, MEDIA_DBR *mr, POOL_DBR *pr)
554 if (!ua->jcr->pool) {
555 /* Find Pool resource */
556 ua->jcr->pool = (POOL *)GetResWithName(R_POOL, pr->Name);
557 if (!ua->jcr->pool) {
558 bsendmsg(ua, _("Pool %s resource not found!\n"), pr->Name);
562 if (ua->jcr->pool->cleaning_prefix == NULL) {
565 return strncmp(mr->VolumeName, ua->jcr->pool->cleaning_prefix,
566 strlen(ua->jcr->pool->cleaning_prefix)) == 0;