#!/bin/execlineb -W # SPDX-License-Identifier: EUPL-1.2+ # SPDX-FileCopyrightText: 2024-2025 Alyssa Ross backtick id { backtick -E dir { mktemp -d /run/vm/by-id/XXXXXX } basename -- $dir } if { importas -Siu id if { useradd -P /run -Urd / -s /bin/nologin gpu-${id} } if { useradd -P /run -Urd / -s /bin/nologin -G tty,vmm vmm-${id} } if { useradd -P /run -Urd / -s /bin/nologin xdp-spectrum-${id} } if { mkdir /run/vsock/${id} } if { chown vmm-${id} /run/vm/by-id/${id} /run/vsock/${id} } if { install -do fs /run/configs/${id}/fs } if { ln -s /usr/lib/spectrum/img/appvm/blk /usr/lib/spectrum/img/appvm/vmlinux /run/configs/${id} } if { ln -s /run/configs/${id} /run/vm/by-id/${id}/config } if { create-vm-dependencies $id } } backtick diskdir { backtick mountpoint { importas -Siu 1 findmnt -no TARGET -T $1 } if -tn { redirfd -w 2 /dev/null multisubstitute { importas -Siu mountpoint importas -Siu 2 } printf "%s/Spectrum/data/spectrum/storage/persist.%s\n" $mountpoint $2 } s6-setuidgid fs importas -Siu mountpoint if { mkdir -p -- ${mountpoint}/Spectrum/data/spectrum/storage } mktemp -d -- ${mountpoint}/Spectrum/data/spectrum/storage/tmp.XXXXXX } if { s6-envuidgid fs s6-applyuidgid -Uzu 0 multisubstitute { importas -Siu id importas -Siu 1 importas -Siu diskdir } nsenter --preserve-credentials -S0 --mount=/run/vm/by-id/${id}/ns/mnt --user=/run/vm/by-id/${id}/ns/user cd /run/fs/${id} if { redirfd -w 1 config/type echo appimage } if { touch config/run } if { mount --bind $1 config/run } mount --bind -- $diskdir disk } multisubstitute { importas -Siu diskdir importas -Siu id } piperw 4 3 background { fdclose 3 fdmove 0 4 # Wait for the VMM to be up, then start the VM. if { redirfd -w 1 /dev/null head -1 } vm-start $id } fdclose 4 foreground { run-vmm $id } fdclose 3 if { # Even if s6-instance-delete fails, cgroup-purge is enough # to ensure that the VMM and all per-VM services are killed. foreground { s6-instance-delete /run/service/vm-services $id } cgroup-purge /vm-services.slice/vm-${id}.slice } if { umount -R /run/vm/by-id/${id}/ns } rm -r -- $diskdir /run/vm/by-id/${id} /run/configs/${id}