[Ovirt-devel] Hook script to preserve one partition untouched during install

Higor Aparecido Vieira Alves halves at linux.vnet.ibm.com
Mon Jul 18 20:44:36 UTC 2011


New version of hook script to preserve one partition untouched during
install.

Changelog:
- Can ignore multiple partitions (up to 9)
	ignore_vol=sda:2;3;4
- New boot parameter, data2_size to set Data2 Size (in MB)
	data2_size=15000
- Will handle Data2 Logical Volume only instead Data2 and Backup LVs
- Set partition type do LVM (partitions used in Data2 LV)


#!/bin/bash
#VERSION: 0.13

LOG_FILE="/var/log/partition.log"
RETURN=""
CMDLINE="/proc/cmdline"
VG_NAME="AppVG"
LV_DATA2="Data2"

# Write log messages in /var/log/partition
# Parameters:
#   - MSG: Log message
log() {
    local MSG=$1

    echo "$MSG" >> $LOG_FILE
}

# Scan a hard disk to find all available partitions to create 
# AppVG Volume Group.
# Parameter:
#   - DISK: hard disk to be analized (example, sdb)
#   - PARTITION: partition number to be preserved (example, 1)
# Return:
#   - ARRAY_PV: array with partitions available
disk_scan() {
    local DISK=$1
    local PART=$2
    local DM=''
    local DM_NAME=''
    local PATTERN=''
    local ARRAY=''
    local REGEX=''
    local DIR=''
    local DEVICE=''
    local NEW_DEVICE=''
    RETURN=''

    if [ -z "$DISK" -o -z "$PART" ]; then
        log "ERROR: $FUNCTION missing parameter"
        return 1
    fi

    if [ ! -e "/dev/$DISK" ]; then
        log "ERROR: $DISK not found"
        return 1
    fi

    DEVICE=$(echo "$PART" | sed 's/;//g')
    REGEX=".*[a-z][^$DEVICE]$"
    
    log "Change the partition type for LVM (8e) in AppVG partitions"
    PATTERN="$DISK?"
    DIR="/dev/"
    ARRAY=( $(find "$DIR" -type b -name "$PATTERN" -regex "$REGEX") )
    for DEVICE in ${ARRAY[*]}; do
        if $(fdisk -l /dev/$DISK | grep -q  ".*$DEVICE.*83.*"); then
	        parted /dev/$DISK set ${DEVICE:8} lvm on
                sleep 10
        fi
    done

    DM=$(ls /sys/block/$DISK/holders/)
    if [ -n "$DM" ]; then 
        log "Found a device mapper assigned to $DISK"
        
        if grep -q "0QEMU" /sys/block/${DM[0]}/dm/name; then
            # Remove extra white spaces from device holder name and
            # change it for "_"
            log "Using QEMU disks"
            log "Removing white spaces from device mapper name"
            DM_NAME=$(cat /sys/block/${DM[0]}/dm/name | sed -e 's/[ ]\
+/ /g' -e 's/ /_/g')
            OLD_IFS="$IFS"
            IFS="
"
            # Create symbolic links whithout white spaces for QEMU disks
            log "Creating symbolic links without white speaces to QEMU
disks"
            for DEVICE in $(ls /dev/mapper/0QEMU*); do
                NEW_DEVICE=$(echo $DEVICE | sed -e 's/[ ]\+/ /g' -e
's/ /_/g')
                ls -s "${DEVICE}" $NEW_DEVICE
            done
            IFS="$OLD_IFS"
        else
            DM_NAME=$(cat /sys/block/${DM[0]}/dm/name)
        fi

        PATTERN="*${DM_NAME}p*"
        DIR="/dev/disk/by-id/"
        RETURN=( $(find $DIR -type l -name "$PATTERN" -regex "$REGEX") )
        if [ ${#RETURN[*]} -ne 0 ]; then
            return 0 
        fi
        log "Error: Can not find $DISK partitions in /dev/disk/by-id"
        log "Looking in /dev/mapper"
        DIR="/dev/mapper"
        RETURN=( $(find $DIR -type l -name "$PATTERN" -regex "$REGEX") )
        if [ ${#RETURN[*]} -eq 0 ]; then
            log "Error: Can not identify partitions available for $DISK"
            return 1
        fi 
    else
        PATTERN="$DISK?"
        DIR="/dev/"
        RETURN=( $(find "$DIR" -type b -name "$PATTERN" -regex
"$REGEX") )
        if [ $? -ne 0 ]; then
            log "ERROR: Can not identify partitions available in
$DISK/$DM_NAME"
            return 1
        fi
    fi
    return 0
}

main() {


   local DEVICE=''
    local IGNORE_VOL=''
    local VG=''
    local PV=''
    local ARRAY_PV=''
    local OUTPUT=''
    local DISK''
    local PART=''
    local DATA2_SIZE=''
    local LV_DATA2_SIZE=''

    if grep -q "firstboot" $CMDLINE && grep -q "ignore_vol" $CMDLINE;
then
        IGNORE_VOL=$(cat $CMDLINE | sed 's/^.*ignore_vol=//' | awk
'{print $1}')
    else
        log "Parameters firstboot or ignore_vol not found in $CMDLINE.
Aborting"
        return 1
    fi

    DISK=`echo $IGNORE_VOL | sed -e 's/^\/dev\///' -e 's/:.*$//'\
                                -e 's/[[:digit:]].*$//'`

    PART=$(echo $IGNORE_VOL | sed -e 's/^.*://' -e 's/^.*[[:lower:]]//')

    if grep -q "data2_size" $CMDLINE; then
        DATA2_SIZE=$(cat $CMDLINE | sed 's/^.*data2_size=//' | awk
'{print $1}')
        LV_DATA2_SIZE="-L ${DATA2_SIZE}M"
    else
        LV_DATA2_SIZE="-L 290000M"
    fi

    if ! disk_scan $DISK $PART; then
        log "ERROR: Can not scan $DISK"
        return 1
    fi

    ARRAY_PV=${RETURN[*]}
    if [ ${#ARRAY_PV[*]} -eq 0 ]; then
        log "ERROR: Partitions not found, verify your disk."
        return 1
    fi

    for PV in ${ARRAY_PV[*]}; do
        # Looking if partition has a PV
        pvs --noheadings "$PV"
        if [ $? -eq 0 ]; then
            log "Physical Volume found in $PV"
            # Looking if PV has a Volume Group
            OUTPUT=( $(pvs --noheadings -o vg_name "$PV") )
            if [ ${#OUTPUT[*]} -gt 0 ]; then         
                log "PV $PV has a Volume Group"
                VG=${OUTPUT[0]}
                # Looking if Volume Group has Logical Volumes
                OUTPUT=( $(lvs --noheadings -o lv_name "$VG") )
                if [ ${#OUTPUT[*]} -gt 0 ]; then
                    log "$VG has Logical Volumes: ${OUTPUT[*]}"
                    log "Removing Logical Volumes"
                    lvremove -ff "$VG"
                    if [ $? -ne 0 ]; then
                        log "Error: Can not remove Logical Volumes"
                       return 1
                    fi
                fi
                log "Removing Volume Group $VG"
                vgremove -ff "$VG"
                if [ $? -ne 0 ]; then
                    log "Error: Can not remove Volume Group"
                    return 1
                fi
            fi
        else
            log "Creating Physical Volume on $PV"
            pvcreate "$PV"
            if [ $? -ne 0 ]; then
                log "Error: Can not create Physical Volume"
                return 1
            fi
        fi
    done

    log "Creating $VG Volume Group using ${ARRAY_PV[*]}"
    vgcreate "$VG_NAME" ${ARRAY_PV[*]}
    if [ $? -ne 0 ]; then
        log "Error: Can not create Volume Group"
        return 1
    fi

    log "Creating Logical Volume $LV_DATA2"
    lvcreate ${LV_DATA2_SIZE} -n ${LV_DATA2} ${VG_NAME}
    if [ $? -ne 0 ]; then
        log "Error: Can not create Logical Volume"
        return 1
    fi

    log "Formating Logical Volume $LV_DATA2"
    DEVICE="/dev/${VG_NAME}/${LV_DATA2}"
    mkfs -t ext4 $DEVICE
    if [ $? -ne 0 ]; then
        log "Error: Can not format Logical Volume"
        return 1
    fi

    if ! grep -q "$DEVICE" /etc/fstab; then
        log "Automounting $DEVICE..."
        echo "$DEVICE /data2 ext4 defaults,noatime 0 0" >> /etc/fstab
    fi
}

main
exit $?





More information about the ovirt-devel mailing list