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 is_legal_volume_name(UAContext *ua, char *name);
45 static int send_label_request(UAContext *ua, MEDIA_DBR *mr, MEDIA_DBR *omr,
46 POOL_DBR *pr, int relabel);
47 static vol_list_t *get_slot_list_from_SD(UAContext *ua);
48 static int is_cleaning_tape(UAContext *ua, MEDIA_DBR *mr, POOL_DBR *pr);
54 * label storage=xxx volume=vvv
56 int labelcmd(UAContext *ua, char *cmd)
58 return do_label(ua, cmd, 0); /* standard label */
61 int relabelcmd(UAContext *ua, char *cmd)
63 return do_label(ua, cmd, 1); /* relabel tape */
68 * Update Slots corresponding to Volumes in autochanger
70 int update_slots(UAContext *ua)
73 vol_list_t *vl, *vol_list = NULL;
79 store = get_storage_resource(ua, 1);
83 ua->jcr->store = store;
85 vol_list = get_slot_list_from_SD(ua);
89 bsendmsg(ua, _("No Volumes found to label, or no barcodes.\n"));
93 /* Walk through the list updating the media records */
94 for (vl=vol_list; vl; vl=vl->next) {
96 memset(&mr, 0, sizeof(mr));
97 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"),
114 bsendmsg(ua, _("Record for Volume \"%s\" not found in catalog.\n"),
122 for (vl=vol_list; vl; ) {
130 if (ua->jcr->store_bsock) {
131 bnet_sig(ua->jcr->store_bsock, BNET_TERMINATE);
132 bnet_close(ua->jcr->store_bsock);
133 ua->jcr->store_bsock = NULL;
139 * Common routine for both label and relabel
141 static int do_label(UAContext *ua, char *cmd, int relabel)
145 sd = ua->jcr->store_bsock;
146 char dev_name[MAX_NAME_LENGTH];
152 static char *barcode_keyword[] = {
158 memset(&pr, 0, sizeof(pr));
162 store = get_storage_resource(ua, 1);
166 ua->jcr->store = store;
168 if (!relabel && find_arg_keyword(ua, barcode_keyword) >= 0) {
169 label_from_barcodes(ua);
173 /* If relabel get name of Volume to relabel */
175 /* Check for volume=OldVolume */
176 i = find_arg(ua, "volume");
177 if (i >= 0 && ua->argv[i]) {
178 memset(&omr, 0, sizeof(omr));
179 bstrncpy(omr.VolumeName, ua->argv[i], sizeof(omr.VolumeName));
180 if (db_get_media_record(ua->jcr, ua->db, &omr)) {
183 bsendmsg(ua, "%s", db_strerror(ua->db));
185 /* No keyword or Vol not found, ask user to select */
186 if (!select_media_dbr(ua, &omr)) {
190 /* Require Volume to be Purged */
192 if (strcmp(omr.VolStatus, "Purged") != 0) {
193 bsendmsg(ua, _("Volume \"%s\" has VolStatus %s. It must be purged before relabeling.\n"),
194 omr.VolumeName, omr.VolStatus);
199 /* Check for name=NewVolume */
200 i = find_arg(ua, "name");
201 if (i >= 0 && ua->argv[i]) {
202 pm_strcpy(&ua->cmd, ua->argv[i]);
206 /* Get a new Volume name */
208 if (!get_cmd(ua, _("Enter new Volume name: "))) {
212 if (!is_legal_volume_name(ua, ua->cmd)) {
216 memset(&mr, 0, sizeof(mr));
217 bstrncpy(mr.VolumeName, ua->cmd, sizeof(mr.VolumeName));
218 if (db_get_media_record(ua->jcr, ua->db, &mr)) {
219 bsendmsg(ua, _("Media record for new Volume \"%s\" already exists.\n"),
226 /* If autochanger, request slot */
227 if (store->autochanger) {
228 i = find_arg(ua, "slot");
229 if (i >= 0 && ua->argv[i]) {
230 mr.Slot = atoi(ua->argv[i]);
231 } else if (!get_pint(ua, _("Enter slot (0 for none): "))) {
234 mr.Slot = ua->pint32_val;
238 bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
240 /* Must select Pool if not already done */
241 if (pr.PoolId == 0) {
242 memset(&pr, 0, sizeof(pr));
243 if (!select_pool_dbr(ua, &pr)) {
248 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d ...\n"),
249 store->hdr.name, store->address, store->SDport);
250 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
251 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
254 sd = ua->jcr->store_bsock;
256 ok = send_label_request(ua, &mr, &omr, &pr, relabel);
260 if (!db_delete_media_record(ua->jcr, ua->db, &omr)) {
261 bsendmsg(ua, _("Delete of Volume \"%s\" failed. ERR=%s"),
262 omr.VolumeName, db_strerror(ua->db));
264 bsendmsg(ua, _("Old volume \"%s\" deleted from catalog.\n"),
269 strcpy(dev_name, store->dev_name);
270 bsendmsg(ua, _("Requesting mount %s ...\n"), dev_name);
271 bash_spaces(dev_name);
272 bnet_fsend(sd, "mount %s", dev_name);
273 unbash_spaces(dev_name);
274 while (bnet_recv(sd) >= 0) {
275 bsendmsg(ua, "%s", sd->msg);
277 * 3001 OK mount. Device=xxx or
278 * 3001 Mounted Volume vvvv
280 mounted = strncmp(sd->msg, "3001 ", 5) == 0;
285 bsendmsg(ua, _("Do not forget to mount the drive!!!\n"));
287 bnet_sig(sd, BNET_TERMINATE);
289 ua->jcr->store_bsock = NULL;
295 * Request SD to send us the slot:barcodes, then wiffle
296 * through them all labeling them.
298 static void label_from_barcodes(UAContext *ua)
300 STORE *store = ua->jcr->store;
303 vol_list_t *vl, *vol_list = NULL;
305 vol_list = get_slot_list_from_SD(ua);
308 bsendmsg(ua, _("No Volumes found to label, or no barcodes.\n"));
312 /* Display list of Volumes and ask if he really wants to proceed */
313 bsendmsg(ua, _("The following Volumes will be labeled:\n"
315 "==============\n"));
316 for (vl=vol_list; vl; vl=vl->next) {
317 bsendmsg(ua, "%4d %s\n", vl->Slot, vl->VolName);
319 if (!get_cmd(ua, _("Do you want to continue? (y/n): ")) ||
320 (ua->cmd[0] != 'y' && ua->cmd[0] != 'Y')) {
324 memset(&pr, 0, sizeof(pr));
325 if (!select_pool_dbr(ua, &pr)) {
328 memset(&omr, 0, sizeof(omr));
330 /* Fire off the label requests */
331 for (vl=vol_list; vl; vl=vl->next) {
333 memset(&mr, 0, sizeof(mr));
334 bstrncpy(mr.VolumeName, vl->VolName, sizeof(mr.VolumeName));
335 if (db_get_media_record(ua->jcr, ua->db, &mr)) {
336 bsendmsg(ua, _("Media record for Slot %d Volume \"%s\" already exists.\n"),
337 vl->Slot, mr.VolumeName);
340 if (is_cleaning_tape(ua, &mr, &pr)) {
341 set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
342 if (db_create_media_record(ua->jcr, ua->db, &mr)) {
343 bsendmsg(ua, _("Catalog record for cleaning tape \"%s\" successfully created.\n"),
346 bsendmsg(ua, "Catalog error on cleaning tape: %s", db_strerror(ua->db));
350 bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
351 if (ua->jcr->store_bsock) {
352 bnet_sig(ua->jcr->store_bsock, BNET_TERMINATE);
353 bnet_close(ua->jcr->store_bsock);
354 ua->jcr->store_bsock = NULL;
356 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d ...\n"),
357 store->hdr.name, store->address, store->SDport);
358 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
359 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
364 send_label_request(ua, &mr, &omr, &pr, 0);
370 for (vl=vol_list; vl; ) {
378 if (ua->jcr->store_bsock) {
379 bnet_sig(ua->jcr->store_bsock, BNET_TERMINATE);
380 bnet_close(ua->jcr->store_bsock);
381 ua->jcr->store_bsock = NULL;
387 static int is_legal_volume_name(UAContext *ua, char *name)
390 /* Restrict the characters permitted in the Volume name */
391 if (strpbrk(name, "`~!@#$%^&*()[]{}|\\;'\"<>?,/")) {
392 bsendmsg(ua, _("Illegal character | in a volume name.\n"));
396 if (len >= MAX_NAME_LENGTH) {
397 bsendmsg(ua, _("Volume name too long.\n"));
401 bsendmsg(ua, _("Volume name must be at least one character long.\n"));
407 static int send_label_request(UAContext *ua, MEDIA_DBR *mr, MEDIA_DBR *omr,
408 POOL_DBR *pr, int relabel)
411 char dev_name[MAX_NAME_LENGTH];
414 sd = ua->jcr->store_bsock;
415 bstrncpy(dev_name, ua->jcr->store->dev_name, sizeof(dev_name));
416 bash_spaces(dev_name);
417 bash_spaces(mr->VolumeName);
418 bash_spaces(mr->MediaType);
419 bash_spaces(pr->Name);
421 bash_spaces(omr->VolumeName);
422 bnet_fsend(sd, _("relabel %s OldName=%s NewName=%s PoolName=%s MediaType=%s Slot=%d"),
423 dev_name, omr->VolumeName, mr->VolumeName, pr->Name, mr->MediaType, mr->Slot);
424 bsendmsg(ua, _("Sending relabel command from \"%s\" to \"%s\" ...\n"),
425 omr->VolumeName, mr->VolumeName);
427 bnet_fsend(sd, _("label %s VolumeName=%s PoolName=%s MediaType=%s Slot=%d"),
428 dev_name, mr->VolumeName, pr->Name, mr->MediaType, mr->Slot);
429 bsendmsg(ua, _("Sending label command for Volume \"%s\" Slot %d ...\n"),
430 mr->VolumeName, mr->Slot);
431 Dmsg5(200, "label %s VolumeName=%s PoolName=%s MediaType=%s Slot=%d\n",
432 dev_name, mr->VolumeName, pr->Name, mr->MediaType, mr->Slot);
435 while (bnet_recv(sd) >= 0) {
436 bsendmsg(ua, "%s", sd->msg);
437 if (strncmp(sd->msg, "3000 OK label.", 14) == 0) {
441 unbash_spaces(mr->VolumeName);
442 unbash_spaces(mr->MediaType);
443 unbash_spaces(pr->Name);
444 mr->LabelDate = time(NULL);
446 set_pool_dbr_defaults_in_media_dbr(mr, pr);
447 if (db_create_media_record(ua->jcr, ua->db, mr)) {
448 bsendmsg(ua, _("Catalog record for Volume \"%s\", Slot %d successfully created.\n"),
449 mr->VolumeName, mr->Slot);
451 bsendmsg(ua, "%s", db_strerror(ua->db));
455 bsendmsg(ua, _("Label command failed.\n"));
460 static vol_list_t *get_slot_list_from_SD(UAContext *ua)
462 STORE *store = ua->jcr->store;
463 char dev_name[MAX_NAME_LENGTH];
466 vol_list_t *vol_list = NULL;
469 bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d ...\n"),
470 store->hdr.name, store->address, store->SDport);
471 if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
472 bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
475 sd = ua->jcr->store_bsock;
477 bstrncpy(dev_name, store->dev_name, sizeof(dev_name));
478 bash_spaces(dev_name);
479 /* Ask for autochanger list of volumes */
480 bnet_fsend(sd, _("autochanger list %s \n"), dev_name);
482 /* Read and organize list of Volumes */
483 while (bnet_recv(sd) >= 0) {
486 strip_trailing_junk(sd->msg);
488 /* Check for returned SD messages */
489 if (sd->msg[0] == '3' && B_ISDIGIT(sd->msg[1]) &&
490 B_ISDIGIT(sd->msg[2]) && B_ISDIGIT(sd->msg[3]) &&
492 bsendmsg(ua, "%s\n", sd->msg); /* pass them on to user */
496 /* Validate Slot:Barcode */
497 p = strchr(sd->msg, ':');
498 if (p && strlen(p) > 1) {
500 if (!is_an_integer(sd->msg)) {
506 Slot = atoi(sd->msg);
507 if (Slot <= 0 || !is_legal_volume_name(ua, p)) {
511 /* Add Slot and VolumeName to list */
512 vl = (vol_list_t *)malloc(sizeof(vol_list_t));
514 vl->VolName = bstrdup(p);
519 /* Add new entry to end of list */
520 for (vol_list_t *tvl=vol_list; tvl; tvl=tvl->next) {
533 * Check if this is a cleaning tape by comparing the Volume name
534 * with the Cleaning Prefix. If they match, this is a cleaning
537 static int is_cleaning_tape(UAContext *ua, MEDIA_DBR *mr, POOL_DBR *pr)
539 if (!ua->jcr->pool) {
540 /* Find Pool resource */
541 ua->jcr->pool = (POOL *)GetResWithName(R_POOL, pr->Name);
542 if (!ua->jcr->pool) {
543 bsendmsg(ua, _("Pool %s resource not found!\n"), pr->Name);
547 if (ua->jcr->pool->cleaning_prefix == NULL) {
550 return strncmp(mr->VolumeName, ua->jcr->pool->cleaning_prefix,
551 strlen(ua->jcr->pool->cleaning_prefix)) == 0;