Skip to content

Commit

Permalink
diagnostics: added collecting info about disk for windows (#11947)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvovk authored Sep 12, 2024
1 parent 5009a7c commit 4fde7fc
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 1 deletion.
2 changes: 1 addition & 1 deletion erigon-lib/diskutils/diskutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License
// along with Erigon. If not, see <http://www.gnu.org/licenses/>.

//go:build !darwin
//go:build !darwin && !windows

package diskutils

Expand Down
105 changes: 105 additions & 0 deletions erigon-lib/diskutils/diskutils_windows.go
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

//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
}
1 change: 1 addition & 0 deletions erigon-lib/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions erigon-lib/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 4fde7fc

Please sign in to comment.