Code Monkey home page Code Monkey logo

Comments (4)

Misterio77 avatar Misterio77 commented on June 8, 2024

Sorry for the delay (life's been crazy)!

What I am wondering is how the service dependencies make sense.

I'm actually not using systemd in initrd for now, I've tried my best to make that unit work, but I always get the dependency cycles or race conditions :/

The main benefit it offers (for me, at least) is having plymouth start up before the LUKS password prompt, but it seems it's still missing a few features.

A fellow nixer (Tom) e-mailed me with a clever solution for this issue:

let
   rootfsDevice = "/dev/disk/by-label/${hostname}";
   wipeScript = ''
     counter=0

     # systemd initrd offers no way to hook in between
     # 'root device is ready' and 'mount the root device at /sysroot'.
     # Instead, lets poll for the root device.
     # https://github.com/systemd/systemd/issues/24904

     echo wipe: Waiting for ${rootfsDevice}
     while [ ! -e ${rootfsDevice} ]; do
       sleep 0.1
       counter=$((counter + 1))
       if [ $counter -ge 300 ]; then
           echo wipe: Timed out waiting for ${rootfsDevice}
           exit
       fi
     done
     echo wipe: Found ${rootfsDevice}. Wiping ephemeral.

     mkdir -p /btrfs
     mount -o subvol=/ ${rootfsDevice} /btrfs

     echo "Cleaning subvolume"
     btrfs subvolume list -o /btrfs/root | cut -f9 -d ' ' |
       while read subvolume; do
         btrfs subvolume delete "/btrfs/$subvolume"
       done && btrfs subvolume delete /btrfs/root

     echo "Restoring blank subvolume"
     btrfs subvolume snapshot /btrfs/root-blank /btrfs/root

     umount /btrfs
     rm -d /btrfs
   '';
in {
   options = {
     /* truncated */
   };
   config = {
     boot.initrd = {
       supportedFilesystems = [ "btrfs" ];
       systemd = {
         enable = true;
         emergencyAccess = true;
         initrdBin = with pkgs; [ coreutils btrfs-progs ];
         services.initrd-btrfs-root-wipe = {
           description = "Wipe ephemeral btrfs root";
           script = wipeScript;
           serviceConfig.Type = "oneshot";
           unitConfig.DefaultDependencies = "no";

           after = [ "cryptsetup.target" "initrd-root-device.target" ];
           before = [ "sysroot.mount" ];
           wantedBy = [ "sysroot.mount" ];
         };
       };
     };
   };

     /* truncated */
}

from nix-config.

Misterio77 avatar Misterio77 commented on June 8, 2024

Thank you for having this as well as the nix-starter-config repository

Thanks a lot for your kind words! I'm very glad to be able to help out :)

from nix-config.

omernaveedxyz avatar omernaveedxyz commented on June 8, 2024

That solution seems similar to my own, but perhaps a little more janky. Anyway, thanks for getting back to me, good to know that my solution is reasonable.

from nix-config.

Misterio77 avatar Misterio77 commented on June 8, 2024

I'm happy to say that I've finally found a elegant fix for this!

There's a .device unit for each and every device, so you can simply add the relevant one to your wipe service after/requires.

In my case, my btrfs partition is /dev/disk/by-lavel/${hostname}, so the relevant unit is dev-disk-by\x2dlabel-${hostname}.device (\x2d is how systemd escapes -). I found that out trying systemctl list-units -a | grep "dev-disk-by"

"dev-disk-by\\x2dlabel-${hostname}.device"

Note: if your unit has backslashes like mine, you have to either (if using regular nix strings) do double backslashes, or do the '' multiline string.

That means, "\foo" does not work, while "\\foo" and ''\foo'' does.

CC @omernaveedxyz

from nix-config.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.