OK, I fixed it. Brief steps in case others come across the same problem.
1. Check if parted can read the main GPT table. Run it on the device:
Code:
~# parted /dev/mmcblk0
GNU Parted 3.4
Using /dev/mmcblk0
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Error: The backup GPT table is corrupt, but the primary appears OK, so that will be used.
OK/Cancel? OK
Model: MMC 4FPD3R (sd/mmc)
Disk /dev/mmcblk0: 3909MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 4194kB 3146kB boot msftdata
2 4194kB 12.6MB 8389kB kernel1
3 12.6MB 851MB 839MB ext4 rootfs1
4 851MB 860MB 8389kB kernel2
5 860MB 1699MB 839MB ext4 rootfs2
6 1699MB 1707MB 8389kB kernel3
7 1707MB 2546MB 839MB ext4 rootfs3
8 2546MB 2554MB 8389kB kernel4
9 2554MB 3393MB 839MB rootfs4
10 3393MB 3908MB 515MB linux-swap(v1) swap swap
The above output shows the primary table is sound, since I can see all the partitions, but the backup table is not OK.
2. Install the package "gptfdisk" and run it:
Code:
~# gdisk /dev/mmcblk0
GPT fdisk (gdisk) version 1.0.8
Caution: invalid backup GPT header, but valid main header; regenerating
backup header from main header.
Warning! Main and backup partition tables differ! Use the 'c' and 'e' options
on the recovery & transformation menu to examine the two tables.
Warning! One or more CRCs don't match. You should repair the disk!
Main header: OK
Backup header: ERROR
Main partition table: OK
Backup partition table: ERROR
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: damaged
****************************************************************************
Caution: Found protective or hybrid MBR and corrupt GPT. Using GPT, but disk
verification and recovery are STRONGLY recommended.
****************************************************************************
Command (? for help): p
Disk /dev/mmcblk0: 7634944 sectors, 3.6 GiB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): F291D54B-F806-44C3-9011-91F4FB4D63D6
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 7634910
Partitions will be aligned on 2048-sector boundaries
Total free space is 4029 sectors (2.0 MiB)
Number Start (sector) End (sector) Size Code Name
1 2048 8191 3.0 MiB 0700 boot
2 8192 24575 8.0 MiB 8300 kernel1
3 24576 1662975 800.0 MiB 8300 rootfs1
4 1662976 1679359 8.0 MiB 8300 kernel2
5 1679360 3317759 800.0 MiB 8300 rootfs2
6 3317760 3334143 8.0 MiB 8300 kernel3
7 3334144 4972543 800.0 MiB 8300 rootfs3
8 4972544 4988927 8.0 MiB 8300 kernel4
9 4988928 6627327 800.0 MiB 8300 rootfs4
10 6627328 7632895 491.0 MiB 8200 swap
3. Quit with 'q' without making any changes and create a dump of the GPT tables to be able to restore from it if something goes wrong.
Code:
:~# sgdisk -b /media/usb/backup/mmcblk0.gpt /dev/mmcblk0
The operation has completed successfully.
If you need to restore the original GPT table, replace '-b' with '-l' in the above command syntax.
4. Run gdisk once more and press 'r' to enter the Recovery and Transformation menu. Then check if the backup table can be read and how it differs with the primary table, by pressing 'c' and then 'p' to view it. In my case the loaded backup table was empty, which explains why parted and gdisk outputs complained.
5. I pressed 'd' to use the primary GPT header and then 'e' to rebuild the backup with the data in the primary table. 'v' will verify the currently loaded table is in good order. 'w' to write the backup table at the end of the disk and 'q' to quit. Careful to get these steps in the correct order, in case the corruption is on the primary table rather than the backup. You do not want to overwrite healthy data.
A reboot showed all was fine and 'v' verified the status of the GPT table:
Code:
~# gdisk /dev/mmcblk0
GPT fdisk (gdisk) version 1.0.8
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help): p
Disk /dev/mmcblk0: 7634944 sectors, 3.6 GiB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): F291D54B-F806-44C3-9011-91F4FB4D63D6
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 7634910
Partitions will be aligned on 2048-sector boundaries
Total free space is 4029 sectors (2.0 MiB)
Number Start (sector) End (sector) Size Code Name
1 2048 8191 3.0 MiB 0700 boot
2 8192 24575 8.0 MiB 8300 kernel1
3 24576 1662975 800.0 MiB 8300 rootfs1
4 1662976 1679359 8.0 MiB 8300 kernel2
5 1679360 3317759 800.0 MiB 8300 rootfs2
6 3317760 3334143 8.0 MiB 8300 kernel3
7 3334144 4972543 800.0 MiB 8300 rootfs3
8 4972544 4988927 8.0 MiB 8300 kernel4
9 4988928 6627327 800.0 MiB 8300 rootfs4
10 6627328 7632895 491.0 MiB 8200 swap
Command (? for help): v
No problems found. 4029 free sectors (2.0 MiB) available in 2
segments, the largest of which is 2015 (1007.5 KiB) in size.