#!/bin/bash

set -x

# Purpose: Attach more than six SCSI disks to a *single* SCSI controller

# Create 10 raw disk images
function create_disk() {
    for i in `seq 1 10`; 
        do 
            qemu-img create -f raw $i.raw 1G; 
        done
}

# This is incrementing the 'unit' (a "2-digit unit number on a bus")
# attribute, for the 'address' sub-element, which describes where the
# device is placed on the virtual bus presented to the guest.
# http://libvirt.org/formatdomain.html#elementsAddress
j=1

# Construct a guest XML by attaching 10 disks to a single Virtio SCSI
# controller by manually incrementing the 'unit' number

function attach_device() {
    for i in {b..k}; do
        j=$(expr $j + 1)
        cat << EOF > disk-$i.xml
<disk type='file' device='disk'>
  <source file='/export/vmimages/$j.raw'/>
  <target dev='sd$i' bus='scsi'/>
  <address type='drive' controller='0' model='virtio-scsi' bus='0' target='0' unit='$j'/>
</disk>
EOF

        # Attach the device; it takes effect on next boot
        sudo virsh attach-device cvm1 disk-$i.xml  --config

    done
}


# Invoke the functions
create_disk
attach_device
