• Edit
  • Delete

libguestfs-tools Virtual Machine Image Tools

http://libguestfs.org/guestfish.1.html

http://libguestfs.org/guestfs-recipes.1.html

apt-get install libguestfs-tools

Image Information

Partition information

  • virt-filesystems -lh --extra --uuid -a example.img
    • Name       Type        VFS      Label  Size   Parent
      /dev/sda1  filesystem  ext4     -      24G    -
      /dev/sda2  filesystem  unknown  -      1.0K   -
      /dev/sda5  filesystem  swap     -      1022M  -
  • virt-filesystems --all --long --uuid -h -a example.img
    • Even more information :-)

Disk Usage

  • virt-df -h -a example.img
    • Filesystem                         Size       Used  Available  Use%
      example.img:/dev/sda1               23G        14G       8.6G   59%

Make image sparse / smaller

Note: also works for a already sparse image to make it even smaller

  • mkdir /srv/tmp
    • Create larger tmp area
  • virt-sparsify --temp=/srv/tmp/ example.img example.sparse.img
    • Copys the image to a sparse image
    • On 16.04: virt-sparsify --tmp /srv/tmp/ example.img example.sparse.img
  • ls -lahs
    •  98G -rw------- 1 root root 108G Oct 28 19:58 example.img
       49G -rw------- 1 root root 108G Nov 26 13:10 example.sparse.img
  • virt-sparsify --in-place example.img
    • Sparsifys the image in place (overwriting it)

make small and convert to compressed qcow2 for backup:

  • fsck with guestfish (see below)
  • TMPDIR=/var/lib/libvirt/images/tmp virt-sparsify -v --check-tmpdir fail --compress --convert qcow2 example.com.img example.com.minified.qcow2

Remove partitions

  • virt-filesystems --partitions --long -a  example.img
    • Name       Type       MBR  Size         Parent
      /dev/sda1  partition  83   25768755200  /dev/sda
      /dev/sda2  partition  05   1024         /dev/sda #locical base partition
      /dev/sda5  partition  82   1071644672   /dev/sda
  • truncate -s 26G example.withoutswap.img
  • virt-resize --no-extra-partition --delete /dev/sda2 bigfish.ull.at.sparse.img example.withoutswap.img

Shrink Image

  • guestfish -a example.img
    • run
    • list-filesystems
      • /dev/sda1: ext4
        /dev/sda2: unknown
        /dev/sda5: swap
    • e2fsck-f /dev/sda1
    • resize2fs-size /dev/sda1 16G
    • e2fsck-f /dev/sda1
    • quit
  • truncate -s 17G example.shrinked.img
    • Prepare empty file
  • virt-resize --shrink /dev/sda1 example.img example.shrinked.img
    • virt-resize: error: libguestfs error: resize2fs: e2fsck 1.42.13
      (17-May-2015)
      The filesystem size (according to the superblock) is 4194304 blocks
      The physical size of the device is 4194256 blocks
      Either the superblock or the partition table is likely to be corrupt!
  • try again with removing swap
  • truncate -s 26G example.withoutswap.img
  • virt-resize --no-extra-partition --delete /dev/sda2 example.img example.withoutswap.img
  • virt-resize --shrink /dev/sda1 example.withoutswap.img example.shrinked.img
  • virt-filesystems --partitions --long -a bigfish.ull.at.shrinked.img
    • Name       Type       MBR  Size         Parent
      /dev/sda1  partition  83   18251382784  /dev/sda

Expand/grow partition in image

With removal of swap partition, which is added later on in the vm

  • ls -lhs example.img
    • 48G -rw------- 1 syslog uuidd 100G Oct 28 19:58 
  • virt-filesystems --partitions --long -a  example.img
    • Name       Type       MBR  Size          Parent
      /dev/sda1  partition  83   106300440576  /dev/sda
      /dev/sda2  partition  05   1024          /dev/sda
      /dev/sda5  partition  82   1070596096    /dev/sda
      
  • truncate -s 150G example.withoutswap.img
  • ls -l example.withoutswap.img
    • -rw-r--r-- 1 root root 161061273600 Nov 30 11:34
  • We want to leave space for a small swap partition of 2GB
  • echo "(161061273600 - 106300440576 - (2 * 1073741824)) / 1024 / 1024" | bc
    • Total img size - existing partition size - space for new swap
    • 50176
  • virt-resize --no-extra-partition --delete /dev/sda2 --resize /dev/sda1=+50176M example.img example.withoutswap.img
  • ls -lahs example.withoutswap.img
    • 47G -rw-r--r-- 1 root   root  150G Nov 30 12:00
  • virt-filesystems --all --long --uuid -h -a example.withoutswap.img
    • Name      Type       VFS  Label MBR Size Parent   UUID
      /dev/sda1 filesystem ext4 -     -   148G -        5245bfb5-23a2-43f8-91d9-37adbc49001b
      /dev/sda1 partition  -    -     83  148G /dev/sda -
      /dev/sda  device     -    -     -   150G -        -

 Finaly boot the vm, add the swap partition and update /etc/fstab