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

Corey
4 min readJun 12, 2021

--

In general, creating a Windows image on OpenStack isn’t tough, but a tedious and time-consuming task, especially when it has numerous packages, applications, and configurations to apply. It’s a nightmare trust me, I’ve taken a whole day just to customize an image for clients. Besides, it’s hard to manage when we do it by hand, write down the procedures in a document is an obsolete way, it’s always better to scriptize the process. So, in this article, I will introduce an open-source tool and show you how I use it to automate the generation of Windows images.

Today’s subject is windows-openstack-imaging-tools, it’s created and maintained by Cloudbase. The core modules are written in PowerShell, and used in combination with Hyper-V, DISM, qemu-img, ntfszapfree, etc. This tool creates a Virtual Hard Disk(VHD) from an ISO, applies its content, and then starts a virtual machine on Hyper-V using the above VHD. A set of scripts will be triggered inside the VM to install drivers, updates, and the necessary configurations. After that, Sysprep generalize will be executed and the VM will be shut down. Waits until the VM is terminated, the tool shrinks the VHD and converts it to the desired format. In addition to VHD, it also supports RAW and QCOW2 format.

windows-openstack-imaging-tools works like this

Now you have a basic understanding of how it works, the following will show you how to use it. First of all, a Windows environment is needed to make use of this tool, and before going any further, please have a look at the prerequisite listed below:

● A Windows host with Hyper-V enabled
● PowerShell version 5 or later
● Windows Assessment and Deployment Kit (ADK)
● Git for Windows
● A Windows installation ISO

When all are prepared, follow the steps to begin building Windows images.

Step 1

To make sure the networking works in Hyper-V, go to Hyper-V manager > Actions > Virtual Switch Manager > New virtual network switch, create an External virtual switch named “ExtSwitch”, it should be connected to the physical network adapter you use for connectivity.

Step 2

Start a PowerShell terminal, clone the source code from GitHub.

git clone https://github.com/cloudbase/windows-openstack-imaging-tools.git

Step 3

Download virtio-win.iso and Cloudbase-init installer, refer to this tutorial to have cloudbase-init.conf and cloudbase-init-unattend.conf ready. Then put them to the root directory of this tool. And don’t forget to mount the Windows installation ISO to the host.

Step 4

In order to specify a correct edition, type the commands listed below to get the edition list from your ISO.

cd windows-openstack-imaging-tools
Import-Module .\WinImageBuilder.psm1
# the ISO is mounting on 'E' in my case
Get-WimFileImagesInfo -WimFilePath "E:\sources\install.wim"
edition list

The values mapped to the key “ImageName” are all editions it has, choose one you want, we will use it later.

Step 5

Create a PowerShell script called build.ps1, add the code listed below to it. I only post the most relevant command lines here, keep the rest to the default value.

set-executionpolicy remotesignedImport-Module .\WinImageBuilder.psm1
Import-Module .\Config.psm1
Import-Module .\UnattendResources\ini.psm1
# create a config.ini file
$ConfigFilePath = ".\config.ini"
New-WindowsImageConfig -ConfigFilePath $ConfigFilePath
# set the path of wim file (where you mount ISO to)
Set-IniFileValue -Path (Resolve-Path $ConfigFilePath)
-Section "DEFAULT"
-Key "wim_file_path"
-Value "E:\sources\install.wim"
# set windows edition
Set-IniFileValue -Path (Resolve-Path $ConfigFilePath)
-Section "DEFAULT"
-Key "image_name"
-Value "Windows Server 2012 R2 SERVERSTANDARD"
# set the destination of the generated image
Set-IniFileValue -Path (Resolve-Path $ConfigFilePath)
-Section "DEFAULT"
-Key "image_path"
-Value "C:\Image\win-server-2012.img"
# set disk format, it can be RAW, QCOW2 or VHD
Set-IniFileValue -Path (Resolve-Path $ConfigFilePath)
-Section "DEFAULT"
-Key "virtual_disk_format"
-Value "RAW"
# set image type, below are the pairs of options
# RAW -> MAAS
# QCOW2 -> KVM
# VHD -> HYPER-V
Set-IniFileValue -Path (Resolve-Path $ConfigFilePath)
-Section "DEFAULT"
-Key "image_type"
-Value "MAAS"
# set virtual switch
Set-IniFileValue -Path (Resolve-Path $ConfigFilePath)
-Section "VM"
-Key "external_switch"
-Value "ExtSwitch"
# start image generation
New-WindowsOnlineImage -ConfigFilePath $ConfigFilePath

Step 6

Execute build.ps1 to start the building. You would see information printed on the terminal if everything went smoothly. Don’t worry too much when you see error messages, it usually gives you clear information.

the tool is initializing the environment

When the terminal starts showing “Could not retrieve VM runtime logs”, don’t panic, it’s running the Online stage, which means the tool is customizing the system that running on Hyper-V. Use “Get-VM” in another terminal and you will see.

the tool creates a VM for the Online stage
the last part before the end

After a while, the VM is terminated and the last part is done subsequently. Now the image generation is finished, upload the whole new image to OpenStack Glance service, and enjoy it!

--

--

Corey
Corey

Written by Corey

Working & Traveling in Canada

Responses (1)