#!/bin/bash
# edmarcos antonio de souza
# EDEG INFORMATICA LTDA
# APUCARANA PR
# BRASIL
# www.edmarcos.com.br
#
# 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/

# 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 "\"Selecione o disco que já contem o XEN instalado\"" 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 10\"" \
        --checklist "\"Confirme os discos que vão receber o 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}Capturando tabela GPT do disco \"$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}Limpando a tabela de partições de \"$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}Instalando uma tabela GPT em \"$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}Copiando tabela GPT para o disco \"$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}Configurando 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}Criando dispositivo de bloco /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}Criando dispositivo de bloco /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}Criando dispositivo de bloco /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}Agregando \"$OFDISC\" ao raid...${COLOR_LESS}"
echo

echo -e "${COLOR_RED}Criando dispositivo raid /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}Criando dispositivo raid /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}Criando dispositovo raid /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}Criando volume LVM em /dev/md2...${COLOR_LESS}"
echo -e "${COLOR_GREEN}pvcreate -ff /dev/md2${COLOR_LESS}"
                       pvcreate -ff /dev/md2
sleep 3

echo -e "${COLOR_RED}Extendendo volume LVM para /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}Movendo volume LVM em \"${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}Reduzindo volume LVM em \"${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}Removendo volume LVM em \"${IFDISC}3\"...${COLOR_LESS}"
echo -e "${COLOR_GREEN}pvremove ${IFDISC}3${COLOR_LESS}"
                       pvremove ${IFDISC}3
sleep 3

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

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

# splash -------------------------------
echo -e "${COLOR_GREEN}wget http://office.edeg.com.br/reserved/xen/background.png -O /usr/share/splash/background.png${COLOR_LESS}"
                       wget http://office.edeg.com.br/reserved/xen/background.png -O /usr/share/splash/background.png
echo -e ${COLOR_GREEEN}wget http://office.edeg.com.br/reserved/xen/background.png -O /usr/share/splashy/themes/citrix-theme/background.png${COLOR_LESS}
                       wget http://office.edeg.com.br/reserved/xen/background.png -O /usr/share/splashy/themes/citrix-theme/background.png

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

echo -e "${COLOR_RED}Editando /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}Montando /mnt/dev...${COLOR_LESS}"
mount --bind /dev/ /mnt/dev
echo -e "${COLOR_RED}Montando /mnt/sys...${COLOR_LESS}"
mount -t sysfs none /mnt/sys
echo -e "${COLOR_RED}Montando /mnt/proc...${COLOR_LESS}"
mount -t proc none /mnt/proc

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

echo -e "${COLOR_RED}Gravando 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}Editando /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}Criando /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}Desmontando /mnt/dev${COLOR_LESS}"
umount /mnt/dev
echo -e "${COLOR_RED}Desmontando /mnt/sys${COLOR_LESS}"
umount /mnt/sys
echo -e "${COLOR_RED}Desmontando /mnt/proc${COLOR_LESS}"
umount /mnt/proc
echo -e "${COLOR_RED}Desmontando /mnt${COLOR_LESS}"
umount /mnt
echo
echo -e "${COLOR_RED}Parte 1 concluida...${COLOR_LESS}"
echo -e "${COLOR_RED}Agora reinicie o servidor selecione o boot pelo outro disco e execute a parte 2...${COLOR_LESS}"
echo

}

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

PART2()
{

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 10\"" \
        --radiolist "\"Confirme o disco que deve ser adicionado ao 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}Sincronizando os disco \"$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}Processo concluido...${COLOR_LESS}"
}

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

PART3() 
{
echo -e "${COLOR_RED}Troca de disco...${COLOR_LESS}"

# identificando disco de origem ------------------------------------------------
clear
echo
echo -e "${COLOR_RED}DIGITE O DISCO DE ORIGEM (O QUE JA CONTEM O XEN INSTALADO)${COLOR_LESS}"
echo
echo -e "${COLOR_RED}EXEMPLO: \"/dev/sda\"...(normalmente)${COLOR_LESS}"
echo
read IFDISC
echo

# identificando disco de destino -----------------------------------------------
echo
echo -e "${COLOR_RED}DIGITE O DISCO DE DESTINO (O DISCO QUE VAI RECEBER A COPIA)${COLOR_LESS}"
echo
echo -e "${COLOR_RED}EXEMPLO: \"/dev/sdb\"...(normalmente)${COLOR_LESS}"
echo
read OFDISC
echo

# capturando tabela do disco de origem -----------------------------------------
clear
echo
echo -e "${COLOR_RED}Capturando tabela GPT do disco ${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}Limpando a tabela de partições de \"$OFDISC\"...${COLOR_LESS}"
echo
sgdisk --zap-all $OFDISC

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

# copiado setores p/ disco destino -----
clear
echo
echo -e "${COLOR_RED}Copiando tabela GPT para o disco ${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}Gravando 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}Sincronizando os disco ${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}Processo concluido...${COLOR_LESS}"

}

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

case "$1" in
        part1)
	echo "Primeira fase da instalacao..."
	PART1
	;;

        part2)
	echo -e "${COLOR_RED}Conclusao da instalacao...${COLOR_LESS}"
	PART2
        ;;

	part3)
	echo "Substituicao de disco defeituoso..."
	PART3
	;;

	*)
        echo "Usage: $0 {part1|part2|part3}"

        exit 1
esac

exit 0
Enter your comment. Wiki syntax is allowed:
 
  • citrix/program_mdadm_raid_xenserver_6.5.txt
  • Last modified: 2019/02/11 09:13
  • by 127.0.0.1