Created
July 28, 2022 07:00
-
-
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
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 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