Converting CentOS 7 machine from EFI boot to BIOS boot with gpt partitioned disk
Recently ran into an issue where most of our CentOS 7 virtual machines are using EFI boot with gpt partitioned disks. Working with AWS this presented an issue as they required BIOS boot and a few other requirements like UUID device mapping in /etc/fstab and nofail options on the mounts. Here is my script ( at this point anyway ) … to convert a machine from EFI to BIOS with gpt partioned disk. Hope others will find this useful as well.
#!/usr/bin/bash
if [ "$(whoami)" != 'root' ]; then
echo Must be root. Exiting.
exit 1
fi
yum -y install gdisk
umount /boot/efi
sgdisk --typecode=1:EF02 --change-name=1:'BIOS Grub' /dev/sda
sgdisk --typecode=2:8300 --change-name=2:'BOOT' /dev/sda
rm /boot/grub2/grubenv
grub2-install --target=i386-pc --modules="ext2 part_gpt" /dev/sda
grub2-mkconfig >> /boot/grub2/grub.cfg
sed -i 's/linuxefi /linux /g' /boot/grub2/grub.cfg
sed -i 's/initrdefi /initrd /g' /boot/grub2/grub.cfg
sed -i '/\/boot\/efi/ s/^#*/#/' -i /etc/fstab
# Update so UUIDs are mapped instead of devices ( required by AWS )
\cp /etc/fstab /etc/fstab.$(date +%F)
blkid > /tmp/blkid.$(date +%F)
while read line
do
device=`echo $line | awk -F: '{print $1}' | sed 's/\//\\\\\//g' `
uuid=`echo $line | awk '{print $2}' | sed 's/"//g'`
sed -i "s/^$device\s/$uuid /" /etc/fstab
done < /tmp/blkid.$(date +%F)
# Add the nofail option to all entries ( required by AWS )
awk '/^UUID/ { if(!match(/nofail/, $4)) $4=$4",nofail" } 1' /etc/fstab > /tmp/fstab.new
cp /tmp/fstab.new /etc/fstab
chmod 644 /etc/fstab
rm /tmp/fstab.new
echo Now power down vm and set BOOT to BIOS ... then power on