Skip to content

Instantly share code, notes, and snippets.

@FelisDiligens
Created September 27, 2023 18:33
Show Gist options
  • Save FelisDiligens/15380a4d8384513f3b4e0a996aa156fe to your computer and use it in GitHub Desktop.
Save FelisDiligens/15380a4d8384513f3b4e0a996aa156fe to your computer and use it in GitHub Desktop.
VirtualBox VM helper script
#!/bin/bash
# To get the UUID, run $ VBoxManage list vms
vm_uuid="{dc9fb426-ea9e-4583-aa05-95455b2c0814}"
# Install OpenSSH in VM:
ssh_user=$USER
ssh_host=$hostname
# Install Samba in VM:
smb_host="$ssh_host"
smb_share="$ssh_host"
smb_username="$ssh_user"
smb_password="password123"
smb_drive="L:"
if [ -n "$1" ] && ([ "$1" = "poweron" ] || [ "$1" = "pon" ]);
then
echo "Starting VM in headless mode..."
if [ -n "$2" ] && ([ "$2" = "separate" ] || [ "$2" = "gui" ]);
then
VBoxManage startvm $vm_uuid --type separate
else
VBoxManage startvm $vm_uuid --type headless
fi
elif [ -n "$1" ] && ([ "$1" = "poweroff" ] || [ "$1" = "poff" ]);
then
echo "Sending ACPI signal (power button) to VM..."
VBoxManage controlvm $vm_uuid acpipowerbutton
# VBoxManage controlvm $vm_uuid poweroff
elif [ -n "$1" ] && ([ "$1" = "terminal" ] || [ "$1" = "term" ]);
then
ssh "${ssh_user}@${ssh_host}"
elif [ -n "$1" ] && [ "$1" = "state" ];
then
VBoxManage showvminfo $vm_uuid | grep "State" | sed 's/State:\s*//' | sed -e 's/\s*(since.*)//g'
elif [ -n "$1" ] && [ "$1" = "mount" ];
then
net use $smb_drive \\\\$smb_host\\$smb_share /user:$smb_username $smb_password
elif [ -n "$1" ] && ([ "$1" = "unmount" ] || [ "$1" = "umount" ]);
then
net use $smb_drive /delete
else
if [ -n "$1" ] && [ "$1" != "help" ];
then
echo "command not recognized: $1"
echo ""
fi
echo "usage: vm.sh <cmd>"
echo ""
echo "VM helper bash script"
echo ""
echo "<cmd> options:"
echo " poweron (or 'pon') [gui]"
echo " poweroff (or 'poff')"
echo " terminal (or 'term')"
echo " state"
echo " mount"
echo " unmount (or 'umount')"
echo " help"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment