Skip to content

Implement systemd.services.<name>.createDirs #2189

Closed

Description

@offlinehacker

Currently we don't have interface that would create data directories before service is started. People used to handle this on different ways, that worked more or less successfully:

  • Using user.createHome = true. That didn't used to work very well, when new interface for users was introduced services used to break.
  • Using activation scripts. I like this solution, because it's simple, but it's not used very much.
  • Using something i call dual behaviour. So using systemd option PermissionsStartOnly = true systemd starts pre and post startup scripts as root. The problem is that you now have to use su to become user and then you have to handle dual behaviour if services are started as root (with systemd) or with some other implementation. I especially dislike that behaviour, it also does not work on user services implementation i use (https://github.com/kiberpipa/nix-rehash/tree/master/nix-services) in osx.

Now the question is how should i implement this?

  • @shlevy proposed that pre and post start should be split into new service and service would depend on both of them. The problem here is that if we would only need this for creating data dirs, this is kind of overdesign, but on the other hand pre and post scripts could be started independantly.
  • Use activation scripts to create directories
  • ... ?

I would also need some help implementing this. I think this option is very much needed.

Activity

  1. bluescreen303 commented on Apr 9, 2014

    @bluescreen303
    Contributor

    Jaka Hudoklin [email protected] writes:

    • Use activation scripts to create directories
    • ... ?

    Activation scripts are run on every boot and on switch-to-profile.
    They are concatenated and run sequentially before any systemd
    initialization takes place.
    Compared to using any systemd solution, this is a disadvantage, because
    systemd will run everything in parallel and not hold off stuff that does
    not need any dirs. But I too like the simplicity of activation scripts.

    some ideas:

    • turn activation scripts into a run-only-once solution.
      Basically the script should write some version/hash marker to
      /etc/.activation when it finishes. The first line of the script checks
      this marker and exits instantly if it's still current.
    • turn the activation scripts into 1 big run-as-root systemd service and
      have modules that expect directories to be created depend on it.
      This at least allows systemd to initialize other stuff concurrently
      while setting up directories for units that need it.
    • use a clever combination of
      ConditionPathIsDirectory, OnFailure, Before & Requires
      see systemd.unit(5) :

    some-daemon.service
    [Unit]
    ConditionPathIsDirectory=/var/some-daemon
    OnFailure=some-daemon-setup.service
    [Service]
    User=some-daemon-user
    ExecStart=...

    some-daemon-setup.service (runs as root by default)
    [Unit]
    Before=some-daemon.service
    Requires=some-daemon.service
    [Service]
    Type=oneshot
    ExecStart=/path/to/setup/script

    setup-script should then create the directories or perform other tasks.
    The Before+Require combo should do the trick of restarting some-daemon
    afterwards. It's very important that the script exits with non-zero exit
    status if /var/some-daemon already exists. Otherwise you might end up in
    a loop, because the setup script will run if the daemon fails. The
    daemon fails if its directory is not there, but there might be other
    (unexpected) reasons for failure as well. As the setup script/unit is
    only capable of fixing the directory-related failure, we don't want the
    daemon to restart on other failures. Exiting non-zero will cancel the
    daemon restart because the Before= precondition hasn't been met.

    I haven't tested any of this, but it won't be far off. The main
    advantage over any other solution is that it will only run once, if the
    precondition (directory) isn't there and it will use systemd's very
    efficient built-in functionality to check this, which is way cheaper
    than running any shell script.

    I would also need some help implementing this. I think this option is very much needed.


    Reply to this email directly or view it on GitHub:
    #2189

  2. offlinehacker commented on Apr 9, 2014

    @offlinehacker
    ContributorAuthor

    I really like your second idea, i would also like to get more opinions, so
    i could make a best possible solution.
    On Apr 9, 2014 10:12 PM, "Mathijs Kwik" [email protected] wrote:

    Jaka Hudoklin [email protected] writes:

    • Use activation scripts to create directories
    • ... ?

    Activation scripts are run on every boot and on switch-to-profile.
    They are concatenated and run sequentially before any systemd
    initialization takes place.
    Compared to using any systemd solution, this is a disadvantage, because
    systemd will run everything in parallel and not hold off stuff that does
    not need any dirs. But I too like the simplicity of activation scripts.

    some ideas:

    • turn activation scripts into a run-only-once solution.
      Basically the script should write some version/hash marker to
      /etc/.activation when it finishes. The first line of the script checks
      this marker and exits instantly if it's still current.
    • turn the activation scripts into 1 big run-as-root systemd service and
      have modules that expect directories to be created depend on it.
      This at least allows systemd to initialize other stuff concurrently
      while setting up directories for units that need it.
    • use a clever combination of
      ConditionPathIsDirectory, OnFailure, Before & Requires
      see systemd.unit(5) :

    some-daemon.service
    [Unit]
    ConditionPathIsDirectory=/var/some-daemon
    OnFailure=some-daemon-setup.service
    [Service]
    User=some-daemon-user
    ExecStart=...

    some-daemon-setup.service (runs as root by default)
    [Unit]
    Before=some-daemon.service
    Requires=some-daemon.service
    [Service]
    Type=oneshot
    ExecStart=/path/to/setup/script

    setup-script should then create the directories or perform other tasks.
    The Before+Require combo should do the trick of restarting some-daemon
    afterwards. It's very important that the script exits with non-zero exit
    status if /var/some-daemon already exists. Otherwise you might end up in
    a loop, because the setup script will run if the daemon fails. The
    daemon fails if its directory is not there, but there might be other
    (unexpected) reasons for failure as well. As the setup script/unit is
    only capable of fixing the directory-related failure, we don't want the
    daemon to restart on other failures. Exiting non-zero will cancel the
    daemon restart because the Before= precondition hasn't been met.

    I haven't tested any of this, but it won't be far off. The main
    advantage over any other solution is that it will only run once, if the
    precondition (directory) isn't there and it will use systemd's very
    efficient built-in functionality to check this, which is way cheaper
    than running any shell script.

    I would also need some help implementing this. I think this option is
    very much needed.


    Reply to this email directly or view it on GitHub:
    #2189

    Reply to this email directly or view it on GitHubhttps://github.com//issues/2189#issuecomment-40010811
    .

  3. shlevy commented on Apr 10, 2014

    @shlevy
    Member

    FWIW, my suggestion was actually just to make the systemdirs bit a separate service, not all of preStart. But it's not a huge difference.

    The biggest problem with putting this into activation scripts is that it slows down boot. If I have a service that doesn't run all the time or doesn't block other services, but it's state directory is on an NFS mount, we'd have to wait for that filesystem to be up and mounted to even start systemd.

  4. alexanderkjeldaas commented on Jul 14, 2014

    @alexanderkjeldaas
    Contributor

    Whatever is chosen, the scripts that create directories should check and ensure that the uid/gid is still correct upon activation.

    In NixOS, during development and in various branches, service uids and gids do change. Activation scripts that do chmod/chown ensure that services work. Scripts that assume that uids/gids never change break.

  5. alexanderkjeldaas commented on Jul 15, 2014

    @alexanderkjeldaas
    Contributor

    It seems like createDirs could map to the /etc/tmpfiles.d systemd service as it does exactly this. Even if it is documented to be used typically for /tmp and /run, the semantics seems like it could work for anything.

  6. offlinehacker commented on Jul 15, 2014

    @offlinehacker
    ContributorAuthor

    I like the idea, tmpfiles.d could get the job done :)

    On Tue, Jul 15, 2014 at 10:11 PM, Alexander Kjeldaas <
    [email protected]> wrote:

    It seems like createDirs could map to the /etc/tmpfiles.d systemd service
    as it does exactly this. Even if it is documented to be used typically for
    /tmp and /run, the semantics seems like it could work for anything.

    —
    Reply to this email directly or view it on GitHub
    #2189 (comment).

    -----BEGIN PGP PUBLIC KEY BLOCK-----
    Version: GnuPG v1.4.11 (GNU/Linux)

    mQENBFEY1PEBCADPOfERF2wo4qeoq9L1m2z4pKfWqNd4B6BsoFUWPNd7BXmY+9JG
    jJddSkmYobWec7XjAFTBL0Xbttt+rK9SIED2dCOmU1FYMQElhGlM3PNA3kaiQFeV
    ijgH318GCfZzDd0dWa5TN/IshVeWXwgngsIEmZTVf1VSeb3eO3B8Fxe3zsSLUq0b
    71MmU5eLVP9pMkm5V5BTYp+lV70FIekKygkKq+uTDo1csWUatbs4Qvgv37Bymy2t
    oTwOBXGoinQk5N/6asR1jWs3vKv0L0SruoZy/kEG/jXb4l2OZUP85EVMganYKouE
    OchVmcmhBdWV+t3HK4r2ATfyEcMRzvzSflA1ABEBAAG0Jkpha2EgSHVkb2tsaW4g
    PGpha2FodWRva2xpbkBnbWFpbC5jb20+iQE+BBMBAgAoBQJRGNTxAhsDBQkB4TOA
    BgsJCAcDAgYVCAIJCgsEFgIDAQIeAQIXgAAKCRD6Zxi5hZclKnXNCACLOKa8abQp
    eTWv9SXUwC7LVM5pP2mXcgn+Ipqr6YWBdLx4Iij0YlvUok9VeKvwTpUlT+cx++o3
    wCM3AYrUyJE+zrtw49lInUmutz9seqJLU895oq+D+UuGoORrLBpEZrYR5f83uUmQ
    E3Z1ZmWrNGXYtITWDtVZD/KauMF2nkPcmy/XaYXhd4WHD81DGNlKtGAHig6A3Phc
    8Mr0A4yLDeRQJm8lCFEsxMJUNTupgY+ybbsMfVGx1gQvvGOTioV8CLCoRchUCCcm
    YPArFg40KzIDSNjwdo9EVZDnlPx1hbOppfQydxP+JVnZsqoYmVY4UhIWi/NfOl3V
    UMjl338INW1zuQENBFEY1PEBCADRSIfelOMjaTH7IfpMFFUc5Gys//njFnW9QAUg
    wyfs2AFxUp6vKQ7nxXQiJXVhKTwe9iqo+oGxaHp4AeTjC7vXsfMuF5g5lfttbAo3
    YEobEe6OG5so41nbwan6SyeIIQ2AmQqJBw8TKKMSec2qUN0Pw7iZRs0o9uJM/obG
    DPsAsMOQgNLxJyMCP7X2jBtDXxkMFVHMmk50Tl3h3Fi9qWuNxgTXjs0tUvKkXiu2
    Pco952jnm7HpCIKBek2pqR/UJXXb5qxy5G6Lc0qaMWZ5GKnSMTJmTY6Xl44EnaLK
    zh0rqgF9qpoWck470ZbiGASMtB008hy2l0cyxUfvDaS3tY4hABEBAAGJASUEGAEC
    AA8FAlEY1PECGwwFCQHhM4AACgkQ+mcYuYWXJSoT6AgAkvzvC0EGmeCR3cn9O3Gf
    yG00Kqk9/1gJvlphis7AAce8iUgU+4xd94Vp0u8rghpdy88xKN5lF1W2YZQmmBaf
    AVe6b7TOg6kxc3GKkVsWDxNyQKkpB46BwefIGaSljH7502X9aEWosrqWyJJNYCtt
    QDit4BysX0Ww3Ka5Rx6ZFhC9ybPKoW2i8JwpyBaXDt7R2k+PC/ClBf9qzL+sb2es
    zh/zCMVKNdm8KUITHU/5lgn2qZpUFZwiASPCMGGFP9u8g6UKeUTYTPD+GWaHIW63
    RAgNIAffxx0M1r3P/2ipkAdI3NX/1iBKDQNG8Odsf+BswFKrNCnyUDdLPvJAhODS
    gw==
    =tmrm
    -----END PGP PUBLIC KEY BLOCK-----

  7. Profpatsch commented on Mar 6, 2016

    @Profpatsch
    Member

    (triage) @offlinehacker do you plan on following through with that? Is it even necessary anymore?

  8. aragnon commented on Mar 16, 2016

    @aragnon

    In general, don't we just want to copy the same module set as other popular configuration management systems have? E.g. in Puppet, you have:

    https://docs.puppetlabs.com/puppet/latest/reference/type.html#file . When I see NixOS configuration management it sort of feels like one still needs to build all the basics before getting to the real work.

    From a UI point of view something like the following:

    dir."/exports/foo".status = "present";
    dir."/exports/foo".owner = "alice";
    

    seems to be the way to go. The advantages of this is that it leverages well known terminology and seems to be fairly consistent with how NixOS already does configuration.

  9. Profpatsch commented on Mar 18, 2016

    @Profpatsch
    Member

    I don’t quite understand what you are referring to, @aragnon. The case of systemd service files or sth. more general?

    Since no followup in over a week we can close @jagajaga

  10. aragnon commented on Mar 18, 2016

    @aragnon

    @Profpatsch Something more general; I don't think merely creating a directory should depend on systemd.

  11. Profpatsch commented on Mar 18, 2016

    @Profpatsch
    Member

    You are probably overcomplicating things. You can put any file you want into the store, trivially (look into pkgs/build-support/trivial-builders.
    And for etc there exists environment.etc, although it should be used very sparingly and only if that crude program behaviour cannot be patched out.

  12. aragnon commented on Mar 18, 2016

    @aragnon

    The store is /nix, so how do I specify an arbitrary location like /constant?

  13. Profpatsch commented on Mar 19, 2016

    @Profpatsch
    Member

    @aragnon Why do you want to do that? If you want to do anything on a package level, you should stay inside the store. If you want to do something elaborate on a module level (like having state files for services) you can create a new user that has its own home directory. See for example how I do it here, where I create a home in /var/lib.

  14. aragnon commented on Mar 20, 2016

    @aragnon

    @Profpatsch I want to setup an NFSv4 location at e.g. /nfs-export/constant.

  15. Profpatsch commented on Mar 20, 2016

    @Profpatsch
    Member

    close @zimbatm

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

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