package linux_generic import ( "strings" ) // lsblkSafeColumns are columns whose cells are never empty and never contain // spaces, so a row can be split on whitespace up to the first other column. var lsblkSafeColumns = map[string]bool{ "NAME": true, "MAJ:MIN": true, "RM": true, "SIZE": true, "RO": true, "TYPE": true, "ROTA": true, } // lsblkVirtualPrefixes are kernel block devices that are not physical drives. var lsblkVirtualPrefixes = []string{"loop", "zram", "ram", "sr", "md", "dm-", "nbd", "fd"} // parseLsblk reads the `lsblk` tree. It only tells that a disk exists and // roughly how large it is, so it fills gaps left by nvme-cli/smartctl and // adds disks no other utility described. Removable media (the live USB stick // the capture ran from) is skipped. func parseLsblk(file sourceFile, inv *inventory) { lines := strings.Split(file.content, "\n") for i := 0; i < len(lines); i++ { if !strings.HasPrefix(lines[i], "NAME") { continue } index := make(map[string]int) for n, column := range strings.Fields(lines[i]) { if !lsblkSafeColumns[column] { break } index[column] = n } _, hasSize := index["SIZE"] _, hasType := index["TYPE"] if !hasSize || !hasType { continue } next := i + 1 for ; next < len(lines) && strings.TrimSpace(lines[next]) != ""; next++ { row := strings.Fields(lines[next]) // Partitions and wrapped mountpoint lines start with tree // drawing characters or indentation. if len(row) < len(index) || isIndented(lines[next]) || !isLsblkDeviceName(row[0]) { continue } cell := func(column string) string { if n, ok := index[column]; ok { return row[n] } return "" } if cell("TYPE") != "disk" || cell("RM") == "1" || isVirtualBlockDevice(row[0]) { continue } d := inv.findDrive("", row[0]) if d.roughSizeGB == 0 { d.roughSizeGB = parseLsblkSizeGB(cell("SIZE")) } switch cell("ROTA") { case "1": setIfEmpty(&d.mediaType, "HDD") case "0": setIfEmpty(&d.mediaType, "SSD") } if d.controller != "" { setIfEmpty(&d.mediaType, "SSD") setIfEmpty(&d.busInterface, "NVMe") } } i = next - 1 } } func isLsblkDeviceName(name string) bool { for _, c := range name { if !(c >= 'a' && c <= 'z') && !(c >= '0' && c <= '9') && c != '-' && c != '_' { return false } } return name != "" } func isVirtualBlockDevice(name string) bool { for _, prefix := range lsblkVirtualPrefixes { if strings.HasPrefix(name, prefix) { return true } } return false } // parseLsblkSizeGB converts an lsblk size ("2.9T", "894.3G") to decimal GB. // lsblk prints binary units with a single-letter suffix. func parseLsblkSizeGB(value string) int { if value == "" { return 0 } unit := map[byte]string{'K': "KiB", 'M': "MiB", 'G': "GiB", 'T': "TiB", 'P': "PiB"}[value[len(value)-1]] if unit == "" { return 0 } size, ok := parseSizeBytes(strings.ReplaceAll(value[:len(value)-1], ",", "."), unit) if !ok { return 0 } return bytesToGB(size) }