forked from kobaltcore/EFIClone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.sh
executable file
·115 lines (94 loc) · 3.64 KB
/
utils.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# EFI Partition Clone Script Utilities
function usage() {
[ -n "${1-}" ] && echo -e "$1\n"
echo "Usage: $0 [-n|--dry-run] [-h|--help] SOURCE DEST"
echo " -n, --dry-run Simulate a clone without actually modifying any files."
echo " -h, --help Display this help."
echo " SOURCE The source volume. This can be the source EFI volume, or a"
echo " sibling volume of the source EFI."
echo " DEST The destination volume. This can be the destination EFI volume,"
echo " or a sibling volume of the destination EFI."
echo ""
echo "Example: $0 --dry-run /Volumes/macOS /Volumes/BackUp"
[ -n "${1-}" ] && exit 1 || exit 0
}
function echo_log() {
echo "[`date`] - ${*}"
}
function display_notification() {
osascript -e "display notification \"${*}\" with title \"EFI Clone Script\""
}
function fail_gracefully() {
echo_log "$1 Exiting."
display_notification "$2 EFI Clone Script failed."
exit "${3:-1}"
}
function validate_param_count() {
if [[ "$1" != "$2" ]]; then
fail_gracefully "Parameter count of $2 is not supported." 'Unsupported set of parameters received.'
fi
}
function get_disk_number() {
diskutil info "$1" 2>/dev/null | grep 'Part of Whole' | rev | cut -d ' ' -f1 | rev
}
function get_core_storage_physical_disk_number() {
diskutil info "$1" | grep 'PV UUID' | rev | cut -d '(' -f1 | cut -d ')' -f2 | rev | cut -d 'k' -f2 | cut -d 's' -f1
}
function get_apfs_physical_disk_number() {
diskutil apfs list | grep -A 9 "Container $1 " | grep "APFS Physical Store" | rev | cut -d ' ' -f 1 | cut -d 's' -f 2 | cut -d 'k' -f 1 | rev
}
function get_efi_volume() {
diskutil list | grep "$1s" | grep "EFI" | rev | cut -d ' ' -f 1 | rev
}
function get_efi_partition() {
local volume_disk="$1"
local disk=$volume_disk
local EFIPartition="$(get_efi_volume "$disk")"
# If we don't find an EFI partition on the disk that was identified by the
# volume path, we check to see if it is a coreStorage volume and get the disk
# number from there.
if [[ "$EFIPartition" == "" ]]; then
disk='disk'"$(get_core_storage_physical_disk_number "$volume_disk")"
if [[ "$disk" == "disk" ]]; then
disk=$volume_disk
fi
EFIPartition="$(get_efi_volume "$disk")"
fi
# If we still don't have an EFI partition then we check to see if the
# volume_disk is an APFS volume and find its physical disk.
if [[ "$EFIPartition" == "" ]]; then
disk='disk'"$(get_apfs_physical_disk_number "$volume_disk")"
EFIPartition="$(get_efi_volume "$disk")"
fi
echo "$EFIPartition"
}
function get_disk_mount_point() {
diskutil info "$1" | grep 'Mount Point' | rev | cut -d ':' -f 1 | rev | awk '{$1=$1;print}'
}
function get_efi_directory_hash() {
find -s . -not -path '*/\.*' -type f \( ! -iname ".*" \) -print0 | xargs -0 shasum | shasum
}
function log_efi_directory_hash_details() {
find -s . -not -path '*/\.*' -type f \( ! -iname ".*" \) -print0 | xargs -0 shasum
}
function collect_efi_hash() {
local efi_mount_point="$1"
pushd "$efi_mount_point/" > /dev/null
EFIHash="$(get_efi_directory_hash "$efi_mount_point/EFI")"
log_efi_directory_hash_details "$efi_mount_point"
popd > /dev/null
echo "$EFIHash"
}
function get_system_boot_volume_name() {
system_profiler SPSoftwareDataType | grep 'Boot Volume' | rev | cut -d ':' -f 1 | rev | awk '{$1=$1;print}'
}
function get_current_boot_efi_volume_uuid() {
bdmesg | grep 'SelfDevicePath' | rev | cut -d ')' -f 2 | rev | cut -d ',' -f 3
}
function get_device_id_from_uuid() {
diskutil info "$1" | grep 'Device Identifier' | rev | cut -d ' ' -f 1 | rev
}
function get_disk_id_from_uuid() {
echo_log "$1"
diskutil info "$1" | grep 'Device Identifier' | rev | cut -d ' ' -f 1 | rev
}