I am trying to figure out how I can get some additional disk properties on macOS using Swift. I am especially interested in the type of disk (like SSD, HDD, Optical).
I am getting a list of mounted volumes using the following code:
FileManager.default.mountedVolumeURLs(includingResourceValuesForKeys: nil)
I am then iterating over all volumes and get additional properties like total disk space using property keys:
for volumeUrl in mountedVolumeURLs {
if let values = try? volumeUrl.resourceValues(forKeys: [.volumeTotalCapacityKey, .volumeNameKey, .volumeIsInternalKey] {
// Do something
}
}
I could not find a resource key to get the type. I then saw that there is an additional framework called DiskArbitration
.
I used to following code to get the BSD Name in hope to find the type using IOReg, but this did not help me either (I am only using this code to get the bad name, I think the DiskArbitration is for unmounting / mounting volumes only).
if let session = DASessionCreate(kCFAllocatorDefault)
{
let mountedVolumeURLs = FileManager.default.mountedVolumeURLs(includingResourceValuesForKeys: nil)!
for volumeURL in mountedVolumeURLs
{
if let disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, volumeURL as CFURL)
{
if let bsdname = DADiskGetBSDName(disk)
{
let bsdString = String(cString : bsdname)
print(volumeURL.path, bsdString)
}
}
}
}
Is it possible to get this information with a (public) framework on macOS?
You are pretty close, there is an API DADiskCopyDescription
in DiskArbitration
which returns a dictionary with a lot of information
if let session = DASessionCreate(kCFAllocatorDefault)
{
let mountedVolumeURLs = FileManager.default.mountedVolumeURLs(includingResourceValuesForKeys: nil)!
for volumeURL in mountedVolumeURLs
{
if let disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, volumeURL as CFURL)
{
if let diskInfo = DADiskCopyDescription(disk) as? [String:Any]
{
print(volumeURL.path, diskInfo)
}
}
}
}
But as far as I know it doesn't show the information whether the disk is SSD or HD, this could be determined with the IORegistry in IOKit
.