Skip to content

Instantly share code, notes, and snippets.

@lovelock
Created October 14, 2016 11:03
Show Gist options
  • Save lovelock/13e7b8759450b4095bc95a5083019211 to your computer and use it in GitHub Desktop.
Save lovelock/13e7b8759450b4095bc95a5083019211 to your computer and use it in GitHub Desktop.
VBoxManage 操作
#!/usr/bin/env bash
OPTIND=1
ADDRESS="0.0.0.0"
PORT=5000
MEMORY=1024
VM_NAME=""
ISO_FILE=""
function show_help {
#echo "${0} -n <name> -f <file> [-a <address>] [-p <port>]"
echo "-h | ? Print this message"
echo "-n <name> Name of your guest vm"
echo "-m <memory>M Memory your guest os will use."
echo "-f <file> ISO file which you would use to install your guest os"
echo "-a <address> IP address you would bind to, default is 0.0.0.0."
echo " If set to another address, you would not access your guest via ssh"
echo "-p <port> Port you would bind to"
}
while getopts "h?nfapm:" opt; do
case ${opt} in
h) show_help
;;
n) VM_NAME=${OPTARG}
;;
f) ISO_FILE=${OPTARG}
;;
a) ADDRESS=${OPTARG}
;;
p) PORT=${OPTARG}
;;
m) MEMORY=${OPTARG}
;;
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
function get_ip {
INTERFACE=$(ip a | grep 'state UP' | awk -F': ' '{print $2}')
}
get_ip
echo ${VM_NAME}
echo ${ISO_FILE}
echo ${INTERFACE}
exit 0
function install {
VBoxManage createmedium disk --filename ${VM_NAME}.vdi --size 50000
VBoxManage createvm --name ${VM_NAME} --ostype Linux_64 --register
VBoxManage storagectl ${VM_NAME} --add sata --controller IntelAHCI --name "SATA Controller"
VBoxManage storageattach ${VM_NAME} --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium ${VM_NAME}.vdi
VBoxManage storagectl ${VM_NAME} --name "IDE Controller" --add ide
VBoxManage storageattach ${VM_NAME} --storagectl "IDE Controller" --port 0 --device 0 --type dvddrive --medium ${ISO_FILE}
VBoxManage modifyvm ${VM_NAME} --nic1 bridged --bridgeadapter1 ${INTERFACE} --vrde on --vrdeaddress ${ADDRESS} --vrdeport ${PORT} --memory ${MEMORY} --cpus 1 --vram 8
VBoxManage startvm ${VM_NAME} --type=headless
}
install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment