Skip to content

Instantly share code, notes, and snippets.

@lkarlslund
Last active August 12, 2025 10:12
Show Gist options
  • Select an option

  • Save lkarlslund/2330250ded87b919bfd043e9b353aac4 to your computer and use it in GitHub Desktop.

Select an option

Save lkarlslund/2330250ded87b919bfd043e9b353aac4 to your computer and use it in GitHub Desktop.
Deploy LEAF Bering uClibc 7.3.1 and later to dual BIOS / EFI bootable USB
#!/bin/bash
SCRIPT=$(readlink -f $0)
DISK="$1"
IMAGE="$2"
# Check if IMAGE variable is set and non-empty
if [ -n "$IMAGE" ]; then
# Check if IMAGE is a relative path (doesn't start with /)
if [[ "$IMAGE" != /* ]]; then
# Convert relative path to absolute path
IMAGE=$(realpath "$IMAGE")
fi
fi
# Set the distribution type in variable DIST, either "ubuntu" or "arch"
if [ `grep -q -i "ubuntu" /etc/os-release && echo 1 || echo 0` -eq 1 ]; then
echo "Running on Ubuntu"
DIST="ubuntu"
elif [ `grep -q -i "arch" /etc/os-release && echo 1 || echo 0` -eq 1 ]; then
echo "Running on Arch"
DIST="arch"
else
echo "Unsupported distribution"
exit 1
fi
# use which to test that all tools are available: sgdisk, losetup, mkfs.vfat, grub-install
for tool in sgdisk losetup mkfs.vfat grub-install partprobe; do
if ! which $tool > /dev/null; then
echo "Required tool '$tool' is not installed. Please install it and try again."
exit 1
fi
done
#apt install gdisk grub-efi-amd64-signed grub-pc
if [ -f "$DISK" ]; then
sgdisk --zap-all "$DISK"
sgdisk -n 1:0:-2MiB -t 0:ef00 -c 0:GRUB2EFI "$DISK"
sgdisk -n 2:0:0 -t 0:ef02 -c 0:BIOS "$DISK"
losetup -f --partscan "$DISK" || { echo 'Could not set up loop device' ; exit 1; }
DEVICE=`losetup -j "$DISK" | cut -d ":" -f 1`
FILEPART="${DEVICE}p1"
EFIPART="${DEVICE}p2"
elif [ -b $DISK ]; then
sgdisk --zap-all "$DISK"
sgdisk -n 1:0:-2MiB -t 0:ef00 -c 0:GRUB2EFI "$DISK"
sgdisk -n 2:0:0 -t 0:ef02 -c 0:BIOS "$DISK"
partprobe
DEVICE="$DISK"
FILEPART="${DISK}1"
EFIPART="${DISK}2"
elif [ ! -e $DISK ]; then
echo "Target not found"
exit 1
else
echo "Unsupported target"
exit 2
fi
mkfs.vfat -F32 -n GRUB2EFI $FILEPART || { echo 'Formatting device failed' ; exit 1; }
WORKDIR=/tmp/mnt_usb_booter.$$
mkdir $WORKDIR
mount -t vfat $FILEPART $WORKDIR || { echo 'Mounting new filesystem failed' ; exit 1; }
cd $WORKDIR
# Decompress the pre-configured LEAF image
echo Decompressing files ...
if [ -z $IMAGE ]; then
# Embedded image
PAYLOAD_LINE=`awk '/^__PAYLOAD_BELOW_HERE__/ {print NR + 1; exit 0; }' $SCRIPT`
tail -n+$PAYLOAD_LINE $SCRIPT | tar xz || { echo 'Decompressing embedded files failed' ; exit 1; }
else
# External image
cat $IMAGE | tar xz || { echo 'Decompressing external LEAF image failed' ; exit 1; }
fi
# Different layout used here
mkdir -p boot/grub/
# BIOS grub
echo "Installing GRUB (BIOS mode)"
grub-install --target=i386-pc --boot-directory=$WORKDIR/boot $DEVICE || { echo 'BIOS GRUB install failed' ; exit 1; }
# EFI grub
echo "Installing GRUB (EFI mode)"
if [ $DIST == "ubuntu" ]; then
# Ubuntu specific grub-efi-amd64-signed package
grub-install --target=x86_64-efi --removable --efi-directory=$WORKDIR --uefi-secure-boot --boot-directory=$WORKDIR/boot $DEVICE || { echo 'EFI GRUB install failed' ; exit 1; }
else
# Generic grub-efi package
grub-install --target=x86_64-efi --removable --efi-directory=$WORKDIR --boot-directory=$WORKDIR/boot $DEVICE || { echo 'EFI GRUB install failed' ; exit 1; }
fi
cat << EOF >> boot/grub/grub.cfg
insmod efi_gob
insmod efi_uga
insmod gfx_term
insmod gettext
insmod gzio
insmod part_gpt
insmod fat
set gfxpayload=keep
set timeout=5
set gfxmode=auto
terminal_output console
menuentry 'LEAF' {
search --label --set=root GRUB2EFI
linux /linux root=/dev/sda1 LEAFCFG="/dev/sda1:vfat" nomodeset reboot=bios usb_wait=3 intel_idle.max_cstate=0 processor.max_cstate=1 spectre_v2=off
initrd /initrd.lrp
}
EOF
echo "Unmounting device and cleaning up"
cd -
umount $WORKDIR && rmdir $WORKDIR
if [ ! "$DISK" == "$DEVICE" ]; then
losetup -d $DEVICE || { echo 'losetup teardown failed failed' ; exit 1; }
fi
echo "Done!"
exit 0
__PAYLOAD_BELOW_HERE__
@bcoff12
Copy link

bcoff12 commented Jul 4, 2025

Lars - Is this a working script? I need exactly this. Thank you!

@lkarlslund
Copy link
Author

Not sure if it's the latest version in prod, but fairly sure it worked for me. Using it for deployment of pre-configured LEAF instances at a customer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment