diff --git a/go.mod b/go.mod index 82fad4a..471c944 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/longhorn/go-iscsi-helper go 1.21 require ( - github.com/longhorn/go-common-libs v0.0.0-20240103081802-3993c5908447 + github.com/longhorn/go-common-libs v0.0.0-20240219051150-081e7dcbfef2 github.com/pkg/errors v0.9.1 github.com/sirupsen/logrus v1.9.3 golang.org/x/sys v0.13.0 diff --git a/go.sum b/go.sum index 2cc2f09..2a0a223 100644 --- a/go.sum +++ b/go.sum @@ -14,8 +14,8 @@ github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/longhorn/go-common-libs v0.0.0-20240103081802-3993c5908447 h1:NwR+xCGLpAORmB1UwNfDkQj3vPNIVikJe/hZe9+BQe0= -github.com/longhorn/go-common-libs v0.0.0-20240103081802-3993c5908447/go.mod h1:nIECQARppamt2zwFSdzADRTVKo/7izFwWIS3VWi7D/s= +github.com/longhorn/go-common-libs v0.0.0-20240219051150-081e7dcbfef2 h1:MP6JLqc8zFSbeq8FUHbAwvTqoBQk9mVefPr/pLyatm0= +github.com/longhorn/go-common-libs v0.0.0-20240219051150-081e7dcbfef2/go.mod h1:nIECQARppamt2zwFSdzADRTVKo/7izFwWIS3VWi7D/s= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I= github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc= github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg= diff --git a/iscsi/initiator.go b/iscsi/initiator.go index 3cd07cd..abe4c77 100644 --- a/iscsi/initiator.go +++ b/iscsi/initiator.go @@ -36,7 +36,7 @@ func CheckForInitiatorExistence(nsexec *lhns.Executor) error { opts := []string{ "--version", } - _, err := nsexec.Execute(iscsiBinary, opts, lhtypes.ExecuteDefaultTimeout) + _, err := nsexec.Execute(nil, iscsiBinary, opts, lhtypes.ExecuteDefaultTimeout) return err } @@ -53,7 +53,7 @@ func UpdateIscsiDeviceAbortTimeout(target string, timeout int64, nsexec *lhns.Ex "-n", "node.session.err_timeo.abort_timeout", "-v", strconv.FormatInt(timeout, 10), } - _, err := nsexec.Execute(iscsiBinary, opts, lhtypes.ExecuteDefaultTimeout) + _, err := nsexec.Execute(nil, iscsiBinary, opts, lhtypes.ExecuteDefaultTimeout) return err } @@ -63,7 +63,7 @@ func DiscoverTarget(ip, target string, nsexec *lhns.Executor) error { "-t", "sendtargets", "-p", ip, } - output, err := nsexec.Execute(iscsiBinary, opts, lhtypes.ExecuteDefaultTimeout) + output, err := nsexec.Execute(nil, iscsiBinary, opts, lhtypes.ExecuteDefaultTimeout) if err != nil { return err } @@ -91,7 +91,7 @@ func DeleteDiscoveredTarget(ip, target string, nsexec *lhns.Executor) error { if ip != "" { opts = append(opts, "-p", ip) } - _, err := nsexec.Execute(iscsiBinary, opts, lhtypes.ExecuteDefaultTimeout) + _, err := nsexec.Execute(nil, iscsiBinary, opts, lhtypes.ExecuteDefaultTimeout) return err } @@ -103,7 +103,7 @@ func IsTargetDiscovered(ip, target string, nsexec *lhns.Executor) bool { if ip != "" { opts = append(opts, "-p", ip) } - _, err := nsexec.Execute(iscsiBinary, opts, lhtypes.ExecuteDefaultTimeout) + _, err := nsexec.Execute(nil, iscsiBinary, opts, lhtypes.ExecuteDefaultTimeout) return err == nil } @@ -114,7 +114,7 @@ func LoginTarget(ip, target string, nsexec *lhns.Executor) error { "-p", ip, "--login", } - _, err := nsexec.Execute(iscsiBinary, opts, lhtypes.ExecuteDefaultTimeout) + _, err := nsexec.Execute(nil, iscsiBinary, opts, lhtypes.ExecuteDefaultTimeout) if err != nil { return err } @@ -146,7 +146,7 @@ func LogoutTarget(ip, target string, nsexec *lhns.Executor) error { if ip != "" { opts = append(opts, "-p", ip) } - _, err := nsexec.Execute(iscsiBinary, opts, lhtypes.ExecuteDefaultTimeout) + _, err := nsexec.Execute(nil, iscsiBinary, opts, lhtypes.ExecuteDefaultTimeout) return err } @@ -173,7 +173,7 @@ func IsTargetLoggedIn(ip, target string, nsexec *lhns.Executor) bool { "-m", "session", } - output, err := nsexec.Execute(iscsiBinary, opts, lhtypes.ExecuteDefaultTimeout) + output, err := nsexec.Execute(nil, iscsiBinary, opts, lhtypes.ExecuteDefaultTimeout) if err != nil { return false } @@ -205,7 +205,7 @@ func manualScanSession(ip, target string, nsexec *lhns.Executor) error { "-p", ip, "--rescan", } - _, err := nsexec.Execute(iscsiBinary, opts, ScanTimeout) + _, err := nsexec.Execute(nil, iscsiBinary, opts, ScanTimeout) return err } @@ -216,7 +216,7 @@ func getIscsiNodeSessionScanMode(ip, target string, nsexec *lhns.Executor) (stri "-p", ip, "-o", "show", } - output, err := nsexec.Execute(iscsiBinary, opts, ScanTimeout) + output, err := nsexec.Execute(nil, iscsiBinary, opts, ScanTimeout) if err != nil { return "", err } @@ -233,7 +233,7 @@ func findScsiDevice(ip, target string, lun int, nsexec *lhns.Executor) (*lhtypes "-m", "session", "-P", "3", } - output, err := nsexec.Execute(iscsiBinary, opts, lhtypes.ExecuteDefaultTimeout) + output, err := nsexec.Execute(nil, iscsiBinary, opts, lhtypes.ExecuteDefaultTimeout) if err != nil { return nil, err } @@ -362,6 +362,6 @@ func RescanTarget(ip, target string, nsexec *lhns.Executor) error { if ip != "" { opts = append(opts, "-p", ip) } - _, err := nsexec.Execute(iscsiBinary, opts, lhtypes.ExecuteDefaultTimeout) + _, err := nsexec.Execute(nil, iscsiBinary, opts, lhtypes.ExecuteDefaultTimeout) return err } diff --git a/iscsidev/iscsi.go b/iscsidev/iscsi.go index 13c8664..06e7c34 100644 --- a/iscsidev/iscsi.go +++ b/iscsidev/iscsi.go @@ -337,7 +337,7 @@ func (dev *Device) DeleteTarget() error { // UnbindInitiator can return tgtadmSuccess, tgtadmAclNoexist or tgtadmNoTarget // Target is deleted in the last step, so tgtadmNoTarget error should not occur here. - // Just ingore tgtadmAclNoexist and continue working on the remaining tasks. + // Just ignore tgtadmAclNoexist and continue working on the remaining tasks. if err := iscsi.UnbindInitiator(tid, "ALL"); err != nil { if !strings.Contains(err.Error(), types.TgtadmAclNoexist) { return err diff --git a/types/tgtadm_errors.go b/types/tgtadm_errors.go index 8092789..1b7215c 100644 --- a/types/tgtadm_errors.go +++ b/types/tgtadm_errors.go @@ -1,6 +1,6 @@ package types -// erros are from tgt/usr/tgtadm_error.h and tgt/usr/tgtadm.c +// errors are from tgt/usr/tgtadm_error.h and tgt/usr/tgtadm.c const ( TgtadmSuccess = "success" TgtadmUnknown = "unknown error" diff --git a/vendor/github.com/longhorn/go-common-libs/ns/crypto.go b/vendor/github.com/longhorn/go-common-libs/ns/crypto.go index d8988dd..5638b37 100644 --- a/vendor/github.com/longhorn/go-common-libs/ns/crypto.go +++ b/vendor/github.com/longhorn/go-common-libs/ns/crypto.go @@ -65,5 +65,5 @@ func (nsexec *Executor) CryptsetupWithPassphrase(passphrase string, args []strin // For Talos Linux, cryptsetup comes pre-installed in the host namespace // (ref: https://github.com/siderolabs/pkgs/blob/release-1.4/reproducibility/pkg.yaml#L10) // for the [Disk Encryption](https://www.talos.dev/v1.4/talos-guides/configuration/disk-encryption/). - return nsexec.ExecuteWithStdin(types.BinaryCryptsetup, args, passphrase, timeout) + return nsexec.ExecuteWithStdin(nil, types.BinaryCryptsetup, args, passphrase, timeout) } diff --git a/vendor/github.com/longhorn/go-common-libs/ns/executor.go b/vendor/github.com/longhorn/go-common-libs/ns/executor.go index 617f21e..a850e77 100644 --- a/vendor/github.com/longhorn/go-common-libs/ns/executor.go +++ b/vendor/github.com/longhorn/go-common-libs/ns/executor.go @@ -46,7 +46,7 @@ func NewNamespaceExecutor(processName, procDirectory string, namespaces []types. } // prepareCommandArgs prepares the nsenter command arguments. -func (nsexec *Executor) prepareCommandArgs(binary string, args []string) []string { +func (nsexec *Executor) prepareCommandArgs(binary string, args, envs []string) []string { cmdArgs := []string{} for _, ns := range nsexec.namespaces { nsPath := filepath.Join(nsexec.nsDirectory, ns.String()) @@ -59,24 +59,29 @@ func (nsexec *Executor) prepareCommandArgs(binary string, args []string) []strin cmdArgs = append(cmdArgs, "--net="+nsPath) } } + if len(envs) > 0 { + cmdArgs = append(cmdArgs, "env", "-i") + cmdArgs = append(cmdArgs, envs...) + } + cmdArgs = append(cmdArgs, binary) return append(cmdArgs, args...) } // Execute executes the command in the namespace. If NsDirectory is empty, // it will execute the command in the current namespace. -func (nsexec *Executor) Execute(binary string, args []string, timeout time.Duration) (string, error) { - return nsexec.executor.Execute(nil, types.NsBinary, nsexec.prepareCommandArgs(binary, args), timeout) +func (nsexec *Executor) Execute(envs []string, binary string, args []string, timeout time.Duration) (string, error) { + return nsexec.executor.Execute(nil, types.NsBinary, nsexec.prepareCommandArgs(binary, args, envs), timeout) } // ExecuteWithStdin executes the command in the namespace with stdin. // If NsDirectory is empty, it will execute the command in the current namespace. -func (nsexec *Executor) ExecuteWithStdin(binary string, args []string, stdinString string, timeout time.Duration) (string, error) { - return nsexec.executor.ExecuteWithStdin(types.NsBinary, nsexec.prepareCommandArgs(binary, args), stdinString, timeout) +func (nsexec *Executor) ExecuteWithStdin(envs []string, binary string, args []string, stdinString string, timeout time.Duration) (string, error) { + return nsexec.executor.ExecuteWithStdin(types.NsBinary, nsexec.prepareCommandArgs(binary, args, envs), stdinString, timeout) } // ExecuteWithStdinPipe executes the command in the namespace with stdin pipe. // If NsDirectory is empty, it will execute the command in the current namespace. -func (nsexec *Executor) ExecuteWithStdinPipe(binary string, args []string, stdinString string, timeout time.Duration) (string, error) { - return nsexec.executor.ExecuteWithStdinPipe(types.NsBinary, nsexec.prepareCommandArgs(binary, args), stdinString, timeout) +func (nsexec *Executor) ExecuteWithStdinPipe(envs []string, binary string, args []string, stdinString string, timeout time.Duration) (string, error) { + return nsexec.executor.ExecuteWithStdinPipe(types.NsBinary, nsexec.prepareCommandArgs(binary, args, envs), stdinString, timeout) } diff --git a/vendor/github.com/longhorn/go-common-libs/ns/filelock.go b/vendor/github.com/longhorn/go-common-libs/ns/filelock.go index 698cca6..b0ce92c 100644 --- a/vendor/github.com/longhorn/go-common-libs/ns/filelock.go +++ b/vendor/github.com/longhorn/go-common-libs/ns/filelock.go @@ -40,7 +40,7 @@ func LockFile(path string) (result *os.File, err error) { // FileLock is a struct responsible for locking a file. type FileLock struct { FilePath string // The path of the file to lock. - File *os.File // The file handle aquired after successful lock. + File *os.File // The file handle acquired after successful lock. Timeout time.Duration // The maximum time to wait for lock acquisition. done chan struct{} // A channel for signaling lock release. diff --git a/vendor/github.com/longhorn/go-common-libs/sys/sys.go b/vendor/github.com/longhorn/go-common-libs/sys/sys.go index ea6f029..e4e23a3 100644 --- a/vendor/github.com/longhorn/go-common-libs/sys/sys.go +++ b/vendor/github.com/longhorn/go-common-libs/sys/sys.go @@ -56,13 +56,13 @@ func GetOSDistro(osReleaseContent string) (string, error) { // GetSystemBlockDeviceInfo returns the block device info for the system. func GetSystemBlockDeviceInfo() (map[string]types.BlockDeviceInfo, error) { - return getSystemBlockDeviceInfo(os.ReadDir, os.ReadFile) + return getSystemBlockDeviceInfo(types.SysClassBlockDirectory, os.ReadDir, os.ReadFile) } // getSystemBlockDeviceInfo returns the block device info for the system. // It injects the readDirFn and readFileFn for testing. -func getSystemBlockDeviceInfo(readDirFn func(string) ([]os.DirEntry, error), readFileFn func(string) ([]byte, error)) (map[string]types.BlockDeviceInfo, error) { - devices, err := readDirFn(types.SysClassBlockDirectory) +func getSystemBlockDeviceInfo(sysClassBlockDirectory string, readDirFn func(string) ([]os.DirEntry, error), readFileFn func(string) ([]byte, error)) (map[string]types.BlockDeviceInfo, error) { + devices, err := readDirFn(sysClassBlockDirectory) if err != nil { return nil, err } @@ -82,12 +82,12 @@ func getSystemBlockDeviceInfo(readDirFn func(string) ([]os.DirEntry, error), rea deviceInfo := make(map[string]types.BlockDeviceInfo, len(devices)) for _, device := range devices { deviceName := device.Name() - devicePath := filepath.Join(types.SysClassBlockDirectory, deviceName, "dev") + devicePath := filepath.Join(sysClassBlockDirectory, deviceName, "dev") if _, err := os.Stat(devicePath); os.IsNotExist(err) { // If the device path does not exist, check if the device path exists in the "device" directory. // Some devices such as "nvme0cn1" created from SPDK do not have "dev" file under their sys/class/block directory. - alternativeDevicePath := filepath.Join(types.SysClassBlockDirectory, deviceName, "device", "dev") + alternativeDevicePath := filepath.Join(sysClassBlockDirectory, deviceName, "device", "dev") if _, altErr := os.Stat(alternativeDevicePath); os.IsNotExist(altErr) { errs := fmt.Errorf("primary error: %w; alternative error: %w", err, altErr) logrus.WithFields(logrus.Fields{ diff --git a/vendor/modules.txt b/vendor/modules.txt index c860449..17582c5 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -14,7 +14,7 @@ github.com/kr/pretty # github.com/kr/text v0.1.0 ## explicit github.com/kr/text -# github.com/longhorn/go-common-libs v0.0.0-20240103081802-3993c5908447 +# github.com/longhorn/go-common-libs v0.0.0-20240219051150-081e7dcbfef2 ## explicit; go 1.21 github.com/longhorn/go-common-libs/exec github.com/longhorn/go-common-libs/io