Received: from eggs.gnu.org ([2001:4830:134:3::10]:52666) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZGBFT-0004Ws-3w for qemu-devel@nongnu.org; Fri, 17 Jul 2015 15:24:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZGBFP-0001UX-Oj for qemu-devel@nongnu.org; Fri, 17 Jul 2015 15:24:43 -0400 Mime-Version: 1.0 (Apple Message framework v1084) Content-Type: text/plain; charset=us-ascii From: Programmingkid In-Reply-To: <20150717134101.GE6572@stefanha-thinkpad.redhat.com> Date: Fri, 17 Jul 2015 15:24:34 -0400 Content-Transfer-Encoding: quoted-printable Message-Id: <6226AC11-BD4A-4FC2-90DD-CA40305FFC9A@gmail.com> References: <99ECAD58-C9CC-4780-B63C-AF975AECCED6@gmail.com> <20150717134101.GE6572@stefanha-thinkpad.redhat.com> Subject: Re: [Qemu-devel] [PATCH v2] raw-posix.c: Make physical devices usable in QEMU under Mac OS X host List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Stefan Hajnoczi Cc: Laurent Vivier , Peter Maydell , qemu-devel qemu-devel , Qemu-block On Jul 17, 2015, at 9:41 AM, Stefan Hajnoczi wrote: > On Thu, Jul 16, 2015 at 04:46:07PM -0400, Programmingkid wrote: >> @@ -2014,7 +2015,9 @@ kern_return_t GetBSDPath( io_iterator_t = mediaIterator, char *bsdPath, CFIndex ma >> if ( bsdPathAsCFString ) { >> size_t devPathLength; >> strcpy( bsdPath, _PATH_DEV ); >> - strcat( bsdPath, "r" ); >> + if (flags & BDRV_O_NOCACHE) { >> + strcat(bsdPath, "r"); >> + } >> devPathLength =3D strlen( bsdPath ); >> if ( CFStringGetCString( bsdPathAsCFString, bsdPath + = devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) { >> kernResult =3D KERN_SUCCESS; >=20 > Is this the fix that makes CD-ROM passthrough work for you? >=20 > Does the guest boot successfully when you do: >=20 > -drive if=3Dide,media=3Dcdrom,cache=3Dnone,file=3D/dev/cdrom The guest fails during the boot process with the above command line.=20 >> @@ -2027,7 +2030,67 @@ kern_return_t GetBSDPath( io_iterator_t = mediaIterator, char *bsdPath, CFIndex ma >> return kernResult; >> } >>=20 >> -#endif >> +/* Sets up a physical device for use in QEMU */ >> +static void setupDevice(const char *bsdPath) >> +{ >> + /* >> + * Mac OS X does not like allowing QEMU to use physical devices = that are >> + * mounted. Attempts to do so result in 'Resource busy' errors. >> + */ >> + >> + int fd; >> + fd =3D qemu_open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE); >> + >> + /* if the device fails to open */ >> + if (fd < 0) { >> + printf("Error: failed to open %s\n", bsdPath); >> + printf("If device %s is mounted on the desktop, unmount it" >> + " first before using it in QEMU.\n", bsdPath); >> + printf("\nCommand to unmount device: diskutil unmountDisk = %s", bsdPath); >> + printf("\nCommand to mount device: diskutil mountDisk = %s\n\n", bsdPath); >> + } >=20 > This error message is printed regardless of the errno value. What is > the specific errno value when open(2) fails because the device is > mounted? I could change the patch to take into account the errno value.=20 errno =3D 2 when it fails. strerror(errno) =3D "No such file or = directory".=20 >=20 > Also, can you move this after the raw_open_common() call to avoid the > setupCDROM()/setupDevice() changes made in this patch and duplicating > the error message. It doesn't seem necessary to open the files ahead = of > raw_open_common() since the code continues in the error case anyway: >=20 > ret =3D raw_open_common(bs, options, flags, 0, &local_err); > if (ret < 0) { > if (local_err) { > error_propagate(errp, local_err); > } > #if defined(__APPLE__) && defined(__MACH__) > if (strstart(filename, "/dev/") =3D=3D 0 && ret =3D=3D -EBUSY) = { /* or whatever */ > error_report("If device %s is mounted on the desktop, = unmount it" > " first before using it in QEMU.", bsdPath); > error_report("Command to unmount device: diskutil = unmountDisk %s", bsdPath); > error_report("Command to mount device: diskutil mountDisk = %s", bsdPath); > } > #endif /* defined(__APPLE__) && defined(__MACH__) */ > return ret; > } >=20 > That's a much smaller change. I will see what I can do.=20 >=20 >> + >> + /* if the device opens */ >> + else { >> + qemu_close(fd); >> + } >> +} >> + >> +/* Sets up a real cdrom for use in QEMU */ >> +static void setupCDROM(char *bsdPath) >> +{ >> + int index, numOfTestPartitions =3D 2, fd; >> + char testPartition[MAXPATHLEN]; >> + bool partitionFound =3D false; >> + >> + /* look for a working partition */ >> + for (index =3D 0; index < numOfTestPartitions; index++) { >> + strncpy(testPartition, bsdPath, MAXPATHLEN); >=20 > The safe way to use strncpy() is: >=20 > strncpy(testPartition, bsdPath, MAXPATHLEN - 1); > testPartition[MAXPATHLEN - 1] =3D '\0'; >=20 > but pstrcpy() is a easier to use correctly. Please use that instead. Is pstrcpy() ansi c? I'm having trouble finding documentation for it.=20 >=20 >> + snprintf(testPartition, MAXPATHLEN, "%ss%d", testPartition, = index); >> + fd =3D qemu_open(testPartition, O_RDONLY | O_BINARY | = O_LARGEFILE); >> + if (fd > 0) { >=20 > Should be fd >=3D 0 since fd =3D 0 is valid too, although unlikely. >=20 >> + partitionFound =3D true; >> + qemu_close(fd); >> + break; >> + } >> + } >> + >> + /* if a working partition on the device was not found */ >> + if (partitionFound =3D=3D false) { >> + printf("Error: Failed to find a working partition on = disc!\n"); >> + printf("If your disc is mounted on the desktop, trying = unmounting it" >> + " first before using it in QEMU.\n"); >> + printf("\nCommand to unmount disc: " >> + "diskutil unmountDisk %s\n", bsdPath); >> + printf("Command to mount disc: " >> + "diskutil mountDisk %s\n\n", bsdPath); >> + } >> + >> + DPRINTF("Using %s as CDROM\n", testPartition); >> + strncpy(bsdPath, testPartition, MAXPATHLEN); >=20 > Please use pstrcpy(). >=20 >> +} >> + >> +#endif /* defined(__APPLE__) && defined(__MACH__) */ >>=20 >> static int hdev_probe_device(const char *filename) >> { >> @@ -2119,30 +2182,28 @@ static int hdev_open(BlockDriverState *bs, = QDict *options, int flags, >> #if defined(__APPLE__) && defined(__MACH__) >> const char *filename =3D qdict_get_str(options, "filename"); >>=20 >> - if (strstart(filename, "/dev/cdrom", NULL)) { >> - kern_return_t kernResult; >> - io_iterator_t mediaIterator; >> - char bsdPath[ MAXPATHLEN ]; >> - int fd; >> - >> - kernResult =3D FindEjectableCDMedia( &mediaIterator ); >> - kernResult =3D GetBSDPath( mediaIterator, bsdPath, sizeof( = bsdPath ) ); >> - >> - if ( bsdPath[ 0 ] !=3D '\0' ) { >> - strcat(bsdPath,"s0"); >> - /* some CDs don't have a partition 0 */ >> - fd =3D qemu_open(bsdPath, O_RDONLY | O_BINARY | = O_LARGEFILE); >> - if (fd < 0) { >> - bsdPath[strlen(bsdPath)-1] =3D '1'; >> - } else { >> - qemu_close(fd); >> + /* If using a physical device */ >> + if (strstart(filename, "/dev/", NULL)) { >> + >> + /* If the physical device is a cdrom */ >> + if (strcmp(filename, "/dev/cdrom") =3D=3D 0) { >> + char bsdPath[MAXPATHLEN]; >> + io_iterator_t mediaIterator; >> + FindEjectableCDMedia(&mediaIterator); >> + GetBSDPath(mediaIterator, bsdPath, sizeof(bsdPath), = flags); >=20 > Why did you remove the if (bsdPath[0] !=3D [0]) check? Now the code > ignored GetBSDPath() failures. Ok, I will put that back in.=20 >=20 >> + if (mediaIterator) { >> + IOObjectRelease(mediaIterator); >> } >> + setupCDROM(bsdPath); >> filename =3D bsdPath; >> - qdict_put(options, "filename", = qstring_from_str(filename)); >> } >=20 > bsdPath is out of scope here so filename is a dangling pointer in the > /dev/cdrom case. Good catch. Will fix that.=20