Files
logpile/internal/parser/vendors/linux_generic/lsblk.go
T
Mikhail ChusavitinandClaude Opus 5.5 b4b15e58a2 feat(parser): linux-generic content-driven parser and RAR archive support
Field bundles from ad-hoc service scripts (first sample: Giga G894-SD3 .rar
with dmidecode/lscpu/lspci/nvme/lsblk output, no fixed layout). ADL-078:
- linux-generic: recognizes utility output by content, merges records by
  stable key, low detect confidence (20-30) below all vendor parsers
- pciids.LookupByDescription: resolve a textual lspci description back to
  vendor/device IDs and manufacturer/model
- archive: accept .rar via the in-repo internal/rar5 reader (RAR 5 / RAR 7,
  single volume, solid; encrypted, multi-volume, SFX and RAR 4.x rejected),
  same per-file and total limits as tar/zip
- UI: .rar in the upload accept list

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 15:00:38 +03:00

110 lines
2.9 KiB
Go

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)
}