вторник, 13 октября 2015 г.

скрипт xen65mdadm.sh создания soft raid1 на основе mdadm под xenserver 6.5, Скрипт создан бразильцем Edmarcos Antonio de Souza и переведен на английский язык.
#!/bin/bash
# edmarcos antonio de souza
# EDEG INFORMATICA LTDA
# APUCARANA PR
# BRASIL
# www.edmarcos.com.br
# Translate to english by profvds.com
#
# 20120415
# fontes: http://blog.codeaddict.org/?p=5
# 20120416
# 20120919
# 20121116
# http://lukas.zapletalovi.com/2011/12/how-to-get-rid-of-guid-partition-table.html
# http://www.ducea.com/2009/03/08/mdadm-cheat-sheet/
# http://ubuntuforums.org/showthread.php?t=1722440
# 20130127
# http://pastebin.com/buBHcfxU
# http://stackoverflow.com/questions/8707156/scripting-mdadm-when-a-component-device-may-contain-ext2-file-system-already
# http://stilen.com/notes/raid1_debian_howto.txt
# http://wysotsky.info/?p=106
# http://pastebin.com/buBHcfxU
# http://blog.codeaddict.org/?p=5
# http://www.zultron.com/2012/09/xcpxenserver-on-raid1-booting-pitfalls/
# 20130316
# http://www200.pair.com/mecham/raid/raid1-page3.html
# 20130522
# 20131028
# 20140115
# 20140531
# http://wiki.mesouug.com/index.php/XCP_/_Xenserver
# 20150104
# 20150124 (xenserver 6.5)
# http://uwot.eu/blog/2014/02/xenserver-and-fake-raid1/
# http://advancelinux.blogspot.com.br/2013/06/how-to-rebuild-initrd-or-initramfs-in.html
# https://www.centos.org/forums/viewtopic.php?f=15&t=47023
# https://aubreykloppers.wordpress.com/2012/07/06/mdadm-devmd127/
#
# 20150126
# http://discussions.citrix.com/topic/360943-software-raid-mdadm-on-xenserver-65-unexpected-failure/
# 20150427

# mdadm --examine --verbose --scan
# mdadm --stop /dev/md0
# mdadm --stop /dev/md1
# mdadm --stop /dev/md2
# mdadm --remove /dev/md0
# mdadm --zero-superblock /dev/sda
#
# parted /dev/sda
# mklabel msdos
# quit

# http://en.wikipedia.org/wiki/Echo_%28command%29
COLOR_RED="\033[0;31m"
COLOR_GREEN="\033[0;32m"
COLOR_LESS="\033[0m"
NULL=/dev/null

# part1 ------------------------------------------------------------------------

PART1()
{

DISKS=$(fdisk -l 2>$NULL | grep ^Disk  | awk {'print $2'} | cut -d: -f1)

cat $NULL > /tmp/disks

for i in `echo $DISKS`
    do
    echo "$i \"disco $i\" off" >> /tmp/disks
    done

# SOURCE -----------------------------------------------------------------------
CHOSE=$(eval whiptail --backtitle "\"EDEG INFORMATICA LTDA\"" --title "\"MAKE RAID 1\"" \
        --radiolist "\"Select the disk that contains the already installed XENo\"" 0 0 0 \
        $(cat /tmp/disks) 3>&1 1>&2 2>&3)

retval=$?

case "$retval" in
        0)
    IFDISC=$CHOSE
        ;;

        1)
        rm -f /tmp/disks
        ;;
esac

# DESTINOS ----------------------------------------------------
CHOSE=$(eval whiptail --backtitle "\"EDEG INFORMATICA LTDA\"" --title "\"MAKE RAID 1\"" \
        --checklist "\"Confirm the disks that are going to get XEN\"" 0 0 0 \
        $(cat /tmp/disks | grep -v "$IFDISC") 3>&1 1>&2 2>&3)

retval=$?

case "$retval" in
        0)
    test -z $CHOSE
    OFDISC=$(echo $CHOSE | sed s/\"//g)
        ;;

        1)
        rm -f /tmp/disks
        ;;
esac

# ------------------------------------------------------------------------------
clear
#echo
echo -e "${COLOR_RED}Capturing disk GPT table \"$IFDISC\"...${COLOR_LESS}"
echo

PART1_HOME=`sgdisk -p $IFDISC | grep "^   1" | awk {'print $2'}`
PART1_END=`sgdisk -p $IFDISC  | grep "^   1" | awk {'print $3'}`

PART2_HOME=`sgdisk -p $IFDISC | grep "^   2" | awk {'print $2'}`
PART2_END=`sgdisk -p $IFDISC  | grep "^   2" | awk {'print $3'}`

PART3_HOME=`sgdisk -p $IFDISC | grep "^   3" | awk {'print $2'}`
PART3_END=`sgdisk -p $IFDISC  | grep "^   3" | awk {'print $3'}`

# limpando disco destino -------------------------------------------------------
#echo
echo -e "${COLOR_RED}Clearing the partition table \"$OFDISC\"...${COLOR_LESS}"
echo -e "${COLOR_GREEN}sgdisk --zap-all ${OFDISC}${COLOR_LESS}"
                       sgdisk --zap-all ${OFDISC}

# instalando uma tabela GPT disco destino --------------------------------------
#echo
echo -e "${COLOR_RED}Installing a GPT table \"$OFDISC\"...${COLOR_LESS}"
echo -e "${COLOR_GREEN}sgdisk --mbrtogpt --clear ${OFDISC}${COLOR_LESS}"
                       sgdisk --mbrtogpt --clear $OFDISC

# copiado setores p/ disco destino ---------------------------------------------
echo -e "${COLOR_RED}Copying GPT table to disk \"$OFDISC\"...${COLOR_LESS}"

echo -e "${COLOR_GREEN}sgdisk --new=1:$PART1_HOME:$PART1_END ${OFDISC}${COLOR_LESS}"
                       sgdisk --new=1:$PART1_HOME:$PART1_END ${OFDISC}
echo -e "${COLOR_GREEN}sgdisk --typecode=1:fd00 ${OFDISC}${COLOR_LESS}"
                       sgdisk --typecode=1:fd00 ${OFDISC}
echo -e "${COLOR_GREEN}sgdisk --attributes=1:set:2 ${OFDISC}${COLOR_LESS}"
                       sgdisk --attributes=1:set:2 ${OFDISC}
echo -e "${COLOR_GREEN}sgdisk --new=2:$PART2_HOME:$PART2_END ${OFDISC}${COLOR_LESS}"
                       sgdisk --new=2:$PART2_HOME:$PART2_END ${OFDISC}
echo -e "${COLOR_GREEN}sgdisk --typecode=2:fd00 ${OFDISC}${COLOR_LESS}"
                       sgdisk --typecode=2:fd00 ${OFDISC}
echo -e "${COLOR_GREEN}sgdisk --new=3:$PART3_HOME:$PART3_END ${OFDISC}${COLOR_LESS}"
                       sgdisk --new=3:$PART3_HOME:$PART3_END ${OFDISC}
echo -e "${COLOR_GREEN}sgdisk --typecode=3:fd00 ${OFDISC}${COLOR_LESS}"
                       sgdisk --typecode=3:fd00 $OFDISC

# RAID -------------------------------------------------------------------------
echo
echo -e "${COLOR_RED}Setting raid ...${COLOR_LESS}"
echo
lsmod | grep -q ^raid1 || {
echo -e "${COLOR_RED}/sbin/modprobe raid1${COLOR_LESS}"
                 /sbin/modprobe raid1
}

# /dev/md0 -----------------------------
test -b /dev/md0 || {
echo -e "${COLOR_RED}Creating block device /dev/md0...${COLOR_LESS}"
echo -e "${COLOR_GREEN}mknod /dev/md0 b 9 0${COLOR_LESS}"
                       mknod /dev/md0 b 9 0
sleep 5
}

# /dev/md1 -----------------------------
test -b /dev/md1 || {
echo -e "${COLOR_RED}Creating block device /dev/md1...${COLOR_LESS}"
echo -e "${COLOR_GREEN}mknod /dev/md1 b 9 1${COLOR_LESS}"
                       mknod /dev/md1 b 9 1
sleep 5
}

# /dev/md2 -----------------------------
test -b /dev/md2 || {
echo -e "${COLOR_RED}Creating block device /dev/md2...${COLOR_LESS}"
echo -e "${COLOR_GREEN}mknod /dev/md2 b 9 2${COLOR_LESS}"
                       mknod /dev/md2 b 9 2
sleep 5
}

echo -e "${COLOR_RED}Adding \"$OFDISC\" to raid...${COLOR_LESS}"
echo

echo -e "${COLOR_RED}Creating raid device /dev/md0...${COLOR_LESS}"
echo -e "${COLOR_GREEN}/usr/bin/yes|mdadm --create /dev/md0 --level=1 --raid-devices=2 missing ${OFDISC}1 --metadata=0.90${COLOR_LESS}"
#                      /usr/bin/yes|mdadm --create /dev/md0 --level=1 --raid-devices=2 missing ${OFDISC}1
                       /usr/bin/yes|mdadm --create /dev/md0 --level=1 --raid-devices=2 missing ${OFDISC}1 --metadata=0.90

sleep 5

echo -e "${COLOR_RED}Creating raid device /dev/md1...${COLOR_LESS}"
echo -e "${COLOR_GREEN}/usr/bin/yes|mdadm --create /dev/md1 --level=1 --raid-devices=2 missing ${OFDISC}2${COLOR_LESS}"
                       /usr/bin/yes|mdadm --create /dev/md1 --level=1 --raid-devices=2 missing ${OFDISC}2
sleep 5

echo -e "${COLOR_RED}Creating raid device /dev/md2...${COLOR_LESS}"
echo -e "${COLOR_GREEN}/usr/bin/yes|mdadm --create /dev/md2 --level=1 --raid-devices=2 missing ${OFDISC}3${COLOR_LESS}"
                       /usr/bin/yes|mdadm --create /dev/md2 --level=1 --raid-devices=2 missing ${OFDISC}3
sleep 5

# MOVE LVM TO MD2 --------------------------------------------------------------
echo
echo -e "${COLOR_RED}Creating LVM volume /dev/md2...${COLOR_LESS}"
echo -e "${COLOR_GREEN}pvcreate -ff /dev/md2${COLOR_LESS}"
                       pvcreate -ff /dev/md2
sleep 3

echo -e "${COLOR_RED}Extend LVM volume /dev/md2...${COLOR_LESS}"
#echo -e "${COLOR_GREEN}vgextend `vgscan | grep VG | cut -d\' \' -f6 | sed s/\"//g` /dev/md2${COLOR_LESS}"
                       vgextend `vgscan | grep VG | cut -d' ' -f6 | sed s/\"//g` /dev/md2
sleep 3

echo -e "${COLOR_RED}Moving LVM volume in \"${IFDISC}3\" para /dev/md2...${COLOR_LESS}"
echo -e "${COLOR_GREEN}pvmove ${IFDISC}3 /dev/md2${COLOR_LESS}"
                       pvmove ${IFDISC}3 /dev/md2
sleep 3

echo -e "${COLOR_RED}Reducing LVM volume \"${IFDISC}3\"...${COLOR_LESS}"
#echo -e "${COLOR_GREEN}vgreduce `vgscan | grep VG | cut -d\' \' -f6 | sed s/\"//g` ${IFDISC}3${COLOR_LESS}"
                       vgreduce `vgscan | grep VG | cut -d' ' -f6 | sed s/\"//g` ${IFDISC}3
sleep 3

echo -e "${COLOR_RED}Remove LVM volume \"${IFDISC}3\"...${COLOR_LESS}"
echo -e "${COLOR_GREEN}pvremove ${IFDISC}3${COLOR_LESS}"
                       pvremove ${IFDISC}3
sleep 3

# ------------------------------------------------------------------------------
echo -e "${COLOR_RED}Formatting /dev/md0...${COLOR_LESS}"
echo -e "${COLOR_GREEN}mkfs.ext3 /dev/md0${COLOR_LESS}"
                       mkfs.ext3 /dev/md0

echo -e "${COLOR_RED}Mount /dev/md0 in /mnt...${COLOR_LESS}"
echo -e "${COLOR_GREEN}mount /dev/md0 /mnt${COLOR_LESS}"
                       mount /dev/md0 /mnt

#
echo -e "${COLOR_RED}Copying / to /mnt...${COLOR_LESS}"
echo -e "${COLOR_GREEN}cp -xpR / /mnt${COLOR_LESS}"
                       cp -xpR / /mnt

echo -e "${COLOR_RED}Editing /mnt/etc/fstab${COLOR_LESS}"
LABEL=`cat /mnt/etc/fstab | grep ^LABEL | awk {'print $1'}`
HDUUID=`blkid | grep ^/dev/md0 | awk {'print $2'} | sed s/\"//g`
#sed -i "s|${LABEL}|/dev/md0|" /mnt/etc/fstab
sed -i "s|${LABEL}|$HDUUID|" /mnt/etc/fstab

# montar particoes chroot ------------------------------------------------------
echo -e "${COLOR_RED}Mount /mnt/dev...${COLOR_LESS}"
mount --bind /dev/ /mnt/dev
echo -e "${COLOR_RED}Mount /mnt/sys...${COLOR_LESS}"
mount -t sysfs none /mnt/sys
echo -e "${COLOR_RED}Mount /mnt/proc...${COLOR_LESS}"
mount -t proc none /mnt/proc

echo -e "${COLOR_RED}Installing /boot...${COLOR_LESS}"
chroot /mnt /sbin/extlinux --install /boot

echo -e "${COLOR_RED}Recording gptmbr...${COLOR_LESS}"
echo -e "${COLOR_GREEN}dd if=/mnt/usr/share/syslinux/gptmbr.bin of=${OFDISC}${COLOR_LESS}"
                       dd if=/mnt/usr/share/syslinux/gptmbr.bin of=${OFDISC}

echo -e "${COLOR_RED}Editing /mnt/boot/extlinux.conf...${COLOR_LESS}"
#echo -e "${COLOR_GREEN}sed -i \"s|${LABEL}|/dev/md0|\" /mnt/boot/extlinux.conf${COLOR_LESS}"
#                       sed -i "s|${LABEL}|/dev/md0|" /mnt/boot/extlinux.conf
echo -e "${COLOR_GREEN}sed -i \"s|${LABEL}|$HDUUID|\" /mnt/boot/extlinux.conf${COLOR_LESS}"
                       sed -i "s|${LABEL}|$HDUUID|" /mnt/boot/extlinux.conf

# mdadm.conf -------------------------------------------------------------------
echo -e "${COLOR_RED}Creating /etc/mdadm.conf..${COLOR_LESS}"
/sbin/mdadm --detail --scan >> /mnt/etc/mdadm.conf

# mkinitrd ---------------------------------------------------------------------
# mkinitrd -f -v /boot/initrd-$(uname -r).img $(uname -r)

echo -e "${COLOR_RED}chroot /mnt /sbin/mkinitrd    -f --fstab=/etc/fstab --theme=/usr/share/splash --without-multipath /boot/initrd-`uname -r`.img `uname -r`${COLOR_LESS}"
#                    chroot /mnt /sbin/mkinitrd -v -f --fstab=/etc/fstab --theme=/usr/share/splash --without-multipath /boot/initrd-`uname -r`.img `uname -r`
                     chroot /mnt /sbin/mkinitrd    -f --fstab=/etc/fstab --theme=/usr/share/splash --without-multipath /boot/initrd-`uname -r`.img `uname -r`

sleep 5

# http://pastebin.com/buBHcfxU ---------
#mkdir /mnt/root/initrd-raid
#mkinitrd -v --fstab=/mnt/etc/fstab /mnt/root/initrd-raid/initrd-`uname -r`-raid.img `uname -r`
#cd /mnt/root/initrd-raid
#zcat initrd-`uname -r`-raid.img | cpio -i
#sed -i "s|^raidautorun /dev/md0|raidautorun /dev/md0\nraidautorun /dev/md1\nraidautorun /dev/md2|" /mnt/root/initrd-raid/init

# --------------------------------------

#

echo -e "${COLOR_RED}Unmount /mnt/dev${COLOR_LESS}"
umount /mnt/dev
echo -e "${COLOR_RED}Unmount /mnt/sys${COLOR_LESS}"
umount /mnt/sys
echo -e "${COLOR_RED}Unmount /mnt/proc${COLOR_LESS}"
umount /mnt/proc
echo -e "${COLOR_RED}Unmount /mnt${COLOR_LESS}"
umount /mnt
echo
echo -e "${COLOR_RED}Part 1 completed ...${COLOR_LESS}"
echo -e "${COLOR_RED}Now restart the server select the boot from another drive and run part 2 ...${COLOR_LESS}"
echo
#echo "Primeira fase da instalacao..."

}

################################################################################

PART2()
{

df | grep "/dev/md0" || {
    whiptail --backtitle "\"EDEG INFORMATICA LTDA\"" --title "\"MAKE RAID 1\"" \
    --msgbox "The root of your system is not in / dev / md0.
I can not continue this way.
Recheck the right boot drive" 10 78
    exit 0
    }

DISKS=$(fdisk -l 2>$NULL | grep ^Disk | grep -v "/dev/md[0-3]" | awk  {'print $2'} | cut -d: -f1)

cat $NULL > /tmp/disks
for i in `echo $DISKS`
    do
    echo "$i \"disco $i\" off" >> /tmp/disks
    done

cat $NULL > /tmp/b
for i in `cat /proc/mdstat | grep sd[a-z] | cut -d: -f2- | awk {'print $3" "$4" "$5'}`
    do
    echo $i | cut -c 1-3 >> /tmp/b
    done

IGNORE=$(cat /tmp/b | sort | uniq)

CHOSE=$(eval whiptail --backtitle "\"EDEG INFORMATICA LTDA\"" --title "\"MAKE RAID 1\"" \
        --radiolist "\"Confirm the disk that must be added to RAID\"" 0 0 0 \
        $(cat /tmp/disks | grep -v "$IGNORE") 3>&1 1>&2 2>&3)

retval=$?

case "$retval" in
        0)
    OFDISC=$CHOSE
        ;;

        1)
        rm -f /tmp/disks
    rm -f /tmp/b
        ;;
esac

# ------------------------------------------------------------------------------

echo
echo -e "${COLOR_RED}Mirror ${OFDISC}...${COLOR_LESS}"
echo -e "${COLOR_GREEN}sgdisk --typecode=1:fd00 ${OFDISC}${COLOR_LESS}"
                       sgdisk --typecode=1:fd00 ${OFDISC}
echo -e "${COLOR_GREEN}sgdisk --typecode=2:fd00 ${OFDISC}${COLOR_LESS}"
                       sgdisk --typecode=2:fd00 ${OFDISC}
echo -e "${COLOR_GREEN}sgdisk --typecode=3:fd00 ${OFDISC}${COLOR_LESS}"
                       sgdisk --typecode=3:fd00 ${OFDISC}
sleep 5

echo
echo -e "${COLOR_RED}Synchronizing disk \"$OFDISC\"...${COLOR_LESS}"

echo -e "${COLOR_GREEN}mdadm -a /dev/md0 ${OFDISC}1${COLOR_LESS}"
                       mdadm -a /dev/md0 ${OFDISC}1
sleep 30
echo -e "${COLOR_GREEN}mdadm -a /dev/md1 ${OFDISC}2${COLOR_LESS}"
                       mdadm -a /dev/md1 ${OFDISC}2
sleep 30
echo -e "${COLOR_GREEN}mdadm -a /dev/md2 ${OFDISC}3${COLOR_LESS}"
                       mdadm -a /dev/md2 ${OFDISC}3
sleep 5

echo

echo -e "${COLOR_RED}Process completed ...${COLOR_LESS}"

echo -e "${COLOR_RED}Completion of installation ...${COLOR_LESS}"
}

################################################################################

PART3()
{
echo -e "${COLOR_RED}Drive converter ...${COLOR_LESS}"

# identificando disco de origem ------------------------------------------------
clear
echo
echo -e "${COLOR_RED}ENTER THE ORIGIN OF DISCO (WHAT JA CONTAINS XEN INSTALLED)${COLOR_LESS}"
echo
echo -e "${COLOR_RED}EXEMPLE: \"/dev/sda\"...(normally)${COLOR_LESS}"
echo
read IFDISC
echo

# identificando disco de destino -----------------------------------------------
echo
echo -e "${COLOR_RED}ENTER THE FATE OF DISC (DISC THAT WILL RECEIVE A COPY)${COLOR_LESS}"
echo
echo -e "${COLOR_RED}EXEMPLE: \"/dev/sdb\"...(normally)${COLOR_LESS}"
echo
read OFDISC
echo

# capturando tabela do disco de origem -----------------------------------------
clear
echo
echo -e "${COLOR_RED}Capturing disk GPT table ${IFDISC}...${COLOR_LESS}"
echo

PART1_HOME=`sgdisk -p $IFDISC | grep "^   1" | awk {'print $2'}`
PART1_END=`sgdisk -p $IFDISC  | grep "^   1" | awk {'print $3'}`

PART2_HOME=`sgdisk -p $IFDISC | grep "^   2" | awk {'print $2'}`
PART2_END=`sgdisk -p $IFDISC  | grep "^   2" | awk {'print $3'}`

PART3_HOME=`sgdisk -p $IFDISC | grep "^   3" | awk {'print $2'}`
PART3_END=`sgdisk -p $IFDISC  | grep "^   3" | awk {'print $3'}`

# limpando disco destino -------------------------------------------------------
echo
echo -e "${COLOR_RED}Clearing the partition table \"$OFDISC\"...${COLOR_LESS}"
echo
sgdisk --zap-all $OFDISC

# instalando uma tabela GPT disco destino --------------------------------------
echo
echo -e "${COLOR_RED}Installing a GPT table \"$OFDISC\"...${COLOR_LESS}"
echo
sgdisk --mbrtogpt --clear $OFDISC

# copiado setores p/ disco destino -----
clear
echo
echo -e "${COLOR_RED}Copying GPT table to disk ${OFDISC}...${COLOR_LESS}"
echo
sgdisk --new=1:$PART1_HOME:$PART1_END $OFDISC
sgdisk --typecode=1:fd00 $OFDISC
sgdisk --attributes=1:set:2 $OFDISC
sgdisk --new=2:$PART2_HOME:$PART2_END $OFDISC
sgdisk --typecode=2:fd00 $OFDISC
sgdisk --new=3:$PART3_HOME:$PART3_END $OFDISC
sgdisk --typecode=3:fd00 $OFDISC

# gravando mbr -----------------------------------------------------------------
# http://www.syslinux.org/wiki/index.php/Mbr
echo -e "${COLOR_RED}Recording gptmbr...${COLOR_LESS}"
dd if=/usr/share/syslinux/gptmbr.bin of=$OFDISC
#dd bs=440 count=1 conv=notrunc if=/usr/share/syslinux/gptmbr.bin of=$OFDISC

sleep 5

# sincronizando raid -------------------
echo
echo -e "${COLOR_RED}Synchronizing disk ${OFDISC}...${COLOR_LESS}"

echo
echo "mdadm -a /dev/md0 ${OFDISC}1"
      mdadm -a /dev/md0 ${OFDISC}1
sleep 30

echo
echo "mdadm -a /dev/md1 ${OFDISC}2"
      mdadm -a /dev/md1 ${OFDISC}2
sleep 30

echo
echo "mdadm -a /dev/md2 ${OFDISC}3"
      mdadm -a /dev/md2 ${OFDISC}3
sleep 5

echo -e "${COLOR_RED}Process completed ...${COLOR_LESS}"

}

################################################################################

case "$1" in
        part1)
    PART1
    ;;

        part2)
    PART2
        ;;

    #part3)
    #echo "Replacement of defective disc ..."
    #PART3
    #;;

    *)
        #echo "Usage: $0 {part1|part2|part3}"
        echo "Usage: $0 {part1|part2}"
        exit 1
esac

exit 0

Комментариев нет:

Отправить комментарий