Sep 14, 2011

How to create a Mac OS X Lion recovery partition

Update (Oct 23, 2010): while this article still provides a working way to (re)create the recovery partition, I now recommend a simpler way. But if you decide to go with the way described here, make sure you read comments too! This article was written by extracting shell commands from a list of many successful and unsuccessful commands. Therefore there are some mismatches in numbers in this article. However the workflow is correct. If you use this article, you should carefully calculate your own numbers. But really, use the other way. It is easier and less risky.
Recently my old (42 months) Macbook Pro had a problem and I had to restore it completely from the Apple Time Machine backup. This Macbook is my primary instrument, so I do everything to keep its information safe and alive.
As a part of being "safe", I always encrypt my hard drive. Before Lion I used PGP Whole Disk Encryption. It was a bad experience (2x slower, troubles after each OS X update, etc) but it worked. With Lion I happily threw away PGP without being sorry at all and encrypted my hard disk with new shiny FileVault2. FileVault2 also encrypts the whole disk and requires a pre-boot authentication to unlock the drive. Information cannot be recovered without a proper password.
After recent crash and recovery from a Time Machine, I went to encrypt my drive again. But now Lion showed me a message that my disk cannot be encrypted because it does not meet the requirements for FileVault2. I knew what was the problem. FileVault2 requires a hidden "recovery" partition that Apple creates on the disk. It seems like I did not have that. Searching the Internet revealed that if I install a clean system, recovery partition is silently created. However, if I restore from a Time Machine Backup, it is not created.
This is bad news. I need FileVault2. So I have to make that recovery partition in some way.
There are two possible options:
  • repartition the disk, install a clean system and than restore from Time Machine
  • do some dirty manual work to recreate the partition
I am going to cover both ways and explain advantages and disadvantages of both. Now I must warn that this is neither safe, nor approved way. It worked for me but I have no responsibility if it does not work for you. Lawyers, you can go for other people, I made a disclamer ;)

Easy way: reinstall

Easy way is for people, who do not feel comfortable with terminal and afraid of irrecoverably breaking their current data. This way is safer than the second way but it also can go wrong (those chances to go wrong are much less than with a second way). It also a very lengthy way: expect to spend about 4-8 hours without your Mac. I do not like loosing time, so I did not choose this way. But I did it before and will describe it briefly here to give you the idea.
Here is what you need to do:
  • Backup your data using Time Machine. Use USB disk of greater capacity than you have on your Mac. I recommend using USB instead of network because USB is faster.
    You need to run the Disk Utility and format that disk with "Mac OS X Extended (Journaled)". Watch carefully what you format. Formatting a wrong disk would destroy everything on that disk. Eject and physically disconnect the disk after backing up.
  • Reboot using Lion USB stick. You can make that yourself. That article also gives you instructions on formatting partitions with Disk Utility (you needed those in the previous point).
  • You will a prompt on what to do. Go to the disk utility.
  • In the disk utility follow Apple's official article on creating a proper disk configuration (go straight to “Installing Lion on an external storage device”). Note that this destroys all data on the disk. But you made a backup, so you should be safe. Just watch carefully what you partition.
  • Ext disk utility and install Lion. This is the easiest thing to do. Do not select "Restore from Time Machine" but install normally.
  • When Lion prompts to restore your data, connect your backup USB disk and restore.
That's all. While it is easier and safer than the second way, you may experience some quirks. You may have troubles with root user or your own permissions. Also you can miss some configuration items.
If you have a highly customised setup and you will ok to use Terminal, you may be better with a second way.

Hard way: forge recovery partition

This is a very dangerous way. I repeat: it is very dangerous. With this way you do not touch your data if you do everything carefully. However you can destroy your data with a single inaccurate move. Therefore, always look twice or thrice and make sure you know what you are doing with each move. You have been warned.
The general plan for this method is:
  • We create a minimal proper OS X installation on an external USB stick.
  • We boot from USB
  • We resize a single existing partition on Mac's internal disk
  • We create a new partition on Mac's disk using disk utility
  • We note certain partition data, delete and recreate that partition as a recovery partition using command line tools
  • We copy recovery partition from the external USB stick using command line tools
Total time needed: less than an hour (provided that you do not screw up in the middle). Let's go step by step.

Create a minimal Lion installation

Use two USB sticks: one with Lion installer (see above how to make it) and the other – empty, 16GB or more.
Boot from the USB stick and install Lion. You can customize the installation to remove almost everything but I recommend to keep it as is: you will get a bootable OS X stick for your computer with full Mac OS X. The whole installation process will take about 20 minutes. In the end you will be booted to the system without reboot.

Resize Mac's partition

Go to the Disk Utility and repair your disk. Don't waste time on Verify, just press Repair. It will verify and repair if necessary. You must do that. If you go to resize but disk has errors, you will loose your data.
Use Disk Utility to resize Mac's hard drive. Make about 700-800GB free space after your existing partition. For that click on the disk (not the partition but the disk and use the triangle at the bottom of the partitioner to shrink the existing partition. No pictures, sorry, but if you go this way, you should realize what I wrote. If not, you may need to poke around disk utility a little.

Create a new partition

Now we need to create a new partition using disk utility. We are not going to use but we need it to note proper data.
Firsts, we need to know the size. Open terminal and look information for all connected disks:
Dmitrys-MacBook-Pro:~ dima$ diskutil list
/dev/disk0
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:                       GUID_partition_scheme        *128.0 GB  disk0
   1:                       EFI                          209.7 MB   disk0s1
   2:                       Apple_HFS Macintosh HD       127.2 GB   disk0s2
/dev/disk1
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:                       FDisk_partition_scheme       *8.0 GB    disk1     1:                       Apple_HFS Mac OS X Install ESD8.0 GB    disk1s1
/dev/disk2
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:                       GUID_partition_scheme        *160.0 GB  disk2
   1:                       EFI                          209.7 MB   disk2s1
   2:                       Apple_HFS EXT HD             159.2 GB    disk2s2
   3:                       Apple_Boot Recovery HD       650.0 MB   disk2s3
The line in bold is the one you need now. In your system the order of disks can be different but you need to find the only recovery partition in the system and get its size.
Enter the size, which is same or a little more than this size to the Disk Utility and create a new partition. Type does not matter much but best is “Mac OS X (Journalled)”.

Fix recovery partiton size

Now we need to make the new partition exactly the same size as our current recovery partition.
Firsts, become root in Terminal:
Dmitrys-MacBook-Pro:~ dima$ sudo bash
WARNING: Improper use of the sudo command could lead to data loss or the deletion of important system files. Please double-check your typing when using sudo. Type "man sudo" for more information.
To proceed, enter your password, or type Ctrl-C to abort.

Password:
bash-3.2#
Next find out the size of the existing recovery partition::
bash-3.2# diskutil info disk2s3
    Device Identifier:        disk2s3
    Device Node:              /dev/disk2s3
    Part of Whole:            disk2
    Device / Media Name:      Recovery HD
 
    Volume Name:              Recovery HD
    Escaped with Unicode:     Recovery HD
 
    Mounted:                  No
 
    File System Personality:  Journaled HFS+
    Type (Bundle):            hfs
    Name (User Visible):      Mac OS Extended (Journaled)
    Journal:                  Unknown (not mounted)
    Owners:                   Disabled
 
    Partition Type:           Apple_Boot
    OS Can Be Installed:      No
    Media Type:               Generic
    Protocol:                 USB
    SMART Status:             Not Supported
    Volume UUID:              E9E6BB78-29B5-3D44-B680-16374F020509
 
    Total Size:               650.0 MB (650002432 Bytes) (exactly 1269536 512-Byte-Blocks)
    Volume Free Space:        0 B (0 Bytes) (exactly 0 512-Byte-Blocks)
    Device Block Size:        512 Bytes
 
    Read-Only Media:          No
    Read-Only Volume:         Not applicable (not mounted)
    Ejectable:                Yes
 
    Whole:                    No
    Internal:                 No
Here I must note that you may not simply use numbers listed here: you must go through these steps because your Mac setup can be different! Note also that you need to get information for your external disk, not Mac's disk!
Resize your current partition on the Mac disk. Use the size you found (in bytes!):
bash-3.2# diskutil resizeVolume disk0s3 650002432B
Started partitioning on disk0s3 Recovery HD
Verifying the disk
Checking file system
Checking Journaled HFS Plus volume
Checking extents overflow file
Checking catalog file
Checking multi-linked files
Checking catalog hierarchy
Checking extended attributes file
Checking volume bitmap
Checking volume information
The volume Recovery HD appears to be OK
Resizing
Finished partitioning on disk0s3 Recovery HD
/dev/disk0
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *128.0 GB   disk0
   1:                        EFI                         209.7 MB   disk0s1
   2:                  Apple_HFS Macintosh HD            126.3 GB   disk0s2
   3:                  Apple_HFS Recovery HD             650.0 MB   disk0s3
Now a tricky part. We need to change the type of this partition. This involves noting partition parameters, deleting and creating a patition with gpt. Firsts, let find out parameters:
bash-3.2# gpt show 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  246725744      2  GPT part - 48465300-0000-11AA-AA11-00306543ECAC
    247135384     262144
   247397528    1269528       3  GPT part - 48465300-0000-11AA-AA11-00306543ECAC
    248667056    1402591
    250069647         32         Sec GPT table
    250069679          1         Sec GPT header 
Next, delete and add the partition. Note that we use values found on the previous step.
bash-3.2# gpt remove -b 247397528 -s 1269536 -t 48465300-0000-11AA-AA11-00306543ECAC disk0
disk0s3 removed
bash-3.2# gpt add -b 247397528 -s 1269536 -t 426F6F74-0000-11AA-AA11-00306543ECAC
disk0 disk0s3 added
Notice the different GUID. That is important because it tells OS X the type of the partition.

Copy data

Now we copy data from the existing recovery partition to the newly created partition:
bash-3.2# dd if=/dev/disk2s3 of=/dev/disk0s3
1269536+0 records in
1269536+0 records out
650002432 bytes transferred in 97.374958 secs (6675252 bytes/sec)
Check if everything is ok:
bash-3.2# diskutil list disk0
/dev/disk0
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *128.0 GB   disk0
   1:                        EFI                         209.7 MB   disk0s1
   2:                  Apple_HFS Macintosh HD            126.3 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3
Now you need to go to system preferences (Apple logo in the top menu bar) and set your start up disk. Resizing partitions removes the start up flag, so to boot into the updated disk you need to set it as startup disk again. You can reboot from there. If you want, boot from the recovery partition (by holding Command+R at boot time) to verify that it works.
That's all.

Conclusion

As you see, with a certain knowledge it is possible to recreate Apple's recovery partition. Why both these ways are not easy or totally safe, following them carefully can fix issues you have.
Good luck!

23 comments:

  1. Hi Dmitry,



    I faced the same problem last week, when I erased my SSD after a defect SATA cable.



    However, the way you describe first also did not work. After reinstalling and then restoring from TimeMachine, there was no Recovery Disk available anymore (same, of course, when recovering TM directly from the Lion DVD).



    After some research, I stumbled over the advice to just reinstall Lion *after* restoring the Backup over the existing installation. This keeps your data and recreates the Recovery Partition (in case you do not delete the partition manually ;-)).



    I'd say you did it the Linux way in the second explanation.. Would have been also the most obvious way for me, if I would be familar with Apple's diskutil and other tools.



    Thanks for sharing!

    Steffen

    ReplyDelete
  2. I did not see a way to reinstall without erasing data. Lion installer always tell me it will erase the disk with all data in it. May be I am missing something.

    ReplyDelete
  3. Maybe it tells you.. but it doesn't ;-)



    However, I must admit that some system files or preferences are reset. Optimal way seems to be somewhere between yours and mine ;-)



    Steffen

    ReplyDelete
  4. There's an easier way outlined at http://derflounder.wordpress.com/2011/08/04/creating-a-never-booted-10-7-image-with-included-recovery-hd-partition/ , in the Creating Recovery HD section. Doesn't even require a reboot! Ignore the rest.

    ReplyDelete
  5. Dave, you call that easier? Installing a couple of other programs to make a recovery partition without reboot? Hmmmm.



    But this link is definitely useful: http://www.afp548.com/article.php?story=20110730045914590

    ReplyDelete
  6. Hi Dmitry,



    when i send "gpt show disk0" command, i allways obtain "unable to open device 'disk0' : Resource busy"

    any idea? thanks!



    (boot from external usb disk lion install



    best regards

    ReplyDelete
  7. Hi Dmitry,



    on command "gpt show disk0" i always obtain an answer like "unable to show disk : : resource busy"

    what can i do (i've done it from an external boot, need to try from internal boot)?

    thanks and best regards

    ReplyDelete
  8. Philippe, eject it through Disk Utility :)

    ReplyDelete
  9. Thanks, i just found that at the same moment ;)



    all seem to be good now (after a few minuts afraid during copying data without any signal) : diskutil list give me an "Apple_Boot" type for my new "Recovery HD"



    BIG THANKS TO YOU!!!



    best regards

    ReplyDelete
  10. Hi Dmitry, thanks for this!



    A couple of questions if I may...



    "The line in bold is the one you need now." <-I don't see a line in bold in the first code box?



    Do you mean "Make about 700-800GB" or MB free?



    Also, do you think this method will work on a Hackintosh (Legit copy of Lion of course)?





    TIA...Dan

    ReplyDelete
  11. Hi Dimitri,



    Thanks for the great tutorial.



    It kind work for me, I have the Recovery HD partition working, but not with cmd+r, using alt key when restarting and then select the recovery partition.



    But in the OSx I cant make the update of the recovery HD that it was out a few days ago and I still can't activate Find My Mac in 10.7.2 because my recovery partition is damage, etc...



    Any ideas?



    Regards,



    Marco

    ReplyDelete
  12. Hi Dimitri,



    Thanks for the great tutorial.



    It kind work for me, I have the Recovery HD partition working, but not with cmd+r, using alt key when restarting and then select the recovery partition.



    But in the OSx I cant make the update of the recovery HD that it was out a few days ago and I still can't activate Find My Mac in 10.7.2 because my recovery partition is damage, etc...



    Any ideas?



    Regards,



    Marco

    ReplyDelete
  13. Not sure, update is ok for me and Find My Mac works too.

    ReplyDelete
  14. Be Advised:

    Symantec PGP 10.2.0 build 1672 when simply installed on OS X 10.7.2 will destroy (remove) the Lion "Recovery HD" partition on your Mac. The steps to restore this partition are painful at best.



    Here's what I did:



    1. Boot Mac into Recovery Mode (CMD + R)



    2. Wait for Internet Recovery to launch ... ... ... ...



    3. Attach an external hard drive large enough with enough free space to hold all the data on your boot partition



    4. Launch Disk Utility



    5. Select your boot partition (probably called "Macintosh HD")



    6. Select "New Image" from the Toolbar and save this image to your external hard drive (I recommend you save it out as READ ONLY and not compressed as performing compression cooks your computer and takes longer) -- this will take awhile ... ... ... ...



    7. Once your recovery image has been saved, select your hard drive, then click on Partition and remove, then add a new partition (using defaults). You'll need to rename it back to "Macintosh HD" -- ( removing and creating a new partition may not be required, but for sake of cleanness, it is what I did )



    8. Close Disk Utility then select "Reinstall Mac OS X", follow the steps, this will take awhile ... ... ... ...



    9. Your system will reboot. Go through the motions until you get to a point where you can cleanly reboot, then reboot and enter into Recovery Mode again (CMD + R) -- this should load very quickly as the reinstall restored you Recovery HD



    10. Once again, once you're presented with the recovery menu, select Disk Utility



    11. Select your boot partition (probably called "Macintosh HD"), then select Restore



    12. You'll have two fields, Source and Destination. For source, click on the "image" button and choose the image file you created under step 6 above. When mounting the image file, disk utility may attempt to verify the image, you may skip or stop that if you like as it will take a long time (or you may let it run if time isn't an issue). Simply drag your boot partition from the left column into the destination field, and select "restore" -- this will take awhile ... ... ... ...



    It took hours to backup and restore everything. I should note that before I created my recovery image I ran disk utility's "First Aid" on my boot partition. First ran "Repair Disk" then "Repair Disk Permissions" -- this is to ensure you get a clean image. Additionally, before I entered into recovery mode, I uninstalled Symantec PGP ( this may not be necessary, however, if you ever find yourself needing to reinstall Symantec PGP, you will be faced with performing these steps again -- in the meantime, I'm looking for an alternative to encrypt my external drives )



    Once the image has been restored, I run disk utility's "First Aid" again ("Repair Disk" and "Repair Disk Permissions") on my newly restored boot partition just to make sure all is good.



    Hope this helps.

    ReplyDelete
  15. great instructions. It worked for me to create a recovery partition after I lost it when I got a new hard disk and installed my timeMachine backup.



    I can now use FindMyMac again. Thanks so much!

    ReplyDelete
  16. Fantastic instructions Dmitry and Jason! Thanks a lot, helped me replacing my HD with a SSD.

    Jason, you are mentioning you are encrypting your external disk with PGP. I used PGP to encrypt my internal HD, had Apricorns HW encrypted disks for offsite backup and also used encrypted apple disk images. But since Lion Apple also offers to encrypt not only the system disk, you can encrypt and external disk with it. The only unfortunate thin, as far as I know, is that you have to format the disk which forces you to copy the data away first.

    Not sure if you new.

    ReplyDelete
  17. Worked like a charm. Thank you for the article! Also, If you've captured a dmg of a recovery partition, you can use DiskUtility to restore it. But you need to run the gpt remove/add commands afterwards to change the partition style from Apple_HFS to Apple_Boot.

    ReplyDelete
  18. Didn't work for me. I have a snow leopard partition as well as the lion partition. When I was done with the process, the partitions were as follows:

    #: TYPE NAME SIZE IDENTIFIER
    0: GUID_partition_scheme *500.1 GB disk0
    1: EFI 209.7 MB disk0s1
    2: Apple_HFS drive w Lion 212.4 GB disk0s2
    3: Apple_HFS drive w SL 280.7 GB disk0s4
    4: Apple_Boot Recovery HD 650.0 MB disk0s5

    So I reinstalled Lion, as someone in the comments here suggested. That worked great—none of my files were touched, and I now have a working recovery partition.

    Amusingly, the old recovery partition is still there. Here's the layout:

    #: TYPE NAME SIZE IDENTIFIER
    0: GUID_partition_scheme *500.1 GB disk0
    1: EFI 209.7 MB disk0s1
    2: Apple_HFS drive w Lion 212.4 GB disk0s2
    3: Apple_Boot Recovery HD 650.0 MB disk0s3
    4: Apple_HFS drive w SL 280.7 GB disk0s4
    5: Apple_Boot Recovery HD 650.0 MB disk0s5

    This makes me wonder whether the order of the drives matters: is it possible that my Recovery HD (now #5, was #4) didn't function because it wasn't immediately after my Lion drive (now #2, was #2)?

    ReplyDelete
  19. Every time I try to use the gpt command to remove and add the disk again, the disk always comes back as "Apple_HFS" I'm typing it in correctly withe numbers that I find on my computer, but it doesn't do it...

    ReplyDelete
  20. Thanks, this worked great for me. One suggestion: in the "gpt add" command, the "disk0" parameter should be at the end of the command line instead of on the following line with the results.

    ReplyDelete
  21. Thanks Dmitry. Losing Recovery HD is a pain in the ass.

    I solved the problem a different way, using Lion installer:
    http://hints.binaryage.com/recover-lost-recovery-hd-for-filevault

    ReplyDelete
  22. Dmitry,
    Thanks for your posting being new to the mac world and needing to figure out how to get a recovery partition on a customer mac so filevault would run i was going nuts trying to do this with dmtest and could never get it to work. dd to the rescue!

    I ended up running dd to a file (on a shared network drive) then booting the target machine (netboot) and writing the file back to the newly created disk0s3 partition.

    I didnt need to check the size of the recovery partition on the "donor" machine because i had the byte count of the file. divide by 512 and you get sectors just what gpt wants

    I can't thank you enough for taking the time to post this. I spent hours trying to get other methods to work.

    ReplyDelete