From 4fde7fcac2650c55caadd2872ea6e8aea9956484 Mon Sep 17 00:00:00 2001 From: Dmytro Vovk Date: Thu, 12 Sep 2024 03:23:50 +0100 Subject: [PATCH] diagnostics: added collecting info about disk for windows (#11947) --- erigon-lib/diskutils/diskutils.go | 2 +- erigon-lib/diskutils/diskutils_windows.go | 105 ++++++++++++++++++++++ erigon-lib/go.mod | 1 + erigon-lib/go.sum | 2 + go.mod | 1 + go.sum | 2 + 6 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 erigon-lib/diskutils/diskutils_windows.go diff --git a/erigon-lib/diskutils/diskutils.go b/erigon-lib/diskutils/diskutils.go index 90d97749cbf..e22567b35e8 100644 --- a/erigon-lib/diskutils/diskutils.go +++ b/erigon-lib/diskutils/diskutils.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with Erigon. If not, see . -//go:build !darwin +//go:build !darwin && !windows package diskutils diff --git a/erigon-lib/diskutils/diskutils_windows.go b/erigon-lib/diskutils/diskutils_windows.go new file mode 100644 index 00000000000..854ac23ae8e --- /dev/null +++ b/erigon-lib/diskutils/diskutils_windows.go @@ -0,0 +1,105 @@ +// Copyright 2024 The Erigon Authors +// This file is part of Erigon. +// +// Erigon is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Erigon is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with Erigon. If not, see . + +//go:build windows + +package diskutils + +import ( + "bytes" + "fmt" + "os/exec" + "path/filepath" + "strings" + + shortcut "github.com/nyaosorg/go-windows-shortcut" +) + +func MountPointForDirPath(dirPath string) string { + mountPoint := "C" + actualPath := SmlinkForDirPath(dirPath) + + psCommand := fmt.Sprintf(`(Get-Item -Path "%s").PSDrive.Name`, actualPath) + cmd := exec.Command("powershell", "-Command", psCommand) + var out bytes.Buffer + cmd.Stdout = &out + err := cmd.Run() + if err == nil { + mountPoint = strings.TrimSpace(out.String()) + } + + mountPoint = mountPoint + ":" + return mountPoint +} + +func SmlinkForDirPath(dirPath string) string { + if filepath.Ext(dirPath) == ".lnk" { + actualPath, _, err := shortcut.Read(dirPath) + if err != nil { + return dirPath + } + + return actualPath + } else { + return dirPath + } +} + +func DiskInfo(disk string) (string, error) { + disk = strings.TrimSuffix(disk, ":") + // Get the serial number for the disk with the specified drive letter + psCommand := fmt.Sprintf(` + $volume = Get-Volume -DriveLetter %s + + if ($volume) { + # Get the partition associated with this volume + $partition = Get-Partition -DriveLetter %s + + if ($partition) { + # Get the disk associated with this partition + $disk = Get-Disk -Number $partition.DiskNumber + + if ($disk) { + Get-PhysicalDisk -SerialNumber $disk.SerialNumber | Select-Object DeviceID, FriendlyName, SerialNumber, MediaType, BusType, FirmwareVersion, Manufacturer, Model, Size, PartitionStyle, OperationalStatus, Usage + } else { + exit 1 + } + } else { + exit 2 + } + } else { + exit 3 + } + `, disk, disk) + cmd := exec.Command("powershell", "-Command", psCommand) + var out bytes.Buffer + var stderr bytes.Buffer + cmd.Stdout = &out + cmd.Stderr = &stderr + err := cmd.Run() + if err != nil { + exitCode := err.(*exec.ExitError).ExitCode() + if exitCode == 1 { + return "", fmt.Errorf("error getting disk for partition with drive letter: %s", disk) + } else if exitCode == 2 { + return "", fmt.Errorf("error getting partition for volume with drive letter: %s", disk) + } else { + return "", fmt.Errorf("error getting volume with drive letter: %s", disk) + } + } + + return strings.TrimSpace(out.String()), nil +} diff --git a/erigon-lib/go.mod b/erigon-lib/go.mod index 719a14bba10..dc9cd629c24 100644 --- a/erigon-lib/go.mod +++ b/erigon-lib/go.mod @@ -31,6 +31,7 @@ require ( github.com/hashicorp/golang-lru/v2 v2.0.7 github.com/holiman/bloomfilter/v2 v2.0.3 github.com/holiman/uint256 v1.3.1 + github.com/nyaosorg/go-windows-shortcut v0.0.0-20220529122037-8b0c89bca4c4 github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 github.com/pelletier/go-toml/v2 v2.2.3 github.com/prometheus/client_golang v1.20.2 diff --git a/erigon-lib/go.sum b/erigon-lib/go.sum index 4f7afa5de4a..c6d533a37b7 100644 --- a/erigon-lib/go.sum +++ b/erigon-lib/go.sum @@ -318,6 +318,8 @@ github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdh github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= +github.com/nyaosorg/go-windows-shortcut v0.0.0-20220529122037-8b0c89bca4c4 h1:+3bXHpIl3RiBuPKlqeCZZeShGHC9RFhR/P2OJfOLRyA= +github.com/nyaosorg/go-windows-shortcut v0.0.0-20220529122037-8b0c89bca4c4/go.mod h1:9YR30vCq/4djj0WO7AvLm48YvNs7M094LWRieEFDE4A= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= diff --git a/go.mod b/go.mod index 8e214bff982..b36a6cf740b 100644 --- a/go.mod +++ b/go.mod @@ -115,6 +115,7 @@ require ( github.com/hashicorp/go-cleanhttp v0.5.2 // indirect github.com/hashicorp/go-retryablehttp v0.7.7 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect + github.com/nyaosorg/go-windows-shortcut v0.0.0-20220529122037-8b0c89bca4c4 // indirect github.com/tklauser/go-sysconf v0.3.14 // indirect github.com/tklauser/numcpus v0.8.0 // indirect github.com/yusufpapurcu/wmi v1.2.4 // indirect diff --git a/go.sum b/go.sum index c62508c89be..2cd43edc03d 100644 --- a/go.sum +++ b/go.sum @@ -657,6 +657,8 @@ github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86/go.mod h1:kHJE github.com/neelance/sourcemap v0.0.0-20151028013722-8c68805598ab/go.mod h1:Qr6/a/Q4r9LP1IltGz7tA7iOK1WonHEYhu1HRBA7ZiM= github.com/nxadm/tail v1.4.11 h1:8feyoE3OzPrcshW5/MJ4sGESc5cqmGkGCWlco4l0bqY= github.com/nxadm/tail v1.4.11/go.mod h1:OTaG3NK980DZzxbRq6lEuzgU+mug70nY11sMd4JXXHc= +github.com/nyaosorg/go-windows-shortcut v0.0.0-20220529122037-8b0c89bca4c4 h1:+3bXHpIl3RiBuPKlqeCZZeShGHC9RFhR/P2OJfOLRyA= +github.com/nyaosorg/go-windows-shortcut v0.0.0-20220529122037-8b0c89bca4c4/go.mod h1:9YR30vCq/4djj0WO7AvLm48YvNs7M094LWRieEFDE4A= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=