Virtual Machines with media on a separate data partition

Discussion and Technical Support for general software applications and utilities including OS related issues.
thinmintaddict
Member
Member
Posts: 25
Joined: March 24th, 2011, 6:35 am

Re: Virtual Machines with media on a separate data partition

Post by thinmintaddict »

I figured I should also provide my scripts as none are really available for VirtualBox.
This is my script to make VMs:

Code: Select all

#!/bin/bash#
#Name of file: makevm
#Test if a process is currently running.

# This script will create a VM with 1G of Memory and a 10GB Hard Drive with the same name as the VM. this can of course be changed in the script.
# The Proper Usage is : "makevm 'nameofVM' '/full/path/to/install/dvd.iso'"
# if you don't wish to use a dvd, you can use "none" as the last variable.

## VirtualMachine Variables ##
machine=
dvd=

if [ "$1" != "" ]; then
    if [ "$2" != "" ]; then
        machine=$1
        dvd=$2
        VBoxManage createvm --name "$machine" --register
        VBoxManage modifyvm "$machine" --memory 1024 --acpi on --boot1 dvd --nic1 bridged --bridgeadapter1 eth0
        VBoxManage createhd --filename "$vdi".vdi --size 10000 --register
        VBoxManage storagectl "$machine" --name "IDE Controller" --add ide
        VBoxManage storageattach "$machine" --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium "$vdi".vdi
        VBoxManage storageattach "$machine" --storagectl "IDE Controller" --port 1 --device 0 --type dvddrive --medium "$dvd"
    else
        echo "Proper Usage is: makevm <nameofVM> <dvdpath>"
    fi
  else
     echo "Proper Usage is: makevm <nameofVM> <dvdpath>"
  fi
fi

#EOF

To Use it you simply run "./makevm <nameofVM> </full/path/to/install/dvd.iso>" if you don't want to define an iso, you can replace the second variable with "none".

and this is the script I use to start VMs. It works very similarly to the one Ian wrote for KVM. I don't have ssmtp set up on my server yet, so it doesnt have the error email functionality yet.

Code: Select all

#!/bin/bash
# Script to start a VirtualBox VM on start up. to use, add this script as a
# cron job in the crontab of the user that controls your VMs.

# This Scripts expects  a single parameter, which is the name of your VM.
# So proper Usage is "startVM nameofyourvm"
 
#list the necessary Variables
VMNAME=$1
VMSTATUS=`VBoxManage list runningvms | grep $VMNAME | wc -l` #don't change this line, this checks whether the VM is running.

sleep 180 # wait 3 minutes for the server to finish booting up so all the VBox Modules have a chance to load

if [ $VMSTATUS = 1 ];
   then echo "$VMNAME is Already Running, Aborting Script."
   else
     echo "$VMNAME is NOT running, starting now."
     VBoxManage startvm $VMNAME -type vrdp >/dev/null 2>&1
 # This is the command if you are using VirtualBox 3.2. If you have upgraded to VirtualBox 4.0, replace "-type vrdp" with  "-type headless"
     if [ `VBoxManage list runningvms | grep $VMNAME | wc -l` = "1" ]
       then echo "Done."
       else
         echo "Error Starting VM. Please check the VMNAME Variable, and that you are running this script as your VirtualBox Administrator."
         
     fi
fi

#EOF

All it does is check to make sure the VM is not already running, and if it isn't, it starts it. It does wait 3 minutes to execute the commands. this gives the server time to boot, so that the resources VBox uses will be available.
To start the VM at start up add the full path to the script as a cronjob to be run by the user that controls your VirtualBox.
so for me i added "/home/russell/scripts/startVM MediaServer" to cron to be run as "russell"

same as always,

type

Code: Select all

nano makevm
OR
nano startVM

paste the script into the file and save it.
then run

Code: Select all

chmod a+x makevm
OR
chmod a+x startVM

to make them executable.
Post Reply