-
Notifications
You must be signed in to change notification settings - Fork 37
/
partitionDisk.sh
121 lines (95 loc) · 3.43 KB
/
partitionDisk.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
116
117
118
119
120
121
#!/bin/sh
# This script was written to automate the re-partitioning of drives for Macs using
# a logical volume group and a logical drive. Fusion drives are supported as well
# as single-disk systems. This script assumes it is running from a NetBoot environment
# where drives can be unmounted, partitioned and reformatted freely. Use at your own risk.
# Author: Andrew Thomson
# Date: 02-04-2015
function countDisks() {
# reset array
unset ARRAY_DISKS
# enumerate all installed disks
ARRAY_DISKS=(`/usr/sbin/diskutil list | awk -F: '/dev/ {print $NF}'`)
echo "The count of installed disks is: ${#ARRAY_DISKS[@]}."
# remove any external disks from the array
for INDEX in $(seq 0 $((${#ARRAY_DISKS[@]}-1))); do
if /usr/sbin/diskutil info ${ARRAY_DISKS[$INDEX]} | grep Internal: | grep No > /dev/null; then
unset ARRAY_DISKS[$INDEX]
fi
done
echo "The count of internal disks is: ${#ARRAY_DISKS[@]}."
}
function countLogicalVolumes() {
# reset array
unset ARRAY_LOGICAL_VOLUMES
# refresh disk count
countDisks
# enumerate internal disks to find logical volumes
for INDEX in $(seq 0 $((${#ARRAY_DISKS[@]}-1))); do
if /usr/sbin/diskutil list ${ARRAY_DISKS[$INDEX]} | grep "Logical Volume" > /dev/null; then
ARRAY_LOGICAL_VOLUMES+=(${ARRAY_DISKS[$INDEX]})
fi
done
echo "The count of logical volumes is: ${#ARRAY_LOGICAL_VOLUMES[@]}."
echo "----------"
}
# initialize disk counts
countLogicalVolumes
# only delete and recreate primary partion if no logical volumes found.
if [ ${#ARRAY_LOGICAL_VOLUMES[@]} -eq 0 ]; then
if /usr/sbin/diskutil partitionDisk disk0 GPT JHFS+ "Macintosh HD" 100% > /dev/null; then
echo "Successfully deleted and recreated primary parititon."
exit 0
else
"ERROR: Unable to delete and recreate primary partition."
exit $LINENO
fi
fi
# delete any found logical volumes
if [ ${#ARRAY_LOGICAL_VOLUMES[@]} -ne 0 ]; then
for INDEX in $(seq 0 $((${#ARRAY_LOGICAL_VOLUMES[@]}-1))); do
if /usr/sbin/diskutil cs deleteVolume ${ARRAY_LOGICAL_VOLUMES[$INDEX]} > /dev/null; then
echo "Successfully deleted logical volume on: ${ARRAY_LOGICAL_VOLUMES[$INDEX]}."
else
echo "ERROR: Unable to delete logical volume on ${ARRAY_LOGICAL_VOLUMES[$INDEX]}."
exit $LINENO
fi
done
echo "----------"
countDisks
fi
# delete any internal disks
if [ ${#ARRAY_DISKS[@]} -ne 0 ]; then
for INDEX in $(seq 0 $((${#ARRAY_DISKS[@]}-1))); do
if /usr/sbin/diskutil partitionDisk ${ARRAY_DISKS[$INDEX]} GPT "Free Space" "Untitled $INDEX" 100% > /dev/null; then
echo "Successfully deleted disk: ${ARRAY_DISKS[$INDEX]}."
else
echo "ERROR: Unable to delete disk: ${ARRAY_DISKS[$INDEX]}."
exit $LINENO
fi
done
echo "----------"
fi
# create logical volume group
if [ ${#ARRAY_DISKS[@]} -ne 0 ]; then
if /usr/sbin/diskutil cs create "Macintosh HD" ${ARRAY_DISKS[@]} > /dev/null; then
echo "Successfully created logical volume group."
# get UUID of newly created logical volume group
UUID=`/usr/sbin/diskutil cs list | grep "Logical Volume Group" | awk '{print $5}'`
if [ -n $UUID ]; then
# create logical volume
if /usr/sbin/diskutil cs createVolume $UUID JHFS+ "Macintosh HD" 100% > /dev/null; then
echo "Successfully created logical volume."
else
echo "ERROR: Unable to create logical volume."
exit $LINENO
fi
else
echo "ERROR: Unable find logical volume group UUID."
exit $LINENO
fi
else
echo "ERROR: Unable to create logical volume group."
exit $LINENO
fi
fi