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