Inizio  >  Docs  >  Mac OSX  >  How To  
How ToHow To

Index

partition resize

http://www.macworld.com/2007/02/secrets/marchgeekfactor/index.php?pf=1

 

Resize partitions on-the-fly

Geek Factor

By Kirk McElhearn

Command-line enthusiasts know that if you can do something in the regular Mac interface, you can usually do it faster using Terminal. But as of Mac OS X 10.4.6, Terminal also lets you perform a feat that previously required add-on software. Using a hidden command, you can resize disk partitions on-the-fly, without losing data.

<iframe src="http://ad.doubleclick.net/adi/mcw.mw.secrets/;kw=compressionencryption+software+tech+backupfilesynchronization2+systemutilities+compressionencryption2+backupfilesynchronization+200702marchgeekfactor;sect=secrets;art=55274;pos=showcase-sm;tile=4;sz=300x250;ord=1195656528?" marginwidth="0" marginheight="0" hspace="0" vspace="0" bordercolor="#000000" frameborder="0" height="250" scrolling="no" width="300"> <script language="JavaScript" src="http://ad.doubleclick.net/adj/mcw.mw.secrets/;kw=compressionencryption+software+tech+backupfilesynchronization2+systemutilities+compressionencryption2+backupfilesynchronization+200702marchgeekfactor;sect=secrets;art=55274;pos=showcase-sm;tile=4;sz=300x250;abr=!ie;ord=1195656528?" type="text/javascript"></script> <noscript></noscript> </iframe>

This command lets you change the size of an Intel Mac’s partitions, or of external disks connected to that Mac, while the data remains untouched—a job that used to require software such as Coriolis Systems’ $45 iPartition. The addition of this feature is a boon to anyone who has ever split up a disk, realized that one partition was too small, and then suffered through backing up, repartitioning, and copying data back to the drive once again. (For more information about partitioning, see Multiply Your Drive.) Of course, you should always back up data before messing with your disk, but if all works as planned, your backup will now just be a safety net.

PICTURES

click to see larger image

Particular Partitions: Before you can resize your partitions, you need to know their proper names. Use the diskutil list command to discover those names. Its output shows you the partition identifiers (A).

What’s the magic command? It’s a function of the <code>diskutil</code> command called <code>resizeVolume</code> . It’s so secret that it doesn’t even display in the appropriate <code>man</code> page. However, by typing <code>diskutil resizeVolume</code> in Terminal (/Applications/Utilities), you’ll get an overview of the command and its syntax.

Note that this command works only on Intel Macs with hard disks formatted using the GPT (GUID Partition Table) format with a journaled Hierarchical File System Plus (HFS+) file system. This is the default for Intel Macs’ hard disks, but you can also format an external drive in this manner through Disk Utility (/Applications/Utilities).

Gather info

To use the <code>resizeVolume</code> command, you need to get some information: you must be able to specify which partition you want to resize. You also need to know the partition’s size limitations, since it must be big enough to hold data already on the disk.

To find the partition’s name, type <code>diskutil list</code> in Terminal. Press return and you’ll see a list of all the disks on your Mac (see “Particular Partitions”). The one labeled /dev/disk0 is your boot disk. If you have other disks, they’re named disk1, disk2, and so on. Look under the Identifier header for the names of the disk’s partitions; for example, disk2s2. (Ignore any partitions labeled GUID_partition_scheme or EFI.)

Now you need to find out what size your new partition can be. Run this command: <code>diskutil resizeVolume disk_identifier limits</code> , replacing disk_identifier with your partition’s identifier. This will return the current size of the partition, as well as the minimum and maximum sizes you can use. For example:

<code>For device disk2s2 Untitled:
Current size: 215822106624 bytes
Minimum size: 6691028992 bytes
Maximum size: 215822106624 bytes</code>

Compose your command

Now that you know the disk’s name and size limits, prepare your command. It should follow this basic model:

<code>diskutil resizeVolume disk_identifier partition_size second_partition_format second_partition_name second_partition_size</code>

The first part of the command is, of course, the command itself: <code>diskutil resizeVolume</code> . Follow that with the identifier and size of the partition you’d like to split. Type in the size you want this partition to be, not what it currently is. So, for example, if you want the first partition to be 100GB, specify <code>100G</code> . (Notice that you drop the B from the abbreviation for gigabyte; you’d do the same for megabyte). Finally, specify the format, name of your choosing, and size for the partition you want to create. Want more than two partitions? Just add additional arguments to your command.

Although you can resize the first partition, you can’t change its format—that’s why you don’t need to specify one for it. For each additional partition you wish to create, you must specify the format you want it to adopt. For example, type <code>JHFS+</code> for journaled HFS+, <code>HFS+</code> for unjournaled HFS+, <code>MS-DOS</code> for FAT32, <code>UFS</code> for Unix File System, and so on. You must specify the size for each partition. For example, to create a 100GB partition in journaled HFS+, you’d type <code>JHFS+ new_partition_name 100G</code> .

You cannot create a bootable partition for Boot Camp using this command. The <code>diskutil resizeVolume</code> command will resize your disk, partition it, and format it to use MS-DOS, but it can’t install an MBR (Master Boot Record) on the disk. To make a bootable partition for Windows, you must use the Boot Camp Assistant or partition your disk with the <code>diskutil partitionDisk</code> command, which will destroy all data on it. (Type <code>man diskutil</code> in Terminal for more information on this command.)

The command in action

Here’s an example of a slightly more complicated <code>diskutil resizeVolume</code> command at work:

<code>diskutil resizeVolume disk2s2 100G JHFS+ Part2 100G</code>

This command splits a single partition in two. It specifies a size of 100GB for the first partition. Then it creates a new, second partition, named Part2, using the journaled HFS+ format, with a minimum size of 100GB. If there’s more empty space in the partition, the command will use it all. So if you split a 232GB partition, the above command would give you a first partition of 100GB and a second partition of 132GB.

Two Disks, Two Formats You can also use this command to make partitions with different formats if you need more disk space for Boot Camp. Using the previous example of a 232GB hard disk, here’s how you’d redo the partitions to create one partition for Mac OS X and another for Windows:

<code>diskutil resizeVolume disk2s2 132G MS-DOS Windows 100G</code>

You now have a 132GB journaled HFS+ partition (for Mac OS X), and a new 100GB FAT32 partition named Windows. Here’s something to keep in mind: Windows ScanDisk, the Windows disk-checking and -repair utility, requires that you make the FAT32 partition no larger than 124.55GB. If you don’t plan to use ScanDisk, don’t worry about this limit.

Reassess Before You Resize If you want to split your Mac partition again, run <code>diskutil list</code> to make sure you know which identifier to use—the partition numbers have probably changed. To split the Mac partition from the previous example in two again, for example, you could run this command:

<code>diskutil resizeVolume disk2s2 65G JHFS+ Part2 65G</code>

You now have two Mac partitions and one FAT32 partition. You cannot resize FAT32 partitions with the <code>diskutil resizeVolume</code> command.

The Last Word

The <code>resizeVolume</code> command occasionally fails. If it encounters any disk problems, it will stop, and you’ll need to run Disk Utility or another disk-maintenance program. If you have any system or special metadata files—which can’t be moved—in the section of your partition that you wish to reallocate, the command will also fail. Unfortunately, the error messages won’t go into any detail.

This new command is a work in progress and will probably see changes when OS X 10.5 arrives. In the meantime, with this tool at your disposal you can resize partitions on-the-fly, rather than having to copy data back and forth to an external drive. And you don’t have to spend a dime to do it.

Tip

Before using this technique to resize the partitions on your drive, make sure you’ve backed up everything. It’s all too easy to make a typo, and you can’t undo the command line! (For backup tips, see Better Mac Backups.)

[Kirk McElhearn is the author of many books, including The Mac OS X Command Line: Unix under the Hood (Sybex, 2004).]

 

Torna sù

part 2

http://www.friday.com/bbum/2006/05/04/mac-os-x-live-partitioning-example/

Mac OS X live partitioning example

I made a mistake when I set up my MacBook Pro. Namely, I elected to have a single boot partition of 30GB with the remainder being home to my user account. Stupid me. I need to fairly regularly have two different boot configurations; one stock, software update only, partition and one partition that can be futzed with at will.

What a perfect time to use 10.4.6’s resizeVolume to make myself a new boot partition.

Don’t even think of doing this without making a backup or five first. I saved a disk image of the entire drive prior to doing this. And I ran BackUp to another device. And I also updated 3 separate Aperture vaults. And I also copied the contents of the drive to another machine, too. No, I’m not kidding — what follows can seriously hose your data. Heck, I may have actually created an overlapping partition map and my “success” may actually be “slow data rot”.

Update: One bit of subltety. Apparently, a bootable partition needs to be evenly divisible by 4,096 bytes. That explains the small slice between partitions 2 and 3 seen below. I didn’t do that for partition 4 and spent a bit of time figuring out how to resize the partition properly. More details later.

Sure… first to check to see what I have available:

% sudo diskutil resizeVolume disk0s3 limits
For device disk0s3 Data:
Current size: 63244640256 bytes
Minimum size: 33682370560 bytes
Maximum size: 63244640256 bytes

So, in theory, I have about 30GB or so that I can rip out of Data for use as a boot volume. I do want some buffer space, so I’ll go for 20GB or so.

That would be a command something like the following (to create a 20G partition named MunchyTown):

sudo diskutil resizeVolume disk0s3 40G "Journaled HFS+" 20G MunchyTown

Crunch…crunch…crunch… about 5 to 10 minutes later, the command fails stating that the Data volume ran out of space. More likely than not, this means that the minimum size is an absolute minimum assuming that all files could be packed into the partition without any fragmentation. That simply isn’t possible and, as such, the actual minimum will be some other number.

Given the time it takes to do this operation, I figured I could short cut a bit by resizing the Data partition down only a little bit and then resize the MunchyTown partition later. So:

sudo diskutil resizeVolume disk0s3 50G "Journaled HFS+" 1G MunchyTown

OK — that worked and I managed to get my data partition down to 44G. But it didn’t quite do what I expected. Namely, I now had free space interleaved with the Data and MunchyTown partitions. Quite literally: [OriginalBoot][Data][FreeSpace][MunchyTown][FreeSpace].

Well, crap. Now what? resizeVolume doesn’t seem to allow one to reclaim space before the partition being resized. Or something. In any case, this was not looking good. Poking with diskutil quickly led to the conclusion that the only way to fix this is to edit the partition table of the drive.

So, tossing the machine into FireWire target mode and connecting it to my wife’s powerbook, I fired up the gpt utility and started poking around. First, I had to figure out the existing partition table. It looked something like:

% sudo gpt -r show /dev/disk1
Password:
start size index contents
0 1 PMBR
1 1 Pri GPT header
2 32 Pri GPT table
34 6
40 409600 1 GPT part - C12A7328-F81F-11D2-BA4B-00A0C93EC93B
409640 62652416 2 GPT part - 48465300-0000-11AA-AA11-00306543ECAC
63062056 262144
63324200 92274688 3 GPT part - 48465300-0000-11AA-AA11-00306543ECAC
######### ########
######### ######## 4 GPT part - 48465300-0000-11AA-AA11-00306543ECAC
######### ########
195371535 32 Sec GPT table
195371567 1 Sec GPT header

The “######### ########”s are just because I didn’t write down the values. As can plainly be seen, partition 4 is sitting smack between 2 chunks of unallocated space (I have no idea why there is a small space between 2 and 3 Update: I do now! That is a little bit of padding that allows partition 2 and 3 to be a size in bytes that is divisible by 4,096!).

gpt can delete that (you can add -r after ‘gpt’ to make it do a read only operation):

sudo gpt remove -i 4 /dev/disk0

To see the results:

% sudo gpt -r show /dev/disk0
start size index contents
0 1 PMBR
1 1 Pri GPT header
2 32 Pri GPT table
34 6
40 409600 1 GPT part - C12A7328-F81F-11D2-BA4B-00A0C93EC93B
409640 62652416 2 GPT part - 48465300-0000-11AA-AA11-00306543ECAC
63062056 262144
63324200 92274688 3 GPT part - 48465300-0000-11AA-AA11-00306543ECAC
155598888 39772647
195371535 32 Sec GPT table
195371567 1 Sec GPT header

Excellent! Now, to add an hfs partition (and verify):

% sudo gpt add -b 155598888 -s 39772647 -t hfs /dev/disk0
% sudo gpt -r show /dev/disk0
start size index contents
0 1 PMBR
1 1 Pri GPT header
2 32 Pri GPT table
34 6
40 409600 1 GPT part - C12A7328-F81F-11D2-BA4B-00A0C93EC93B
409640 62652416 2 GPT part - 48465300-0000-11AA-AA11-00306543ECAC
63062056 262144
63324200 92274688 3 GPT part - 48465300-0000-11AA-AA11-00306543ECAC
155598888 39772647 4 GPT part - 48465300-0000-11AA-AA11-00306543ECAC
195371535 32 Sec GPT table
195371567 1 Sec GPT header

From this point, it was a simple matter of rebooting the machine and using Disk Utility to format the freshly created partition. While there was a couple of moments of “Oh Crap! What have I done!” in between, I ended up with a 44G Data partition and a 19G munchable boot volume — just about perfect for my needs.

Except it wasn’t perfect. I couldn’t install onto the new partition because Installer complained about not being able to make it bootable. A bit of research finally revealed that the problem is that the partition I was trying to install into did was not of a length evenly divisible by 4,096 bytes. OK. Whatever. Disks are weird. 4k is, likely not coincidentally, the same size as a Mac OS X memory page. I would expect that the disk is likely subdivided by 4K clusters or something. I’m curious, but would rather have a working boot partition.

So, time for a bit of math. Partition 2 is my original 30GB boot partition. Yet, it is listed as having length of 62,652,416 — clearly, not bytes. The drive is subdivided into sectors and they happen to be 512 bytes, yielding 32,078,036,992 bytes — 30GB.

So… how many sectors do I need to subtract from 39,772,647 to yield some multiple of 4,096 bytes?

Uhhh… damnit. Still don’t need calculus, but that elementary math is about to come in handy. Or I could be über-lazy and just let the system do it for me. Actually, it isn’t lazy, it is just letting the system do the number crunching for me.

First, I deleted the partition using <code>sudo gpt remove -i 4 /dev/disk0</code>. Then I recreated the partition quite a bit smaller than desired using <code>sudo gpt add -b 155598888 -s 39760000 -t hfs /dev/disk0</code> (39760000 sectors yields 20357120000 bytes, which is evenly divisible into 4970000 4096 byte chunks).

From there, it is a simple matter of using <code>sudo diskutil resizeVolume disk0s4 limits</code>, followed by <code>sudo diskutil resizeVolume disk0s4 XXXXXM</code> to specify a size in megabytes where XXXXX is the max size returned by limits divided by (1024 * 1024) — thus yielding a size guaranteed to be divisible by 4,096.

Done and done. The damned thing boots now. Woot! I would post more specific details — numbers and all — but I didn’t have time to do more than make this work now, damnit! this afternoon.

 

Torna sù