Grant McWilliams

Add a hard disk to a VM

Installing from an XCP/Xenserver template usually gives you one Virtual Disk to install the operating system on. Depending on your needs this disk may not be large enough. Following is a tutorial on how to add an additional disk to a virtual machine.

Terminology: 

 

Virtual Machine - A virtual machine is a computer that's virtualized and running on a hypervisor. In our case the hypervisor is Xen Cloud Platform/Xenserver. The Virtual Machine can be running any operating system.

Virtual Disk Image - Think of a Virtual Disk Image as a hard drive.

Storage Repository - A "box" storing Virtual Disk Images. Think of this as an external box storing virtual hard drives. The virtual hard drives are the Virtual Disk Images mentioned above.

Virtual Block Device - A Virtual Block Device connects a Virtual Disk Image to a Virtual Machine. In traditional computer terms you could think of it as the cable. 

The process for adding a hard drive to a real computer

  1. Insert the disk in the hard drive box
  2. Connect the cable to the hard drive box
  3. Insert the cable into the Computer

The process of adding a new Virtual Disk for a Virtual Machine is 

  1. Create a new Virtual Disk Image
  2. Create a new Virtual Block Device for it
  3. Connect the Virtual Block Device to the Virtual Machine

 

1. Get available free space

You will need to know how much free space is available on your Storage Repository.

[ root@cloud2 ~ ] xe sr-list
uuid ( RO)                : 36bf480a-5df9-4453-50f0-2bac4a86cb42
              name-label ( RW): localsr-cloud1
    name-description ( RW): 
                         host ( RO): cloud1.acs.edcc.edu
                        type ( RO): lvm
           content-type ( RO): user

Now that we have the Storage Repository's UUID number (36bf480a-5df9-4453-50f0-2bac4a86cb42) we can use xe sr-list again to give us the physical size and how much space is being utilized.

[ root@cloud2 ~ ] xe sr-list uuid=36bf480a-5df9-4453-50f0-2bac4a86cb42 \
params=physical-utilisation,physical-size
physical-utilisation ( RO) : 214752559104 physical-size ( RO): 991600574464

Quick math (991600574464 - 214752559104 = 776848015360) shows us that we have about 776 MB free.

2. Create the Virtual Disk Image

Now that we know the available space on the storage repository we can make a new Virtual Disk Image using xe vdi-create.

[ root@cloud2 ~ ] xe vdi-create sr-uuid=bd1ac90d-7c23-dc07-dfa3-edc9f1cd73c4 \
name-label=DATADISK type=user virtual-size=100GiB
ee9c5daa-392c-4a0d-a5c1-4ebb7caabd73

This command outputs the VDI's UUID. You can get information about any VDI using the xe vdi-list command.

[ root@cloud2 ~ ] xe vdi-list uuid=ee9c5daa-392c-4a0d-a5c1-4ebb7caabd73 
uuid ( RO)                : ee9c5daa-392c-4a0d-a5c1-4ebb7caabd73
          name-label ( RW): DATADISK
 name-description ( RW): 
                  sr-uuid ( RO): bd1ac90d-7c23-dc07-dfa3-edc9f1cd73c4
            virtual-size ( RO): 107374182400
               sharable ( RO): false
             read-only ( RO): false

The result of xe vm-list shows that the virtual size of the VDI is about 100 GB and it's name-label is DATADISK. To add this new disk to a VM I'll need to get the VM's UUID number by using xe vm-list.

[ root@cloud2 ~ ] xe vm-list name-label=CentOS6
     uuid ( RO)           : cefb9f88-0424-6701-5ba1-070490c69203
     name-label ( RW): CentOS6
      power-state ( RO): running

 

3. Get the available Virtual Block Device numbers

We will also need to know which Virtual Block Device numbers are available. We can use the xe vm-param-get command for this.

[ root@cloud2 ~ ] xe vm-param-get uuid=cefb9f88-0424-6701-5ba1-070490c69203 \ 
param-name=allowed-VBD-devices
7; 8; 9; 10; 11; 12; 13; 14; 15

In summary:

  1. VDI UUID is ee9c5daa-392c-4a0d-a5c1-4ebb7caabd73
  2. VM UUID is cefb9f88-0424-6701-5ba1-070490c69203
  3. Available VBD numbers are 7, 8, 9, 10, 11, 12, 13, 14 and 15

 

4. Create the Virtual Block Device

Create the Virtual Block Device (VBD) using the xe vbd-create command and the VM UUID, VDI UUID and the first available VBD number.

[ root@cloud2 ~ ] xe vbd-create device=7 vm-uuid=cefb9f88-0424-6701-5ba1-070490c69203 \ 
vdi-uuid=ee9c5daa-392c-4a0d-a5c1-4ebb7caabd73 bootable=false mode=RW type=Disk
333ab620-3ee1-0420-d31a-217e4ef1df45

I created Virtual Block Device 7 (device=7). Using device=0 would have given me a /dev/dev/xvda which I already have. The xe-param-get command showed my first available Virtual Block Device number was 7. Notice that we associated the Virtual Disk Image (VDI) to the Virtual Machine (VM) by using a Virtual Block Device (VBD).

5. Plug in the disk to the VM

The VM won't see the disk yet as it hasn't been "plugged in". We can do this by either rebooting the VM or using the xe vbd-plug command. Let's plug the VBD into the running VM.

[ root@cloud2 ~ ]  xe vbd-plug uuid=333ab620-3ee1-0420-d31a-217e4ef1df45

6. Verify that it worked

Log into the VM via ssh or xenconsole and see if the disk appeared by catting /proc/partitions.

[root@Centos6 ~]# cat /proc/partitions
major minor  #blocks  name

 202        0    8388608 xvda
 202        1     102400 xvda1
 202        2    8285184 xvda2
 253        0    7733248 dm-0
 253        1     524288 dm-1
 202       16  104857600 xvdb