Automate the Process of Building Windows Images for OpenStack | Part 2

Corey
3 min readJul 18, 2021

As was mentioned in the previous article, we already know that windows-openstack-imaging-tools can do us a favor for automize image generation. You might still feel curious about how to assign more specific values to customize the image and specify the driver’s version, such as virtio-win or Cloudbase-init installer. This article will explain more details about its modules, and how I use them to apply my needs.

Here are three modules you need to know in the first place:

init.psm1: Provides functions to get/set/remove values for init file, e.g., Get-IniFileValue, Set-IniFileValue.
Config.psm1: Defines options that allow users to assign values they want, and provides functions to create/parse init file, e.g., New-WindowsImageConfig, Get-WindowsImageConfig.
WinImageBuilder.psm1: Provides functions to create virtual disks, apply images, create virtual machines, convert disk formats, e.g., New-WindowsOnlineImage, Add-VirtIODriversFromISO. It is the main module that handles all tasks for the Offline stage.

Before starting any manipulates, import them to the current session.

Import-Module .\UnattendResources\ini.psm1
Import-Module .\Config.psm1
Import-Module .\WinImageBuilder.psm1

How do values pass to the tool exactly?

Firstly, “Set-IniFileValue” sets the value for the key within config.ini file. Then, pass this config file as a parameter to the start command “New-WindowsOnlineImage”. Lastly, the start command invokes the function “Get-WindowsImageConfig” to parse the file and convert its content to a hash table. This table will be used from start to the finish of image generation, for retrieving the user-input values in different steps.

A picture is worth a thousand words.

Now, let’s find out what options you can use.

Have a glance at Config.psm1, all available options can be found in the function Get-AvailableConfigOptions, some are for images, others are for Hyper-V virtual machines, and still others are for assigning file paths. The following table includes the most commonly used parameters.

widely used keys and their descriptions
the section and the default value of each key

Note that the values of section “vm” will be applied to the Hyper-V virtual machine, which is used to generate images. It doesn’t mean the Openstack instance created by this image should have the same CPUs, RAM, and disk size as prescribed here. Openstack instance’s compute, memory, and storage capacity are defined by flavors, so it’s ok to give larger resources here to boost the build speed.

Can I add my own option?

Of course. The tool is open-sourced, go to fork the project and write code, it’s easy as walking in a park. Here is an example, I add a key to determine whether to activate the system via the KMS host computer or not. Since it’s not necessary for all cases, I set the default value as false.

function Get-AvailableConfigOptions {
return @(
......
......
@{ "Name" = "activate";
"GroupName" = "custom";
"DefaultValue" = $false;
"Description" = "Use KMS to activate Windows."}
)
}

Set it to true by using “Set-IniFileValue”.

Set-IniFileValue -Path (Resolve-Path $ConfigFilePath)
-Section "custom"
-Key "activate"
-Value $true

That’s it, the tool can read the self-defined value from config hash table during the image generation.

So far I’ve not defined what to do when the value “activate” is true, so it won’t change anything actually. Next part, we will dig into the core functions of Online stage, all customization related to image contents will be done there.

--

--