1 2 Previous Next 20 Replies Latest reply: Apr 19, 2012 9:37 PM by aaanorton
aaanorton Level 1 Level 1 (0 points)

Hi!

 

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)
  • 1. Re: Restore Multiple Partitions Through Disk Utility With AppleScript?
    aaanorton Level 1 Level 1 (0 points)

    Upon further reflection, I guess I don't need it to be an AppleScript. Any type script would be great. I just need a solution that will run on a stock Lion iMac or MBP/A.

     

    Thanks again!

  • 2. Re: Restore Multiple Partitions Through Disk Utility With AppleScript?
    twtwtw Level 5 Level 5 (4,690 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.

  • 3. Re: Restore Multiple Partitions Through Disk Utility With AppleScript?
    aaanorton Level 1 Level 1 (0 points)

    I'd like exact copies of the master disk. The master has 16 partitions, each (or most) of which contains data. I'd like to be able to plug in a FW drive and run a script that partitions the target with 16 equal partitions, and then restores each of the master's partion data to each of the target's.

    If the partitioning portion of this is more work than necessary, I'd be happy to manually partition each new target drive before running a script to copy the data.

    Make sense? I'll need to do this fairly regularly, so a script would really help.

    More details:

    Master and target drives are all FW 400/800 external drives.

    I'd like some utuility for choosing the master and target drives when the script launches.

    I'm pretty sure a shell script can be saved as a launchable application, right? So either would be fine. I would like to be able to easily share this with co-workers.

  • 4. Re: Restore Multiple Partitions Through Disk Utility With AppleScript?
    Camelot Level 8 Level 8 (45,790 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


  • 5. Re: Restore Multiple Partitions Through Disk Utility With AppleScript?
    aaanorton Level 1 Level 1 (0 points)

    This looks very interesting! I should have a chance to check it out in a couple days. I will definitely report back.

    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.

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

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

    And can you explain why why the IDs in the repeat script end in s, as in disk1s? I see that same convention in System Info: disk0s1 as the device and disk0s2 and disk0s3 as the volumes.

     

    Thanks a ton! More soon.

  • 6. Re: Restore Multiple Partitions Through Disk Utility With AppleScript?
    Camelot Level 8 Level 8 (45,790 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.

  • 7. Re: Restore Multiple Partitions Through Disk Utility With AppleScript?
    aaanorton Level 1 Level 1 (0 points)

    Camelot wrote:

     

     

    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.

     

    10-4. Thanks! I figured that was the case from the info I'm viewing about my disks. Makes perfect sense!

     

    So I gave the script a whirl and here's the result I got in Apple Script Editor:

     

    Screen Shot 2012-04-15 at 8.32.06 PM.png

     

    Basically, it seems to be trying to restore part1 of source to slice1 (device) of target. Here are the results from Terminal shell:

     

    [1] 84221

    [2] 84222

    [3] 84223

    -bash:     : command not found

    -bash: part: command not found

    [4] 84224

    -bash:  --target disk2s: command not found

    -bash: part: command not found

    -bash:  --erase: command not found

    [1]   Exit 127                          do shell script "/usr/sbin/asr restore --source disk1s"

    [2]   Exit 127                part

    [3]-  Exit 127                " --target disk2s"

    [4]+  Exit 127                part

     

    I'm thinking I should change the script to specify the 1st partition on disk2 like this:

     

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

     

    Make sene? Please lemme know whatcha think!

    And thanks!

  • 8. Re: Restore Multiple Partitions Through Disk Utility With AppleScript?
    Camelot Level 8 Level 8 (45,790 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.

  • 9. Re: Restore Multiple Partitions Through Disk Utility With AppleScript?
    aaanorton Level 1 Level 1 (0 points)

    Camelot wrote:

     

    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

     

    Hmmm... My thought was to give it a valid starting point, but I see now what you're saying.

     

    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

     

    Yes, I draw the line firmly at 199 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.

     

    OK, I'm willing to chalk this one up to a PEBKAC issue for now. I'll re-attempt it tomorrow and diligently (re)check my IDs.

    Regarding that disk2s1 is not a volume message, If I had used the wrong ID, wouldn't it report something more like disk2s1 does not exist?

    More soon!

  • 10. Re: Restore Multiple Partitions Through Disk Utility With AppleScript?
    aaanorton Level 1 Level 1 (0 points)

    OK, so I confirmed my setup, and it seems I'm getting the IDs right. Below, each info window is next to its own disk and partitions. Note the highlight and active window.

    Any more ideas? I really appreciate the help so far!

     

    Screen Shot 2012-04-16 at 5.49.39 PM.jpg

  • 11. Re: Restore Multiple Partitions Through Disk Utility With AppleScript?
    Camelot Level 8 Level 8 (45,790 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.

  • 12. Re: Restore Multiple Partitions Through Disk Utility With AppleScript?
    aaanorton Level 1 Level 1 (0 points)

    Ding ding ding!

    That seems to be the problem. My 1st partition is s2 on both drives. Is there a way to change this behavior from Disk Utility? Or do I need to fire up that partitioning script you recommended?

  • 13. Re: Restore Multiple Partitions Through Disk Utility With AppleScript?
    Camelot Level 8 Level 8 (45,790 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.

  • 14. Re: Restore Multiple Partitions Through Disk Utility With AppleScript?
    aaanorton Level 1 Level 1 (0 points)

    Right on. But this seems to fail too. This from AppleScript Editor:

     

    tell current application

              do shell script "/usr/sbin/asr restore --source disk1s2 --target disk2s2 --erase" with administrator privileges

                        --> error "          Validating target...

    \"/disk2s2\" is not a volume

    Could not validate target - No such file or directory" number 1

    Result:

    error "          Validating target...

    \"/disk2s2\" is not a volume

    Could not validate target - No such file or directory" number 1


1 2 Previous Next