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.
Mastering KVM Virtualization - The Ultimate eBook
From home labs to production clouds - master KVM Host management, automating KVM administration using Terraform, Vagrant, and cloud automation. This eBook will enable you to build scalable virtual infrastructure that works whether you're learning at home or deploying enterprise solutions. Get your full copy today
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-in
with 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