Skip to content

Commit

Permalink
Vagrant: add libvirt provider
Browse files Browse the repository at this point in the history
Signed-off-by: Philippe Vanhaesendonck <philippe.vanhaesendonck@oracle.com>
  • Loading branch information
AmedeeBulle committed Mar 13, 2020
1 parent 1298637 commit dfc9c00
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 0 deletions.
4 changes: 4 additions & 0 deletions oracle-linux-image-tools/README.md
Expand Up @@ -27,6 +27,9 @@ The tool currently supports:
- Vagrant (VirtualBox provider)
Target packages: VirtualBox guest additions
Image format: box
- Vagrant (libvirt provider)
Target packages: nfs-utils
Image format: box
- Generic (No cloud setup)
Target packages: none
Image format: OVA
Expand All @@ -38,6 +41,7 @@ The tool currently supports:
- For `OCI` or `OLVM` images, install the ` qemu-img` package:
`yum install qemu-img`
- For `Vagrant` box (VirtualBox provider), install [HashiCorp Vagrant](https://vagrantup.com/)
- For `Vagrant` box (libvirt provider), download the [create_box.sh](https://github.com/vagrant-libvirt/vagrant-libvirt/blob/master/tools/create_box.sh) third party script from the [vagrant-libvirt](https://github.com/vagrant-libvirt/vagrant-libvirt) project or install [HashiCorp Vagrant](https://vagrantup.com/) and the [vagrant-libvirt](https://github.com/vagrant-libvirt/vagrant-libvirt) plugin
1. Clone this repo:
`git clone https://github.com/oracle/ol-sample-scripts.git`
1. The build script need root privileges during the build.
Expand Down
17 changes: 17 additions & 0 deletions oracle-linux-image-tools/cloud/vagrant-libvirt/env.properties
@@ -0,0 +1,17 @@
# Default parameters for the vagrant-libvirt cloud.
# Do NOT change anything in this file, customisation must be done in separate
# env file.

# Location of the vagrant libvirt create_box.sh script.
# Install the vagrant-libvirt plugin on your build machine or download the
# script from:
# https://github.com/vagrant-libvirt/vagrant-libvirt/blob/master/tools/create_box.sh
# VAGRANT_LIBVIRT_BOX_SCRIPT="${HOME}/.vagrant.d/gems/<vagrant version>/gems/vagrant-libvirt-<version>/tools/create_box.sh"

# Memory and CPU to allocate to the box by default at runtime (default: use
# build VM parameters)
# VAGRANT_LIBVIRT_MEM_SIZE=
# VAGRANT_LIBVIRT_CPU_NUM=

# Install developer release packages (common to all providers)
VAGRANT_DEVELOPER_REPOS="no"
74 changes: 74 additions & 0 deletions oracle-linux-image-tools/cloud/vagrant-libvirt/image-scripts.sh
@@ -0,0 +1,74 @@
#!/usr/bin/env bash
#
# Cleanup and package image for the "vagrant-libvirt" image
#
# Copyright (c) 2020 Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at
# https://oss.oracle.com/licenses/upl.
#
# Description: this module provides 2 functions:
# cloud::image_cleanup: cloud specific actions to cleanup the image
# This function is optional
# cloud::image_package: Package the raw image for the target cloud.
# This function must be defined either at cloud or cloud/distribution level
#
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
#

#######################################
# Parameter validation
# Globals:
# VAGRANT_LIBVIRT_BOX_SCRIPT
# Arguments:
# None
# Returns:
# None
#######################################
cloud::validate() {
[[ -n ${VAGRANT_LIBVIRT_BOX_SCRIPT} && -x ${VAGRANT_LIBVIRT_BOX_SCRIPT} ]] || error "missing vagrant box_create script"
[[ ${VAGRANT_LIBVIRT_CPU_NUM} =~ ^[0-9]*$ ]] || error "vagrant cpu count is not numeric"
[[ ${VAGRANT_LIBVIRT_MEM_SIZE} =~ ^[0-9]*$ ]] || error "vagrant memory is not numeric"
[[ ${VAGRANT_DEVELOPER_REPOS,,} =~ ^(yes)|(no)$ ]] || error "VAGRANT_DEVELOPER_REPOS must be Yes or No"
}

#######################################
# Cleanup actions run directly on the image
# Globals:
# None
# Arguments:
# root filesystem directory
# boot filesystem directory
# Returns:
# None
#######################################
cloud::image_cleanup() {
:
}

#######################################
# Image packaging: generate box using vagrant tool
# Globals:
# VM_NAME
# Arguments:
# None
# Returns:
# None
#######################################
cloud::image_package() {
local cpus="${VAGRANT_LIBVIRT_CPU_NUM:-$CPU_NUM}"
local memory="${VAGRANT_LIBVIRT_MEM_SIZE:-$MEM_SIZE}"

# Convert to qcow2
qemu-img convert -c -O qcow2 System.img "${VM_NAME}.qcow"
rm System.img

# Defaults for the box
cat > Vagrantfile <<-EOF
config.vm.provider :libvirt do |libvirt|
libvirt.memory = ${memory}
libvirt.cpus = ${cpus}
end
EOF
${VAGRANT_LIBVIRT_BOX_SCRIPT} "${VM_NAME}.qcow" "${VM_NAME}.box" Vagrantfile
rm "${VM_NAME}.qcow" Vagrantfile
}
76 changes: 76 additions & 0 deletions oracle-linux-image-tools/cloud/vagrant-libvirt/provision.sh
@@ -0,0 +1,76 @@
#!/usr/bin/env bash
#
# Packer provisioning script for Vagrant-libvirt
#
# Copyright (c) 2020 Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at
# https://oss.oracle.com/licenses/upl.
#
# Description: Vagrant specific provisioning. This module provides 2 functions,
# both are optional.
# cloud::provision: provision the instance
# cloud::cleanup: instance cleanup before shutdown
#
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
#

# Load vagrant common scripts
source /tmp/packer_files/cloud/vagrant-common.sh

#######################################
# Configure Vagrant instance
# Globals:
# None
# Arguments:
# None
# Returns:
# None
#######################################
cloud::config()
{
vagrant::config
}

#######################################
# Install NFS client (needed for shared /vagrant folder)
# Globals:
# YUM_VERBOSE
# Arguments:
# None
# Returns:
# None
#######################################
cloud::install_agent()
{
echo_message "Install NFS client"
yum install -y ${YUM_VERBOSE} nfs-utils
}

#######################################
# Provisioning module
# Globals:
# None
# Arguments:
# None
# Returns:
# None
#######################################
cloud::provision()
{
cloud::install_agent
cloud::config
}

#######################################
# Cleanup module
# Globals:
# None
# Arguments:
# None
# Returns:
# None
#######################################
cloud::cleanup()
{
vagrant::cleanup
}
11 changes: 11 additions & 0 deletions oracle-linux-image-tools/env.properties
Expand Up @@ -84,6 +84,17 @@ CLOUD="none"
# Empty means no disk, otherwhise it is the size in GB of the extra disk
# VAGRANT_VIRTUALBOX_EXTRA_DISK_GB=

# Vagrant libvirt
# Location of the vagrant libvirt create_box.sh script.
# Install the vagrant-libvirt plugin on your build machine or download the
# script from:
# https://github.com/vagrant-libvirt/vagrant-libvirt/blob/master/tools/create_box.sh
# VAGRANT_LIBVIRT_BOX_SCRIPT="${HOME}/.vagrant.d/gems/<vagrant version>/gems/vagrant-libvirt-<version>/tools/create_box.sh"
# Memory and CPU to allocate to the box by default at runtime (default: same
# as build VM values)
# VAGRANT_LIBVIRT_MEM_SIZE=
# VAGRANT_LIBVIRT_CPU_NUM=

# Vagrant all providers
# Install developer release packages (Yes/No, default: no)
# VAGRANT_DEVELOPER_REPOS=

0 comments on commit dfc9c00

Please sign in to comment.