Last active
October 18, 2021 21:55
-
-
Save afair/2d321d02ea733df824e0 to your computer and use it in GitHub Desktop.
VM command to wrap virtualbox vboxmanage commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# install - http://www.unixmen.com/install-oracle-virtualbox-and-manage-it-using-phpvirtualbox-on-ubuntu-15-10-headless-server/ | |
# remoting - http://www.virtualbox.org/manual/ch07.html | |
# vboxmanage - http://www.virtualbox.org/manual/ch08.html | |
if [ "$1" = "" ]; then | |
echo "Usage: vm command vmname args..." | |
exit | |
fi | |
freebsd_iso=$HOME/vdisks/FreeBSD-10.2-RELEASE-amd64-disc1.iso | |
ubuntu_iso=$HOME/vdisks/ubuntu-15.10-server-amd64.iso | |
coreos_vdi=$HOME/vdisks/coreos_production_835.13.0.vdi | |
default_if=enp3s0 | |
default_os=${ostype-Ubuntu_64} # FreeBSD_64 | |
default_iso=${iso-$ubuntu_iso} | |
default_disksize=8000 | |
default_ram=1024 | |
host=`hostname` | |
cmd=$1; shift; | |
vm=$1; shift; | |
case "$cmd" in | |
check) | |
#sudo systemctl status vboxdrv | |
/etc/init.d/virtualbox status | |
;; | |
up) | |
/etc/init.d/virtualbox start | |
;; | |
down) | |
/etc/init.d/virtualbox stop | |
;; | |
system) | |
/etc/init.d/virtualbox "${1-status}" | |
;; | |
list) | |
VBoxManage list ${1-vms} | |
echo "running:" | |
VBoxManage list runningvms | |
;; | |
show) | |
VBoxManage showvminfo "$vm" | |
;; | |
new) # new vmname ostype /path/to/dvd.iso | |
VBoxManage createvm --name "$vm" --register | |
VBoxManage modifyvm "$vm" --ostype "${1-$default_os}" --memory ${ramsize-$default_ram} --acpi on | |
VBoxManage modifyvm "$vm" --boot1 dvd --nic1 bridged --bridgeadapter1 ${net-$default_if} | |
VBoxManage createhd --filename "$HOME/vdisks/$vm.vdi" --size ${disksize-$default_disksize} | |
#VBoxManage modifyvm "$vm" --vrde on | |
VBoxManage storagectl "$vm" --name "IDE Controller" --add ide | |
VBoxManage storageattach "$vm" --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium "$HOME/vdisks/$vm.vdi" | |
VBoxManage storageattach "$vm" --storagectl "IDE Controller" --port 1 --device 0 --type dvddrive --medium "${2-$default_iso}" | |
;; | |
new-freebsd) | |
$0 new "$vm" FreeBSD_64 $freebsd_iso | |
;; | |
new-coreos) | |
$0 new-from-vdi "$vm" Linux26_64 "$coreos_vdi" "config-2" | |
echo "Created. Start VM and: ssh core@ip" | |
;; | |
new-from-vdi) # new-from-vdi ostype /path/to/template.vdi config_drive | |
VBoxManage clonehd "$2" "$HOME/vdisks/$vm.vdi" | |
VBoxManage modifyhd "$HOME/vdisks/$vm.vdi" --resize ${disksize-$default_disksize} | |
VBoxManage createvm --name "$vm" --register | |
VBoxManage modifyvm "$vm" --ostype "${1-$default_os}" --memory ${ramsize-$default_ram} --acpi on | |
VBoxManage modifyvm "$vm" --boot1 dvd --nic1 bridged --bridgeadapter1 ${net-$default_if} | |
VBoxManage storagectl "$vm" --name "IDE Controller" --add ide | |
VBoxManage storageattach "$vm" --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium "$HOME/vdisks/$vm.vdi" | |
if [ "$3" = "config-2" ]; then | |
$HOME/bin/bin/create-basic-configdrive -H v02 -S ~/.ssh/id_dsa.pub | |
VBoxManage storageattach "$vm" --storagectl "IDE Controller" --port 1 --device 0 --type dvddrive --medium "$HOME/vdisks/$vm.iso" | |
fi | |
;; | |
config-drive) | |
cd $HOME/vdisks | |
$HOME/bin/bin/create-basic-configdrive -H v02 -S ~/.ssh/id_dsa.pub | |
cd - | |
;; | |
modify) | |
VBoxManage modifyvm "$vm" --$1 "$2" # ex:-> --bridgeadapter1 eth0 | |
;; | |
vrde) # vdre on|off port | |
if [ "$1" = "on" ]; then | |
VBoxManage modifyvm "$vm" --vrde on --vrdeport ${2-3389} | |
echo "Set $vm with RDP on $host:${rpd_port-3389} ..." | |
else | |
VBoxManage modifyvm "$vm" --vrde off | |
echo "Changed $vm to remove RDP" | |
fi | |
;; | |
start-rdp) # start rde_port | |
VBoxManage modifyvm "$vm" --vrde on --vrdeport ${1-3389} | |
echo "Starting $vm with RDP on $host:${1-3389} ..." | |
VBoxManage startvm "$vm" --type headless | |
;; | |
start) # start vmname rdp_port | |
if [ "$1" != "" ]; then | |
echo VBoxManage modifyvm "$vm" --vrde on --vrdeport ${1-3389} | |
VBoxManage modifyvm "$vm" --vrde on --vrdeport ${1-3389} | |
echo "Starting $vm with RDP on $host:${1-3389} ..." | |
fi | |
VBoxManage startvm "$vm" --type headless | |
;; | |
pause) | |
VBoxManage controlvm "$vm" pause | |
;; | |
stop) | |
VBoxManage controlvm "$vm" poweroff | |
;; | |
reset) | |
VBoxManage controlvm "$vm" reset | |
;; | |
attach) | |
VBoxManage storageattach "$vm" --storagectl "IDE Controller" --port 1 --device 0 --type dvddrive --medium "${1-$default_iso}" | |
;; | |
eject) | |
VBoxManage storageattach "$vm" --storagectl "IDE Controller" --port 1 --device 0 --type dvddrive --medium none | |
;; | |
*) | |
echo "Unknown subcommand: $cmd" | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment