summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlyssa Ross <hi@alyssa.is>2021-04-10 00:15:16 +0000
committerAlyssa Ross <hi@alyssa.is>2021-04-14 23:42:58 +0000
commit5093fc8a086feda0e5204e21f6c99959b36860d7 (patch)
treee2025edadfbe6b8aca795cedbaa81a5caaadc3a3
parenta71eb59d8acf8f02328db002b89d4db78bb9ae56 (diff)
downloadnixpkgs-5093fc8a086feda0e5204e21f6c99959b36860d7.tar
nixpkgs-5093fc8a086feda0e5204e21f6c99959b36860d7.tar.gz
nixpkgs-5093fc8a086feda0e5204e21f6c99959b36860d7.tar.bz2
nixpkgs-5093fc8a086feda0e5204e21f6c99959b36860d7.tar.lz
nixpkgs-5093fc8a086feda0e5204e21f6c99959b36860d7.tar.xz
nixpkgs-5093fc8a086feda0e5204e21f6c99959b36860d7.tar.zst
nixpkgs-5093fc8a086feda0e5204e21f6c99959b36860d7.zip
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 <cole.e.helbling@outlook.com>
-rw-r--r--pkgs/os-specific/linux/spectrum/vm/app/default.nix63
-rw-r--r--pkgs/os-specific/linux/spectrum/vm/default.nix2
2 files changed, 65 insertions, 0 deletions
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 { };