Skip to content

Commit 358753a

Browse files
committed
check hostid on Jail.start when hostid_strict_check is enabled
1 parent 74963a4 commit 358753a

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

libioc/Config/Jail/Defaults.py

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
"host_hostuuid": None,
5757
"host_hostname": None,
5858
"host_domainname": None,
59+
"hostid": None,
60+
"hostid_strict_check": False,
5961
"devfs_ruleset": 4,
6062
"enforce_statfs": 2,
6163
"children_max": 0,

libioc/Host.py

+14
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class HostGenerator:
5555
_devfs: libioc.DevfsRules.DevfsRules
5656
_defaults: libioc.Resource.DefaultResource
5757
_defaults_initialized = False
58+
__hostid: str
5859
releases_dataset: libzfs.ZFSDataset
5960
datasets: libioc.Datasets.Datasets
6061
distribution: _distribution_types
@@ -114,6 +115,19 @@ def _init_defaults(
114115
zfs=self.zfs
115116
)
116117

118+
@property
119+
def id(self) -> str:
120+
"""Return the hostid and memoize on first lookup."""
121+
try:
122+
return self.__hostid
123+
except AttributeError:
124+
pass
125+
126+
with open("/etc/hostid", "r") as f:
127+
self.__hostid = f.read().strip()
128+
129+
return self.__hostid
130+
117131
@property
118132
def defaults(self) -> 'libioc.Resource.DefaultResource':
119133
"""Return the lazy-loaded defaults."""

libioc/Jail.py

+23
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ def start(
466466
"""
467467
self.require_jail_existing()
468468
self.require_jail_stopped()
469+
self.require_jail_match_hostid()
469470

470471
try:
471472
yield from self.config["resolver"].apply(
@@ -1430,6 +1431,8 @@ def _create_from_resource(
14301431
if backend is not None:
14311432
backend.setup(self.storage, resource)
14321433

1434+
self.config["hostid"] = self.host.id
1435+
14331436
self._update_fstab()
14341437
self.save()
14351438

@@ -2012,6 +2015,26 @@ def require_jail_is_template(self, log_errors: bool=True) -> None:
20122015
logger=(self.logger if log_errors else None)
20132016
)
20142017

2018+
def require_jail_match_hostid(self, log_errors: bool=True) -> None:
2019+
"""Raise JailIsTemplate exception if the jail is a template."""
2020+
if self.hostid_check_ok is False:
2021+
raise libioc.errors.JailHostIdMismatch(
2022+
jail=self,
2023+
host_hostid=self.host.id,
2024+
logger=(self.logger if log_errors else None)
2025+
)
2026+
2027+
@property
2028+
def hostid_check_ok(self) -> bool:
2029+
"""Return true if the hostid check passes."""
2030+
if self.config["hostid_strict_check"] is False:
2031+
self.logger.spam("hostid_strict_check is disabled")
2032+
return True
2033+
jail_hostid = self.config["hostid"]
2034+
if (jail_hostid is None) or (jail_hostid == self.host.id):
2035+
return True
2036+
return False
2037+
20152038
def require_storage_backend(self, log_errors: bool=True) -> None:
20162039
"""Raise if the jail was not initialized with a storage backend."""
20172040
if self.storage_backend is None:

libioc/errors.py

+17
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,23 @@ def __init__(
461461
IocException.__init__(self, message=msg, logger=logger)
462462

463463

464+
class JailHostIdMismatch(JailException):
465+
"""Raised when attempting to start a jail with mismatching hostid."""
466+
467+
def __init__(
468+
self,
469+
host_hostid: str,
470+
jail: 'libioc.Jail.JailGenerator',
471+
logger: typing.Optional['libioc.Logger.Logger']=None
472+
) -> None:
473+
jail_hostid = jail.config["hostid"]
474+
msg = (
475+
f"The jail hostid '{jail_hostid}' "
476+
f"does not match the hosts hostid '{host_hostid}'"
477+
)
478+
JailException.__init__(self, message=msg, jail=jail, logger=logger)
479+
480+
464481
class JailConfigNotFound(IocException):
465482
"""Raised when a jail is not configured."""
466483

0 commit comments

Comments
 (0)