Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 67 lines (52 sloc) 2.11 KB
#!/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