-
Notifications
You must be signed in to change notification settings - Fork 417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bootc: Keep /usr read-only after transient transactions ("re-locking") #2195
Open
evan-goode
wants to merge
3
commits into
rpm-software-management:bootc
Choose a base branch
from
evan-goode:evan-goode/bootc-relock
base: bootc
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+124
−38
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
from .pycomp import PY3, basestring | ||
from dnf.i18n import _, ucd | ||
import argparse | ||
import ctypes | ||
import dnf | ||
import dnf.callback | ||
import dnf.const | ||
|
@@ -642,33 +643,104 @@ def _is_file_pattern_present(specs): | |
return False | ||
|
||
|
||
def _is_bootc_host(): | ||
"""Returns true is the system is managed as an immutable container, false | ||
otherwise.""" | ||
ostree_booted = "/run/ostree-booted" | ||
return os.path.isfile(ostree_booted) | ||
|
||
|
||
def _is_bootc_unlocked(): | ||
"""Check whether /usr is writeable, e.g. if we are in a normal mutable | ||
system or if we are in a bootc after `bootc usr-overlay` or `ostree admin | ||
unlock` was run.""" | ||
class _BootcSystem: | ||
usr = "/usr" | ||
return os.access(usr, os.W_OK) | ||
|
||
|
||
def _bootc_unlock(): | ||
"""Set up a writeable overlay on bootc systems.""" | ||
CLONE_NEWNS = 0x00020000 # defined in linux/include/uapi/linux/sched.h | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sigh, but yeah. |
||
|
||
if _is_bootc_unlocked(): | ||
return | ||
|
||
unlock_command = ["bootc", "usr-overlay"] | ||
|
||
try: | ||
completed_process = subprocess.run(unlock_command, text=True) | ||
completed_process.check_returncode() | ||
except FileNotFoundError: | ||
raise dnf.exceptions.Error(_("bootc command not found. Is this a bootc system?")) | ||
except subprocess.CalledProcessError: | ||
raise dnf.exceptions.Error(_("Failed to unlock system: %s", completed_process.stderr)) | ||
def __init__(self): | ||
if not self.is_bootc_system(): | ||
raise RuntimeError(_("Not running on a bootc system.")) | ||
|
||
import gi | ||
self._gi = gi | ||
|
||
gi.require_version("OSTree", "1.0") | ||
from gi.repository import OSTree | ||
|
||
self._OSTree = OSTree | ||
|
||
self._sysroot = self._OSTree.Sysroot.new_default() | ||
assert self._sysroot.load(None) | ||
|
||
self._booted_deployment = self._sysroot.require_booted_deployment() | ||
assert self._booted_deployment is not None | ||
|
||
@staticmethod | ||
def is_bootc_system(): | ||
"""Returns true is the system is managed as an immutable container, false | ||
otherwise.""" | ||
ostree_booted = "/run/ostree-booted" | ||
return os.path.isfile(ostree_booted) | ||
|
||
@classmethod | ||
def is_writable(cls): | ||
"""Returns true if and only if /usr is writable.""" | ||
return os.access(cls.usr, os.W_OK) | ||
|
||
def _get_unlocked_state(self): | ||
return self._booted_deployment.get_unlocked() | ||
|
||
def is_unlocked_transient(self): | ||
"""Returns true if and only if the bootc system is unlocked in a | ||
transient state, i.e. a overlayfs is mounted as read-only on /usr. | ||
Changes can be made to the overlayfs by remounting /usr as | ||
read/write in a private mount namespace.""" | ||
return self._get_unlocked_state() == self._OSTree.DeploymentUnlockedState.TRANSIENT | ||
|
||
@classmethod | ||
def _set_up_mountns(cls): | ||
# os.unshare is only available in Python >= 3.12 | ||
libc = ctypes.CDLL(dnf.const.LIBC_SONAME) | ||
if libc.unshare(cls.CLONE_NEWNS) != 0: | ||
raise OSError("Failed to unshare mount namespace") | ||
|
||
mount_command = ["mount", "--options-source=disable", "-o", "remount,rw", cls.usr] | ||
try: | ||
completed_process = subprocess.run(mount_command, text=True) | ||
completed_process.check_returncode() | ||
except FileNotFoundError: | ||
raise dnf.exceptions.Error(_("%s: command not found.") % mount_command[0]) | ||
except subprocess.CalledProcessError: | ||
raise dnf.exceptions.Error(_("Failed to mount %s as read/write: %s", cls.usr, completed_process.stderr)) | ||
|
||
@staticmethod | ||
def _unlock(): | ||
unlock_command = ["ostree", "admin", "unlock", "--transient"] | ||
try: | ||
completed_process = subprocess.run(unlock_command, text=True) | ||
completed_process.check_returncode() | ||
except FileNotFoundError: | ||
raise dnf.exceptions.Error(_("%s: command not found. Is this a bootc system?") % unlock_command[0]) | ||
except subprocess.CalledProcessError: | ||
raise dnf.exceptions.Error(_("Failed to unlock system: %s", completed_process.stderr)) | ||
|
||
def make_writable(self): | ||
"""Set up a writable overlay on bootc systems.""" | ||
|
||
bootc_unlocked_state = self._get_unlocked_state() | ||
|
||
valid_bootc_unlocked_states = ( | ||
self._OSTree.DeploymentUnlockedState.NONE, | ||
self._OSTree.DeploymentUnlockedState.DEVELOPMENT, | ||
self._OSTree.DeploymentUnlockedState.TRANSIENT, | ||
self._OSTree.DeploymentUnlockedState.HOTFIX, | ||
) | ||
|
||
if bootc_unlocked_state not in valid_bootc_unlocked_states: | ||
raise ValueError(_("Unhandled bootc unlocked state: %s") % bootc_unlocked_state.value_nick) | ||
|
||
if bootc_unlocked_state in (self._OSTree.DeploymentUnlockedState.DEVELOPMENT, self._OSTree.DeploymentUnlockedState.HOTFIX): | ||
# System is already unlocked in development mode, and usr is | ||
# already mounted read/write. | ||
pass | ||
elif bootc_unlocked_state == self._OSTree.DeploymentUnlockedState.NONE: | ||
# System is not unlocked. Unlock it in transient mode, then set up | ||
# a mount namespace for DNF. | ||
self._unlock() | ||
self._set_up_mountns() | ||
elif bootc_unlocked_state == self._OSTree.DeploymentUnlockedState.TRANSIENT: | ||
# System is unlocked in transient mode, so usr is mounted | ||
# read-only. Set up a mount namespace for DNF. | ||
self._set_up_mountns() | ||
|
||
assert os.access(self.usr, os.W_OK) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only thing constant is change. Though we have had the DT_SONAME libc.so.6 for 25+ years, I bet it will change in the future. Probably for a dumb reason too. At least musl uses libc.so.1 right now and someone out there is likely to try and make this work on musl.
We have a Python module called pyelftools, available in Fedora as python3-pyelftools. You could do something like this to get the libc SONAME dynamically:
Read the DT_NEEDED entries on the executable running the code, which would be the Python executable. This is likely over-engineering, but constants like this always come back up later to present a problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, maybe we can use
ctypes.CDLL(None)
:https://bugs.python.org/issue34592
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well there you go looking for the easy and shorter non-yak shaving way to do it!