Monday, March 12, 2007

Lossless same-drive Mac HD Repartition

Recently I discovered a lossless way to repartition my Mac's main hard drive, without having to boot from an external drive. I have been looking for this for quite a while, as I wanted to install the Mac OS X Leopard preview in order to test the new development tools and maybe try a new feature or two for Xslimmer.



Some people had suggested to use the Bootcamp utility to resize and partition the drive. But I had already installed Windows (basically for gaming) and did not want to lose that partition.

So, I finally read an article on how to do this using the command line command diskutil, and its hidden feature named resizeVolume. If you check the man pages, you will see that there is no information on this matter, but you can obtain some executing "diskutil resizeVolume":

freeport:~ jorge$ diskutil resizeVolume
Disk Utility Tool
Usage: diskutil resizeVolume [Mount Point|Disk Identifier|Device Node] size
...
Non-destructively resize a disk. You may increase or decrease its size.
When decreasing size, you may optionally supply a list of new partitions to create.
Ownership of the affected disk is required.
Valid partition sizes are in the format of .
Valid sizes are B(ytes), K(ilobytes), M(egabytes), G(igabytes), T(erabytes)
Example: 10G (10 gigabytes), 4.23T (4.23 terabytes), 5M (5 megabytes)
resizeVolume is only supported on GPT media with a Journaled HFS+ filesystem.
A size of "limits" will print the range of valid values for the current filesystem.
Example: diskutil resizeVolume disk1s3 10G
JHFS+ HDX1 5G MS-DOS HDX2 5G
Valid filesystems: "Case-sensitive HFS+" "Journaled HFS+" "Case-sensitive Journaled HFS+" "HFS+" "HFS" "MS-DOS FAT32" "MS-DOS FAT16" "MS-DOS" "MS-DOS FAT12" "UFS" "Linux" "Swap"


So, as you can see, to resize a partition you first need to know its name. For that you can use "diskutil list":

freeport:~ jorge$ diskutil list
/dev/disk0
#: type name size identifier
0: GUID_partition_scheme *465.8 GB disk0
1: EFI 200.0 MB disk0s1
2: Apple_HFS Macintosh HD 434.0 GB disk0s2
3: Microsoft Basic Data WINDOWS HD 31.4 GB disk0s3


In my case, the partition was disk0s2. The partition scheme or the EFI partition should be ignored. As I said before, I already had a Mac OS partition and the Bootcamp partition. I wanted to create a new 20 Gb partition out of the main Mac OS partition, disk0s2.

Now, there are some limitations to the resizing. I guess it has to do with how much free space you have in the partition you want to divide. To verify the limitation, you use "diskutil resizeVolume partition_name limits":

freeport:~ jorge$ diskutil resizeVolume disk0s2 limits
For device disk0s2 Macintosh HD:
Current size: 466003951616 bytes
Minimum size: 213448208384 bytes
Maximum size: 466003951616 bytes


Finally, you have to provide the resizing parameters to the "diskutil resizeVolume" command. In my case, I wanted to keep 414Gb for the main partition and create a new Journaled HFS+ with 20Gb:

freeport:~ jorge$ diskutil resizeVolume disk0s2 414G JHFS+ Leopard 20G
Started resizing on disk disk0s2 Macintosh HD
Verifying
Resizing Volume
Adjusting Partitions

Finished resizing on disk disk0s2 Macintosh HD
You will need to manually reformat your new partitions.
WARNING: You must now reboot!


In my case the first step, "Verifying" was what took the longest time. After that it all went very fast.

Once done, you should reboot. After rebooting, you can use diskutil or Disk Utility to prepare that new partition for use.

freeport:~ jorge$ diskutil list
/dev/disk0
#: type name size identifier
0: GUID_partition_scheme *465.8 GB disk0
1: EFI 200.0 MB disk0s1
2: Apple_HFS Macintosh HD 414.0 GB disk0s2
3: Apple_HFS 19.9 GB disk0s3
4: Microsoft Basic Data WINDOWS HD 31.4 GB disk0s4


In my case, I used Disk Utility, selecting the new partition, the Erase tab, then proving a name for the partition, and clicking on the Erase button.

freeport:~ jorge$ diskutil list
/dev/disk0
#: type name size identifier
0: GUID_partition_scheme *465.8 GB disk0
1: EFI 200.0 MB disk0s1
2: Apple_HFS Macintosh HD 414.0 GB disk0s2
3: Apple_HFS Leopard HD 19.9 GB disk0s3
4: Microsoft Basic Data WINDOWS HD 31.4 GB disk0s4

(Update) One more thing needs to be done. This time is for Windows to keep running fine. You should edit boot.ini (I used textmate), so it knows the partition is it located in. Mine looked like this:

[boot loader]
timeout=30
default=multi(0)disk(0)rdisk(0)partition(3)\WINDOWS
[operating systems]
multi(0)disk(0)rdisk(0)partition(3)\WINDOWS="Microsoft Windows XP Professional" /noexecute=optin /fastdetect
As you see, it says partition(3) for the Windows partition, which is now wrong. I changed it to partition(4) and it worked like a charm. Notice partition(3) appears twice, you should change both.

All set!

Now I have Leopard on my Mac along with Tiger and Windows, thanks to these simple operations. Needless to say, before you attempt to do anything like this, you should have a backup.

[Update: I wrote a second part of this article]

Disclaimer: Visitors do assume all the risk of viewing, reading, using, or relying upon this information. We assume no responsibility for damage to computers or software of the visitor or any person the visitor subsequently communicates this information to.

Have fun!

10 comments:

schildkroeter@web.de said...

I tried this, but I get an error no matter which disk I use:
jons-machine:~ Jonathan$ diskutil resizeVolume disk1s3 limits
Error obtaining resizing information

Resizing encountered error Could not modify partition map (-9986) on disk disk1s3 Lacie HD 2

That was the message for external as well as internal drives...

Jorge said...

I am not fully sure, but maybe this is an Intel Mac procedure only, as this is necessary for creating the bootcamp Windows partitions. Is you machine Intel or PowerPC?

schildkroeter@web.de said...

hmmm PPC

Dazza said...

errr, wow thank you. I was bricking myself about doing this, until I read your article. I'm a sysadmin, so I'm used to doing this stuff, but I don't know Unix too well. I really didn't want to lose the 300Gb of stuff, just for a 30Gb partition. I hit enter on the command, closed my eyes for 5 seconds, after opening them I found two drives on my desktop -- On with the Leopard installs :-)

Jake said...

now that boot camp beta is expired, I'm trying to figure out how to DELETE my windows partition - I'm ready to sever ties with Bill Gates. :) is there a way to do that using this method?

Anonymous said...

change date and time before September 2007 and run bootcamp

Raghu said...

I too get the same error... :(

Error obtaining resizing information

Resizing encountered error Could not modify partition map (-9986) on disk

But I'm on Intel mac with mbr partition...!
Any help?
Thanks

Jorge said...

@raghu

You might be interested in checking our second part of this article.

Anonymous said...

On my Mac Pro, i devided: 79.9G for Leopard, 30G for Vista. Now i want to have 20G from 79.9G of Leopard to share data between Vista and Leopard. Is there any way for me to do it without harming currently 2 working OSs ?

dai_vernon said...

From what I understand this requires a GUID partitioned drive, which ppcs don't have; ppc macs will say Apple_partition_map and Apple_partition_scheme and whatnot and will not be resized by diskutil resizeVolume and will give the error output that schildkroeter@web.de got.