~elementary-os/elementaryos/os-patch-grub2-bionic

« back to all changes in this revision

Viewing changes to util/grub.d/30_os-prober.in

  • Committer: RabbitBot
  • Date: 2018-02-05 13:05:56 UTC
  • Revision ID: rabbitbot@elementary.io-20180205130556-qgaormf12qpm3v40
Initial import, version 2.02-2ubuntu4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /bin/sh
 
2
set -e
 
3
 
 
4
# grub-mkconfig helper script.
 
5
# Copyright (C) 2006,2007,2008,2009  Free Software Foundation, Inc.
 
6
#
 
7
# GRUB is free software: you can redistribute it and/or modify
 
8
# it under the terms of the GNU General Public License as published by
 
9
# the Free Software Foundation, either version 3 of the License, or
 
10
# (at your option) any later version.
 
11
#
 
12
# GRUB is distributed in the hope that it will be useful,
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
# GNU General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU General Public License
 
18
# along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 
19
 
 
20
prefix="@prefix@"
 
21
exec_prefix="@exec_prefix@"
 
22
datarootdir="@datarootdir@"
 
23
 
 
24
export TEXTDOMAIN=@PACKAGE@
 
25
export TEXTDOMAINDIR="@localedir@"
 
26
 
 
27
. "$pkgdatadir/grub-mkconfig_lib"
 
28
 
 
29
if [ "x${GRUB_DISABLE_OS_PROBER}" = "xtrue" ]; then
 
30
  exit 0
 
31
fi
 
32
 
 
33
if [ -z "`which os-prober 2> /dev/null`" ] || [ -z "`which linux-boot-prober 2> /dev/null`" ] ; then
 
34
  # missing os-prober and/or linux-boot-prober
 
35
  exit 0
 
36
fi
 
37
 
 
38
OSPROBED="`os-prober | tr ' ' '^' | paste -s -d ' '`"
 
39
if [ -z "${OSPROBED}" ] ; then
 
40
  # empty os-prober output, nothing doing
 
41
  exit 0
 
42
fi
 
43
 
 
44
osx_entry() {
 
45
    if [ x$2 = x32 ]; then
 
46
        # TRANSLATORS: it refers to kernel architecture (32-bit)
 
47
        bitstr="$(gettext "(32-bit)")"
 
48
    else
 
49
        # TRANSLATORS: it refers to kernel architecture (64-bit)
 
50
        bitstr="$(gettext "(64-bit)")"
 
51
    fi
 
52
    # TRANSLATORS: it refers on the OS residing on device %s
 
53
    onstr="$(gettext_printf "(on %s)" "${DEVICE}")"
 
54
        cat << EOF
 
55
menuentry '$(echo "${LONGNAME} $bitstr $onstr" | grub_quote)' --class osx --class darwin --class os \$menuentry_id_option 'osprober-xnu-$2-$(grub_get_device_id "${DEVICE}")'  {
 
56
EOF
 
57
        save_default_entry | grub_add_tab
 
58
        prepare_grub_to_access_device ${DEVICE} | grub_add_tab
 
59
        cat << EOF
 
60
        load_video
 
61
        set do_resume=0
 
62
        if [ /var/vm/sleepimage -nt10 / ]; then
 
63
           if xnu_resume /var/vm/sleepimage; then
 
64
             set do_resume=1
 
65
           fi
 
66
        fi
 
67
        if [ \$do_resume = 0 ]; then
 
68
           xnu_uuid ${OSXUUID} uuid
 
69
           if [ -f /Extra/DSDT.aml ]; then
 
70
              acpi -e /Extra/DSDT.aml
 
71
           fi
 
72
           if [ /kernelcache -nt /System/Library/Extensions ]; then
 
73
              $1 /kernelcache boot-uuid=\${uuid} rd=*uuid
 
74
           elif [ -f /System/Library/Kernels/kernel ]; then
 
75
              $1 /System/Library/Kernels/kernel boot-uuid=\${uuid} rd=*uuid
 
76
              xnu_kextdir /System/Library/Extensions
 
77
           else
 
78
              $1 /mach_kernel boot-uuid=\${uuid} rd=*uuid
 
79
              if [ /System/Library/Extensions.mkext -nt /System/Library/Extensions ]; then
 
80
                xnu_mkext /System/Library/Extensions.mkext
 
81
              else
 
82
                xnu_kextdir /System/Library/Extensions
 
83
              fi
 
84
           fi
 
85
           if [ -f /Extra/Extensions.mkext ]; then
 
86
              xnu_mkext /Extra/Extensions.mkext
 
87
           fi
 
88
           if [ -d /Extra/Extensions ]; then
 
89
              xnu_kextdir /Extra/Extensions
 
90
           fi
 
91
           if [ -f /Extra/devprop.bin ]; then
 
92
              xnu_devprop_load /Extra/devprop.bin
 
93
           fi
 
94
           if [ -f /Extra/splash.jpg ]; then
 
95
              insmod jpeg
 
96
              xnu_splash /Extra/splash.jpg
 
97
           fi
 
98
           if [ -f /Extra/splash.png ]; then
 
99
              insmod png
 
100
              xnu_splash /Extra/splash.png
 
101
           fi
 
102
           if [ -f /Extra/splash.tga ]; then
 
103
              insmod tga
 
104
              xnu_splash /Extra/splash.tga
 
105
           fi
 
106
        fi
 
107
}
 
108
EOF
 
109
}
 
110
 
 
111
used_osprober_linux_ids=
 
112
 
 
113
for OS in ${OSPROBED} ; do
 
114
  DEVICE="`echo ${OS} | cut -d ':' -f 1`"
 
115
  LONGNAME="`echo ${OS} | cut -d ':' -f 2 | tr '^' ' '`"
 
116
  LABEL="`echo ${OS} | cut -d ':' -f 3 | tr '^' ' '`"
 
117
  BOOT="`echo ${OS} | cut -d ':' -f 4`"
 
118
  if UUID="`${grub_probe} --target=fs_uuid --device ${DEVICE%@*}`"; then
 
119
    EXPUUID="$UUID"
 
120
 
 
121
    if [ x"${DEVICE#*@}" != x ] ; then
 
122
      EXPUUID="${EXPUUID}@${DEVICE#*@}"
 
123
    fi
 
124
 
 
125
    if [ "x${GRUB_OS_PROBER_SKIP_LIST}" != "x" ] && [ "x`echo ${GRUB_OS_PROBER_SKIP_LIST} | grep -i -e '\b'${EXPUUID}'\b'`" != "x" ] ; then
 
126
      echo "Skipped ${LONGNAME} on ${DEVICE} by user request." >&2
 
127
      continue
 
128
    fi
 
129
  fi
 
130
 
 
131
  BTRFS="`echo ${OS} | cut -d ':' -f 5`"
 
132
  if [ "x$BTRFS" = "xbtrfs" ]; then
 
133
        BTRFSuuid="`echo ${OS} | cut -d ':' -f 6`"
 
134
        BTRFSsubvol="`echo ${OS} | cut -d ':' -f 7`"
 
135
  fi
 
136
 
 
137
  if [ -z "${LONGNAME}" ] ; then
 
138
    LONGNAME="${LABEL}"
 
139
  fi
 
140
 
 
141
  # os-prober returns text string followed by optional counter
 
142
  CLASS="--class $(echo "${LABEL}" | LC_ALL=C sed 's,[[:digit:]]*$,,' | cut -d' ' -f1 | tr 'A-Z' 'a-z' | LC_ALL=C sed 's,[^[:alnum:]_],_,g')"
 
143
 
 
144
  gettext_printf "Found %s on %s\n" "${LONGNAME}" "${DEVICE}" >&2
 
145
 
 
146
  case ${BOOT} in
 
147
    chain)
 
148
 
 
149
          onstr="$(gettext_printf "(on %s)" "${DEVICE}")"
 
150
      cat << EOF
 
151
menuentry '$(echo "${LONGNAME} $onstr" | grub_quote)' $CLASS --class os \$menuentry_id_option 'osprober-chain-$(grub_get_device_id "${DEVICE}")' {
 
152
EOF
 
153
      save_default_entry | grub_add_tab
 
154
      prepare_grub_to_access_device ${DEVICE} | grub_add_tab
 
155
 
 
156
      if [ x"`${grub_probe} --device ${DEVICE} --target=partmap`" = xmsdos ]; then
 
157
          cat << EOF
 
158
        parttool \${root} hidden-
 
159
EOF
 
160
      fi
 
161
 
 
162
      case ${LONGNAME} in
 
163
        Windows\ Vista*|Windows\ 7*|Windows\ Server\ 2008*)
 
164
        ;;
 
165
        *)
 
166
          cat << EOF
 
167
        drivemap -s (hd0) \${root}
 
168
EOF
 
169
        ;;
 
170
      esac
 
171
 
 
172
      cat <<EOF
 
173
        chainloader +1
 
174
}
 
175
EOF
 
176
    ;;
 
177
    efi)
 
178
 
 
179
        EFIPATH=${DEVICE#*@}
 
180
        DEVICE=${DEVICE%@*}
 
181
        onstr="$(gettext_printf "(on %s)" "${DEVICE}")"
 
182
      cat << EOF
 
183
menuentry '$(echo "${LONGNAME} $onstr" | grub_quote)' $CLASS --class os \$menuentry_id_option 'osprober-efi-$(grub_get_device_id "${DEVICE}")' {
 
184
EOF
 
185
      save_default_entry | sed -e "s/^/\t/"
 
186
      prepare_grub_to_access_device ${DEVICE} | sed -e "s/^/\t/"
 
187
 
 
188
      cat <<EOF
 
189
        chainloader ${EFIPATH}
 
190
}
 
191
EOF
 
192
    ;;
 
193
    linux)
 
194
      if [ "x$BTRFS" = "xbtrfs" ]; then
 
195
         LINUXPROBED="`linux-boot-prober btrfs ${BTRFSuuid} ${BTRFSsubvol}  2> /dev/null | tr ' ' '^' | paste -s -d ' '`"
 
196
      else
 
197
         LINUXPROBED="`linux-boot-prober ${DEVICE} 2> /dev/null | tr ' ' '^' | paste -s -d ' '`"
 
198
      fi
 
199
      prepare_boot_cache=
 
200
      boot_device_id=
 
201
      is_top_level=true
 
202
      title_correction_code=
 
203
      OS="${LONGNAME}"
 
204
 
 
205
      for LINUX in ${LINUXPROBED} ; do
 
206
        LROOT="`echo ${LINUX} | cut -d ':' -f 1`"
 
207
        LBOOT="`echo ${LINUX} | cut -d ':' -f 2`"
 
208
        LLABEL="`echo ${LINUX} | cut -d ':' -f 3 | tr '^' ' '`"
 
209
        LKERNEL="`echo ${LINUX} | cut -d ':' -f 4`"
 
210
        LINITRD="`echo ${LINUX} | cut -d ':' -f 5`"
 
211
        LPARAMS="`echo ${LINUX} | cut -d ':' -f 6- | tr '^' ' '`"
 
212
 
 
213
        if [ -z "${LLABEL}" ] ; then
 
214
          LLABEL="${LONGNAME}"
 
215
        fi
 
216
 
 
217
        if [ "${LROOT}" != "${LBOOT}" ]; then
 
218
          LKERNEL="${LKERNEL#/boot}"
 
219
          LINITRD="${LINITRD#/boot}"
 
220
        fi
 
221
 
 
222
        onstr="$(gettext_printf "(on %s)" "${DEVICE}")"
 
223
        recovery_params="$(echo "${LPARAMS}" | grep single)" || true
 
224
        counter=1
 
225
        while echo "$used_osprober_linux_ids" | grep 'osprober-gnulinux-$LKERNEL-${recovery_params}-$counter-$boot_device_id' > /dev/null; do
 
226
            counter=$((counter+1));
 
227
        done
 
228
        if [ -z "$boot_device_id" ]; then
 
229
            boot_device_id="$(grub_get_device_id "${DEVICE}")"
 
230
        fi
 
231
        used_osprober_linux_ids="$used_osprober_linux_ids 'osprober-gnulinux-$LKERNEL-${recovery_params}-$counter-$boot_device_id'"
 
232
 
 
233
        if [ -z "${prepare_boot_cache}" ]; then
 
234
          prepare_boot_cache="$(prepare_grub_to_access_device ${LBOOT} | grub_add_tab)"
 
235
        fi
 
236
 
 
237
        if [ "x$is_top_level" = xtrue ] && [ "x${GRUB_DISABLE_SUBMENU}" != xy ]; then
 
238
            cat << EOF
 
239
menuentry '$(echo "$OS $onstr" | grub_quote)' $CLASS --class gnu-linux --class gnu --class os \$menuentry_id_option 'osprober-gnulinux-simple-$boot_device_id' {
 
240
EOF
 
241
            save_default_entry | grub_add_tab
 
242
            printf '%s\n' "${prepare_boot_cache}"
 
243
            cat <<  EOF
 
244
        linux ${LKERNEL} ${LPARAMS}
 
245
EOF
 
246
            if [ -n "${LINITRD}" ] ; then
 
247
          cat << EOF
 
248
        initrd ${LINITRD}
 
249
EOF
 
250
            fi
 
251
        cat << EOF
 
252
}
 
253
EOF
 
254
            echo "submenu '$(gettext_printf "Advanced options for %s" "${OS} $onstr" | grub_quote)' \$menuentry_id_option 'osprober-gnulinux-advanced-$boot_device_id' {"
 
255
            is_top_level=false
 
256
        fi
 
257
        title="${LLABEL} $onstr"
 
258
        cat << EOF
 
259
        menuentry '$(echo "$title" | grub_quote)' --class gnu-linux --class gnu --class os \$menuentry_id_option 'osprober-gnulinux-$LKERNEL-${recovery_params}-$boot_device_id' {
 
260
EOF
 
261
        save_default_entry | sed -e "s/^/$grub_tab$grub_tab/"
 
262
        printf '%s\n' "${prepare_boot_cache}" | grub_add_tab
 
263
        cat <<  EOF
 
264
                linux ${LKERNEL} ${LPARAMS}
 
265
EOF
 
266
        if [ -n "${LINITRD}" ] ; then
 
267
            cat << EOF
 
268
                initrd ${LINITRD}
 
269
EOF
 
270
        fi
 
271
        cat << EOF
 
272
        }
 
273
EOF
 
274
        if [ x"$title" = x"$GRUB_ACTUAL_DEFAULT" ] || [ x"Previous Linux versions>$title" = x"$GRUB_ACTUAL_DEFAULT" ]; then
 
275
            replacement_title="$(echo "Advanced options for ${OS} $onstr" | sed 's,>,>>,g')>$(echo "$title" | sed 's,>,>>,g')"
 
276
            quoted="$(echo "$GRUB_ACTUAL_DEFAULT" | grub_quote)"
 
277
            title_correction_code="${title_correction_code}if [ \"x\$default\" = '$quoted' ]; then default='$(echo "$replacement_title" | grub_quote)'; fi;"
 
278
            grub_warn "$(gettext_printf "Please don't use old title \`%s' for GRUB_DEFAULT, use \`%s' (for versions before 2.00) or \`%s' (for 2.00 or later)" "$GRUB_ACTUAL_DEFAULT" "$replacement_title" "gnulinux-advanced-$boot_device_id>gnulinux-$version-$type-$boot_device_id")"
 
279
        fi
 
280
      done
 
281
      if [ x"$is_top_level" != xtrue ]; then
 
282
          echo '}'
 
283
      fi
 
284
      echo "$title_correction_code"
 
285
    ;;
 
286
    macosx)
 
287
      if [ "${UUID}" ]; then
 
288
        OSXUUID="${UUID}"
 
289
        osx_entry xnu_kernel 32
 
290
        osx_entry xnu_kernel64 64
 
291
      fi
 
292
    ;;
 
293
    hurd)
 
294
      onstr="$(gettext_printf "(on %s)" "${DEVICE}")"
 
295
      cat << EOF
 
296
menuentry '$(echo "${LONGNAME} $onstr" | grub_quote)' --class hurd --class gnu --class os \$menuentry_id_option 'osprober-gnuhurd-/boot/gnumach.gz-false-$(grub_get_device_id "${DEVICE}")' {
 
297
EOF
 
298
      save_default_entry | grub_add_tab
 
299
      prepare_grub_to_access_device ${DEVICE} | grub_add_tab
 
300
      grub_device="`${grub_probe} --device ${DEVICE} --target=drive`"
 
301
      mach_device="`echo "${grub_device}" | sed -e 's/(\(hd.*\),msdos\(.*\))/\1s\2/'`"
 
302
      grub_fs="`${grub_probe} --device ${DEVICE} --target=fs`"
 
303
      case "${grub_fs}" in
 
304
        *fs)    hurd_fs="${grub_fs}" ;;
 
305
        *)      hurd_fs="${grub_fs}fs" ;;
 
306
      esac
 
307
      cat << EOF
 
308
        multiboot /boot/gnumach.gz root=device:${mach_device}
 
309
        module /hurd/${hurd_fs}.static ${hurd_fs} --readonly \\
 
310
                        --multiboot-command-line='\${kernel-command-line}' \\
 
311
                        --host-priv-port='\${host-port}' \\
 
312
                        --device-master-port='\${device-port}' \\
 
313
                        --exec-server-task='\${exec-task}' -T typed '\${root}' \\
 
314
                        '\$(task-create)' '\$(task-resume)'
 
315
        module /lib/ld.so.1 exec /hurd/exec '\$(exec-task=task-create)'
 
316
}
 
317
EOF
 
318
    ;;
 
319
    minix)
 
320
          cat << EOF
 
321
menuentry "${LONGNAME} (on ${DEVICE}, Multiboot)" {
 
322
EOF
 
323
         save_default_entry | sed -e "s/^/\t/"
 
324
         prepare_grub_to_access_device ${DEVICE} | sed -e "s/^/\t/"
 
325
         cat << EOF
 
326
        multiboot /boot/image_latest
 
327
}
 
328
EOF
 
329
    ;;
 
330
    *)
 
331
      # TRANSLATORS: %s is replaced by OS name.
 
332
      gettext_printf "%s is not yet supported by grub-mkconfig.\n" "  ${LONGNAME}" >&2
 
333
    ;;
 
334
  esac
 
335
done