Copying files and directories to a virtual machine is typically done using command-line tools like rsync and scp, which operate over the SSH protocol. However, if you need a method that directly interacts with the guest’s disk image without requiring the VM to be running, virt-copy-in is the ideal solution.

🔥 TRENDING - Our #1 Seller

Mastering KVM Virtualization

From home labs to production clouds - master KVM, Terraform, Vagrant, and cloud automation. Build scalable virtual infrastructure that works whether you're learning at home or deploying enterprise solutions.

Only $10 $20
Get Instant Access →

virt-copy-in is a command-line utility included in the libguestfs suite, designed to streamline the process of copying files or directories from the local disk into a virtual machine’s disk image. Its key advantage is the ability to modify the contents of the guest filesystem without needing to boot the VM, making it an efficient and convenient option for working with offline VM disks.

Use case?

A practical use case for virt-copy-in is resolving issues with files that prevent a VM from booting. For instance, you can use the tool to copy a corrected configuration file or script directly into the VM’s disk image. Once the fix is in place, you can start the instance and verify that the issue has been resolved.

virt-copy-in with examples

This section demonstrates usage of virt-copy-inwith examples.

Command usage syntax:

  • Specified guest image disk path
virt-copy-in -a <disk-image> <local-files-or-directories> /destination
  • Specified domain
virt-copy-in -d <vm-name> <local-files-or-directories> /destination
Example 1: Copy a single file

Copy the file1.conf file into the /root directory of the VM:

sudo virt-copy-in -d <vm-name> file1.conf /root
Example 2: Copy multiple files

Copy multiple configuration files into the /etc directory of the guest:

sudo virt-copy-in -a /var/lib/libvirt/images/myvm.qcow2 \
config1.conf config2.conf /etc
Example 3: Copy a Directory Recursively

Copy an entire directory (wordpress) into the /var/www/html directory of the VM:

sudo virt-copy-in -d <vm-name> wordpress /var/www/html

LEAVE A REPLY

Please enter your comment!
Please enter your name here