Skip to content
This repository has been archived by the owner on Sep 18, 2020. It is now read-only.

Commit

Permalink
build_library: support uefi boot from iso
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Owens committed Jun 13, 2016
1 parent 39c3ec9 commit 812a105
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 43 deletions.
75 changes: 50 additions & 25 deletions build_library/grub.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Main GRUB config

# dump vars as they are when grub loads this config
set

# figure out the root
if [ "$grub_platform" = "efi" ]; then
if [ -e "$cmddevice/isolinux/efi.img" ]; then
set isoboot="1"
fi

# on efi, $cmddevice has the disk grub was invoked from.
set root="$cmddevice"
else
search --fs-uuid @@ESP_FSID@@ --set root --hint "$root"
fi

# Set the prefix back to the correct value after we're done with memdisk
set prefix=($root)/coreos/grub

Expand Down Expand Up @@ -57,23 +72,25 @@ if [ "$net_default_server" != "" ]; then
fi

# Search for the OEM partition, load additional configuration if found.
if [ "$secure_boot" = "0" ]; then
if [ "$secure_boot" = "0" -a -z "$isoboot" ]; then
search --no-floppy --set oem --part-label OEM --hint "$root"
if [ -n "$oem" -a -f "($oem)/grub.cfg" ]; then
source "($oem)/grub.cfg"
fi
fi

# Determine if this is a first boot.
search --no-floppy --set first_boot \
--disk-uuid 00000000-0000-0000-0000-000000000001
if [ -n "$first_boot" ]; then
# Note we explicitly request the disk-guid randomization, first_boot only triggers ignition.
set first_boot="coreos.first_boot=1 coreos.randomize_disk_guid=00000000-0000-0000-0000-000000000001"
fi

if [ -n "$oem_id" ]; then
set oem_id="coreos.oem.id=$oem_id"
if [ -z "$isoboot" ]; then
search --no-floppy --set first_boot \
--disk-uuid 00000000-0000-0000-0000-000000000001
if [ -n "$first_boot" ]; then
# Note we explicitly request the disk-guid randomization, first_boot only triggers ignition.
set first_boot="coreos.first_boot=1 coreos.randomize_disk_guid=00000000-0000-0000-0000-000000000001"
fi

if [ -n "$oem_id" ]; then
set oem_id="coreos.oem.id=$oem_id"
fi
fi

# If no specific console has been set by the OEM then select based on
Expand Down Expand Up @@ -107,19 +124,27 @@ fi
# Assemble the options applicable to all the kernels below
set linux_cmdline="rootflags=rw mount.usrflags=ro consoleblank=0 $linux_root $linux_console $first_boot $oem_id $linux_append"

menuentry "CoreOS default" --id=coreos {
gptprio.next -d usr -u usr_uuid
if [ "$usr_uuid" = "7130c94a-213a-4e5a-8e26-6cce9662f132" ]; then
linux$suf /coreos/vmlinuz-a @@MOUNTUSR@@=PARTUUID=$usr_uuid $linux_cmdline
else
linux$suf /coreos/vmlinuz-b @@MOUNTUSR@@=PARTUUID=$usr_uuid $linux_cmdline
fi
}

menuentry "CoreOS USR-A" --id=coreos-a {
linux$suf /coreos/vmlinuz-a @@MOUNTUSR@@=PARTLABEL=USR-A $linux_cmdline
}
if [ -z "$isoboot" ]; then
menuentry "CoreOS default" --id=coreos {
gptprio.next -d usr -u usr_uuid
if [ "$usr_uuid" = "7130c94a-213a-4e5a-8e26-6cce9662f132" ]; then
linux$suf /coreos/vmlinuz-a @@MOUNTUSR@@=PARTUUID=$usr_uuid $linux_cmdline
else
linux$suf /coreos/vmlinuz-b @@MOUNTUSR@@=PARTUUID=$usr_uuid $linux_cmdline
fi
}

menuentry "CoreOS USR-A" --id=coreos-a {
linux$suf /coreos/vmlinuz-a @@MOUNTUSR@@=PARTLABEL=USR-A $linux_cmdline
}

menuentry "CoreOS USR-B" --id=coreos-b {
linux$suf /coreos/vmlinuz-b @@MOUNTUSR@@=PARTLABEL=USR-B $linux_cmdline
}
else
menuentry "CoreOS default" --id=coreos {
linux$suf /coreos/vmlinuz coreos.autologin= $linux_console
initrd$suf /coreos/cpio.gz
}
fi

menuentry "CoreOS USR-B" --id=coreos-b {
linux$suf /coreos/vmlinuz-b @@MOUNTUSR@@=PARTLABEL=USR-B $linux_cmdline
}
20 changes: 6 additions & 14 deletions build_library/grub_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ case "${FLAGS_target}" in
CORE_NAME="core.img"
;;
x86_64-efi)
CORE_MODULES+=( serial linuxefi efi_gop getenv smbios efinet verify http )
CORE_MODULES+=( serial linuxefi efi_gop getenv smbios efinet verify http iso9660 )
CORE_NAME="core.efi"
;;
x86_64-xen)
CORE_NAME="core.elf"
;;
arm64-efi)
CORE_MODULES+=( serial efi_gop getenv smbios efinet verify http )
CORE_MODULES+=( serial linux efi_gop getenv smbios efinet verify http iso9660 )
CORE_NAME="core.efi"
BOARD_GRUB=1
;;
Expand Down Expand Up @@ -133,20 +133,10 @@ for file in "${GRUB_SRC}"/*{.lst,.mod}; do
gzip --best --stdout "${file}" | sudo_clobber "${out}"
done

info "Generating ${GRUB_DIR}/load.cfg"
# Include a small initial config in the core image to search for the ESP
# by filesystem ID in case the platform doesn't provide the boot disk.
# The existing $root value is given as a hint so it is searched first.
ESP_FSID=$(sudo grub-probe -t fs_uuid -d "${LOOP_DEV}p1")
sudo_clobber "${ESP_DIR}/${GRUB_DIR}/load.cfg" <<EOF
search.fs_uuid ${ESP_FSID} root \$root
set prefix=(memdisk)
set
EOF

# Generate a memdisk containing the appropriately generated grub.cfg. Doing
# this because we need conflicting default behaviors between verity and
# non-verity images.
ESP_FSID=$(sudo grub-probe -t fs_uuid -d "${LOOP_DEV}p1")
GRUB_TEMP_DIR=$(mktemp -d)
if [[ ! -f "${ESP_DIR}/coreos/grub/grub.cfg.tar" ]]; then
info "Generating grub.cfg memdisk"
Expand All @@ -162,6 +152,9 @@ if [[ ! -f "${ESP_DIR}/coreos/grub/grub.cfg.tar" ]]; then
sed 's/@@MOUNTUSR@@/mount.usr/' > "${GRUB_TEMP_DIR}/grub.cfg"
fi

# fix up ESP UUID for PC boot. EFI boot will be able to provide the disk to grub.
sed --in-place --expression "s/@@ESP_FSID@@/${ESP_FSID}/" "${GRUB_TEMP_DIR}/grub.cfg"

sudo tar cf "${ESP_DIR}/coreos/grub/grub.cfg.tar" \
-C "${GRUB_TEMP_DIR}" "grub.cfg"
fi
Expand All @@ -171,7 +164,6 @@ sudo grub-mkimage \
--compression=auto \
--format "${FLAGS_target}" \
--directory "${GRUB_SRC}" \
--config "${ESP_DIR}/${GRUB_DIR}/load.cfg" \
--memdisk "${ESP_DIR}/coreos/grub/grub.cfg.tar" \
--output "${ESP_DIR}/${GRUB_DIR}/${CORE_NAME}" \
"${CORE_MODULES[@]}"
Expand Down
46 changes: 42 additions & 4 deletions build_library/vm_image_util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -570,16 +570,17 @@ _write_cpio_disk() {
}

_write_iso_disk() {
local base_dir="${VM_TMP_ROOT}/usr"
local base_dir="${VM_TMP_ROOT}"
local iso_target="${VM_TMP_DIR}/rootiso"
local fat_target="${VM_TMP_DIR}/rootfat"
local dst_dir=$(_dst_dir)
local vmlinuz_name="$(_dst_name ".vmlinuz")"

mkdir "${iso_target}"
pushd "${iso_target}" >/dev/null
mkdir isolinux syslinux coreos
_write_cpio_common "$1" "${iso_target}/coreos/cpio.gz"
cp "${base_dir}"/boot/vmlinuz "${iso_target}/coreos/vmlinuz"
cp "${base_dir}/usr/boot/vmlinuz" "${iso_target}/coreos/vmlinuz"
cp -R /usr/share/syslinux/* isolinux/
cat<<EOF > isolinux/isolinux.cfg
INCLUDE /syslinux/syslinux.cfg
Expand All @@ -594,9 +595,46 @@ label coreos
kernel /coreos/vmlinuz
append initrd=/coreos/cpio.gz coreos.autologin
EOF
mkisofs -v -l -r -J -o $2 -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table .
isohybrid $2

# create fat image of grub for EFI
# 2880K floppy which fits our grub nicely.
dd if=/dev/zero of=isolinux/efi.img bs=512 count=5760 2>/dev/null
mformat -i isolinux/efi.img -v EFI -f 2880
mmd -i isolinux/efi.img efi
mmd -i isolinux/efi.img efi/boot

local grub_arch
local grub_name
case $BOARD in
amd64-usr)
loader_name="bootx64.efi"
# this copies shim+grub to the fat
mcopy -i isolinux/efi.img -o "${base_dir}/boot/efi/boot/${loader_name}" "::efi/boot/${loader_name}"
mcopy -i isolinux/efi.img -o "${base_dir}/boot/efi/boot/grub.efi" "::efi/boot/grub.efi"
;;
arm64-usr)
# no shim on arm64 yet.
loader_name="bootaa64.efi"
mcopy -i isolinux/efi.img -o "${base_dir}/boot/efi/boot/${loader_name}" "::efi/boot/${loader_name}"
;;
esac

mkisofs -v -l -r -J -o $2 \
-eltorito-boot isolinux/isolinux.bin \
-eltorito-catalog isolinux/boot.cat \
-no-emul-boot \
-boot-load-size 4 \
-boot-info-table \
-eltorito-alt-boot \
-eltorito-platform efi \
-eltorito-boot isolinux/efi.img \
-no-emul-boot \
.

isohybrid --uefi $2

popd >/dev/null

}

# If a config format is defined write it!
Expand Down

0 comments on commit 812a105

Please sign in to comment.