Skip to content

Instantly share code, notes, and snippets.

@delameter
Last active December 28, 2021 14:47
Show Gist options
  • Save delameter/e41475b2533311ff71e35f2d434d5e17 to your computer and use it in GitHub Desktop.
Save delameter/e41475b2533311ff71e35f2d434d5e17 to your computer and use it in GitHub Desktop.

Dual-boot Win7/Ubuntu

How to: shred your filesystem and then
assemble it back (if lucky enough)

Starting point

Oct 2021 | ASUS B85M-G, Samsung SSD 860 250gb (MBR), Intel Core i5-4570 Haswell, MSI GeForce GTX 960

  • dual-boot GRUB loader at /sdb
  • /sdb1 is Windows loader working in legacy mode
  • /sdb2 is Windows 7 x64
  • /sdb3 is NTFS data partition
  • /sdb4 is EFI extended logical partition 😭
    • /sdb5 (part of /sdb4) is FAT32 bootable EFI partition
    • /sdb6 (same) is Ext4 partition with broken Ubuntu 20 (it launched only once)
Device     Boot     Start       End   Sectors         Size Id Type              
/dev/sdb1            2048    206847    204800    104857600  7 HPFS/NTFS/exFAT   
/dev/sdb2          208896 266244095 266035200 136210022400  7 HPFS/NTFS/exFAT   
/dev/sdb3       266244096 327682047  61437952  31456231424  7 HPFS/NTFS/exFAT   
/dev/sdb4       327684094 488396799 160712706  82284905472  5 Extended          
/dev/sdb5  *    327684096 328732671   1048576    536870912 ef EFI (FAT-12/16/32)
/dev/sdb6       328734720 488396799 159662080  81746984960 83 Linux             

Issue

After launching Win7 it probably fucked up EFI partition or GRUB loader or modified its own bootsector NO, it was graphics card issue. Ubuntu shows black screen or says something like "finished removing stale ext4 metadata" in recovery mode, but that's it. Windows works. Fast boot is enabled, Compatibility Support Module allows booting from both UEFI devices and Legacy OPROM devices.

Operating Ubuntu is required, and preferably without Windows reinstall.

Possible solution

  • Boot up Ubuntu from USB drive
  • Make partition table backups
  • Make whole partition's backups (you never know)
  • Purge broken Ubuntu partition and (possibly also broken) UEFI partition - whole /sdb4
  • Create one ext4 partition (MBR allows max 4 primary partitions per physical disk, this will be 4th)
  • Install to this partition Ubuntu 20 @ legacy mode
  • ..PROFIT

TL;DR RESULTS WHERE

Dual-boot backup/restore recipe book (sort of)

Acquiring information

blkid -c /dev/null -d [OPTIONS]

...v/sdb2: LABEL="Win7SSD" UUID="B44059DC4059A5C0" TYPE="ntfs" PARTUUID="0307eed0-02"
/dev/sdb3: LABEL="Dev" UUID="52380386380367F5" TYPE="ntfs" PARTUUID="0307eed0-03"
/dev/sdb5: UUID="2842-B924" TYPE="vfat" PARTUUID="0307eed0-05"
/dev/sdb6: UUID="4c99afe4-a712-4ba2-8740-1ea5499517c7" TYPE="ext4" PARTUUID="0307eed0-06"...

fdisk -l --bytes [OPTIONS]

# root@ubuntu:~$ fdisk -l --bytes -o Device,Start,End,Sectors,Size,Type,Type-UUID,Attrs,Name,UUID /dev/sda

...k /dev/sda: 5.47 TiB, 6001175126016 bytes, 11721045168 sectors
Disk model: WDC WD60EFRX-68L
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 11AD7D07-153F-481C-B469-5FA600A45122

Device      Start         End     Sectors          Size Type                 Type-UUID                            Attrs Name                         UUID
/dev/sda1      34      262177      262144     134217728 Microsoft reserved   E3C9E316-0B5C-4DB8-817D-F92DF00215AE       Microsoft reserved partition 39A8A3DA-A3B2-426A-816B-7F4B76C412C8
/dev/sda2  264192 11721043967 11720779776 6001039245312 Microsoft basic data EBD0A0A2-B9E5-4433-87C0-68B6B72699C7       Basic data partition         4683F487-C5BF-4F64-B16E-059374924D48...

lsblk --f [OPTIONS]

...E   FSTYPE   LABEL                    UUID                                 FSAVAIL FSUSE% MOUNTPOINT
sda                                                                                          
├─sda1                                                                                       
└─sda2 ntfs     Vault-2a                 6236336A36333E7B                                    
sdb                                                                                          
├─sdb1 ntfs     Зарезервировано системой 049C56899C567560                       75.8M    24% /mnt/sdb1
├─sdb2 ntfs     Win7SSD                  B44059DC4059A5C0                                    
├─sdb3 ntfs     Dev                      52380386380367F5...

lshw -class disk [OPTIONS]

...*-disk
       description: ATA Disk
       product: WDC WD121KRYZ-01
       vendor: Western Digital
       physical id: 0.0.0
       bus info: scsi@4:0.0.0
       logical name: /dev/sdc
       version: 1H01
       serial: 8DGDRYGY
       size: 10TiB (12TB)
       capabilities: gpt-1.00 partitioned partitioned:gpt
       configuration: ansiversion=5 guid=c593f097-c3f7-49a2-b9f8-7825eecc500d logicalsectorsize=512 sectorsize=4096...

Partition tables (MBR) manipulations

How do I back-up Linux partition with sfdisk command?

To backup /dev/sda partition table, enter:

sfdisk -d /dev/sda > sda.partition.table.12-30-2015.txt

Copy sda.partition.table.12-30-2015.txt to NFS mounted share or a USB pen drive.

How do I restore the Linux partition with sfdisk command to the disk?

sfdisk /dev/sda < sda.partition.table.12-30-2015.txt
sfdisk /dev/sda < /path/to/usb/pen/sda.partition.table.12-30-2015.txt
# or
sfdisk -f /dev/sda < /media/usb/sda.partition.table.12-30-2015.txt

MBR / image format

# backup (always 512b for MBR)
dd if=/dev/sda of=mbr.img bs=512 count=1

# recovery
dd if=mbr.img of=/dev/sda

How do I backup /dev/sda GPT partition table to a file?

To save partition data to a backup file called /root/sda_partition_table_12_30_2015, run:

sgdisk --backup={/path/to/file} {/dev/device/here}
sgdisk --backup=/root/sda_partition_table_12_30_2015 /dev/sda

How do I restore GPT partition table from a file?

To restore the backup use:

sgdisk --load-backup={/path/to/file} {/dev/device/here}
sgdisk --load-backup=/media/usb/sda_partition_table_12_30_2015 /dev/sda

Partition table & data / image format

# mount readonly
mount -r /dev/sdb1 /mnt/sdb1

# partition -> image
dd if=/dev/sdb1 of=sdb1.img bs=4M

# mount writable
mount -w /dev/sdu1 /mnt/sdu1

# image -> partition
dd if=sdb1.img of=/dev/sdu1 bs=4M conv=fdatasync

# unmount
umount /mnt/sdu1

WANT ALL AND IMMEDIATELY

boot-repair GUI

# https://help.ubuntu.com/community/Boot-Repair
sudo add-apt-repository ppa:yannubuntu/boot-repair && \
     apt-get update && \
     apt-get install -y boot-repair && \
     boot-repair

Ubuntu Disks GUI

# https://askubuntu.com/a/589060
sudo apt-get install gnome-disk-utility
gnome-disks

Hesitation

fdisk + lsblk

Disk /dev/sdb: 232.91 GiB, 250059350016 bytes, 488397168 sectors
Disk model: Samsung SSD 860
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos  <<< mbr
Disk identifier: 0x0307eed0

Device     Boot     Start       End   Sectors   Size Id Type               grub address + LABEL + notes          UUID
/dev/sdb1            2048    206847    204800   100M  7 HPFS/NTFS/exFAT    (hd1,msdos0) winbootmgr ("reserved")  049C56899C567560
/dev/sdb2          208896 266244095 266035200 126.9G  7 HPFS/NTFS/exFAT    (hd1,msdos1) Win7SSD                  B44059DC4059A5C0
/dev/sdb3       266244096 327682047  61437952  29.3G  7 HPFS/NTFS/exFAT    ssd dev part                          52380386380367F5
/dev/sdb4       327684094 488396799 160712706  76.6G  5 Extended           sdb5+sdb6 logical disk???             ???
/dev/sdb5  *    327684096 328732671   1048576   512M ef EFI (FAT-12/16/32) ubuntu uefi partition                 2842-B924
/dev/sdb6       328734720 488396799 159662080  76.1G 83 Linux              (hd1,msdos6) ubuntu primary           4c99afe4-a712-4ba2-8740-1ea5499517c7

Wrong UUID

This one raised some new questions:

root@ubuntu:~$ cat /mnt/sdb5/EFI/ubuntu/grub.cfg
search.fs_uuid fc67b057-1157-4562-9ab0-c2f03f42225b root hd1,msdos6  <<< fc67b057-who-the-f-is-dat

Boot attempts

Boot config UEFI, fast boot enabled / legacy disabled
ubuntu (sdb4) goes back to bios
EFI OS (sdb4??) goes back to bios
EFI Sandisk (ubuntu usb) ok
Boot config UEFI, fast boot disabled / legacy only
ubuntu (sdb6?) black screen & blinking cursor
ubuntu recovery mode (sdb6?) ext4 message, freezes
Win7 SSD (sdb1) ok
Sandisk (no-EFI) (ubuntu usb) ok

Just do it

Today I Learned:

  • you can't boot one OS from hdd/ssd in UEFI mode, and another (after system restart) - in legacy mode
  • UEFI bootsector MUST be on primary partition of the disk (mine was on logical)
# disabled fast boot
# set compatibility device mode to boot from legacy OPROM devices only ! U SHALL NOT PASS
# after wiping broken parts and creating new:
├─sdb1 ntfs     Зарезервировано системой 049C56899C567560                                    
├─sdb2 ntfs     Win7SSD                  B44059DC4059A5C0                                    
├─sdb3 ntfs     Dev                      52380386380367F5                                    
└─sdb4 ext4     Ubuntu                   a63c3998-cd54-4bb0-bdb7-c5f316ac8363

/dev/sdb1            2048    206847    204800    104857600  7 HPFS/NTFS/exFAT
/dev/sdb2          208896 266244095 266035200 136210022400  7 HPFS/NTFS/exFAT
/dev/sdb3       266244096 327682047  61437952  31456231424  7 HPFS/NTFS/exFAT
/dev/sdb4  *    327682048 488396799 160714752  82285953024 83 Linux
# boot-repair restores mbr table:
/dev/sdb1  *         2048    206847    204800    104857600  7 HPFS/NTFS/exFAT
...v/sdb4       327682048 488396799 160714752  82285953024 83 Linux

# windows boots up - OK
# live usb ubuntu boots up, installing it to sdb4:
The partition tables of the following devices are changed:
 SCSI4 (0,0,0) (sdb)
The following partitions are going to be formatted:
 partition 4 of SCSI4 (0,0,0) (sdb) as ext4
# post-install stage, booted up GRUB -> sdb4:
Device     Boot     Start       End   Sectors   Size Id Type
/dev/sdb1  *         2048    206847    204800   100M  7 HPFS/NTFS/exFAT
...v/sdb4       327682048 488396799 160714752  76.7G 83 Linux

# installed ubuntu wifi drivers
# switched win7/ubuntu and back twice

Results

Both systems seem to work (for now). Also, there was a surprise for me, that in case of multiple loaders on one disk and enabled EFI exactly EFI partition will be prefered, even if it is in the middle of nowhere.

As a post scriptum:

This method worked for me after a particularly nasty case of a Windows 10 update overwriting GRUB and then breaking its own boot loader. Following this procedure got both Windows and Linux back. In my case it was Linux Mint 18.1, and the boot-repair menus looked slightly different, but it all worked fine. Thanks!

TheBigH May 28 '17 at 22:53

Screenshot from 2021-10-19 19-23-53

@delameter | 15-19 Oct 2021

Everyone was in surprise

The system worked for one day and then was refusing to show anything else other than black screen and blinking cursor (after choosing Ubuntu in GRUB menu).

It turns out that (probably) boot loaders / MBR tables / partitions were completely fine and all of this was for nothing 😡.

The problem was the default nVidia drivers for my GeForce GTX 960. I found this answer which was kind of similar to my issue, tried it without any hopes to success anymore, but it worked and I was able to boot Ubuntu and log in:

# I selected Ubuntu in GRUB menu, pressed E and then a bit changed Ubuntu load config:
-linux	/boot/vmlinuz-5.11.0-37-generic root=UUID=4c99afe4-a712-4ba2-8740-1ea5499517c7 ro  quiet splash $vt_handoff
+linux	/boot/vmlinuz-5.11.0-37-generic root=UUID=4c99afe4-a712-4ba2-8740-1ea5499517c7 ro  quiet splash nomodeset nouveau.blacklist=1  $vt_handoff

Then it was clear that graphics card is idle: HDMI-monitor was blank, VGA-DVI-monitor was working in low resolution. After reading this and this I was able to demolish existing nvidia drivers and installed new ones (command log is approximate):

nvidia-detector
nvidia-smi
sudo ubuntu-drivers devices
lspci -k | grep -iEA4 "VGA|3D|Display"
nvidia-settings 
sudo apt install nvidia-driver-470

Even though the result of all this was the very same package nvidia-driver-470, it was good enough for the system to flawlessly boot after restarting it 😵.

The moral of the story is: when nothing else works, you should start disabling all of your hardware one-by-one; at least it can get you closer to understanding what's going on.

@delameter | 25 Oct 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment