forked from RehabMan/HP-ProBook-4x30s-DSDT-Patch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mount_efi.sh
executable file
·87 lines (77 loc) · 3.6 KB
/
mount_efi.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
#set -x
# Note: Based on CloverPackage MountESP script.
if [[ "$1" == "" ]]; then
DestVolume=/
else
DestVolume="$1"
fi
# find whole disk for the destination volume
DiskDevice=$(LC_ALL=C diskutil info "$DestVolume" 2>/dev/null | sed -n 's/.*Part [oO]f Whole: *//p')
if [[ -z "$DiskDevice" ]]; then
echo "Error: Not able to find volume with the name \"$DestVolume\""
exit 1
fi
# check if target volume is a logical Volume instead of physical
if [[ "$(echo $(LC_ALL=C diskutil list | grep -i 'Logical Volume' | awk '{print tolower($0)}'))" == *"logical volume"* ]]; then
# ok, we have a logical volume somewhere.. so that can assume that we can use "diskutil cs"
LC_ALL=C diskutil cs info $DiskDevice > /dev/null 2>&1
if [[ $? -eq 0 ]] ; then
# logical volumes does not have an EFI partition (or not suitable for us?)
# find the partition uuid
UUID=$(LC_ALL=C diskutil info "${DiskDevice}" 2>/dev/null | sed -n 's/.*artition UUID: *//p')
# with the partition uuid we can find the real disk in in diskutil list output
if [[ -n "$UUID" ]]; then
realDisk=$(LC_ALL=C diskutil list | grep -B 1 "$UUID" | grep -i 'logical volume' | awk '{print $4}' | sed -e 's/,//g' | sed -e 's/ //g')
if [[ -n "$realDisk" ]]; then
DiskDevice=$(LC_ALL=C diskutil info "${realDisk}" 2>/dev/null | sed -n 's/.*Part [oO]f Whole: *//p')
fi
fi
fi
fi
# check if target volume is APFS, and therefore part of an APFS container
if [[ "$(echo $(LC_ALL=C diskutil list "$DiskDevice" | grep -i 'APFS Container Scheme' | awk '{print tolower($0)}'))" == *"apfs container scheme"* ]]; then
# ok, this disk is an APFS partition, extract physical store device
realDisk=$(LC_ALL=C diskutil list "$DiskDevice" 2>/dev/null | sed -n 's/.*Physical Store *//p')
DiskDevice=$(LC_ALL=C diskutil info "$realDisk" 2>/dev/null | sed -n 's/.*Part [oO]f Whole: *//p')
fi
PartitionScheme=$(LC_ALL=C diskutil info "$DiskDevice" 2>/dev/null | sed -nE 's/.*(Partition Type|Content \(IOContent\)): *//p')
# Check if the disk is an MBR disk
if [[ "$PartitionScheme" == "FDisk_partition_scheme" ]]; then
echo "Error: Volume \"$DestVolume\" is part of an MBR disk"
exit 1
fi
# Check if not GPT
if [[ "$PartitionScheme" != "GUID_partition_scheme" ]]; then
echo "Error: Volume \"$DestVolume\" is not on GPT disk or APFS container"
exit 1
fi
# Find the associated EFI partition on DiskDevice
diskutil list -plist "/dev/$DiskDevice" 2>/dev/null >/tmp/org_rehabman_diskutil.plist
for ((part=0; 1; part++)); do
content=`/usr/libexec/PlistBuddy -c "Print :AllDisksAndPartitions:0:Partitions:$part:Content" /tmp/org_rehabman_diskutil.plist 2>&1`
if [[ "$content" == *"Does Not Exist"* ]]; then
echo "Error: cannot locate EFI partition for $DestVolume"
exit 1
fi
if [[ "$content" == "EFI" ]]; then
EFIDevice=`/usr/libexec/PlistBuddy -c "Print :AllDisksAndPartitions:0:Partitions:$part:DeviceIdentifier" /tmp/org_rehabman_diskutil.plist 2>&1`
break
fi
done
# should not happen
if [[ -z "$EFIDevice" ]]; then
echo "Error: unable to determine EFIDevice from $DiskDevice"
exit 1
fi
# Get the EFI mount point if the partition is currently mounted
code=0
EFIMountPoint=$(LC_ALL=C diskutil info "$EFIDevice" 2>/dev/null | sed -n 's/.*Mount Point: *//p')
if [[ -z "$EFIMountPoint" ]]; then
# try to mount the EFI partition
diskutil mount /dev/$EFIDevice >/dev/null 2>&1
EFIMountPoint=$(LC_ALL=C diskutil info "$EFIDevice" 2>/dev/null | sed -n 's/.*Mount Point: *//p')
code=$?
fi
echo $EFIMountPoint
exit $code