xl create PV guest with qcow/qcow2 disk images fail
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)
tools/libxl/libxl.c +77 -3
@@ -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(lichar *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(libreak ;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 ; }
tools/libxl/libxl.h +1 -1
@@ -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);
tools/libxl/libxl_bootloader.c +1 -1
@@ -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 )
Stefano, could you review the revised patch and share your comments? Thanks.
Quoted 1 line
Quoted 1 line
Quoted 1 line
Chunyan Liu <[email protected]> 10/28/2011 9:27 PM >>>
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)
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +0 -0
@@ -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;
}tools/libxl/libxl.hFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.hThu Oct 20 15:24:46 2011 +0800 +0 -0
@@ -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);
tools/libxl/libxl_bootloader.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl_bootloader.cThu Oct 20 15:24:46 2011 +0800 +0 -0
@@ -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)_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Quoted 15 lines · Chun Yan Liu
Stefano, could you review the revised patch and share your comments? Thanks.
Quoted 1 line
Quoted 1 line
Quoted 1 line
Chunyan Liu <[email protected]> 10/28/2011 9:27 PM >>>
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 <[email protected]>diff -r
b4cf57bbc3fbtools/libxl/libxl.c
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 out_free: · line 1085
Quoted 9 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +6
-0@@ -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();
This needs to be libxl_fork, I think. Perhaps even this whole thing should be libxl__spawn_spawn (not sure about that)?
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 out_free: · line 1092
Quoted 7 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +7
-0+ if (pid < 0) + return -1; + else if (pid == 0){ + execvp(arg0, args); + exit(127); + } + sleep(1);
Why do you need this sleep?
Quoted 6 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +6
-0+ while (waitpid(pid, &status, 0) < 0) { + if (errno != EINTR) { + status = -1; + break; + } + }
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 out_free: · line 1108
Quoted 10 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +10
-0+ + 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};
"-r" perhaps?
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 out_free: · line 1112
Quoted 4 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +4
-0+ char *ret = NULL; + + for (i = 0; i < nbds_max; i++) { + nbd_dev = libxl__sprintf(gc, "/dev/nbd%d", i);
We can't get qemu-nbd to find a free device on our behalf and tell us what it was?
Quoted 51 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +19
-0+ 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){
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +7
-2@@ -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 attachingqdisk %s\n",
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 char * libxl_device_disk_local_attach(li · line 1196
Quoted 5 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +2
-0@@ -1135,11 +1192,13 @@ char * libxl_device_disk_local_attach(li out: if (dev != NULL) ret = strdup(dev); + if (mdev) + free(mdev);
free(NULL) is acceptable.
Quoted 10 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +1
-1libxl__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. */
This should be moved into the switch which you have added e.g.
case LIBXL_DISKBACK_END_PHY: /* nothing to do */ break;
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 int libxl_device_disk_local_detach(libxl · line 1207
Quoted 4 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +0
-0@@ -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.
should be another explicit case statement.
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 int libxl_device_disk_local_detach(libxl · line 1210
Quoted 3 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +2
-0*/ + libxl__gc gc = LIBXL_INIT_GC(ctx); + int ret;
Please declare these at the top of the function.
Quoted 53 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +4
-0+ switch (disk->backend) { + case LIBXL_DISK_BACKEND_QDISK: + if (disk->format != LIBXL_DISK_FORMAT_RAW) { + LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "Locally detach anon-raw "
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +9
-0+ "qdisk image"); + ret = nbd_unmount_disk(&gc, diskpath); + return ret; + } + default: + break; + } + + libxl__free_all(&gc); return 0;}
diff -r
b4cf57bbc3fbtools/libxl/libxl.htools/libxl/libxl.hFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.hThu Oct 20 15:24:46 2011 +0800 +1
-1@@ -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); intlibxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic);
diff -rb4cf57bbc3fbtools/libxl/libxl_bootloader.ctools/libxl/libxl_bootloader.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl_bootloader.cThu Oct 20 15:24:46 2011 +0800 +0
-0@@ -424,7 +424,7 @@ int libxl_run_bootloader(libxl_ctx *ctx, rc = 0;
out_close:if (diskpath) {tools/libxl/libxl_bootloader.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl_bootloader.cThu Oct 20 15:24:46 2011 +0800 +1
-1- libxl_device_disk_local_detach(ctx, disk); + libxl_device_disk_local_detach(ctx, disk, diskpath); free(diskpath); } if (fifo_fd > -1)_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Ian Campbell writes ("Re: [Xen-devel]xl create PV guest with qcow/qcow2 disk images fail"):
Quoted 5 lines · Chun Yan Liu
Quoted 1 line
+ pid =
fork();This needs to be
libxl_fork, I think. Perhaps even this whole thing should belibxl__spawn_spawn(not sure about that)?
libxl__spawn_spawn is for processes which are going to survive past the lifetime of the call to libxl. I think that for bootloaders this is going to be the case when we do event handling, so probably that's correct.
NB that you want the version from staging as that has important changes from Olaf regarding subprocesses.
Ian.
2011/11/2 Ian Campbell <[email protected]>
Quoted 12 lines · Chun Yan Liu
Quoted 11 lines
Stefano, could you review the revised patch and share your comments? Thanks.
Quoted 1 line
Quoted 1 line
Quoted 1 line
Chunyan Liu <[email protected]> 10/28/2011 9:27 PM >>>
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 <[email protected]>
Quoted 17 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 out_free: · line 1085
Quoted 13 lines
diff -r
b4cf57bbc3fbtools/libxl/libxl.ctools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +6
-0@@ -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();This needs to be
libxl_fork, I think. Perhaps even this whole thing should belibxl__spawn_spawn(not sure about that)?
I noticed that in libxl, some places use fork() and other places use libxl_fork(), device-model uses libxl__spawn_spwan. As for this place, I am not clear if fork() + execvp() might cause some problem? Or it is just considered better to use libxl_fork or libxl__spawn_spwan?
Quoted 11 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 out_free: · line 1092
Quoted 8 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +7
-0+ if (pid < 0) + return -1; + else if (pid == 0){ + execvp(arg0, args); + exit(127); + } + sleep(1);Why do you need this sleep?
I know it seems odd. But without sleep, after executing "qemu-nbd -c" here and passing the mount device node /dev/nbd* to fork_exec_bootloader(), pygrub fails to parse /dev/nbd*. I am not sure if /dev/nbd* is actually not prepared yet. But adding sleep() here or anywhere else before fork_exec_bootloader(), then there is no problem.
Quoted 10 lines
Quoted 10 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +10
-0+ while (waitpid(pid, &status, 0) < 0) { + if (errno != EINTR) { + status = -1; + break; + } + } + + return status; +} +
Quoted 8 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 out_free: · line 1108
Quoted 6 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +6
-0+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};"-r" perhaps?
To mount the qcow/qcow2 disk image to /dev/nbd* so that pygrub can parse kernel and initrd from it, "-c" is enough. Of course, we can add "-r".
Quoted 8 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 out_free: · line 1112
Quoted 4 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +4
-0+ char *ret = NULL; + + for (i = 0; i < nbds_max; i++) { + nbd_dev = libxl__sprintf(gc, "/dev/nbd%d", i);We can't get qemu-nbd to find a free device on our behalf and tell us what it was?
Well, it meant to do a thing that when "qemu-nbd -c /dev/nbd0 disk.img" fails, it can try next nbd device. I also noticed that qemu-nbd doesn't check if /dev/nbd* is free, even if /dev/nbd0 is already used, you can still issue "qemu-nbd -c /dev/nbd0 disk.img". Seems no better way to check that except "ps aux | grep /dev/nbd*". To choose a free nbd device and mount disk, maybe write a script to do that is more proper. And at this place, call that script.
Quoted 135 lines
+ args[2] =
libxl__sprintf(gc, "%s", nbd_dev);Quoted 50 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +18
-0+ 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){
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +7
-2@@ -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 attachingqdisk %s\n",
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 char * libxl_device_disk_local_attach(li · line 1196
Quoted 5 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +2
-0@@ -1135,11 +1192,13 @@ char * libxl_device_disk_local_attach(li out: if (dev != NULL) ret = strdup(dev); + if (mdev) + free(mdev);free(NULL) is acceptable.
Quoted 10 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +1
-1libxl__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. */This should be moved into the switch which you have added e.g.
case LIBXL_DISKBACK_END_PHY: /* nothing to do */ break;tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 int libxl_device_disk_local_detach(libxl · line 1207
Quoted 4 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +0
-0@@ -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.should be another explicit case statement.
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 int libxl_device_disk_local_detach(libxl · line 1210
Quoted 3 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +2
-0*/ + libxl__gc gc = LIBXL_INIT_GC(ctx); + int ret;Please declare these at the top of the function.
Quoted 47 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +4
-0+ switch (disk->backend) { + case LIBXL_DISK_BACKEND_QDISK: + if (disk->format != LIBXL_DISK_FORMAT_RAW) { + LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "Locally detach anon-raw "
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +9
-0+ "qdisk image"); + ret = nbd_unmount_disk(&gc, diskpath); + return ret; + } + default: + break; + } + + libxl__free_all(&gc); return 0;}
diff -r
b4cf57bbc3fbtools/libxl/libxl.htools/libxl/libxl.hFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.hThu Oct 20 15:24:46 2011 +0800 +1
-1@@ -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); intlibxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic);
diff -rb4cf57bbc3fbtools/libxl/libxl_bootloader.ctools/libxl/libxl_bootloader.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl_bootloader.cThu Oct 20 15:24:46 2011 +0800 +0
-0@@ -424,7 +424,7 @@ int libxl_run_bootloader(libxl_ctx *ctx, rc = 0;
out_close:if (diskpath) {tools/libxl/libxl_bootloader.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl_bootloader.cThu Oct 20 15:24:46 2011 +0800 +1
-1- libxl_device_disk_local_detach(ctx, disk); + libxl_device_disk_local_detach(ctx, disk, diskpath); free(diskpath); } if (fifo_fd > -1)
Quoted 9 lines
Quoted 6 lines
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
2011/11/4 Chunyan Liu <[email protected]>:
Quoted 7 lines
Quoted 2 lines
This needs to be
libxl_fork, I think. Perhaps even this whole thing should belibxl__spawn_spawn(not sure about that)?I noticed that in libxl, some places use
fork()and other places uselibxl_fork(), device-model useslibxl__spawn_spwan. As for this place, I am not clear iffork()+execvp()might cause some problem? Or it is just considered better to uselibxl_forkorlibxl__spawn_spwan?
If you are going to use fork to execute a file it might be best to use vfork + libxl__exec instead. I have proposed the addition of a new function in a patch to just do this (call vfork and libxl__exec), which is also interesting for the execution of hotplug scripts.
Quoted 8 lines · Roger Pau Monné
2011/11/4 Chunyan Liu <[email protected]>:
Quoted 7 lines
Quoted 2 lines
This needs to be
libxl_fork, I think. Perhaps even this whole thing should belibxl__spawn_spawn(not sure about that)?I noticed that in libxl, some places use
fork()and other places uselibxl_fork(), device-model useslibxl__spawn_spwan. As for this place, I am not clear iffork()+execvp()might cause some problem? Or it is just considered better to uselibxl_forkorlibxl__spawn_spwan?
vfork() no longer exists in the POSIX.1-2008 standard so you probably should not use it.
David
Quoted 1 line · Chun Yan Liu
Stefano, could you review the revised patch and share your comments? Thanks.
Sure.
Quoted 16 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Chunyan Liu <[email protected]> 10/28/2011 9:27 PM >>>
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 <[email protected]>diff -r
b4cf57bbc3fbtools/libxl/libxl.ctools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +3
-0@@ -1077,6 +1077,58 @@ out_free: Â Â Â Â libxl__free_all(&gc); Â Â Â Â return rc; } +static int fork_exec(char *arg0, char **args) +{ +Â Â Â pid_t pid;
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 out_free: · line 1092
Quoted 10 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +10
-0+Â Â Â int status; + +Â Â Â pid = fork(); +Â Â Â if (pid < 0) +Â Â Â Â Â Â Â return -1; +Â Â Â else if (pid == 0){ +Â Â Â Â Â Â Â execvp(arg0, args); +Â Â Â Â Â Â Â exit(127); +Â Â Â } +Â Â Â sleep(1);
In a following email you wrote that without the sleep the device is "not prepared yet".
What do you mean by that?
The device is present but reading/writing to it returns an error? If so, rather than a sleep we need an explicit wait for the device to be ready. Even trying to read from the device in a loop until it succeeds would be better than a sleep. At least we would know exactly what we are doing and why we are doing it.
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 out_free: · line 1100
Quoted 8 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +8
-0+Â Â Â while (waitpid(pid, &status, 0) < 0) { +Â Â Â Â Â Â Â if (errno != EINTR) { +Â Â Â Â Â Â Â Â Â Â Â status = -1; +Â Â Â Â Â Â Â Â Â Â Â break; +Â Â Â Â Â Â Â } +Â Â Â } +Â Â Â return status; +}
Here you are waiting for the death of qemu-nbd; I thought that qemu-nbd needs to be kept running?
Quoted 10 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +10
-0+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);
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 out_free: · line 1120
Quoted 10 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +10
-0+Â Â Â Â Â Â Â 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; +}
This is not great. I would read /proc/partitions instead. Also keep in mind that xl works on BSD now so at the very least you need to ifdef all the linux specific code.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
2011/11/7 Stefano Stabellini <[email protected]>:
Quoted 14 lines
Review
Quoted 13 lines
(quoted diff) +13
-0+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);
Quoted 8 lines
In a following email you wrote that without the sleep the device is "not prepared yet".
What do you mean by that?
The device is present but reading/writing to it returns an error? If so, rather than a sleep we need an explicit wait for the device to be ready. Even trying to read from the device in a loop until it succeeds would be better than a sleep. At least we would know exactly what we are doing and why we are doing it.
I really don't understand why a sleep is needed before executing waitpid, I would understand that you wait before accessing the device (although, as Stefano noticed, it's not the preferred way), but this wait should not be done here, in fact I think the fork_exec function should be added to libxl_exec.c and made public. I have a patch with a libxl__forkexec function prepared for submission, which might come in handy here.
Quoted 24 lines
Quoted 24 lines
+ while (
waitpid(pid, &status, 0)< 0) {
+ if (errno != EINTR) {
+ status = -1;
+ break;
+ }
+ }
+ return status;(quoted diff) +17
-0+} +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; + }
Quoted 8 lines
Review
Quoted 4 lines
+ }
(quoted diff) +3
-0+ + return ret; +}This is not great. I would read
/proc/partitionsinstead. Also keep in mind that xl works on BSD now so at the very least you need to ifdef all the linux specific code.
I would rather say that xl is closer to working on BSD now, but nbd is not supported on BSD (neither is qdisk), so anything related to nbd should be keept as Linux specific code.
Regards, Roger.
2011/11/7 Stefano Stabellini <[email protected]>
Quoted 30 lines · Chun Yan Liu
Quoted 1 line
Stefano, could you review the revised patch and share your comments?
Thanks.
Sure.
Quoted 2 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Chunyan Liu <[email protected]> 10/28/2011 9:27 PM >>>
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.
Quoted 21 lines
v2: use fork and exec instead of system(3)
Signed-off-by Chunyan Liu <[email protected]>diff -r
b4cf57bbc3fbtools/libxl/libxl.ctools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +10
-0@@ -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);
Quoted 9 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 out_free: · line 1092
Quoted 3 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +3
-0+ exit(127); + } + sleep(1);In a following email you wrote that without the sleep the device is "not prepared yet".
What do you mean by that?
The device is present but reading/writing to it returns an error?
If so, rather than a sleep we need an explicit wait for the device
Quoted 4 lines
to be ready. Even trying to read from the device in a loop until it succeeds would be better than a sleep. At least we would know exactly what we are doing and why we are doing it.
OK, after checking qemu-nbd source code, I think I know where the problem is. I tried to fork_exec "qemu-nbd -c /dev/nbd0 disk.img", with this command option, qemu-nbd will call daemon(3) to run in background and itself should exit immediately, that's why waitpid can successfully wait the exit of qemu-nbd pid and fork_exec will return 0. The problem is that I should not use fork_exec return value to decide if the disk.img is already connected to nbd device. That's not correct.
Quoted 29 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 out_free: · line 1100
Quoted 8 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +8
-0+ while (waitpid(pid, &status, 0) < 0) { + if (errno != EINTR) { + status = -1; + break; + } + } + return status; +}Here you are waiting for the death of qemu-nbd; I thought that qemu-nbd needs to be kept running?
Same as above.
Quoted 14 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +14
-0+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);
Quoted 9 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 out_free: · line 1120
Quoted 6 lines
tools/libxl/libxl.cFri Oct 28 20:50:36 2011 +0800 renamed from tools/libxl/libxl.cThu Oct 20 15:24:46 2011 +0800 +6
-0+ break; + } + } + + return ret; +}This is not great. I would read
/proc/partitionsinstead.
Thanks, that's a better way to find if a nbd device is free. The whole thing (find a free nbd device and connect disk.img to that nbd device) seems better to write a script to do that and in libxl call that script.
Quoted 3 lines
Also keep in mind that xl works on BSD now so at the very least you need to ifdef all the linux specific code.
My fault, thanks for reminding.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Quoted 11 lines · Chunyan Liu
In a following email you wrote that without the sleep the device is "not prepared yet".
What do you mean by that?
The device is present but reading/writing to it returns an error?If so, rather than a sleep we need an explicit wait for the device to be ready. Even trying to read from the device in a loop until it succeeds would be better than a sleep. At least we would know exactly what we are doing and why we are doing it.
Quoted 4 lines
OK, after checking qemu-nbd source code, I think I know where the problem is. I tried to
fork_exec"qemu-nbd -c/dev/nbd0disk.img", with this command option, qemu-nbd will call daemon(3) to run in background and itself should exit immediately, that's why waitpid can successfully wait the exit of qemu-nbd pid andfork_execwill return 0. The problem is that I should not usefork_execreturn value to decide if the disk.img is already connected to nbd device. That's not correct.
Good.
Quoted 4 lines
This is not great. I would read
/proc/partitionsinstead.Thanks, that's a better way to find if a nbd device is free. The whole thing (find a free nbd device and connect disk.img to that nbd device) seems better to write a script to do that and in libxl call that script.
Maybe, but I think it makes sense for it to be in libxl and after all reading/writing a file can be done quite well in a library too.
Quoted 12 lines · Stefano Stabellini
Quoted 11 lines · Chunyan Liu
In a following email you wrote that without the sleep the device is "not prepared yet".
What do you mean by that?
The device is present but reading/writing to it returns an error?If so, rather than a sleep we need an explicit wait for the device to be ready. Even trying to read from the device in a loop until it succeeds would be better than a sleep. At least we would know exactly what we are doing and why we are doing it.
Quoted 12 lines
Quoted 4 lines
OK, after checking qemu-nbd source code, I think I know where the problem is. I tried to
fork_exec"qemu-nbd -c/dev/nbd0disk.img", with this command option, qemu-nbd will call daemon(3) to run in background and itself should exit immediately, that's why waitpid can successfully wait the exit of qemu-nbd pid andfork_execwill return 0. The problem is that I should not usefork_execreturn value to decide if the disk.img is already connected to nbd device. That's not correct.Good.
Quoted 4 lines
This is not great. I would read
/proc/partitionsinstead.Thanks, that's a better way to find if a nbd device is free. The whole thing (find a free nbd device and connect disk.img to that nbd device) seems better to write a script to do that and in libxl call that script.
Really this is the sort of thing qemu-nbd should do for us (c.f. "losetup -f"). I think it would be worth getting such a patch upstream even if we can't yet make use of it.
Ian.
Quoted 8 lines
Maybe, but I think it makes sense for it to be in libxl and after all reading/writing a file can be done quite well in a library too.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel