Archiv für Januar, 2016

Create VirtualBox virtual machine in command line

30. Januar 2016 Keine Kommentare

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
# create a new virtualbox machine
## (c) 2016, Arkadiusz Drabczyk

if [ "$#" -lt 2 ]
    echo Usage: "$0" \<MACHINE-NAME\> \<ISO\> [RAM] [DISK] [TYPE]
    echo Example:
    echo ./ \"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


set -e

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

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:

Kategorienshell, virtualbox Tags: