Rescue partition table after major update of Windows via parted

Recently I update the Windows system to 20H2 in my desktop. This machine has dual boot, the other system is Ubuntu 20.04 and the booting procedure is managed by grub2. During the update, the machine restarted itself and all of a sudden I’m encountered by a grub rescue prompt indicating my booting partition is missing.

Linux partition becomes UNALLOCATED SPACE after windows update

Windows is an ignorant system and when it updates itself, especially during major updates, some bad things might happen to your along side Linux system. Below are some possible outcomes:

  1. Grub2 cannot boot and went into rescue mode

    error: no such partition.
    Entering rescue mode...
    grub rescue>
    
  2. If you boot from a liveCD, or use some tools such as Ventoy to boot into Windows, you will find your Linux partition labeled as UNALLOCATED FREE SPACE.

It does not necessarily mean you’ve lost your data in your linux partition. It might be caused by windows updating program. Since it can not understand the linux file system, it deletes those entries and marked the space as unallocated in your partition table.

Luckily, some tools are available for rescuing/restoring the partition table. Here I introduce the tool parted and fdisk.

NOTE: Please DO NOT mess with the content in the missing partition if you don’t want to lose your data in that partition. Please DO NOT format those so-called unallocated free space as there’s a chance that formatting would wipe out the content in that partition.

NOTE: Next time, backup your partition table before updating windows

sudo sfdisk -d /dev/sda > parts.txt

You can save the parts.txt to another machine. And later restore it to the same device by running

sudo sfdisk /dev/sda < parts.txt

If you are lucky, grub rescue is engough

If the partition entries in the table is not deleted by windows, just changed some order, then it’s possible to restore your boot via just grub rescue. This rescue prompt is limited in function, there is no TAB completion.

  1. First of all, you can use set to check your current grub boot settings

    set
    

    An example output would be like

    prefix=(hd0,gpt6)/boot/grub
    root=hd0,gpt6
    

    And you can use ls to list current partitions that grub finds

    ls
    

    An example output would be like

    (hd0),(hd0,msdos3),(hd0,msdos2),(hd0,msdos1)
    
  2. Try these commands with all the printed partitions to test if grub can find the booting partition

    ls (hd0)/
    ls (hd0)/boot
    ls (hd0,msdos3)/
    ls (hd0,msdos3)/boot
    # etc... Iterate through all those previous partitions, test for location / and /boot
    

    Let’s say that grub recognize (hd0,gpt7). Then you can fix your grub parameters with

    set prefix=(hd0,gpt7)/boot/grub
    set root=(hd0,gpt7)
    

    and reboot the system.

But if your booting partition entries is deleted during updation of Windows, then you got to rescue that partition info before you can fix your bootloader.

Use parted to rescue your partition table

  1. The existing partition table can be shown by

    sudo fdisk -l
    
  2. The devices installed in your system are listed in /dev folder. Normally, SATA drives would be listed as sda, sdb, etc. And NVME drives would be listed as nvme0n1, nvme0n2, etc. In my case, the ubuntu partition at nvme0n1p3 is missing. So I start and tell parted to use the NVME device

    sudo parted /dev/nvme0n1    # start parted at a given device
    

    If you just use sudo parted, it will start at the first device based on its default order. If you want to change device later, you can use the select command in parted

    select /dev/sda    # select the 1st SATA drive.
    
  3. The prompt would become (parted) and you can use help to check a list of commands. First of all, you can print the partition table

    print    # print partition table of the currently chosen device.
    print all    # print partition tables for all the devices.
    

    My output looks like this

    Model: SAMSUNG MZVLB256HAHQ-000L7 (nvme)
    Disk /dev/nvme0n1: 256GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags: 
       
    Number  Start   End    Size    Type      File system     Flags
     1      1049kB  169GB  169GB   primary   ntfs            boot
     2      169GB   170GB  607MB   primary   ntfs            msftres
     3      170GB   255GB  85.9GB  extended
     4      255GB   256GB  626MB   primary   ntfs
    

    My linux partition is in Number 3, but now its info is not shown in this table. But don’t worry, we can recover it with rescue.

  4. Change the display unit of parted to sector, so it would be easier to specify the start and end point of your missing partition.

    unit s
    print
    

    Then the printed partition table would be like

    Model: SAMSUNG MZVLB256HAHQ-000L7 (nvme)
    Disk /dev/nvme0n1: 500118192s
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags: 
       
    Number  Start       End         Size        Type      File system     Flags
     1      2048s       329932191s  329930144s  primary   ntfs            boot
     2      329932800s  331118591s  1185792s    primary   ntfs            msftres
     3      331120638s  498892799s  167772162s  extended
     4      498892800s  500115455s  1222656s    primary   ntfs
    

    So we can see that the previous partition ends at sector 221118591 and the desired partition number 3 starts at sector 331120638. The parted’s rescue comand tries to rescue a partition that locates approximately between start and end. So we can use this to rescue the linux partition in Number 3 by

    rescue
    Start? 331120600
    End? 498892799
    Information: A ext4 partition was found at xxxxx
    Do you want to add it to the partition table?
    Yes/No/Cancel? Yes
    

    Then the missing partition is recued!

Repair Grub after rescuing partition table

It’s all possible that grub now has different partition number than before, which means you will still stuck at grub rescue prompt. But now the tool Boot Repair can fix the problem(It does no effect if you don’t fix your missing partition table first).

  1. First, boot into a live session, such as an Ubuntu LiveCD and install Boot Repair

    sudo add-apt-repository ppa:yannubuntu/boot-repair
    sudo apt update
    sudo apt install boot-repair
    
  2. Start boot-repair and just the recommended fix would suffice.

Reference

  1. grub rescue
    https://askubuntu.com/questions/654386/windows-10-upgrade-led-to-grub-rescue/655080#655080

  2. parted
    Answer from oldfred and Martin Thornton https://askubuntu.com/questions/654386/windows-10-upgrade-led-to-grub-rescue/655080#655080
    Manual for parted rescue https://www.gnu.org/software/parted/manual/html_node/rescue.html

Chao Cheng
Chao Cheng
Statistician

My research interests include applied statistics and machine learning.

Related