Index: man/xml-source/amanda-devices.7.xml
===================================================================
--- man/xml-source/amanda-devices.7.xml	(revision 2164)
+++ man/xml-source/amanda-devices.7.xml	(working copy)
@@ -449,6 +449,10 @@
  (read-write) This boolean property specifies whether the device driver may use the MTFSR operation (forward seek record).
 </listitem></varlistentry>
  <!-- ==== -->
+ <varlistentry><term>NONBLOCKING_OPEN</term><listitem>
+ (read-write) Set this boolean property to "true" if O_NONBLOCK must be used on the open call. Default to "true" on Linux and "false" on all others machines. Witout it, Linux wait for a few seconds if no tape are loaded. Solaris have strange error it is set to "yes".
+</listitem></varlistentry>
+ <!-- ==== -->
  <varlistentry><term>READ_BUFFER_SIZE</term><listitem>
  (read-write) This property specifies the minimum buffer size that will be used for reads; this should be large enough to contain any block that may be read from the device, and must be larger than BLOCK_SIZE.  This property exists for tape devices which cannot determine the size of on-tape blocks, or which may discard data which overflows a small buffer.  The tapetype parameter <emphasis>READBLOCKSIZE</emphasis> sets this property.  See BLOCK SIZES, above.
 </listitem></varlistentry>
Index: device-src/tape-device.c
===================================================================
--- device-src/tape-device.c	(revision 2164)
+++ device-src/tape-device.c	(working copy)
@@ -63,6 +63,7 @@
 DevicePropertyBase device_property_bsr;
 DevicePropertyBase device_property_eom;
 DevicePropertyBase device_property_bsf_after_eom;
+DevicePropertyBase device_property_nonblocking_open;
 DevicePropertyBase device_property_final_filemarks;
 
 void tape_device_register(void);
@@ -178,6 +179,15 @@
 	    &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
     device_set_simple_property(d_self, PROPERTY_BSF_AFTER_EOM,
 	    &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
+
+#ifdef DEFAULT_TAPE_NON_BLOCKING_OPEN
+    self->nonblocking_open = TRUE;
+#else
+    self->nonblocking_open = FALSE;
+#endif
+    g_value_set_boolean(&response, self->nonblocking_open);
+    device_set_simple_property(d_self, PROPERTY_NONBLOCKING_OPEN,
+	    &response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
     g_value_unset(&response);
 
     self->final_filemarks = 2;
@@ -308,6 +318,11 @@
 	    device_simple_property_get_fn,
 	    tape_device_set_feature_property_fn);
 
+    device_class_register_property(device_class, PROPERTY_NONBLOCKING_OPEN,
+	    PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
+	    device_simple_property_get_fn,
+	    tape_device_set_feature_property_fn);
+
     device_class_register_property(device_class, PROPERTY_FINAL_FILEMARKS,
 	    PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
 	    device_simple_property_get_fn,
@@ -374,6 +389,8 @@
 	self->eom = new_bool;
     else if (base->ID == PROPERTY_BSF_AFTER_EOM)
 	self->bsf_after_eom = new_bool;
+    else if (base->ID == PROPERTY_NONBLOCKING_OPEN)
+	self->nonblocking_open = new_bool;
     else
 	return FALSE; /* shouldn't happen */
 
@@ -495,6 +512,11 @@
                                       "bsf_after_eom",
       "Does this drive require an MTBSF after MTEOM in order to append?" );
 
+    device_property_fill_and_register(&device_property_nonblocking_open,
+                                      G_TYPE_BOOLEAN,
+                                      "nonblocking_open",
+      "Does this drive require a open with O_NONBLOCK?" );
+
     device_property_fill_and_register(&device_property_final_filemarks,
                                       G_TYPE_UINT, "final_filemarks",
       "How many filemarks to write after the last tape file?" );
@@ -503,28 +525,66 @@
     register_device(tape_device_factory, device_prefix_list);
 }
 
+/* Open the tape device, trying various combinations of O_RDWR and
+   O_NONBLOCK.  Returns -1 and calls device_set_error for errors
+   On Linux, with O_NONBLOCK, the kernel just checks the state once,
+   whereas it checks it every second for ST_BLOCK_SECONDS if O_NONBLOCK is
+   not given.  Amanda already have the code to poll, we want open to check
+   the state only once. */
+
 static int try_open_tape_device(TapeDevice * self, char * device_filename) {
     int fd;
     int save_errno;
     DeviceStatusFlags new_status;
 
-    fd = robust_open(device_filename, O_RDWR,0);
+#ifdef O_NONBLOCK
+    int nonblocking = 0;
+
+    if (self->nonblocking_open) {
+	nonblocking = O_NONBLOCK;
+    }
+#endif
+
+#ifdef O_NONBLOCK
+    fd  = robust_open(device_filename, O_RDWR | nonblocking, 0);
     save_errno = errno;
+    if (fd < 0 && nonblocking && (save_errno == EWOULDBLOCK || save_errno == EINVAL)) {
+        /* Maybe we don't support O_NONBLOCK for tape devices. */
+        fd = robust_open(device_filename, O_RDWR, 0);
+	save_errno = errno;
+    }
+#else
+    fd = robust_open(device_filename, O_RDWR, 0);
+    save_errno = errno;
+#endif
     if (fd >= 0) {
         self->write_open_errno = 0;
     } else {
-        if (errno == EACCES || errno == EPERM
-#ifdef EROFS
-			    || errno == EROFS
-#endif
-	   ) {
+        if (errno == EACCES || errno == EPERM) {
             /* Device is write-protected. */
             self->write_open_errno = errno;
-            fd = robust_open(device_filename, O_RDONLY,0);
+#ifdef O_NONBLOCK
+            fd = robust_open(device_filename, O_RDONLY | nonblocking, 0);
 	    save_errno = errno;
+            if (fd < 0 && nonblocking && (save_errno == EWOULDBLOCK || save_errno == EINVAL)) {
+                fd = robust_open(device_filename, O_RDONLY, 0);
+		save_errno = errno;
+            }
+#else
+            fd = robust_open(device_filename, O_RDONLY, 0);
+	    save_errno = errno;
+#endif
         }
     }
+#ifdef O_NONBLOCK
+    /* Clear O_NONBLOCK for operations from now on. */
+    if (fd >= 0 && nonblocking)
+	fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK);
+    errno = save_errno;
+    /* function continues after #endif */
 
+#endif /* O_NONBLOCK */
+
     if (fd < 0) {
 	DeviceStatusFlags status_flag = 0;
 	if (errno == EBUSY)
Index: device-src/tape-device.h
===================================================================
--- device-src/tape-device.h	(revision 2164)
+++ device-src/tape-device.h	(working copy)
@@ -98,6 +98,10 @@
 extern DevicePropertyBase device_property_bsf_after_eom;
 #define PROPERTY_BSF_AFTER_EOM (device_property_bsf_after_eom.ID)
 
+/* Should the device be opened with O_NONBLOCK */
+extern DevicePropertyBase device_property_nonblocking_open;
+#define PROPERTY_NONBLOCKING_OPEN (device_property_nonblocking_open.ID)
+
 /* How many filemarks to write at EOD? (Default is 2).
  * This property is a G_TYPE_UINT, but can only really be set to 1 or 2. */
 extern DevicePropertyBase device_property_final_filemarks;
Index: config/amanda/tape.m4
===================================================================
--- config/amanda/tape.m4	(revision 2164)
+++ config/amanda/tape.m4	(working copy)
@@ -184,4 +184,9 @@
 	],[
 	    AC_MSG_RESULT(no)
 	])
+
+    case "$target" in
+	*linux*) AC_DEFINE(DEFAULT_TAPE_NON_BLOCKING_OPEN,1,
+			[Define if open of tape device require O_NONBLOCK]);;
+    esac
 ])
