4 # Apply a sed expression using extended regular expressions in place to a
5 # file, if it exists. The file is the first argument in order to work
6 # neatly with apply_conf_tweaks.
14 # Substitute a debconf answer into a configuration file with the syntax of
16 merge_debconf_into_conf
()
18 local tmpfile
; tmpfile
="$1"
19 local setting
; setting
="$2"
20 local template
; template
="$3"
23 local value
; value
="$(echo "$RET" | sed -e 's,[$`"\
],\\&,g
')"
24 if grep -q "^${setting}=" "$tmpfile"; then
25 value="$(echo "$value" | sed -e 's
,[\@
],\\&,g
')"
26 sed -i -re "s@^(${setting}=).*@\1\"${value}\"@" "$tmpfile"
29 echo "${setting}=\"${value}\"" >> "$tmpfile"
33 # Apply the same configuration tweak to multiple files.
36 local files; files="$1"
41 for file in $files; do
42 if [ -e "$file" ]; then
50 if [ ! -x /usr/share/lupin-support/grub-mkimage ] || \
51 ! /usr/share/lupin-support/grub-mkimage --test; then
55 local bootdev="$(grub-probe --target=device /boot)" || true
58 /dev/loop/*|/dev/loop[0-9])
59 loop_file="$(losetup "$bootdev" | sed -e "s/^[^(]*(\([^)]\+\)).*/\1/")"
60 # If it's loop-mounted from another device
, it isn
't Wubi.
71 # This only works on a Linux system with udev running. This is probably the
72 # vast majority of systems where we need any of this, though, and we fall
73 # back reasonably gracefully if we don't have it.
79 if [ "$cached_available_ids" ]; then
80 echo "$cached_available_ids"
84 [ -d /dev
/disk
/by-id
] ||
return
85 cached_available_ids
="$(
86 for path in /dev/disk/by-id/*; do
87 [ -e "$path" ] || continue
88 printf '%s %s\n' "$path" "$
(readlink
-f "$path")"
89 done | sort -k2 -s -u | cut -d' ' -f1
91 echo "$cached_available_ids"
94 # Returns non-zero and no output if no mapping can be found.
98 for id
in $
(available_ids
); do
99 if [ "$(readlink -f "$id")" = "$(readlink -f "$1")" ]; then
104 # Fall back to the plain device name if there's no by-id link for it.
115 for id
in $
(available_ids
); do
127 for id
in $
(available_ids
); do
128 if [ "$id" != "$1" ] && [ "${id%-part*}" = "$1" ]; then
129 ids
="${ids:+$ids }$id"
135 # In order to determine whether we accidentally ran grub-install without
136 # upgrade-from-grub-legacy on versions older than 1.98+20100617-1, we need
137 # to be able to scan a disk to determine whether GRUB 2 was installed in its
138 # boot sector. This is specific to i386-pc (but that's the only platform
142 if ! dd if="$1" bs
=512 count
=1 2>/dev
/null |
grep -aq GRUB
; then
143 # No version of GRUB is installed.
147 # The GRUB boot sector always starts with a JMP instruction.
148 initial_jmp
="$(dd if="$1" bs=2 count=1 2>/dev/null | od -Ax -tx1 | \
149 head -n1 | cut -d' ' -f2,3)"
150 [ "$initial_jmp" ] ||
return 1
151 initial_jmp_opcode
="${initial_jmp%% *}"
152 [ "$initial_jmp_opcode" = eb
] ||
return 1
153 initial_jmp_operand
="${initial_jmp#* }"
154 case $initial_jmp_operand in
156 # I believe this covers all versions of GRUB 2 up to the package
157 # version where we gained a more explicit mechanism. GRUB Legacy
158 # always had 48 here.
169 local num_sectors sector_size size
170 # Try to find out the size without relying on a partitioning tool being
171 # installed. This isn't too hard on Linux 2.6 with sysfs, but we have to
172 # try a couple of variants on detection of the sector size.
173 if [ -e "$1/size" ]; then
174 num_sectors
="$(cat "$1/size
")"
176 if [ -e "$1/queue/logical_block_size" ]; then
177 sector_size
="$(cat "$1/queue
/logical_block_size
")"
178 elif [ -e "$1/queue/hw_sector_size" ]; then
179 sector_size
="$(cat "$1/queue
/hw_sector_size
")"
181 size
="$(expr "$num_sectors" \* "$sector_size" / 1000 / 1000)"
183 [ "$size" ] || size
='???'
190 local num_sectors sector_size size
=
192 if num_sectors
="$(camcontrol readcap "$1" -q -s -N)"; then
193 sector_size
="$(camcontrol readcap "$1" -q -b)"
194 size
="$(expr "$num_sectors" \* "$sector_size" / 1000 / 1000)"
197 [ "$size" ] || size
='???'
203 if which udevadm
>/dev
/null
2>&1; then
208 # Returns value in $RET, like a debconf command.
211 local disk id sysfs_path disk_basename size model
218 sysfs_path
="$(maybe_udevadm info -n "$disk" -q path)"
219 if [ -z "$sysfs_path" ]; then
220 sysfs_path
="/block/$(printf %s "${disk#/dev/}" | sed 's,/,!,g')"
222 size
="$(sysfs_size "/sys
$sysfs_path")"
224 model
="$(maybe_udevadm info -n "$disk" -q property | sed -n 's/^ID_MODEL=//p')"
225 if [ -z "$model" ]; then
226 model
="$(maybe_udevadm info -n "$disk" -q property | sed -n 's/^DM_NAME=//p')"
227 if [ -z "$model" ]; then
228 model
="$(maybe_udevadm info -n "$disk" -q property | sed -n 's/^MD_NAME=//p')"
229 if [ -z "$model" ] && which dmsetup
>/dev
/null
2>&1; then
230 model
="$(dmsetup info -c --noheadings -o name "$disk" 2>/dev/null || true)"
236 disk_basename
=$
(basename "$disk")
237 size
="$(camcontrol_size "$disk_basename")"
238 model
="$(camcontrol inquiry "$disk_basename" | sed -ne "s
/^pass0
: <\
([^
>]*\
)>.
*/\
1/p
")"
242 [ "$model" ] || model
='???'
244 db_subst grub-pc
/disk_description DEVICE
"$disk"
245 db_subst grub-pc
/disk_description SIZE
"$size"
246 db_subst grub-pc
/disk_description MODEL
"$model"
247 db_metaget grub-pc
/disk_description description
250 # Returns value in $RET, like a debconf command.
253 local disk part id path sysfs_path diskbase partbase size
259 sysfs_path
="$(maybe_udevadm info -n "$part" -q path)"
260 if [ -z "$sysfs_path" ]; then
261 diskbase
="${disk#/dev/}"
262 diskbase
="$(printf %s "$diskbase" | sed 's,/,!,g')"
263 partbase
="${part#/dev/}"
264 partbase
="$(printf %s "$partbase" | sed 's,/,!,g')"
265 sysfs_path
="/block/$diskbase/$partbase"
267 size
="$(sysfs_size "/sys
$sysfs_path")"
269 db_subst grub-pc
/partition_description DEVICE
"$part"
270 db_subst grub-pc
/partition_description SIZE
"$size"
271 db_subst grub-pc
/partition_description PATH
"$path"
272 db_metaget grub-pc
/partition_description description
277 local last_partition path partition partition_id
280 for path
in / /boot
/boot
/grub
; do
281 partition
="$(grub-probe -t device "$path" || true)"
282 if [ -z "$partition" ] ||
[ "$partition" = "$last_partition" ]; then
285 partition_id
="$(device_to_id "$partition" || true)"
286 echo "$path:$partition_id"
287 last_partition
="$partition"
293 local relpath boot_mountpoint
295 relpath
="$(grub-mkrelpath "$1")"
296 boot_mountpoint
="${1#$relpath}"
297 echo "${boot_mountpoint:-/}"
302 for x
in /etc
/default
/grub
/etc
/default
/grub.d
/*.cfg
; do
307 if [ "$(eval echo "\
${$1+set}")" = set ]; then
314 running_in_container
()
316 type systemd-detect-virt
>/dev
/null
2>&1 && systemd-detect-virt
--quiet --container
320 db_get grub
2/update_nvram
321 if [ "$RET" = false
]; then
328 if ! grub-install $@
; then
329 echo "Failed: grub-install $@" >&2
330 echo "WARNING: Bootloader is not properly installed, system may not be bootable" >&2
336 .
/usr
/share
/debconf
/confmodule
338 devicemap_regenerated
=
340 if egrep -q '^[[:space:]]*post(inst|rm)_hook[[:space:]]*=[[:space:]]*(/sbin/|/usr/sbin/)?update-grub' /etc
/kernel-img.conf
2>/dev
/null
; then
341 echo 'Removing update-grub hooks from /etc/kernel-img.conf in favour of' >&2
342 echo '/etc/kernel/ hooks.' >&2
343 sed -ri /etc
/kernel-img.conf
-e '\%^[[:space:]]*post(inst|rm)_hook[[:space:]]*=[[:space:]]*(/sbin/|/usr/sbin/)?update-grub%d'
350 if test -e /boot
/grub
/device.map
&& ! test -e /boot
/grub
/core.img
&& \
351 ! test -e /boot
/grub
/@FIRST_CPU_PLATFORM@
/core.img
; then
352 # Looks like your device.map was generated by GRUB Legacy, which
353 # used to generate broken device.map (see #422851). Avoid the risk
354 # by regenerating it.
355 grub-mkdevicemap
--no-floppy
356 devicemap_regenerated
=1
361 if test -z "$devicemap_regenerated" && \
362 dpkg
--compare-versions "$2" lt-nl
1.99~
20101210-2 && \
363 grep -qs /md-uuid-
/boot
/grub
/device.map
; then
364 echo "Removing MD devices from device.map, since the BIOS cannot read from these." >&2
365 sed -i '/\/md-uuid-/d' /boot
/grub
/device.map
368 tmp_default_grub
="$(mktemp -t grub.XXXXXXXXXX)"
369 trap "rm -f ${tmp_default_grub}" EXIT
370 cp -p /usr
/share
/grub
/default
/grub
${tmp_default_grub}
372 # Apply configuration from debconf to both the template configuration
373 # file (so that any ucf conflict resolution is shown with the configured
374 # values in place) and to /etc/default/grub (in order that debconf
375 # changes are effective even if we have to use UCF_FORCE_CONFFOLD=1).
377 # The config script takes care to read current values from
378 # /etc/default/grub before possibly prompting the user to change them,
379 # so this should comply with policy's strictures on configuration file
380 # handling: the debconf prompts constitute an explicit administrator
381 # request to change the configuration file.
383 # If the administrator changes their answers to any of these debconf
384 # questions in the same run as a change to the template, then they'll
385 # get a spurious ucf conflict. To fix this, we'd need to keep track of
386 # the old answers, substitute the old answers into $tmp_default_grub,
387 # and substitute the new answers into /etc/default/grub. Fortunately,
388 # debconf won't normally ask these questions again during an upgrade, so
389 # this should be rare in practice.
391 conf_files
="$tmp_default_grub /etc/default/grub"
393 apply_conf_tweaks
"$conf_files" merge_debconf_into_conf GRUB_CMDLINE_LINUX grub
2/linux_cmdline
394 apply_conf_tweaks
"$conf_files" merge_debconf_into_conf GRUB_CMDLINE_LINUX_DEFAULT grub
2/linux_cmdline_default
398 apply_conf_tweaks
"$conf_files" merge_debconf_into_conf GRUB_TIMEOUT grub-pc
/timeout
399 apply_conf_tweaks
"$conf_files" sed_conf
's/^(GRUB_TIMEOUT=)"([0-9][0-9]*)"/\1\2/'
400 db_get grub-pc
/hidden_timeout
401 if [ "$RET" = false
]; then
402 apply_conf_tweaks
"$conf_files" sed_conf
's/^GRUB_HIDDEN_TIMEOUT=/#&/'
407 # If the template configuration file hasn't changed, then no conflict is
408 # possible. ucf can't figure this out for itself since we apply
409 # debconf-based customisations on top of the template configuration
411 if [ -e /var
/lib
/grub
/ucf
/grub.previous
] && \
412 cmp -s /usr
/share
/grub
/default
/grub
/var
/lib
/grub
/ucf
/grub.previous
&& \
413 [ -e /etc
/default
/grub
]; then
414 ucf_env
=UCF_FORCE_CONFFOLD
=1
419 env
$ucf_env ucf
--three-way --debconf-ok --sum-file=/usr
/share
/grub
/default
/grub.
md5sum "$tmp_default_grub" /etc
/default
/grub
420 cp -a /usr
/share
/grub
/default
/grub
/var
/lib
/grub
/ucf
/grub.previous
421 package
="$(ucfq --with-colons /etc/default/grub | cut -d : -f 2)"
422 if echo $package |
grep -q "^grub-" ; then
423 ucfr
--force @PACKAGE@
/etc
/default
/grub
425 ucfr @PACKAGE@
/etc
/default
/grub
432 if test -e /boot
/grub
/stage2
&& test -e /boot
/grub
/menu.lst
&& \
433 ! test -e /boot
/grub
/grub2-installed
&& \
434 test -z "$UPGRADE_FROM_GRUB_LEGACY"; then
435 # Unfortunately, it's still possible that the user upgraded fully
436 # to GRUB 2 in some way other than running
437 # upgrade-from-grub-legacy; perhaps they ran grub-install by hand
438 # for some reason. It's really quite difficult to detect this
439 # situation, because the only difference between this and a
440 # working chainloaded setup is that in this case grub-setup has
441 # been run. So, to try to tell the difference, we scan the boot
442 # sectors of all disks for a GRUB 2 boot sector. Hopefully this
443 # won't cause too much to explode, since I can't think of a better
446 for disk
in $
(all_disks
); do
447 if scan_grub2
"$disk"; then
448 grub2_disks
="${grub2_disks:+$grub2_disks }$(readlink -f "$disk")"
451 if [ "$grub2_disks" ]; then
452 # No || true here; it's vital that the user sees this, and it's
453 # better to throw an error than to do the wrong thing.
454 db_subst grub-pc
/mixed_legacy_and_grub2 DISKS
"$grub2_disks"
455 db_fset grub-pc
/mixed_legacy_and_grub2 seen false
456 db_input critical grub-pc
/mixed_legacy_and_grub2
458 db_get grub-pc
/mixed_legacy_and_grub2
459 if [ "$RET" = true
]; then
460 db_reset grub-pc
/install_devices
461 UPGRADE_FROM_GRUB_LEGACY
=1
463 # Fall through to normal installation logic.
468 # Make sure that Wubi users never see confusing device prompts.
469 # Wubi is a very specialised hack that does complicated things with
470 # grub-install diversions to create an image that's chained from the
471 # Windows boot loader to boot an operating system from a file on a
472 # Windows file system. In these circumstances, prompting for where
473 # to install GRUB is not going to help anyone.
474 wubi_device
="$(get_wubi_device)" || true
475 if [ "$wubi_device" ]; then
476 db_set grub-pc
/install_devices
"$wubi_device"
477 db_fset grub-pc
/install_devices seen true
480 if test -e /boot
/grub
/stage2
&& test -e /boot
/grub
/menu.lst
&& \
481 ! test -e /boot
/grub
/grub2-installed
&& \
482 test -z "$UPGRADE_FROM_GRUB_LEGACY"; then
483 db_get grub-pc
/chainload_from_menu.lst
485 # Create core.img (but do not risk writing to MBR).
486 # Using grub-probe instead of "(hd0)" avoids (UUID=) hack slowness
487 # in case /boot/grub is not on (hd0) in device.map.
488 echo "Generating core.img" >&2
489 grub-install
--target=i386-pc
--no-floppy --grub-setup=/bin
/true
"$(grub-probe -t drive /boot/grub)" > /dev
/null
491 # Update menu.lst to reflect that:
492 # - core.img is present now
493 # - core.img has to be the first option
494 echo "Saving menu.lst backup in /boot/grub/menu.lst_backup_by_grub2_postinst" >&2
495 cp /boot
/grub
/menu.lst
{,_backup_by_grub2_postinst
}
496 echo "Running update-grub Legacy to hook our core.img in it" >&2
497 LET_US_TRY_GRUB_2
=true
/usr
/lib
/grub-legacy
/update-grub
2>&1 |
sed -e "s/^/ /g" >&2
498 # We just hooked GRUB 2 in menu.lst; then also generate grub.cfg.
499 touch /boot
/grub
/grub.cfg
501 elif running_in_container
; then
502 # Skip grub-install in containers.
504 elif test -z "$2" ||
test -e /boot
/grub
/core.img || \
505 test -e /boot
/grub
/@FIRST_CPU_PLATFORM@
/core.img || \
506 test "$UPGRADE_FROM_GRUB_LEGACY" ||
test "$wubi_device"; then
507 question
=grub-pc
/install_devices
509 device_map
="$(grub-mkdevicemap -m - | grep -v '^(fd[0-9]\+)' || true)"
510 devices
="$(echo "$device_map" | cut -f2)"
511 db_get grub-pc
/install_devices
513 for device
in $RET; do
514 if [ ! -e "${device%,}" ]; then
519 if [ "$valid" = 0 ]; then
520 question
=grub-pc
/install_devices_disks_changed
522 db_set
"$question" "$RET"
523 db_fset
"$question" seen false
524 db_fset grub-pc
/install_devices_empty seen false
530 partitions
="$(usable_partitions)"
531 for device
in $devices; do
532 disk_id
="$(device_to_id "$device" || true)"
533 if [ "$disk_id" ]; then
534 ids
="${ids:+$ids, }$disk_id"
535 describe_disk
"$(readlink -f "$device")" "$disk_id"
536 RET
="$(printf %s "$RET" | sed 's/,/\\,/g')"
537 descriptions
="${descriptions:+$descriptions, }$RET"
538 for partition_pair
in $partitions; do
539 partition_id
="${partition_pair#*:}"
540 if [ "${partition_id#$disk_id-part}" != "$partition_id" ]; then
541 ids
="${ids:+$ids, }$partition_id"
542 describe_partition
"$(readlink -f "$device")" "$(readlink -f "$partition_id")" "$partition_id" "$(get_mountpoint "${partition_pair%%:*}")"
543 RET
="$(printf %s "$RET" | sed 's/,/\\,/g')"
544 descriptions
="${descriptions:+$descriptions, }$RET"
549 # Some "partitions" may in fact be at the disk level, e.g. RAID.
550 # List these as well if they haven't already been listed.
551 for partition_pair
in $partitions; do
552 partition_id
="${partition_pair#*:}"
553 if [ "${partition_id#*-part}" = "$partition_id" ]; then
555 ", $partition_id, ") ;;
557 ids
="${ids:+$ids, }$partition_id"
558 describe_disk
"$(readlink -f "$partition_id")" "$partition_id"
559 RET
="$(printf %s "$RET" | sed 's/,/\\,/g')"
560 descriptions
="${descriptions:+$descriptions, }$RET"
565 db_subst
"$question" RAW_CHOICES
"$ids"
566 db_subst
"$question" CHOICES
"$descriptions"
567 db_input
"$priority" "$question" || true
571 for i
in `echo $RET | sed -e 's/, / /g'` ; do
572 real_device
="$(readlink -f "$i")"
573 if grub-install
--target=i386-pc
--force --no-floppy $real_device ; then
574 # We just installed GRUB 2; then also generate grub.cfg.
575 touch /boot
/grub
/grub.cfg
577 failed_devices
="$failed_devices $real_device"
581 if [ "$question" != grub-pc
/install_devices
] && [ "$RET" ]; then
582 # XXX cjwatson 2019-02-26: The description of
583 # grub-pc/install_devices_disks_changed ought to explain that
584 # selecting no devices will leave the configuration unchanged
585 # so that you'll be prompted again next time, but it's a bit
586 # close to the Debian 10 release to be introducing new
587 # translatable text. For now, it should be sufficient to
588 # avoid losing configuration data.
589 db_set grub-pc
/install_devices
"$RET"
590 db_fset grub-pc
/install_devices seen true
593 if [ "$failed_devices" ]; then
594 if [ "$UPGRADE_FROM_GRUB_LEGACY" ]; then
595 db_subst grub-pc
/install_devices_failed_upgrade FAILED_DEVICES
"$failed_devices"
596 db_fset grub-pc
/install_devices_failed_upgrade seen false
597 if db_input critical grub-pc
/install_devices_failed_upgrade
; then
599 db_get grub-pc
/install_devices_failed_upgrade
600 if [ "$RET" = true
]; then
601 db_fset
"$question" seen false
602 db_fset grub-pc
/install_devices_failed_upgrade seen false
608 exit 1 # noninteractive
611 db_subst grub-pc
/install_devices_failed FAILED_DEVICES
"$failed_devices"
612 db_fset grub-pc
/install_devices_failed seen false
613 if db_input critical grub-pc
/install_devices_failed
; then
615 db_get grub-pc
/install_devices_failed
616 if [ "$RET" = true
]; then
619 db_fset
"$question" seen false
620 db_fset grub-pc
/install_devices_failed seen false
624 break # noninteractive
630 if [ -z "$RET" ]; then
631 # Reset the seen flag if the current answer is false, since
632 # otherwise we'll loop with no indication of why.
633 db_get grub-pc
/install_devices_empty
634 if [ "$RET" = false
]; then
635 db_fset grub-pc
/install_devices_empty seen false
637 if db_input critical grub-pc
/install_devices_empty
; then
639 db_get grub-pc
/install_devices_empty
640 if [ "$RET" = true
]; then
643 db_fset
"$question" seen false
644 db_fset grub-pc
/install_devices_empty seen false
647 break # noninteractive
655 # /boot/grub/ has more chances of being accessible by GRUB
656 for i
in /usr
/share
/grub
/unicode.pf2
; do
662 if [ "$fix_mixed_system" ]; then
663 # These never contain any valuable information, and they aren't
664 # useful for boot any more, since we just overwrote MBR/PBR.
665 rm -f /boot
/grub
/{{xfs
,reiserfs
,e2fs
,fat
,jfs
,minix
}_stage1_5
,stage
{1,2}}
666 # Remove marker file used to indicate that grub-install was run
667 # rather than upgrade-from-grub-legacy. Since stage2 has been
668 # removed, we don't need this any more.
669 rm -f /boot
/grub
/grub2-installed
673 grub-efi-ia32|grub-efi-amd64|grub-efi-ia64|grub-efi-arm|grub-efi-arm64
)
674 bootloader_id
="$(config_item GRUB_DISTRIBUTOR | tr A-Z a-z | \
676 case $bootloader_id in
677 kubuntu
) bootloader_id
=ubuntu
;;
678 devuan
) bootloader_id
=debian
;;
680 if [ "$bootloader_id" ] && [ -d "/boot/efi/EFI/$bootloader_id" ]; then
682 grub-efi-ia32
) target
=i386-efi
;;
683 grub-efi-amd64
) target
=x86_64-efi
;;
684 grub-efi-ia64
) target
=ia64-efi
;;
685 grub-efi-arm
) target
=arm-efi
;;
686 grub-efi-arm64
) target
=arm64-efi
;;
688 db_get grub
2/force_efi_extra_removable
689 if [ "$RET" = true
]; then
690 FORCE_EXTRA_REMOVABLE
="--force-extra-removable"
692 NO_NVRAM
="$(no_nvram_arg)"
693 run_grub_install
--target="$target" "$FORCE_EXTRA_REMOVABLE" "$NO_NVRAM"
696 # /boot/grub/ has more chances of being accessible by GRUB
697 for i
in /usr
/share
/grub
/unicode.pf2
; do
703 if type update-secureboot-policy
>/dev
/null
2>&1; then
704 update-secureboot-policy || true
709 case $
(dpkg
--print-architecture) in
710 powerpc|ppc64|ppc64el
)
711 # Output may be empty; if so, just update the core image but
712 # don't install it to any PReP partition.
713 prep_bootdev
="$(/usr/lib/grub/powerpc-ieee1275/prep-bootdev)"
714 NO_NVRAM
="$(no_nvram_arg)"
715 run_grub_install
--target=powerpc-ieee1275
$prep_bootdev "$NO_NVRAM"
721 run_grub_install
--target=mipsel-loongson
725 # Install for x86_64 regardless of arch, since a 32-bit userspace can still boot with a 64-bit kernel.
727 run_grub_install
--target=x86_64-xen
728 case $
(dpkg
--print-architecture) in
730 run_grub_install
--target=i386-xen
733 # Similarly, the PVH boot loader is usable regardless of arch.
734 run_grub_install
--target=i386-xen_pvh
738 # If grub.cfg has been generated, update it.
739 if test -e /boot
/grub
/grub.cfg
&& ! running_in_container
; then
743 abort-upgrade|abort-remove|abort-deconfigure
)
746 echo "postinst called with unknown argument \`$1'" >&2
751 # dh_installdeb will replace this with shell code automatically
752 # generated by other debhelper scripts.