Welcome to the Linux Foundation Forum!

Lab 34.4. Recovering from GPT

albiurs
albiurs Posts: 49
edited November 25 in LFS207 Class Forum

Hi,

My Ubuntu VM is setup with a GPT partition table and LVM. This is what my system partitions look like:

alu@ubuntu-dt-vm:~$ lsblk | grep -v loop
NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sr0                        11:0    1   5.8G  0 rom  /media/alu/Ubuntu 24.04.1 LTS amd64
vda                       253:0    0   128G  0 disk 
\u251c\u2500vda1                    253:1    0     1M  0 part 
\u251c\u2500vda2                    253:2    0     2G  0 part /boot
\u2514\u2500vda3                    253:3    0   126G  0 part 
  \u2514\u2500ubuntu--vg-ubuntu--lv 252:0    0   126G  0 lvm  /

So I made backups form the GPT configuration from either fdisk and also gdisk. I saved the configs to a USB stick, as I could not access the backups anymore after destroying the GPT patition table.

So I destroyed the GPT by creating a new table in gdisk without any partitions. I'm not sure that was the right way to go though. After rebooting into the install media, I was able to recover the GPT partition table with the backups, but the system did not boot anyway. However, the partitions are back and I can mount and access the original root filesystem again.

Any Idea how to proceed with recovering GPT?

Thanks,
Urs

Comments

  • Hi Urs,

    Can you put here all the commands you ran for items 1, 2 and 4, please? I think you used 'vda' instead 'sda', as shown in the guide.

    Regards,
    Luis.

  • albiurs
    albiurs Posts: 49

    Hi Luis,

    The vda is the main paravirualized disk of this VM which runs on KVM. It was automatically created like this during the installation of Ubuntu. So there is no sda disk, only this single vda. But the disk type should not matter I guess.

    I did not run either of the commands from step 1, 2 or 4 as these commands are related to the MBR partition (dump of the first couple of bytes on the disk with dd). So I only mad backups of the GPT partition table and destroyed it afterwards. This is what I meant when I mentioned that I don't know if that is the right way to go. Do I have to follow the steps 1, 2 and 4 and dump/destroy/recover the MBR partition with dd even if the VM's disk is configured with GPT?

    Regards,
    Urs

  • albiurs
    albiurs Posts: 49

    Sorry, this was the wrong topic. Can you please delete my last post?
    Thanks,
    Urs

  • Wrong post deleted.

  • albiurs
    albiurs Posts: 49

    Hi Luis,

    Thanks for deleting the post. Would be nice, if you could follow up on my last post from November 25.

    Regards,
    Urs

  • Hi Urs, I see it now.

    So, if you have a GPT partition you need to use the 'sgdisk' tool, as you can see in the man sgdisk(8) page:

    -b, --backup=file
    Save partition data to a backup file. You can back up your current in-memory partition table to a disk file using this option. The resulting file is a binary file consisting of the protective MBR, the main GPT header, the backup GPT header, and one copy of the partition table, in that order. Note that the backup is of the current in-memory data structures, so if you launch the program, make changes, and then use this option, the backup will reflect your changes. If the GPT data structures are damaged, the backup may not accurately reflect the damaged state; instead, they will reflect GPT fdisk's first-pass interpretation of the GPT.

    And then:

    -l, --load-backup=file
    Load partition data from a backup file. This option is the reverse of the -b option. Note that restoring partition data from anything but the original disk is not recommended. This option will work even if the disk's original partition table is bad; however, most other options on the same command line will be ignored.

    From this:

    So I made backups form the GPT configuration from either fdisk and also gdisk.

    I see you already ran gdisk and made a backup, perhaps with something like this, right?

    sgdisk --backup=gptsave

    So, that file generated before by the sgdisk command is the one that you should load with 'sgdisk --load-backup=gspsave' '.

    Regards,
    Luis.

  • albiurs
    albiurs Posts: 49

    Hi Luis,

    Thanks, now I got it. Not sure what the issue was the last time. I guess it was just a typo or I forgot to save the restored GPT tables in interactive mode. However, with sgdisk it worked like a charm. I'd suggest to update the according chapter and Lab to cover also the process of backup and recovery of GPT. In case someone will have difficultis too, here are all the steps which I went through:

    1. Backup GPT to a USB thumb drive with sgdisk:
    root@ubuntu-dt-vm:~# lsblk
    NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
    loop0                       7:0    0     4K  1 loop /snap/bare/5
    loop1                       7:1    0  74.3M  1 loop /snap/core22/1564
    loop2                       7:2    0  73.9M  1 loop /snap/core22/1663
    loop3                       7:3    0 269.8M  1 loop /snap/firefox/4793
    loop4                       7:4    0   273M  1 loop /snap/firefox/5273
    loop5                       7:5    0  10.7M  1 loop /snap/firmware-updater/127
    loop6                       7:6    0  11.1M  1 loop /snap/firmware-updater/147
    loop7                       7:7    0 505.1M  1 loop /snap/gnome-42-2204/176
    loop8                       7:8    0  91.7M  1 loop /snap/gtk-common-themes/1535
    loop9                       7:9    0  10.5M  1 loop /snap/snap-store/1173
    loop10                      7:10   0  38.8M  1 loop /snap/snapd/21759
    loop11                      7:11   0   500K  1 loop /snap/snapd-desktop-integration/178
    loop12                      7:12   0   568K  1 loop /snap/snapd-desktop-integration/253
    loop13                      7:13   0  10.7M  1 loop /snap/snap-store/1218
    loop14                      7:14   0 150.3M  1 loop /snap/thunderbird/546
    loop15                      7:15   0 150.3M  1 loop /snap/thunderbird/562
    loop16                      7:16   0  44.3M  1 loop /snap/snapd/23258
    sda                         8:0    1  57.3G  0 disk 
    \u2514\u2500sda1                      8:1    1  57.3G  0 part /media/alu/ed845a58-3e7c-4652-a256-261e29f1e792
    sr0                        11:0    1   5.8G  0 rom  /media/alu/Ubuntu 24.04.1 LTS amd64
    vda                       253:0    0   128G  0 disk 
    \u251c\u2500vda1                    253:1    0     1M  0 part 
    \u251c\u2500vda2                    253:2    0     2G  0 part /boot
    \u2514\u2500vda3                    253:3    0   126G  0 part 
      \u2514\u2500ubuntu--vg-ubuntu--lv 252:0    0   126G  0 lvm  /
    root@ubuntu-dt-vm:~# 
    root@ubuntu-dt-vm:~# 
    root@ubuntu-dt-vm:~# sgdisk -b /media/alu/ed845a58-3e7c-4652-a256-261e29f1e792/sgdisk_gpt.bak /dev/vda
    The operation has completed successfully.
    root@ubuntu-dt-vm:~# ls -l /media/alu/ed845a58-3e7c-4652-a256-261e29f1e792/sgdisk_gpt.bak
    -rw-r--r-- 1 root root 17920 Dec  1 11:47 /media/alu/ed845a58-3e7c-4652-a256-261e29f1e792/sgdisk_gpt.bak
    root@ubuntu-dt-vm:~# 
    root@ubuntu-dt-vm:~# 
    root@ubuntu-dt-vm:~# umount /dev/sda1
    root@ubuntu-dt-vm:~# 
    
    1. Destroy GPT partition table and reboot
    root@ubuntu-dt-vm:~# sgdisk --print-mbr /dev/vda
    
    Disk size is 268435456 sectors (128.0 GiB)
    MBR disk identifier: 0x00000000
    MBR partitions:
    
    Number  Boot  Start Sector   End Sector   Status      Code
       1                     1    268435455   primary     0xEE
    root@ubuntu-dt-vm:~# 
    root@ubuntu-dt-vm:~# 
    root@ubuntu-dt-vm:~# sgdisk --print /dev/vda
    Disk /dev/vda: 268435456 sectors, 128.0 GiB
    Sector size (logical/physical): 512/512 bytes
    Disk identifier (GUID): 4472920E-CC02-4ACE-894D-ADBED8562A6E
    Partition table holds up to 128 entries
    Main partition table begins at sector 2 and ends at sector 33
    First usable sector is 2048, last usable sector is 268435422
    Partitions will be aligned on 2048-sector boundaries
    Total free space is 2015 sectors (1007.5 KiB)
    
    Number  Start (sector)    End (sector)  Size       Code  Name
       1            2048            4095   1024.0 KiB  EF02  
       2            4096         4198399   2.0 GiB     8300  
       3         4198400       268433407   126.0 GiB   8300  
    root@ubuntu-dt-vm:~#
    root@ubuntu-dt-vm:~#
    root@ubuntu-dt-vm:~# sgdisk -d 1 /dev/vda
    Warning: The kernel is still using the old partition table.
    The new table will be used at the next reboot or after you
    run partprobe(8) or kpartx(8)
    The operation has completed successfully.
    root@ubuntu-dt-vm:~# sgdisk -d 2 /dev/vda
    Warning: The kernel is still using the old partition table.
    The new table will be used at the next reboot or after you
    run partprobe(8) or kpartx(8)
    The operation has completed successfully.
    root@ubuntu-dt-vm:~# sgdisk -d 3 /dev/vda
    Warning: The kernel is still using the old partition table.
    The new table will be used at the next reboot or after you
    run partprobe(8) or kpartx(8)
    The operation has completed successfully.
    root@ubuntu-dt-vm:~# 
    root@ubuntu-dt-vm:~# 
    root@ubuntu-dt-vm:~# sgdisk --print /dev/vda
    Disk /dev/vda: 268435456 sectors, 128.0 GiB
    Sector size (logical/physical): 512/512 bytes
    Disk identifier (GUID): 4472920E-CC02-4ACE-894D-ADBED8562A6E
    Partition table holds up to 128 entries
    Main partition table begins at sector 2 and ends at sector 33
    First usable sector is 2048, last usable sector is 268435422
    Partitions will be aligned on 2048-sector boundaries
    Total free space is 268433375 sectors (128.0 GiB)
    
    Number  Start (sector)    End (sector)  Size       Code  Name
    root@ubuntu-dt-vm:~#
    root@ubuntu-dt-vm:~#
    root@ubuntu-dt-vm:~# reboot
    
    1. Normal boot fails. Hence, boot from rescue media and restore GPT with sgdisk:
    root@ubuntu:~# lsblk
    NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
    loop0    7:0    0   1.6G  1 loop /rofs
    loop1    7:1    0   471M  1 loop 
    loop2    7:2    0 874.4M  1 loop 
    loop3    7:3    0     4K  1 loop /snap/bare/5
    loop4    7:4    0  74.3M  1 loop /snap/core22/1564
    loop5    7:5    0 269.8M  1 loop /snap/firefox/4793
    loop6    7:6    0  10.7M  1 loop /snap/firmware-updater/127
    loop7    7:7    0 505.1M  1 loop /snap/gnome-42-2204/176
    loop8    7:8    0  91.7M  1 loop /snap/gtk-common-themes/1535
    loop9    7:9    0  10.5M  1 loop /snap/snap-store/1173
    loop10   7:10   0  38.8M  1 loop /snap/snapd/21759
    loop11   7:11   0   500K  1 loop /snap/snapd-desktop-integration/178
    loop12   7:12   0 149.6M  1 loop /snap/thunderbird/507
    loop13   7:13   0 116.8M  1 loop /snap/ubuntu-desktop-bootstrap/237
    sda      8:0    1  57.3G  0 disk 
    └─sda1   8:1    1  57.3G  0 part 
    sr0     11:0    1   5.8G  0 rom  /cdrom
    vda    253:0    0   128G  0 disk 
    root@ubuntu:~# 
    root@ubuntu:~# 
    root@ubuntu:~# mount /dev/sda1 /mnt
    root@ubuntu:~# cd /mnt
    root@ubuntu:/mnt# ls -l sgdisk_gpt.bak 
    -rw-r--r-- 1 root root 17920 Dec  1 10:47 sgdisk_gpt.bak
    root@ubuntu:/mnt# 
    root@ubuntu:/mnt# 
    root@ubuntu:/mnt# sgdisk -l /mnt/sgdisk_gpt.bak /dev/vda 
    Warning: The kernel is still using the old partition table.
    The new table will be used at the next reboot or after you
    run partprobe(8) or kpartx(8)
    The operation has completed successfully.
    root@ubuntu:/mnt# 
    root@ubuntu:/mnt# 
    root@ubuntu:/mnt# sgdisk -p /dev/vda
    Disk /dev/vda: 268435456 sectors, 128.0 GiB
    Sector size (logical/physical): 512/512 bytes
    Disk identifier (GUID): 4472920E-CC02-4ACE-894D-ADBED8562A6E
    Partition table holds up to 128 entries
    Main partition table begins at sector 2 and ends at sector 33
    First usable sector is 2048, last usable sector is 268435422
    Partitions will be aligned on 2048-sector boundaries
    Total free space is 2015 sectors (1007.5 KiB)
    
    Number  Start (sector)    End (sector)  Size       Code  Name
       1            2048            4095   1024.0 KiB  EF02  
       2            4096         4198399   2.0 GiB     8300  
       3         4198400       268433407   126.0 GiB   8300  
    root@ubuntu:/mnt#
    root@ubuntu:/mnt#
    root@ubuntu:/mnt# reboot
    

    So, that's it.

    Thanks again Louis for your support.

    Regards,
    Urs

  • luisviveropena
    luisviveropena Posts: 1,247

    Hi Urs,

    I'd suggest to update the according chapter and Lab to cover also the process of backup and recovery of GPT.

    Ok, it's a good suggestion, thanks.

    Thanks again Louis for your support.

    It's a pleasure! I'm glad that you are progressing in the course.

    Many regards,
    Luis.

Categories

Upcoming Training