virt tools

The libguestfs project has a number of other useful tools, including:

  • virt-edit for editing a file inside of an image.
  • virt-df for displaying free space inside of an image.
  • virt-resize for resizing an image.
  • virt-sysprep for preparing an image for distribution (for example, delete SSH host keys, remove MAC address info, or remove user accounts).
  • virt-sparsify for making an image sparse.
  • virt-p2v for converting a physical machine to an image that runs on KVM.
  • virt-v2v for converting Xen and VMware images to KVM images.

Modify a single file inside of an image

This example shows how to use virt-edit to modify a file. The command can take either a filename as an argument with the -a flag, or a domain name as an argument with the -d flag. The following examples shows how to use this to modify the /etc/shadow file in instance with libvirt domain name instance-000000e1 that is currently running:

# virsh shutdown instance-000000e1
# virt-edit -d instance-000000e1 /etc/ssh/sshd_config
# virsh start instance-000000e1

Resize an image

Here is an example of how to use virt-resize to resize an image. Assume we have a 16 GB Windows image in qcow2 format that we want to resize to 50 GB.

  1. First, we use virt-filesystems to identify the partitions:
    # virt-filesystems --long --parts --blkdevs -h -a /data/images/win2012.qcow2
    Name       Type       MBR  Size  Parent
    /dev/sda1  partition  07   350M  /dev/sda
    /dev/sda2  partition  07   16G   /dev/sda
    /dev/sda   device     -    16G   -
    
  2. In this case, it is the /dev/sda2 partition that we want to resize. We create a new qcow2 image and use the virt-resize command to write a resized copy of the original into the new image:
    # qemu-img create -f qcow2 /data/images/win2012-50gb.qcow2 50G
    # virt-resize --expand /dev/sda2 /data/images/win2012.qcow2 \
      /data/images/win2012-50gb.qcow2
    Examining /data/images/win2012.qcow2 ...
    **********
    
    Summary of changes:
    
    /dev/sda1: This partition will be left alone.
    
    /dev/sda2: This partition will be resized from 15.7G to 49.7G.  The
        filesystem ntfs on /dev/sda2 will be expanded using the
        'ntfsresize' method.
    
    **********
    Setting up initial partition table on /data/images/win2012-50gb.qcow2 ...
    Copying /dev/sda1 ...
     100% [                                                                 ] 00:00
    Copying /dev/sda2 ...
     100% [                                                                 ] 00:00
    Expanding /dev/sda2 using the 'ntfsresize' method ...
    
    Resize operation completed with no errors. Before deleting the old
    disk, carefully check that the resized disk boots and works correctly.

     

 

Modify a virtual machine image with guestfish

Guestfish is a shell and command-line tool for examining and modifying virtual machine filesystems. It uses libguestfs and exposes all of the functionality of the guestfs API.

Sometimes you must modify a virtual machine image to remove any traces of the MAC address that was assigned to the virtual network interface card when the image was first created. This is because the MAC address is different when the virtual machine images boots. This example shows how to use the guestfish to remove references to the old MAC address by deleting the/etc/udev/rules.d/70-persistent-net.rules file and removing the HWADDR line from the /etc/sysconfig/network-scripts/ifcfg-eth0 file.

Assume that you have a CentOS qcow2 image called centos63_desktop.img. Mount the image in read-write mode as root, as follows:

# guestfish --rw -a centos.img

Welcome to guestfish, the libguestfs filesystem interactive shell for
editing virtual machine filesystems.

Type: 'help' for help on commands
'man' to read the manual
'quit' to quit the shell

><fs>

This starts a guestfish session.

Notethe guestfish prompt looks like a fish: ><fs>.

We must first use the run command at the guestfish prompt before we can do anything else. This will launch a virtual machine, which will be used to perform all of the file manipulations.

><fs> run
  1. We can now view the file systems in the image using the list-filesystems command:
    ><fs> list-filesystems
    /dev/vda1: ext4
    /dev/vg_centosbase/lv_root: ext4
    /dev/vg_centosbase/lv_swap: swap
    
  2. We need to mount the logical volume that contains the root partition:
    ><fs> mount /dev/vg_centosbase/lv_root /
    
  3. Next, we want to delete a file. We can use the rm guestfish command, which works the same way it does in a traditional shell.
    ><fs> rm /etc/udev/rules.d/70-persistent-net.rules
    
  4. We want to edit the ifcfg-eth0 file to remove the HWADDR line. The edit command will copy the file to the host, invoke your editor, and then copy the file back.
    ><fs> edit /etc/sysconfig/network-scripts/ifcfg-eth0
    
  5. If you want to modify this image to load the 8021q kernel at boot time, you must create an executable script in the/etc/sysconfig/modules/ directory. You can use the touch guestfish command to create an empty file, the edit command to edit it, and the chmod command to make it executable.
    ><fs> touch /etc/sysconfig/modules/8021q.modules
    ><fs> edit /etc/sysconfig/modules/8021q.modules
    
  6. We add the following line to the file and save it:
    modprobe 8021q
    
  7. Then we set to executable:
    ><fs> chmod 0755 /etc/sysconfig/modules/8021q.modules
    
  8. We are done, so we can exit using the exit command:
    ><fs> exit
    

Example 2: Add passwordless sudo to a user:

guestfish --rw -a centos.img
><fs> run
><fs> list-filesystems
/dev/sda1: ext4
><fs> mount /dev/sda1 /
><fs> touch /etc/sudoers.d/user1-sudo
><fs> vi /etc/sudoers.d/useer1-sudo
><fs> quit

This will add the sudoers file and enable passwordless sudo for user1