|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "How to repair a broken GRUB in Ubuntu?" |
| 4 | +date: "2017-08-28 13:35:42 +0530" |
| 5 | +tag: |
| 6 | + - Ubuntu |
| 7 | + - GRUB |
| 8 | + - How-to |
| 9 | +excerpt: Steps to restore your GRUB if you have formatted boot partition mistakenly |
| 10 | +--- |
| 11 | + |
| 12 | + |
| 14 | + |
| 15 | +## Background story |
| 16 | + |
| 17 | +Yesterday, I was having some free time after lunch. I decided to complete a long |
| 18 | +term plan of checking the compatibility of few [Freedom Operating |
| 19 | +Systems][freedom] with my workstation. From [this list][freedom_oses], I decided |
| 20 | +to check [Trisquel][trisquel] OS first. [Trisquel][trisquel] is freedom clone |
| 21 | +of world-famous OS Ubuntu. The simplest option was to prepare a live USB drive |
| 22 | +and boot the [Trisquel][trisquel] from it. I inserted the USB drive and |
| 23 | +instructed the [Gparted][gparted] to format it. Bang! That simple step ruined |
| 24 | +my entire Sunday evening. Instead of formatting my USB drive, I mistakenly |
| 25 | +formatted the boot partition! Without putting any extra measures, I formatted my |
| 26 | +root partition which is also a type [FAT][fat]. I was lucky enough to identify |
| 27 | +that I have formatted the partition from my SSD and not the USB drive. After |
| 28 | +taking advice from the people of [##linux][linux_irc_channel] I found, I will |
| 29 | +not be able to re-boot because the [GRUB][GRUB] is lost. In this post, I will |
| 30 | +describe the steps I followed to restore the [GRUB][GRUB]. |
| 31 | + |
| 32 | + |
| 33 | +## Procedure of restoring the GRUB |
| 34 | + |
| 35 | + |
| 36 | +You can restore your [GRUB][GRUB] using three methods: |
| 37 | + |
| 38 | +* Using GUI utility [“Boot-repair”][boot_repair]. This is the simplest step to |
| 39 | +follow first. Unfortunately, I was not able to fix my [GRUB][GRUB] using this |
| 40 | +method. |
| 41 | + |
| 42 | +* Boot from a live operating system, mount the infected boot partition and |
| 43 | +perform the steps of restoring the [GRUB][GRUB]. Since I identified the problem at an |
| 44 | +early stage, I didn’t restart my system until I was sure that nothing is |
| 45 | +broken. |
| 46 | + |
| 47 | +* Last is to run the steps of restoring the [GRUB][GRUB] from the command line |
| 48 | +if you haven’t reboot your system after formatting the boot partition. I will |
| 49 | +describe the steps of restoring your [GRUB][GRUB] using this method in this |
| 50 | +post. |
| 51 | + |
| 52 | +If you had rebooted and are unable to start the system then I will request to |
| 53 | +follow the steps described at [How to geek][how_to_geek] post rather than |
| 54 | +continuing here. If you are using Legacy BIOS rather than UEFI type then this |
| 55 | +post might not work for you. To identify which type your system has booted with, |
| 56 | +[follow this steps][bios_or_uefi]. |
| 57 | + |
| 58 | +## So let’s start! |
| 59 | + |
| 60 | +* **Identify the type of your boot partition:** You can use GUI utilities like |
| 61 | +[GParted][gparted] or any other of your choice. The boot partition is the very |
| 62 | +first partition of the drive you are using for booting your operating system. |
| 63 | + |
| 64 | +  |
| 66 | + |
| 67 | + In my case, it is `/dev/sda1`. This partition should be either [FAT32][fat32] |
| 68 | + or [FAT16][fat16]. If it is anything other than that you should format it to |
| 69 | + [FAT][fat] version of your choice. |
| 70 | + |
| 71 | +* **Assert `/boot/efi` is mounted:** Run below command at your terminal. |
| 72 | + |
| 73 | + ``` sudo mount /boot/efi ``` |
| 74 | + |
| 75 | + Sample output |
| 76 | + |
| 77 | + ``` |
| 78 | + $sudo mount /boot/efi/ |
| 79 | + mount: /dev/sda1 is already mounted or /boot/efi busy |
| 80 | + /dev/sda1 is already mounted on /boot/efi |
| 81 | + ``` |
| 82 | + If it is mounted, it will throw a warning indicating that the partition is |
| 83 | + already mounted. If it isn’t mounted, then the prompt will come back without |
| 84 | + any warning message. |
| 85 | + |
| 86 | +* **Next, restore and update the [GRUB][GRUB]:** Run below command at your terminal. |
| 87 | + |
| 88 | + ``` sudo grub-install && update-grub ``` |
| 89 | + |
| 90 | + Sample output |
| 91 | + ``` |
| 92 | + $ sudo grub-install && update-grub |
| 93 | + Installing for x86_64-efi platform. |
| 94 | + Installation finished. No error reported. |
| 95 | + ``` |
| 96 | + |
| 97 | + If there is any error, I will advise to not move further and try other |
| 98 | + options I mentioned above for restoring your [GRUB][GRUB]. |
| 99 | + |
| 100 | +* **Finally, replace the UUID of the formatted partition:** Find the **UUID** of |
| 101 | +your boot partition. Once you format the partition, the **UUID** of the |
| 102 | +partition is changed. You have to update the new **UUID** value at `/etc/fstab` |
| 103 | +file. Use below command to get the latest **UUID** of your boot partition. |
| 104 | + |
| 105 | + ```sudo blkid /dev/sda1``` |
| 106 | + |
| 107 | +  |
| 109 | + |
| 110 | + Copy the value of **UUID** which is between the double quotes. |
| 111 | + |
| 112 | + Open the `/etc/fstab` file with your desired editor and update the value with |
| 113 | + the existing **UUID** of `/dev/sda1`. I am doing this procedure using the |
| 114 | + [vim][vim] editor. You can choose any editor of your choice. |
| 115 | + |
| 116 | + ```sudo vim /etc/fstab``` |
| 117 | + |
| 118 | +  |
| 120 | + |
| 121 | + You will require the root privileges for writing to this file. |
| 122 | + |
| 123 | +We are done! Now when you will run `sudo ls -l /boot/efi`, you should able to |
| 124 | +identify the files beneath that directory. It is time to confirm by rebooting |
| 125 | +your system. |
| 126 | + |
| 127 | +## Vote of Thanks! |
| 128 | + |
| 129 | +I would like to thank [ioria][ioria] and [ducasse][ducasse], member of |
| 130 | +[#ubuntu][ubuntu_irc_channel] at Freenode, who invested a great amount of time |
| 131 | +to guide me in fixing this problem. [#ubutu][ubuntu_irc_channel] has great |
| 132 | +members who are always willing to help you. |
| 133 | + |
| 134 | +*Note: While mentioning the GRUB in this post, I actually mean the GRUB2.* |
| 135 | + |
| 136 | + |
| 137 | +###### Proofreaders: [Dhavan Vaidya](http://codingquark.com/), Pentode@##linux(Freenode), parsnip@#emacs (Freenode) |
| 138 | + |
| 139 | +[GRUB]: https://www.gnu.org/software/grub/ |
| 140 | +[trisquel]: https://trisquel.info/ |
| 141 | +[gparted]: http://gparted.org/ |
| 142 | +[fat]: https://en.wikipedia.org/wiki/File_Allocation_Table |
| 143 | +[fat32]: https://en.wikipedia.org/wiki/File_Allocation_Table#FAT32 |
| 144 | +[fat16]: https://en.wikipedia.org/wiki/File_Allocation_Table#FAT16 |
| 145 | +[linux_irc_channel]: https://freenode.linux.community/ |
| 146 | +[boot_repair]: https://help.ubuntu.com/community/Boot-Repair |
| 147 | +[vim]: http://www.vim.org |
| 148 | +[ubuntu_irc_channel]: irc://irc.freenode.net/ubuntu |
| 149 | +[ioria]: https://launchpad.net/~di-iorio |
| 150 | +[ducasse]: https://launchpad.net/~ducasse |
| 151 | +[freedom_oses]: https://www.gnu.org/distros/free-distros.en.html |
| 152 | +[freedom]: https://www.gnu.org/philosophy/free-sw.en.html |
| 153 | +[how_to_geek]: https://www.howtogeek.com/114884/how-to-repair-grub2-when-ubuntu-wont-boot/ |
| 154 | +[bios_or_uefi]: https://askubuntu.com/a/162896 |
0 commit comments