Skip to content

Create images

raw

qemu-img create -f raw image.raw 20G

qcow2

qemu-img create -f qcow2 image.qcow2 20G

Modify partitions on raw partitions

fdisk image.raw

Mount

raw images

Information about the partitions

Useful for size in sectors and start sector.

sudo fdisk -lu windows10.raw
----------------------------
Sector size (logical/physical): 512 bytes / 512 bytes

Device           Start       End   Sectors  Size Type
windows10.raw1    2048   1023999   1021952  499M Windows recovery environ
windows10.raw2 1024000   1228799    204800  100M EFI System
windows10.raw3 1228800   1261567     32768   16M Microsoft reserved
windows10.raw4 1261568 125829086 124567519 19.4G Microsoft basic data

Mount

# Partition 1
mount -o loop,offset=$((2048*512)) windows10.raw /mnt/hdisk/1/
# Partition 2
mount -o loop,offset=$((1024000*512)) windows10.raw /mnt/hdisk/2/
# Partition 3
mount -o loop,offset=$((1228800*512)) windows10.raw /mnt/hdisk/3/
# Partition 4
mount -o loop,offset=$((1261568*512)) windows10.raw /mnt/hdisk/4/

Mount as loop device

sudo losetup -P -f --show windows10.raw

List all used devices

sudo losetup -a

List mounted devices

sudo losetup -l

Dettach all

sudo losetup -D

qcow2

Install libguestfs-tools. libguestfs in Arch.

Find the partitions in the image

kpartx -av /path/to/image.img

Mount the partition

sudo guestmount -a /path/to/image.qcow2 -m /dev/sda1 /mnt

Shrink an image file

  1. Mount file
  2. Write zeros
  3. Convert

Install libguestfs-tools.

Find the partitions in the image

Mount the partition

sudo guestmount -a /path/to/image.qcow2 -m /dev/sda1 /mnt

Write zeros to it

dd if=/dev/zero of=/mnt/zero-file

Convert

mv /path/to/image.qcow2 /path/to/image.qcow2.original
qemu-img convert -O qcow2 /path/to/image.qcow2.original /path/to/image.qcow2

Grow an image

qemu-img resize /path/to/image.qcow2 +100G

Convert

Convert VDI to qcow2

qemu-img convert -f vdi -O qcow2 ubuntu.vdi ubuntu.qcow2

Convert qcow2 to VDI

qemu-img convert -f qcow2 -O vdi ubuntu.qcow2 ubuntu.vdi