3 * Routines for handling the autochanger.
5 * Kern Sibbald, August MMII
10 Copyright (C) 2000-2003 Kern Sibbald and John Walker
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write to the Free
24 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
29 #include "bacula.h" /* pull in global headers */
30 #include "stored.h" /* pull in Storage Deamon headers */
32 /* Forward referenced functions */
33 static char *edit_device_codes(JCR *jcr, char *omsg, char *imsg, char *cmd);
37 * Called here to do an autoload using the autochanger, if
38 * configured, and if a Slot has been defined for this Volume.
39 * On success this routine loads the indicated tape, but the
40 * label is not read, so it must be verified.
42 * Note if dir is not NULL, it is the console requesting the
43 * autoload for labeling, so we respond directly to the
46 * Returns: 1 on success
49 int autoload_device(JCR *jcr, DEVICE *dev, int writing, BSOCK *dir)
51 int slot = jcr->VolCatInfo.Slot;
55 * Handle autoloaders here. If we cannot autoload it, we
56 * will return FALSE to ask the sysop.
58 if (writing && dev_cap(dev, CAP_AUTOCHANGER) && slot <= 0) {
60 return 0; /* For user, bail out right now */
62 if (dir_find_next_appendable_volume(jcr)) {
63 slot = jcr->VolCatInfo.Slot;
66 Dmsg1(400, "Want changer slot=%d\n", slot);
68 if (slot > 0 && jcr->device->changer_name && jcr->device->changer_command) {
69 uint32_t timeout = jcr->device->max_changer_wait;
70 POOLMEM *changer, *results;
73 results = get_pool_memory(PM_MESSAGE);
74 changer = get_pool_memory(PM_FNAME);
76 /* Find out what is loaded, zero means device is unloaded */
77 Jmsg(jcr, M_INFO, 0, _("3301 Issuing autochanger \"loaded\" command.\n"));
78 changer = edit_device_codes(jcr, changer, jcr->device->changer_command,
80 status = run_program(changer, timeout, results);
81 Dmsg3(100, "run_prog: %s stat=%d result=%s\n", changer, status, results);
83 loaded = atoi(results);
85 Jmsg(jcr, M_INFO, 0, _("3991 Bad autochanger \"loaded\" command, status=%d.\n"), status);
86 loaded = -1; /* force unload */
88 Dmsg1(400, "loaded=%s\n", results);
90 /* If bad status or tape we want is not loaded, load it. */
91 if (status != 0 || loaded != slot) {
92 offline_or_rewind_dev(dev);
93 /* We are going to load a new tape, so close the device */
95 if (loaded != 0) { /* must unload drive */
96 Dmsg0(400, "Doing changer unload.\n");
97 Jmsg(jcr, M_INFO, 0, _("3302 Issuing autochanger \"unload\" command.\n"));
98 changer = edit_device_codes(jcr, changer,
99 jcr->device->changer_command, "unload");
100 status = run_program(changer, timeout, NULL);
101 Dmsg1(400, "unload status=%d\n", status);
104 * Load the desired cassette
106 Dmsg1(400, "Doing changer load slot %d\n", slot);
107 Jmsg(jcr, M_INFO, 0, _("3303 Issuing autochanger \"load slot %d\" command.\n"),
109 changer = edit_device_codes(jcr, changer,
110 jcr->device->changer_command, "load");
111 status = run_program(changer, timeout, NULL);
113 Jmsg(jcr, M_INFO, 0, _("3304 Autochanger \"load slot %d\", status is OK.\n"),
116 Jmsg(jcr, M_INFO, 0, _("3992 Bad autochanger \"load slot %d\", status=%d.\n"),
119 Dmsg2(400, "load slot %d status=%d\n", slot, status);
121 free_pool_memory(changer);
122 free_pool_memory(results);
123 Dmsg1(400, "After changer, status=%d\n", status);
124 if (status == 0) { /* did we succeed? */
125 rtn_stat = 1; /* tape loaded by changer */
132 * The Volume is not in the correct slot, so mark this
133 * Volume as not being in the Changer.
135 void invalid_slot_in_catalog(JCR *jcr, DEVICE *dev)
137 Jmsg(jcr, M_ERROR, 0, _("Autochanger Volume \"%s\" not found in slot %d.\n"
138 " Setting slot to zero in catalog.\n"),
139 jcr->VolCatInfo.VolCatName, jcr->VolCatInfo.Slot);
140 jcr->VolCatInfo.InChanger = false;
141 dev->VolCatInfo.InChanger = false;
142 Dmsg0(100, "update vol info in mount\n");
143 dir_update_volume_info(jcr, dev, 1); /* set new status */
147 * List the Volumes that are in the autoloader possibly
148 * with their barcodes.
149 * We assume that it is always the Console that is calling us.
151 int autochanger_list(JCR *jcr, DEVICE *dev, BSOCK *dir)
153 uint32_t timeout = jcr->device->max_changer_wait;
156 int len = sizeof_pool_memory(dir->msg) - 1;
158 if (!dev_cap(dev, CAP_AUTOCHANGER) || !jcr->device->changer_name ||
159 !jcr->device->changer_command) {
160 bnet_fsend(dir, _("3993 Not a autochanger device.\n"));
164 changer = get_pool_memory(PM_FNAME);
165 offline_or_rewind_dev(dev);
166 /* We are going to load a new tape, so close the device */
167 force_close_dev(dev);
169 /* First unload any tape */
170 bnet_fsend(dir, _("3305 Issuing autochanger \"unload\" command.\n"));
171 changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "unload");
172 run_program(changer, timeout, NULL);
174 /* Now list slots occupied */
175 changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "list");
176 bnet_fsend(dir, _("3306 Issuing autochanger \"list\" command.\n"));
177 bpipe = open_bpipe(changer, timeout, "r");
179 bnet_fsend(dir, _("3993 Open bpipe failed.\n"));
182 /* Get output from changer */
183 while (fgets(dir->msg, len, bpipe->rfd)) {
184 dir->msglen = strlen(dir->msg);
187 bnet_sig(dir, BNET_EOD);
191 free_pool_memory(changer);
198 * Edit codes into ChangerCommand
200 * %a = archive device name
201 * %c = changer device name
210 * omsg = edited output message
211 * imsg = input string containing edit codes (%x)
212 * cmd = command string (load, unload, ...)
215 static char *edit_device_codes(JCR *jcr, char *omsg, char *imsg, char *cmd)
222 Dmsg1(400, "edit_device_codes: %s\n", imsg);
223 for (p=imsg; *p; p++) {
230 str = dev_name(jcr->device->dev);
233 str = NPRT(jcr->device->changer_name);
239 sprintf(add, "%d", jcr->VolCatInfo.Slot - 1);
243 sprintf(add, "%d", jcr->VolCatInfo.Slot);
246 case 'j': /* Job name */
250 str = NPRT(jcr->VolumeName);
253 str = NPRT(jcr->client_name);
268 Dmsg1(400, "add_str %s\n", str);
269 pm_strcat(&omsg, (char *)str);
270 Dmsg1(400, "omsg=%s\n", omsg);