Torxed fix sys command calls (#932)

* Fixed exceptions in is_vm() and virtualization()

* Added exception handling for parted in BlockDevice.free_space
This commit is contained in:
Anton Hvornum 2022-02-02 08:18:12 +01:00 committed by GitHub
parent a7c57bac53
commit 7f01747efc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 7 deletions

View File

@ -8,7 +8,7 @@ from typing import Optional, Dict, Any, Iterator, Tuple, List, TYPE_CHECKING
if TYPE_CHECKING:
from .partition import Partition
from ..exceptions import DiskError
from ..exceptions import DiskError, SysCallError
from ..output import log
from ..general import SysCommand
from ..storage import storage
@ -189,10 +189,13 @@ class BlockDevice:
# that is "outside" the disk. in /dev/sr0 this is usually the case with Archiso,
# so the free will ignore the ESP partition and just give the "free" space.
# Doesn't harm us, but worth noting in case something weird happens.
for line in SysCommand(f"parted -s --machine {self.path} print free"):
if 'free' in (free_space := line.decode('UTF-8')):
_, start, end, size, *_ = free_space.strip('\r\n;').split(':')
yield (start, end, size)
try:
for line in SysCommand(f"parted -s --machine {self.path} print free"):
if 'free' in (free_space := line.decode('UTF-8')):
_, start, end, size, *_ = free_space.strip('\r\n;').split(':')
yield (start, end, size)
except SysCallError as error:
log(f"Could not get free space on {self.path}: {error}", level=logging.INFO)
@property
def largest_free_space(self) -> List[str]:

View File

@ -6,6 +6,7 @@ from typing import Iterator, Optional, Union
from .general import SysCommand
from .networking import list_interfaces, enrich_iface_types
from .exceptions import SysCallError
from .output import log
__packages__ = [
@ -170,10 +171,19 @@ def mem_total() -> Optional[int]:
def virtualization() -> Optional[str]:
return str(SysCommand("systemd-detect-virt")).strip('\r\n')
try:
return str(SysCommand("systemd-detect-virt")).strip('\r\n')
except SysCallError as error:
log(f"Could not detect virtual system: {error}", level=logging.DEBUG)
return None
def is_vm() -> bool:
return b"none" not in b"".join(SysCommand("systemd-detect-virt")).lower()
try:
return b"none" not in b"".join(SysCommand("systemd-detect-virt")).lower()
except SysCallError as error:
log(f"System is not running in a VM: {error}", level=logging.DEBUG)
return None
# TODO: Add more identifiers