Friday, 11 July 2008

How to non-destructively resize a bootcamp partition

I recently realised when that partitioning the drive on my Macbook Pro 50% OsX and 50% vista was not such a great plan. It turns out I use OsX for 90% of my work and Windows for 10%. So my OsX partition was rapidly filling, whereas my Vista partition was empty.

So I decided to try and shrink it. However all the instructions I found suggested you had to back up, resize vista, and then restore it. This sounded tedious - so knowing better :) I decided I would have a go at doing without doing backing up and restoring and instead using gparted. I have used gparted before several times (it is an open source equivalent of partition magic) and it has previously worked well.

The instructions below detail how to do this - and for me it did work. However this all comes with a huge health warning - I nearly lost my Vista partition doing this - and in
hindsight it would have been quicker (and easier to just back it up and restore). But for posterity I decided to write up the process anyhow.

Before you start

You will need

  • a backup in case anything goes wrong
  • Your Windows Disk
  • A network connection to download stuff
  • 2 CDRs (one for rEFIt and one for gparted)
  • To be on the mains - don't even think about trying to do this on battery!

The Steps

  • Run JKDefragGUI on the partition in 'Forced' mode -

    This will force all the files to the end of the Vista partition so you can shrink it. Unfortunately this can't move some system files - hence the next step.

  • (Optional but may be needed) Run page defrag.
    On Vista this won't 'just' work and you need to do the following.
    • On the start menu find command prompt
    • Right Click -> Run as administrator
    • Run page defrag set it to run at next boot
    • Restart
  • (Optional but may be needed) Disable your windows swap file. This enusres the windows page file doesn't get in the way. This can be done as an alternative or as well as running pagedefrag above. Page defrag is still useful as it also defrags the registry.
    • Start Control Panel
    • Start System
    • Click Advanced System Settings
    • Click performance and change to the advanced tab
    • Set all swap files to none
  • Download the GParted Live CD and burn to a disk.
    • Boot into gparted (hold ALT during startup) - the defaults should just work
    • Shrink your Vista/XP partition (don't play with the osx one at all)
    • Watch out for the laptop going to sleep.

    • If it can't resize the disk small enough for you either-- You need to delete something--
      Or you have some files stuck in an annoying place on the partition

  • Now we ned to grow Mac diskWe need to be 'root' so

       sudo su -
    discutil -list/dev/disk0
    #: TYPE NAME SIZE IDENTIFIER
    0: GUID_partition_scheme *186.3 Gi disk0
    1: EFI 200.0 Mi disk0s1
    2: Apple_HFS Macintosh HD 91.9 Gi disk0s2
    3: Microsoft Basic Data 58.6 Gi disk0s3
    /dev/disk1
    #: TYPE NAME SIZE IDENTIFIER
    0: Apple_partition_scheme *183.8 Gi disk1
    1: Apple_partition_map 31.5 Ki disk1s1
    2: Apple_HFS ben 183.7 Gi disk1s2

    will list your disks - find the name of the one you want to grow - in my case it is disk0s2
    Now check the limits you can resize within

      diskutil resizeVolume disk0s2 limits
    For device disk0s2 Macintosh HD:
    Current size: 98650030080 bytes
    Minimum size: 73892048896 bytes
    Maximum size: 136787914752 bytes

    Presuming you want the maximum run (substituting the size from above followed by a B)

      diskutil resizeVolume disk0s2 136787914752B
    Started resizing on disk disk0s2 Macintosh HD
    Verifying
    Resizing Volume
    Adjusting Partitions
    [ + 0%..10%..20%..30%..40%..50%..60%..70%..80%..90%..100% ]
    Finished resizing on disk disk0s2 Macintosh HD

    This took 2-3 minutes - while it is doing it the screen may freeze. Don't panic. Just wait.

  • You now have a smaller bootcamp partition and a larger OSX one.

    However bootcamp will now be very confused - this is to do with Vista/Linux/OSX having slightly different ideas of what a partition is.

    So you need to 'fix' this - fortunately there is a free utility for this - refit.

    • Download rEFIt and burn the DMG to a CD.
    • It is bootable - so reboot and hold down Alt to boot off it.
    • Once in the menu use the arrow key to select partition manager.It should fix your MBR.
      Reboot and this time start Windows Via Bootcamp. It may fail horribly (it did for me) as the Windows bootloader couldn't find Vista.
    • Insert your windows CD
    • Reboot and boot off it (by holding ALT)
    • Select your language
    • Select repair (at the bottom of the screen)This will fix vista.
      Reboot again - this time boot into Vista
    • Chkdsk will run and 'fix' stuff
    • Vista should now be well again.
  • If you deleted your swapfile above you will want to recreate it otherwise your vista will be v. slow and will break a lot. A helpful hint is NOT to let Vista manage it dynamically as that is how the page file gets fragmented in the first place and instead just create one at the recommended size.
  • If you use VMFusion (this may also be true for parallels) to boot the Vista partition inside OSX it won't work straight away. It seems to store some details that have changed. To fix this 'simply'
    Start Terminal

      cd Library/Application\ Support/VMware\ Fusion/Virtual\ Machines/
    rm -rf Boot\ Camp

    This will reset VMWare, it will now re-detect and configure your boot camp partition and it should all work again.


2 comments:

Scott and Lyndsay Crews said...

Thanks for this post! I put a new hard drive in my MacBook and tried to grow my windows partition with gparted...and thought I'd wrecked the whole thing for sure when the windows partition didn't come up on the boot screen. rEFIt was just what I needed.

A couple of things went differently for me that may help others who find this post:
I was only trying to grow the windows partition into unpartitioned space...I wasn't touching my OSX partition. (I used Winclone to copy my bootcamp partition from the old drive to the new and it worked great...except it kept the old partition size)
I used Windows XP rather than Vista.
Once I used rEFIt to restore my MBR, I rebooted and everything worked perfectly. I didn't have to restore my XP partition from the installer or anything.
I use Parallels, and it booted up fine--no reset necessary like you had with VMWare.

Whew! That was a pretty stressful half hour. Thanks again for the rEFIt walkthrough. It was very helpful.

jgATL said...

Two small tweaks.
1. make sure you maintain the small unallocated space between OS X and your Windows partition.
2. drop your virtual machine from Fusion and re-add it by closing VMWare, removing the Users/[username]/Library/Application Support/VMWare Fusion/Virtual Machines/Boot Camp/ directory and then restarting VMWare. It will pickup the boot partition and redo your VM.

More detail on the first item, Bootcamp (or OSX) looks for a very small (512k) unpartitioned space between your OS X partition and your Windows partition. If you want to be able to see your Windows partition as a valid disk inside OS X (system preferences > startup disk), you need to maintain that space. When I Gparted, i assumed that I could just move the partition all the way over to end of the OS X partition.

Be sure to note, before you do any resizing, the size of this space so when you use Gparted, you can ensure the starting position maintains that unallocated space.