-
Notifications
You must be signed in to change notification settings - Fork 496
/
Copy pathosx_hide_partition
executable file
·66 lines (52 loc) · 2.11 KB
/
osx_hide_partition
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
#!/usr/bin/env bash
if [[ ! "$1" || "$1" == "-h" || "$1" == "--help" ]]; then cat <<HELP
Hide or Show an OS X Partition
http://benalman.com/
Usage: sudo $(basename "$0") mode device
Where mode is either "hide" or "show" and device is a valid /dev/disk??? path.
Tested on Apple_HFS and Apple_Boot partition types in OS X 10.8.
Copyright (c) 2012 "Cowboy" Ben Alman
Licensed under the MIT license.
http://benalman.com/about/license/
HELP
[[ "$1" ]]; exit; fi
mode="$1"
device="$2"
# Error handling
[[ $UID != 0 ]] && echo "Error: please run with sudo" && exit 1
[[ ! "$mode" =~ ^(hide|show)$ ]] && echo "Error: please specify hide or show" && exit 2
info="$(diskutil info "$device")" || { echo "Error: please specify a valid device" && exit 3; }
disk="$(echo "$info" | perl -ne 'm#Part of Whole:\s+(.*)# && print $1')"
dev_name="$(echo "$info" | perl -ne 'm#Device / Media Name:\s+(.*)# && print $1')"
vol_name="$(echo "$info" | perl -ne 'm#Volume Name:\s+(.*)# && print $1')"
info="$(gpt -r show -l $disk | grep "GPT part - \"$dev_name\"")"
start="$(echo "$info" | awk '{print $1}')"
length="$(echo "$info" | awk '{print $2}')"
index="$(echo "$info" | awk '{print $3}')"
guid="$(gpt -r show $disk | grep "$(echo "$info" | cut -c 1-39)" | awk '{print $7}')"
# for v in device disk dev_name vol_name start length index guid; do
# printf "%-8s : %s\n" $v "${!v}"
# done
# From http://en.wikipedia.org/wiki/GUID_Partition_Table
guid_hfs="48465300-0000-11AA-AA11-00306543ECAC"
guid_boot="426F6F74-0000-11AA-AA11-00306543ECAC"
if [[ $mode == "hide" ]]; then
[[ "$guid" == "$guid_boot" ]] && echo "Volume \"$vol_name\" already hidden." && exit 4
old=$guid_hfs
new=$guid_boot
else
[[ "$guid" == "$guid_hfs" ]] && echo "Volume \"$vol_name\" already shown." && exit 4
old=$guid_boot
new=$guid_hfs
fi
# Read user input.
REPLY=
read -n 1 -s -p "Are you sure you want to $mode \"$vol_name\" on $disk? [y/N] "
echo
[[ ! "$REPLY" =~ ^[Yy]$ ]] && echo "Aborting." && exit 5
# Detach device.
hdiutil detach "$device"
# Remove partition.
gpt remove -b $start -s $length -t $old $disk
# Recreate partition.
gpt add -b $start -s $length -t $new $disk