Home > shell, virtualbox > Create VirtualBox virtual machine in command line

Create VirtualBox virtual machine in command line

Although I don’t think about it every day today I realized that I used quite a few Linux based and Unix-like systems so far: a short adventure with Fedora known as Fedora Core 3 around 10 years ago, Slackware at home (and soon also at work), Tizen at work, Ubuntu at work and on parents‘ machine, FreeBSD in VirtualBox, OpenWRT on router and Android on a phone and tablet and OpenELEC on Raspberry Pi. Each system is different – they differ in package manager, startup style, libc, some provide GNU-based coreutils and some don’t. Time to try even more – let’s try new Fedora, Arch, Debian, CentOS, Gentoo, NetBSD, MacOS, OpenIndiana, BeOS, Haiku, ReoctOS and others! I am especially interested in learning new concepts and easy testing of shell scripts I wrote. Ideally they should be as portable as possible. VirtualBox is great for testing new systems. It allows to run multiple system at the same time without the constant need to reboot and tinkering with partitions layout and boot order. To make experimenting with new systems I made a short script that creates a virtual machine automatically. It takes two parameters: a name of a new virtual machine and a path to ISO file. Third paramater is optional and describes system type – it defaults to x64 Linux. Here it is:

#!/usr/bin/env sh
# vb.sh: create a new virtualbox machine
#
## (c) 2016, Arkadiusz Drabczyk

if [ "$#" -lt 2 ]
then
    echo Usage: "$0" \<MACHINE-NAME\> \<ISO\> [RAM] [DISK] [TYPE]
    echo Example:
    echo ./vb.sh \"Slackware x64 14.1\" slackware64-14.1-install-dvd.iso
    echo RAM is 256 megabytes if not explicitly requested.
    echo Disk is 20 gigabytes if not explicitly requested.
    echo Type is Linux_64 if not explicitly requested.
    exit 1
fi

ram=${3:-256}
hdd=${4:-20000}
type=${5:-Linux_64}

set -e

vboxmanage createvm --name "$1" \
                    --ostype "$type" \
                    --register

vboxmanage modifyvm "$1" \
                    --memory "$ram" \
                    --usb on

vboxmanage createhd --filename "$PWD/${1}.vdi" \
                    --size "$hdd"

vboxmanage storagectl "$1" \
                    --name "IDE Controller" \
                    --add ide

vboxmanage storageattach "$1" \
                    --storagectl "IDE Controller" \
                    --port 0 \
                    --device 0 \
                    --type hdd \
                    --medium "$PWD/${1}.vdi"

vboxmanage storageattach "$1" \
                    --storagectl "IDE Controller" \
                    --port 1 \
                    --device 0 \
                    --type dvddrive \
                    --medium "$2"

Also available as Github gist: https://gist.github.com/ardrabczyk/65b68d0121f2964cd99e

Kategorienshell, virtualbox Tags:
  1. Bisher keine Kommentare