Ensure script is ran as root.

This commit is contained in:
Filip Strajnar 2024-10-26 18:25:37 +02:00
parent 5f39ae7138
commit b98b76d4e4
3 changed files with 24 additions and 0 deletions

View file

@ -1,5 +1,10 @@
from sys import platform
from pci_passthrough_assist.permissions import is_ran_by_root
if platform != "linux":
print("This tool will only work on Linux based OS.")
exit(1)
if not is_ran_by_root():
print("This script needs to run as root.")
exit(1)

View file

@ -0,0 +1,5 @@
from pci_passthrough_assist.process_runner import sh
def is_ran_by_root() -> bool:
return sh(["whoami"]) == "root"

View file

@ -0,0 +1,14 @@
from subprocess import run
def sh_binary(args: list[str], ) -> bytes:
return run(args, capture_output=True).stdout
def sh(args: list[str], rstrip_newline=True):
string_output: str = sh_binary(args).decode()
return string_output if not rstrip_newline else string_output.rstrip("\n")
def sh_lines(args: list[str]) -> list[str]:
return sh(args, rstrip_newline=True).splitlines()