Skip to content

Latest commit

 

History

History
64 lines (47 loc) · 1.63 KB

10_USEFUL_TOOLS.md

File metadata and controls

64 lines (47 loc) · 1.63 KB

QEMU Disk utils/LibGuestFS handy commands

  1. Create a sparse (dynamically allocated) disk:

    # Yields a 100 GiB (not GB) disk.
    # `-f qcow2` is required, as the default is raw (which preallocates all the space).
    qemu-img create -f qcow2 "$DISK_IMAGE" 100G
  2. Create a diff disk:

    qemu-img create -f qcow2 -b "$DISK_IMAGE" "$DISK_IMAGE.diff"
  3. Create a compressed copy of a disk (can also be used to merge a diff image):

    # [-p]rogress, [-O]utput format, [-c]ompressed
    qemu-img convert -p -O qcow2 -c "$DISK_IMAGE" "$DISK_IMAGE.merged"
  4. Create a compressed copy without unallocated space (use --tmp, otherwise /tmp is used!!):

    virt-sparsify --compress --tmp $(dirname "$DISK_IMAGE") "$DISK_IMAGE" "$DISK_IMAGE.sparse.compressed"
  5. Import a directory in a disk:

    virt-copy-in -a "$DISK_IMAGE" /tmp/pizza /
  6. Mount a disk using qemu-nbd:

    PARTITION_NUMBER=4                           # 1-based
    modprobe nbd max_part=8
    qemu-nbd --connect=/dev/nbd0 "$DISK_IMAGE"   # wait a second after this
    partprobe /dev/nbd0
    mount "/dev/nbd0p"$PARTITION_NUMBER" /mnt
  7. and unmount:

    umount /mnt
    qemu-nbd --disconnect /dev/nbd0
    rmmod nbd
  8. Mounting a disk can also be used to shrink an image:

    # mount the disk (5.)
    dd -status=progress if=/dev/zero of=/mnt/ZEROFILE
    rm /mnt/ZEROFILE
    # now umount the disk (6.)
    # now compress the image (2.)

Previous: Sample IOMMU groups | Next: References