How do I import a disk image into libvirt?

Can someone post the proper syntax? I have tried using the chmod command , but I am doing something incorrectly. I am just trying to move a centos iso file to /images to set up a test vm


You're not really "supposed" to do it that way, since libvirt takes care of its own volume pools. Use:

size=$(stat -Lc%s centos.iso)
virsh vol-create-as default centos $size --format raw
virsh vol-upload --pool default centos centos.iso

This will create a virsh volume called centos in the default pool with the contents of centos.iso.

This way, libvirt takes care of all the required permissions and ownership itself.


I assume two things: /var/lib/libvirt/images/ is owned by root and you are not root. That's fine but you have to respect that's what the problem is.

It would probably make most sense to just copy the file over as root, using sudo:

sudo cp my-image /var/lib/libvirt/images/

Alternatively, you can take over /var/lib/libvirt/images/:

sudo chown $USER /var/lib/libvirt/images/

Or (least good) you could just make it globally writable:

sudo chmod 755 $USER /var/lib/libvirt/images/

I'd follow the logic that it's root for a reason but if you're just playing around and this is never going to be a production machine, you probably don't need to be that careful.


I know it's an old post but I wanted to share what I did.

In my case, I needed to import a qcow2 image into a volume. I've used jq, qemu-img and and bash:

# export IMGSIZE=$(qemu-img info --output json /data/myfile.qcow2 | jq -r .[\"virtual-size\"])
# export IMGFMT=$(qemu-img info --output json /data/myfile.qcow2 | jq -r .format)

List the pools and select the one you want:

# virsh pool-list --all
 Name                 State      Autostart
-------------------------------------------
 default              active     yes

# export IMGPOOL=default

Create the volume:

# virsh vol-create-as $IMGPOOL myvolume $IMGSIZE --format $IMGFMT
Vol myvolume created

And upload the image:

# virsh vol-upload --pool $IMGPOOL myvolume /data/myfile.qcow2

I don't know if you can pipe qemu-img to virsh, sort of like 'qemu-img dd ... | virsh vol-upload ...' but that would be convenient :-)

Thanks @Robie Basak for his post, I've used it to write the information above.


You can try doing it in nautilus just type

sudo -i nautilus

and after that copy the file as you would normally do.