+/*
+ * If permitted, we label the device, make sure we can do
+ * it by checking that the VolCatBytes is zero => not labeled,
+ * once the Volume is labeled we don't want to label another
+ * blank tape with the same name. For disk, we go ahead and
+ * label it anyway, because the OS insures that there is only
+ * one Volume with that name.
+ * As noted above, at this point dcr->VolCatInfo has what
+ * the Director wants and dev->VolCatInfo has info on the
+ * previous tape (or nothing).
+ *
+ * Return codes are:
+ * try_next_vol label failed, look for another volume
+ * try_read_vol labeled volume, now re-read the label
+ * try_error hard error (catalog update)
+ * try_default I couldn't do anything
+ */
+int DCR::try_autolabel(bool opened)
+{
+ DCR *dcr = this;
+
+ if (dev->poll && !dev->is_tape()) {
+ return try_default; /* if polling, don't try to create new labels */
+ }
+ /* For a tape require it to be opened and read before labeling */
+ if (!opened && dev->is_tape()) {
+ return try_default;
+ }
+ if (dev->has_cap(CAP_LABEL) && (VolCatInfo.VolCatBytes == 0 ||
+ (!dev->is_tape() && strcmp(VolCatInfo.VolCatStatus,
+ "Recycle") == 0))) {
+ Dmsg0(150, "Create volume label\n");
+ /* Create a new Volume label and write it to the device */
+ if (!write_new_volume_label_to_dev(dcr, VolumeName,
+ pool_name, false, /* no relabel */ false /* defer DVD label */)) {
+ Dmsg0(150, "!write_vol_label\n");
+ if (opened) {
+ mark_volume_in_error();
+ }
+ return try_next_vol;
+ }
+ Dmsg0(150, "dir_update_vol_info. Set Append\n");
+ /* Copy Director's info into the device info */
+ dev->VolCatInfo = VolCatInfo; /* structure assignment */
+ if (!dir_update_volume_info(dcr, true, true)) { /* indicate tape labeled */
+ return try_error;
+ }
+ Jmsg(dcr->jcr, M_INFO, 0, _("Labeled new Volume \"%s\" on device %s.\n"),
+ VolumeName, dev->print_name());
+ return try_read_vol; /* read label we just wrote */
+ }
+ if (!dev->has_cap(CAP_LABEL) && VolCatInfo.VolCatBytes == 0) {
+ Jmsg(jcr, M_WARNING, 0, _("Device %s not configured to autolabel Volumes.\n"),
+ dev->print_name());
+ }
+ /* If not removable, Volume is broken */
+ if (!dev->is_removable()) {
+ Jmsg(jcr, M_WARNING, 0, _("Volume \"%s\" not on device %s.\n"),
+ VolumeName, dev->print_name());
+ mark_volume_in_error();
+ return try_next_vol;
+ }
+ return try_default;
+}
+