All of lore.kernel.org
 help / color / mirror / Atom feed
* Re: xl create PV guest with qcow/qcow2 disk images fail
@ 2011-10-28 13:27 Chunyan Liu
  2011-11-02  6:58 ` Chun Yan Liu
  0 siblings, 1 reply; 23+ messages in thread
From: Chunyan Liu @ 2011-10-28 13:27 UTC (permalink / raw)
  To: stefano.stabellini, xen-devel

Start qemu-nbd to mount non-raw qdisk in dom0 so that xl can create PV guest with qcow/qcow2 disk image and using pygrub.
v2: use fork and exec instead of system(3)

Signed-off-by: Chunyan Liu <cyliu@suse.com>

diff -r b4cf57bbc3fb tools/libxl/libxl.c
--- a/tools/libxl/libxl.c	Thu Oct 20 15:24:46 2011 +0800
+++ b/tools/libxl/libxl.c	Fri Oct 28 20:50:36 2011 +0800
@@ -1077,6 +1077,58 @@ out_free:
     libxl__free_all(&gc);
     return rc;
 }
+static int fork_exec(char *arg0, char **args)
+{
+    pid_t pid;
+    int status;
+
+    pid = fork();
+    if (pid < 0)
+        return -1;
+    else if (pid == 0){
+        execvp(arg0, args);
+        exit(127);
+    }
+    sleep(1);    
+    while (waitpid(pid, &status, 0) < 0) {
+        if (errno != EINTR) {
+            status = -1;
+            break;
+        }
+    }
+
+    return status;
+}
+
+static char * nbd_mount_disk(libxl__gc *gc, libxl_device_disk *disk)
+{
+    int i;
+    int nbds_max = 16;
+    char *nbd_dev = NULL;
+    char *args[] = {"qemu-nbd","-c",NULL,NULL,NULL};
+    char *ret = NULL;
+
+    for (i = 0; i < nbds_max; i++) {
+        nbd_dev = libxl__sprintf(gc, "/dev/nbd%d", i);
+        args[2] = libxl__sprintf(gc, "%s", nbd_dev);
+        args[3] = libxl__sprintf(gc, "%s", disk->pdev_path);
+        if (fork_exec(args[0], args) == 0) {
+            ret = strdup(nbd_dev);
+            break;
+        }
+    }
+
+    return ret;
+}
+
+static int nbd_unmount_disk(libxl__gc *gc, char *diskpath) {
+    char *args[] = {"qemu-nbd","-d",NULL,NULL};
+    args[2] = libxl__sprintf(gc, "%s", diskpath);
+    if (fork_exec(args[0], args))
+        return 0;
+    else
+        return ERROR_FAIL;
+}
 
 char * libxl_device_disk_local_attach(libxl_ctx *ctx, libxl_device_disk *disk)
 {
@@ -1084,6 +1136,7 @@ char * libxl_device_disk_local_attach(li
     char *dev = NULL;
     char *ret = NULL;
     int rc;
+    char *mdev = NULL;
 
     rc = libxl__device_disk_set_backend(&gc, disk);
     if (rc) goto out;
@@ -1118,8 +1171,12 @@ char * libxl_device_disk_local_attach(li
             break;
         case LIBXL_DISK_BACKEND_QDISK:
             if (disk->format != LIBXL_DISK_FORMAT_RAW) {
-                LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "cannot locally"
-                           " attach a qdisk image if the format is not raw");
+                LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "attaching a non-raw qdisk image to domain 0\n");
+                mdev = nbd_mount_disk(&gc, disk);
+                if (mdev)
+                    dev = mdev;
+                else
+                    LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "fail to mount image with qemu-nbd");
                 break;
             }
             LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "locally attaching qdisk %s\n",
@@ -1135,11 +1192,13 @@ char * libxl_device_disk_local_attach(li
  out:
     if (dev != NULL)
         ret = strdup(dev);
+    if (mdev)
+        free(mdev);
     libxl__free_all(&gc);
     return ret;
 }
 
-int libxl_device_disk_local_detach(libxl_ctx *ctx, libxl_device_disk *disk)
+int libxl_device_disk_local_detach(libxl_ctx *ctx, libxl_device_disk *disk, char *diskpath)
 {
     /* Nothing to do for PHYSTYPE_PHY. */
 
@@ -1147,7 +1206,22 @@ int libxl_device_disk_local_detach(libxl
      * For other device types assume that the blktap2 process is
      * needed by the soon to be started domain and do nothing.
      */
+    libxl__gc gc = LIBXL_INIT_GC(ctx);
+    int ret;
 
+    switch (disk->backend) {
+        case LIBXL_DISK_BACKEND_QDISK:
+            if (disk->format != LIBXL_DISK_FORMAT_RAW) {
+                LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "Locally detach a non-raw "
+                    "qdisk image");
+                ret = nbd_unmount_disk(&gc, diskpath);
+                return ret;
+            }
+        default:
+            break;
+    }
+
+    libxl__free_all(&gc);
     return 0;
 }
 
diff -r b4cf57bbc3fb tools/libxl/libxl.h
--- a/tools/libxl/libxl.h	Thu Oct 20 15:24:46 2011 +0800
+++ b/tools/libxl/libxl.h	Fri Oct 28 20:50:36 2011 +0800
@@ -390,7 +390,7 @@ int libxl_cdrom_insert(libxl_ctx *ctx, u
  * Make a disk available in this domain. Returns path to a device.
  */
 char * libxl_device_disk_local_attach(libxl_ctx *ctx, libxl_device_disk *disk);
-int libxl_device_disk_local_detach(libxl_ctx *ctx, libxl_device_disk *disk);
+int libxl_device_disk_local_detach(libxl_ctx *ctx, libxl_device_disk *disk, char *diskpath);
 
 int libxl_device_nic_init(libxl_device_nic *nic, int dev_num);
 int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic);
diff -r b4cf57bbc3fb tools/libxl/libxl_bootloader.c
--- a/tools/libxl/libxl_bootloader.c	Thu Oct 20 15:24:46 2011 +0800
+++ b/tools/libxl/libxl_bootloader.c	Fri Oct 28 20:50:36 2011 +0800
@@ -424,7 +424,7 @@ int libxl_run_bootloader(libxl_ctx *ctx,
     rc = 0;
 out_close:
     if (diskpath) {
-        libxl_device_disk_local_detach(ctx, disk);
+        libxl_device_disk_local_detach(ctx, disk, diskpath);
         free(diskpath);
     }
     if (fifo_fd > -1)

^ permalink raw reply	[flat|nested] 23+ messages in thread
* xl create PV guest with qcow/qcow2 disk images fail
@ 2011-10-14  8:25 Chun Yan Liu
  2011-10-17 10:54 ` Dario Faggioli
                   ` (2 more replies)
  0 siblings, 3 replies; 23+ messages in thread
From: Chun Yan Liu @ 2011-10-14  8:25 UTC (permalink / raw)
  To: xen-devel


[-- Attachment #1.1: Type: text/plain, Size: 645 bytes --]

Hi, List,

I'm trying xl create a pv guest with qcow/qcow2 image, it always fails at libxl_device_disk_local_attach. 
#xl create pv_config_file
libxl: error: libxl.c:1119:libxl_device_disk_local_attach: cannot locally attach a qdisk image if the format is not raw
libxl: error: libxl_create.c:467:do_domain_create: failed to run bootloader: -3

disk configuration is:
disk=[ 'tap:qcow2:/var/lib/xen/images/sles11pv/disk0.qcow2,xvda,w', ]

Is there any way to make it work?

Thanks,
_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xensource.com
http://lists.xensource.com/xen-devel





[-- Attachment #1.2: Type: text/html, Size: 993 bytes --]

[-- Attachment #2: Type: text/plain, Size: 138 bytes --]

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xensource.com
http://lists.xensource.com/xen-devel

^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2011-11-09  9:36 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-10-28 13:27 xl create PV guest with qcow/qcow2 disk images fail Chunyan Liu
2011-11-02  6:58 ` Chun Yan Liu
2011-11-02 12:59   ` Ian Campbell
2011-11-02 16:45     ` Ian Jackson
2011-11-04  6:40     ` Chunyan Liu
2011-11-04  9:14       ` Roger Pau Monné
2011-11-04 11:45         ` David Vrabel
2011-11-07 14:16   ` Stefano Stabellini
2011-11-07 14:53     ` Roger Pau Monné
2011-11-09  8:54     ` Chunyan Liu
2011-11-09  9:27       ` Stefano Stabellini
2011-11-09  9:36         ` Ian Campbell
  -- strict thread matches above, loose matches on Subject: below --
2011-10-14  8:25 Chun Yan Liu
2011-10-17 10:54 ` Dario Faggioli
2011-10-17 10:59 ` Ian Campbell
2011-10-19 10:52 ` Stefano Stabellini
2011-10-19 10:55   ` Stefano Stabellini
2011-10-19 13:34     ` Fajar A. Nugraha
2011-10-19 13:40       ` Stefano Stabellini
2011-10-27  3:08         ` Chun Yan Liu
2011-10-27  3:08         ` Chun Yan Liu
2011-10-27  9:58           ` Stefano Stabellini
2011-10-27 13:11           ` Ian Campbell

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.