#!/bin/bash
#VAR
ISO_LOCATION=/home/qiwichupa/ISO/
################################
# == functions ==
# HEADS
function mainHead {
clear
echo "=================================="
echo "========== Main options =========="
echo "=================================="
echo ""
}
function newVmHead {
clear
echo "=================================="
echo "======== Creating new VM ========="
echo "=================================="
echo ""
}
function listHead {
clear
echo "=================================="
echo "========= Your machines =========="
echo "=================================="
echo ""
}
function helpHead {
clear
echo "=================================="
echo "============== HELP =============="
echo "=================================="
echo ""
}
# creating CD selector
function cdSelector {
# mystical action with iso list
# http://www.linux.org.ru/view-message.jsp?msgid=3128616#comment-3132277
temp=$IFS
IFS=$'\n'
ls=$(ls -1 "$ISO_LOCATION")
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='Choose ISO: '
eval set $tmp
select opt in "$@"
do
echo $opt
VM_CD=$opt
break
done
}
# MAIN FUNCTIONS
# registred machines
function list {
listHead
VBoxManage list vms | grep \"| awk '{print $1}'
echo ""
}
# main level actions
function mainact {
echo "Enter action, '?' for action list or 'x' for exit."
echo -n " act sel > "
read ACT
}
# help
function help {
helpHead
echo "list — show the list of machines"
echo "new — create new machine"
echo "del — delete machine"
echo "start — start machine"
echo "stop — stop machine"
echo ""
}
# creating new VM
function new {
newVmHead
echo "Machines:"
VBoxManage list vms | grep \"| awk '{print $1}'
echo ""
echo "Enter new machine name."
echo -n " new, step 1 - name > "
read VM_NAME # vm name
NAME_TEST=$(VBoxManage list vms | grep "$VM_NAME") # check registred names
if [ -z "$NAME_TEST" ]; then # if unique...
newVmHead
echo "Enter memory size (Mb)."
echo -n " new, step 2 - memory > "
read VM_MEM # vm memory size
newVmHead
echo "Enter HardDrive size (Mb) or leave blank to skip HD creating."
echo -n " new, step 3 - HardDrive > "
read VM_HD # vm hd size
newVmHead
echo "ISO location: $ISO_LOCATION"
echo "Available ISOs:"
cdSelector
clear # if correct...
echo "========================="
echo "=======Creating VM======="
echo "========================="
VM_MEM=$(echo "$VM_MEM""MB")
VM_HD_NAME=$(echo "$VM_NAME"".vdi")
# creating vm
VBoxManage createvm -name "$VM_NAME" -register
# setup memory size, acpi, boot device, network options
VBoxManage modifyvm "$VM_NAME" -memory "$VM_MEM" -acpi on -boot1 dvd -nic1 bridged --bridgeadapter1 eth0
if [ -z "$VM_HD" ]; then # if HD size is not set
echo "Without HardDrive..."
else # else...
# creating HD and adding into VM
VBoxManage createvdi -filename "$VM_HD_NAME" -size "$VM_HD" -register
VBoxManage modifyvm "$VM_NAME" -hda "$VM_HD_NAME"
fi
# registring CD and adding into VM
FULL_CD=$(echo $ISO_LOCATION$VM_CD)
echo $FULL_CD
VBoxManage registerimage dvd "$FULL_CD"
VBoxManage modifyvm "$VM_NAME" -dvd "$FULL_CD"
else # if name is not unique
clear
list
echo "Error: choose another name"
break
fi
}
################################
# == main level ==
mainHead
while [ true ]
do
mainact
case $ACT in
'list')
list
;;
'?')
help
;;
'x')
clear
exit
;;
'new')
new
;;
'')
mainHead
;;
esac
done