From 5093fc8a086feda0e5204e21f6c99959b36860d7 Mon Sep 17 00:00:00 2001 From: Alyssa Ross Date: Sat, 10 Apr 2021 00:15:16 +0000 Subject: spectrumPackages.sys-vms.app: init This is a very barebones VM. It's purpose is just to be a demonstration that other VMs can connect to the router and have their packets reach the network. So all it does is infer its IPv4 address, and the IPv4 address of the router, from the MAC address of its virtual ethernet device, and configure the network interfaces and routes appropriately. vmID is an integer seed we can use to derive things for the VM like IP and VSOCK addresses. I don't foresee this sitting around, because I think it would make more sense for this to be assigned at runtime, since starting arbitrary VMs at runtime is a goal. But we'll need some way to ensure unique addresses. Message-Id: <20210411115740.29615-16-hi@alyssa.is> Reviewed-by: Cole Helbling --- pkgs/os-specific/linux/spectrum/vm/app/default.nix | 63 ++++++++++++++++++++++ pkgs/os-specific/linux/spectrum/vm/default.nix | 2 + 2 files changed, 65 insertions(+) create mode 100644 pkgs/os-specific/linux/spectrum/vm/app/default.nix diff --git a/pkgs/os-specific/linux/spectrum/vm/app/default.nix b/pkgs/os-specific/linux/spectrum/vm/app/default.nix new file mode 100644 index 00000000000..65dbb51f5e1 --- /dev/null +++ b/pkgs/os-specific/linux/spectrum/vm/app/default.nix @@ -0,0 +1,63 @@ +{ runCommand, writeScript, writeText, makeRootfs +, busybox, execline, linux_vm, jq, iproute +}: + +runCommand "vm-app" rec { + linux = linux_vm; + + login = writeScript "login" '' + #! ${execline}/bin/execlineb -s0 + unexport ! + ${busybox}/bin/login -p -f root $@ + ''; + + rootfs = makeRootfs { + rcServices.ok-all = { + type = writeText "ok-all-type" '' + bundle + ''; + contents = writeText "ok-all-contents" '' + net + ''; + }; + + rcServices.net = { + type = writeText "net-type" '' + oneshot + ''; + up = writeText "net-up" '' + backtick -i LOCAL_IP { + pipeline { ip -j link show eth0 } + pipeline { jq -r ".[0].address | split(\":\") | .[3:6] | \"0x\" + .[]" } + xargs printf "100.%d.%d.%d" + } + importas -iu LOCAL_IP LOCAL_IP + + backtick -i REMOTE_IP { + jq -jn --arg localip $LOCAL_IP + "$localip | split(\".\") | .[3] |= tonumber - 1 | join(\".\")" + } + importas -iu REMOTE_IP REMOTE_IP + + if { ip address add ''${LOCAL_IP}/31 dev eth0 } + if { ip link set eth0 up } + ip route add default via $REMOTE_IP + ''; + }; + + services.getty.run = writeScript "getty-run" '' + #! ${execline}/bin/execlineb -P + ${busybox}/bin/getty -i -n -l ${login} 38400 ttyS0 + ''; + + path = [ iproute jq ]; + }; + + inherit (rootfs) squashfs; + vmID = 0; +} '' + mkdir $out + echo "$vmID" > $out/vm-id + ln -s $linux/bzImage $out/kernel + ln -s $squashfs $out/squashfs +'' diff --git a/pkgs/os-specific/linux/spectrum/vm/default.nix b/pkgs/os-specific/linux/spectrum/vm/default.nix index c4ff729cb8a..f5d591a960a 100644 --- a/pkgs/os-specific/linux/spectrum/vm/default.nix +++ b/pkgs/os-specific/linux/spectrum/vm/default.nix @@ -1,6 +1,8 @@ { callPackage }: { + app = callPackage ./app { }; + comp = callPackage ./comp { }; net = callPackage ./net { }; -- cgit 1.4.1