update to 72d79e141d3aaf430bb44f6036a2980f3f5865bd
[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 set -a
7
8 # do not set up grub during dirinstall
9 if [ "$FAI_ACTION" = "dirinstall" ] ; then
10 exit 0
11 fi
12 # during softupdate use this file
13 [ -r $LOGDIR/disk_var.sh ] && . $LOGDIR/disk_var.sh
14
15 if [ -z "$BOOT_DEVICE" ]; then
16 exit 189
17 fi
18
19 # disable os-prober because of #802717
20 ainsl /etc/default/grub 'GRUB_DISABLE_OS_PROBER=true'
21
22 # skip the rest, if not an initial installation
23 if [ $FAI_ACTION != "install" ]; then
24 $ROOTCMD update-grub
25 exit $error
26 fi
27
28 get_stable_devname() {
29
30 local _DEV="$1"
31 local i
32 declare -a _RES
33
34 # prefer SCSI over ATA over WWN over path
35 # do not use by-path
36
37 for i in $($ROOTCMD udevadm info -r --query=symlink "$_DEV"); do
38 if [[ "$i" =~ /by-id/scsi ]]; then
39 _RES[10]="$i"
40 elif [[ "$i" =~ /by-id/ata ]]; then
41 _RES[20]="$i"
42 elif [[ "$i" =~ /by-id/wwn ]]; then
43 _RES[99]="$i"
44 fi
45 done
46
47 echo "${_RES[@]::1}"
48 }
49
50 # handle /boot in lvm-on-md
51 _bdev=$(readlink -f $BOOT_DEVICE)
52 if [ "${_bdev%%-*}" = "/dev/dm" ]; then
53 BOOT_DEVICE=$( lvs --noheadings -o devices $BOOT_DEVICE | sed -e 's/^*\([^(]*\)(.*$/\1/' )
54 fi
55
56 # Check if RAID is used for the boot device
57 if [[ $BOOT_DEVICE =~ '/dev/md' ]]; then
58 raiddev=${BOOT_DEVICE#/dev/}
59 # install grub on all members of RAID
60 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
61 pdevice=$(get_stable_devname /dev/$device)
62 if [ -z "$pdevice" ]; then
63 # if we cannot find a persistent name (for e.g. in a VM) use old name
64 pdevice="/dev/$device"
65 fi
66 mbrdevices+="$pdevice, "
67 echo Installing grub on /dev/$device = $pdevice
68 $ROOTCMD grub-install --no-floppy "/dev/$device"
69 done
70 # remove last ,
71 mbrdevices=${mbrdevices%, }
72 else
73 mbrdevices=$(get_stable_devname $BOOT_DEVICE)
74 if [ -z "$mbrdevices" ]; then
75 # if we cannot find a persistent name (for e.g. in a VM) use old name
76 mbrdevices=$BOOT_DEVICE
77 fi
78 echo "Installing grub on $BOOT_DEVICE = $mbrdevices"
79 $ROOTCMD grub-install --no-floppy "$mbrdevices"
80 fi
81
82 echo "grub-pc grub-pc/install_devices multiselect $mbrdevices" | $ROOTCMD debconf-set-selections
83 $ROOTCMD dpkg-reconfigure grub-pc
84 exit $error