Skip to content

Fixing the delete-root-at-boot script (re: your cohost ask) #5

@NireBryce

Description

@NireBryce

Hey, you don't have asks enabled and cohost doesn't notify for @-s, so I'm opening an issue.

Your workaround for postDeviceCommands (where you wrote just a script = block) didn't work for me (There was an issue for this, I wrote that it didn't work but then got busy with a move and forgot to follow up)

I found a way that does, from either one of the nixos disk related repos or their issues tab, unfortunately I can't find the source. But this seems to work, without needing to risk things with postDeviceCommands

# delete root at boot
  boot.initrd.postResumeCommands = lib.mkAfter ''
      mkdir -p /mnt
    # We first mount the btrfs root to /mnt
    # so we can manipulate btrfs subvolumes.
      mount -o subvol=/ /dev/mapper/enc /mnt

    # While we\'re tempted to just delete /root and create
    # a new snapshot from /root-blank, /root is already
    # populated at this point with a number of subvolumes,
    # which makes `btrfs subvolume delete` fail.
    # So, we remove them first.
    #
    # /root contains subvolumes:
    # - /root/var/lib/portables
    # - /root/var/lib/machines
    #
    # I suspect these are related to systemd-nspawn, but
    # since I don\'t use it I\'m not 100% sure.
    # Anyhow, deleting these subvolumes hasn\'t resulted
    # in any issues so far, except for fairly
    # benign-looking errors from systemd-tmpfiles.
      btrfs subvolume list -o /mnt/root |
      cut -f9 -d' ' |
      while read subvolume; do
        echo "deleting /$subvolume subvolume..."
        btrfs subvolume delete "/mnt/$subvolume"
      done &&
      echo "deleting /root subvolume..." &&
      btrfs subvolume delete /mnt/root

      echo "restoring blank /root subvolume..."
      btrfs subvolume snapshot /mnt/root-blank /mnt/root

    # Once we\'re done rolling back to a blank snapshot,
    # we can unmount /mnt and continue on the boot process.
        umount /mnt
  '';

Technically I think this should make it trigger on resume, but I've never had that happen. Which is, perhaps, it's own problem. Currently looking for where I found it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions