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(100, "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 changer = edit_device_codes(jcr, changer, jcr->device->changer_command,
79 status = run_program(changer, timeout, results);
80 Dmsg3(100, "run_prog: %s stat=%d result=%s\n", changer, status, results);
82 loaded = atoi(results);
84 loaded = -1; /* force unload */
86 Dmsg1(100, "loaded=%s\n", results);
88 /* If bad status or tape we want is not loaded, load it. */
89 if (status != 0 || loaded != slot) {
90 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
93 /* We are going to load a new tape, so close the device */
95 if (loaded != 0) { /* must unload drive */
96 Dmsg0(100, "Doing changer unload.\n");
98 bnet_fsend(dir, _("3902 Issuing autochanger \"unload\" command.\n"));
100 Jmsg(jcr, M_INFO, 0, _("Issuing autochanger \"unload\" command.\n"));
102 changer = edit_device_codes(jcr, changer,
103 jcr->device->changer_command, "unload");
104 status = run_program(changer, timeout, NULL);
105 Dmsg1(100, "unload status=%d\n", status);
108 * Load the desired cassette
110 Dmsg1(100, "Doing changer load slot %d\n", slot);
112 bnet_fsend(dir, _("3903 Issuing autochanger \"load slot %d\" command.\n"),
115 Jmsg(jcr, M_INFO, 0, _("Issuing autochanger \"load slot %d\" command.\n"),
118 changer = edit_device_codes(jcr, changer,
119 jcr->device->changer_command, "load");
120 status = run_program(changer, timeout, NULL);
122 Jmsg(jcr, M_INFO, 0, _("Autochanger \"load slot\" status is OK.\n"));
124 Jmsg(jcr, M_INFO, 0, _("Bad autochanger \"load slot\" status = %d.\n"),
127 Dmsg2(100, "load slot %d status=%d\n", slot, status);
129 free_pool_memory(changer);
130 free_pool_memory(results);
131 Dmsg1(100, "After changer, status=%d\n", status);
132 if (status == 0) { /* did we succeed? */
133 rtn_stat = 1; /* tape loaded by changer */
139 int autochanger_list(JCR *jcr, DEVICE *dev, BSOCK *dir)
141 uint32_t timeout = jcr->device->max_changer_wait;
144 int len = sizeof_pool_memory(dir->msg) - 1;
146 if (!dev_cap(dev, CAP_AUTOCHANGER) || !jcr->device->changer_name ||
147 !jcr->device->changer_command) {
148 bnet_fsend(dir, _("Not a changer device.\n"));
152 changer = get_pool_memory(PM_FNAME);
153 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
156 /* We are going to load a new tape, so close the device */
157 force_close_dev(dev);
159 /* First unload any tape */
160 bnet_fsend(dir, _("3902 Issuing autochanger \"unload\" command.\n"));
161 changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "unload");
162 run_program(changer, timeout, NULL);
164 /* Now list slots occupied */
165 changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "list");
166 bnet_fsend(dir, _("3903 Issuing autochanger \"list\" command.\n"));
167 bpipe = open_bpipe(changer, timeout, "r");
169 bnet_fsend(dir, _("Open bpipe failed.\n"));
172 /* Get output from changer */
173 while (fgets(dir->msg, len, bpipe->rfd)) {
174 dir->msglen = strlen(dir->msg);
177 bnet_sig(dir, BNET_EOD);
181 free_pool_memory(changer);
188 * Edit codes into ChangerCommand
190 * %a = archive device name
191 * %c = changer device name
200 * omsg = edited output message
201 * imsg = input string containing edit codes (%x)
202 * cmd = command string (load, unload, ...)
205 static char *edit_device_codes(JCR *jcr, char *omsg, char *imsg, char *cmd)
212 Dmsg1(200, "edit_device_codes: %s\n", imsg);
213 for (p=imsg; *p; p++) {
220 str = jcr->device->dev->dev_name;
223 str = NPRT(jcr->device->changer_name);
229 sprintf(add, "%d", jcr->VolCatInfo.Slot - 1);
233 sprintf(add, "%d", jcr->VolCatInfo.Slot);
236 case 'j': /* Job name */
240 str = NPRT(jcr->VolumeName);
243 str = NPRT(jcr->client_name);
258 Dmsg1(200, "add_str %s\n", str);
259 pm_strcat(&omsg, (char *)str);
260 Dmsg1(200, "omsg=%s\n", omsg);