Created
October 25, 2022 14:07
-
-
Save amomchilov/7f53d9d7f2d20ef057be4a55871dc215 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
func getDriveBSDNames() -> [String] { | |
var iterator: io_iterator_t = 0 | |
let matching: CFDictionary = IOServiceMatching(kIOServicePlane) | |
// Use 'kIOMasterPortDefault' for macOS older than 12.0 Monterey | |
IOServiceGetMatchingServices(kIOMainPortDefault, matching, &iterator) | |
return sequence(state: iterator, next: { i in IOIteratorNext(i) }) | |
.prefix(while: { child in child > 0 }) | |
.compactMap { child in | |
guard let bsdNameAnyObject = IORegistryEntryCreateCFProperty( | |
child, "BSD Name" as CFString, | |
kCFAllocatorDefault, | |
IOOptionBits(kIORegistryIterateRecursively)) else { return nil } | |
guard let bsdNameString = bsdNameAnyObject.takeRetainedValue() as? String else { return nil } | |
guard bsdNameString.starts(with: "disk") else { return nil } | |
return bsdNameString | |
} | |
} | |
print(getDriveBSDNames()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment