2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998, 2000 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 * $Id: amlabel.c,v 1.53 2006/07/25 18:27:57 martinea Exp $
29 * write an Amanda label on a tape
36 #include <timestamp.h>
37 #include <taperscan.h>
41 int main(int argc, char **argv);
43 static void usage(void) {
44 g_fprintf(stderr, _("Usage: %s [-f] <conf> <label> [slot <slot-number>] [-o configoption]*\n"),
49 static void print_read_label_status_error(ReadLabelStatusFlags status) {
52 if (status == READ_LABEL_STATUS_SUCCESS)
55 status_strv = g_flags_nick_to_strv(status,
56 READ_LABEL_STATUS_FLAGS_TYPE);
57 g_assert(g_strv_length(status_strv) > 0);
58 if (g_strv_length(status_strv) == 1) {
59 g_printf("Error was %s.\n", *status_strv);
61 char * status_list = g_english_strjoinv(status_strv, "or");
62 g_printf("Error was one of %s.\n", status_list);
65 g_strfreev(status_strv);
75 char *label, *tapename = NULL;
76 char *labelstr, *slotstr;
77 char *conf_tapelist_old;
81 size_t tt_blocksize_kb;
84 ReadLabelStatusFlags label_status;
86 config_overwrites_t *cfg_ovr = NULL;
89 * Configure program for internationalization:
90 * 1) Only set the message locale for now.
91 * 2) Set textdomain for all amanda related programs to "amanda"
92 * We don't want to be forced to support dozens of message catalogs.
94 setlocale(LC_MESSAGES, "C");
100 set_pname("amlabel");
102 dbopen(DBG_SUBDIR_SERVER);
105 /* Don't die when child closes pipe */
106 signal(SIGPIPE, SIG_IGN);
108 erroutput_type = ERR_INTERACTIVE;
110 cfg_ovr = extract_commandline_config_overwrites(&argc, &argv);
112 if(argc > 1 && strcmp(argv[1],"-f") == 0)
116 if(argc != 3+force && argc != 5+force)
119 cfg_opt = argv[1+force];
120 label = argv[2+force];
122 if(argc == 5+force) {
123 if(strcmp(argv[3+force], "slot"))
125 slotstr = argv[4+force];
132 config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_FATAL,
134 apply_config_overwrites(cfg_ovr);
136 check_running_as(RUNNING_AS_DUMPUSER);
138 dbrename(config_name, DBG_SUBDIR_SERVER);
140 conf_tapelist = config_dir_relative(getconf_str(CNF_TAPELIST));
141 if (read_tapelist(conf_tapelist)) {
142 error(_("could not load tapelist \"%s\""), conf_tapelist);
146 labelstr = getconf_str(CNF_LABELSTR);
148 if(!match(labelstr, label)) {
149 error(_("label %s doesn't match labelstr \"%s\""), label, labelstr);
153 if((lookup_tapelabel(label))!=NULL) {
155 error(_("label %s already on a tape\n"),label);
159 tape = lookup_tapetype(getconf_str(CNF_TAPETYPE));
160 tt_blocksize_kb = (size_t)tapetype_get_blocksize(tape);
162 if((have_changer = changer_init()) == 0) {
165 _("%s: no tpchanger specified in \"%s\", so slot command invalid\n"),
166 argv[0], config_filename);
169 tapename = getconf_str(CNF_TAPEDEV);
170 if (tapename == NULL) {
171 error(_("No tapedev specified"));
173 } else if(have_changer != 1) {
174 error(_("changer initialization failed: %s"), strerror(errno));
177 if(changer_loadslot(slotstr, &outslot, &tapename)) {
178 error(_("could not load slot \"%s\": %s"), slotstr, changer_resultstr);
182 g_printf(_("labeling tape in slot %s (%s):\n"), outslot, tapename);
186 g_printf("Reading label...\n");fflush(stdout);
187 device = device_open(tapename);
188 if (device == NULL) {
189 error("Could not open device %s.\n", tapename);
192 device_set_startup_properties_from_config(device);
193 label_status = device_read_label(device);
195 if (label_status & READ_LABEL_STATUS_VOLUME_UNLABELED) {
196 g_printf("Found an unlabeled tape.\n");
197 } else if (label_status != READ_LABEL_STATUS_SUCCESS) {
198 g_printf("Reading the tape label failed: \n ");
199 print_read_label_status_error(label_status);
202 /* got an amanda tape */
203 g_printf(_("Found Amanda tape %s"),device->volume_label);
204 if(match(labelstr, device->volume_label) == 0) {
205 g_printf(_(", but it is not from configuration %s."), config_name);
209 if((lookup_tapelabel(device->volume_label)) != NULL) {
210 g_printf(_(", tape is active"));
219 char *timestamp = NULL;
221 g_printf(_("Writing label %s..\n"), label); fflush(stdout);
223 timestamp = get_undef_timestamp();
224 if (!device_start(device, ACCESS_WRITE, label, timestamp)) {
225 error(_("Error writing label.\n"));
226 g_assert_not_reached();
227 } else if (!device_finish(device)) {
228 error(_("Error closing device.\n"));
229 g_assert_not_reached();
233 g_printf(_("Checking label...\n")); fflush(stdout);
235 label_status = device_read_label(device);
236 if (label_status != READ_LABEL_STATUS_SUCCESS) {
237 g_printf("Checking the tape label failed: \n ");
238 print_read_label_status_error(label_status);
240 } else if (device->volume_label == NULL) {
241 error(_("no label found.\n"));
242 g_assert_not_reached();
243 } else if (strcmp(device->volume_label, label) != 0) {
244 error(_("Read back a different label: Got %s, but expected %s\n"),
245 device->volume_label, label);
246 g_assert_not_reached();
247 } else if (get_timestamp_state(device->volume_time) !=
249 error(_("Read the right label, but the wrong timestamp: "
250 "Got %s, expected X.\n"), device->volume_time);
251 g_assert_not_reached();
254 /* write tape list */
257 conf_tapelist_old = stralloc2(conf_tapelist, ".amlabel");
258 if(write_tapelist(conf_tapelist_old)) {
259 error(_("couldn't write tapelist: %s"), strerror(errno));
262 amfree(conf_tapelist_old);
264 /* XXX add cur_tape number to tape list structure */
265 remove_tapelabel(label);
266 add_tapelabel("0", label);
267 if(write_tapelist(conf_tapelist)) {
268 error(_("couldn't write tapelist: %s"), strerror(errno));
272 g_printf(_("Success!\n"));
274 g_printf(_("\ntape not labeled\n"));
277 g_object_unref(device);
282 amfree(conf_tapelist);