How do I repartition my Mac to get rid of my dual boot setup?

I setup dual boot on my Mac (with Ubuntu) and want to get rid of it. The Disk Util GUI shows me:

Partition Table

However attempting to remove the disk0s4 or disk0s5 partitions results in:

Could not unmount partition to be deleted.

From the terminal, running diskutil list, I get:

/dev/disk0
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *251.0 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:          Apple_CoreStorage                         149.4 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3
   4:                 Linux Swap                         8.6 GB     disk0s4
   5: 11111111-1111-1111-1111-111111111111               92.1 GB    disk0s5
/dev/disk1
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:                  Apple_HFS Macintosh HD           *149.1 GB   disk1
                                 Logical Volume on disk0s2
                                 22222222-2222-2222-2222-222222222222
                                 Unencrypted

Essentially how do I go about reclaiming the space from those two partitions (the swap and my linux partition) without damaging anything Mac related?

And additionally, I think, but I'm not sure if the EFI partition was something to do with the Linux bootloader - do I need it (I've already reset the boot device to Mac).


Update 1, This is the state of /dev/disk0 after a failed attempt to remove the disk0s4 partition. The partition has definitely decreased in size, but I want it gone entirely:

$ gpt -r show /dev/disk0

gpt show: /dev/disk0: Suspicious MBR at sector 0
        size index   contents
           1        MBR
           1        Pri GPT header
          32        Pri GPT table
      409600    1   GPT part - 11111111-1111-1111-1111-111111111111
   291884360    2   GPT part - 22222222-2222-2222-2222-222222222222
     1269536    3   GPT part - 33333333-3333-3333-3333-333333333333
         880    
    16777216    4   MBR part 130
   179893087    
          32        Sec GPT table
           1        Sec GPT header

Update 2, after failure of diskutil cs resizeStack xxx 0g

diskutil corestorage resizeStack 265D8E12-2452-4D7F-BF56-2075FBA9773A 0g
265D8E12-2452-4D7F-BF56-2075FBA9773A does not appear to be a valid Core Storage Logical Volume UUID or disk

The output of diskutil cs list:

CoreStorage logical volume groups (1 found)
|
+-- Logical Volume Group 265D8E12-2452-4D7F-BF56-2075FBA9773A
    =========================================================
    Name:         Macintosh HD
    Status:       Online
    Size:         149444792320 B (149.4 GB)
    Free Space:   0 B (0 B)
    |
    +-< Physical Volume 8E81C240-7CE6-4688-A61C-66CFFBCF37F8
    |   ----------------------------------------------------
    |   Index:    0
    |   Disk:     disk0s2
    |   Status:   Online
    |   Size:     149444792320 B (149.4 GB)
    |
    +-> Logical Volume Family 09B78781-FCBC-4FFD-9430-2126A9FF24A1
        ----------------------------------------------------------
        Encryption Status:       Unlocked
        Encryption Type:         None
        Conversion Status:       NoConversion
        Conversion Direction:    -none-
        Has Encrypted Extents:   No
        Fully Secure:            No
        Passphrase Required:     No
        |
        +-> Logical Volume 2AB8ABA4-9A16-48C2-A085-D3FE3251E41F
            ---------------------------------------------------
            Disk:                  disk1
            Status:                Online
            Size (Total):          149082341376 B (149.1 GB)
            Conversion Progress:   -none-
            Revertible:            Yes (no decryption required)
            LV Name:               Macintosh HD
            Volume Name:           Macintosh HD
            Content Hint:          Apple_HFS

Sorry, I posted the wrong error, I also get:

diskutil corestorage resizeStack 2AB8ABA4-9A16-48C2-A085-D3FE3251E41F 0g
The Core Storage Logical Volume UUID is 2AB8ABA4-9A16-48C2-A085-D3FE3251E41F
Started CoreStorage operation
Error: -69722: You can't perform this resize unless it has a booter (target partition is probably too small)

As prompted by David Anderson, the output from sudo fdisk /dev/disk0 is:

Disk: /dev/disk0    geometry: 30515/255/63 [490234752 sectors]
Signature: 0xAA55
         Starting       Ending
 #: id  cyl  hd sec -  cyl  hd sec [     start -       size]
------------------------------------------------------------------------
 1: EE    0   0   1 - 1023 254  63 [         1 -     409639] <Unknown ID>
*2: AF 1023 254  63 - 1023 254  63 [    409640 -  291884360] HFS+        
 3: AF 1023 254  63 - 1023 254  63 [ 292294000 -    1269536] HFS+        
 4: 82 1023 254  63 - 1023 254  63 [ 293564416 -   16777216] Linux swap

Update, attempts to delete the deep partition from internet recovery mode.

enter image description here


Update: The current results of sudo got -r show /dev/disk0 and sudo fdisk /dev/disk0:

Peters-MBP:~ peter$ sudo gpt -r show /dev/disk0
Password:
gpt show: /dev/disk0: Suspicious MBR at sector 0
      start       size  index  contents
          0          1         MBR
          1          1         Pri GPT header
          2         32         Pri GPT table
         34          6         
         40     409600      1  GPT part - C12A7328-F81F-11D2-BA4B-00A0C93EC93B
     409640  291884360      2  GPT part - 53746F72-6167-11AA-AA11-00306543ECAC
  292294000    1269536      3  GPT part - 426F6F74-0000-11AA-AA11-00306543ECAC
  293563536        880         
  293564416   16777216      4  MBR part 130
  310341632  179893087         
  490234719         32         Sec GPT table
  490234751          1         Sec GPT header
Peters-MBP:~ peter$ sudo fdisk /dev/disk0
Disk: /dev/disk0    geometry: 30515/255/63 [490234752 sectors]
Signature: 0xAA55
         Starting       Ending
 #: id  cyl  hd sec -  cyl  hd sec [     start -       size]
------------------------------------------------------------------------
 1: EE    0   0   1 - 1023 254  63 [         1 -     409639] <Unknown ID>
*2: AF 1023 254  63 - 1023 254  63 [    409640 -  291884360] HFS+        
 3: AF 1023 254  63 - 1023 254  63 [ 292294000 -    1269536] HFS+        
 4: 82 1023 254  63 - 1023 254  63 [ 293564416 -   16777216] Linux swap 

Solution 1:

Basically you have to delete the linux partitions (disk0s4 and disk0s5) and then expand your CoreStorage volume (Macintosh HD) to the full size.

  1. Boot to Internet Recovery Mode by pressing altcmdR while booting
  2. Open Terminal in the menubar -> Utilities
  3. enter diskutil list and diskutil cs list to get the partition and the CoreStorage listing.
  4. Now unmount all disks except the OS X Base System (~1.3 GB) and its accompanying volumes (eleven really small sized volumes). First the CoreStorage disk (probably disk2, check the size: ~149.1 GB!) and then the physical disk:

    diskutil unmountdisk /dev/disk2
    diskutil unmountdisk /dev/disk0
    
  5. Enter gpt -r show /dev/disk0 to get the partition table
  6. Remove the two linux partitions (choose the proper index numbers of the two linux partitions):

    gpt remove -i 5 /dev/disk0
    gpt remove -i 4 /dev/disk0
    

    If the deletion of the Linux swap volume fails try to reformat it with diskutil eraseVolume JHFS+ Blank /dev/disk0s4 and continue with step 4.

  7. Reenter gpt -r show /dev/disk0 to check if all Linux partitions are deleted
  8. Remount any previously unmounted volume in the reverse order with

    diskutil mountdisk /dev/disk0
    diskutil mount LVUUID #LVUUID=UUID of the logical volume found previously
    
  9. Enter diskutil cs resizeStack LVUUID size with LVUUID: UUID of the logical volume (usually the last UUID found in the diskutil cs list listing you got previously) and size: the final size of your CoreStorage volume.

    Example:

    diskutil cs resizeStack 2AB8ABA4-9A16-48C2-A085-D3FE3251E41F 0g 
    

    to expand it to the full available size (0g is a magic number here).

  10. Finally you might delete the GRUB loader if it was installed on disk0s1.

Solution 2:

Update 3

A simpler way, to accomplish the same results as Update 1, would be to use the following commands.

INPUT=$(printf "f 4\ne 4\n0\ne 3\n0\ne 2\n0\ne 1\nee\n\n1\n\nq\ny")
sudo fdisk -e /dev/disk2 <<<"$INPUT";echo

Update 2

Based on the current state of your MBR and GUID partition tables, I recommend the following steps to finish fixing your computer.

I do not think it is necessary to boot to Internet Recovery Mode before entering these commands.

  1. Make a Time Machine or other form of backup. (Just in case.)

  2. Execute my Update 1 on /dev/disk0. Upon completion, the output of sudo fdisk /dev/disk0 should appear as shown below.

    Peters-MBP:~ peter$ sudo fdisk /dev/disk0
    Disk: /dev/disk0    geometry: 30515/255/63 [490234752 sectors]
    Signature: 0xAA55
             Starting       Ending
     #: id  cyl  hd sec -  cyl  hd sec [     start -       size]
    ------------------------------------------------------------------------
     1: EE    0   0   2 - 1023 254  63 [         1 -  490234751] <Unknown ID>
     2: 00    0   0   0 -    0   0   0 [         0 -          0] unused      
     3: 00    0   0   0 -    0   0   0 [         0 -          0] unused      
     4: 00    0   0   0 -    0   0   0 [         0 -          0] unused  
    
  3. Execute the command given below.

    diskutil cs resizeStack 2AB8ABA4-9A16-48C2-A085-D3FE3251E41F 0g 
    

Update 1

For anyone's information, a hybrid GPT drive can be changed back to a normal GPT drive by using the fdisk command in interactive mode. The interactive commands are universal. These commands are as follows.

f 4
e 4
0
e 3
0
e 2
0
e 1
ee
n
1
[number of blocks] #You just have to hit the enter key to choose the given default
q
y

For example, say I have a flash drive on /dev/disk2 and the output of sudo fdisk /dev/disk2 was the following.

Steelhead:~ davidanderson$ sudo fdisk /dev/disk2
Password:
Disk: /dev/disk2    geometry: 978/128/63 [7892087 sectors]
Signature: 0xAA55
         Starting       Ending
 #: id  cyl  hd sec -  cyl  hd sec [     start -       size]
------------------------------------------------------------------------
 1: EE 1023 254  63 - 1023 254  63 [         1 -     409639] <Unknown ID>
 2: AC 1023 254  63 - 1023 254  63 [    409640 -    6915528] <Unknown ID>
 3: AB 1023 254  63 - 1023 254  63 [   7325168 -     262144] Darwin Boot 
*4: 0B 1023 254  63 - 1023 254  63 [   7587840 -     303104] Win95 FAT-32

Entering the command sudo fdisk -e /dev/disk2 followed by the above interactive commands would result in the following output.

Steelhead:~ davidanderson$ sudo fdisk -e /dev/disk2
fdisk: could not open MBR file /usr/standalone/i386/boot0: No such file or directory
Enter 'help' for information
fdisk: 1> f 4
Partition 4 marked active.
fdisk: 1> e 4
         Starting       Ending
 #: id  cyl  hd sec -  cyl  hd sec [     start -       size]
------------------------------------------------------------------------
*4: 0B 1023 254  63 - 1023 254  63 [   7587840 -     303104] Win95 FAT-32
Partition id ('0' to disable)  [0 - FF]: [B] (? for help) 0
Partition 4 is disabled.
fdisk:*1> e 3
         Starting       Ending
 #: id  cyl  hd sec -  cyl  hd sec [     start -       size]
------------------------------------------------------------------------
 3: AB 1023 254  63 - 1023 254  63 [   7325168 -     262144] Darwin Boot 
Partition id ('0' to disable)  [0 - FF]: [AB] (? for help) 0
Partition 3 is disabled.
fdisk:*1> e 2
         Starting       Ending
 #: id  cyl  hd sec -  cyl  hd sec [     start -       size]
------------------------------------------------------------------------
 2: AC 1023 254  63 - 1023 254  63 [    409640 -    6915528] <Unknown ID>
Partition id ('0' to disable)  [0 - FF]: [AC] (? for help) 0
Partition 2 is disabled.
fdisk:*1> e 1
         Starting       Ending
 #: id  cyl  hd sec -  cyl  hd sec [     start -       size]
------------------------------------------------------------------------
 1: EE 1023 254  63 - 1023 254  63 [         1 -     409639] <Unknown ID>
Partition id ('0' to disable)  [0 - FF]: [EE] (? for help) ee
Do you wish to edit in CHS mode? [n] 
Partition offset [0 - 7892087]: [63] 1
Partition size [1 - 7892086]: [7892086] 
fdisk:*1> q
Writing current MBR to disk.
Device could not be accessed exclusively.
A reboot will be needed for changes to take effect. OK? [n] y
Steelhead:~ davidanderson$ 

Now when entering 'sudo fdisk /dev/disk2' the result is

Steelhead:~ davidanderson$ sudo fdisk /dev/disk2
Password:
Disk: /dev/disk2    geometry: 978/128/63 [7892087 sectors]
Signature: 0xAA55
         Starting       Ending
 #: id  cyl  hd sec -  cyl  hd sec [     start -       size]
------------------------------------------------------------------------
 1: EE    0   0   2 - 1023 254  63 [         1 -    7892086] <Unknown ID>
 2: 00    0   0   0 -    0   0   0 [         0 -          0] unused      
 3: 00    0   0   0 -    0   0   0 [         0 -          0] unused      
 4: 00    0   0   0 -    0   0   0 [         0 -          0] unused  

I do recognize the UEFI specification requires the Ending hd entry for partition 1 to be a value of 255. In the table above, the value is 254. In the case of OS X, I do not think this discrepancy matters.


Original Answer:

I tested my answer using a flash drive. This is not the same a core storage on disk0. Even though klanomath's answer seems more complex than mine, he does have far more experience with core storage than I have. I would not hesitate to use his answer over mine.

The EFI partition (disk0s1) existed before you installed Ubuntu. The installation of Ubuntu probably added the GRUB boot loader to this partition. You could mount this partition (using the Disk Utility application) and delete EFI/ubuntu folder containing these files, but it would be easier to just leave them where they are.

To remove Ubuntu, you do need to delete disk0s4 and disk0s5. You should be able to do this from the Disk Utility application. Remove disk0s4 first, followed by disk0s5. Both operations may take quite a long time to complete. Be patient.