installer/mk-setup-grub.sh
f4d17450
 #!/bin/bash
 #################################################
 #       Title:  mk-setup-grub                   #
 #        Date:  2014-11-26                      #
 #     Version:  1.0                             #
 #      Author:  sharathg@vmware.com             #
 #     Options:                                  #
 #################################################
c3771c35
 #    Overview
 #        This is a precursor for the vmware build system.
 #        This assumes that an empty hard disk is attached to the build VM.
 #        The path to this empty disk is specified in the HDD variable in config.inc
 #    End
f4d17450
 #
c20a93b2
 
1c636dbf
 grub_efi_install()
 {
a6e91563
     mkdir -p $BUILDROOT/boot/efi
5e74d4d5
     #
     # if it is a loop device then we should mount the dev mapped boot partition
     #
     if [[ $HDD == *"loop"* ]]
     then
          BOOT_PARTITION=/dev/mapper/`basename ${HDD}`p1
9072ba9a
     elif [[ $HDD == *"nvme"* || $HDD == *"mmcblk"* ]]
f5c55576
     then
          BOOT_PARTITION=${HDD}p1
5e74d4d5
     else
85904234
          BOOT_PARTITION=${HDD}1
5e74d4d5
     fi
85904234
     mkfs.fat $BOOT_PARTITION
5e74d4d5
     mount -t vfat $BOOT_PARTITION $BUILDROOT/boot/efi
1c636dbf
     cp boot/unifont.pf2 /usr/share/grub/
c3d08ae9
 
c811dc3b
     mkdir -p $BUILDROOT/boot/efi/EFI/Boot/
c3d08ae9
     if [ $(uname -m) == "aarch64" ]
     then
         cp EFI_aarch64/BOOT/* $BUILDROOT/boot/efi/EFI/Boot/
f0eaa707
         local EXE_NAME="bootaa64.efi"
c3d08ae9
     elif [ $(uname -m) == "x86_64" ]
     then
         cp EFI_x86_64/BOOT/* $BUILDROOT/boot/efi/EFI/Boot/
f0eaa707
         local EXE_NAME="bootx64.efi"
c3d08ae9
     fi
 
a6e91563
     mkdir -p $BUILDROOT/boot/efi/boot/grub2
b3051521
     cat > $BUILDROOT/boot/efi/boot/grub2/grub.cfg << EOF
 search -n -u ${BOOT_UUID} -s
 configfile ${BOOT_DIRECTORY}grub2/grub.cfg
 EOF
f0eaa707
     efibootmgr --create --remove-dups --disk "$HDD" --part 1 --loader "/EFI/Boot/$EXE_NAME" --label Photon --verbose >&2
1c636dbf
     umount $BUILDROOT/boot/efi
 }
 
 grub_mbr_install()
 {
f5cac196
     $grubInstallCmd --target=i386-pc --force --boot-directory=$BUILDROOT/boot "$HDD"
1c636dbf
 }
 
c3771c35
 set -o errexit        # exit if error...insurance ;)
 set -o nounset        # exit if variable not initalized
 set +h            # disable hashall
 PRGNAME=${0##*/}    # script name minus the path
 source config.inc        #    configuration parameters
 source function.inc        #    commonn functions
 LOGFILE=/var/log/"${PRGNAME}-${LOGFILE}"    #    set log file name
 ARCH=$(uname -m)    # host architecture
 [ ${EUID} -eq 0 ]    || fail "${PRGNAME}: Need to be root user: FAILURE"
 > ${LOGFILE}        #    clear/initialize logfile
f4d17450
 
 # Check if passing a HHD and partition
85904234
 if [ $# -eq 6 ]
c3771c35
     then
7fd875e7
         BOOTMODE=$1
c3771c35
     HDD=$2
     ROOT_PARTITION_PATH=$3
     BOOT_PARTITION_PATH=$4
     BOOT_DIRECTORY=$5
85904234
     BOOT_PARTITION_NUMBER=$6
f4d17450
 fi
 
 #
c3771c35
 #    Install grub2.
f4d17450
 #
4b093b77
 PARTUUID=$(blkid -s PARTUUID -o value $ROOT_PARTITION_PATH)
c3771c35
 BOOT_UUID=$(blkid -s UUID -o value $BOOT_PARTITION_PATH)
a561daa4
 
 grubInstallCmd=""
315bf556
 mkdir -p $BUILDROOT/boot/grub2
a561daa4
 ln -sfv grub2 $BUILDROOT/boot/grub
 command -v grub-install >/dev/null 2>&1 && grubInstallCmd="grub-install" && { echo >&2 "Found grub-install"; }
 command -v grub2-install >/dev/null 2>&1 && grubInstallCmd="grub2-install" && { echo >&2 "Found grub2-install"; }
 
a6e91563
 if [ "$BOOTMODE" == "bios" ]; then
     if [ -z $grubInstallCmd ]; then
         echo "Unable to find grub install command"
         exit 1
     fi
7fd875e7
     grub_mbr_install
 fi
b44edc63
 if [ "$BOOTMODE" == "efi" ]; then
7fd875e7
     grub_efi_install
 fi
1c636dbf
 
a0b500b6
 rm -rf ${BUILDROOT}/boot/grub2/fonts
96b60cc5
 cp boot/ascii.pf2 ${BUILDROOT}/boot/grub2/
ea6053e3
 mkdir -p ${BUILDROOT}/boot/grub2/themes/photon
adde0296
 cp boot/splash.png ${BUILDROOT}/boot/grub2/themes/photon/photon.png
ea6053e3
 cp boot/terminal_*.tga ${BUILDROOT}/boot/grub2/themes/photon/
 cp boot/theme.txt ${BUILDROOT}/boot/grub2/themes/photon/
b44edc63
 # linux-esx tries to mount rootfs even before nvme got initialized.
 # rootwait fixes this issue
 EXTRA_PARAMS=""
 if [[ $HDD == *"nvme"* ]]; then
     EXTRA_PARAMS=rootwait
 fi
 
fb6292b3
 cat > $BUILDROOT/boot/grub2/grub.cfg << EOF
ea6053e3
 # Begin /boot/grub2/grub.cfg
71e98c0c
 
f4d17450
 set default=0
 set timeout=5
c3771c35
 search -n -u $BOOT_UUID -s
52cf674f
 loadfont ${BOOT_DIRECTORY}grub2/ascii.pf2
f4d17450
 
 insmod gfxterm
 insmod vbe
 insmod tga
adde0296
 insmod png
fb6292b3
 insmod ext2
 insmod part_gpt
f4d17450
 
 set gfxmode="640x480"
 gfxpayload=keep
 
 terminal_output gfxterm
 
52cf674f
 set theme=${BOOT_DIRECTORY}grub2/themes/photon/theme.txt
 load_env -f ${BOOT_DIRECTORY}photon.cfg
 if [ -f  ${BOOT_DIRECTORY}systemd.cfg ]; then
     load_env -f ${BOOT_DIRECTORY}systemd.cfg
 else
     set systemd_cmdline=net.ifnames=0
 fi
4b093b77
 set rootpartition=PARTUUID=$PARTUUID
f4d17450
 
 menuentry "Photon" {
b44edc63
     linux ${BOOT_DIRECTORY}\$photon_linux root=\$rootpartition \$photon_cmdline \$systemd_cmdline $EXTRA_PARAMS
52cf674f
     if [ -f ${BOOT_DIRECTORY}\$photon_initrd ]; then
         initrd ${BOOT_DIRECTORY}\$photon_initrd
fb6292b3
     fi
f4d17450
 }
ea6053e3
 # End /boot/grub2/grub.cfg
f4d17450
 EOF
 
 #Cleanup the workspace directory
 rm -rf "$BUILDROOT"/tools
6b3c4668
 rm -rf "$BUILDROOT"/RPMS
f4d17450