diff --git a/tools/kvm/builtin-run.c b/tools/kvm/builtin-run.c
index 52e0190..cb48ead 100644
--- a/tools/kvm/builtin-run.c
+++ b/tools/kvm/builtin-run.c
@@ -58,7 +58,6 @@ struct kvm_cpu **kvm_cpus;
 __thread struct kvm_cpu *current_kvm_cpu;
 
 static int  kvm_run_wrapper;
-extern int  debug_iodelay;
 
 bool do_debug_print = false;
 
@@ -78,17 +77,11 @@ enum {
 	KVM_RUN_SANDBOX,
 };
 
-void kvm_run_set_wrapper_sandbox(void)
-{
-	kvm_run_wrapper = KVM_RUN_SANDBOX;
-}
-
 static int img_name_parser(const struct option *opt, const char *arg, int unset)
 {
 	char path[PATH_MAX];
-	const char *cur;
 	struct stat st;
-	char *sep;
+	struct kvm *kvm = opt->ptr;
 
 	if (stat(arg, &st) == 0 &&
 	    S_ISDIR(st.st_mode)) {
@@ -124,39 +117,12 @@ static int img_name_parser(const struct option *opt, const char *arg, int unset)
 		return 0;
 	}
 
-	if (kvm->cfg.image_count >= MAX_DISK_IMAGES)
-		die("Currently only 4 images are supported");
-
-	kvm->cfg.disk_image[kvm->cfg.image_count].filename = arg;
-	cur = arg;
-
-	if (strncmp(arg, "scsi:", 5) == 0) {
-		sep = strstr(arg, ":");
-		if (sep)
-			kvm->cfg.disk_image[kvm->cfg.image_count].wwpn = sep + 1;
-		sep = strstr(sep + 1, ":");
-		if (sep) {
-			*sep = 0;
-			kvm->cfg.disk_image[kvm->cfg.image_count].tpgt = sep + 1;
-		}
-		cur = sep + 1;
-	}
-
-	do {
-		sep = strstr(cur, ",");
-		if (sep) {
-			if (strncmp(sep + 1, "ro", 2) == 0)
-				kvm->cfg.disk_image[kvm->cfg.image_count].readonly = true;
-			else if (strncmp(sep + 1, "direct", 6) == 0)
-				kvm->cfg.disk_image[kvm->cfg.image_count].direct = true;
-			*sep = 0;
-			cur = sep + 1;
-		}
-	} while (sep);
-
-	kvm->cfg.image_count++;
+	return disk_img_name_parser(opt, arg, unset);
+}
 
-	return 0;
+void kvm_run_set_wrapper_sandbox(void)
+{
+	kvm_run_wrapper = KVM_RUN_SANDBOX;
 }
 
 static int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, int unset)
@@ -406,7 +372,7 @@ static int shmem_parser(const struct option *opt, const char *arg, int unset)
 	return 0;
 }
 
-#define BUILD_OPTIONS(name, cfg)					\
+#define BUILD_OPTIONS(name, cfg, kvm)					\
 	struct option name[] = {					\
 	OPT_GROUP("Basic options:"),					\
 	OPT_STRING('\0', "name", &(cfg)->guest_name, "guest name",	\
@@ -420,7 +386,7 @@ static int shmem_parser(const struct option *opt, const char *arg, int unset)
 		     shmem_parser, NULL),				\
 	OPT_CALLBACK('d', "disk", kvm, "image or rootfs_dir", "Disk 	\
 			image or rootfs directory", img_name_parser,	\
-			NULL),						\
+			kvm),						\
 	OPT_BOOLEAN('\0', "balloon", &(cfg)->balloon, "Enable virtio	\
 			balloon"),					\
 	OPT_BOOLEAN('\0', "vnc", &(cfg)->vnc, "Enable VNC framebuffer"),\
@@ -473,7 +439,7 @@ static int shmem_parser(const struct option *opt, const char *arg, int unset)
 			"Enable ioport debugging"),			\
 	OPT_BOOLEAN('\0', "debug-mmio", &(cfg)->mmio_debug,		\
 			"Enable MMIO debugging"),			\
-	OPT_INTEGER('\0', "debug-iodelay", &debug_iodelay,		\
+	OPT_INTEGER('\0', "debug-iodelay", &(cfg)->debug_iodelay,	\
 			"Delay IO by millisecond"),			\
 	OPT_END()							\
 	};
@@ -768,7 +734,7 @@ static const char *find_vmlinux(void)
 
 void kvm_run_help(void)
 {
-	BUILD_OPTIONS(options, &kvm->cfg);
+	BUILD_OPTIONS(options, &kvm->cfg, kvm);
 	usage_with_options(run_usage, options);
 }
 
@@ -927,7 +893,7 @@ static int kvm_cmd_run_init(int argc, const char **argv)
 	kvm->cfg.custom_rootfs_name = "default";
 
 	while (argc != 0) {
-		BUILD_OPTIONS(options, &kvm->cfg);
+		BUILD_OPTIONS(options, &kvm->cfg, kvm);
 		argc = parse_options(argc, argv, options, run_usage,
 				PARSE_OPT_STOP_AT_NON_OPTION |
 				PARSE_OPT_KEEP_DASHDASH);
@@ -969,6 +935,8 @@ static int kvm_cmd_run_init(int argc, const char **argv)
 
 	}
 
+	kvm->nr_disks = kvm->cfg.image_count;
+
 	if (!kvm->cfg.kernel_filename)
 		kvm->cfg.kernel_filename = find_kernel();
 
@@ -1130,15 +1098,10 @@ static int kvm_cmd_run_init(int argc, const char **argv)
 		strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline));
 	}
 
-	if (kvm->cfg.image_count) {
-		kvm->nr_disks = kvm->cfg.image_count;
-		kvm->disks = disk_image__open_all((struct disk_image_params *)&kvm->cfg.disk_image, kvm->cfg.image_count);
-		if (IS_ERR(kvm->disks)) {
-			r = PTR_ERR(kvm->disks);
-			pr_err("disk_image__open_all() failed with error %ld\n",
-					PTR_ERR(kvm->disks));
-			goto fail;
-		}
+	r = disk_image__init(kvm);
+	if (r < 0) {
+		pr_err("disk_image__init() failed with error %d\n", r);
+		goto fail;
 	}
 
 	printf("  # %s run -k %s -m %Lu -c %d --name %s\n", KVM_BINARY_NAME,
@@ -1342,9 +1305,9 @@ static void kvm_cmd_run_exit(int guest_ret)
 	if (r < 0)
 		pr_warning("virtio_rng__exit() failed with error %d\n", r);
 
-	r = disk_image__close_all(kvm->disks, kvm->cfg.image_count);
+	r = disk_image__exit(kvm);
 	if (r < 0)
-		pr_warning("disk_image__close_all() failed with error %d\n", r);
+		pr_warning("disk_image__exit() failed with error %d\n", r);
 
 	r = serial8250__exit(kvm);
 	if (r < 0)
diff --git a/tools/kvm/disk/core.c b/tools/kvm/disk/core.c
index f7e2c7f..639b7b3 100644
--- a/tools/kvm/disk/core.c
+++ b/tools/kvm/disk/core.c
@@ -1,6 +1,7 @@
 #include "kvm/disk-image.h"
 #include "kvm/qcow.h"
 #include "kvm/virtio-blk.h"
+#include "kvm/kvm.h"
 
 #include <linux/err.h>
 #include <sys/eventfd.h>
@@ -10,6 +11,49 @@
 
 int debug_iodelay;
 
+static int disk_image__close(struct disk_image *disk);
+
+int disk_img_name_parser(const struct option *opt, const char *arg, int unset)
+{
+	const char *cur;
+	char *sep;
+	struct kvm *kvm = opt->ptr;
+
+	if (kvm->cfg.image_count >= MAX_DISK_IMAGES)
+		die("Currently only 4 images are supported");
+
+	kvm->cfg.disk_image[kvm->cfg.image_count].filename = arg;
+	cur = arg;
+
+	if (strncmp(arg, "scsi:", 5) == 0) {
+		sep = strstr(arg, ":");
+		if (sep)
+			kvm->cfg.disk_image[kvm->cfg.image_count].wwpn = sep + 1;
+		sep = strstr(sep + 1, ":");
+		if (sep) {
+			*sep = 0;
+			kvm->cfg.disk_image[kvm->cfg.image_count].tpgt = sep + 1;
+		}
+		cur = sep + 1;
+	}
+
+	do {
+		sep = strstr(cur, ",");
+		if (sep) {
+			if (strncmp(sep + 1, "ro", 2) == 0)
+				kvm->cfg.disk_image[kvm->cfg.image_count].readonly = true;
+			else if (strncmp(sep + 1, "direct", 6) == 0)
+				kvm->cfg.disk_image[kvm->cfg.image_count].direct = true;
+			*sep = 0;
+			cur = sep + 1;
+		}
+	} while (sep);
+
+	kvm->cfg.image_count++;
+
+	return 0;
+}
+
 #ifdef CONFIG_HAS_AIO
 static void *disk_image__thread(void *param)
 {
@@ -75,7 +119,7 @@ struct disk_image *disk_image__new(int fd, u64 size,
 	return disk;
 }
 
-struct disk_image *disk_image__open(const char *filename, bool readonly, bool direct)
+static struct disk_image *disk_image__open(const char *filename, bool readonly, bool direct)
 {
 	struct disk_image *disk;
 	struct stat st;
@@ -118,7 +162,7 @@ struct disk_image *disk_image__open(const char *filename, bool readonly, bool di
 	return ERR_PTR(-ENOSYS);
 }
 
-struct disk_image **disk_image__open_all(struct disk_image_params *params, int count)
+static struct disk_image **disk_image__open_all(struct kvm *kvm)
 {
 	struct disk_image **disks;
 	const char *filename;
@@ -128,6 +172,8 @@ struct disk_image **disk_image__open_all(struct disk_image_params *params, int c
 	bool direct;
 	void *err;
 	int i;
+	struct disk_image_params *params = (struct disk_image_params *)&kvm->cfg.disk_image;
+	int count = kvm->cfg.image_count;
 
 	if (!count)
 		return ERR_PTR(-EINVAL);
@@ -163,6 +209,7 @@ struct disk_image **disk_image__open_all(struct disk_image_params *params, int c
 			err = disks[i];
 			goto error;
 		}
+		disks[i]->debug_iodelay = kvm->cfg.debug_iodelay;
 	}
 
 	return disks;
@@ -183,7 +230,7 @@ int disk_image__flush(struct disk_image *disk)
 	return fsync(disk->fd);
 }
 
-int disk_image__close(struct disk_image *disk)
+static int disk_image__close(struct disk_image *disk)
 {
 	/* If there was no disk image then there's nothing to do: */
 	if (!disk)
@@ -200,7 +247,7 @@ int disk_image__close(struct disk_image *disk)
 	return 0;
 }
 
-int disk_image__close_all(struct disk_image **disks, int count)
+static int disk_image__close_all(struct disk_image **disks, int count)
 {
 	while (count)
 		disk_image__close(disks[--count]);
@@ -287,3 +334,19 @@ void disk_image__set_callback(struct disk_image *disk,
 {
 	disk->disk_req_cb = disk_req_cb;
 }
+
+int disk_image__init(struct kvm *kvm)
+{
+	if (kvm->cfg.image_count) {
+		kvm->disks = disk_image__open_all(kvm);
+		if (IS_ERR(kvm->disks))
+			return PTR_ERR(kvm->disks);
+	}
+
+	return 0;
+}
+
+int disk_image__exit(struct kvm *kvm)
+{
+	return disk_image__close_all(kvm->disks, kvm->nr_disks);
+}
diff --git a/tools/kvm/include/kvm/disk-image.h b/tools/kvm/include/kvm/disk-image.h
index 83fc725..b728052 100644
--- a/tools/kvm/include/kvm/disk-image.h
+++ b/tools/kvm/include/kvm/disk-image.h
@@ -3,6 +3,7 @@
 
 #include "kvm/read-write.h"
 #include "kvm/util.h"
+#include "kvm/parse-options.h"
 
 #include <linux/types.h>
 #include <linux/fs.h>	/* for BLKGETSIZE64 */
@@ -65,13 +66,13 @@ struct disk_image {
 #endif
 	const char			*wwpn;
 	const char			*tpgt;
+	int				debug_iodelay;
 };
 
-struct disk_image *disk_image__open(const char *filename, bool readonly, bool direct);
-struct disk_image **disk_image__open_all(struct disk_image_params *params, int count);
+int disk_img_name_parser(const struct option *opt, const char *arg, int unset);
+int disk_image__init(struct kvm *kvm);
+int disk_image__exit(struct kvm *kvm);
 struct disk_image *disk_image__new(int fd, u64 size, struct disk_image_operations *ops, int mmap);
-int disk_image__close(struct disk_image *disk);
-int disk_image__close_all(struct disk_image **disks, int count);
 int disk_image__flush(struct disk_image *disk);
 ssize_t disk_image__read(struct disk_image *disk, u64 sector, const struct iovec *iov,
 				int iovcount, void *param);
diff --git a/tools/kvm/include/kvm/kvm-config.h b/tools/kvm/include/kvm/kvm-config.h
index fd7a5cd..35cde48 100644
--- a/tools/kvm/include/kvm/kvm-config.h
+++ b/tools/kvm/include/kvm/kvm-config.h
@@ -23,6 +23,7 @@ struct kvm_config {
 	u8 num_net_devices;
 	bool virtio_rng;
 	int active_console;
+	int debug_iodelay;
 	const char *kernel_cmdline;
 	const char *kernel_filename;
 	const char *vmlinux_filename;
