clavarium 0.2.398-next.1 → 0.2.400-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,47 @@
1
+ #!/bin/sh
2
+ # THE DOOR OF A CONVERSATION IN A SHARED MACHINE (#221), run as root by Clavarium for everything it runs for
3
+ # that conversation: clavarium-enter NAME -- COMMAND [ARGS...]
4
+ #
5
+ # The command runs as the conversation's own user (c-NAME, no sudo), in a mount namespace of its own where
6
+ # /work is THIS conversation's work (the host mounts it at /srv/clavarium/NAME/work),
7
+ # /resources/* are only those given to it (/srv/clavarium/NAME/resources),
8
+ # /tools is the machine's (the same for all, read-only),
9
+ # ~ is /home/c-NAME (closed to the others),
10
+ # /tmp is its own,
11
+ # within a slice of its own (memory, processes: /etc/clavarium/limits/NAME, set by Clavarium). Seen from inside,
12
+ # a conversation is alone in its machine: no tool, no agent needs to know.
13
+ set -eu
14
+ [ "$(id -u)" -eq 0 ] || { echo "clavarium-enter: run as root" >&2; exit 77; }
15
+ name="${1:-}"; shift || true
16
+ [ "${1:-}" = "--" ] && shift
17
+ printf '%s' "$name" | grep -Eq '^[a-z]([a-z0-9-]{0,30}[a-z0-9])?$' || { echo "clavarium-enter: not a conversation name" >&2; exit 64; }
18
+ [ $# -gt 0 ] || { echo "clavarium-enter: no command" >&2; exit 64; }
19
+ user="c-$name"; base="/srv/clavarium/$name"
20
+ # Its user, made at its first entry: a home of its own, closed to the others; no sudo, ever.
21
+ if ! id "$user" >/dev/null 2>&1; then
22
+ useradd --create-home --home-dir "/home/$user" --shell /bin/bash --user-group "$user"
23
+ fi
24
+ chmod 700 "/home/$user"
25
+ mkdir -p "$base/work" "$base/resources" "$base/tmp"
26
+ chown "$user:$user" "$base/tmp"; chmod 700 "$base/tmp"
27
+ # Its limits (memory in MB, processes), when Clavarium set them.
28
+ mem=""; procs=""
29
+ [ -r "/etc/clavarium/limits/$name" ] && . "/etc/clavarium/limits/$name"
30
+ # Into its own mount namespace: its /work, its resources, its /tmp; then its user, in its slice.
31
+ exec unshare --mount --propagation private -- sh -c '
32
+ set -eu
33
+ base="$1"; user="$2"; mem="$3"; procs="$4"; shift 4
34
+ mkdir -p /work /resources
35
+ mount --bind "$base/work" /work
36
+ mount -t tmpfs -o mode=755,size=1m none /resources
37
+ for r in "$base"/resources/*; do [ -e "$r" ] || continue; mkdir -p "/resources/$(basename "$r")"; mount --rbind "$r" "/resources/$(basename "$r")"; done
38
+ mount --bind "$base/tmp" /tmp
39
+ # The processes of the others hidden (a procfs of its own, Linux 5.8+): only its own are seen.
40
+ mount -t proc -o hidepid=invisible proc /proc 2>/dev/null || true
41
+ cd "/home/$user" 2>/dev/null || cd /
42
+ set -- setpriv --reuid="$user" --regid="$user" --init-groups --reset-env -- env HOME="/home/$user" USER="$user" LOGNAME="$user" SHELL=/bin/bash PATH=/usr/local/bin:/usr/bin:/bin "$@"
43
+ if [ -n "$mem$procs" ] && command -v systemd-run >/dev/null 2>&1; then
44
+ exec systemd-run --quiet --scope --slice="clavarium-$user" ${mem:+-p MemoryMax=${mem}M} ${procs:+-p TasksMax=$procs} -- "$@"
45
+ fi
46
+ exec "$@"
47
+ ' clavarium-enter "$base" "$user" "$mem" "$procs" "$@"
@@ -1106,6 +1106,42 @@ case "$AREA $VERB" in
1106
1106
  *) refuse "a mount is of a folder, of a view, of a work folder or of the tools" ;;
1107
1107
  esac
1108
1108
  run "$MULTIPASS" mount -u "$4:$VM_ID" -g "$5:$VM_ID" "$source" "$target" ;;
1109
+ "vm share"|"vm unshare")
1110
+ # A SHARED MACHINE (#221): the conversations that live in it, kept here (DATA/shared/<vm>), so that a
1111
+ # work folder or a resource only ever goes into the machine its conversation lives in. <vm> <conversation>
1112
+ [ $# -eq 2 ] || usage; vm_of "$1"; is_name "$2" || refuse "not a conversation name: $2"
1113
+ [ "$1" != "clv-$2" ] || refuse "a conversation of its own is not shared"
1114
+ list="$DATA/shared/$1"
1115
+ if [ "$SAY" = "yes" ]; then printf '%s %s %s\n' "$VERB" "$list" "$2"; exit 0; fi
1116
+ mkdir -p "$DATA/shared"; touch "$list"
1117
+ if [ "$VERB" = "share" ]; then
1118
+ grep -qx "$2" "$list" || printf '%s\n' "$2" >> "$list"
1119
+ else
1120
+ grep -vx "$2" "$list" > "$list.new" || true; mv "$list.new" "$list"
1121
+ fi
1122
+ exit 0 ;;
1123
+ "vm smount")
1124
+ # <vm> <conversation> <work|folder|view> <resource> <uid> <gid>: into the conversation's own place in a
1125
+ # shared machine (/srv/clavarium/<conversation>/...), where its door (clavarium-enter) finds it -- only for
1126
+ # a conversation that lives in that machine.
1127
+ [ $# -eq 6 ] || usage; vm_of "$1"; is_name "$2" || refuse "not a conversation name: $2"; is_name "$4" || refuse "not a folder name: $4"; number "$5"; number "$6"
1128
+ [ -f "$DATA/shared/$1" ] && grep -qx "$2" "$DATA/shared/$1" || refuse "$2 does not live in $1"
1129
+ case "$3" in
1130
+ work) [ "$4" = "$2" ] || refuse "a work folder belongs to its own conversation"
1131
+ kept work; source="$MNT/work/$2"; held "$source"
1132
+ [ "$SAY" = "yes" ] || mountpoint -q "$source" || refuse "the work disk of $2 is not in place"
1133
+ target="$1:/srv/clavarium/$2/work" ;;
1134
+ folder) folder_of "$4"; source="$MNT/resources/$4"; target="$1:/srv/clavarium/$2/resources/$4" ;;
1135
+ view) kept views; source="$MNT/views/$4"; held "$source"
1136
+ [ "$SAY" = "yes" ] || mountpoint -q "$source" || refuse "the read-only view of $4 is not in place"
1137
+ target="$1:/srv/clavarium/$2/resources/$4" ;;
1138
+ *) refuse "a shared mount is of a work folder, a folder or a view" ;;
1139
+ esac
1140
+ run "$MULTIPASS" mount -u "$5:$VM_ID" -g "$6:$VM_ID" "$source" "$target" ;;
1141
+ "vm sumount")
1142
+ # <vm> <conversation> <work|resource name>: taken out of its place in a shared machine.
1143
+ [ $# -eq 3 ] || usage; vm_of "$1"; is_name "$2" || refuse "not a conversation name: $2"; is_name "$3" || refuse "not a name: $3"
1144
+ if [ "$3" = "work" ]; then run "$MULTIPASS" umount "$1:/srv/clavarium/$2/work"; else run "$MULTIPASS" umount "$1:/srv/clavarium/$2/resources/$3"; fi ;;
1109
1145
  "vm unwork")
1110
1146
  # The work folder is taken out of its machine (its work moves to the
1111
1147
  # machine's own disk, or a move is undone).
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.398-next.1",
3
+ "version": "0.2.400-next.1",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "clavarium",
9
- "version": "0.2.398-next.1",
9
+ "version": "0.2.400-next.1",
10
10
  "license": "MIT",
11
11
  "dependencies": {
12
12
  "@xterm/addon-fit": "^0.11.0",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.398-next.1",
3
+ "version": "0.2.400-next.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "description": "A conversation is a VM with a terminal in it. Everything else is a folder.",
package/server/main.mjs CHANGED
@@ -310,8 +310,10 @@ export async function startHome({ port, host, dataDir, mntDir = dataDir, gitRese
310
310
  const instance = new Instance({ file: new JsonFile(`${dataDir}/instance.json`, {}), origin: publicOrigin, given: appName });
311
311
  let repairTerminal = async () => { throw new Error('not ready'); };
312
312
  const asHost = runner(host);
313
- const vms = new Vms(asHost, host);
314
- const all = conversations ?? new Conversations({ store: new Store(`${dataDir}/conversations.json`), layout: new JsonFile(`${dataDir}/layout.json`, {}), vms, tools, ...(stopDelayMs === undefined ? {} : { stopDelayMs }) });
313
+ /* A conversation in a shared machine (#221) says which: the machine its VM operations reach. */
314
+ const conversationStore = new Store(`${dataDir}/conversations.json`);
315
+ const vms = new Vms(asHost, host, { machineOf: (name) => { const m = conversationStore.get(name)?.machine; return typeof m === 'string' && m ? m : undefined; } });
316
+ const all = conversations ?? new Conversations({ store: conversationStore, layout: new JsonFile(`${dataDir}/layout.json`, {}), vms, tools, ...(stopDelayMs === undefined ? {} : { stopDelayMs }) });
315
317
  /* Who may do what with what: ONE table, shared by everything below. */
316
318
  const grants = new Grants(new Store(`${dataDir}/grants.json`));
317
319
  /* Folder-style boxes the owner drops assets into, to tidy the views and hand a whole box at once (groups.mjs). */
package/server/vm.mjs CHANGED
@@ -84,10 +84,11 @@ export class VmUnknown extends Error {
84
84
  export class Vms {
85
85
  /* `run(argv, options)` reaches the host program; `prefix` is the same
86
86
  route as an argument vector, for the one caller that needs a terminal. */
87
- #ask; #snapshot = null; #asking = null; #now; #freshMs;
87
+ #ask; #snapshot = null; #asking = null; #now; #freshMs; #machineOf;
88
88
  /* `freshMs`: how long one answer serves every caller (0: only those asking meanwhile). */
89
- constructor(run, prefix, { now = () => Date.now(), freshMs = STATES_FRESH_MS } = {}) {
90
- this.#ask = run; this.prefix = prefix; this.#now = now; this.#freshMs = freshMs;
89
+ /* `machineOf(conversation)`: the shared machine it lives in (#221), or nothing -- its own, `clv-<conversation>`. */
90
+ constructor(run, prefix, { now = () => Date.now(), freshMs = STATES_FRESH_MS, machineOf = () => undefined } = {}) {
91
+ this.#ask = run; this.prefix = prefix; this.#now = now; this.#freshMs = freshMs; this.#machineOf = machineOf;
91
92
  /* What changes a machine makes the states asked again (not taken from before it). */
92
93
  this.run = async (argv, options) => {
93
94
  const changes = argv[0] === 'vm' && CHANGES.has(argv[1]);
@@ -115,25 +116,38 @@ export class Vms {
115
116
  return this.#asking;
116
117
  }
117
118
 
119
+ /* ITS MACHINE (#221): a conversation's own (`clv-<name>`, as always), or the shared one it lives in. */
120
+ vmOf(conversation) {
121
+ const shared = this.#machineOf(conversation);
122
+ if (shared === undefined) return vmName(conversation);
123
+ vmName(conversation); /* its own name checked all the same */
124
+ return vmName(shared);
125
+ }
126
+ /* In a shared machine, everything run for a conversation goes through its door (clavarium-enter, as root):
127
+ its own /work, its own home and user, its limits -- each tool sees what it sees in a machine of its own. */
128
+ #door(conversation, argv) {
129
+ return this.#machineOf(conversation) === undefined ? argv : ['sudo', '-n', 'clavarium-enter', conversation, '--', ...argv];
130
+ }
131
+
118
132
  launch(conversation, memoryMb, diskGb, cpus) {
119
133
  /* The disk and cores go with the memory only when they are the chosen size, not the defaults. */
120
134
  const size = diskGb && cpus ? [String(diskGb), String(cpus)] : [];
121
- return this.run(['vm', 'launch', vmName(conversation), String(memoryMb), ...size], { timeoutMs: (LAUNCH_SECONDS + 60) * 1000 });
135
+ return this.run(['vm', 'launch', this.vmOf(conversation), String(memoryMb), ...size], { timeoutMs: (LAUNCH_SECONDS + 60) * 1000 });
122
136
  }
123
137
 
124
138
  /* The machine of a conversation as a COPY of the ready-made one (see
125
139
  shelf.mjs), with its own memory. Left stopped: started by the caller. */
126
140
  clone(conversation, shelf, memoryMb) {
127
- return this.run(['vm', 'clone', shelf, vmName(conversation), String(memoryMb)], { timeoutMs: CLONE_MS });
141
+ return this.run(['vm', 'clone', shelf, this.vmOf(conversation), String(memoryMb)], { timeoutMs: CLONE_MS });
128
142
  }
129
143
 
130
144
  exec(conversation, argv, options) {
131
- return this.run(['vm', 'exec', vmName(conversation), '--', ...argv], options);
145
+ return this.run(['vm', 'exec', this.vmOf(conversation), '--', ...this.#door(conversation, argv)], options);
132
146
  }
133
147
 
134
148
  /* What a terminal attaches to: the same `exec`, as an argument vector. */
135
149
  execArgv(conversation, argv) {
136
- return [...this.prefix, 'vm', 'exec', vmName(conversation), '--', ...argv];
150
+ return [...this.prefix, 'vm', 'exec', this.vmOf(conversation), '--', ...this.#door(conversation, argv)];
137
151
  }
138
152
 
139
153
  /* Every machine's state, by VM name (a Map); throws VmUnknown when the VM manager did not answer. */
@@ -150,7 +164,7 @@ export class Vms {
150
164
  frozen inside had "multipass info" fail, read as "no longer exists"). */
151
165
  /* `fresh`: asked now (an owner opening it, building it again), not taken from a few seconds ago. */
152
166
  async state(conversation, { fresh = false } = {}) {
153
- const name = vmName(conversation);
167
+ const name = this.vmOf(conversation);
154
168
  const snap = await this.#states(fresh);
155
169
  if (!snap.states) throw new VmUnknown(snap.why);
156
170
  return snap.states.get(name);
@@ -162,57 +176,57 @@ export class Vms {
162
176
  is on the host is decided by the host program, not here. `uid`/`gid`:
163
177
  files written in the VM belong, on the host, to this account. */
164
178
  mount(conversation, kind, name, uid, gid) {
165
- return this.run(['vm', 'mount', vmName(conversation), kind, name, String(uid), String(gid)], { timeoutMs: 600_000 });
179
+ return this.run(['vm', 'mount', this.vmOf(conversation), kind, name, String(uid), String(gid)], { timeoutMs: 600_000 });
166
180
  }
167
181
 
168
182
  /* The work folder of a conversation: kept on the host, shown in its
169
183
  machine at /work. Made (or found again) before it is mounted. */
170
- workMake(conversation) { vmName(conversation); return this.run(['work', 'make', conversation]); }
184
+ workMake(conversation) { this.vmOf(conversation); return this.run(['work', 'make', conversation]); }
171
185
  workMount(conversation, uid, gid) {
172
- return this.run(['vm', 'mount', vmName(conversation), 'work', conversation, String(uid), String(gid)], { timeoutMs: 600_000 });
186
+ return this.run(['vm', 'mount', this.vmOf(conversation), 'work', conversation, String(uid), String(gid)], { timeoutMs: 600_000 });
173
187
  }
174
- workUnmount(conversation) { return this.run(['vm', 'unwork', vmName(conversation)], { timeoutMs: 120_000 }); }
188
+ workUnmount(conversation) { return this.run(['vm', 'unwork', this.vmOf(conversation)], { timeoutMs: 120_000 }); }
175
189
  /* Megabytes left: { disk, host }, or undefined if it cannot be told. */
176
190
  async workRoom(conversation) {
177
- vmName(conversation);
191
+ this.vmOf(conversation);
178
192
  const r = await this.run(['work', 'room', conversation]);
179
193
  const m = /^disk (\d+) host (\d+)$/m.exec(r.stdout ?? '');
180
194
  return r.code === 0 && m ? { disk: Number(m[1]), host: Number(m[2]) } : undefined;
181
195
  }
182
- workAside(conversation) { vmName(conversation); return this.run(['work', 'aside', conversation]); }
196
+ workAside(conversation) { this.vmOf(conversation); return this.run(['work', 'aside', conversation]); }
183
197
  /* Deleted for good, or reset, by its owner: its work disk erased, not put aside. */
184
- workErase(conversation) { vmName(conversation); return this.run(['work', 'erase', conversation]); }
198
+ workErase(conversation) { this.vmOf(conversation); return this.run(['work', 'erase', conversation]); }
185
199
 
186
200
  /* The tools folder (see toolbox.mjs), shown read-only at /tools. */
187
201
  mountTools(conversation, uid, gid) {
188
- return this.run(['vm', 'mount', vmName(conversation), 'tools', 'tools', String(uid), String(gid)], { timeoutMs: 600_000 });
202
+ return this.run(['vm', 'mount', this.vmOf(conversation), 'tools', 'tools', String(uid), String(gid)], { timeoutMs: 600_000 });
189
203
  }
190
- unmountTools(conversation) { return this.run(['vm', 'untools', vmName(conversation)], { timeoutMs: 120_000 }); }
204
+ unmountTools(conversation) { return this.run(['vm', 'untools', this.vmOf(conversation)], { timeoutMs: 120_000 }); }
191
205
 
192
206
  umount(conversation, name) {
193
- return this.run(['vm', 'umount', vmName(conversation), name], { timeoutMs: 120_000 });
207
+ return this.run(['vm', 'umount', this.vmOf(conversation), name], { timeoutMs: 120_000 });
194
208
  }
195
209
 
196
210
  /* `signal`: a start given up (Force stop asked meanwhile). */
197
- start(conversation, { signal } = {}) { return this.run(['vm', 'start', vmName(conversation)], { timeoutMs: 300_000, ...(signal ? { signal } : {}) }); }
198
- stop(conversation, timeoutMs = 300_000) { return this.run(['vm', 'stop', vmName(conversation)], { timeoutMs }); }
211
+ start(conversation, { signal } = {}) { return this.run(['vm', 'start', this.vmOf(conversation)], { timeoutMs: 300_000, ...(signal ? { signal } : {}) }); }
212
+ stop(conversation, timeoutMs = 300_000) { return this.run(['vm', 'stop', this.vmOf(conversation)], { timeoutMs }); }
199
213
  /* Stopped without waiting for the system inside: after a clean stop hung. */
200
- kill(conversation) { return this.run(['vm', 'kill', vmName(conversation)], { timeoutMs: 300_000 }); }
214
+ kill(conversation) { return this.run(['vm', 'kill', this.vmOf(conversation)], { timeoutMs: 300_000 }); }
201
215
  /* STOPPED FOR SURE, step by step (the host program's `vm halt`): a polite stop, a stop by force,
202
216
  then its own qemu ended (SIGTERM, SIGKILL), then checked. `from`/`until`: the first and last
203
217
  step (polite|force|term|kill; until force|kill). Each `step ...` line is told to `onLine`. */
204
218
  halt(conversation, { from = 'polite', until = 'kill', onLine, signal } = {}) {
205
- return this.run(['vm', 'halt', vmName(conversation), from, until], { timeoutMs: HALT_MS, onLine, signal });
219
+ return this.run(['vm', 'halt', this.vmOf(conversation), from, until], { timeoutMs: HALT_MS, onLine, signal });
206
220
  }
207
221
  /* "Force stop", as a power cut: its qemu ended, nothing asked of the VM manager (`vm hardstop`). */
208
- hardstop(conversation, { onLine, signal } = {}) { return this.run(['vm', 'hardstop', vmName(conversation)], { timeoutMs: HARDSTOP_MS, onLine, signal }); }
222
+ hardstop(conversation, { onLine, signal } = {}) { return this.run(['vm', 'hardstop', this.vmOf(conversation)], { timeoutMs: HARDSTOP_MS, onLine, signal }); }
209
223
  /* Memory in megabytes, disk in gigabytes: only on a stopped machine. */
210
- setMemory(conversation, mb) { return this.run(['vm', 'set', vmName(conversation), 'memory', String(mb)], { timeoutMs: 120_000 }); }
211
- setCpus(conversation, n) { return this.run(['vm', 'set', vmName(conversation), 'cpus', String(n)], { timeoutMs: 120_000 }); }
212
- setDisk(conversation, gb) { return this.run(['vm', 'set', vmName(conversation), 'disk', String(gb)], { timeoutMs: 600_000 }); }
224
+ setMemory(conversation, mb) { return this.run(['vm', 'set', this.vmOf(conversation), 'memory', String(mb)], { timeoutMs: 120_000 }); }
225
+ setCpus(conversation, n) { return this.run(['vm', 'set', this.vmOf(conversation), 'cpus', String(n)], { timeoutMs: 120_000 }); }
226
+ setDisk(conversation, gb) { return this.run(['vm', 'set', this.vmOf(conversation), 'disk', String(gb)], { timeoutMs: 600_000 }); }
213
227
  /* What the machine really has: { memoryMb, diskGb }, each undefined when it cannot be told. */
214
228
  async size(conversation) {
215
- const r = await this.run(['vm', 'size', vmName(conversation)], { timeoutMs: 60_000 });
229
+ const r = await this.run(['vm', 'size', this.vmOf(conversation)], { timeoutMs: 60_000 });
216
230
  if (r.code !== 0) return { memoryMb: undefined, diskGb: undefined };
217
231
  const line = (key) => new RegExp(`^${key} (\\S+)$`, 'm').exec(r.stdout ?? '')?.[1];
218
232
  const mb = megabytes(line('memory'));
@@ -221,9 +235,9 @@ export class Vms {
221
235
  return { memoryMb: mb === undefined ? undefined : Math.round(mb), diskGb: disk === undefined ? undefined : Math.round(disk / 1024), cpus };
222
236
  }
223
237
  /* Its steps (`step suspend ...`, then a wake and sleep again, then a clean stop) told to `onLine`. */
224
- suspend(conversation, { signal, onLine } = {}) { return this.run(['vm', 'suspend', vmName(conversation)], { timeoutMs: 600_000, ...(signal ? { signal } : {}), ...(onLine ? { onLine } : {}) }); }
238
+ suspend(conversation, { signal, onLine } = {}) { return this.run(['vm', 'suspend', this.vmOf(conversation)], { timeoutMs: 600_000, ...(signal ? { signal } : {}), ...(onLine ? { onLine } : {}) }); }
225
239
  /* Free the RAM of a machine that is asleep but whose qemu still holds it (a
226
240
  suspend that did not end the process): wake+resleep, then a clean stop. */
227
- reclaim(conversation) { return this.run(['vm', 'reclaim', vmName(conversation)], { timeoutMs: 300_000 }); }
228
- remove(conversation) { return this.run(['vm', 'delete', vmName(conversation)], { timeoutMs: 300_000 }); }
241
+ reclaim(conversation) { return this.run(['vm', 'reclaim', this.vmOf(conversation)], { timeoutMs: 300_000 }); }
242
+ remove(conversation) { return this.run(['vm', 'delete', this.vmOf(conversation)], { timeoutMs: 300_000 }); }
229
243
  }