โปรดทราบว่าคำตอบที่ได้รับการยอมรับในปัจจุบัน (Molly7244) จะเริ่ม VM เมื่อคุณเข้าสู่ระบบ - ไม่ใช่เมื่อคุณบูตเครื่อง มันไม่ได้เป็นบริการ
สำหรับบริการจริงที่ทำงานเมื่อบูทเครื่องฉันใช้สองสคริปต์ (มาจากที่นี่ ) ร่วมกับ cygwin (cygrunsrv) ใช้โหมด VBoxHeadless ตามที่กล่าวไว้ที่อื่นในหน้านี้
สคริปต์แรกรัน VM ของคุณผ่าน VBoxHeadless มันได้รับชื่อของ VM ที่เหมาะสมในการเรียกใช้ (และข้อมูลอื่น ๆ เช่นไดเรกทอรีบ้าน VBOX ของคุณ) จากตัวแปรสภาพแวดล้อม สคริปต์ที่สองติดตั้งบริการสำหรับ VM เฉพาะ (โดยใช้ cygrunsrv เพื่อเรียกใช้สคริปต์แรกที่มีชุด env. vars ที่ถูกต้อง) ในที่สุดก็มีไฟล์ที่สามซึ่งมีฟังก์ชั่นทั่วไป หากคุณใส่สิ่งเหล่านี้ลงในไดเรกทอรีด้วยกันคุณสามารถติดตั้ง vm ใหม่เช่น:
$ VBOX_USER_HOME="/path/to/.VirtualBox/" vboxd-install MyVMName 3333
จากนั้นเริ่มบริการด้วย "net start vboxd-MyVMName" หรือ "cygrunsrv -S vboxd-MyVMName"
นี่คือสคริปต์ทำงานของ VM "vboxd":
#!/bin/bash
# from http://forums.virtualbox.org/viewtopic.php?f=1&t=23536
##
## Manages start / stop of VirtualBox virtual machines
##
## load common functions
basedir="$(readlink -f $(dirname $0))"
source "$basedir/.libcommon" || exit 1
## parse arguments
parseArg vmName "$1" "$VBOXD_VM_NAME"
parseArg vmPort "$2" "$VBOXD_VM_PORT"
VBOX_INSTALL_PATH="$(cygpath "$VBOX_MSI_INSTALL_PATH")"
## define signal handler
function onHalt {
warn "Stopping virtual machine '$vmName'"
"$VBOX_INSTALL_PATH/VBoxManage" controlvm "$vmName" savestate
exit 0
}
## install signal handler; cygrunsrv uses SIGTERM by default
trap 'onHalt' TERM
## hardcode this path if you like; it's required for VBox* utils to
## find the correct VirtualBox.xml config file and is usually set
## during a call to vboxd-install.
#export VBOX_USER_HOME="$USERPROFILE\\.VirtualBox"
## default VBoxHeadless port specification
portSpec="-e \"TCP/Ports=$vmPort\""
## determine vm state
info "Querying virtual machine '$vmName' state"
vmState=$( \
"$VBOX_INSTALL_PATH/VBoxManage" showvminfo "$vmName" \
| grep '^State:' \
| sed 's/State: *//' )
info "Virtual machine '$vmName' is $vmState"
## if vm state is saved, we can't specify port without an exception,
## as port spec requires modification of the (immutable) saved machine
## state. See http://www.virtualbox.de/ticket/3609
if [ "${vmState##saved}" != "$vmState" ]; then
## state is saved; clear port specification
warn "Port specification is not allowed for saved vms"
portSpec=""
fi
## start the VM
info "Starting virtual machine '$vmName' on port $vmPort"
"$VBOX_INSTALL_PATH/VBoxHeadless" -s "$vmName" $portSpec &
## record pid of VBoxHeadless child process and wait on it
pid="$!"
info "Waiting on VBoxHeadless child process $pid"
wait "$pid"
และนี่คือสคริปต์ตัวติดตั้ง "vboxd-install":
#!/bin/bash
# http://forums.virtualbox.org/viewtopic.php?f=1&t=23536
##
## Registers a VirtualBox virtual machine to start as a service via cygrunsrv
##
## load common functions
basedir="$(readlink -f $(dirname $0))"
source "$basedir/.libcommon" || exit 1
## test for presence of cygrunsrv utility
if [ ! -x "$(which cygrunsrv)" ]; then
die "Utility 'cygrunsrv' is not in path"
fi
## test VirtualBox configuration
if [ -z "$VBOX_USER_HOME" ]; then
die "Required environment variable 'VBOX_USER_HOME' is undefined. " \
"Please ensure this variable is set to point to the directory " \
"containing your VirtualBox.xml configuration file."
fi
configFile=$(cygpath -u "$VBOX_USER_HOME\\VirtualBox.xml")
if [ ! -e "$configFile" ]; then
die "VirtualBox configuration file '$(cygpath -w $configFile)' not found"
fi
## parse arguments
parseArg vmName "$1"
parseArg vmPort "$2"
parseArg vmUser "$3" "SYSTEM"
## if vmUser is not SYSTEM, update userSpec
userSpec="--interactive"
if [ "$vmUser" != "SYSTEM" ]; then
## "interactive" option disallowed when user is specified
userSpec="--user \"$vmUser\""
fi
## install the service
cygrunsrv \
--install "vboxd-$vmName" \
--path "$basedir/vboxd" \
--env "VBOXD_VM_NAME=$vmName" \
--env "VBOXD_VM_PORT=$vmPort" \
--env "VBOX_USER_HOME=$VBOX_USER_HOME" \
--desc "VirtualBox virtual machine '$vmName' on port $vmPort" \
$userSpec \
--type auto \
--termsig TERM \
--shutsig TERM \
--neverexits \
--preshutdown \
|| die "Failed to install service"
และในที่สุดนี่คือสคริปต์ ".libcommon" ที่อ้างอิงโดยทั้งสองสิ่งนี้:
# -*-shell-script-*-
# from http://forums.virtualbox.org/viewtopic.php?f=1&t=23536
SCRIPT="$(basename $0)"
BASEDIR="$(readlink -f $(dirname $0))"
[ -z "$LOGLEVEL" ] && LOGLEVEL=2
[ -z "$LOGDATEFORMAT" ] && LOGDATEFORMAT="%Y-%m-%d %H:%M:%S "
function log {
local now=""
[ -n "$LOGDATEFORMAT" ] && now=$(date +"$LOGDATEFORMAT")
echo "$SCRIPT $now$@" >&2
}
function debug {
[ "$LOGLEVEL" -lt 3 ] && return
log "[DEBUG] $@"
}
function info {
[ "$LOGLEVEL" -lt 2 ] && return
log "[INFO] $@"
}
function warn {
[ "$LOGLEVEL" -lt 1 ] && return
log "[WARN] $@"
}
function error {
log "[ERROR] $@"
}
function die {
error "$@"
exit 1
}
function parseArg {
local _name="$1"
local _value="$2"
local _default="$3"
if [ -z "$_value" ]; then
if [ -z "$_default" ]; then
die "Required argument '$_name' is undefined"
fi
if [ "$_default" = "*EMPTY*" ]; then
_value=""
else
_value="$_default"
fi
fi
debug "$_name=\"$_value\""
eval "$_name=\"$_value\""
}
วิธีนี้ใช้ได้ดีสำหรับฉัน หวังว่าคุณจะมีโชคเหมือนกัน