Skip to content

Instantly share code, notes, and snippets.

@davebarkerxyz
Created March 3, 2015 16:53
Show Gist options
  • Save davebarkerxyz/d807c7ceb3103db9d311 to your computer and use it in GitHub Desktop.
Save davebarkerxyz/d807c7ceb3103db9d311 to your computer and use it in GitHub Desktop.
Create new desktop (not headless) VirtualBox VM from template
#!/bin/bash
set -e
MASTER="deb7-dev-master"
NEWNAME=$1
IDENTITY="$HOME/.ssh/keys/dbark_1"
VMUSER="sysadmin"
SSHGO="ssh -i $IDENTITY -o StrictHostKeyChecking=no "
echo "Cloning $MASTER to $NEWNAME..."
vboxmanage clonevm "$MASTER" --name "$NEWNAME" --register
echo "Starting $NEWNAME..."
vboxmanage startvm "$NEWNAME" --type headless
echo "Getting IP for $NEWNAME..."
NEWIP=$(vboxmanage guestproperty wait "$NEWNAME" /VirtualBox/GuestInfo/Net/0/V4/IP | awk -F ", " '{print $2}' | awk -F": " '{print $2}' )
echo "Got IP $(tput setaf 2)$(tput bold)$NEWIP$(tput sgr 0) for $NEWNAME"
sleep 5 # Allow sshd to come up
echo "Updating packages on $NEWNAME..."
$SSHGO $VMUSER@$NEWIP "sudo apt-get update && sudo apt-get dist-upgrade -y"
echo "Updating hostname on $NEWNAME..."
$SSHGO $VMUSER@$NEWIP "sudo sed -i 's/$MASTER/$NEWNAME/g' /etc/hosts"
$SSHGO $VMUSER@$NEWIP "sudo sed -i 's/$MASTER/$NEWNAME/g' /etc/hostname"
echo "Restarting $NEWNAME..."
$SSHGO $VMUSER@$NEWIP "sudo shutdown -r now"
sleep 10 # Allow time to restart
echo "Reconnecting to $NEWNAME..."
NEWIP=$(vboxmanage guestproperty wait "$NEWNAME" /VirtualBox/GuestInfo/Net/0/V4/IP | awk -F ", " '{print $2}' | awk -F": " '{print $2}' )
echo ""
echo "$NEWNAME created successfully with IP $(tput setaf 2)$(tput bold)$NEWIP$(tput sgr 0)"
sleep 5 # Allow sshd to come up
$SSHGO $VMUSER@$NEWIP
@davebarkerxyz
Copy link
Author

Admin user $VMUSER should be setup on $MASTER VM with sudo set to passwordless.

SSH key should be preinstalled on template, and reside on local machine at $IDENTITY.

$MASTER VM should have NIC #0 accessible from the host (bridged or internal network with DHCP).

$MASTER should have a kernel which includes the VirtualBox guest extensions, as neccessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment