File: gl-gen-grub2-efi-bldr

package info (click to toggle)
clonezilla 5.9.9-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 4,516 kB
  • sloc: sh: 40,222; perl: 193; python: 59; makefile: 26
file content (108 lines) | stat: -rwxr-xr-x 3,840 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/bash
# Author: Steven Shiau <steven _at_ clonezilla org>
# License: GPL
# Program to create the EFI boot loader from grub2

# Settings
# Load functions
. /usr/share/gparted/bin/gl-functions

efi_required_mod="part_gpt part_msdos hfsplus fat ext2 ntfs btrfs normal chain boot configfile linux appleldr minicmd multiboot iso9660 acpi efi_gop efi_uga elf loadbios loopback video_fb usb search fixvideo png gfxterm gfxterm_background gfxterm_menu echo videotest video pci reboot halt gfxmenu gettext"
grub_required_files="/usr/share/grub/unicode.pf2"

# Functions
USAGE() {
  echo "To create an EFI boot loader from grub2."
  echo "Usage: $0 OUTPUT_DIR"
  echo "OUTPUT_DIR is the where the created boot loader will be placed."
  echo "   E.g.  $0 /tmp/efi/"
}
#
#############
###  MAIN ###
#############
#
check_if_root

output_dir="$1"

# Checking
if [ ! -e /etc/debian_version ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "This is not a Debian Linux system. This program only works on Debian Linux system."
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "Program terminated!"
  exit 1
fi
if [ -z "$output_dir" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "No output dir! Program terminated!"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  USAGE
  exit 1
fi
if [ ! -d "$output_dir" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "The output dir \"$output_dir\" does not exist, or it's not a directory."
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "Program terminated!"
  exit 1
fi

#
grub_efi_dir="$(mktemp -d /tmp/grub-efi.XXXXXX)"
for i in grub-efi-ia32 grub-efi-amd64; do
  echo "Download $i from deb packages repository..."
  LC_ALL=C apt-get -d --reinstall -y install $i &>/dev/null
done
for i in /var/cache/apt/archives/grub-efi*.deb; do
  dpkg --extract $i $grub_efi_dir
done
# Prepare a grub.cfg so that the eltorito image could find it.
cat <<-GRUB_END > $grub_efi_dir/grub_head.cfg
search --file --set=root /.disk/info
set prefix=(\$root)/EFI/boot/

GRUB_END

# Check if xz compression is supported by grub-mkimage. If yes, use it. The prompt should look like:
# -C, --compression=(xz|none|auto)  choose the compression to use
grub_mkimg_compress_opt=""
if [ -n "$(LC_ALL=C grub-mkimage --help 2>&1 | grep -Ew -- "-C" | grep -iw xz)" ]; then
  # Even if grub-mkimage --help shows "-C xz" support, it still be possible that it actually lacks XZ support.
  # Ref: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=692246
  # Therefore we test here. When #692246 is fixed, we can remove this testing.
  if LC_ALL=C grub-mkimage -C xz -O i386-efi -o /dev/null &>/dev/null; then
    grub_mkimg_compress_opt="-C xz"
  fi
fi
#
for i in i386 x86_64; do
  export EFI_ARCH=$i
  case "$EFI_ARCH" in
    i386)   out_f="$output_dir/bootia32.efi" ;;
    x86_64) out_f="$output_dir/bootx64.efi" ;;
  esac
  # Checking the required modules
  for im in $efi_required_mod; do
    if [ ! -e "$grub_efi_dir/usr/lib/grub/$EFI_ARCH-efi/${im}.mod" ]; then
      [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
      echo "Missing grub2 module ${im}.mod... Exluding ${im}.mod..."
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      efi_required_mod="$(echo $efi_required_mod | sed -r -e "s/$im //g")"
    fi
  done
  grub-mkimage $grub_mkimg_compress_opt -O $EFI_ARCH-efi -d $grub_efi_dir/usr/lib/grub/$EFI_ARCH-efi/ -o $out_f --prefix=/EFI/boot/ -c $grub_efi_dir/grub_head.cfg $efi_required_mod
  rc=$?
  if [ "$rc" -gt 0 ]; then
    # When grub-mkimage fails, an empty output file will still be created. We'd better to clean it.
    rm -f $out_f
  fi
done
# Copy the required files, e.g. fonts to the output dir.
cp -a $grub_required_files $output_dir

# Clean the temp dir
if [ -e "$grub_efi_dir" -a -n "$(echo $grub_efi_dir | grep -E "grub-efi")" ]; then
  rm -rf $grub_efi_dir
fi