debian-forge/mounts/org.osbuild.ostree.deployment
Christian Kellner a25ae2b1d5 mounts/ostree.deployment: create private tree mount
Create a private mount point for the tree, so that later we can
move the `root` mount point. This is needed since "moving a mount
residing under a shared mount is invalid and unsupported.", see
`mount(8)`. Currently the `tree` is mounted via a private mount-
point since reading the tree is done via bind-mounts, but this
will change in subsequent commits; this prepares for it.
2022-11-21 17:26:53 +01:00

136 lines
3.6 KiB
Python
Executable file

#!/usr/bin/python3
"""
OSTree deployment mount service
This mount service will setup all needed bind mounts so
that a given `tree` will look like an active OSTree
deployment, very much as OSTree does during early boot.
More specifically it will:
- setup the sysroot bindmount to the deployment
- setup the shared var directory
- bind the boot directory into the deployment
Host commands used: mount
"""
import os
import subprocess
import sys
from typing import Dict
from osbuild import mounts
from osbuild.util import ostree
SCHEMA_2 = """
"additionalProperties": false,
"required": ["name", "type"],
"properties": {
"name": { "type": "string" },
"type": { "type": "string" },
"options": {
"type": "object",
"required": ["deployment"],
"properties": {
"deployment": {
"type": "object",
"additionalProperties": false,
"required": ["osname", "ref"],
"properties": {
"osname": {
"description": "Name of the stateroot to be used in the deployment",
"type": "string"
},
"ref": {
"description": "OStree ref to create and use for deployment",
"type": "string"
},
"serial": {
"description": "The deployment serial (usually '0')",
"type": "number",
"default": 0
}
}
}
}
}
}
"""
class OSTreeDeploymentMount(mounts.MountService):
def __init__(self, args):
super().__init__(args)
self.tree = None
self.mountpoint = None
self.check = False
@staticmethod
def bind_mount(source, target):
subprocess.run([
"mount", "--bind", "--make-private", source, target,
], check=True)
return target
def mount(self, args: Dict):
tree = args["tree"]
options = args["options"]
deployment = options["deployment"]
osname = deployment["osname"]
ref = deployment["ref"]
serial = deployment.get("serial", 0)
# create a private mountpoint for the tree, which is needed
# in order to be able to move the `root` mountpoint, which
# is contained inside tree, since "moving a mount residing
# under a shared mount is invalid and unsupported."
# - `mount(8)`
self.tree = self.bind_mount(tree, tree)
root = ostree.deployment_path(tree, osname, ref, serial)
print(f"Deployment root at '{os.path.relpath(root, tree)}'")
var = os.path.join(tree, "ostree", "deploy", osname, "var")
boot = os.path.join(tree, "boot")
self.mountpoint = root
self.bind_mount(root, root) # prepare to move it later
self.bind_mount(tree, os.path.join(root, "sysroot"))
self.bind_mount(var, os.path.join(root, "var"))
self.bind_mount(boot, os.path.join(root, "boot"))
subprocess.run([
"mount", "--move", root, tree,
], check=True)
self.mountpoint = tree
self.check = True
def umount(self):
if self.mountpoint:
subprocess.run(["sync", "-f", self.mountpoint],
check=self.check)
subprocess.run(["umount", "-R", self.mountpoint],
check=self.check)
self.mountpoint = None
if self.tree:
subprocess.run(["umount", "-R", self.tree],
check=self.check)
self.tree = None
def main():
service = OSTreeDeploymentMount.from_args(sys.argv[1:])
service.main()
if __name__ == '__main__':
main()