Standard Cloud Image
The cloud image is a standard release for many of the Linux distributions. Thanks to cloud-init, we can uniformly provision and customize a VM quickly for different flavors of Linux in a standard way.
Some times, we need an image with more than 2GiB of disk, this document will describe how to create (extend) a new disk size, and how to set the cloud-init parameters.
This document is based on the default Ubuntu 22.04LTS cloud image that has 2GiB of disk and we would like to extend it to 8GiB.
Get the latest cloud image
Download the current version of 22.04 LTS:
curl -LO https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64.img
Modify the size of the cloud image
Checking the size of the image
qemu-img info ubuntu-22.04-server-cloudimg-amd64.img
The output is:
image: ubuntu-22.04-server-cloudimg-amd64.img
file format: qcow2
virtual size: 2.2 GiB (2361393152 bytes)
disk size: 641 MiB
cluster_size: 65536
Format specific information:
compat: 0.10
refcount bits: 16
Manipulate the size of the image/partition
You need the following library :
apt install libguestfs-tools
We can check the filesystem of the original image:
virt-filesystems --long -h --all -a ubuntu-22.04-server-cloudimg-amd64.img
Name Type VFS Label MBR Size Parent
/dev/sda1 filesystem ext4 cloudimg-rootfs - 2.1G -
/dev/sda14 filesystem unknown - - 4.0M -
/dev/sda15 filesystem vfat UEFI - 106M -
/dev/sda1 partition - - - 2.1G /dev/sda
/dev/sda14 partition - - - 4.0M /dev/sda
/dev/sda15 partition - - - 106M /dev/sda
/dev/sda device - - - 2.2G -
Extend the size of the image
In this exemple, we will extend the size from 2.2GiB to 8GiB, at first, create a new disk image:
qemu-img create -f qcow2 ubuntu-22.04.img 8G
Expand Filesystem
We have now to extend the partition to the new disk image, based on the original image:
virt-resize -expand /dev/sda1 ubuntu-22.04-server-cloudimg-amd64.img ubuntu-22.04.img
Let’s check the size of the new image:
virt-df -h -a ubuntu-22.04.img
virt-filesystems --long -h --all -a ubuntu-22.04.img
Sometimes the /dev/sda1
become /dev/sda3
, so the grub is pointing to the wrong partition.
We can correct the new image with this command:
virt-customize -a ubuntu-22.04.img --run-command 'grub-install /dev/sda'
Create the cloud-init-data
See cloud_init for more details.