/*
|| Oracle 11gR2 Grid Infrastructure Listing
||
|| Contains code samples for configuration of Oracle 11gR2 Grid Infrastructure
|| as part of the Oracle 11gR2 demonstrations that are part of this article series.Listing
||
|| Author: Jim Czuprynski
||
|| Usage Notes:
|| These examples are provided to demonstrate various Oracle 11gR2 Grid Infrastructure
|| features, and they should be carefully proofread before being executed
|| against any existing Oracle database to avoid potential damage!
*/

/*   
|| Listing 1.1:
|| Using an LOSETUP script to assign logical partitions as virtual block devices 
|| during server startup
*/

#!/bin/sh
# chkconfig: 345 13 89
# description: Sets up logical partitions as linked loopback devices
#
# Include function library
. /etc/init.d/functions

PATH=/sbin:/bin:/usr/sbin:/usr/bin
RETVAL=0

start()
{
    # Allocate raw partitions as loopback devices
    losetup /dev/loop1  /dev/sdd5
    losetup /dev/loop2  /dev/sdd6
    losetup /dev/loop3  /dev/sdd7
    losetup /dev/loop4  /dev/sdd8
    losetup /dev/loop5  /dev/sdd9
    losetup /dev/loop6  /dev/sde5
    losetup /dev/loop7  /dev/sde6
    losetup /dev/loop8  /dev/sde7
    losetup /dev/loop9  /dev/sde8
    losetup /dev/loop10 /dev/sde9
    losetup /dev/loop11 /dev/sde10
    losetup /dev/loop12 /dev/sde11
    losetup /dev/loop13 /dev/sde12

    # Create symbolic links for loopback devices to make them appear
    # as Oracle VM virtual block devices (VBDs)
    ln -s /dev/loop1  /dev/xvdb
    ln -s /dev/loop2  /dev/xvdc
    ln -s /dev/loop3  /dev/xvdd
    ln -s /dev/loop4  /dev/xvde
    ln -s /dev/loop5  /dev/xvdf
    ln -s /dev/loop6  /dev/xvdg
    ln -s /dev/loop7  /dev/xvdh
    ln -s /dev/loop8  /dev/xvdi
    ln -s /dev/loop9  /dev/xvdj
    ln -s /dev/loop10 /dev/xvdk
    ln -s /dev/loop11 /dev/xvdl
    ln -s /dev/loop12 /dev/xvdm
    ln -s /dev/loop13 /dev/xvdn

    # Establish proper ownership and access permissions
    chown oracle:oinstall /dev/loop[1-9]
    chown oracle:oinstall /dev/loop1[0-3]
    chmod 666 /dev/loop[1-9]
    chmod 666 /dev/loop1[0-3]

    success
    echo
}


stop()
{
    # Deallocate loopback devices
    losetup -d /dev/loop1
    losetup -d /dev/loop2
    losetup -d /dev/loop3
    losetup -d /dev/loop4
    losetup -d /dev/loop5
    losetup -d /dev/loop6
    losetup -d /dev/loop7
    losetup -d /dev/loop8
    losetup -d /dev/loop9
    losetup -d /dev/loop10
    losetup -d /dev/loop11
    losetup -d /dev/loop12
    losetup -d /dev/loop13

    # Remove all associated symbolic links to loopback devices
    rm -f /dev/xvd[b-n]

    success
}

case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        *)
                echo $"Usage: $0 {start|stop}"
                exit 1
esac
exit $RETVAL