#!/bin/bash
##
# Prepare udev SRs

set -eu

. "/opt/xensource/lib/storage-creation-utils.sh"
UPGRADE="false"
[ -r ${FIRSTBOOT_DATA_DIR}/host.conf ] && . ${FIRSTBOOT_DATA_DIR}/host.conf


start() {
    [ "$UPGRADE" = true ] && return 0
    found_cd=0
    found_block=0
    # Iterate through local SRs to see if we have the udev SRs already
    IFS=","
    for local_sr in $($XE pbd-list host=${INSTALLATION_UUID} params=sr-uuid --minimal); do
      for SR in $($XE sr-list type=udev sm-config:type=cd uuid=${local_sr} params=uuid --minimal); do
        found_cd=1
      done
      for SR in $($XE sr-list type=udev sm-config:type=block uuid=${local_sr} params=uuid --minimal); do
        found_block=1
      done
    done
    if [ ${found_block} == 0 ]; then
      sr_create "Removable storage" "" "udev" "disk" "local-hotplug-disk" "" \
          "sm-config:type=block" "device-config-location=/dev/xapi/block"
    fi
    if [ ${found_cd} == 0 ]; then 
      sr_create "DVD drives" "Physical DVD drives" "udev" "iso" \
          "local-hotplug-cd" "" \
          "sm-config:type=cd" "device-config-location=/dev/xapi/cd"
    fi
}

case $1 in
    start)  start ;;
esac

