-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from maximumadmin/detect-vm
Optionally skip initialization if running on a VM
- Loading branch information
Showing
5 changed files
with
64 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
"itertools", | ||
"lsmod", | ||
"mkswap", | ||
"virt", | ||
"zram", | ||
"zramd", | ||
"zstd" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,6 @@ | |
|
||
# Swap priority | ||
# PRIORITY=100 | ||
|
||
# Skip initialization if running inside a virtual machine | ||
# SKIP_VM=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package system | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"os/exec" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// IsRoot will check if current process was started by the init system (e.g. | ||
// systemd) from which we expect to handle this process' capabilities, otherwise | ||
// check if the current process is running as root. | ||
func IsRoot() bool { | ||
return os.Getppid() == 1 || os.Geteuid() == 0 | ||
} | ||
|
||
func cpuInfo() *[]byte { | ||
data, err := os.ReadFile("/proc/cpuinfo") | ||
if err != nil { | ||
panic(err) | ||
} | ||
return &data | ||
} | ||
|
||
// IsVM detects if we are currently running inside a VM, see also | ||
// https://man.archlinux.org/man/systemd-detect-virt.1.en. | ||
func IsVM() bool { | ||
// Try to run systemd-detect-virt which is more accurate but is not present on | ||
// all systems. | ||
cmd := exec.Command("systemd-detect-virt", "--vm") | ||
out, err := cmd.Output() | ||
if err == nil { | ||
return strings.TrimSpace(string(out)) != "none" | ||
} | ||
// If error happened because systemd-detect-virt is not available on the | ||
// system, try to use cpuinfo (less accurate but available everywhere). | ||
if errors.Is(err, exec.ErrNotFound) { | ||
info := cpuInfo() | ||
pattern := "(?m)^flags\\s*\\:.*\\s+hypervisor(?:\\s+.*)?$" | ||
match, _ := regexp.Match(pattern, *info) | ||
return match | ||
} | ||
panic(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters