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

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 Selling eBook

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

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

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Unlock the Right Solutions with Confidence

At CloudSpinx, we don’t just offer services - we deliver clarity, direction, and results. Whether you're navigating cloud adoption, scaling infrastructure, or solving DevOps challenges, our seasoned experts help you make smart, strategic decisions with total confidence. Let us turn complexity into opportunity and bring your vision to life.

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Post

When running Virtual Machines with KVM, there will likely come a time when you need to expand the size of […]

As a Linux user, you might think that just because Linux systems are pretty much more secure than Windows, there […]

If you’re rolling out multiple virtual machines, it makes sense to create a clean image that doesn’t contain sensitive information […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.