Skip to content

Instantly share code, notes, and snippets.

@Sorecchione07435
Last active October 29, 2023 15:45
Show Gist options
  • Select an option

  • Save Sorecchione07435/56a69665597b598b03bb9fe699d8ec22 to your computer and use it in GitHub Desktop.

Select an option

Save Sorecchione07435/56a69665597b598b03bb9fe699d8ec22 to your computer and use it in GitHub Desktop.
How to convert cloud images to VHD to run on Hyper-V

How to convert cloud images to VHD to run on Hyper-V

If you followed the previous guide where I explained how to obtain a functional OpenStack distribution internally on Windows, and you would like to use Ubuntu, CentOS 7 etc. cloud images

In this guide we will explain how to convert virtual disks of cloud images downloaded from the Internet to VHD format to run instances on the Hyper-V Hypervisor

Create Network SMB Share

Obviously, a network share is required to access the image downloaded on your computer by the OpenStack controller

Create a network share in your Downloads folder with

New-SmbShare -Name Downloads -Path C:\Users\username\Downloads\

Now you will need to grant all read and write permissions on the newly created share

Grant-SmbShareAccess -Name Downloads -AccountName "Everyone" -AccessRight Full -Force

Set a password for the root user (Optional)

This part is only mandatory if you want to use the cloud image running on an instance

First of all, log in to your OpenStack controller

Install the libguestfs-tools package

sudo su
apt install libguestfs-tools

Or if you are on CentOS Stream:

dnf install libguestfs-tools

After you have installed this package

Mount your Downloads directory from SMB

mount //yourip/Downloads /mnt -o username=hostusername,password=hostpassword

Well now we can give the last command to run the tool and have the root password set within the cloud image

virt-customize -a /mnt/bionic-server-cloudimg-amd64.img --root-password password:<pass>

Obviously replace with a password of your choice

Virtual Disk Conversion

Well now it will not be possible to directly upload the image to Glance because the instances will not start, as the hypervisor does not support this QCOW2 disk format

The OpenStack Hyper-V Nova Compute driver includes qemu-img

Now go to your Downloads folder from Powershell

cd C:\Users\username\Downloads\

and now run qemu-img to convert the virtual disk in VHD format (it will take a few seconds)

This conversion is supported in these formats: QCOW2, RAW, VMDK

qemu-img.exe convert bionic-server-cloudimg-amd64.img -O vpc -o subformat=dynamic bionic-server-cloudimg-amd64.vhd

Finally from your OpenStack controller upload the image to Glance

source admin-openrc.sh
glance image-create --name "Ubuntu Bionic 18.04 Server (amd64)" --container-format bare --disk-format vhd --file /mnt/bionic-server-cloudimg-amd64.vhd --property os_type=linux --visibility public

After the image is uploaded, you will finally be able to launch instances and run Ubuntu, CentOS, Debian Server on Hyper-V

If you have set a password for root previously, you will be able to access it within the instance

Assign a password without editing the image virtual disk

NOTICE: If you have not changed the root password within the Cloud image, you will be able to access it another way

When you are launching an instance, go to the "Configuration" tab

And in the Customization Script field, paste this

#cloud-config
password: yourpassword
chpasswd: { expire: False }
ssh_pwauth: True

Replace yourpassword with a password of your choice

Well, when you launch the instance you will be able to log in with the default username 'ubuntu' or 'centos' with the password you entered

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment