This is a only version

Found at: raymii.org:70/KVM_add_disk_image_or_swap_image_to_virtual_machine_with_virsh.txt

This is a text-only version of the following page on https://raymii.org:
Title       : 	KVM add disk image or swap image to virtual machine with virsh
Author      : 	Remy van Elst
Date        : 	23-02-2014
URL         : 	https://raymii.org/s/tutorials/KVM_add_disk_image_or_swap_image_to_virtual_machine_with_virsh.html
Format      : 	Markdown/HTML

This tutorial shows you how to create and add a disk image to a KVM vm using
virsh. This is useful when you for example want to expand the disk space of your
virtual machine when it is using LVM, or if you want to add a swap disk to a
virtual machine. Note that you can also create a swap file instead of a disk,
however, this is an example for adding the disk.

I'm developing an open source monitoring app called Leaf Node Monitoring, for windows, linux & android. Go check it out!

Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.

You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $100 credit for 60 days.

Read this tutorial to [learn how to set up a proper KVM hypervisor host:
https://raymii.org/s/tutorials/KVM _with_ bonding _and_ VLAN _tagging_ setup
_on_ Ubuntu_12.04.html][2]

### Requirements

  * Host running KVM and virsh

  * Virtual Machine to add disk to

This was tested on a KVM hypervisor host running Ubuntu 12.04 LTS and a Ubuntu
13.10 virtual machine. The KVM hypervisor uses virsh for management.

The example vm is named `example-vm` in virsh (domain).

### Create and attach the disk image

Execute these steps on the KVM hypervisor host.

cd to the folder where you store your disk images:

    cd /var/lib/libvirt/images/

Create the new disk image:

    qemu-img create -f raw example-vm-swap.img 1G

We use `qemu-img` to `create` a new `raw` disk image with a size of 1 GB.

Attach the disk to the example virtual machine using virsh:

    virsh attach-disk example-vm --source /var/lib/libvirt/images/example-vm-swap.img --target vdb --persistent

We use `virsh` to attach the disk image `/var/lib/libvirt/images/example-vm-
swap` as a `virtio` (`/dev/vdb`) disk to the domain (vm) `example-vm`. The
`--persistent` option updates the domain xml file with an element for the newly
attached disk.

Note that if you already have a `/dev/vdb` disk you need to change `vdb` to a
free device like `vdc` or `vdd`.

### Formatting the disk

Execute these steps in your virtual machine.

Reboot it so that the kernel sees the new disk:


Partition the drive with `cfdisk`. For our example we use filesystem type 82
(linux/linux swap):

    cfdisk /dev/vdb

Format the disk as swap:

    mkswap /dev/vdb1

Or format it as ext4:

    mkfs.ext4 /dev/vdb1

Make the swap active:

    swapon /dev/vdb1

Or mount the partition:

    mkdir /mnt/new-disk
    mount /dev/vdb1 /mnt/new-disk

Add to /etc/fstab for reboot persistence:

    /dev/vdb1   swap            swap    defaults    0 0

Or for the ext4 disk:

    /dev/vdb1   /mnt/new-disk   ext4    defaults    0 0

That's it. You've now created, attached, formatted and mounted a new disk in
your VM.

### Sources

  * [qemu-img man page][3]
  * [virsh-attach doc page][4]

   [1]: https://www.digitalocean.com/?refcode=7435ae6b8212
   [2]: https://raymii.org/s/tutorials/KVM_with_bonding_and_VLAN_tagging_setup_on_Ubuntu_12.04.html
   [3]: http://linux.die.net/man/1/qemu-img
   [4]: http://builder.virt-tools.org/artifacts/libvirt-virshcmdref/html/sect-attach-disk.html


All the text on this website is free as in freedom unless stated otherwise. 
This means you can use it in any way you want, you can copy it, change it 
the way you like and republish it, as long as you release the (modified) 
content under the same license to give others the same freedoms you've got 
and place my name and a link to this site with the article as source.

This site uses Google Analytics for statistics and Google Adwords for 
advertisements. You are tracked and Google knows everything about you. 
Use an adblocker like ublock-origin if you don't want it.

All the code on this website is licensed under the GNU GPL v3 license 
unless already licensed under a license which does not allows this form 
of licensing or if another license is stated on that page / in that software:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see .

Just to be clear, the information on this website is for meant for educational 
purposes and you use it at your own risk. I do not take responsibility if you 
screw something up. Use common sense, do not 'rm -rf /' as root for example. 
If you have any questions then do not hesitate to contact me.

See https://raymii.org/s/static/About.html for details.