vbox

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2023 License: Apache-2.0 Imports: 20 Imported by: 0

README

go-vbox

Forked https://github.com/terra-farm/go-virtualbox

Usage

go get -u github.com/mel2oo/go-vbox

extend

1、snapshot manager

Documentation

Index

Constants

View Source
const (
	// Poweroff is a MachineState value.
	Poweroff = MachineState("poweroff")
	// Running is a MachineState value.
	Running = MachineState("running")
	// Paused is a MachineState value.
	Paused = MachineState("paused")
	// Saved is a MachineState value.
	Saved = MachineState("saved")
	// Aborted is a MachineState value.
	Aborted = MachineState("aborted")
	// Stopping is a MachineState value.
	Stopping = MachineState("stopping")
	// Gurumeditation
	Gurumeditation = MachineState("gurumeditation")
)
View Source
const (
	// NICNetAbsent when there is no NIC.
	NICNetAbsent = NICNetwork("none")
	// NICNetDisconnected when the NIC is disconnected
	NICNetDisconnected = NICNetwork("null")
	// NICNetNAT when the NIC is NAT-ed to access the external network.
	NICNetNAT = NICNetwork("nat")
	// NICNetBridged when the NIC is the bridge to the external network.
	NICNetBridged = NICNetwork("bridged")
	// NICNetInternal when the NIC does not have access to the external network.
	NICNetInternal = NICNetwork("intnet")
	// NICNetHostonly when the NIC can only access one host-only network.
	NICNetHostonly = NICNetwork("hostonly")
	// NICNetGeneric when the NIC behaves like a standard physical one.
	NICNetGeneric = NICNetwork("generic")
)
View Source
const (
	// AMDPCNetPCIII when the NIC emulates a Am79C970A hardware.
	AMDPCNetPCIII = NICHardware("Am79C970A")
	// AMDPCNetFASTIII when the NIC emulates a Am79C973 hardware.
	AMDPCNetFASTIII = NICHardware("Am79C973")
	// IntelPro1000MTDesktop when the NIC emulates a 82540EM hardware.
	IntelPro1000MTDesktop = NICHardware("82540EM")
	// IntelPro1000TServer when the NIC emulates a 82543GC hardware.
	IntelPro1000TServer = NICHardware("82543GC")
	// IntelPro1000MTServer when the NIC emulates a 82545EM hardware.
	IntelPro1000MTServer = NICHardware("82545EM")
	// VirtIO when the NIC emulates a virtio.
	VirtIO = NICHardware("virtio")
)
View Source
const (
	// PFTCP when forwarding a TCP port.
	PFTCP = PFProto("tcp")
	// PFUDP when forwarding an UDP port.
	PFUDP = PFProto("udp")
)
View Source
const (
	// SysBusIDE when the storage controller provides an IDE bus.
	SysBusIDE = SystemBus("ide")
	// SysBusSATA when the storage controller provides a SATA bus.
	SysBusSATA = SystemBus("sata")
	// SysBusSCSI when the storage controller provides an SCSI bus.
	SysBusSCSI = SystemBus("scsi")
	// SysBusFloppy when the storage controller provides access to Floppy drives.
	SysBusFloppy = SystemBus("floppy")
	// SysBusSAS storage controller provides a SAS bus.
	SysBusSAS = SystemBus("sas")
	// SysBusUSB storage controller proveds an USB bus.
	SysBusUSB = SystemBus("usb")
	// SysBusPCIE storage controller proveds a PCIe bus.
	SysBusPCIE = SystemBus("pcie")
	// SysBusVirtio storage controller proveds a Virtio bus.
	SysBusVirtio = SystemBus("virtio")
)
View Source
const (
	// CtrlLSILogic when the storage controller emulates LSILogic hardware.
	CtrlLSILogic = StorageControllerChipset("LSILogic")
	// CtrlLSILogicSAS when the storage controller emulates LSILogicSAS hardware.
	CtrlLSILogicSAS = StorageControllerChipset("LSILogicSAS")
	// CtrlBusLogic when the storage controller emulates BusLogic hardware.
	CtrlBusLogic = StorageControllerChipset("BusLogic")
	// CtrlIntelAHCI when the storage controller emulates IntelAHCI hardware.
	CtrlIntelAHCI = StorageControllerChipset("IntelAHCI")
	// CtrlPIIX3 when the storage controller emulates PIIX3 hardware.
	CtrlPIIX3 = StorageControllerChipset("PIIX3")
	// CtrlPIIX4 when the storage controller emulates PIIX4 hardware.
	CtrlPIIX4 = StorageControllerChipset("PIIX4")
	// CtrlICH6 when the storage controller emulates ICH6 hardware.
	CtrlICH6 = StorageControllerChipset("ICH6")
	// CtrlI82078 when the storage controller emulates I82078 hardware.
	CtrlI82078 = StorageControllerChipset("I82078")
	// CtrlUSB storage controller emulates USB hardware.
	CtrlUSB = StorageControllerChipset("USB")
	// CtrlNVME storage controller emulates NVME hardware.
	CtrlNVME = StorageControllerChipset("NVMe")
	// CtrlVirtIO storage controller emulates VirtIO hardware.
	CtrlVirtIO = StorageControllerChipset("VirtIO")
)
View Source
const (
	// DriveDVD when the drive is a DVD reader/writer.
	DriveDVD = DriveType("dvddrive")
	// DriveHDD when the drive is a hard disk or SSD.
	DriveHDD = DriveType("hdd")
	// DriveFDD when the drive is a floppy.
	DriveFDD = DriveType("fdd")
)

Variables

View Source
var (
	// Verbose toggles the library in verbose execution mode.
	Verbose bool
	// ErrMachineExist holds the error message when the machine already exists.
	ErrMachineExist = errors.New("machine already exists")
	// ErrMachineNotExist holds the error message when the machine does not exist.
	ErrMachineNotExist = errors.New("machine does not exist")
	// ErrCommandNotFound holds the error message when the VBoxManage commands was not found.
	ErrCommandNotFound = errors.New("command not found")
)
View Source
var (
	// ErrHostonlyInterfaceCreation is the error message created when VBoxManaged failed to create a new hostonly interface.
	ErrHostonlyInterfaceCreation = errors.New("failed to create hostonly interface")
)
View Source
var ErrRunCmdTimeOut = errors.New("run cmd timeout")

Functions

func AddHostonlyDHCP

func AddHostonlyDHCP(ifname string, d DHCP) error

AddHostonlyDHCP adds a DHCP server to a host-only network.

func AddInternalDHCP

func AddInternalDHCP(netname string, d DHCP) error

AddInternalDHCP adds a DHCP server to an internal network.

func CloneHD

func CloneHD(input, output string) error

CloneHD virtual harddrive

func CloneMachine

func CloneMachine(baseImageName string, newImageName string, register bool) error

CloneMachine clones the given machine name into a new one.

func DHCPs

func DHCPs() (map[string]*DHCP, error)

DHCPs gets all DHCP server settings in a map keyed by DHCP.NetworkName.

func DeleteGuestProperty

func DeleteGuestProperty(vm string, prop string) error

DeleteGuestProperty deletes a VirtualBox guestproperty.

func GetGuestProperty

func GetGuestProperty(vm string, prop string) (string, error)

GetGuestProperty reads a VirtualBox guestproperty.

func HostonlyNets

func HostonlyNets() (map[string]*HostonlyNet, error)

HostonlyNets gets all host-only networks in a map keyed by HostonlyNet.NetworkName.

func ImportOV

func ImportOV(path string) error

ImportOV imports ova or ovf from the given path

func MakeDiskImage

func MakeDiskImage(dest string, size uint, r io.Reader) error

MakeDiskImage makes a disk image at dest with the given size in MB. If r is not nil, it will be read as a raw disk image to convert from.

func NATNets

func NATNets() (map[string]NATNet, error)

NATNets gets all NAT networks in a map keyed by NATNet.Name.

func ParseIPv4Mask

func ParseIPv4Mask(s string) net.IPMask

ParseIPv4Mask parses IPv4 netmask written in IP form (e.g. 255.255.255.0). This function should really belong to the net package.

func Run

func Run(_ context.Context, args ...string) (string, string, error)

Run is a helper method used to execute the commands using the configured VBoxManage path. The command should be omitted and only the arguments should be passed. It will return the stdout, stderr and error if one occured during command execution.

func SetGuestProperty

func SetGuestProperty(vm string, prop string, val string) error

SetGuestProperty writes a VirtualBox guestproperty to the given value.

func WaitGuestProperties

func WaitGuestProperties(vm string, propPattern string, done chan bool, wg *sync.WaitGroup) chan GuestProperty

WaitGuestProperties wait for changes in GuestProperties

WaitGetProperties wait for changes in the VirtualBox GuestProperties matching the given propsPattern, for the given VM. The given bool channel indicates caller-required closure. The optional sync.WaitGroup enabke the caller program to wait for Go routine completion.

It returns a channel of GuestProperty objects (name-values pairs) populated as they change.

If the bool channel is never closed, the Waiter Go routine never ends, but on VBoxManage error.

Each GuestProperty change must be read from thwe channel before the waiter Go routine resumes waiting for the next matching change.

func WaitGuestProperty

func WaitGuestProperty(vm string, prop string) (string, string, error)

WaitGuestProperty blocks until a VirtualBox guestproperty is changed

The key to wait for can be a fully defined key or a key wild-card (glob-pattern). The first returned value is the property name that was changed. The second returned value is the new property value, Deletion of the guestproperty causes WaitGuestProperty to return the string.

func ZeroFill

func ZeroFill(w io.Writer, n int64) error

ZeroFill writes n zero bytes into w.

Types

type Command

type Command interface {
	// contains filtered or unexported methods
}

Command is the mock-able interface to run VirtualBox commands such as VBoxManage (host side) or VBoxControl (guest side)

func NewCommand added in v0.1.6

func NewCommand() (Command, error)

func NewSSHCmd added in v0.1.6

func NewSSHCmd(user, password, host string, port int, runTimeout time.Duration) (Command, error)

func NewSSHCmdWithPrivateKey added in v0.1.7

func NewSSHCmdWithPrivateKey(user, privateKeyFile, host string, port int, runTimeout time.Duration) (Command, error)

type DHCP

type DHCP struct {
	NetworkName string
	IPv4        net.IPNet
	LowerIP     net.IP
	UpperIP     net.IP
	Enabled     bool
}

DHCP server info.

type DriveType

type DriveType string

DriveType represents the hardware type of a drive.

type Flag

type Flag int

Flag is an active VM configuration toggle

const (
	ACPI Flag = 1 << iota
	IOAPIC
	RTCUSEUTC
	CPUHOTPLUG
	PAE
	LONGMODE
	HPET
	HWVIRTEX
	TRIPLEFAULTRESET
	NESTEDPAGING
	LARGEPAGES
	VTXVPID
	VTXUX
	ACCELERATE3D
)

Flag names in lowercases to be consistent with VBoxManage options.

func (Flag) Get

func (f Flag) Get(o Flag) string

Get tests if flag is set. Return "on" or "off".

type GuestProperty

type GuestProperty struct {
	Name  string
	Value string
}

GuestProperty holds key, value and associated flags.

type HostonlyNet

type HostonlyNet struct {
	Name        string
	GUID        string
	DHCP        bool
	IPv4        net.IPNet
	IPv6        net.IPNet
	HwAddr      net.HardwareAddr
	Medium      string
	Status      string
	NetworkName string // referenced in DHCP.NetworkName
}

HostonlyNet defines each host-only network.

func CreateHostonlyNet

func CreateHostonlyNet() (*HostonlyNet, error)

CreateHostonlyNet creates a new host-only network.

func (*HostonlyNet) Config

func (n *HostonlyNet) Config() error

Config changes the configuration of the host-only network.

type LogFunc

type LogFunc func(string, ...interface{})

LogFunc is the signature to log traces.

var Debug LogFunc = noLog

Debug is the Logger currently in use.

type Machine

type Machine struct {
	Name       string
	Firmware   string
	UUID       string
	State      MachineState
	CPUs       uint
	Memory     uint // main memory (in MB)
	VRAM       uint // video memory (in MB)
	CfgFile    string
	BaseFolder string
	OSType     string
	Flag       Flag
	BootOrder  []string // max 4 slots, each in {none|floppy|dvd|disk|net}
	NICs       []NIC
}

Machine information.

func CreateMachine

func CreateMachine(name, basefolder string) (*Machine, error)

CreateMachine creates a new machine. If basefolder is empty, use default.

func GetMachine

func GetMachine(id string) (*Machine, error)

GetMachine finds a machine by its name or UUID.

func ListMachines

func ListMachines() ([]*Machine, error)

ListMachines lists all registered machines.

func New

func New() *Machine

New creates a new machine.

func (*Machine) AddNATPF

func (m *Machine) AddNATPF(n int, name string, rule PFRule) error

AddNATPF adds a NAT port forarding rule to the n-th NIC with the given name.

func (*Machine) AddStorageCtl

func (m *Machine) AddStorageCtl(name string, ctl StorageController) error

AddStorageCtl adds a storage controller with the given name.

func (*Machine) AttachStorage

func (m *Machine) AttachStorage(ctlName string, medium StorageMedium) error

AttachStorage attaches a storage medium to the named storage controller.

func (*Machine) BindCpu added in v0.1.6

func (m *Machine) BindCpu(cpuset string) error

func (*Machine) DelNATPF

func (m *Machine) DelNATPF(n int, name string) error

DelNATPF deletes the NAT port forwarding rule with the given name from the n-th NIC.

func (*Machine) DelStorageCtl

func (m *Machine) DelStorageCtl(name string) error

DelStorageCtl deletes the storage controller with the given name.

func (*Machine) Delete

func (m *Machine) Delete() error

Delete deletes the machine and associated disk images.

func (*Machine) DeleteExtraData

func (m *Machine) DeleteExtraData(key string) error

DeleteExtraData removes custom string from the VM.

func (*Machine) DeleteSnapshot

func (m *Machine) DeleteSnapshot(name string) error

DeleteSnapshot

func (*Machine) DisconnectSerialPort

func (m *Machine) DisconnectSerialPort(portNumber int) error

DisconnectSerialPort sets given serial port to disconnected.

func (*Machine) GetExtraData

func (m *Machine) GetExtraData(key string) (*string, error)

GetExtraData retrieves custom string from the VM.

func (*Machine) ListSnapshot

func (m *Machine) ListSnapshot(name string) error

ListSnapshot

func (*Machine) Modify

func (m *Machine) Modify() error

Modify changes the settings of the machine.

func (*Machine) Pause

func (m *Machine) Pause() error

Pause pauses the execution of the machine.

func (*Machine) Poweroff

func (m *Machine) Poweroff() error

Poweroff forcefully stops the machine. State is lost and might corrupt the disk image.

func (*Machine) ReDefine added in v0.1.9

func (m *Machine) ReDefine() error

func (*Machine) Refresh

func (m *Machine) Refresh() error

Refresh reloads the machine information.

func (*Machine) Reset

func (m *Machine) Reset() error

Reset forcefully restarts the machine. State is lost and might corrupt the disk image.

func (*Machine) Restart

func (m *Machine) Restart() error

Restart gracefully restarts the machine.

func (*Machine) RestoreCurrentSnapshot

func (m *Machine) RestoreCurrentSnapshot(name string) error

RestoreCurrentSnapshot

func (*Machine) RestoreSnapshot

func (m *Machine) RestoreSnapshot(name string) error

RestoreSnapshot

func (*Machine) Save

func (m *Machine) Save() error

Save suspends the machine and saves its state to disk.

func (*Machine) SetExtraData

func (m *Machine) SetExtraData(key, val string) error

SetExtraData attaches custom string to the VM.

func (*Machine) SetLinkstate added in v0.2.2

func (m *Machine) SetLinkstate(nic int, onoff string) error

func (*Machine) SetNIC

func (m *Machine) SetNIC(n int, nic NIC) error

SetNIC set the n-th NIC.

func (*Machine) Start

func (m *Machine) Start() error

Start the machine, and return the underlying error when unable to do so.

func (*Machine) Stop

func (m *Machine) Stop() error

Stop gracefully stops the machine.

func (*Machine) TakeSnapshot

func (m *Machine) TakeSnapshot(name string) error

TakeSnapshot

type MachineState

type MachineState string

MachineState stores the last retrieved VM state.

type NATNet

type NATNet struct {
	Name    string
	IPv4    net.IPNet
	IPv6    net.IPNet
	DHCP    bool
	Enabled bool
}

A NATNet defines a NAT network.

type NIC

type NIC struct {
	Network       NICNetwork
	Hardware      NICHardware
	HostInterface string // The host interface name to bind to in 'hostonly' and 'bridged' mode
	MacAddr       string
}

NIC represents a virtualized network interface card.

type NICHardware

type NICHardware string

NICHardware represents the type of NIC hardware.

type NICNetwork

type NICNetwork string

NICNetwork represents the type of NIC networks.

type PFProto

type PFProto string

PFProto represents the protocol of a port forwarding rule.

type PFRule

type PFRule struct {
	Proto     PFProto
	HostIP    net.IP // can be nil to match any host interface
	GuestIP   net.IP // can be nil if guest IP is leased from built-in DHCP
	HostPort  uint16
	GuestPort uint16
}

PFRule represents a port forwarding rule.

func (PFRule) Format

func (r PFRule) Format() string

Format returns the string needed as a command-line argument to VBoxManage.

func (PFRule) String

func (r PFRule) String() string

String returns a human-friendly representation of the port forwarding rule.

type StorageController

type StorageController struct {
	SysBus      SystemBus
	Ports       uint // SATA port count 1--30
	Chipset     StorageControllerChipset
	HostIOCache bool
	Bootable    bool
}

StorageController represents a virtualized storage controller.

type StorageControllerChipset

type StorageControllerChipset string

StorageControllerChipset represents the hardware of a storage controller.

type StorageMedium

type StorageMedium struct {
	Port      uint
	Device    uint
	DriveType DriveType
	Medium    string // none|emptydrive|<uuid>|<filename|host:<drive>|iscsi
}

StorageMedium represents the storage medium attached to a storage controller.

type SystemBus

type SystemBus string

SystemBus represents the system bus of a storage controller.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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