Skip to content

Instantly share code, notes, and snippets.

@Joyfolk
Created July 28, 2022 07:00
Show Gist options
  • Select an option

  • Save Joyfolk/0b7bbb10b6fb59d97dde69dad6a2dfbb to your computer and use it in GitHub Desktop.

Select an option

Save Joyfolk/0b7bbb10b6fb59d97dde69dad6a2dfbb to your computer and use it in GitHub Desktop.
How to find linux device name for path and check if it is ssd
import java.io.File
import java.nio.file.{Files, Path}
import scala.io.Source
def deviceName(path: Path): String = {
val absPath = path.toAbsolutePath
val fsName = Files.getFileStore(absPath).name()
val fsPath = absPath.getRoot.resolve(fsName).toRealPath()
fsPath.getFileName.toString
}
def isSsd(devName: String): Boolean = {
import scala.collection.JavaConverters._
val sysinfo = new File("/").toPath.getRoot.resolve("sys").resolve("block")
val d = Files.newDirectoryStream(sysinfo).asScala
.filter(d => d.getFileName.startsWith(devName))
.toSeq
.maxBy(_.toString.length)
val path = d.resolve("queue").resolve("rotational")
val s = Source.fromFile(path.toFile)
try {
s.getLines().mkString.trim == "0"
} finally s.close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment