Can't delete Linux partition with Disk utility
Weeks ago I created a new partition for Linux installation positioned after the Mac OS partition. I used the new larger partition to install Ubuntu (which caused the partition to be further partitioned into root, home, swap etc).
Now I'd like to delete that partition and resize the Mac OS partition back to full disk. Disk utility won't let me for reasons unknown.
Here is diskutil list
:
/dev/disk0 (internal, physical):
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme *500.3 GB disk0
1: EFI EFI 209.7 MB disk0s1
2: FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF 80.0 GB disk0s2
3: Linux Filesystem 319.1 GB disk0s4
4: Linux Filesystem 399.5 MB disk0s5
5: Linux Swap 600.8 MB disk0s6
/dev/disk1 (synthesized):
#: TYPE NAME SIZE IDENTIFIER
0: APFS Container Scheme - +80.0 GB disk1
Physical Store disk0s2
1: APFS Volume Macintosh SSD 54.1 GB disk1s1
2: APFS Volume Preboot 45.3 MB disk1s2
3: APFS Volume Recovery 1.0 GB disk1s3
4: APFS Volume VM 8.6 GB disk1s4
/dev/disk2 (disk image):
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme +1.1 GB disk2
1: Apple_HFS IntelliJ IDEA CE 977.2 MB disk2s1
/dev/disk3 (disk image):
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme +1.6 GB disk3
1: Apple_HFS LibreOffice 1.4 GB disk3s1
/dev/disk4 (disk image):
#: TYPE NAME SIZE IDENTIFIER
0: Apple_partition_scheme +179.1 MB disk4
1: Apple_partition_map 32.3 KB disk4s1
2: Apple_HFS Wireshark 179.1 MB disk4s2
/dev/disk5 (disk image):
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme +10.1 MB disk5
1: Apple_HFS Android File Transfer 10.0 MB disk5s1
/dev/disk6 (disk image):
#: TYPE NAME SIZE IDENTIFIER
0: Apple_partition_scheme +196.8 MB disk6
1: Apple_partition_map 32.3 KB disk6s1
2: Apple_HFS Firefox 196.7 MB disk6s2
/dev/disk7 (disk image):
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme +1.6 GB disk7
1: Apple_HFS Android Studio 3.1.4 1.4 GB disk7s1
The partition type for the APFS partition has the incorrect value.
The simplest solution would be to use the gdisk
command supplied with Ubuntu to correct this problem.
If you can boot to Ubuntu from either the internal drive or from a live version, then proceed as follows.
- Boot to Ubuntu.
- Open a Terminal windows by entering the key combination control+option+T.
-
In the window, enter the following command.
sudo gdisk /dev/sda
-
Enter the following commands into
gdisk
. Basically, you will be changing the partition type to the correct GUID.t 2 7C3457EF-0000-11AA-AA11-00306543ECAC w y
Note: If you are using version 1.0.4 or newer of
gdisk
, then you can substituteaf0a
for7C3457EF-0000-11AA-AA11-00306543ECAC
Quit the Terminal application.
Reboot to macOS. You may need to hold down the option key at startup and then select macOS.
Removing Linux without Using the Disk Utility Application.
When removing partitions, entering commands in a Terminal application window can provide more satisfying results than relying on the Disk Utility application.
A linux partitions can be converted back to free space by entering the commands below.
-
Use the command below to get the identifier of a partition.
diskutil list
-
Use the command below to remove the partition. Replace
diskXsY
with the identifier determined in step 1.diskutil eraseVolume free none diskXsY
When finished removing partitions, the free space should be allocated back to macOS. In your case, this can be accomplished using the command given below.
diskutil apfs resizeContainer disk0s2 0
Alternate Method of Employing gdisk
The gdisk
command can now be accessed by a rEFInd bootable USB flash drive. Below are the instructions for creating the bootable flash drive.
- Goto the website The rEFInd Boot Manager: Getting rEFInd.
- Click on the link labeled
A USB flash drive image file
. Currently, this will download the file refind-flashdrive-0.11.3.zip, which defaults to the creation of the folderrefind-flashdrive-0.11.3
in theDownloads
folder. - Insert a flash drive in a USB port. Note: All the files on this flash drive will be erased.
- Open a Terminal application window.
-
Enter the command below to get the identifier of the flash drive.
diskutil list
-
Enter the commands below to copy the image to the flash drive. If necessary, replace
disk1
with the identifier from the previous step. Also, make the appropriate substitution, if your rEFInd version is be newer than0.11.3
.cd ~/Downloads/refind-flashdrive-0.11.3 diskutil unmountdisk disk1 sudo dd if=refind-flashdrive-0.11.3.img of=/dev/disk1
Next, follow the steps below fix the partition type error.
- Restart the Mac and immediately hold down the option key.
- Select the external drive icon labeled
EFI Boot
. - From the rEFInd menu, select the icon labeled
Start disk partitioning tool
. -
Enter the commands given below.
t 2 7C3457EF-0000-11AA-AA11-00306543ECAC w y
Select the icon labeled
Reboot Computer
.