+ mlockall(MCL_CURRENT | MCL_FUTURE);
+}
+
+void usage(char* errmsg, ...) {
+ if(errmsg) {
+ char tmp[256];
+ va_list ap;
+ va_start(ap, errmsg);
+ snprintf(tmp, 256, "ERROR: %s\n\n", errmsg);
+ vfprintf(stderr, errmsg, ap);
+ va_end(ap);
+ } else {
+ fprintf(stderr, "nbd-client version %s\n", PACKAGE_VERSION);
+ }
+ fprintf(stderr, "Usage: nbd-client host port nbd_device [-block-size|-b block size] [-timeout|-t timeout] [-swap|-s] [-sdp|-S] [-persist|-p] [-nofork|-n] [-name|-N name]\n");
+ fprintf(stderr, "Or : nbd-client -d nbd_device\n");
+ fprintf(stderr, "Or : nbd-client -c nbd_device\n");
+ fprintf(stderr, "Or : nbd-client -h|--help\n");
+ fprintf(stderr, "Default value for blocksize is 1024 (recommended for ethernet)\n");
+ fprintf(stderr, "Allowed values for blocksize are 512,1024,2048,4096\n"); /* will be checked in kernel :) */
+ fprintf(stderr, "Note, that kernel 2.4.2 and older ones do not work correctly with\n");
+ fprintf(stderr, "blocksizes other than 1024 without patches\n");
+}
+
+void disconnect(char* device) {
+ int nbd = open(device, O_RDWR);
+
+ if (nbd < 0)
+ err("Cannot open NBD: %m\nPlease ensure the 'nbd' module is loaded.");
+ printf("Disconnecting: que, ");
+ if (ioctl(nbd, NBD_CLEAR_QUE)< 0)
+ err("Ioctl failed: %m\n");
+ printf("disconnect, ");
+#ifdef NBD_DISCONNECT
+ if (ioctl(nbd, NBD_DISCONNECT)<0)
+ err("Ioctl failed: %m\n");
+ printf("sock, ");