Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fstrim: Add service #26205

Closed
wants to merge 1 commit into from
Closed

fstrim: Add service #26205

wants to merge 1 commit into from

Conversation

ghost
Copy link

@ghost ghost commented May 29, 2017

Motivation for this change

Provide fstrim service and timer, like other systemd distros do today https://www.digitalocean.com/community/tutorials/how-to-configure-periodic-trim-for-ssd-storage-on-linux-servers

Things done
  • Tested using sandboxing
    (nix.useSandbox on NixOS,
    or option build-use-sandbox in nix.conf
    on non-NixOS)
  • Built on platform(s)
    • NixOS
    • macOS
    • Linux
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nox --run "nox-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Fits CONTRIBUTING.md.

@mention-bot
Copy link

@gnidorah, thanks for your PR! By analyzing the history of the files in this pull request, we identified @edolstra, @offlinehacker and @bjornfor to be potential reviewers.

@edolstra
Copy link
Member

Can we use the upstream units included in util-linux? It looks like we're not currently installing them. If we did, we could just say

systemd.packages = [ pkgs.utillinux ];

to get the fstrim units.

@pSub pSub added the 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS label May 29, 2017
@danbst
Copy link
Contributor

danbst commented May 29, 2017

I guess a note like "Note, that it won't TRIM whole SSD, but only mounted filesystems. Unused partitions and unused LVM space should be TRIMmed manually." should be added.

Personally, I use an adapted script which takes care of unallocated LVM space.

  periodic_fstrim = { pkgs, ... }: {
    environment.etc."lvm/lvm.conf".text = ''
      devices {
        issue_discards = 1
      }
    '';

    systemd.services.periodic_fstrim = {
      startAt = "Sun 00:00:00";
      path = with pkgs; [ utillinux lvm2 ];
      script = ''
        echo TRIMming mounted filesystems...
        time fstrim -v --all
        echo TRIMming unused LVM space...
        lvm vgs -o vg_name,vg_free_count --noheadings | while read vgname free ; do
		      vgdev="/dev/$vgname/discard"
		      if [ -e "$vgdev" ]; then
			       echo "Logical volume $vgdev does already exist." >&2
			       echo "If it is just a leftover from a previous run then remove it with:" >&2
			       echo "lvm lvremove -f \"$vgdev\"" >&2
		      elif [ "$free" -gt 0 ]; then
			       lvm lvcreate -l100%FREE -n discard "$vgname"
			       cnt=5
			       while [ $cnt -gt 0 ] && ! time lvm lvremove -f "$vgdev" ; do
				       sleep 1
				       cnt=$(($cnt-1))
			       done
			       if [ "$cnt" -eq 0 ]; then
				       echo "Could not remove logical volume $vgdev" >&2
			       fi
		      fi
        done
        echo TRIMming finished
      '';
    };
  };

@ghost
Copy link
Author

ghost commented May 29, 2017

@edolstra Thank you much! Let's try it.

  1. At least for me, in utillinux package, $out in configureFlags is mistakenly not evaluated: https://github.com/NixOS/nixpkgs/blob/master/pkgs/os-specific/linux/util-linux/default.nix#L36
    configuring
    fixing libtool script ./config/ltmain.sh
    configure flags:
    ...
    --with-systemdsystemunitdir=$out/lib/systemd/system/
    
    so units are not shipped. Possible fix pushed, please correct if needed.
  2. Besides systemd.packages = [ pkgs.utillinux ]; I heard that an additional explicit wantedBy is needed in NixOS to deal dependencies, or am I wrong?

@ghost
Copy link
Author

ghost commented May 29, 2017

@danbst Interesting what to do with EFI partition, since fstrim still not supported by vfat. Distros what I've seen do nothing (don't even use discard flag for it).

@ghost
Copy link
Author

ghost commented May 30, 2017

I now get cycle detected error. Suggestions?

@edolstra
Copy link
Member

Regarding the cycle, where do the units get installed? If they're installed in the same output as the binaries, you shouldn't get a cycle. Then you also don't need to patch out the ExecStart paths and don't need to override ExecStart in the module. (The usefulness of the arguments option is debatable IMHO.)

Yes, a wantedBy is needed to pull in the timer.

@ghost
Copy link
Author

ghost commented May 30, 2017

@edolstra Thanks! Haven't meet split derivations before, moved units installation from out to bin and removed arguments option in module.

@ghost
Copy link
Author

ghost commented Jun 1, 2017

Seems to work here. BTW, do these systemd-related configure flags also need to be added to crossAttrs.preConfigure?

@joachifm
Copy link
Contributor

joachifm commented Jun 5, 2017

Given that this changes util-linux, I expect this will be a large rebuild. Perhaps should go via staging.

@edolstra
Copy link
Member

edolstra commented Jun 7, 2017

Merged to staging, thanks!

@edolstra edolstra closed this Jun 7, 2017
@ghost
Copy link
Author

ghost commented Jun 7, 2017

@edolstra Thank you!

@ghost ghost deleted the master2 branch June 7, 2017 10:09
@ghost
Copy link
Author

ghost commented Jun 12, 2017

@bjornfor Installed fstrim.timer from utillinux already do this.

@bjornfor
Copy link
Contributor

@gnidorah: I don't understand what that means.

@ghost
Copy link
Author

ghost commented Jun 12, 2017

@bjornfor
In short: upstream does this.
This a996fe8#diff-fab06f256b6956e04e4806ab5b9dabfbR34 says to use units from utillinux package.
And fstrim.timer unit from utillinux package defines Persistent=true like you suggested https://github.com/karelzak/util-linux/blob/stable/v2.29/sys-utils/fstrim.timer#L8

@ghost ghost mentioned this pull request Jun 13, 2017
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants