How To

How to Copy files/dirs to VM disk image using virt-copy-in

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.

Original content from computingforgeeks.com - post 90770

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

Keep reading

Install VirtIO Drivers on Windows 11 and Server 2025: ISO and MSI KVM Install VirtIO Drivers on Windows 11 and Server 2025: ISO and MSI Install KVM and Virt-Manager on Arch Linux Virtualization Install KVM and Virt-Manager on Arch Linux Install KVM on Debian 13 / Debian 12: Complete Guide KVM Install KVM on Debian 13 / Debian 12: Complete Guide XNULinux: Run macOS 27 on Linux on Apple Silicon, Tested macos XNULinux: Run macOS 27 on Linux on Apple Silicon, Tested Lume macOS VM: Run macOS Guests for AI Agents on Apple Silicon macos Lume macOS VM: Run macOS Guests for AI Agents on Apple Silicon Run RHEL 10 and Fedora 45 / 44 on WSL in Windows 11 Tips & Tricks Run RHEL 10 and Fedora 45 / 44 on WSL in Windows 11

Leave a Comment

Press ESC to close