Received: from eggs.gnu.org ([2001:4830:134:3::10]:55561) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZFq2s-0006u1-JK for qemu-devel@nongnu.org; Thu, 16 Jul 2015 16:46:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZFq2l-0000xu-8h for qemu-devel@nongnu.org; Thu, 16 Jul 2015 16:46:18 -0400 From: Programmingkid Content-Type: multipart/alternative; boundary=Apple-Mail-14--789133287 Date: Thu, 16 Jul 2015 16:46:07 -0400 Message-Id: <99ECAD58-C9CC-4780-B63C-AF975AECCED6@gmail.com> Mime-Version: 1.0 (Apple Message framework v1084) Subject: [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: qemu-devel qemu-devel , Qemu-block Cc: Laurent Vivier , Peter Maydell , Stefan Hajnoczi --Apple-Mail-14--789133287 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Mac OS X can be picky when it comes to allowing the user to use physical = devices in QEMU. This patch fixes that issue by testing each physical device = first before using it in QEMU. If an issue is detected, a message is displayed showing the user how to unmount a volume.=20 Signed-off-by: John Arbuckle --- Removed volume unmounting code. Removed automatic remounting code. Displays helpful error message in place of remounting code. block/raw-posix.c | 115 = ++++++++++++++++++++++++++++++++++++++++------------ 1 files changed, 88 insertions(+), 27 deletions(-) diff --git a/block/raw-posix.c b/block/raw-posix.c index cbe6574..9de37ea 100644 --- a/block/raw-posix.c +++ b/block/raw-posix.c @@ -42,9 +42,8 @@ #include #include #include -//#include #include -#endif +#endif /* (__APPLE__) && (__MACH__) */ =20 #ifdef __sun__ #define _POSIX_PTHREAD_SEMANTICS 1 @@ -1972,8 +1971,9 @@ BlockDriver bdrv_file =3D { /* host device */ =20 #if defined(__APPLE__) && defined(__MACH__) -static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator = ); -static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char = *bsdPath, CFIndex maxPathSize ); +static kern_return_t FindEjectableCDMedia(io_iterator_t = *mediaIterator); +static kern_return_t GetBSDPath(io_iterator_t mediaIterator, char = *bsdPath, + CFIndex maxPathSize, int flags); =20 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator ) { @@ -2001,7 +2001,8 @@ kern_return_t FindEjectableCDMedia( io_iterator_t = *mediaIterator ) return kernResult; } =20 -kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, = CFIndex maxPathSize ) +kern_return_t GetBSDPath(io_iterator_t mediaIterator, char *bsdPath, + CFIndex maxPathSize, int flags) { io_object_t nextMedia; kern_return_t kernResult =3D KERN_FAILURE; @@ -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; @@ -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); + } + + /* 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); + snprintf(testPartition, MAXPATHLEN, "%ss%d", testPartition, = index); + fd =3D qemu_open(testPartition, O_RDONLY | O_BINARY | = O_LARGEFILE); + if (fd > 0) { + 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); +} + +#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); + if (mediaIterator) { + IOObjectRelease(mediaIterator); } + setupCDROM(bsdPath); filename =3D bsdPath; - qdict_put(options, "filename", qstring_from_str(filename)); } =20 - if ( mediaIterator ) - IOObjectRelease( mediaIterator ); + /* Setup any other physical device e.g. USB flash drive */ + else { + setupDevice(filename); + } + + qdict_put(options, "filename", qstring_from_str(filename)); } #endif =20 --=20 1.7.5.4 --Apple-Mail-14--789133287 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii
Mac OS = X can be picky when it comes to allowing the user to use physical = devices
in QEMU. This patch fixes that issue by testing = each physical device first
before using it in QEMU. If an issue = is detected, a message is displayed
showing the user how to unmount a = volume. 

Signed-off-by: John Arbuckle <programmingkidx@gmail.com>= ;

---
Removed volume unmounting = code.
Removed automatic remounting code.
 block/raw-posix.c |  115 = ++++++++++++++++++++++++++++++++++++++++------------

diff --git a/block/raw-posix.c = b/block/raw-posix.c
index cbe6574..9de37ea 100644
--- = a/block/raw-posix.c
+++ b/block/raw-posix.c
@@ = -42,9 +42,8 @@
 #include = <IOKit/storage/IOMediaBSDClient.h>
 #include = <IOKit/storage/IOMedia.h>
 #include = <IOKit/storage/IOCDMedia.h>
-//#include = <IOKit/storage/IOCDTypes.h>
 #include = <CoreFoundation/CoreFoundation.h>
-#endif
 

 #define _POSIX_PTHREAD_SEMANTICS 1
@@ = -1972,8 +1971,9 @@ BlockDriver bdrv_file =3D {
 

 #if defined(__APPLE__) = && defined(__MACH__)
-static kern_return_t = FindEjectableCDMedia( io_iterator_t *mediaIterator );
+static kern_return_t = FindEjectableCDMedia(io_iterator_t *mediaIterator);
+              =                   CFIndex = maxPathSize, int flags);

 

 kern_return_t = FindEjectableCDMedia( io_iterator_t *mediaIterator )
@@ -2001,7 +2001,8 @@ kern_return_t = FindEjectableCDMedia( io_iterator_t *mediaIterator )
 }

 

+kern_return_t GetBSDPath(io_iterator_t = mediaIterator, char *bsdPath,
+           =               CFIndex maxPathSize, = int flags)
 {
     io_object_t =     nextMedia;
     kern_return_t =   kernResult =3D KERN_FAILURE;
@@ -2014,7 +2015,9 @@ kern_return_t = GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex = ma
             = strcpy( bsdPath, _PATH_DEV );
-          =   strcat( bsdPath, "r" );
+          =   if (flags & BDRV_O_NOCACHE) {
+        =         strcat(bsdPath, "r");
             if = ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, = maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
@@ -2027,7 +2030,67 @@ kern_return_t = GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex = ma
 }

 

+/* Sets up a physical device for use in QEMU = */
+   /*
+    * Mac OS X does not = like allowing QEMU to use physical devices that are
+    */
+
+    int = fd;
+    fd =3D qemu_open(bsdPath, O_RDONLY | = O_BINARY | O_LARGEFILE);
+
+    /* if the device fails = to open */
+    if (fd < 0) {
+        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);
+    }
+    /* if the device opens */
+        = qemu_close(fd);
+    }
+}
+/* Sets up a real cdrom for use in QEMU = */
+{
+    /* look for a working partition = */
+        strncpy(testPartition, = bsdPath, MAXPATHLEN);
+        = snprintf(testPartition, MAXPATHLEN, "%ss%d", testPartition, = index);
+        fd =3D = qemu_open(testPartition, O_RDONLY | O_BINARY | O_LARGEFILE);
+            = 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);
+ =               "diskutil mountDisk = %s\n\n", bsdPath);
+    }
+
+    DPRINTF("Using = %s as CDROM\n", testPartition);
+    strncpy(bsdPath, = testPartition, MAXPATHLEN);
+}
+
+#endif /* defined(__APPLE__) = && defined(__MACH__) */

 

 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");

 

-    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 the physical = device is a cdrom */
+        if (strcmp(filename, = "/dev/cdrom") =3D=3D 0) {
+          =   char bsdPath[MAXPATHLEN];
+          =   io_iterator_t mediaIterator;
+          =   FindEjectableCDMedia(&mediaIterator);
+          =   if (mediaIterator) {
+          =       IOObjectRelease(mediaIterator);
             = filename =3D bsdPath;
-            = qdict_put(options, "filename", qstring_from_str(filename));
 

-        if ( = mediaIterator )
-            = IOObjectRelease( mediaIterator );
+        /* Setup = any other physical device e.g. USB flash drive */
+        =     setupDevice(filename);
+        = }
+        qdict_put(options, = "filename", qstring_from_str(filename));
     = }
 

-- 
1.7.5.4

= --Apple-Mail-14--789133287--