X

News, tips, partners, and perspectives for the Oracle Linux operating system and upstream Linux kernel work

  • Linux
    February 17, 2020

Building (Small) Oracle Linux Images For The Cloud

Overview

Oracle Linux Image Tools is a sample project to build small or customized Oracle Linux Cloud images in a repeatable way.

It provides a bash modular framework which uses HashiCorp Packer to build images in Oracle VM VirtualBox. Images are then converted to an appropriate format depending on the Cloud provider.

This article shows you how to build the sample images from this repository and how to use the framework to build custom images.

The framework is based around two concepts: Distribution and Cloud modules.

A Distribution module is responsible for the installation and configuration of Oracle Linux as well as the packages needed for your project. The sample ol7-slim distribution provides an Oracle Linux 7 image with a minimalist set of packages (about 250 packages – smaller than an Oracle Linux 7 Minimal Install).

A Cloud module ensures that the image is properly configured and packaged for a particular cloud provider. The following modules are currently available:

  • oci: Oracle Cloud Infrastructure (QCOW2 file)
  • olvm: Oracle Linux Virtualization Manager (OVA file)
  • ovm: Oracle VM Server (OVA file)
  • azure: Microsoft Azure (VHD file)
  • none: no cloud customization (OVA file)

Build requirements

Environment

A Linux environment is required for building images. The project is developed and tested with Oracle Linux 7, but should run on most Linux distribution. If your environment is a virtual machine, it must support nested virtualization.

The build tool needs root privileges to mount the generated images. Ensure sudo is properly configured for the user running the build.

Software

You will need the following software installed:

  • HashiCorp Packer and Oracle VM VirtualBox
    yum --enablerepo=ol7_developer install packer VirtualBox-6.0
  • kpartx and qemu-img to manipulate the artifacts
    yum install kpartx qemu-img

Disk space

You will need at least twice the size of your images as free disk space. That is: building a 30GB image will require 60GB of free space.

Building the project images

Building the images from the project is straightforward.

Configuration

Build configuration is done by editing the env.properties file (or better, a copy of it).
Options are documented in the property file, but at the very least you must provide:

  • WORKSPACE: the directory used for the build
  • ISO_URL / ISO_SHA1_CHECKSUM: location of the Oracle Linux ISO image.
    You can download it from the Oracle Software Delivery Cloud or use a public mirror.
    The image is cached in the workspace.
  • DISTR: the Distribution to build
  • CLOUD: the target cloud provider.

Sample build

The following env.properties.oci property file is used to build a minimal OL7 image for the Oracle Cloud Infrastructure, using all default parameters:

WORKSPACE="/data/workspace"
ISO_URL="http://my.mirror.example.com/iso/ol7/OracleLinux-R7-U7-Server-x86_64-dvd.iso"
ISO_SHA1_CHECKSUM="3ef94628cf1025dab5f10bbc1ed2005ca0cb0933"
DISTR="ol7-slim"
CLOUD="oci"

Run the script:

$ ./bin/build-image.sh --env env.properties.oci
+++ build-image.sh: Parse arguments
+++ build-image.sh: Load environment
+++ build-image.sh: Stage Packer files
+++ build-image.sh: Stage kickstart file
+++ build-image.sh: Generate Packer configuration file
+++ build-image.sh: Run Packer
    build-image.sh: Spawn HTTP server
    build-image.sh: Invoke Packer

...

    build-image.sh: Package image
+++ build-image.sh: Cleanup Workspace
+++ build-image.sh: All done
+++ build-image.sh: Image available in /data/workspace/OL7U7_x86_64-oci-b0
$

That’s it!
The /data/workspace/OL7U7_x86_64-oci-b0 directory now contains OL7U7_x86_64-oci-b0.qcow, a QCOW2 file which can be imported and run on OCI.

Adding new modules

Directory layout

Directory layout

Each Distribution module is represented by a subdirectory of the distr directory.
Each Cloud module is represented by a subdirectory of the cloud directory.
Additionally, Cloud actions for a specific Distribution can be defined in the cloud/<cloud>/<distr> directory.

Any element not necessary can be omitted – e.g. the none cloud module only provides a packaging function.

All the env.properties files are merged and made available to the scripts at runtime. They define parameters with default values which can be overridden by the user in the global env.properties file in the project base directory.

Adding a distribution

To add a new distribution, create a directory in distr/ with the following files:

  • env.properties: parameters for the distribution.
  • ks.cfg: a kickstart file to bootstrap the installation. This is the only mandatory file.
  • image-scripts.sh: a shell script with the following optional functions which will be invoked on the build host:
    • distr::validate: validate the parameters before the build.
    • distr::kickstart: tailor the kickstart file based on the parameters.
    • distr::image_cleanup: disk image cleanup run at the end of the build.
  • provision.sh: a shell script with the following optional functions which will be invoked on the VM used for the build:
    • distr::provision: image provisioning (install/configure software)
    • distr::cleanup: image cleanup (uninstall software, …)
  • files directory: the files in this directory are copied to the image in /tmp/distr and can be used by the provisioning scripts.

Adding a cloud

The process is similar to the distribution: create a directory in cloud/ with the following files:

  • env.properties: parameters for the cloud.
  • image-scripts.sh: a shell script with the following optional functions which will be invoked on the build host:
    • cloud::validate: validate the parameters before the build.
    • cloud::kickstart: tailor the kickstart file based on the parameters.
    • cloud::image_cleanup: disk image cleanup run at the end of the build.
    • cloud::image_package: package the image in a suitable format for the cloud provider. This is the only mandatory function.
  • provision.sh: a shell script with the following optional functions which will be invoked on the VM used for the build:
    • cloud::provision: image provisioning (install/configure software)
    • cloud::cleanup: image cleanup (uninstall software, …)
  • files directory: the files in this directory are copied to the image in /tmp/cloud and can be used by the provisioning scripts.

If some cloud actions are specific to a particular distribution, they can be specified in the <cloud>/<distr> subdirectory.
If a cloud_distr::image_package function is provided it will override the cloud::image_package one.

Builder flow

The complete build flow is illustrated hereunder: Build flow

The builder goes through the following steps:

  1. Build environment
    1. All the env.properties files are sourced and merged. The user provided one is sourced last and defines the build behavior
    2. The validate() functions are called. These hooks perform a sanity check on the parameters
  2. Packer configuration and run
    1. The distribution kickstart file is copied and the kickstart() hooks have the opportunity to customize it
    2. The distribution is installed in a VirtualBox VM using this kickstart files
    3. The files directories are copied in /tmp on the VM
    4. The provision() functions are run in the VM
    5. The cleanup() functions are run in the VM
    6. Packer will then shutdown and export the VM
  3. Image cleanup
    1. The generated image is unpacked and mounted on the host
    2. The image_cleanup() functions are called
    3. The image is unmounted
    4. The final package is created by the image_package() function, either from cloud_distr or from cloud

Join the discussion

Comments ( 1 )
  • Sean Gray Saturday, March 28, 2020
    Thanks Philippe, this is excellent!
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.