Compare commits

..

7 Commits

Author SHA1 Message Date
e318dfd2f6 shrink boot media footprint 2025-07-27 11:11:40 -04:00
31296f5b0f add poweroff and reboot functions 2025-07-27 07:54:18 -04:00
5d35d19543 clean up commented code 2025-07-27 07:07:26 -04:00
28d4cf288e pxe boot in flake 2025-07-27 06:27:24 -04:00
bc572173d7 facts: add APIVersion constant 2025-07-24 21:51:46 -04:00
e659349290 add qemu testing to flake 2025-07-24 19:01:11 -04:00
96f54930ee fix boot issues on hyper-v 2025-07-24 15:54:34 -04:00
6 changed files with 150 additions and 7 deletions

View File

@@ -7,6 +7,8 @@ import (
"github.com/zcalusic/sysinfo"
)
const APIVersion string = "facts/v1alpha1"
type FactsResponse struct {
APIVersion string `json:"apiVersion"`
CPUInfo sysinfo.CPU `json:"cpu"`
@@ -24,7 +26,7 @@ func GetFacts(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
resp := FactsResponse{
APIVersion: "v1",
APIVersion: APIVersion,
CPUInfo: si.CPU,
BoardInfo: si.Board,
DeviceInfo: si.Product,

9
client/power/power.go Normal file
View File

@@ -0,0 +1,9 @@
package power
const APIVersion string = "power/v1alpha1"
type PowerActionResponse struct {
APIVersion string `json:"apiVersion"`
Action string `json:"action"`
Success bool `json:"success"`
}

30
client/power/poweroff.go Normal file
View File

@@ -0,0 +1,30 @@
package power
import (
"net/http"
"os/exec"
"git.dubyatp.xyz/orphanage/client/httputil"
)
func PowerOffResponse(success bool) PowerActionResponse {
resp := PowerActionResponse{
APIVersion: APIVersion,
Action: "poweroff",
Success: success,
}
return resp
}
func PowerOff(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cmd := exec.Command("systemctl", "poweroff")
err := cmd.Run()
if err != nil {
httputil.WriteJSON(w, http.StatusOK, PowerOffResponse(false))
} else {
httputil.WriteJSON(w, http.StatusOK, PowerOffResponse(true))
}
})
}

30
client/power/reboot.go Normal file
View File

@@ -0,0 +1,30 @@
package power
import (
"net/http"
"os/exec"
"git.dubyatp.xyz/orphanage/client/httputil"
)
func RebootResponse(success bool) PowerActionResponse {
resp := PowerActionResponse{
APIVersion: APIVersion,
Action: "reboot",
Success: success,
}
return resp
}
func Reboot(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cmd := exec.Command("systemctl", "reboot")
err := cmd.Run()
if err != nil {
httputil.WriteJSON(w, http.StatusOK, RebootResponse(false))
} else {
httputil.WriteJSON(w, http.StatusOK, RebootResponse(true))
}
})
}

View File

@@ -5,14 +5,17 @@ import (
"git.dubyatp.xyz/orphanage/client/facts"
"git.dubyatp.xyz/orphanage/client/httputil"
"git.dubyatp.xyz/orphanage/client/power"
"git.dubyatp.xyz/orphanage/client/testfunc"
)
func AddRoutes(
mux *http.ServeMux,
) {
mux.Handle("/", http.NotFoundHandler())
mux.Handle("/helloworld", httputil.HelloWorld(nil))
mux.Handle("/testjson", testfunc.HelloWorldJSON(nil))
mux.Handle("/facts", facts.GetFacts(nil))
mux.Handle("GET /", http.NotFoundHandler())
mux.Handle("GET /helloworld", httputil.HelloWorld(nil))
mux.Handle("GET /testjson", testfunc.HelloWorldJSON(nil))
mux.Handle("GET /facts", facts.GetFacts(nil))
mux.Handle("POST /power/reboot", power.Reboot(nil))
mux.Handle("POST /power/poweroff", power.PowerOff(nil))
}

View File

@@ -42,9 +42,9 @@
system.stateVersion = "25.05";
boot.initrd.kernelModules = ["hv_vmbus" "hv_storvsc"]; # Hyper-V Support
# Disable unneeded features
##boot.loader.grub.enable = true; # Not needed as iso-image.nix in modulesPath defines these and cause conflict
##boot.loader.grub.device = "nodev";
documentation.enable = false;
fonts.fontconfig.enable = false;
services.udisks2.enable = false;
@@ -62,6 +62,75 @@
})
];
};
boot-env-pxe = let
systemConfig = nixpkgs.lib.nixosSystem {
inherit system;
modules = [
({modulesPath, ...}: {
imports = [
(modulesPath + "/installer/netboot/netboot-minimal.nix")
];
system.stateVersion = "25.05";
boot.initrd.kernelModules = ["hv_vmbus" "hv_storvsc"]; # Hyper-V Support
# Disable unneeded features
documentation.enable = nixpkgs.lib.mkForce false;
documentation.nixos.enable = nixpkgs.lib.mkForce false;
fonts.fontconfig.enable = false;
services.udisks2.enable = false;
users.allowNoPasswordLogin = true;
users.mutableUsers = false;
security.sudo.enable = false;
services.getty.helpLine = nixpkgs.lib.mkForce "";
nix.enable = false;
networking.firewall.enable = false; # Technically we COULD use the firewall, but given that this is a network-dependent, one-time-use service, it would cause more issues
services.getty.autologinUser = nixpkgs.lib.mkForce "root";
environment.systemPackages = [ clientPackage ];
environment.etc."profile.local".text = ''
client
'';
})
];
}; in pkgs.stdenv.mkDerivation {
name = "boot-env-pxe";
buildCommand = ''
mkdir -p $out
cp -r ${systemConfig.config.system.build.kernel}/bzImage $out/kernel
cp -r ${systemConfig.config.system.build.netbootRamdisk} $out/initrd
cat <<EOF > $out/boot.ipxe
#!ipxe
imgfree
kernel http://127.0.0.1:8081/kernel init=${systemConfig.config.system.build.toplevel}/init initrd=initrd ${toString systemConfig.config.boot.kernelParams} ''${cmdline}
initrd http://127.0.0.1:8081/initrd
boot
EOF
'';
};
});
apps = forAllSystems (system:
let
pkgs = nixpkgsFor.x86_64-linux;
iso = self.packages.x86_64-linux.boot-env-iso;
in
{
test-iso-x86_64 = {
type = "app";
program = "${pkgs.writeScriptBin "test-iso" ''
#!/bin/sh
${pkgs.qemu}/bin/qemu-system-x86_64 -cdrom ${iso}/iso/*.iso -m 1G \
-net nic,model=rtl8139 -net user,hostfwd=tcp::8080-:8080
''}/bin/test-iso";
};
});
devShells = forAllSystems (system: