hyperv

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package hyperv contains pure PowerShell/XML builders and parsers for the LabLink VM-management tools. It deliberately has NO dependency on mcp-go, gRPC, or the rest of the server so the script/XML construction can be unit tested in isolation (Heimdall review §3 layering requirement).

Every script emitted here is "argument safe": caller-supplied values are emitted as single-quoted PowerShell literals via PSLit / PSBool, never interpolated directly into a command line. This matches the existing escapePSString convention in internal/mcptools/deploy.go and the network reviewer's "argument-safe construction" requirement.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildCreateVMScript

func BuildCreateVMScript(p CreateVMParams) (string, error)

BuildCreateVMScript builds the create_vm mutation script with the network reviewer's completeness items: Gen2 secure boot template, deterministic DVD selection by ISO path, dynamic-memory min/max/buffer, path + free-space validation, VM-off and VHD-not-in-use checks.

func BuildCreateVSwitchScript

func BuildCreateVSwitchScript(p CreateVSwitchParams) (string, error)

BuildCreateVSwitchScript builds the create_vswitch mutation script with the management-NIC severance safeguard baked in. Returns an error for invalid param combinations that can be detected statically.

func BuildListNicsScript

func BuildListNicsScript(includeVirtual bool, mgmtIP string) string

BuildListNicsScript builds the discovery script for physical NICs.

mgmtIP is the target-side IP address that the LabLink server uses to reach this host (the host part of the registry node Address). The NIC that owns that IP — or, failing an exact match, the NIC carrying the active default route — is flagged is_management_nic / management_risk so callers and the create_vswitch safeguard can avoid severing the agent connection. For a localhost target mgmtIP is empty and only the default-route NIC is flagged (lower risk: you own your own host).

func BuildListVSwitchesScript

func BuildListVSwitchesScript() string

BuildListVSwitchesScript builds the discovery script for vSwitches.

func EscapePS

func EscapePS(s string) string

EscapePS escapes a string for inclusion inside a PowerShell single-quoted literal by doubling embedded single quotes.

func PSBool

func PSBool(b bool) string

PSBool renders a Go bool as a PowerShell boolean literal.

func PSLit

func PSLit(s string) string

PSLit renders s as a complete single-quoted PowerShell string literal, doubling any embedded single quotes (the standard PowerShell escape).

func PreflightScript

func PreflightScript() string

PreflightScript returns the shared Test-LabLinkHyperV preamble that every mutating Hyper-V tool runs first. It verifies the Hyper-V PowerShell module is present and the caller is elevated, emitting a tagged error otherwise.

Enabling the Hyper-V role itself (Enable-WindowsOptionalFeature) requires a reboot and is out of scope for Phase 1 — we only detect-and-report.

func WrapTagged

func WrapTagged(body string) string

WrapTagged is the exported form of wrapTagged for sibling packages (e.g. internal/hyperv/unattend) that build their own script bodies but want the same single-tagged-line failure convention.

Types

type CreateVMParams

type CreateVMParams struct {
	Name       string
	VMLocation string // -Path for VM config; empty unless UseHostDefaults

	// Exactly one of VHDPath (attach existing) or NewVHDPath (+NewVHDSizeGB).
	VHDPath      string
	NewVHDPath   string
	NewVHDSizeGB float64

	MemoryMB float64 // startup memory (default applied by caller)

	// Dynamic-memory policy. When DynamicMemory is true, Min/Max/Buffer apply.
	DynamicMemory       bool
	DynamicMinMB        float64
	DynamicMaxMB        float64
	DynamicBufferPct    float64
	CPUCount            float64
	VSwitch             string
	ISOPath             string
	SecureBoot          bool
	SecureBootTemplate  string // default MicrosoftWindows
	UseHostDefaults     bool   // permit Hyper-V default VM location when VMLocation empty
	RequiredFreeSpaceGB float64
}

CreateVMParams configures BuildCreateVMScript for a Gen2 Windows VM.

type CreateVSwitchParams

type CreateVSwitchParams struct {
	Name              string
	Type              string // "external" | "internal" | "private"
	NetAdapter        string // required when Type == external
	AllowManagementOS bool   // default true (caller sets)
	IfExists          string // "reuse" (default) | "fail" | "replace"

	// IsRemote is true when the target is a registered node (not localhost).
	// On a remote target an external switch on the management NIC is BLOCKED
	// unless AllowMgmtDisruption is true (network reviewer finding #1).
	IsRemote bool
	// AllowMgmtDisruption overrides the management-NIC safeguard.
	AllowMgmtDisruption bool
	// MgmtIP is the target-side IP the server uses to reach this host; used to
	// identify the management NIC at runtime on the target.
	MgmtIP string
}

CreateVSwitchParams configures BuildCreateVSwitchScript.

type NIC

type NIC struct {
	Name                 string `json:"name"`
	InterfaceDescription string `json:"interface_description"`
	MAC                  string `json:"mac"`
	Status               string `json:"status"`
	LinkSpeed            string `json:"link_speed"`
	BoundVSwitch         string `json:"bound_vswitch"`
	IsManagementNIC      bool   `json:"is_management_nic"`
	ManagementRisk       bool   `json:"management_risk"`
	RecommendedExternal  bool   `json:"recommended_for_external"`
}

NIC is a parsed physical network adapter row from list_physical_nics.

func ParseNICs

func ParseNICs(jsonText string) ([]NIC, error)

ParseNICs parses the JSON array emitted by BuildListNicsScript. PowerShell emits a bare object (not an array) for a single row; ParseNICs handles both.

type VSwitch

type VSwitch struct {
	Name              string `json:"name"`
	Type              string `json:"type"`
	NetAdapter        string `json:"net_adapter"`
	AllowManagementOS bool   `json:"allow_management_os"`
}

VSwitch is a parsed Hyper-V virtual switch row.

func ParseVSwitches

func ParseVSwitches(jsonText string) ([]VSwitch, error)

ParseVSwitches parses the JSON emitted by BuildListVSwitchesScript.

Directories

Path Synopsis
Package unattend renders Windows answer files (unattend.xml / Autounattend.xml) and builds the PowerShell injection scripts.
Package unattend renders Windows answer files (unattend.xml / Autounattend.xml) and builds the PowerShell injection scripts.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL