linsk/vm/os_specifics.go

47 lines
1.1 KiB
Go
Raw Normal View History

2023-08-28 11:35:57 +02:00
//go:build !windows
package vm
import (
"os/exec"
2023-08-29 15:31:17 +01:00
"path/filepath"
"strings"
2023-08-28 11:35:57 +02:00
"syscall"
2023-08-29 15:31:17 +01:00
"github.com/pkg/errors"
2023-08-28 11:35:57 +02:00
)
func prepareVMCmd(cmd *exec.Cmd) {
// This is to prevent Ctrl+C propagating to the child process.
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
}
func terminateProcess(pid int) error {
return syscall.Kill(-pid, syscall.SIGTERM)
}
2023-08-29 15:31:17 +01:00
// This is never used except for a band-aid that would check
// that there are no double-mounts.
func checkDeviceSeemsMounted(devPathPrefix string) (bool, error) {
2023-08-31 21:01:45 +01:00
// Quite a bit hacky implementation, but it's to be used as a failsafe band-aid anyway.
2023-08-29 15:31:17 +01:00
absDevPathPrefix, err := filepath.Abs(devPathPrefix)
if err != nil {
return false, errors.Wrap(err, "get abs path")
}
mounts, err := exec.Command("mount").Output()
if err != nil {
return false, errors.Wrap(err, "run mount command")
}
for _, line := range strings.Split(string(mounts), "\n") {
// I know, I know, this is a rare band-aid.
if strings.HasPrefix(line, devPathPrefix) || strings.HasPrefix(line, absDevPathPrefix) {
return true, nil
}
}
return false, nil
}