{"id":1065,"date":"2018-10-24T23:35:08","date_gmt":"2018-10-24T20:35:08","guid":{"rendered":"http:\/\/kifarunix.com\/?p=1065"},"modified":"2024-03-11T19:59:32","modified_gmt":"2024-03-11T16:59:32","slug":"automate-virtual-machine-installation-on-virtualbox","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/automate-virtual-machine-installation-on-virtualbox\/","title":{"rendered":"Automate Virtual Machine Installation on VirtualBox"},"content":{"rendered":"\n<p>In this tutorial, you will learn how to automate virtual machine installation on VirtualBox. Many times, not once, not twice when you would want to create virtual machine on VirtualBox. So just imagine how tedious and time consuming it is to create a VM manually every time you need a virtual lab to play with. No, that manual procedure is not for you.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Automating Virtual Machine Installation on VirtualBox<\/h2>\n\n\n\n<p>To automate virtual machine creation on VirtualBox, we are going to use the <strong><a href=\"https:\/\/docs.oracle.com\/en\/virtualization\/virtualbox\/7.0\/user\/BasicConcepts.html#unattended-guest-install-example\" target=\"_blank\" rel=\"noreferrer noopener\">unattended install<\/a><\/strong> feature of VBoxManage command. With this feature, you can get the VM automatically installed with very little human interaction while setting up installation variables.<\/p>\n\n\n\n<p><strong>You can also script the whole of this procedure to achieve full blown automation.<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Install VirtualBox on your Host system<\/h3>\n\n\n\n<p>To begin with, you need to have installed VirtualBox on your host system. You can check the link below on how to install VirtualBox;<\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/?s=install+virtualbox\" target=\"_blank\" rel=\"noreferrer noopener\">How to install VirtualBox on Linux system<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create and Register a Virtual Machine<\/h3>\n\n\n\n<p>As a first step on how to automatatically install a virtual machine on VirtualBox, run the command below to create and register the vm.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>VBoxManage createvm --name ubuntu18server --ostype Ubuntu_64 --register<\/code><\/pre>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Virtual machine 'ubuntu18server' is created and registered.\nUUID: 41d12b9c-10b2-46a1-8710-7f00a9a02de7\nSettings file: '\/home\/kifarunix\/VirtualBox VMs\/ubuntu18server\/ubuntu18server.vbox'<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <strong>createvm<\/strong> command creates a new XML virtual machine definition file.<\/li>\n\n\n\n<li>The <strong>&#8211;ostype<\/strong> option specifies the guest operating system to run in the VM. Run the command<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>VBoxManage list ostypes | less<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <strong>&#8211;register<\/strong> option imports the virtual machine definition in an XML file into VirtualBox.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Setup the Virtual Machine Storage Medium<\/h2>\n\n\n\n<p>Once you have registered a VM, create a storage medium as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>VBoxManage createmedium \\\n--filename \/home\/kifarunix\/VirtualBox\\ VMs\/ubuntu18server\/ubuntu18server.vdi \\\n--size 10240<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <strong>createmedium <\/strong>command creates a new medium<\/li>\n\n\n\n<li>The <strong>&#8211;filename<\/strong> option specifies the location of the VM. As shown above, the format of the medium is VDI. Other formats exist; VMDK, VHD.<\/li>\n\n\n\n<li>The <strong>&#8211;size<\/strong> option specifies the size of the storage medium in megabytes.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Add and Attach SATA and IDE Storage Controllers<\/h3>\n\n\n\n<p>VirtualBox usually uses SATA controller as the default for newly created virtual machines. To add a SATA controller to VM, run the command below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>VBoxManage storagectl ubuntu18server --name SATA --add SATA --controller IntelAhci<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>VBoxManage storageattach ubuntu18server \\\n--storagectl SATA --port 0 --device 0 --type hdd \\\n--medium \/home\/kifarunix\/VirtualBox\\ VMs\/ubuntu18server\/ubuntu18server.vdi<\/code><\/pre>\n\n\n\n<p>IDE ATA provides support CD-ROM drives and other types of removable media. It enables you to attach upto four virtual storage devices to your vm. One of these four virtual storages can be preconfigured to be the machine&#8217;s virtual CD\/DVD drive.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>VBoxManage storagectl ubuntu18server --name IDE --add ide<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>VBoxManage storageattach ubuntu18server \\\n--storagectl IDE --port 0 --device 0 --type dvddrive \\\n--medium \/home\/kifarunix\/Downloads\/iso\/Debian\/ubuntu-22.04-desktop-amd64.iso<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <strong>storagectl<\/strong> command defines the storage controller for the VM<\/li>\n\n\n\n<li>The <strong>&#8211;name<\/strong> parameter specifies the name of the storage controller. It is mandatory.<\/li>\n\n\n\n<li>The <strong>&#8211;add<\/strong> parameter specifies the type of the system bus to which the storage controller must be connected.<\/li>\n\n\n\n<li>The <strong>&#8211;controller<\/strong> parameter enables a choice of chipset type being emulated for the given storage controller.<\/li>\n\n\n\n<li>The <strong>storageattach<\/strong> command attaches the storage medium to the controller.<\/li>\n\n\n\n<li>The <strong>&#8211;port<\/strong> parameter specifies the number of the storage controller&#8217;s port which is to be modified.<\/li>\n\n\n\n<li>The <strong>&#8211;type<\/strong> parameter defines the type of the drive to which the medium is being attached<\/li>\n\n\n\n<li>The <strong>&#8211;medium<\/strong> parameter specifies what is to be attached.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Define the VM General Settings<\/h2>\n\n\n\n<p>Next, we are going to define various properties of the registered VM. To achieve this, we are going to us the <strong>modifyvm<\/strong> command. Most of the properties that this command makes available correspond to the VM settings that VirtualBox graphical user interface displays in each VM&#8217;s &#8220;Settings&#8221; dialog.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Set the VM RAM and Virtual graphics card RAM size<\/h4>\n\n\n\n<p>Allocate the vm a specific RAM as well as nvram size as shown below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>VBoxManage modifyvm ubuntu18server --memory 1024 --vram 16<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Enable IO APIC<\/h4>\n\n\n\n<p>With IO APIC enabled, the OS can use more than 16 interrupt requests (IRQs) thus avoiding IRQ sharing for improved reliability.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>VBoxManage modifyvm ubuntu18server --ioapic on<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Define the boot order for the virtual machine<\/h4>\n\n\n\n<p>Run the command below to define the vm boot order.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>VBoxManage modifyvm ubuntu18server --boot1 dvd --boot2 disk --boot3 none --boot4 none<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Define the number of virtual CPUs for the VM<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>VBoxManage modifyvm ubuntu18server --cpus 2<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Disable Audio for the VM<\/h4>\n\n\n\n<p>Since this is a server, there is no point of having audio enabled.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>VBoxManage modifyvm ubuntu18server --audio none<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Disable USB. USB2.0, USB3.0 controllers.<\/h4>\n\n\n\n<p>Since I am not planning on using USB with my VM, am choosing to disable this. The case may be different for you.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>VBoxManage modifyvm ubuntu18server --usb off\nVBoxManage modifyvm ubuntu18server --usbehci off\nVBoxManage modifyvm ubuntu18server --usbxhci off<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Define the Networking settings for the VM<\/h2>\n\n\n\n<p>In this case, am going to assign my VM two NICs;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>NAT and Bridged for NIC1 and NIC2 respectively.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>VBoxManage modifyvm ubuntu18server --nic1 bridged --bridgeadapter1 wlp0s20f3 --nic2 nat<\/code><\/pre>\n\n\n\n<p>You can define as many settings as it pleases you.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Virtual Machine Unattended Installation<\/h2>\n\n\n\n<p>Once you are done, it is time to install the VM unattended. Run the following&nbsp;&nbsp; command to start the VM auto-installation.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>VBoxManage unattended install ubuntu18server \\\n--user=kifarunix --password=P@SSWORD \\\n--country=US --time-zone=EST \\\n--language=en-US \\\n--hostname=ubuntu18server.kifarunix.com \\\n--iso=\/home\/kifarunix\/Downloads\/iso\/Debian\/ubuntu-22.04-desktop-amd64.iso \\\n--start-vm=gui<\/code><\/pre>\n\n\n\n<p>Sample command output;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>\nVBoxManage: info: Starting unattended installation of Ubuntu_64 in machine 'ubuntu18server' (0875d510-dbe1-456f-8e72-ee2ee28757ad).\nVBoxManage: info: Using values:\n                           isoPath = \/home\/kifarunix\/Downloads\/iso\/Debian\/ubuntu-22.04-desktop-amd64.iso\n                              user = kifarunix\n                          password = P@SSWORD\n                      fullUserName = \n                        productKey = \n                  additionsIsoPath = \/usr\/share\/virtualbox\/VBoxGuestAdditions.iso\n             installGuestAdditions = false\n              validationKitIsoPath = \n            installTestExecService = false\n                            locale = en_US\n                           country = US\n                          timeZone = EST\n                             proxy = \n                          hostname = ubuntu18server.kifarunix.com\n       packageSelectionAdjustments = \n                 auxiliaryBasePath = \/home\/kifarunix\/vol01\/VirtualBox VMs\/ubuntu18server\/Unattended-0875d510-dbe1-456f-8e72-ee2ee28757ad-\n                        imageIndex = 1\n                scriptTemplatePath = \/usr\/share\/virtualbox\/UnattendedTemplates\/ubuntu_preseed.cfg\n     postInstallScriptTemplatePath = \/usr\/share\/virtualbox\/UnattendedTemplates\/debian_postinstall.sh\n                postInstallCommand = \n      extraInstallKernelParameters =  auto=true preseed\/file=\/cdrom\/preseed.cfg priority=critical quiet splash noprompt noshell automatic-ubiquity debian-installer\/locale=en_US keyboard-configuration\/layoutcode=us languagechooser\/language-name=English localechooser\/supported-locales=en_US.UTF-8 countrychooser\/shortlist=US --\n                          language = en-US\n                  detectedOSTypeId = Ubuntu_64\n                 detectedOSVersion = 18.04.1 LTS \"Jammy Jellyfish\"\n                  detectedOSFlavor = Ubuntu-Server \n               detectedOSLanguages = en-US\n                   detectedOSHints = \nVBoxManage: info: Waiting for VM 'ubuntu18server' to power on...\nVBoxManage: info: VM 'ubuntu18server' (0875d510-dbe1-456f-8e72-ee2ee28757ad) has been successfully started.\n<\/code><\/pre>\n\n\n\n<p>What the command does is that, it launches and configure the VM with all the variable set. The breakdown of the same command is as in below;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <strong>unattended install<\/strong> configures the VM with the defined variables non-interactively.<\/li>\n\n\n\n<li><strong>&#8211;user<\/strong> sets the login username. If not specified, the default user set will be vboxuser.<\/li>\n\n\n\n<li><strong>&#8211;password<\/strong> sets the password for the defined login user<\/li>\n\n\n\n<li><strong>&#8211;country<\/strong> speficies the two letter country code.<\/li>\n\n\n\n<li><strong>&#8211;time-zone<\/strong> specifies the time zone to set up the guest OS with.<\/li>\n\n\n\n<li><strong>&#8211;hostname<\/strong> sets the VM hostname to the defined fqdn.<\/li>\n\n\n\n<li><strong>&#8211;iso<\/strong> specifies the installation ISO to run the detection on.<\/li>\n\n\n\n<li><strong>&#8211;start-vm<\/strong> starts the VM using the front end given by session type such as headless, gui<\/li>\n<\/ul>\n\n\n\n<p>That is all it takes to automate virtual machine installation on VirtualBox. The process may not seem automated due to various settings being made. Therefore, consider putting all these steps in bash script to achieve full automation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Other Tutorials<\/h2>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-kvm-on-oracle-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\">Install KVM on Oracle Linux<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/change-windows-boot-device-from-ide-to-virtio-on-kvm\/\" target=\"_blank\" rel=\"noreferrer noopener\">Change Windows Boot Device from IDE to Virtio on KVM<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to automate virtual machine installation on VirtualBox. Many times, not once, not twice when you would want to<\/p>\n","protected":false},"author":1,"featured_media":15192,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[36,121,46],"tags":[6261,210,209,6263,24,6262],"class_list":["post-1065","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-virtualization","category-howtos","category-virtualbox","tag-automate-virtual-machine-install-virtualbox","tag-automation","tag-unattended-os-install","tag-unattended-virtualbox-guest-installation","tag-virtualbox","tag-virtualbox-unattended-guest-install","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/1065"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=1065"}],"version-history":[{"count":5,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/1065\/revisions"}],"predecessor-version":[{"id":21025,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/1065\/revisions\/21025"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/15192"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=1065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=1065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=1065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}