Skip to main content


Creating a Vagrant Box from an Ubuntu Cloud Image

To create a Vagrant box from Ubuntu Cloud Imagesopen in new window, follow these steps.
These instructions explain how to convert an Ubuntu Cloud Image into a Vagrant-compatible box, allowing you to use it locally with the "vagrant up" command.
This enables you to create your own Vagrant "Base-boxes" and reduces your dependency on pre-built boxes from the Vagrant Cloud.

We cling to Ubuntu Images because mDIS runs well on that Linux distribution and its graphical desktop environments.

  1. Download the Official Ubuntu OVA File:

    • Navigate to Ubuntu Cloud Imagesopen in new window.
    • Select the desired Ubuntu release (e.g., noble64/ for Ubuntu 24.04 LTS).
    • Download the .ova file, such as noble-server-cloudimg-amd64.ova.
  2. Modify the OVA File:

    • Extract the contents of the OVA:
      tar -xvf noble-server-cloudimg-amd64.ova
      
    • Rename the .ovf file to box.ovf:
      (.ovf: Open Virtualization Format, an XML file format that describes the virtual machine settings and configuration)
      mv ubuntu-noble-24.04-cloudimg.ovf box.ovf
      
    • Repackage the .ova file with the renamed box.ovf:
      tar -cvf noble-server-cloudimg-amd64.ova box.ovf ubuntu-noble-24.04-cloudimg.vmdk
      
  3. Add Metadata for Vagrant:

    • Create a tiny metadata.json file with the following content:
      {
        "provider": "virtualbox"
      }
      
    • Add metadata.json to the OVA:
      tar --append --file=noble-server-cloudimg-amd64.ova metadata.json
      
  4. Prepare a second metadata file, not part of OVA file

    • Create a metadata JSON file with the following content. You can call it box-catalog.json, for example. The values are freely selectable:
      {
        "name": "ubuntu/noble64",
        "description": "Ubuntu 24.04 LTS (noble) Vagrant Box, 64-bit server",
        "versions": [
          {
            "version": "20250117",
            "providers": [
              {
                "name": "virtualbox",
                "url": "noble-server-cloudimg-amd64.ova"
              }
            ]
          }
        ]
      }
      
  5. Import the Box into Vagrant box cache:

    • Add the box to Vagrant:
      vagrant box list
      # remove the box if it already exists
      vagrant box remove noble64 --provider virtualbox --box-version 20250117
      # re-add the box.
      vagrant box add --name ubuntu/noble64 ./box-catalog.json
      
    • now a copy should be in $HOME/.vagrant.d/boxes/ directory.
  6. Configure Cloud-Init for the Vagrant User:

    • Create a user-data.yaml file with the following content:
      users:
        - name: vagrant
          gecos: Vagrant User
          groups: users
          sudo: ALL=(ALL) NOPASSWD:ALL
          shell: /bin/bash
          lock_passwd: true
          # vagrant's widely-known insecure public key 
          ssh_authorized_keys:
            - "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key"
         # ....on host, find private key in ~/vagrant.d/insecure_private_key
      
      This enables the initial login of the vagrant user with the insecure private key provided by Vagrant.
    • In your Vagrantfile (see below for an example), add:
      config.vm.box = 'ubuntu/noble64'
      config.vm.cloud_init content_type: "text/cloud-config", path: "./user-data.yaml"
      

    File user-data.yaml contains Metadata for the user vagrant and the initial login key.

  7. Initialize and Start the Vagrant Environment:

    • Run:
      vagrant up
      

Now customize the running Virtual Machine. Installing and configure software and applications as needed.

Vagrantfile Example

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/noble64"
  config.vm.box_version = "20250115.0.1"
  config.vm.box_check_update = false
  config.vm.network "private_network", type: "dhcp"
  config.vm.cloud_init content_type: "text/cloud-config", path: "./user-data.yaml"   
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
    vb.cpus = 2
  end
  config.vm.provision "shell", inline: <<-SHELL
    # Add your provisioning scripts here
    echo "Provisioning completed."
  SHELL
end

As mentioned, line 6, config.vm.cloud_init ..., instructs Vagrant to apply the cloud-init configuration specified in user-data.yaml (see step 6 above) during the provisioning of the virtual machine. By doing so, the vagrant user is created with the defined settings, ensuring consistent and automated user setup across your Vagrant environments.

Cloud-init is a utility installed within the guest machine (the virtual machine or cloud instance) that automates its initial configuration upon boot.

References

More about cloud-init: Cloud-Init Documentationopen in new window

More about Vagrant: Vagrant Documentationopen in new window

Back to "installation-virtualbox-from-online-instance" Instructions