Skip to content

Instantly share code, notes, and snippets.

@Jeimerson
Forked from williamcaban/Export RHV to QCOW2.md
Created January 11, 2024 11:06
Show Gist options
  • Save Jeimerson/2f1c241ac5314385e7dc14386b397adb to your computer and use it in GitHub Desktop.
Save Jeimerson/2f1c241ac5314385e7dc14386b397adb to your computer and use it in GitHub Desktop.
Customizing and Expanding QCOW2 Images

Export RHV/Ovirt VM to qcow2 format

  1. Configure export domain in RHV or Ovirt

  2. Export the VM to the export domain. A new directory with the vdisk uuid will be created and the VM will be exported to it.

    • A sample export /exportfs/export_domain/<vdisk_uuid>/images/<disk_id>
  3. Mount the export domain ito a Linux mcahine and go into the <vdisk_uuid>/images

  4. In the folder there iwll be a file with a uuid representing the <disk_id> and a meta file

  5. Use qemu-img convert to convert to qcow2 format

qemu-img convert -O qcow2 <disk_id> vm-disk-name.qcow2

Customizing RHEL8.1 qcow2 image

Need following packages:

yum -y install libguestfs-tools
  • Copy template image
export VMDISK="registry.qcow2"

cp /root/rhel-8.1-x86_64-kvm.qcow2 /var/lib/libvirt/images/pool/${VMDISK}
chmod 644 /var/lib/libvirt/images/pool/${VMDISK}
chown qemu.qemu /var/lib/libvirt/images/pool/${VMDISK}
  • Run virt-sysprep with customiztions
export VMDISK="registry.qcow2"
export ROOTPASS="changeme"
export SUDOUSER="william"
export SSHKEYFILE="/root/.ssh/id_rsa.pub"

sudo virt-sysprep -a ${VMDISK} \
--root-password password:changeme \
--run-command 'useradd ${SUDOUSER}' \
--ssh-inject william:file:${SSHKEYFILE} \
--run-command 'sed -i s/cloud-user/${SUDOUSER}/g /etc/sudoers' \
--uninstall cloud-init \
--selinux-relabel 

Expanding a Guest XFS File System

Need the following packages:

yum -y install libguestfs-tools guestfish
  • Shutdown the VM
  • Create new empty disk with desired size
export VMDISK="registry.qcow2"

# Create new empty disk weith desired size
qemu-img create -f qcow2 new-${VMDISK} 150G

# list existing partitions
virt-filesystems --long --parts --blkdevs -h -a new-${VMDISK}
Name       Type       MBR  Size  Parent
/dev/sda1  partition  83   7.8G  /dev/sda

# use virt-reside expand the desired partition
virt-resize --expand /dev/sda1 ${VMDISK} new-${VMDISK}

# extending the xfs partition
guestfish --rw -a new-${VMDISK}

><fs> run
><fs> list-filesystems
/dev/sda1: xfs
><fs> mount /dev/sda1 /
><fs> xfs-growfs /
><fs> exit

# validate changes
virt-df -h registry-new.qcow2
Filesystem                                Size       Used  Available  Use%
registry-new.qcow2:/dev/sda1              200G       5.6G       194G    3%
  • replace new disk by old disk
export VMDISK="registry.qcow2"

mv ${VMDISK} ${VMDISK}-bak
mv new-${VMDISK} to ${VMDISK}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment