Skip to content

How to Create a Virtual Disk: VHDX on Windows, qcow2 and Disk Image Files on Linux

4 min read

Applies to: Windows 10/11 and Windows Server (Disk Management, diskpart, New-VHD) according to Microsoft’s documentation; Linux tested on Ubuntu 26.04 LTS with qemu-img 10.2.1 and e2fsprogs 1.47.2

A virtual disk is a file that behaves like a hard drive. On Windows, open Disk Management and choose Action > Create VHD, or run New-VHD in PowerShell. On Linux, create one with qemu-img create -f qcow2 disk.qcow2 20G for a virtual machine, or with truncate and mkfs for a disk image you mount on the host.

Which format?

  • VHDX: Windows and Hyper-V. Microsoft recommends it over the older VHD.
  • qcow2: KVM and QEMU.
  • VDI: VirtualBox. VMDK: VMware.
  • Raw image (.img): a plain byte-for-byte disk that any tool can read.

Each can be fixed (the full size is reserved at once) or dynamic (the file grows as data is written).

Windows: Disk Management

  1. Open Disk Management as administrator (search for it in the Start menu).
  2. Choose Action > Create VHD.
  3. Click Browse, pick a folder (not inside \Windows), enter a file name and set Save as type to Virtual Disk files (*.vhdx).
  4. Enter the Virtual hard disk size and choose Dynamically expanding or Fixed size, then click OK.

The new disk appears in the list like a physical disk. To use it, right-click the disk and choose Initialize Disk (GPT is the default partition style), then right-click the Unallocated space and choose New Simple Volume to format it and give it a drive letter. Later, Action > Attach VHD connects an existing file again, and right-click > Detach VHD disconnects it without deleting anything. According to Microsoft, you need to be in the Administrators or Backup Operators group, and a VHD must be at least 3 MB.

Windows: PowerShell or diskpart

With the Hyper-V PowerShell module, Microsoft’s example creates, attaches, initialises and formats a disk in one line:

New-VHD -Path C:\VHDs\Data.vhdx -Dynamic -SizeBytes 20GB | Mount-VHD -Passthru | Initialize-Disk -Passthru | New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume -FileSystem NTFS -Confirm:$false -Force

The file extension decides the format (.vhdx or .vhd). If New-VHD is not recognised, the Hyper-V module is not installed; use Disk Management or diskpart instead:

diskpart
create vdisk file="C:\VHDs\Data.vhd" maximum=20000 type=expandable
select vdisk file="C:\VHDs\Data.vhd"
attach vdisk

maximum is in megabytes. Without type=expandable, diskpart creates a fixed disk, which takes longer because the whole file is allocated at once. After attach vdisk, initialise and format it as above.

Linux: a disk for a virtual machine (qemu-img)

sudo apt install qemu-utils        # Debian, Ubuntu (qemu-img on AlmaLinux and Rocky Linux)
qemu-img create -f qcow2 vm-disk.qcow2 20G
Formatting 'vm-disk.qcow2', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=21474836480 lazy_refcounts=off refcount_bits=16

The same tool writes the other formats:

qemu-img create -f vhdx data.vhdx 10G                      # Hyper-V
qemu-img create -f vpc -o subformat=dynamic data.vhd 10G   # older VHD
qemu-img create -f vdi disk.vdi 10G                        # VirtualBox

All of them start small: the 20 GB qcow2 file used 196 KB on disk, the 10 GB VHDX 1.3 MB. Check a file with qemu-img info, and convert between formats with qemu-img convert (see convert a VMware disk for KVM). VirtualBox users can also run VBoxManage createmedium disk --filename=disk.vdi --size=10240 (size in MB).

Linux: a disk image file with a file system

To get a file you can format and mount on the host itself, create a raw image:

truncate -s 1G disk.img             # sparse: takes no space until used
fallocate -l 1G disk.img            # or reserve the full 1 GB now
mkfs.ext4 -L data disk.img
blkid disk.img
disk.img: LABEL="data" UUID="45779706-7e03-41e1-b175-09bf00016c92" BLOCK_SIZE="4096" TYPE="ext4"

ls -l shows a sparse file at its full size, while du -h disk.img shows the space really used: 0 right after truncate, and 33M in our test once the ext4 file system was on it. Mount it through a loop device with sudo mkdir -p /mnt/data && sudo mount -o loop disk.img /mnt/data; the details, including partitions inside an image, are in create a file system in a file and mount it as a loop device and partitions inside a loopback device.

Choosing a hypervisor to use these disks with? See KVM, QEMU and VirtualBox compared.

Official documentation: Manage virtual hard disks, New-VHD and qemu-img.

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Linux server management

hardening, patching, monitoring and automation for Ubuntu, Debian, RHEL and AlmaLinux.

Was this answer helpful?

Still stuck? Open a ticket