From 3c8b485d32f080d38d5cdde08e0ccd37d78c02cb Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Wed, 29 Nov 2023 22:22:58 +0200 Subject: [PATCH] Add mount_actor Synit utility for mounting file systems. --- README.md | 43 +++++++++++++++++++++++++++++++++++++ mountpoints.prs | 5 +++++ src/mount_actor.nim | 44 ++++++++++++++++++++++++++++++++++++++ src/schema/mountpoints.nim | 30 ++++++++++++++++++++++++++ syndicate_utils.nimble | 4 ++-- 5 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 mountpoints.prs create mode 100644 src/mount_actor.nim create mode 100644 src/schema/mountpoints.nim diff --git a/README.md b/README.md index c50feb9..2e12d57 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,49 @@ Do not send messages immediately to the dataspace passed `json_socket_translator A utility for minting [Sturdyrefs](https://synit.org/book/operation/builtin/gatekeeper.html#sturdyrefs). +## mount_actor + +Actor for mounting filesystems on Linux. + +Sample Syndicate server script: +``` +# Assert a file-system we want to mount. + + +# Transform mount assertions into mount status observations. +? [ + ? [ ] +] + +# Assert mounting succeded. +? [ + ready> +] +# Assert mount failed. +? > [ + failed> +] + +# Assert the details into the machine dataspace. +? [ + $config ? [ + $machine + ] +] + +# Require the mount_actor daemon. +> + + +# Pass the daemon the config dataspace. +? ?cap> [ + $cap { dataspace: $config } +] +``` + ## msg A utility that sends messages to `$SYNDICATE_ROUTE`. diff --git a/mountpoints.prs b/mountpoints.prs new file mode 100644 index 0000000..2e18d45 --- /dev/null +++ b/mountpoints.prs @@ -0,0 +1,5 @@ +version 1. + +Mountpoint = . +Status = Failure / #t. +Failure = . diff --git a/src/mount_actor.nim b/src/mount_actor.nim new file mode 100644 index 0000000..56cb5f2 --- /dev/null +++ b/src/mount_actor.nim @@ -0,0 +1,44 @@ +# SPDX-FileCopyrightText: ☭ Emery Hemingway +# SPDX-License-Identifier: Unlicense + +## An actor for Linux file-system mounting. + +when not defined(linux): + {.error: "this component only tested for Linux".} + +import std/oserrors +import preserves +import syndicate, syndicate/relays +import ./schema/mountpoints + +type BootArgs {.preservesDictionary.} = object + dataspace: Cap + +proc mount(source, target, fsType: cstring; flags: culong; data: pointer): cint {.importc, header: "".} + ## `mount(2)` + +proc umount(target: cstring): cint {.importc, header: "".} + ## `umount(2)` + +runActor("mount_actor") do (turn: var Turn; root: Cap): + let + targetPat = ?Observe(pattern: !Mountpoint) ?? { 1: grabLit() } + sourcePat = ?Observe(pattern: !Mountpoint) ?? { 0: grabLit(), 2: grabLit() } + connectStdio(turn, root) + during(turn, root, ?BootArgs) do (ds: Cap): + during(turn, ds, targetPat) do (target: string): + during(turn, ds, sourcePat) do (source: string, fsType: string): + var mountpoint = Mountpoint( + source: source, + target: target, + `type`: fsType, + ) + var rc = mount(source, target, fsType, 0, nil) + if rc == 0: + mountpoint.status = Status(orKind: StatusKind.true) + else: + mountpoint.status = Status(orKind: StatusKind.Failure) + mountpoint.status.failure.msg = osErrorMsg(osLastError()) + discard publish(turn, ds, mountpoint) + do: + discard umount(target) diff --git a/src/schema/mountpoints.nim b/src/schema/mountpoints.nim new file mode 100644 index 0000000..d936376 --- /dev/null +++ b/src/schema/mountpoints.nim @@ -0,0 +1,30 @@ + +import + preserves + +type + Failure* {.preservesRecord: "failure".} = object + `msg`*: string + + Mountpoint* {.preservesRecord: "mount".} = object + `source`*: string + `target`*: string + `type`*: string + `status`*: Status + + StatusKind* {.pure.} = enum + `Failure`, `true` + `Status`* {.preservesOr.} = object + case orKind*: StatusKind + of StatusKind.`Failure`: + `failure`*: Failure + + of StatusKind.`true`: + `true`* {.preservesLiteral: "#t".}: bool + + +proc `$`*(x: Failure | Mountpoint | Status): string = + `$`(toPreserve(x)) + +proc encode*(x: Failure | Mountpoint | Status): seq[byte] = + encode(toPreserve(x)) diff --git a/syndicate_utils.nimble b/syndicate_utils.nimble index 86d1104..b3095b6 100644 --- a/syndicate_utils.nimble +++ b/syndicate_utils.nimble @@ -1,11 +1,11 @@ # Package -version = "20231108" +version = "20231129" author = "Emery Hemingway" description = "Utilites for Syndicated Actors and Synit" license = "unlicense" srcDir = "src" -bin = @["cache_actor", "json_socket_translator", "json_translator", "mintsturdyref", "msg", "net_mapper", "preserve_process_environment", "syndex_card", "syndump"] +bin = @["cache_actor", "json_socket_translator", "json_translator", "mintsturdyref", "mount_actor", "msg", "net_mapper", "preserve_process_environment", "syndex_card", "syndump"] # Dependencies