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