]> git.proxmox.com Git - grub2.git/blob - debian/postinst.in
Replace out-of-tree arm64 4k alignment patches w/ upstream cherry-picks
[grub2.git] / debian / postinst.in
1 #!/bin/bash
2 set -e
3
4 merge_debconf_into_conf()
5 {
6 local tmpfile; tmpfile="$1"
7 local setting; setting="$2"
8 local template; template="$3"
9
10 db_get "$template"
11 local value; value="$(echo "$RET" | sed -e 's,[$`"\],\\&,g')"
12 if grep -q "^${setting}=" "$tmpfile"; then
13 value="$(echo "$value" | sed -e 's,[\@],\\&,g')"
14 sed -i -re "s@^(${setting}=).*@\1\"${value}\"@" "$tmpfile"
15 else
16 echo >> "$tmpfile"
17 echo "${setting}=\"${value}\"" >> "$tmpfile"
18 fi
19 }
20
21 get_wubi_device()
22 {
23 if [ ! -x /usr/share/lupin-support/grub-mkimage ] || \
24 ! /usr/share/lupin-support/grub-mkimage --test; then
25 return 1
26 fi
27
28 local bootdev="$(grub-probe --target=device /boot)" || true
29 local loop_file=
30 case $bootdev in
31 /dev/loop/*|/dev/loop[0-9])
32 loop_file="$(losetup "$bootdev" | sed -e "s/^[^(]*(\([^)]\+\)).*/\1/")"
33 # If it's loop-mounted from another device, it isn't Wubi.
34 case $loop_file in
35 /dev/*) return 1 ;;
36 esac
37 ;;
38 *) return 1 ;;
39 esac
40
41 echo "$bootdev"
42 }
43
44 # This only works on a Linux system with udev running. This is probably the
45 # vast majority of systems where we need any of this, though, and we fall
46 # back reasonably gracefully if we don't have it.
47 cached_available_ids=
48 available_ids()
49 {
50 local id path
51
52 if [ "$cached_available_ids" ]; then
53 echo "$cached_available_ids"
54 return
55 fi
56
57 [ -d /dev/disk/by-id ] || return
58 cached_available_ids="$(
59 for path in /dev/disk/by-id/*; do
60 [ -e "$path" ] || continue
61 printf '%s %s\n' "$path" "$(readlink -f "$path")"
62 done | sort -k2 -s -u | cut -d' ' -f1
63 )"
64 echo "$cached_available_ids"
65 }
66
67 # Returns non-zero and no output if no mapping can be found.
68 device_to_id()
69 {
70 local id
71 for id in $(available_ids); do
72 if [ "$(readlink -f "$id")" = "$(readlink -f "$1")" ]; then
73 echo "$id"
74 return 0
75 fi
76 done
77 # Fall back to the plain device name if there's no by-id link for it.
78 if [ -e "$1" ]; then
79 echo "$1"
80 return 0
81 fi
82 return 1
83 }
84
85 devices_to_ids()
86 {
87 local device id ids
88 ids=
89 for device; do
90 id="$(device_to_id "$device" || true)"
91 if [ "$id" ]; then
92 ids="${ids:+$ids, }$id"
93 fi
94 done
95 echo "$ids"
96 }
97
98 all_disks()
99 {
100 local id
101 for id in $(available_ids); do
102 case $id in
103 *-part*) ;;
104 *) echo "$id" ;;
105 esac
106 done
107 }
108
109 all_partitions()
110 {
111 local id ids
112 ids=
113 for id in $(available_ids); do
114 if [ "$id" != "$1" ] && [ "${id%-part*}" = "$1" ]; then
115 ids="${ids:+$ids }$id"
116 fi
117 done
118 echo "$ids"
119 }
120
121 # In order to determine whether we accidentally ran grub-install without
122 # upgrade-from-grub-legacy on versions older than 1.98+20100617-1, we need
123 # to be able to scan a disk to determine whether GRUB 2 was installed in its
124 # boot sector. This is specific to i386-pc (but that's the only platform
125 # where we need it).
126 scan_grub2()
127 {
128 if ! dd if="$1" bs=512 count=1 2>/dev/null | grep -aq GRUB; then
129 # No version of GRUB is installed.
130 return 1
131 fi
132
133 # The GRUB boot sector always starts with a JMP instruction.
134 initial_jmp="$(dd if="$1" bs=2 count=1 2>/dev/null | od -Ax -tx1 | \
135 head -n1 | cut -d' ' -f2,3)"
136 [ "$initial_jmp" ] || return 1
137 initial_jmp_opcode="${initial_jmp%% *}"
138 [ "$initial_jmp_opcode" = eb ] || return 1
139 initial_jmp_operand="${initial_jmp#* }"
140 case $initial_jmp_operand in
141 47|4b|4c|63)
142 # I believe this covers all versions of GRUB 2 up to the package
143 # version where we gained a more explicit mechanism. GRUB Legacy
144 # always had 48 here.
145 return 0
146 ;;
147 esac
148
149 return 1
150 }
151
152 # for Linux
153 sysfs_size()
154 {
155 local num_sectors sector_size size
156 # Try to find out the size without relying on a partitioning tool being
157 # installed. This isn't too hard on Linux 2.6 with sysfs, but we have to
158 # try a couple of variants on detection of the sector size.
159 if [ -e "$1/size" ]; then
160 num_sectors="$(cat "$1/size")"
161 sector_size=512
162 if [ -e "$1/queue/logical_block_size" ]; then
163 sector_size="$(cat "$1/queue/logical_block_size")"
164 elif [ -e "$1/queue/hw_sector_size" ]; then
165 sector_size="$(cat "$1/queue/hw_sector_size")"
166 fi
167 size="$(expr "$num_sectors" \* "$sector_size" / 1000 / 1000)"
168 fi
169 [ "$size" ] || size='???'
170 echo "$size"
171 }
172
173 # for kFreeBSD
174 camcontrol_size()
175 {
176 local num_sectors sector_size size=
177
178 if num_sectors="$(camcontrol readcap "$1" -q -s -N)"; then
179 sector_size="$(camcontrol readcap "$1" -q -b)"
180 size="$(expr "$num_sectors" \* "$sector_size" / 1000 / 1000)"
181 fi
182
183 [ "$size" ] || size='???'
184 echo "$size"
185 }
186
187 maybe_udevadm()
188 {
189 if which udevadm >/dev/null 2>&1; then
190 udevadm "$@" || true
191 fi
192 }
193
194 # Returns value in $RET, like a debconf command.
195 describe_disk()
196 {
197 local disk id sysfs_path disk_basename size model
198 disk="$1"
199 id="$2"
200
201 model=
202 case $(uname -s) in
203 Linux)
204 sysfs_path="$(maybe_udevadm info -n "$disk" -q path)"
205 if [ -z "$sysfs_path" ]; then
206 sysfs_path="/block/$(printf %s "${disk#/dev/}" | sed 's,/,!,g')"
207 fi
208 size="$(sysfs_size "/sys$sysfs_path")"
209
210 model="$(maybe_udevadm info -n "$disk" -q property | sed -n 's/^ID_MODEL=//p')"
211 if [ -z "$model" ]; then
212 model="$(maybe_udevadm info -n "$disk" -q property | sed -n 's/^DM_NAME=//p')"
213 if [ -z "$model" ]; then
214 model="$(maybe_udevadm info -n "$disk" -q property | sed -n 's/^MD_NAME=//p')"
215 if [ -z "$model" ] && which dmsetup >/dev/null 2>&1; then
216 model="$(dmsetup info -c --noheadings -o name "$disk" 2>/dev/null || true)"
217 fi
218 fi
219 fi
220 ;;
221 GNU/kFreeBSD)
222 disk_basename=$(basename "$disk")
223 size="$(camcontrol_size "$disk_basename")"
224 model="$(camcontrol inquiry "$disk_basename" | sed -ne "s/^pass0: <\([^>]*\)>.*/\1/p")"
225 ;;
226 esac
227
228 [ "$model" ] || model='???'
229
230 db_subst grub-pc/disk_description DEVICE "$disk"
231 db_subst grub-pc/disk_description SIZE "$size"
232 db_subst grub-pc/disk_description MODEL "$model"
233 db_metaget grub-pc/disk_description description
234 }
235
236 # Returns value in $RET, like a debconf command.
237 describe_partition()
238 {
239 local disk part id path sysfs_path diskbase partbase size
240 disk="$1"
241 part="$2"
242 id="$3"
243 path="$4"
244
245 sysfs_path="$(maybe_udevadm info -n "$part" -q path)"
246 if [ -z "$sysfs_path" ]; then
247 diskbase="${disk#/dev/}"
248 diskbase="$(printf %s "$diskbase" | sed 's,/,!,g')"
249 partbase="${part#/dev/}"
250 partbase="$(printf %s "$partbase" | sed 's,/,!,g')"
251 sysfs_path="/block/$diskbase/$partbase"
252 fi
253 size="$(sysfs_size "/sys$sysfs_path")"
254
255 db_subst grub-pc/partition_description DEVICE "$part"
256 db_subst grub-pc/partition_description SIZE "$size"
257 db_subst grub-pc/partition_description PATH "$path"
258 db_metaget grub-pc/partition_description description
259 }
260
261 usable_partitions()
262 {
263 local last_partition path partition partition_id
264
265 last_partition=
266 for path in / /boot /boot/grub; do
267 partition="$(grub-probe -t device "$path" || true)"
268 if [ -z "$partition" ] || [ "$partition" = "$last_partition" ]; then
269 continue
270 fi
271 partition_id="$(device_to_id "$partition" || true)"
272 echo "$path:$partition_id"
273 last_partition="$partition"
274 done | sort -t: -k2
275 }
276
277 get_mountpoint()
278 {
279 local relpath boot_mountpoint
280
281 relpath="$(grub-mkrelpath "$1")"
282 boot_mountpoint="${1#$relpath}"
283 echo "${boot_mountpoint:-/}"
284 }
285
286 config_item()
287 {
288 for x in /etc/default/grub /etc/default/grub.d/*.cfg; do
289 if [ -e "$x" ]; then
290 . "$x"
291 fi
292 done
293 if [ "$(eval echo "\${$1+set}")" = set ]; then
294 eval echo "\$$1"
295 else
296 return
297 fi
298 }
299
300 running_in_container()
301 {
302 type systemd-detect-virt >/dev/null 2>&1 && systemd-detect-virt --quiet --container
303 }
304
305 no_nvram_arg() {
306 db_get grub2/update_nvram
307 if [ "$RET" = false ]; then
308 echo "--no-nvram"
309 fi
310 }
311
312 run_grub_install()
313 {
314 if ! grub-install $@ ; then
315 echo "Failed: grub-install $@" >&2
316 echo "WARNING: Bootloader is not properly installed, system may not be bootable" >&2
317 fi
318 }
319
320 case "$1" in
321 configure)
322 . /usr/share/debconf/confmodule
323
324 devicemap_regenerated=
325
326 if egrep -q '^[[:space:]]*post(inst|rm)_hook[[:space:]]*=[[:space:]]*(/sbin/|/usr/sbin/)?update-grub' /etc/kernel-img.conf 2>/dev/null; then
327 echo 'Removing update-grub hooks from /etc/kernel-img.conf in favour of' >&2
328 echo '/etc/kernel/ hooks.' >&2
329 sed -ri /etc/kernel-img.conf -e '\%^[[:space:]]*post(inst|rm)_hook[[:space:]]*=[[:space:]]*(/sbin/|/usr/sbin/)?update-grub%d'
330 fi
331
332 mkdir -p /boot/grub
333
334 case @PACKAGE@ in
335 grub-pc)
336 if test -e /boot/grub/device.map && ! test -e /boot/grub/core.img && \
337 ! test -e /boot/grub/@FIRST_CPU_PLATFORM@/core.img; then
338 # Looks like your device.map was generated by GRUB Legacy, which
339 # used to generate broken device.map (see #422851). Avoid the risk
340 # by regenerating it.
341 grub-mkdevicemap --no-floppy
342 devicemap_regenerated=1
343 fi
344 ;;
345 esac
346
347 if test -z "$devicemap_regenerated" && test -s /boot/grub/device.map && \
348 dpkg --compare-versions "$2" lt-nl 1.98+20100702-1 && \
349 test "$(uname -s)" = Linux; then
350 # Earlier versions of GRUB used unstable device names in device.map,
351 # which caused a variety of problems. There is some risk associated with
352 # regenerating it (so we prompt the user if it's non-trivial), but on the
353 # whole it's less risky to move to /dev/disk/by-id/.
354 devicemap_lines="$(egrep -v '^[[:space:]]+#' /boot/grub/device.map | wc -l)"
355 grub-mkdevicemap --no-floppy
356 devicemap_regenerated=1
357 if test "$devicemap_lines" != 1; then
358 db_input critical grub2/device_map_regenerated || true
359 db_go || true
360 fi
361 fi
362
363 if test -z "$devicemap_regenerated" && \
364 dpkg --compare-versions "$2" lt-nl 1.99~20101210-2 && \
365 grep -qs /md-uuid- /boot/grub/device.map; then
366 echo "Removing MD devices from device.map, since the BIOS cannot read from these." >&2
367 sed -i '/\/md-uuid-/d' /boot/grub/device.map
368 fi
369
370 tmp_default_grub="$(mktemp -t grub.XXXXXXXXXX)"
371 trap "rm -f ${tmp_default_grub}" EXIT
372 cp -p /usr/share/grub/default/grub ${tmp_default_grub}
373
374 merge_debconf_into_conf "$tmp_default_grub" GRUB_CMDLINE_LINUX grub2/linux_cmdline
375 merge_debconf_into_conf "$tmp_default_grub" GRUB_CMDLINE_LINUX_DEFAULT grub2/linux_cmdline_default
376
377 case @PACKAGE@ in
378 grub-pc)
379 merge_debconf_into_conf "$tmp_default_grub" GRUB_TIMEOUT grub-pc/timeout
380 sed -i -e 's/^\(GRUB_TIMEOUT=\)"\([0-9][0-9]*\)"/\1\2/' "$tmp_default_grub"
381 db_get grub-pc/hidden_timeout
382 if [ "$RET" = false ]; then
383 sed -i -e 's/^GRUB_HIDDEN_TIMEOUT=/#&/' "$tmp_default_grub"
384 fi
385 ;;
386 esac
387
388 # If the template configuration file hasn't changed, then no conflict is
389 # possible. ucf can't figure this out for itself since we apply
390 # debconf-based customisations on top of the template configuration
391 # file.
392 if [ -e /var/lib/grub/ucf/grub.previous ] && \
393 cmp -s /usr/share/grub/default/grub /var/lib/grub/ucf/grub.previous; then
394 ucf_env=UCF_FORCE_CONFFOLD=1
395 else
396 ucf_env=
397 fi
398
399 env $ucf_env ucf --three-way --debconf-ok --sum-file=/usr/share/grub/default/grub.md5sum "$tmp_default_grub" /etc/default/grub
400 cp -a /usr/share/grub/default/grub /var/lib/grub/ucf/grub.previous
401 package="$(ucfq --with-colons /etc/default/grub | cut -d : -f 2)"
402 if echo $package | grep -q "^grub-" ; then
403 ucfr --force @PACKAGE@ /etc/default/grub
404 else
405 ucfr @PACKAGE@ /etc/default/grub
406 fi
407
408 case @PACKAGE@ in
409 grub-pc)
410
411 fix_mixed_system=
412 if test -e /boot/grub/stage2 && test -e /boot/grub/menu.lst && \
413 ! test -e /boot/grub/grub2-installed && \
414 test -z "$UPGRADE_FROM_GRUB_LEGACY"; then
415 # Unfortunately, it's still possible that the user upgraded fully
416 # to GRUB 2 in some way other than running
417 # upgrade-from-grub-legacy; perhaps they ran grub-install by hand
418 # for some reason. It's really quite difficult to detect this
419 # situation, because the only difference between this and a
420 # working chainloaded setup is that in this case grub-setup has
421 # been run. So, to try to tell the difference, we scan the boot
422 # sectors of all disks for a GRUB 2 boot sector. Hopefully this
423 # won't cause too much to explode, since I can't think of a better
424 # method.
425 grub2_disks=
426 for disk in $(all_disks); do
427 if scan_grub2 "$disk"; then
428 grub2_disks="${grub2_disks:+$grub2_disks }$(readlink -f "$disk")"
429 fi
430 done
431 if [ "$grub2_disks" ]; then
432 # No || true here; it's vital that the user sees this, and it's
433 # better to throw an error than to do the wrong thing.
434 db_subst grub-pc/mixed_legacy_and_grub2 DISKS "$grub2_disks"
435 db_fset grub-pc/mixed_legacy_and_grub2 seen false
436 db_input critical grub-pc/mixed_legacy_and_grub2
437 db_go
438 db_get grub-pc/mixed_legacy_and_grub2
439 if [ "$RET" = true ]; then
440 db_reset grub-pc/install_devices
441 UPGRADE_FROM_GRUB_LEGACY=1
442 fix_mixed_system=1
443 # Fall through to normal installation logic.
444 fi
445 fi
446 fi
447
448 # Make sure that Wubi users never see confusing device prompts.
449 # Wubi is a very specialised hack that does complicated things with
450 # grub-install diversions to create an image that's chained from the
451 # Windows boot loader to boot an operating system from a file on a
452 # Windows file system. In these circumstances, prompting for where
453 # to install GRUB is not going to help anyone.
454 wubi_device="$(get_wubi_device)" || true
455 if [ "$wubi_device" ]; then
456 db_set grub-pc/install_devices "$wubi_device"
457 db_fset grub-pc/install_devices seen true
458 fi
459
460 if test -e /boot/grub/stage2 && test -e /boot/grub/menu.lst && \
461 ! test -e /boot/grub/grub2-installed && \
462 test -z "$UPGRADE_FROM_GRUB_LEGACY"; then
463 db_get grub-pc/chainload_from_menu.lst
464 if $RET ; then
465 # Create core.img (but do not risk writing to MBR).
466 # Using grub-probe instead of "(hd0)" avoids (UUID=) hack slowness
467 # in case /boot/grub is not on (hd0) in device.map.
468 echo "Generating core.img" >&2
469 grub-install --target=i386-pc --no-floppy --grub-setup=/bin/true "$(grub-probe -t drive /boot/grub)" > /dev/null
470
471 # Update menu.lst to reflect that:
472 # - core.img is present now
473 # - core.img has to be the first option
474 echo "Saving menu.lst backup in /boot/grub/menu.lst_backup_by_grub2_postinst" >&2
475 cp /boot/grub/menu.lst{,_backup_by_grub2_postinst}
476 echo "Running update-grub Legacy to hook our core.img in it" >&2
477 LET_US_TRY_GRUB_2=true /usr/lib/grub-legacy/update-grub 2>&1 | sed -e "s/^/ /g" >&2
478 # We just hooked GRUB 2 in menu.lst; then also generate grub.cfg.
479 touch /boot/grub/grub.cfg
480 fi
481 elif running_in_container; then
482 # Skip grub-install in containers.
483 :
484 elif test -z "$2" || test -e /boot/grub/core.img || \
485 test -e /boot/grub/@FIRST_CPU_PLATFORM@/core.img || \
486 test "$UPGRADE_FROM_GRUB_LEGACY" || test "$wubi_device"; then
487 question=grub-pc/install_devices
488 priority=high
489 device_map="$(grub-mkdevicemap -m - | grep -v '^(fd[0-9]\+)' || true)"
490 devices="$(echo "$device_map" | cut -f2)"
491 if dpkg --compare-versions "$2" lt 1.98+20100702-1 && \
492 test "$(uname -s)" = Linux && [ -z "$wubi_device" ]; then
493 # Migrate to new by-id naming scheme.
494 db_get grub-pc/install_devices
495 old_devices="$(echo "$RET" | sed 's/, / /g')"
496 new_devices=
497 # Common-case optimisation: if the list of devices is
498 # identical to the LHS of grub-mkdevicemap's output, then
499 # there's no point asking again; just install to all disks.
500 # (This handles e.g. "(hd0)" with one disk.)
501 if [ "$(echo "$device_map" | cut -f1 | sort)" = \
502 "$(echo "$old_devices" | xargs -n1 | sort)" ]; then
503 new_devices="$(devices_to_ids $devices)"
504 db_set grub-pc/install_devices "$new_devices"
505 # Alternatively, we might be installing to a single partition
506 # on a single disk, and we can deal with that too if there's
507 # only one available disk and it has an appropriate partition.
508 # This doesn't necessarily work for multiple disks because now
509 # the order matters.
510 elif [ "$(echo "$device_map" | wc -l)" = 1 ] && \
511 [ "$(echo "$old_devices" | wc -w)" = 1 ] && \
512 echo "$old_devices" | grep -q ,; then
513 old_device="${old_devices#(}"
514 old_device="${old_device%)}"
515 old_disk="${old_device%,*}"
516 old_partition="${old_device##*,}"
517 new_device="$(echo "$device_map" | grep "^($old_disk)" | \
518 cut -f2)"
519 new_device="$(device_to_id $new_device)"
520 if [ "$new_device" ]; then
521 new_device="$new_device-part$old_partition"
522 # Run through device_to_id again to check for existence.
523 new_device="$(device_to_id $new_device)"
524 fi
525 if [ "$new_device" ]; then
526 new_devices="$new_device"
527 db_set grub-pc/install_devices "$new_device"
528 fi
529 fi
530 if [ -z "$new_devices" ]; then
531 new_devices="$(devices_to_ids $old_devices)"
532 db_set grub-pc/install_devices "$new_devices"
533 # Common-case optimisation: if all devices are translatable
534 # to by-id and the number of devices there is the same as
535 # the number of devices GRUB can see, then there's no point
536 # asking again. (This handles e.g. "/dev/sda" with one
537 # disk.)
538 old_devices_count="$(echo "$old_devices" | wc -w)"
539 new_devices_count="$(echo "$new_devices" | wc -w)"
540 devices_count="$(echo "$devices" | wc -w)"
541 if [ "$old_devices_count" != "$new_devices_count" ] || \
542 [ "$new_devices_count" != "$devices_count" ]; then
543 db_fset grub-pc/install_devices seen false
544 db_fset grub-pc/install_devices_empty seen false
545 fi
546 fi
547 else
548 db_get grub-pc/install_devices
549 valid=1
550 for device in $RET; do
551 if [ ! -e "${device%,}" ]; then
552 valid=0
553 break
554 fi
555 done
556 if [ "$valid" = 0 ]; then
557 question=grub-pc/install_devices_disks_changed
558 priority=critical
559 db_set "$question" "$RET"
560 db_fset "$question" seen false
561 db_fset grub-pc/install_devices_empty seen false
562 fi
563 fi
564
565 while :; do
566 ids=
567 descriptions=
568 partitions="$(usable_partitions)"
569 for device in $devices; do
570 disk_id="$(device_to_id "$device" || true)"
571 if [ "$disk_id" ]; then
572 ids="${ids:+$ids, }$disk_id"
573 describe_disk "$(readlink -f "$device")" "$disk_id"
574 RET="$(printf %s "$RET" | sed 's/,/\\,/g')"
575 descriptions="${descriptions:+$descriptions, }$RET"
576 for partition_pair in $partitions; do
577 partition_id="${partition_pair#*:}"
578 if [ "${partition_id#$disk_id-part}" != "$partition_id" ]; then
579 ids="${ids:+$ids, }$partition_id"
580 describe_partition "$(readlink -f "$device")" "$(readlink -f "$partition_id")" "$partition_id" "$(get_mountpoint "${partition_pair%%:*}")"
581 RET="$(printf %s "$RET" | sed 's/,/\\,/g')"
582 descriptions="${descriptions:+$descriptions, }$RET"
583 fi
584 done
585 fi
586 done
587 # Some "partitions" may in fact be at the disk level, e.g. RAID.
588 # List these as well if they haven't already been listed.
589 for partition_pair in $partitions; do
590 partition_id="${partition_pair#*:}"
591 if [ "${partition_id#*-part}" = "$partition_id" ]; then
592 case ", $ids, " in
593 ", $partition_id, ") ;;
594 *)
595 ids="${ids:+$ids, }$partition_id"
596 describe_disk "$(readlink -f "$partition_id")" "$partition_id"
597 RET="$(printf %s "$RET" | sed 's/,/\\,/g')"
598 descriptions="${descriptions:+$descriptions, }$RET"
599 ;;
600 esac
601 fi
602 done
603 db_subst "$question" RAW_CHOICES "$ids"
604 db_subst "$question" CHOICES "$descriptions"
605 db_input "$priority" "$question" || true
606 db_go
607 db_get "$question"
608 failed_devices=
609 for i in `echo $RET | sed -e 's/, / /g'` ; do
610 real_device="$(readlink -f "$i")"
611 if grub-install --target=i386-pc --force --no-floppy $real_device ; then
612 # We just installed GRUB 2; then also generate grub.cfg.
613 touch /boot/grub/grub.cfg
614 else
615 failed_devices="$failed_devices $real_device"
616 fi
617 done
618
619 if [ "$question" != grub-pc/install_devices ]; then
620 db_set grub-pc/install_devices "$RET"
621 db_fset grub-pc/install_devices seen true
622 fi
623
624 if [ "$failed_devices" ]; then
625 if [ "$UPGRADE_FROM_GRUB_LEGACY" ]; then
626 db_subst grub-pc/install_devices_failed_upgrade FAILED_DEVICES "$failed_devices"
627 db_fset grub-pc/install_devices_failed_upgrade seen false
628 if db_input critical grub-pc/install_devices_failed_upgrade; then
629 db_go
630 db_get grub-pc/install_devices_failed_upgrade
631 if [ "$RET" = true ]; then
632 db_fset "$question" seen false
633 db_fset grub-pc/install_devices_failed_upgrade seen false
634 continue
635 else
636 exit 1
637 fi
638 else
639 exit 1 # noninteractive
640 fi
641 else
642 db_subst grub-pc/install_devices_failed FAILED_DEVICES "$failed_devices"
643 db_fset grub-pc/install_devices_failed seen false
644 if db_input critical grub-pc/install_devices_failed; then
645 db_go
646 db_get grub-pc/install_devices_failed
647 if [ "$RET" = true ]; then
648 break
649 else
650 db_fset "$question" seen false
651 db_fset grub-pc/install_devices_failed seen false
652 continue
653 fi
654 else
655 break # noninteractive
656 fi
657 fi
658 fi
659
660 db_get grub-pc/install_devices
661 if [ -z "$RET" ]; then
662 # Reset the seen flag if the current answer is false, since
663 # otherwise we'll loop with no indication of why.
664 db_get grub-pc/install_devices_empty
665 if [ "$RET" = false ]; then
666 db_fset grub-pc/install_devices_empty seen false
667 fi
668 if db_input critical grub-pc/install_devices_empty; then
669 db_go
670 db_get grub-pc/install_devices_empty
671 if [ "$RET" = true ]; then
672 break
673 else
674 db_fset "$question" seen false
675 db_fset grub-pc/install_devices_empty seen false
676 fi
677 else
678 break # noninteractive
679 fi
680 else
681 break
682 fi
683 done
684 fi
685
686 # /boot/grub/ has more chances of being accessible by GRUB
687 for i in /usr/share/grub/unicode.pf2 ; do
688 if test -e $i ; then
689 cp $i /boot/grub/
690 fi
691 done
692
693 if [ "$fix_mixed_system" ]; then
694 # These never contain any valuable information, and they aren't
695 # useful for boot any more, since we just overwrote MBR/PBR.
696 rm -f /boot/grub/{{xfs,reiserfs,e2fs,fat,jfs,minix}_stage1_5,stage{1,2}}
697 # Remove marker file used to indicate that grub-install was run
698 # rather than upgrade-from-grub-legacy. Since stage2 has been
699 # removed, we don't need this any more.
700 rm -f /boot/grub/grub2-installed
701 fi
702 ;;
703
704 grub-efi-ia32|grub-efi-amd64|grub-efi-ia64|grub-efi-arm|grub-efi-arm64)
705 bootloader_id="$(config_item GRUB_DISTRIBUTOR | tr A-Z a-z | \
706 cut -d' ' -f1)"
707 case $bootloader_id in
708 kubuntu) bootloader_id=ubuntu ;;
709 esac
710 if [ "$bootloader_id" ] && [ -d "/boot/efi/EFI/$bootloader_id" ]; then
711 case @PACKAGE@ in
712 grub-efi-ia32) target=i386-efi ;;
713 grub-efi-amd64) target=x86_64-efi ;;
714 grub-efi-ia64) target=ia64-efi ;;
715 grub-efi-arm) target=arm-efi ;;
716 grub-efi-arm64) target=arm64-efi ;;
717 esac
718 db_get grub2/force_efi_extra_removable
719 if [ "$RET" = true ]; then
720 FORCE_EXTRA_REMOVABLE="--force-extra-removable"
721 fi
722 NO_NVRAM="$(no_nvram_arg)"
723 run_grub_install --target="$target" "$FORCE_EXTRA_REMOVABLE" "$NO_NVRAM"
724 fi
725
726 # /boot/grub/ has more chances of being accessible by GRUB
727 for i in /usr/share/grub/unicode.pf2 ; do
728 if test -e $i ; then
729 cp $i /boot/grub/
730 fi
731 done
732
733 if type update-secureboot-policy >/dev/null 2>&1; then
734 update-secureboot-policy || true
735 fi
736 ;;
737
738 grub-ieee1275)
739 case $(dpkg --print-architecture) in
740 powerpc|ppc64|ppc64el)
741 # Output may be empty; if so, just update the core image but
742 # don't install it to any PReP partition.
743 prep_bootdev="$(/usr/lib/grub/powerpc-ieee1275/prep-bootdev)"
744 NO_NVRAM="$(no_nvram_arg)"
745 run_grub_install --target=powerpc-ieee1275 $prep_bootdev "$NO_NVRAM"
746 ;;
747 esac
748 ;;
749
750 grub-yeeloong)
751 run_grub_install --target=mipsel-loongson
752 ;;
753
754 grub-xen)
755 # Install for x86_64 regardless of arch, since a 32-bit userspace can still boot with a 64-bit kernel.
756 mkdir -p /boot/xen
757 run_grub_install --target=x86_64-xen
758 case $(dpkg --print-architecture) in
759 i386)
760 run_grub_install --target=i386-xen
761 ;;
762 esac
763 # Similarly, the PVH boot loader is usable regardless of arch.
764 run_grub_install --target=i386-xen_pvh
765 ;;
766 esac
767
768 # If grub.cfg has been generated, update it.
769 if test -e /boot/grub/grub.cfg && ! running_in_container; then
770 update-grub 3>&-
771 fi
772 ;;
773 abort-upgrade|abort-remove|abort-deconfigure)
774 ;;
775 *)
776 echo "postinst called with unknown argument \`$1'" >&2
777 exit 1
778 ;;
779 esac
780
781 # dh_installdeb will replace this with shell code automatically
782 # generated by other debhelper scripts.
783
784 #DEBHELPER#
785
786 exit 0