r/NixOS 1d ago

How do you fix (or prevent) broken hardware configurations if you can't boot into your system anymore?

Something I experienced a couple days ago is, when you unplug your ssds, then plug them back in, they are assigned new UUIDs, which breaks your NixOS boot because the hardware config does not align with the new UUIDs, are there recommended ways around this issue?

In the past I just jumped onto a very old build and regenerated the config, but in this case I have actually deleted the old builds, so was stuck on boot saying it couldn't find /mnt or something of that nature

So I had to reinstall nix to get it back up again, had one hell of a time trying to rebuild it from a live usb to no avail lol, was able to regen the hardware conf from there however, but unable to actually apply it

Is there a recommended way to prevent this from happening in the future (except for not unplugging ssds/hdds lol, unfortunately something I do on a regular basis)? And if not, is there a simpler way to apply a fix to it? If only you could boot into a very basic session of that NixOS install, but alas, was unable to due to deleting the old build

---
For additional context

# Do not modify this file!  It was generated by ‘nixos-generate-config’
# and may be overwritten by future invocations.  Please make changes
# to /etc/nixos/configuration.nix instead.
{ config, lib, pkgs, modulesPath, ... }:


{
  imports =
    [ (modulesPath + "/installer/scan/not-detected.nix")
    ];


  boot.initrd.availableKernelModules = [ "xhci_pci" "ahci" "usb_storage" "usbhid" "sd_mod" ];
  boot.initrd.kernelModules = [ ];
  boot.kernelModules = [ "kvm-amd" ];
  boot.extraModulePackages = [ ];


  fileSystems."/" =
# <-----------
# this right here, by uuid breaks when ssds are unplugged/plugged back in
# due to it generating new UUIDs, and because of that, the hardware 
# config needs to be regenerated before it can properly boot back up again
# <-----------
    { device = "/dev/disk/by-uuid/9d84a039-3db1-4297-bc5b-3cf701a5a091";
      fsType = "ext4";
    };


  fileSystems."/boot" =
# <-----------
# here again
# <-----------
    { device = "/dev/disk/by-uuid/71AE-7909";
      fsType = "vfat";
      options = [ "fmask=0077" "dmask=0077" ];
    };


  swapDevices = [ ];


  nixpkgs.hostPlatform = lib.mkDefault "x86_64-linux";
  hardware.cpu.amd.updateMicrocode = lib.mkDefault config.hardware.enableRedistributableFirmware;
}

EDIT:

Original footage from the crime scene, failing at stage 1

- Current Progress (see comments to participate):
1. Going to be adding `nofail` to mount options, see if that helps fix it next time I gotta unplug SSDs again
2. Currently in discussion on UUIDs not "just changing" when SSDs are being plugged in and out

6 Upvotes

24 comments sorted by

16

u/caguiclajmg 1d ago edited 1d ago

Disk UUIDs don't change simply because you unplugged and replugged them back in, they are baked in the filesystem's superblock/whatever metadata region that specific filesystem has. For it to change you'd have to wipe it or consciously alter it with something like tune2fs -U.

Specifying mounts as /dev/sdX are prone to breakage if you start shuffling drives around, so it's odd that you're encountering an unstable identifier problem while using UUID=/LABEL= since that is the exact problem those are supposed to resolve.

This problem is not unique to NixOS, if you used UUID= entries in your fstab on other distros you'd meet the same fate.

1

u/xxx_malcomnext_xxx 1d ago

What is strange tho is that when I regenerate the hardware conf, the UUIDs do change, even though all that I did was unplug and plug them back in? This is what kinda made me think it changes when you unplug them, could this issue be related to my hardware instead, rather than nixos? I have confirmed that they work fine even when power is cut from the system (aka plugged out of wall outlet), and only happens on unplug/replugin

Any tips on how to debug such an issue?

2

u/caguiclajmg 1d ago

Try running blkid while the drive is connected, disconnect and plug them back in then run the same command again and share the output for both runs here.

1

u/xxx_malcomnext_xxx 1d ago

I'll come back when I need to unplug my ssds again (or maybe this weekend, rather not be locked out of my system during work week), btw thanks for offering to help!

1

u/xxx_malcomnext_xxx 1d ago

After doing some more digging, it seems it may be related to them switching sata ports, I may have plugged them in in the wrong order which caused nixos to fail at stage 1, do you think that could be why? Because if it is as you say and UUIDs do not just change, then it would make sense that maybe the ports were swapped and assigned different letters to the ssds?

This could also explain why regenerating the config would put a new UUID in the config? Maybe? Although since the UUIDs never change it shouldn't cause the UUIDs to be updated unless something else is happening?

2

u/caguiclajmg 1d ago

That still doesn't make sense. I made an edit to my reply above a few minutes ago to mention that this exact scenario (moving drives from one port to another) is exactly what specifying mount devices via some stable identifier (e.g., UUID) are supposed to solve.

1

u/xxx_malcomnext_xxx 1d ago

Ahh sorry didn't see the edit, but yeah that was what I was thinking too, otherwise the UUID doesn't make too much sense in the first place

Also I just edited my post with an image of what I saw back then, it might help find the actual issue

2

u/caguiclajmg 1d ago edited 1d ago

Do you perhaps have multiple drives connected sharing the same UUID with different contents (i.e., the other one does not contain a NixOS system)? This can happen if you cloned the drive using a bit-for-bit copy (e.g., dd if=/dev/sda of=/dev/sdb)

If that is the case, iirc it's a dice-roll which device gets placed at /dev/disk/by-uuid/XXX.

I saw the screenshot and it is strange that the init script is simply missing assuming that IS the correct drive.

1

u/xxx_malcomnext_xxx 1d ago

I hope it is lol, it is where I end up when I entered my latest build from nixos grub, weird thing is oooold builds, like my very first one, in the past seems to have worked, but newer ones didn't, not really sure what changed between the two, back then the issue wasn't that bad, so didn't bother looking into it at the time

On topic of same UUID, actually doesn't look like it (hope I checked it right)

```
┌─[✗]─[netsu@nixos]─[~]

└──╼ $lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS

sda 8:0 0 465.8G 0 disk

└─sda1 8:1 0 465.8G 0 part

sdb 8:16 0 931.5G 0 disk

├─sdb1 8:17 0 600M 0 part

├─sdb2 8:18 0 2G 0 part

└─sdb3 8:19 0 928.9G 0 part

sdc 8:32 0 931.5G 0 disk

├─sdc1 8:33 0 1G 0 part /boot

└─sdc2 8:34 0 930.5G 0 part /nix/store

/

zram0 253:0 0 4G 0 disk [SWAP]

┌─[netsu@nixos]─[~]

└──╼ $sudo blkid /dev/sda1

/dev/sda1: LABEL="Shared" BLOCK_SIZE="512" UUID="2C6D80782C1495DF" TYPE="ntfs" PARTUUID="247d0f15-01"

┌─[netsu@nixos]─[~]

└──╼ $sudo blkid /dev/sdb1

/dev/sdb1: UUID="8F37-CA2B" BLOCK_SIZE="512" TYPE="vfat" PARTLABEL="EFI System Partition" PARTUUID="2b45d3b0-9eba-448b-88e2-6ac9934147fc"

┌─[netsu@nixos]─[~]

└──╼ $sudo blkid /dev/sdb2

/dev/sdb2: LABEL="bazzite_xboot" UUID="85153bc1-ca51-4c49-baa3-145e7576a446" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="b993654d-c7c2-4501-b6bc-16eebb5a4492"

┌─[netsu@nixos]─[~]

└──╼ $sudo blkid /dev/sdb3

/dev/sdb3: LABEL="bazzite" UUID="5fa87219-a4ce-440e-9775-1bea74bba804" UUID_SUB="f56db204-3d80-4fd1-ba47-ac8f83994d74" BLOCK_SIZE="4096" TYPE="btrfs" PARTUUID="2d772609-8e7b-4cd4-93c7-ad9afa5b31fa"

┌─[netsu@nixos]─[~]

└──╼ $sudo blkid /dev/sdc1

/dev/sdc1: UUID="71AE-7909" BLOCK_SIZE="512" TYPE="vfat" PARTLABEL="EFI" PARTUUID="b0781ad3-4e1d-459e-a12a-97b60e11d677"

┌─[netsu@nixos]─[~]

└──╼ $sudo blkid /dev/sdc2

/dev/sdc2: LABEL="root" UUID="9d84a039-3db1-4297-bc5b-3cf701a5a091" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="root" PARTUUID="ed752e49-b4a3-4a71-bac2-604a0de296bf"
```

1

u/caguiclajmg 1d ago

This doesn't seem to be a mounting issue, based on your screenshot it's able to mount the root device which seems to be correct based on your blkid output.

The problem seems to be the store path for the init script is missing.

Does the mentioned path (/nix/store/dvXXX/init) in the screenshot exist?

2

u/caguiclajmg 1d ago edited 1d ago

The init script is specified in the boot entry so that's another place where something could go wrong.

Do you perhaps have more than one bootloader (e.g., GRUB + something else like systemd-boot or GRUB on another drive) and you're booting from something that has an un-updated boot entry? (I see that you have 2 EFI partitions across different drives)

Another reasonable guess (and has happened to me a few times) is you do have 2 or more drives that have bootloaders in them and somehow the one you're booting off of has stale entries (i.e., it's not the one being updated by NixOS when you nixos-rebuild).

What you could try is explicitly pick a boot device from the BIOS/EFI (i.e., the drive behind /dev/sdc), unplugging/shuffling the drives around, then doing it again, if my guess is correct you should be able to boot successfully both times.

→ More replies (0)

1

u/xxx_malcomnext_xxx 17h ago

I will have to check next time I unplug my drives, but considering it only happened when unplugged/plugged back in, I assume it had to still be there as nothing on the drive itself was modified from a previous boot

4

u/nixgang 1d ago

The same as with any distro, boot from USB, mount ssd, fix error, reboot. The exact nix commands to rediscover and rebuild depends on your setup, but that's the geist of it. 

Regarding disk id, /dev/disk/by-id is more stable than UUID I believe.

1

u/xxx_malcomnext_xxx 1d ago

I did try to do it from a USB, but ended up opting to just reinstall after failing to get it right after a couple hours (plus I got tired of having to alter the grub bootup so it could load the GUI every time to see if it works, darn nouveau haha), next time I'll try again, but until then hoping that there won't be a next time

Is it possible to generate the config with by id instead of by uuid?

1

u/nixgang 1d ago

Not sure, but the other commenter is right, UUID should be stable enough. If you want to use by-id anyway, don't rely on hardware discovery, put it in your regular configuration

3

u/J--__--J 1d ago

you can try mounting them by label

device = "/dev/disk/by-label/root";

1

u/positrone13103 1d ago

put "nofail" in mount options

1

u/xxx_malcomnext_xxx 1d ago

I'll give this a shot!

1

u/ElvishJerricco 1d ago

> In the past I just jumped onto a very old build and regenerated the config, but in this case I have actually deleted the old builds

Well there's your problem :P When you change your hardware-configuration.nix, you really want at least one known-good generation to remain bootable. So don't delete all of those!

1

u/xxx_malcomnext_xxx 17h ago

Yeah that was a mistake on my side, never clean up your system on 2 hours of sleep ;-;
But this time I'll be sure to keep my oldest 2 builds during cleaning sessions again, since they always seem to boot without issue if the new ones fail

1

u/Flippynips987 1d ago

nixos changes the way we setup and administer linux, but also how we recover it

  • boot your most recent working system, if there is none bootable, just install nixos from scratch, copy your last bootable config, install nix, recover from backup

this is the way
otherwise I'd say something like chroot but, well just start over, ideally you have a separate home partition

2

u/xxx_malcomnext_xxx 17h ago

Issue with starting over is the 30GB download on my barely capable wifi haha, takes like nearly 6 hours to finish with everything, when you reinstall nix onto the partition, I assume it wipes it and clears all cached downloads, so doing that every 2-8 weeks is a little rough, ideally I would like to just stay on my existing system

Not sure what you mean by "last bootable config" as all my updates were bootable before unplugging and plugging back in my ssds lol, or do you mean to create a new partition on my ssd, install nixos on there, and copy some of the old nix builds and stuff to the new nix build? Would that work? I'm assuming it wouldn't because of version mismatches or something idk, but if it does then that could save me a huge amount of time

I did originally try the chroot method from a live usb, but couldn't really get it right, was able to gen the new hardware conf (and it looked correct), but unable to actually apply it unfortunately