linux-boot-probe: mounted/40grub2 add tests for handling escaped $ " \
[os-prober.git] / linux-boot-prober
1 #!/bin/sh
2 . /usr/share/os-prober/common.sh
3
4 set -e
5
6 newns "$@"
7 require_tmpdir
8
9 grep "^/dev/" /proc/mounts | parse_proc_mounts >"$OS_PROBER_TMP/mounted-map" || true
10
11 partition="$1"
12
13 if [ -z "$partition" ]; then
14         echo "usage: linux-boot-prober partition" >&2
15         exit 1
16 fi
17
18 if ! mapped="$(mapdevfs "$partition")"; then
19         log "Device '$partition' does not exist; skipping"
20         continue
21 fi
22
23 if ! grep -q "^$mapped " "$OS_PROBER_TMP/mounted-map"; then
24         for test in /usr/lib/linux-boot-probes/*; do
25                 debug "running $test"
26                 if [ -x $test ] && [ -f $test ]; then
27                         if $test "$partition"; then
28                                 debug "linux detected by $test"
29                                 break
30                         fi
31                 fi
32         done
33 else
34         mpoint=$(grep "^$mapped " "$OS_PROBER_TMP/mounted-map" | head -n1 | cut -d " " -f 2)
35         mpoint="$(unescape_mount "$mpoint")"
36         if [ "$mpoint" != "/target/boot" ] && [ "$mpoint" != "/target" ] && [ "$mpoint" != "/" ]; then
37                 type=$(grep "^$mapped " "$OS_PROBER_TMP/mounted-map" | head -n1 | cut -d " " -f 3)
38                 if ! grep -q " $mpoint/boot " "$OS_PROBER_TMP/mounted-map"; then
39                         linux_mount_boot "$partition" "$mpoint"
40                         bootpart="${mountboot%% *}"
41                         bootmounted="${mountboot#* }"
42                 else
43                         bootpart="$partition"
44                         bootmounted=0
45                 fi
46                 for test in /usr/lib/linux-boot-probes/mounted/*; do
47                         if [ -f $test ] && [ -x $test ]; then
48                                 debug "running $test on mounted $partition"
49                                 if $test "$partition" "$bootpart" "$mpoint" "$type"; then
50                                         debug "$test succeeded"
51                                         break
52                                 fi
53                         fi
54                 done
55                 if [ "$bootmounted" = 1 ]; then
56                         if ! umount "$mpoint/boot"; then
57                                 warn "failed to umount $mpoint/boot"
58                         fi
59                 fi
60         fi
61 fi