update upstream to 9084a3cbc0a55422beea4a55b530c1f03a910617 feb 2024
[automated-distro-installer] / fai / config / scripts / GRUB_PC / 10-setup
1 #! /bin/bash
2 # support for GRUB version 2
3
4 error=0; trap 'error=$(($?>$error?$?:$error))' ERR # save maximum error code
5
6 # do only execute for Debian and similar distros
7 if ! ifclass DEBIAN ; then
8 exit 0
9 fi
10
11 set -a
12
13 # do not set up grub during dirinstall
14 if [ "$FAI_ACTION" = "dirinstall" ] ; then
15 exit 0
16 fi
17 # during softupdate use this file
18 [ -r $LOGDIR/disk_var.sh ] && . $LOGDIR/disk_var.sh
19
20 if [ -z "$BOOT_DEVICE" ]; then
21 exit 189
22 fi
23
24 # disable os-prober because of #802717
25 ainsl /etc/default/grub 'GRUB_DISABLE_OS_PROBER=true'
26
27 # efivars may still be mounted from the host system during fai-diskimage
28 if [ -d $target/sys/firmware/efi/efivars ]; then
29 umount $target/sys/firmware/efi/efivars
30 fi
31
32 # skip the rest, if not an initial installation
33 if [ $FAI_ACTION != "install" ]; then
34 $ROOTCMD update-grub
35 exit $error
36 fi
37
38 get_stable_devname() {
39
40 local _DEV="$1"
41 local i
42 declare -a _RES
43
44 # prefer SCSI over ATA over WWN over path
45 # do not use by-path
46
47 for i in $($ROOTCMD udevadm info -r --query=symlink "$_DEV"); do
48 if [[ "$i" =~ /by-id/scsi ]]; then
49 _RES[10]="$i"
50 elif [[ "$i" =~ /by-id/ata ]]; then
51 _RES[20]="$i"
52 elif [[ "$i" =~ /by-id/wwn ]]; then
53 _RES[99]="$i"
54 fi
55 done
56
57 echo "${_RES[@]::1}"
58 }
59
60 # handle /boot in lvm-on-md
61 _bdev=$(readlink -f $BOOT_DEVICE)
62 if [ "${_bdev%%-*}" = "/dev/dm" ]; then
63 BOOT_DEVICE=$( lvs --noheadings -o devices $BOOT_DEVICE | sed -e 's/^*\([^(]*\)(.*$/\1/' )
64 fi
65
66 # Check if RAID is used for the boot device
67 if [[ $BOOT_DEVICE =~ '/dev/md' ]]; then
68 raiddev=${BOOT_DEVICE#/dev/}
69 # install grub on all members of RAID
70 for device in $(LC_ALL=C perl -ne 'if(/^'$raiddev'\s.+raid\d+\s(.+)/){ $_=$1; s/\d+\[\d+\]//g; s/(nvme.+?)p/$1/g; print }' /proc/mdstat); do
71 pdevice=$(get_stable_devname /dev/$device)
72 if [ -z "$pdevice" ]; then
73 # if we cannot find a persistent name (for e.g. in a VM) use old name
74 pdevice="/dev/$device"
75 fi
76 mbrdevices+="$pdevice, "
77 echo Installing grub on /dev/$device = $pdevice
78 $ROOTCMD grub-install --no-floppy "/dev/$device"
79 done
80 # remove last ,
81 mbrdevices=${mbrdevices%, }
82 else
83 mbrdevices=$(get_stable_devname $BOOT_DEVICE)
84 if [ -z "$mbrdevices" ]; then
85 # if we cannot find a persistent name (for e.g. in a VM) use old name
86 mbrdevices=$BOOT_DEVICE
87 fi
88 echo "Installing grub on $BOOT_DEVICE = $mbrdevices"
89 $ROOTCMD grub-install --no-floppy "$mbrdevices"
90 fi
91
92 echo "grub-pc grub-pc/install_devices multiselect $mbrdevices" | $ROOTCMD debconf-set-selections
93 $ROOTCMD dpkg-reconfigure grub-pc
94 exit $error