Drive Partitioning Script

aburrow
Contributor

I'm trying to write a script that will repartition a drive based on it's Physical size.

If the drive is 300GB or below, 2 partitions the primary partition 50% of the total the 2nd partition will be the rest.

If the drive is above 300GB, 2 partitions the primary partition being 150GB the 2nd partition will be the rest.

Diskutil will do the partitioning.
I can also use Diskutil to get the physical drive size. The output I get for example is "250 GB"

I'm not certain though how to go about writing a script though to check the size and run one partitioning scheme or the other.

1 REPLY 1

stevewood
Honored Contributor II
Honored Contributor II

This was a script I used to use as a pre-image script. It was based on some code that Tom Larkin had done when working at the school district in KC. You could use this as a base and modify it. It basically checks for drive size great than 200GB and uses that logic.

#!/bin/bash

#########################################################################
#                                                                       #
# Name: preimage.sh                                                     #
# Date: 9 February 2010                                                 #
# Author:  Steve Wood (swood@integer.com)                               #
# Purpose:  This script is designed to run as a pre-imaging script for  #
# use with Casper to partition and erase the hard drive in a system.    #
# The script is designed to partition /dev/disk0 into two partitions,   #
# Macintosh HD and Users.                                               #
#                                                                       #
# Thanks goes out to Thomas Larkin (tlarki@kckps.org) for pieces of the #
# script.                                                               #
#########################################################################


#
# this assumes that /dev/disk0 is going to be the main HD in all systems

HardDriveSize=`diskutil info /dev/disk0 | awk '/Total Size/ { print int ($3) }'`

# now compare results to run desired command

if [ $HardDriveSize -gt 200 ]

   then 

    diskutil partitionDisk /dev/disk0 2 GPT JHFS+ "Macintosh HD" 40%  JHFS+ Users 60%
    echo "Disk has been re-partitioned."

   else 

    diskutil partitionDisk /dev/disk0 2 GPT JHFS+ "Macintosh HD" 50% JHFS+ Users 50%
    echo "Disk has been re-partitioned."

fi

exit 0

Just adjust the percentages and the volume names. This script assumes the drive is at drive0, so you might want to build in some logic that verifies the proper disk ID before doing the partitioning. I didn't need to do that for my use.