Skip to content

Instantly share code, notes, and snippets.

@qiwichupa
Created September 29, 2009 19:58
Show Gist options
  • Save qiwichupa/197250 to your computer and use it in GitHub Desktop.
Save qiwichupa/197250 to your computer and use it in GitHub Desktop.
Easy to use, lightweight, interactive shell for VBoxManage and VBoxHeadless tools from Sun VirtualBox. Include basic operations like as creation virtual machine, change basic parameters, start machine in headless mode
#!/bin/bash
#
# Filename: vbmtool-0.7.sh
# Version 0.7
# Date: 2009/09/29 19:56
# Licence: GNU GPL2
# Author: Sergey "Qiwichupa" Pavlov <[email protected]>
#
#
# Easy to use, lightweight, interactive shell for VBoxManage
# and VBoxHeadless tools from Sun VirtualBox. Include basic
# operations like as creation virtual machine, change basic
# parameters, start machine in headless mode
#VAR
ISO_LOCATION=/home/qiwichupa/ISO/
################################
# == functions ==
# HEADS
mainHead () {
clear
echo "=================================="
echo "========== Main options =========="
echo "=================================="
echo ""
}
newVmHead () {
clear
echo "=================================="
echo "======== Creating new VM ========="
echo "=================================="
echo ""
}
listHead () {
clear
echo "=================================="
echo "========= Your machines =========="
echo "=================================="
echo ""
}
editHead () {
clear
echo "=================================="
echo "============= Edit ==============="
echo "=================================="
echo ""
}
# MINIFUNCTIONS
isDigit () # check string, digit or not
{
FAILURE=-1
SUCCESS=0
[ $# -eq 1 ] || return $FAILURE
case $1 in
*[!0-9]*|"") return $FAILURE;;
*) return $SUCCESS;;
esac
}
# editing
ifSelector () { # part of edNetwork ()
newVmHead
# mystical action with iface list
tmp=
temp=$IFS
IFS=$'\n'
ls=$(ip link show | awk '{print $2}' |grep '^[a-z][a-z][a-z0-9]'| sed 's/://') # iface list
if [ -z "$ls" ]; then # if iso not found - exit
echo ""
echo "Error: ifaces not fount"
break
fi
arrFILES=($(echo "$ls"))
IFS=$temp
for line in "${arrFILES[@]}"; do
tmp="$tmp"$(echo "\"""$line""\""" ")
done
# mystical menu building
PS3=" brige with... > "
eval set $tmp
select opt in "$@"; do
VM_NET_IFACE=$opt
break
done
}
machineSelector () {
tmp=
temp=$IFS
IFS=$'\n'
ls=$(VBoxManage list vms | grep \"| awk '{print $1}')
if [ -z "$ls" ]; then # if iso not found - exit
echo ""
echo "Error: ifaces not fount"
break
fi
arrFILES=($(echo "$ls"))
IFS=$temp
for line in "${arrFILES[@]}"; do
if [ "$line" != "Iface" ]; then # del first line in ifconfig -s | awk '{print $1}'
tmp="$tmp"$(echo "\"""$line""\""" ")
fi
done
# mystical menu building
PS3=" machine > "
eval set $tmp
select opt in "$@"; do
VM_NAME=$opt
break
done
}
edName () {
tmp=
echo "Machines:"
VBoxManage list vms | grep \"| awk '{print $1}'
echo ""
echo "Enter new machine name."
echo -n " name > "
read tmp # vm name
NAME_TEST=$(VBoxManage list vms | grep "$tmp") # check registred names
if [ -n "$NAME_TEST" ]; then # if unique...
clear
list
echo "Error: choose another name"
break
fi
VM_NAME=$tmp
}
edMemory () {
tmp=
echo "Enter memory size (natural number in Mb)."
echo -n " memory > "
read tmp
isDigit $tmp # validator
tmpp=$(echo "$?")
if [ "$tmpp" == "255" ]; then
echo "Error: memory size is natural number"
exit
fi
VM_MEM=$tmp
}
edHD () {
tmp=
echo "Enter HardDrive size (natural number in Mb)."
echo -n " HardDrive > "
read tmp
isDigit $tmp # validator
tmpp=$(echo "$?")
if [ "$tmpp" == "255" ]; then
echo "Error: HD size is natural number"
exit
fi
VM_HD=$tmp
}
edISO () {
echo "ISO location: $ISO_LOCATION"
echo "Available ISOs:"
# mystical action with iso list
# http://www.linux.org.ru/view-message.jsp?msgid=3128616#comment-3132277
tmp=
temp=$IFS
IFS=$'\n'
ls=$(ls -1 "$ISO_LOCATION" | grep iso$)
if [ -z "$ls" ]; then # if iso not found - exit
echo ""
echo "Error: *.iso not fount in $ISO_LOCATION"
break
fi
arrFILES=($(echo "$ls"))
IFS=$temp
for line in "${arrFILES[@]}"; do
tmp="$tmp"$(echo "\"""$line""\""" ")
done
# mystical menu building
# http://www.linuxquestions.org/questions/programming-9/bash-script-using-select-to-show-multi-word-options-like-option-1o-317671/#post1613089
PS3=" ISO > "
eval set $tmp
select opt in "$@"; do
VM_CD=$opt
break
done
temp=
IFS=
tmp=
opt=
}
edRDPPort () {
tmp=
echo "Enter RDP port (natural number)"
echo -n " RDP Port > "
read tmp
isDigit $tmp # validator
tmpp=$(echo "$?")
if [ "$tmpp" == "255" ]; then
echo "Error: rdp port is natural number"
exit
fi
VM_RDPPORT=$tmp
}
edNetwork () {
echo "Choose VM iface:"
PS3=" VM iface > "
select VM_NET_VMIFACE in "nic1" "nic2" "nic3" "nic4"; do
case $VM_NET_VMIFACE in
'nic1')
VM_NET_VMIFACE_NUM="1"
break
;;
'nic2')
VM_NET_VMIFACE_NUM="2"
break
;;
'nic3')
VM_NET_VMIFACE_NUM="3"
break
;;
'nic4')
VM_NET_VMIFACE_NUM="4"
break
;;
esac
break
done
clear
echo "Choose network settings for $VM_NET_VMIFACE:"
PS3=" network > "
IFS=' '
select VM_NET_OPT in "brige" "nat" "off"; do
case $VM_NET_OPT in
'brige')
ifSelector
VM_NET_SET="brige"
break
;;
'nat')
VM_NET_SET="nat"
break
;;
'off')
VM_NET_SET="off"
break
;;
esac
done
}
# functions with VirtualBox tools
vbmCreate () {
VBoxManage createvm -name "$VM_NAME" -register
VBoxManage modifyvm "$VM_NAME" -acpi on -boot1 dvd
}
vbmMemory () {
VBoxManage modifyvm "$VM_NAME" -memory "$VM_MEM"
}
vbmHD () {
VBoxManage createvdi -filename "$VM_HD_NAME" -size "$VM_HD" -register
VBoxManage modifyvm "$VM_NAME" -hda "$VM_HD_NAME"
}
vbmCD () {
FULL_CD=$(echo $ISO_LOCATION$VM_CD)
echo $FULL_CD
VBoxManage registerimage dvd "$FULL_CD"
VBoxManage modifyvm "$VM_NAME" -dvd "$FULL_CD"
}
vbmRDP () {
VBoxManage modifyvm "$VM_NAME" -vrdp on
VBoxManage modifyvm "$VM_NAME" -vrdpport "$VM_RDPPORT"
}
vbmNetwork () {
case $VM_NET_SET in
'nat')
VBoxManage modifyvm "$VM_NAME" -"$VM_NET_VMIFACE" nat
;;
'brige')
echo VBoxManage modifyvm "$VM_NAME" -"$VM_NET_VMIFACE" bridged --bridgeadapter"$VM_NET_VMIFACE_NUM" "$VM_NET_IFACE"
VBoxManage modifyvm "$VM_NAME" -"$VM_NET_VMIFACE" bridged --bridgeadapter"$VM_NET_VMIFACE_NUM" "$VM_NET_IFACE"
;;
'off')
VBoxManage modifyvm "$VM_NAME" -"$VM_NET_VMIFACE" none
;;
esac
}
vbmStart () {
VBoxHeadless -s "$VM_NAME"
}
vbmInfo () {
VBoxManage showvminfo "$VM_NAME"
}
# MAIN FUNCTIONS
# show registred machines
function list {
listHead
VBoxManage list vms | grep \"| awk '{print $1}'
echo ""
}
# main level actions
function mainact {
PS3=" act sel > "
select ACT in "start" "list" "info" "new" "edit" "exit"; do
break
done
}
# creating new VM
new () {
newVmHead
edName
newVmHead
edMemory
newVmHead
edHD
newVmHead
edISO
newVmHead
edRDPPort
newVmHead
edNetwork
clear
echo "========================="
echo "=======Creating VM======="
echo "========================="
VM_MEM=$(echo "$VM_MEM""MB")
VM_HD_NAME=$(echo "$VM_NAME"".vdi")
# creating vm, setup acpi and boot device
vbmCreate
# setup memory size
vbmMemory
# creating hdd on hda
vbmHD
# registring CD and adding into VM
vbmCD
# rdp
vbmRDP
# network settings
vbmNetwork
exit
}
edit () {
editHead
machineSelector
editHead
echo "What not cool in $VM_NAME?"
PS3=' edit > '
select VM_EDIT_OPT in "network" "rdp port"; do
case $VM_EDIT_OPT in
'network')
editHead
edNetwork
vbmNetwork
;;
'rdp port')
editHead
edRDPPort
vbmRDP
;;
esac
done
exit
}
start () {
machineSelector
vbmStart
}
info () {
machineSelector
vbmInfo
}
################################
# == main level ==
mainHead
while [ true ]
do
mainact
case $ACT in
'list')
list
;;
'exit')
clear
exit
;;
'info')
info
;;
'edit')
edit
exit
;;
'new')
new
exit
;;
'start')
start
;;
'')
mainHead
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment