mounts: separate file system mount service

Separate the current `MountService` into the more generic base mount
service and a specialized one for file systems.
This commit is contained in:
Christian Kellner 2021-10-08 16:18:23 +00:00 committed by Tom Gundersen
parent 7e776a0763
commit 08c1fbad4b
6 changed files with 35 additions and 19 deletions

View file

@ -33,7 +33,7 @@ SCHEMA_2 = """
"""
class BtrfsMount(mounts.MountService):
class BtrfsMount(mounts.FileSystemMountService):
def translate_options(self, _options: Dict):
return ["-t", "btrfs"]

View file

@ -33,7 +33,7 @@ SCHEMA_2 = """
"""
class Ext4Mount(mounts.MountService):
class Ext4Mount(mounts.FileSystemMountService):
def translate_options(self, _options: Dict):
return ["-t", "ext4"]

View file

@ -33,7 +33,7 @@ SCHEMA_2 = """
"""
class XfsMount(mounts.MountService):
class XfsMount(mounts.FileSystemMountService):
def translate_options(self, _options: Dict):
return ["-t", "vfat"]

View file

@ -37,7 +37,9 @@ SCHEMA_2 = """
class NoOpMount(mounts.MountService):
def mount(self, _source: str, root: str, target: str, _options: Dict):
def mount(self, args: Dict):
root = args["root"]
target = args["target"]
mountpoint = os.path.join(root, target.lstrip("/"))

View file

@ -33,7 +33,7 @@ SCHEMA_2 = """
"""
class XfsMount(mounts.MountService):
class XfsMount(mounts.FileSystemMountService):
def translate_options(self, _options: Dict):
return ["-t", "xfs"]

View file

@ -87,6 +87,28 @@ class MountManager:
class MountService(host.Service):
"""Mount host service"""
@abc.abstractmethod
def mount(self, args: Dict):
"""Mount a device"""
@abc.abstractmethod
def umount(self):
"""Unmount all mounted resources"""
def stop(self):
self.umount()
def dispatch(self, method: str, args, _fds):
if method == "mount":
r = self.mount(args)
return r, None
raise host.ProtocolError("Unknown method")
class FileSystemMountService(MountService):
"""Specialized mount host service for file system mounts"""
def __init__(self, args):
super().__init__(args)
@ -97,7 +119,12 @@ class MountService(host.Service):
def translate_options(self, options: Dict):
return []
def mount(self, source: str, root: str, target: str, options: Dict):
def mount(self, args: Dict):
source = args["source"]
target = args["target"]
root = args["root"]
options = args["options"]
mountpoint = os.path.join(root, target.lstrip("/"))
args = self.translate_options(options)
@ -132,16 +159,3 @@ class MountService(host.Service):
def sync(self):
subprocess.run(["sync", "-f", self.mountpoint],
check=self.check)
def stop(self):
self.umount()
def dispatch(self, method: str, args, _fds):
if method == "mount":
r = self.mount(args["source"],
args["root"],
args["target"],
args["options"])
return r, None
raise host.ProtocolError("Unknown method")