How do I import a disk image into libvirt?

13,454

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

You can try doing it in nautilus just type

sudo -i nautilus

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

Share:
13,454

Related videos on Youtube

Fmoov
Author by

Fmoov

Updated on September 18, 2022

Comments

  • Fmoov
    Fmoov over 1 year

    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

  • Fmoov
    Fmoov almost 11 years
    First time I've used the forum . Excellent advice.
  • Fmoov
    Fmoov almost 11 years
    I probably go with the last command as I understand this one and it is a test environment.
  • Fmoov
    Fmoov almost 11 years
    Do I assume that nautilus is loaded?
  • Mihai
    Mihai almost 11 years
    nautilus is the default file manager for Ubuntu. I don't think i really understand your question.
  • Oli
    Oli almost 11 years
    @Rat2000 FYI
  • Mihai
    Mihai almost 11 years
    Oli yes, you are right gksudo is not instaled by default, so i changed the command to sudo -i, that should do the job.