7 #include <IOKit/IOBSD.h>
8 #include <IOKit/IOKitLib.h>
9 #include <IOKit/storage/IOMedia.h>
12 extern void plog(const char *, ...);
15 * Given a uuid string, look up the BSD device and open it.
16 * This code comes from DanM.
18 * Essentially, it is given a UUID string (from the journal header),
19 * and then looks it up via IOKit. From there, it then gets the
20 * BSD name (e.g., /dev/dsik3), and opens it read-only.
22 * It returns the file descriptor, or -1 on error.
25 OpenDeviceByUUID(void *uuidp
, char **namep
)
27 char devname
[ MAXPATHLEN
];
28 CFStringRef devname_string
;
30 CFMutableDictionaryRef matching
;
32 uuid_string_t uuid_cstring
;
33 CFStringRef uuid_string
;
35 memcpy(&uuid_cstring
, uuidp
, sizeof(uuid_cstring
));
37 uuid_string
= CFStringCreateWithCString( kCFAllocatorDefault
, uuid_cstring
, kCFStringEncodingUTF8
);
39 matching
= IOServiceMatching( kIOMediaClass
);
41 CFDictionarySetValue( matching
, CFSTR( kIOMediaUUIDKey
), uuid_string
);
42 media
= IOServiceGetMatchingService( kIOMasterPortDefault
, matching
);
44 devname_string
= IORegistryEntryCreateCFProperty( media
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
45 if ( devname_string
) {
46 if ( CFStringGetCString( devname_string
, devname
, sizeof( devname
), kCFStringEncodingUTF8
) ) {
48 plog("external journal device name = `%s'\n", devname
);
50 fd
= opendev( devname
, O_RDONLY
, 0, NULL
);
51 if (fd
!= -1 && namep
!= NULL
) {
52 *namep
= strdup(devname
);
55 CFRelease( devname_string
);
57 IOObjectRelease( media
);
59 /* do not CFRelease( matching ); */
61 CFRelease( uuid_string
);