Skip navigation

Restore Multiple Partitions Through Disk Utility With AppleScript?

1739 Views 20 Replies Latest reply: Apr 19, 2012 9:37 PM by aaanorton RSS
1 2 Previous Next
aaanorton Level 1 Level 1 (0 points)
Currently Being Moderated
Apr 13, 2012 10:39 AM



I need an AppleScript to restore a bunch of partitions from a master drive to a stack of external target drives. I don't mind partitioning them 1st, if necessary.

The master drive has 16 partitions, all equally sized, and I'd like a script to run that will have Disk Utility to just go down the list restoring each partition to the matching 16 partitions on one of the target drives.

Anyone know of or have anything l could use for this? Any advice on making this script? I'm not an advanced AppleScripter. And finally, would there be any way to have the script partition the target drives as well?


Thanks in advance!

Mac OS X (10.7.3)
  • twtwtw Level 5 Level 5 (4,580 points)

    unix has the diskutil utility which seems to be what you're looking for.  Without knowing precisely what you're doing (are you trying to make exact copies of the master disk, or just copy the partition structure?) I can't give you precise code; can you give more details?


    you can run this from applescript or a shell script, whichever you're more comfortable with.

  • Camelot Level 8 Level 8 (45,670 points)

    You can use diskutil to do all of this.


    You'll need to know the device id of the FW drive.

    disk0 will be your boot drive. If your 'master' drive is a second drive (other than your boot) it'll typically be disk1, and an attached 'slave' drive would be disk2 - but you should verify that.


    Once you've worked out your IDs you can partition the drive:

    diskutil partitionDisk disk2 16 GPT [format name size]+

    The last three parameters indicate the partition format (e.g. 'jhfs+' for Journaled Extended), partition name, and size) and should be repeated for each partition on the disk - e.g. 16 times in this instance. Sizes can be specified in bytes, kiloybtes, megabytes, gigabytes, terabytes or a percentage of disk space by appending the value with a 'B', 'K', 'M', 'G', 'T' or '%', respectively. You can also use 'R' (without any digits) to use the remaining space on the drive.


    Once you've partitioned the disk you can replicate the 16 partitions via asr. For example, this AppleScript snippet will iterate through 16 partitions on the drives (although there's no error checking to make sure the partitions are valid:


    repeat with part from 1 to 16

              do shell script "/usr/sbin/asr restore --source disk1s" & part & " --target disk2s" & part & " --erase" with administrator privileges

    end repeat

  • Camelot Level 8 Level 8 (45,670 points)

    It looks like the partitioning part would be easier to do manually in Disk Utility. There I can just specify 16 partitions and let the application figure out the details of size and names.


    If you know the size of the partitions you want/need, then it's easy to do. If you want them to all be the same size then a little AppleScript math before the diskutil command will do the trick, or you could use the % form:

    diskutil partitionDisk disk2 16 GPT jhfs+ part1 6.25% ...


    where 6.25% is the equivalent of 1/16 of the total disk space. Creating 16 partitions of this size will consume the entire disk (although you might want to use 'R' for the last partition to use the remaining space, just in case there are rounding issues.


    Will the repeat script as specified rename the target partitions to whatever the source partitions are named? I assume it will.


    You know, that's a good question. Off hand I don't recall whether asr restore copies the volume label or not. I suspect not so you'll either need to name that at the beginning (via the diskutil partitionDisk command) or after the data's copied, via standard AppleScript commands to rename disks, or via diskutil rename.


    I know I can get the device IDs via the Get Info button once in Disk Utility, but is there an easier way?


    You can get device IDs via diskutil info, but the trick is identifying which disk is which. A little experimentation might be in order.


    And can you explain why why the IDs in the repeat script end in s, as in disk1s?


    Sure - disk0 refers to the entire disk and all its partitions. The s denotes a slice (aka partition) on that disk.

    So disk0s4 refers to the fifth slice/partition on disk0 (slices are 0-based, so disk0s0 is the first partition).

    Note that the boot volume is commonly disk0s2 - the third partition since there's likely a partition map and a EFI boot partition before the actual boot volume. You'll need to look at the partition map of a sample disk to work out what the appropriate numbers are.

  • Camelot Level 8 Level 8 (45,670 points)

    It isn't clear why the original script failed, but I don't think your solution is valid, either.


    If you change the command in the way you describe, you have to consider how the script looks as it iterates through the loop from 1 to 16.


    The first time through the loop, 'part' equates to 1, so the part of your script:


    --target disk2s2" & part & " --erase"


    will translate into:


    --target disk2s21 --erase

    I doubt there's 21 partitions on this disk. Of course, as you iterate through the loop and get towards the end - say, part = 16 - you'll get to --target disk2s216 and I know there isn't 216 partitions


    So what I think this comes down to is a simple mismatch on the partition IDs - either there's some extra partition ID that I'm not seeing, or the disk hasn't been partitioned yet. Indeed - the error message in your screen shot - 'disk2s1 is not a volume' - implies that's the case. Either that, or the disk isn't mounted at disk2, so you might need to use disk3, or disk4 or some such. I did warn about this in my original post saying that you'd need to validate the disk IDs before trying this.

  • Camelot Level 8 Level 8 (45,670 points)

    It would be handy to see the Disk Identifier for the first partition on disk2... to ensure they're starting at 1 and not some other value.

  • Camelot Level 8 Level 8 (45,670 points)

    There's no need to worry - if the first usable partition is s2 then just change the script to:


    repeat with part from 2 to 17


    that way you'll replicate disk1s2 to disk2s2, etc., and skip the initial s0 and s1 partitions.

1 2 Previous Next


More Like This

  • Retrieving data ...

Bookmarked By (0)


  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.