Image Builder previously known as Image Generator is a vital tool in the tech industry. System admins around the world use it to create a copy or exact image of a virtual system such as an operating system, container or virtual machine with all the configs. This can serve as a base from which developers are able to build or customize their systems easily as the generated images can be used to spin systems easily in other environments.

This tool allows the creation of custom OS images based on the upstream project Weldr. You can use it from the command line or Cockpit plugin to get started quickly. The Os creation is a very simple process and from it, you can create images for most of the major platforms such as Libvirt/KVM (RHV or general Linux virtualization), VMware, OpenStack, AWS and Azure. The created images can also be deployed from Satellite.

The cool benefits associated with Image Builder are:

  • Simpler to secure: By creating custom images, you only use essential components which largely reduces the exposure to security vulnerabilities.
  • Improved IT productivity: It makes the process of building, maintaining and deploying secure and easy. It eliminates the need to write and maintain automation code as it provides compliant images.
  • Built-in Validation Support: With Image Builder, you can easily validate your images with AWS-provided tests or other providers and your own tests before using them in production.
  • Centralized Policy Enforcement: It allows version control for easier revision management. You can also integrate it with your provider such as AWS Resource Access Manager and AWS Organizations to enable the sharing of automation scripts, recipes, and images across AWS accounts

The below steps will help you understand how to create Custom OS images for KVM, OpenStack, VMware and AWS using Image Builder.

How to Install Image Builder

The good thing is that Image Builder exists in the base repository of RHel-based distributions and can be installed as shown.

sudo dnf install -y osbuild-composer composer-cli vim lorax-composer bash-completion

To install Image Builder Cockpit use the command:

sudo dnf install -y cockpit-composer

Allow Cockpit through the firewall, use:

sudo firewall-cmd --add-service=cockpit && sudo firewall-cmd --runtime-to-permanent

Start and enable the services using:

##Image Builder
sudo systemctl enable --now osbuild-composer.socket

##Cockpit
sudo systemctl enable --now cockpit.socket

Now proceed and use Image Builder as desired.

Create Images using Image Builder

You are now set to use Image Builder to create the images for KVM, OpenStack, VMware and AWS. There are two ways of achieving that, these are:

  • Using Image Builder CLI
  • Using Cockpit Web

Option I – Create Images using Image Builder CLI

The CLI, composer-clican be used to create, list, examine and delete blueprints. You can also use it to build, list, delete and download the images as desired.

There are several commands used. Some of them include:

  • blueprints list: List all available blueprints
  • blueprints show blueprint-name: Show a blueprint contents in the TOML format
  • blueprints save blueprint-name: Save (export) blueprint contents in the toml format into a file blueprint-name.toml
  • blueprints delete blueprint-name: Remove a blueprint
  • blueprints push blueprint-name: Push (import) a blueprint file in the TOML format into Image Builder
  • start blueprint-name image-type: Start a compose
  • compose list: List all composes
  • compose status: List all composes and their status
  • compose cancel compose-uuid: Cancel a running compose
  • compose delete compose-uuid: Delete a finished compose
  • compose info compose-uuid: Show detailed information about a compose
  • compose image compose-uuid: Download the image file of a compose

To understand better, you can use the commands:

man composer-cli
sudo composer-cli help

So we will put some of these commands into real practice with examples.

Creating an image blueprint

An image blueprint includes all that the image needs to run. We will start by creating a simple blueprint file. This is a TOML file with all the required info:

$ vim test-blueprint.toml
name = "rhel-8-base"
description = "my RedHat 8 image blueprint"
version = "0.0.1"
modules = []
groups = []

This is a minimal image to which you can add more and more variables as desired. You can add desired packages to be installed.

[[packages]]
name = "bash-completion"
version = "*"

[[packages]]
name = "tmux"
version = "*"

[[packages]]
name = "git"
version = "*"

[[packages]]
name = "tree"
version = "*"

[[packages]]
name = "bash-completion"
version = "*"

This block is repeated for all the packages you need to install. You can stick to a specific version or use the asterisk to use the latest version.

You can also add users and set customizations for them. For example, when creating a root user with a password, shell and groups.

[[customizations.user]]
name = "testuser"
description = " Test User"
password = "$6$cH09Nvo8oR2j2QOY$Rqb/1pMorgPXZBmo9X.LuiYx1Aze7Ax1qxJkcnqll3ahw1ghyOyrlJ.BjNB7b3wwaQ.QJgky/8ie7ql73l6XY/"
key = "your-ssh-pub-key"
home = "/home/testuser/"
shell = "/usr/bin/bash"
groups = ["users", "wheel"]

Remember the user password needs to be hashed. To achieve that, you can generate the hash using this command:

python3 -c 'import crypt,getpass;pw=getpass.getpass();print(crypt.crypt(pw) if (pw==getpass.getpass("Confirm: ")) else exit())'

You can also add the kernel customizations. For example:

[customizations.kernel]
append = "net.ifnames=0"

You can see a more detailed blueprint file by checking the available example on GitHub.

The next thing after creating the file is pushing it to the Image Builder. To do that, use the command:

sudo composer-cli blueprints push test-blueprint.toml

View if it has been pushed.

$ composer-cli blueprints list
testing-image

Create the Image

Once the blueprint has been created and uploaded, you can generate images by running the commands below.

Start the service and give it the output format for example QCOW, AMI, VMDK, etc.

Now get the list of the supported image types:

$ sudo composer-cli compose types
ami
edge-commit
edge-container
edge-installer
edge-raw-image
edge-simplified-installer
gce
image-installer
minimal-raw
oci
openstack
ova
qcow2
tar
vhd
vmdk
wsl

For this guide, we will use QCOW, which can be used for KVM:

sudo composer-cli compose start <blueprint-name> qcow2

The command will create images with minimal sizes. If you need a larger disk space, you can pass the –size flag and a size in GB.

Check the status

$ sudo composer-cli compose status
ID                                     Status     Time                       Blueprint         Version   Type               Size
22449038-9b3c-4972-a3b6-2c349356befd   RUNNING    Wed Nov 29 21:36:15 2023   testing-image     0.0.2     qcow2

Using the Image

Once complete, the status will change to FINISHED.

Custom OS images for KVM OpenStack VMware and AWS using Image Builder 16

You now download it and use it to build a VM.

sudo composer-cli compose image image-uuid

Replace the image UUID correctly The Image UUID is located at the beginning of the compose status command output. For example:

$ sudo composer-cli compose image 66e3fcce-b0ce-4611-b31a-daeb4e379b00
66e3fcce-b0ce-4611-b31a-daeb4e379b00-disk.qcow2

Once downloaded, you can copy the image to the appropriate repository, rename and use it as desired.

For example, starting a KVM machine using the images, issue:

sudo qemu-kvm --name test-image -m 1024 -hda ./UUID-disk.qcow2

Option II – Create Images using Image Builder Cockpit

For those uncomfortable with the CLI, you can use the Image Builder Cockpit to create images. Once installed, Cockpit will be running on localhost:9090. Open this URL on the browser and navigate to Tools>Applications>Image Builder

Custom OS images for KVM OpenStack VMware and AWS using Image Builder

Once here, you can create a new blueprint and give it a description.

Custom OS images for KVM OpenStack VMware and AWS using Image Builder 1

Proceed and add the desired packages to be installed by searching them under Available Components then use the > button to add them.

Custom OS images for KVM OpenStack VMware and AWS using Image Builder 2

Once the desired packages have been added, select commit to proceed. Now you can make additional customizations to the images such as adding users, groups etc.

Custom OS images for KVM OpenStack VMware and AWS using Image Builder 3

You can customize the file system

Custom OS images for KVM OpenStack VMware and AWS using Image Builder 4

Set the services to be enabled if need be.

Custom OS images for KVM OpenStack VMware and AWS using Image Builder 6

Customize the firewall:

Custom OS images for KVM OpenStack VMware and AWS using Image Builder 7

Add users the set password:

Custom OS images for KVM OpenStack VMware and AWS using Image Builder 8

Add groups:

Custom OS images for KVM OpenStack VMware and AWS using Image Builder 9

Add SSH keys:

Custom OS images for KVM OpenStack VMware and AWS using Image Builder 10

Set the hostname and the install partition.

Custom OS images for KVM OpenStack VMware and AWS using Image Builder 11

You will then have an overview all that has been configured:

Custom OS images for KVM OpenStack VMware and AWS using Image Builder 12

Finally, you can create the image.

In the image creation dialogue, you can set the size of the disk for the VM. The good thing is that this image will be thin provisioned and won’t be the same size as the OS disk size.

Custom OS images for KVM OpenStack VMware and AWS using Image Builder 13

Once done, click Create Image.

Custom OS images for KVM OpenStack VMware and AWS using Image Builder 14

Watch the progress of the process or view the created images in the images tab.

Once created, you can download the image as shown.

Custom OS images for KVM OpenStack VMware and AWS using Image Builder 15

You can now rename and use the created image to deploy machines or containers. A simple demonstration for QEMU/KVM, use:

sudo qemu-kvm --name test-image -m 1024 -hda ./UUID-disk.qcow2

Verdict

That marks the end of this guide on how to create custom OS images for KVM, OpenStack, VMware and AWS using Image Builder. Now you can simplify VM/container deployment tasks using the created images. I hope this was informative.

Related content:

LEAVE A REPLY

Please enter your comment!
Please enter your name here