Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding OS X partition hide/show script.

  • Loading branch information...
commit 7b840fb1966836462f1883729607ef2d270de436 1 parent 89a26cf
@cowboy authored
Showing with 66 additions and 0 deletions.
  1. +66 −0 bin/osx_hide_partition
View
66 bin/osx_hide_partition
@@ -0,0 +1,66 @@
+#!/bin/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
Please sign in to comment.
Something went wrong with that request. Please try again.